Skip to content

Commit

Permalink
Replace vendored httpaf with httpun (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron authored Oct 14, 2024
1 parent c8ec9d1 commit a888e44
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 332 deletions.
16 changes: 0 additions & 16 deletions .gitmodules

This file was deleted.

27 changes: 7 additions & 20 deletions dream-httpaf.opam
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,18 @@ maintainer: "Anton Bachin <[email protected]>"
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: [
Expand Down
13 changes: 5 additions & 8 deletions src/http/adapt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@



module Httpaf = Dream_httpaf_.Httpaf
module H2 = Dream_h2.H2

module Stream = Dream_pure.Stream
module Message = Dream_pure.Message

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions src/http/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
10 changes: 3 additions & 7 deletions src/http/error_handler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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. *)

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. *)
Expand Down
8 changes: 2 additions & 6 deletions src/http/error_handler.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand All @@ -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 ->
Expand Down
58 changes: 25 additions & 33 deletions src/http/http.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 =
Expand All @@ -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.
Expand All @@ -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;

Expand All @@ -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 ->
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 ->
Expand Down
2 changes: 1 addition & 1 deletion src/http/shared/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(libraries
bigstringaf
dream-pure
dream-httpaf.dream-websocketaf
httpun-ws
)
(preprocess (pps lwt_ppx))
(instrumentation (backend bisect_ppx)))
Loading

0 comments on commit a888e44

Please sign in to comment.