From a888e44e88038d2c0338837d855f3f3d1906b710 Mon Sep 17 00:00:00 2001 From: Anton Bachin Date: Mon, 14 Oct 2024 20:00:57 +0300 Subject: [PATCH] Replace vendored httpaf with httpun (#351) --- .gitmodules | 16 --- dream-httpaf.opam | 27 ++--- src/http/adapt.ml | 13 +-- src/http/dune | 14 +-- src/http/error_handler.ml | 10 +- src/http/error_handler.mli | 8 +- src/http/http.ml | 58 ++++------ src/http/shared/dune | 2 +- src/http/shared/websocket.ml | 26 ++--- src/vendor/dune | 215 ----------------------------------- src/vendor/gluten | 1 - src/vendor/h2 | 1 - src/vendor/httpaf | 1 - src/vendor/paf | 1 - src/vendor/websocketaf | 1 - 15 files changed, 62 insertions(+), 332 deletions(-) delete mode 100644 .gitmodules delete mode 100644 src/vendor/dune delete mode 160000 src/vendor/gluten delete mode 160000 src/vendor/h2 delete mode 160000 src/vendor/httpaf delete mode 160000 src/vendor/paf delete mode 160000 src/vendor/websocketaf diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 23c0967f..00000000 --- a/.gitmodules +++ /dev/null @@ -1,16 +0,0 @@ -[submodule "src/vendor/httpaf"] - path = src/vendor/httpaf - url = https://github.com/aantron/httpaf.git -[submodule "src/vendor/gluten"] - path = src/vendor/gluten - url = https://github.com/aantron/gluten.git -[submodule "src/vendor/websocketaf"] - path = src/vendor/websocketaf - url = https://github.com/aantron/websocketaf.git -[submodule "src/vendor/h2"] - path = src/vendor/h2 - url = https://github.com/aantron/ocaml-h2.git -[submodule "src/vendor/paf"] - path = src/vendor/paf - url = https://github.com/aantron/paf-le-chien.git - branch = dream diff --git a/dream-httpaf.opam b/dream-httpaf.opam index 57afb7f1..ae04e78e 100644 --- a/dream-httpaf.opam +++ b/dream-httpaf.opam @@ -15,31 +15,18 @@ maintainer: "Anton Bachin " depends: [ "dream-pure" "dune" {>= "2.7.0"} # --instrument-with. + "gluten" + "gluten-lwt-unix" + "h2" {< "0.13.0"} + "h2-lwt-unix" + "httpun" {< "0.2.0"} + "httpun-lwt-unix" + "httpun-ws" "lwt" "lwt_ppx" {>= "1.2.2"} "lwt_ssl" "ocaml" {>= "4.08.0"} "ssl" {>= "0.5.8"} # Ssl.get_negotiated_alpn_protocol. - - # Currently vendored. - # "gluten" - # "gluten-lwt-unix" - # "httpaf" - # "httpaf-lwt-unix" - # "h2" - # "h2-lwt-unix" - # "hpack" - # "websocketaf" - - # Dependencies of vendored packages. - "angstrom" {>= "0.14.0"} - "base64" {>= "3.0.0"} - "bigstringaf" {>= "0.5.0"} # h2. - "digestif" {>= "0.7.2"} # websocket/af, sha1, default implementation. - "faraday" {>= "0.6.1"} - "faraday-lwt-unix" - "lwt_ssl" {>= "1.2.0"} # Gluten. - "psq" # h2. ] build: [ diff --git a/src/http/adapt.ml b/src/http/adapt.ml index 8f822c0b..c6bd416e 100644 --- a/src/http/adapt.ml +++ b/src/http/adapt.ml @@ -5,9 +5,6 @@ -module Httpaf = Dream_httpaf_.Httpaf -module H2 = Dream_h2.H2 - module Stream = Dream_pure.Stream module Message = Dream_pure.Message @@ -71,14 +68,14 @@ let forward_body_general let forward_body (response : Message.response) - (body : Httpaf.Body.Writer.t) = + (body : Httpun.Body.Writer.t) = forward_body_general response - (Httpaf.Body.Writer.write_string body) - (Httpaf.Body.Writer.write_bigstring body) - (Httpaf.Body.Writer.flush body) - (fun _code -> Httpaf.Body.Writer.close body) + (Httpun.Body.Writer.write_string body) + (Httpun.Body.Writer.write_bigstring body) + (Httpun.Body.Writer.flush body) + (fun _code -> Httpun.Body.Writer.close body) let forward_body_h2 (response : Message.response) diff --git a/src/http/dune b/src/http/dune index 76ddb65e..e50cdba0 100644 --- a/src/http/dune +++ b/src/http/dune @@ -8,17 +8,17 @@ dream-pure dream.server dream-httpaf - dream-httpaf.dream-gluten - dream-httpaf.dream-gluten-lwt-unix - dream-httpaf.dream-h2 - dream-httpaf.dream-h2-lwt-unix - dream-httpaf.dream-httpaf_ - dream-httpaf.dream-httpaf_-lwt-unix + gluten + gluten-lwt-unix + h2 + h2-lwt-unix + httpun + httpun-lwt-unix + httpun-ws lwt lwt.unix lwt_ssl ssl - dream-httpaf.dream-websocketaf ) (preprocess (pps lwt_ppx)) (instrumentation (backend bisect_ppx))) diff --git a/src/http/error_handler.ml b/src/http/error_handler.ml index d371ea75..59b2bde8 100644 --- a/src/http/error_handler.ml +++ b/src/http/error_handler.ml @@ -5,10 +5,6 @@ -module Httpaf = Dream_httpaf_.Httpaf -module H2 = Dream_h2.H2 -module Websocketaf = Dream_websocketaf.Websocketaf - module Catch = Dream__server.Catch module Error_template = Dream__server.Error_template module Method = Dream_pure.Method @@ -270,7 +266,7 @@ let httpaf user's_error_handler = fun client_address ?request error start_response -> - ignore (request : Httpaf.Request.t option); + ignore (request : Httpun.Request.t option); (* TODO LATER Should factor out the request translation function and use it to partially recover the request info. *) @@ -314,7 +310,7 @@ let httpaf | None -> default_response caused_by in - let headers = Httpaf.Headers.of_list (Message.all_headers response) in + let headers = Httpun.Headers.of_list (Message.all_headers response) in let body = start_response headers in Adapt.forward_body response body; @@ -417,7 +413,7 @@ let websocket (* Note: in this function, request and response are from the original request that negotiated the websocket. *) - Websocketaf.Wsd.close socket; + Httpun_ws.Wsd.close socket; (* The only constructor of error is `Exn, so presumably these are server-side errors. Not sure if any I/O errors are possible here. *) diff --git a/src/http/error_handler.mli b/src/http/error_handler.mli index f2a06390..1f7b1160 100644 --- a/src/http/error_handler.mli +++ b/src/http/error_handler.mli @@ -5,10 +5,6 @@ -module Httpaf = Dream_httpaf_.Httpaf -module H2 = Dream_h2.H2 -module Websocketaf = Dream_websocketaf.Websocketaf - module Catch = Dream__server.Catch module Log = Dream__server.Log module Message = Dream_pure.Message @@ -43,7 +39,7 @@ val app : val httpaf : Catch.error_handler -> - (Unix.sockaddr -> Httpaf.Server_connection.error_handler) + (Unix.sockaddr -> Httpun.Server_connection.error_handler) val h2 : Catch.error_handler -> @@ -57,7 +53,7 @@ val websocket : Catch.error_handler -> Message.request -> Message.response -> - (Websocketaf.Wsd.t -> [ `Exn of exn ] -> unit) + (Httpun_ws.Wsd.t -> [ `Exn of exn ] -> unit) val websocket_handshake : Catch.error_handler -> diff --git a/src/http/http.ml b/src/http/http.ml index d0779839..677b4859 100644 --- a/src/http/http.ml +++ b/src/http/http.ml @@ -5,14 +5,6 @@ -module Gluten = Dream_gluten.Gluten -module Gluten_lwt_unix = Dream_gluten_lwt_unix.Gluten_lwt_unix -module Httpaf = Dream_httpaf_.Httpaf -module Httpaf_lwt_unix = Dream_httpaf__lwt_unix.Httpaf_lwt_unix -module H2 = Dream_h2.H2 -module H2_lwt_unix = Dream_h2_lwt_unix.H2_lwt_unix -module Websocketaf = Dream_websocketaf.Websocketaf - module Catch = Dream__server.Catch module Helpers = Dream__server.Helpers module Log = Dream__server.Log @@ -28,10 +20,10 @@ module Stream = Dream_pure.Stream let to_dream_method method_ = - Httpaf.Method.to_string method_ |> Method.string_to_method + Httpun.Method.to_string method_ |> Method.string_to_method -let to_httpaf_status status = - Status.status_to_int status |> Httpaf.Status.of_code +let to_httpun_status status = + Status.status_to_int status |> Httpun.Status.of_code let to_h2_status status = Status.status_to_int status |> H2.Status.of_code @@ -82,8 +74,8 @@ let wrap_handler let conn, upgrade = conn.reqd, conn.upgrade in (* Covert the http/af request to a Dream request. *) - let httpaf_request : Httpaf.Request.t = - Httpaf.Reqd.request conn in + let httpaf_request : Httpun.Request.t = + Httpun.Reqd.request conn in let client = Adapt.address_to_string client_address in @@ -92,22 +84,22 @@ let wrap_handler let target = httpaf_request.target in let headers = - Httpaf.Headers.to_list httpaf_request.headers in + Httpun.Headers.to_list httpaf_request.headers in let body = - Httpaf.Reqd.request_body conn in + Httpun.Reqd.request_body conn in (* TODO Review per-chunk allocations. *) (* TODO Should the stream be auto-closed? It doesn't even have a closed state. The whole thing is just a wrapper for whatever the http/af behavior is. *) let read ~data ~flush:_ ~ping:_ ~pong:_ ~close ~exn:_ = - Httpaf.Body.Reader.schedule_read + Httpun.Body.Reader.schedule_read body ~on_eof:(fun () -> close 1000) ~on_read:(fun buffer ~off ~len -> data buffer off len true false) in let close _code = - Httpaf.Body.Reader.close body in + Httpun.Body.Reader.close body in let body = Stream.reader ~read ~close ~abort:close in let body = @@ -119,7 +111,7 @@ let wrap_handler set_fd request unix_socket; (* Call the user's handler. If it raises an exception or returns a promise - that rejects with an exception, pass the exception up to Httpaf. This + that rejects with an exception, pass the exception up to Httpun. This will cause it to call its (low-level) error handler with variand `Exn _. A well-behaved Dream app should catch all of its own exceptions and rejections in one of its top-level middlewares. @@ -146,15 +138,15 @@ let wrap_handler Message.set_content_length_headers response; let headers = - Httpaf.Headers.of_list (Message.all_headers response) in + Httpun.Headers.of_list (Message.all_headers response) in let status = - to_httpaf_status (Message.status response) in + to_httpun_status (Message.status response) in - let httpaf_response = - Httpaf.Response.create ~headers status in + let httpun_response = + Httpun.Response.create ~headers status in let body = - Httpaf.Reqd.respond_with_streaming conn httpaf_response in + Httpun.Reqd.respond_with_streaming conn httpun_response in Adapt.forward_body response body; @@ -167,19 +159,19 @@ let wrap_handler | Some (client_stream, _server_stream) -> let error_handler = Error_handler.websocket user's_error_handler request response in + ignore error_handler; (* TODO *) let proceed () = - Websocketaf.Server_connection.create_websocket - ~error_handler + Httpun_ws.Server_connection.create_websocket (Dream_httpaf.Websocket.websocket_handler client_stream) - |> Gluten.make (module Websocketaf.Server_connection) + |> Gluten.make (module Httpun_ws.Server_connection) |> upgrade in let headers = - Httpaf.Headers.of_list (Message.all_headers response) in + Httpun.Headers.of_list (Message.all_headers response) in - Websocketaf.Handshake.respond_with_upgrade ~headers ~sha1 conn proceed + Httpun_ws.Handshake.respond_with_upgrade ~headers ~sha1 conn proceed |> function | Ok () -> Lwt.return_unit | Error error_string -> @@ -192,7 +184,7 @@ let wrap_handler @@ fun exn -> (* TODO There was something in the fork changelogs about not requiring report exn. Is it relevant to this? *) - Httpaf.Reqd.report_exn conn exn; + Httpun.Reqd.report_exn conn exn; Lwt.return_unit end in @@ -325,7 +317,7 @@ let no_tls = { let request_handler = wrap_handler false error_handler handler in let error_handler = Error_handler.httpaf error_handler in fun client_address unix_socket -> - Httpaf_lwt_unix.Server.create_connection_handler + Httpun_lwt_unix.Server.create_connection_handler ?config:None ~request_handler:(request_handler unix_socket) ~error_handler @@ -344,7 +336,7 @@ let openssl = { let request_handler = wrap_handler true error_handler handler in let error_handler = Error_handler.httpaf error_handler in fun client_address unix_socket tls_endpoint -> - Httpaf_lwt_unix.Server.SSL.create_connection_handler + Httpun_lwt_unix.Server.SSL.create_connection_handler ?config:None ~request_handler:(request_handler unix_socket) ~error_handler @@ -411,7 +403,7 @@ let ocaml_tls = { let request_handler = wrap_handler true error_handler handler in let error_handler = Error_handler.httpaf error_handler in fun client_address unix_socket -> - Httpaf_lwt_unix.Server.TLS.create_connection_handler_with_default + Httpun_lwt_unix.Server.TLS.create_connection_handler_with_default ~certfile:certificate_file ~keyfile:key_file ?config:None ~request_handler:(request_handler unix_socket) @@ -551,7 +543,7 @@ let serve_with_maybe_https log.warning (fun log -> log "Consider using Dream.to_base64url (Dream.random 32)"); end; *) - (* TODO Make sure there is a similar check in cipher.ml now.Hpack *) + (* TODO Make sure there is a similar check in cipher.ml now. *) match tls with | `No -> diff --git a/src/http/shared/dune b/src/http/shared/dune index 31b3c06a..80de5563 100644 --- a/src/http/shared/dune +++ b/src/http/shared/dune @@ -4,7 +4,7 @@ (libraries bigstringaf dream-pure - dream-httpaf.dream-websocketaf + httpun-ws ) (preprocess (pps lwt_ppx)) (instrumentation (backend bisect_ppx))) diff --git a/src/http/shared/websocket.ml b/src/http/shared/websocket.ml index 0f51d26e..babf92e6 100644 --- a/src/http/shared/websocket.ml +++ b/src/http/shared/websocket.ml @@ -5,8 +5,6 @@ -module Websocketaf = Dream_websocketaf.Websocketaf - module Stream = Dream_pure.Stream @@ -42,7 +40,7 @@ let websocket_handler stream socket = push_frame (Some (`Data (!message_is_binary, is_fin), payload)) in - let eof () = + let eof ?error:_ () = push_frame None in (* The reader retrieves the next frame. If it is a data frame, it keeps a @@ -66,7 +64,7 @@ let websocket_handler stream socket = let first_chunk_offset = ref 0 in let first_chunk_length = ref 0 in let rec drain_payload payload continuation = - Websocketaf.Payload.schedule_read + Httpun_ws.Payload.schedule_read payload ~on_read:(fun buffer ~off ~len -> if not !first_chunk_received then begin @@ -106,7 +104,7 @@ let websocket_handler stream socket = closed := true; close_code := 1005 end; - Websocketaf.Wsd.close socket; + Httpun_ws.Wsd.close socket; close !close_code | Some (`Close, payload) -> drain_payload payload @@ fun buffer offset length -> @@ -138,7 +136,7 @@ let websocket_handler stream socket = read ~data ~flush ~ping ~pong ~close ~exn end | Some ((binary, fin), payload) -> - Websocketaf.Payload.schedule_read + Httpun_ws.Payload.schedule_read payload ~on_read:(fun buffer ~off ~len -> match !last_chunk with @@ -167,7 +165,7 @@ let websocket_handler stream socket = if !closed then close !close_code else - Websocketaf.Wsd.flushed socket ok + Httpun_ws.Wsd.flushed socket ok in let close code = @@ -175,7 +173,7 @@ let websocket_handler stream socket = (* TODO Really need to work out the "close handshake" and how it is exposed in the Stream API. *) (* closed := true; *) - Websocketaf.Wsd.close ~code:(`Other code) socket + Httpun_ws.Wsd.close ~code:(`Other code) socket end in @@ -192,7 +190,7 @@ let websocket_handler stream socket = if !closed then close !close_code else begin - Websocketaf.Wsd.schedule + Httpun_ws.Wsd.schedule socket ~is_fin:fin ~kind buffer ~off:offset ~len:length; bytes_since_flush := !bytes_since_flush + length; if !bytes_since_flush >= 4096 then @@ -208,9 +206,9 @@ let websocket_handler stream socket = close !close_code else begin if length = 0 then - Websocketaf.Wsd.send_ping socket + Httpun_ws.Wsd.send_ping socket else - Websocketaf.Wsd.send_ping + Httpun_ws.Wsd.send_ping ~application_data:{Faraday.buffer; off = offset; len = length} socket; outgoing_loop () @@ -224,9 +222,9 @@ let websocket_handler stream socket = close !close_code else begin if length = 0 then - Websocketaf.Wsd.send_pong socket + Httpun_ws.Wsd.send_pong socket else - Websocketaf.Wsd.send_pong + Httpun_ws.Wsd.send_pong ~application_data:{Faraday.buffer; off = offset; len = length} socket; outgoing_loop () @@ -236,4 +234,4 @@ let websocket_handler stream socket = in outgoing_loop (); - Websocketaf.Websocket_connection.{frame; eof} + Httpun_ws.Websocket_connection.{frame; eof} diff --git a/src/vendor/dune b/src/vendor/dune deleted file mode 100644 index 17aca4cf..00000000 --- a/src/vendor/dune +++ /dev/null @@ -1,215 +0,0 @@ -(data_only_dirs *) - - - -(subdir gluten/lib - (library - (name dream_gluten) - (public_name dream-httpaf.dream-gluten) - (libraries - bigstringaf - faraday - ))) - -(subdir gluten/lwt - (library - (name dream_gluten_lwt) - (public_name dream-httpaf.dream-gluten-lwt) - (libraries - dream-httpaf.dream-gluten - lwt - ))) - -(subdir gluten/lwt-unix - (library - (name dream_gluten_lwt_unix) - (public_name dream-httpaf.dream-gluten-lwt-unix) - (libraries - faraday-lwt-unix - dream-httpaf.dream-gluten-lwt - lwt.unix - (select - ssl_io.ml - from - (lwt_ssl -> ssl_io.real.ml) - (-> ssl_io.dummy.ml)) - (select - tls_io.ml - from - (tls.lwt -> tls_io.real.ml) - (-> tls_io.dummy.ml)) - ) - (modules gluten_lwt_unix tls_io ssl_io))) - - - -(subdir httpaf/lib - (library - (name dream_httpaf_) - (public_name dream-httpaf.dream-httpaf_) - (libraries - angstrom - bigstringaf - faraday - ))) - -(subdir httpaf/lwt - (library - (name dream_httpaf__lwt) - (public_name dream-httpaf.dream-httpaf_-lwt) - (libraries - dream-httpaf.dream-gluten - dream-httpaf.dream-gluten-lwt - dream-httpaf.dream-httpaf_ - lwt - ))) - -(subdir httpaf/lwt-unix - (library - (name dream_httpaf__lwt_unix) - (public_name dream-httpaf.dream-httpaf_-lwt-unix) - (libraries - faraday-lwt-unix - dream-httpaf.dream-gluten-lwt-unix - dream-httpaf.dream-httpaf_ - dream-httpaf.dream-httpaf_-lwt - lwt.unix - ))) - - - -(subdir websocketaf/lib - (library - (name dream_websocketaf) - (public_name dream-httpaf.dream-websocketaf) - (libraries - angstrom - base64 - bigstringaf - faraday - dream-httpaf.dream-gluten - dream-httpaf.dream-httpaf_ - ))) - -(subdir websocketaf/lwt - (library - (name dream_websocketaf_lwt) - (public_name dream-httpaf.dream-websocketaf-lwt) - (libraries - base64 - digestif.ocaml - dream-httpaf.dream-gluten-lwt - lwt - dream-httpaf.dream-websocketaf - ))) - -(subdir websocketaf/lwt-unix - (library - (name dream_websocketaf_lwt_unix) - (public_name dream-httpaf.dream-websocketaf-lwt-unix) - (libraries - faraday-lwt-unix - dream-httpaf.dream-gluten-lwt-unix - lwt.unix - dream-httpaf.dream-websocketaf-lwt - ))) - - - -(subdir h2/hpack/util - (executables - (names gen_huffman gen_static) - (libraries - compiler-libs.common - ))) - -(subdir h2/hpack/src - (library - (name dream_hpack) - (public_name dream-httpaf.dream-hpack) - (libraries - angstrom - faraday - )) - (rule - (targets huffman_table.ml) - (deps ../util/huffman_table.txt) - (action - (with-stdout-to - %{targets} - (run ../util/gen_huffman.exe %{deps}))))) - -(subdir h2/lib - (library - (name dream_h2) - (public_name dream-httpaf.dream-h2) - (libraries - angstrom - base64 - bigstringaf - faraday - dream-httpaf.dream-hpack - dream-httpaf.dream-httpaf_ - psq - ))) - -(subdir h2/lwt - (library - (name dream_h2_lwt) - (public_name dream-httpaf.dream-h2-lwt) - (libraries - dream-httpaf.dream-gluten - dream-httpaf.dream-gluten-lwt - lwt - dream-httpaf.dream-h2 - ))) - -(subdir h2/lwt-unix - (library - (name dream_h2_lwt_unix) - (public_name dream-httpaf.dream-h2-lwt-unix) - (libraries - faraday-lwt-unix - dream-httpaf.dream-gluten-lwt-unix - dream-httpaf.dream-h2 - dream-httpaf.dream-h2-lwt - lwt.unix - ))) - - - -(subdir paf/lib - (library - (name dream_paf) - (public_name dream-mirage.dream-paf) - (modules paf) - (libraries - faraday - bigstringaf - ke - mimic - ))) - -(subdir paf/lib - (library - (name dream_alpn) - (public_name dream-mirage.dream-paf.alpn) - (modules alpn) - (libraries - dream-mirage.dream-paf - dream-httpaf.dream-httpaf_ - dream-httpaf.dream-h2 - ))) - -(subdir paf/lib - (library - (name dream_paf_mirage) - (public_name dream-mirage.dream-paf.mirage) - (modules paf_mirage) - (libraries - tcpip - dream-mirage.dream-paf - tls-mirage - mirage-time - dream-mirage.dream-paf.alpn - ))) diff --git a/src/vendor/gluten b/src/vendor/gluten deleted file mode 160000 index 59e630be..00000000 --- a/src/vendor/gluten +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 59e630be097c8c48c4bfaf3df28ca8f40d1a333f diff --git a/src/vendor/h2 b/src/vendor/h2 deleted file mode 160000 index 3ce5740e..00000000 --- a/src/vendor/h2 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3ce5740ecf7c92c892d5d20684483935ebb5b2e8 diff --git a/src/vendor/httpaf b/src/vendor/httpaf deleted file mode 160000 index 94254931..00000000 --- a/src/vendor/httpaf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 94254931d6e9efd4d304683ba7d1d4e001a506c5 diff --git a/src/vendor/paf b/src/vendor/paf deleted file mode 160000 index 26f47fae..00000000 --- a/src/vendor/paf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 26f47faef8fc15ffd17971b4e01de0108245679e diff --git a/src/vendor/websocketaf b/src/vendor/websocketaf deleted file mode 160000 index aacd79aa..00000000 --- a/src/vendor/websocketaf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit aacd79aafbb5b375b1514a76260adb48c1d70eb8