1313from proxy .types import Ok
1414from proxy .imports import types
1515from proxy .imports .types import (
16- Method_Get , Method_Post , Scheme , Scheme_Http , Scheme_Https , Scheme_Other , IncomingRequest , ResponseOutparam ,
17- OutgoingResponse , Fields , OutgoingBody , OutgoingRequest
16+ Method_Get ,
17+ Method_Post ,
18+ Scheme ,
19+ Scheme_Http ,
20+ Scheme_Https ,
21+ Scheme_Other ,
22+ IncomingRequest ,
23+ ResponseOutparam ,
24+ OutgoingResponse ,
25+ Fields ,
26+ OutgoingBody ,
27+ OutgoingRequest ,
1828)
1929from poll_loop import Stream , Sink , PollLoop
2030from typing import Tuple
2131from urllib import parse
2232
33+
2334class IncomingHandler (exports .IncomingHandler ):
2435 """Implements the `export`ed portion of the `wasi-http` `proxy` world."""
2536
26- def handle (self , request : IncomingRequest , response_out : ResponseOutparam ):
27- """Handle the specified `request`, sending the response to `response_out`.
28-
29- """
37+ def handle (self , request : IncomingRequest , response_out : ResponseOutparam ) -> None :
38+ """Handle the specified `request`, sending the response to `response_out`."""
3039 # Dispatch the request using `asyncio`, backed by a custom event loop
3140 # based on WASI's `poll_oneoff` function.
3241 loop = PollLoop ()
3342 asyncio .set_event_loop (loop )
3443 loop .run_until_complete (handle_async (request , response_out ))
3544
36- async def handle_async (request : IncomingRequest , response_out : ResponseOutparam ):
45+
46+ async def handle_async (
47+ request : IncomingRequest , response_out : ResponseOutparam
48+ ) -> None :
3749 """Handle the specified `request`, sending the response to `response_out`."""
3850
3951 method = request .method ()
@@ -46,7 +58,10 @@ async def handle_async(request: IncomingRequest, response_out: ResponseOutparam)
4658 # buffering the response bodies), and stream the results back to the
4759 # client as they become available.
4860
49- urls = map (lambda pair : str (pair [1 ], "utf-8" ), filter (lambda pair : pair [0 ] == "url" , headers ))
61+ urls = map (
62+ lambda pair : str (pair [1 ], "utf-8" ),
63+ filter (lambda pair : pair [0 ] == "url" , headers ),
64+ )
5065
5166 response = OutgoingResponse (Fields .from_list ([("content-type" , b"text/plain" )]))
5267
@@ -64,7 +79,11 @@ async def handle_async(request: IncomingRequest, response_out: ResponseOutparam)
6479 elif isinstance (method , Method_Post ) and path == "/echo" :
6580 # Echo the request body back to the client without buffering.
6681
67- response = OutgoingResponse (Fields .from_list (list (filter (lambda pair : pair [0 ] == "content-type" , headers ))))
82+ response = OutgoingResponse (
83+ Fields .from_list (
84+ list (filter (lambda pair : pair [0 ] == "content-type" , headers ))
85+ )
86+ )
6887
6988 response_body = response .body ()
7089
@@ -87,6 +106,7 @@ async def handle_async(request: IncomingRequest, response_out: ResponseOutparam)
87106 ResponseOutparam .set (response_out , Ok (response ))
88107 OutgoingBody .finish (body , None )
89108
109+
90110async def sha256 (url : str ) -> Tuple [str , str ]:
91111 """Download the contents of the specified URL, computing the SHA-256
92112 incrementally as the response body arrives.
0 commit comments