]> gitweb.factorcode.org Git - factor.git/commitdiff
HTTP/2 Server Framework (#2457)
authorDavid Mindlin <davidmindlin4@gmail.com>
Tue, 4 May 2021 17:04:54 +0000 (10:04 -0700)
committerGitHub <noreply@github.com>
Tue, 4 May 2021 17:04:54 +0000 (10:04 -0700)
* Framework for http2 server.

* Added some pseudocode to http2 servers.

* added start connection word to http2 server

* add a check for the http secure connection or not

* Added more framework and pseudocode.

* Quick fix to hpack context for http2.

* defaulting to http/1 in insecure case

* fixed misplaced bracket

* insecure no upgrade case

* moved handle-client-error to more appropriate place

* added word to create server

* Commented out line for checking for connection prefix, as it messes up
the stream.

Co-authored-by: David Flores <dflores0818@gmail.com>
basis/http2/server/server.factor [new file with mode: 0644]

diff --git a/basis/http2/server/server.factor b/basis/http2/server/server.factor
new file mode 100644 (file)
index 0000000..f066e96
--- /dev/null
@@ -0,0 +1,68 @@
+USING: accessors continuations http http.server http.server.requests
+io io.encodings.ascii io.servers io.sockets io.streams.peek
+io.streams.limited kernel namespaces openssl.libssl ; 
+
+IN: http2.server
+
+! individual connection stuff
+TUPLE: http2-stream ; ! do I even need this?
+
+TUPLE: http2-connection streams settings hpack-decode-context
+hpack-encode-context ;
+
+CONSTANT: client-connection-prefix "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
+
+: start-http2-connection ( threaded-server prev-req/f -- )
+    2drop
+    ! TODO: establish http2 connection and carry out requests
+    ! send settings frame.
+    ! listen for connection prefix and settings from client.
+    ! save settings and send ack.  
+    ;
+
+! the server stuff
+TUPLE: http2-server < http-server ;
+
+! stack effect: ( threaded-server -- )
+M: http2-server handle-client*
+    ! check if this is a secure connection or not
+    ?refresh-all
+    request-limit get limited-input
+    secure-addr dup port>> local-address get port>> = and
+    [ t ! get tls(1.2?) negotiated thing: replace with get_alpn_selected
+      [ f start-http2-connection ] ! if h2, send prefix and start full http2
+      [ call-next-method ] ! else, revert to http1?
+      if ] ! secure case
+    [ ! first, check if the thing sent is connection prefix, and
+      ! if so, start connection
+      ! this line should check for the connection prefix, but
+      ! seems to mess up the stream for when the the request is
+      ! read in read-request.
+      f ! 24 input-stream get <peek-stream> stream-peek client-connection-prefix =
+      [ f start-http2-connection ]
+      [ 
+        [
+          [ read-request ] ?benchmark 
+          dup "Upgrade" header 
+          "h2c" =
+          [ start-http2-connection ] ! if so, send 101 switching protocols response, start http2,
+          ! including sending prefix and response to initial request.
+          [ 
+            ! else, finish processing as http1.
+            nip
+            [ do-request ] ?benchmark 
+            [ do-response ] ?benchmark 
+          ] if 
+        ]
+        [ nip handle-client-error ] recover 
+        ]
+      if ] ! insecure case
+    if
+    ;
+
+: <http2-server> ( -- server )
+    ascii http2-server new-threaded-server
+        "http2.server" >>name
+        "http" protocol-port >>insecure
+        "https" protocol-port >>secure ;
+