]> gitweb.factorcode.org Git - factor.git/blob - extra/http2/server/server.factor
factor: trim using lists
[factor.git] / extra / http2 / server / server.factor
1 USING: accessors continuations http http.server
2 http.server.requests io.encodings.ascii io.servers io.sockets
3 io.streams.limited kernel namespaces ;
4
5 IN: http2.server
6
7 ! individual connection stuff
8 TUPLE: http2-stream ; ! do I even need this?
9
10 TUPLE: http2-connection streams settings hpack-decode-context
11 hpack-encode-context ;
12
13 CONSTANT: client-connection-prefix "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
14
15 : start-http2-connection ( threaded-server prev-req/f -- )
16     2drop
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.  
21     ;
22
23 ! the server stuff
24 TUPLE: http2-server < http-server ;
25
26 ! stack effect: ( threaded-server -- )
27 M: http2-server handle-client*
28     ! check if this is a secure connection or not
29     ?refresh-all
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?
35       if ] ! secure case
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 ]
43       [ 
44         [
45           [ read-request ] ?benchmark 
46           dup "Upgrade" header 
47           "h2c" =
48           [ start-http2-connection ] ! if so, send 101 switching protocols response, start http2,
49           ! including sending prefix and response to initial request.
50           [ 
51             ! else, finish processing as http1.
52             nip
53             [ do-request ] ?benchmark 
54             [ do-response ] ?benchmark 
55           ] if 
56         ]
57         [ nip handle-client-error ] recover 
58         ]
59       if ] ! insecure case
60     if
61     ;
62
63 : <http2-server> ( -- server )
64     ascii http2-server new-threaded-server
65         "http2.server" >>name
66         "http" protocol-port >>insecure
67         "https" protocol-port >>secure ;
68