1 USING: accessors continuations http http.server
2 http.server.requests io.encodings.ascii io.servers io.sockets
3 io.streams.limited kernel namespaces ;
7 ! individual connection stuff
8 TUPLE: http2-stream ; ! do I even need this?
10 TUPLE: http2-connection streams settings hpack-decode-context
11 hpack-encode-context ;
13 CONSTANT: client-connection-prefix "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
15 : start-http2-connection ( threaded-server prev-req/f -- )
17 ! TODO: establish http2 connection and carry out requests
18 ! send settings frame.
19 ! listen for connection prefix and settings from client.
20 ! save settings and send ack.
24 TUPLE: http2-server < http-server ;
26 ! stack effect: ( threaded-server -- )
27 M: http2-server handle-client*
28 ! check if this is a secure connection or not
30 request-limit get limited-input
31 secure-addr dup port>> local-address get port>> = and
32 [ t ! get tls(1.2?) negotiated thing: replace with get_alpn_selected
33 [ f start-http2-connection ] ! if h2, send prefix and start full http2
34 [ call-next-method ] ! else, revert to http1?
36 [ ! first, check if the thing sent is connection prefix, and
37 ! if so, start connection
38 ! this line should check for the connection prefix, but
39 ! seems to mess up the stream for when the the request is
40 ! read in read-request.
41 f ! 24 input-stream get <peek-stream> stream-peek client-connection-prefix =
42 [ f start-http2-connection ]
45 [ read-request ] ?benchmark
48 [ start-http2-connection ] ! if so, send 101 switching protocols response, start http2,
49 ! including sending prefix and response to initial request.
51 ! else, finish processing as http1.
53 [ do-request ] ?benchmark
54 [ do-response ] ?benchmark
57 [ nip handle-client-error ] recover
63 : <http2-server> ( -- server )
64 ascii http2-server new-threaded-server
66 "http" protocol-port >>insecure
67 "https" protocol-port >>secure ;