From 1b790ad9c0954b2801b71ac422492bc123ca758b Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Thu, 27 Oct 2022 13:21:24 +0200 Subject: [PATCH 1/6] stream the response body --- src/http_mirage_client.ml | 123 ++++++++++++++++++------------------- src/http_mirage_client.mli | 6 +- 2 files changed, 63 insertions(+), 66 deletions(-) diff --git a/src/http_mirage_client.ml b/src/http_mirage_client.ml index 1938e28..230cc54 100644 --- a/src/http_mirage_client.ml +++ b/src/http_mirage_client.ml @@ -153,7 +153,7 @@ let prepare_http_1_1_headers headers host user_pass body_length = | Some v -> add headers "content-length" (string_of_int v) in add_authentication ~add headers user_pass -let single_http_1_1_request ?config flow user_pass host meth path headers body = +let single_http_1_1_request ?config flow user_pass host meth path headers body f f_init = let body_length = Option.map String.length body in let headers = prepare_http_1_1_headers headers host user_pass body_length in let req = Httpaf.Request.create ~headers meth path in @@ -163,34 +163,28 @@ let single_http_1_1_request ?config flow user_pass host meth path headers body = fun v -> if not !w then Lwt.wakeup_later notify_finished v ; w := true in + let on_eof response data () = + let response = + { + version= response.Httpaf.Response.version + ; status= (response.Httpaf.Response.status :> H2.Status.t) + ; reason= response.Httpaf.Response.reason + ; headers= + H2.Headers.of_list + (Httpaf.Headers.to_list response.Httpaf.Response.headers) + } in + wakeup (Ok (response, data)) + in let response_handler response body = - let buf = Buffer.create 0x100 in - let rec on_eof () = - let response = - { - version= response.Httpaf.Response.version - ; status= (response.Httpaf.Response.status :> H2.Status.t) - ; reason= response.Httpaf.Response.reason - ; headers= - H2.Headers.of_list - (Httpaf.Headers.to_list response.Httpaf.Response.headers) - } in - wakeup (Ok (response, Some (Buffer.contents buf))) - and on_read ba ~off ~len = - Buffer.add_string buf (Bigstringaf.substring ~off ~len ba) - ; Httpaf.Body.schedule_read body ~on_read ~on_eof in - let on_eof () = - let response = - { - version= response.Httpaf.Response.version - ; status= (response.Httpaf.Response.status :> H2.Status.t) - ; reason= response.Httpaf.Response.reason - ; headers= - H2.Headers.of_list - (Httpaf.Headers.to_list response.Httpaf.Response.headers) - } in - wakeup (Ok (response, None)) in - Httpaf.Body.schedule_read body ~on_read ~on_eof in + let rec on_read on_eof data ba ~off ~len = + let data = + data >>= fun data -> + f data (Bigstringaf.substring ~off ~len ba) + in + Httpaf.Body.schedule_read body ~on_read:(on_read on_eof data) ~on_eof:(on_eof response data) + in + let f_init = Lwt.return f_init in + Httpaf.Body.schedule_read body ~on_read:(on_read on_eof f_init) ~on_eof:(on_eof response f_init) in let error_handler e = let err = match e with @@ -216,7 +210,7 @@ let prepare_h2_headers headers host user_pass body_length = (string_of_int (Option.value ~default:0 body_length)) in add_authentication ~add headers user_pass -let single_h2_request ?config ~scheme flow user_pass host meth path headers body +let single_h2_request ?config ~scheme flow user_pass host meth path headers body f f_init = let body_length = Option.map String.length body in let headers = prepare_h2_headers headers host user_pass body_length in @@ -227,30 +221,26 @@ let single_h2_request ?config ~scheme flow user_pass host meth path headers body fun v -> if not !w then Lwt.wakeup_later notify_finished v ; w := true in + let on_eof response data () = + let response = + { + version= {major= 2; minor= 0} + ; status= response.H2.Response.status + ; reason= "" + ; headers= response.H2.Response.headers + } in + wakeup (Ok (response, data)) + in let response_handler response response_body = - let buf = Buffer.create 0x100 in - let rec on_eof () = - let response = - { - version= {major= 2; minor= 0} - ; status= response.H2.Response.status - ; reason= "" - ; headers= response.H2.Response.headers - } in - wakeup (Ok (response, Some (Buffer.contents buf))) - and on_read ba ~off ~len = - Buffer.add_string buf (Bigstringaf.substring ~off ~len ba) - ; H2.Body.Reader.schedule_read response_body ~on_read ~on_eof in - let on_eof () = - let response = - { - version= {major= 2; minor= 0} - ; status= response.H2.Response.status - ; reason= "" - ; headers= response.H2.Response.headers - } in - wakeup (Ok (response, None)) in - H2.Body.Reader.schedule_read response_body ~on_read ~on_eof in + let rec on_read on_eof data ba ~off ~len = + let data = + data >>= fun data -> + f data (Bigstringaf.substring ~off ~len ba) + in + H2.Body.Reader.schedule_read response_body ~on_read:(on_read on_eof data) ~on_eof:(on_eof response data) + in + let f_init = Lwt.return f_init in + H2.Body.Reader.schedule_read response_body ~on_read:(on_read on_eof f_init) ~on_eof:(on_eof response f_init) in let error_handler e = let err = match e with @@ -319,7 +309,7 @@ let alpn_protocol_of_string = function | "h2" -> Some `H2 | _ -> None -let single_request ~ctx ~alpn_protocol ?config cfg ~meth ~headers ?body uri = +let single_request ~ctx ~alpn_protocol ?config cfg ~meth ~headers ?body uri f f_init = Lwt.return (decode_uri ~ctx uri) >>? fun (ctx, scheme, host, user_pass, path) -> let ctx = @@ -333,19 +323,22 @@ let single_request ~ctx ~alpn_protocol ?config cfg ~meth ~headers ?body uri = Mimic.resolve ctx >>? fun flow -> (match Option.bind (alpn_protocol flow) alpn_protocol_of_string, config with | (Some `HTTP_1_1 | None), Some (`HTTP_1_1 config) -> - single_http_1_1_request ~config flow user_pass host meth path headers body + single_http_1_1_request ~config flow user_pass host meth path headers body f f_init | (Some `HTTP_1_1 | None), None -> - single_http_1_1_request flow user_pass host meth path headers body + single_http_1_1_request flow user_pass host meth path headers body f f_init | (Some `H2 | None), Some (`H2 config) -> - single_h2_request ~config ~scheme flow user_pass host meth path headers body + single_h2_request ~config ~scheme flow user_pass host meth path headers body f f_init | Some `H2, None -> - single_h2_request ~scheme flow user_pass host meth path headers body + single_h2_request ~scheme flow user_pass host meth path headers body f f_init | Some `H2, Some (`HTTP_1_1 _) -> - single_h2_request ~scheme flow user_pass host meth path headers body + single_h2_request ~scheme flow user_pass host meth path headers body f f_init | Some `HTTP_1_1, Some (`H2 _) -> - single_http_1_1_request flow user_pass host meth path headers body) + single_http_1_1_request flow user_pass host meth path headers body f f_init) >>= fun r -> - Mimic.close flow >|= fun () -> r + Mimic.close flow >>= fun () -> + match r with + | Error _ as e -> Lwt.return e + | Ok (resp, body) -> Lwt.map (fun body -> Ok (resp, body)) body let tls_config ?tls_config ?config authenticator user's_authenticator = lazy @@ -378,7 +371,7 @@ let resolve_location ~uri ~location = | _ -> Error (`Msg ("expected an absolute uri, got: " ^ uri))) | _ -> Error (`Msg ("unknown location (relative path): " ^ location)) -let one_request +let request ?config ?tls_config:cfg {ctx; alpn_protocol; authenticator} @@ -388,18 +381,20 @@ let one_request ?body ?(max_redirect = 5) ?(follow_redirect = true) - uri = + uri + f + f_init = let tls_config = tls_config ?tls_config:cfg ?config authenticator user's_authenticator in if not follow_redirect then single_request ~ctx ~alpn_protocol ?config tls_config ~meth ~headers ?body - uri + uri f f_init else let rec follow_redirect count uri = if count = 0 then Lwt.return_error (`Msg "Redirect limit exceeded") else single_request ~ctx ~alpn_protocol ?config tls_config ~meth ~headers - ?body uri + ?body uri f f_init >>? fun (resp, body) -> if Status.is_redirection resp.status then match Headers.get resp.headers "location" with diff --git a/src/http_mirage_client.mli b/src/http_mirage_client.mli index 2baf154..e15da90 100644 --- a/src/http_mirage_client.mli +++ b/src/http_mirage_client.mli @@ -22,7 +22,7 @@ type response = { ; headers: Headers.t } -val one_request : +val request : ?config:[ `H2 of H2.Config.t | `HTTP_1_1 of Httpaf.Config.t ] -> ?tls_config:Tls.Config.client -> t @@ -33,4 +33,6 @@ val one_request : -> ?max_redirect:int -> ?follow_redirect:bool -> string - -> (response * string option, [> Mimic.error ]) result Lwt.t + -> ('a -> string -> 'a Lwt.t) + -> 'a + -> (response * 'a, [> Mimic.error ]) result Lwt.t From df099590c9f3517c272ae9fbef0b4dadbd6e60af Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Thu, 27 Oct 2022 20:51:42 +0200 Subject: [PATCH 2/6] provide response in f --- src/http_mirage_client.ml | 32 ++++++++++++++++---------------- src/http_mirage_client.mli | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/http_mirage_client.ml b/src/http_mirage_client.ml index 230cc54..aac285b 100644 --- a/src/http_mirage_client.ml +++ b/src/http_mirage_client.ml @@ -164,6 +164,9 @@ let single_http_1_1_request ?config flow user_pass host meth path headers body f if not !w then Lwt.wakeup_later notify_finished v ; w := true in let on_eof response data () = + wakeup (Ok (response, data)) + in + let response_handler response body = let response = { version= response.Httpaf.Response.version @@ -173,15 +176,12 @@ let single_http_1_1_request ?config flow user_pass host meth path headers body f H2.Headers.of_list (Httpaf.Headers.to_list response.Httpaf.Response.headers) } in - wakeup (Ok (response, data)) - in - let response_handler response body = - let rec on_read on_eof data ba ~off ~len = - let data = - data >>= fun data -> - f data (Bigstringaf.substring ~off ~len ba) + let rec on_read on_eof acc ba ~off ~len = + let acc = + acc >>= fun acc -> + f response acc (Bigstringaf.substring ~off ~len ba) in - Httpaf.Body.schedule_read body ~on_read:(on_read on_eof data) ~on_eof:(on_eof response data) + Httpaf.Body.schedule_read body ~on_read:(on_read on_eof acc) ~on_eof:(on_eof response acc) in let f_init = Lwt.return f_init in Httpaf.Body.schedule_read body ~on_read:(on_read on_eof f_init) ~on_eof:(on_eof response f_init) in @@ -222,6 +222,9 @@ let single_h2_request ?config ~scheme flow user_pass host meth path headers body if not !w then Lwt.wakeup_later notify_finished v ; w := true in let on_eof response data () = + wakeup (Ok (response, data)) + in + let response_handler response response_body = let response = { version= {major= 2; minor= 0} @@ -229,15 +232,12 @@ let single_h2_request ?config ~scheme flow user_pass host meth path headers body ; reason= "" ; headers= response.H2.Response.headers } in - wakeup (Ok (response, data)) - in - let response_handler response response_body = - let rec on_read on_eof data ba ~off ~len = - let data = - data >>= fun data -> - f data (Bigstringaf.substring ~off ~len ba) + let rec on_read on_eof acc ba ~off ~len = + let acc = + acc >>= fun acc -> + f response acc (Bigstringaf.substring ~off ~len ba) in - H2.Body.Reader.schedule_read response_body ~on_read:(on_read on_eof data) ~on_eof:(on_eof response data) + H2.Body.Reader.schedule_read response_body ~on_read:(on_read on_eof acc) ~on_eof:(on_eof response acc) in let f_init = Lwt.return f_init in H2.Body.Reader.schedule_read response_body ~on_read:(on_read on_eof f_init) ~on_eof:(on_eof response f_init) in diff --git a/src/http_mirage_client.mli b/src/http_mirage_client.mli index e15da90..6ba8ae6 100644 --- a/src/http_mirage_client.mli +++ b/src/http_mirage_client.mli @@ -33,6 +33,6 @@ val request : -> ?max_redirect:int -> ?follow_redirect:bool -> string - -> ('a -> string -> 'a Lwt.t) + -> (response -> 'a -> string -> 'a Lwt.t) -> 'a -> (response * 'a, [> Mimic.error ]) result Lwt.t From a03e4c1e61ef503e6ae93685d35c77136749dd2d Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Thu, 27 Oct 2022 20:59:49 +0200 Subject: [PATCH 3/6] adjust documentation --- README.md | 3 ++- src/http_mirage_client.mli | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a447342..f52a206 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,8 @@ open Lwt.Infix module Make (HTTP_client : Http_mirage_client.S) = struct let start http_client = - Http_mirage_client.one_request http_client "https://mirage.io/" + let body_f _response acc data = Lwt.return (acc ^ data) in + Http_mirage_client.one_request http_client "https://mirage.io/" body_f "" >>= function | Ok (resp, body) -> ... | Error _ -> ... diff --git a/src/http_mirage_client.mli b/src/http_mirage_client.mli index 6ba8ae6..f0ef229 100644 --- a/src/http_mirage_client.mli +++ b/src/http_mirage_client.mli @@ -36,3 +36,8 @@ val request : -> (response -> 'a -> string -> 'a Lwt.t) -> 'a -> (response * 'a, [> Mimic.error ]) result Lwt.t +(** [request ~config ~tls_config t ~authenticator ~meth ~headers ~body + ~max_redirect ~follow_redirect url body_f body_init] does a HTTP request + to [url] using [meth] and the HTTP protocol in [config]. The response is + the value of this function. The body is provided in chunks (see [body_f]). + Reasonably defaults are used if not provided. *) From b68d101c1246c0095ab051626d7ffb3ee5d2c3fa Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Fri, 28 Oct 2022 16:43:19 +0200 Subject: [PATCH 4/6] Apply ocamlformat --- src/http_mirage_client.ml | 50 ++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/http_mirage_client.ml b/src/http_mirage_client.ml index aac285b..97c6669 100644 --- a/src/http_mirage_client.ml +++ b/src/http_mirage_client.ml @@ -153,7 +153,8 @@ let prepare_http_1_1_headers headers host user_pass body_length = | Some v -> add headers "content-length" (string_of_int v) in add_authentication ~add headers user_pass -let single_http_1_1_request ?config flow user_pass host meth path headers body f f_init = +let single_http_1_1_request + ?config flow user_pass host meth path headers body f f_init = let body_length = Option.map String.length body in let headers = prepare_http_1_1_headers headers host user_pass body_length in let req = Httpaf.Request.create ~headers meth path in @@ -163,9 +164,7 @@ let single_http_1_1_request ?config flow user_pass host meth path headers body f fun v -> if not !w then Lwt.wakeup_later notify_finished v ; w := true in - let on_eof response data () = - wakeup (Ok (response, data)) - in + let on_eof response data () = wakeup (Ok (response, data)) in let response_handler response body = let response = { @@ -178,13 +177,13 @@ let single_http_1_1_request ?config flow user_pass host meth path headers body f } in let rec on_read on_eof acc ba ~off ~len = let acc = - acc >>= fun acc -> - f response acc (Bigstringaf.substring ~off ~len ba) + acc >>= fun acc -> f response acc (Bigstringaf.substring ~off ~len ba) in - Httpaf.Body.schedule_read body ~on_read:(on_read on_eof acc) ~on_eof:(on_eof response acc) - in + Httpaf.Body.schedule_read body ~on_read:(on_read on_eof acc) + ~on_eof:(on_eof response acc) in let f_init = Lwt.return f_init in - Httpaf.Body.schedule_read body ~on_read:(on_read on_eof f_init) ~on_eof:(on_eof response f_init) in + Httpaf.Body.schedule_read body ~on_read:(on_read on_eof f_init) + ~on_eof:(on_eof response f_init) in let error_handler e = let err = match e with @@ -210,8 +209,8 @@ let prepare_h2_headers headers host user_pass body_length = (string_of_int (Option.value ~default:0 body_length)) in add_authentication ~add headers user_pass -let single_h2_request ?config ~scheme flow user_pass host meth path headers body f f_init - = +let single_h2_request + ?config ~scheme flow user_pass host meth path headers body f f_init = let body_length = Option.map String.length body in let headers = prepare_h2_headers headers host user_pass body_length in let req = H2.Request.create ~scheme ~headers meth path in @@ -221,9 +220,7 @@ let single_h2_request ?config ~scheme flow user_pass host meth path headers body fun v -> if not !w then Lwt.wakeup_later notify_finished v ; w := true in - let on_eof response data () = - wakeup (Ok (response, data)) - in + let on_eof response data () = wakeup (Ok (response, data)) in let response_handler response response_body = let response = { @@ -234,13 +231,13 @@ let single_h2_request ?config ~scheme flow user_pass host meth path headers body } in let rec on_read on_eof acc ba ~off ~len = let acc = - acc >>= fun acc -> - f response acc (Bigstringaf.substring ~off ~len ba) + acc >>= fun acc -> f response acc (Bigstringaf.substring ~off ~len ba) in - H2.Body.Reader.schedule_read response_body ~on_read:(on_read on_eof acc) ~on_eof:(on_eof response acc) - in + H2.Body.Reader.schedule_read response_body ~on_read:(on_read on_eof acc) + ~on_eof:(on_eof response acc) in let f_init = Lwt.return f_init in - H2.Body.Reader.schedule_read response_body ~on_read:(on_read on_eof f_init) ~on_eof:(on_eof response f_init) in + H2.Body.Reader.schedule_read response_body ~on_read:(on_read on_eof f_init) + ~on_eof:(on_eof response f_init) in let error_handler e = let err = match e with @@ -309,7 +306,8 @@ let alpn_protocol_of_string = function | "h2" -> Some `H2 | _ -> None -let single_request ~ctx ~alpn_protocol ?config cfg ~meth ~headers ?body uri f f_init = +let single_request + ~ctx ~alpn_protocol ?config cfg ~meth ~headers ?body uri f f_init = Lwt.return (decode_uri ~ctx uri) >>? fun (ctx, scheme, host, user_pass, path) -> let ctx = @@ -323,15 +321,19 @@ let single_request ~ctx ~alpn_protocol ?config cfg ~meth ~headers ?body uri f f_ Mimic.resolve ctx >>? fun flow -> (match Option.bind (alpn_protocol flow) alpn_protocol_of_string, config with | (Some `HTTP_1_1 | None), Some (`HTTP_1_1 config) -> - single_http_1_1_request ~config flow user_pass host meth path headers body f f_init + single_http_1_1_request ~config flow user_pass host meth path headers body f + f_init | (Some `HTTP_1_1 | None), None -> single_http_1_1_request flow user_pass host meth path headers body f f_init | (Some `H2 | None), Some (`H2 config) -> - single_h2_request ~config ~scheme flow user_pass host meth path headers body f f_init + single_h2_request ~config ~scheme flow user_pass host meth path headers body + f f_init | Some `H2, None -> - single_h2_request ~scheme flow user_pass host meth path headers body f f_init + single_h2_request ~scheme flow user_pass host meth path headers body f + f_init | Some `H2, Some (`HTTP_1_1 _) -> - single_h2_request ~scheme flow user_pass host meth path headers body f f_init + single_h2_request ~scheme flow user_pass host meth path headers body f + f_init | Some `HTTP_1_1, Some (`H2 _) -> single_http_1_1_request flow user_pass host meth path headers body f f_init) >>= fun r -> From cb7e10479f9ff831c1232b47dc70f2e7799c9a44 Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Mon, 31 Oct 2022 13:16:41 +0100 Subject: [PATCH 5/6] A a simple test --- test/dune | 9 ++++ test/test.ml | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 test/dune create mode 100644 test/test.ml diff --git a/test/dune b/test/dune new file mode 100644 index 0000000..34b428b --- /dev/null +++ b/test/dune @@ -0,0 +1,9 @@ +(executable + (name test) + (libraries http-mirage-client tcpip.stack-socket paf.mirage + mirage-clock-unix mirage-random-stdlib happy-eyeballs-lwt mirage-time-unix + mimic-happy-eyeballs alcotest-lwt)) + +(rule + (alias runtest) + (action (run ./test.exe --color=always))) diff --git a/test/test.ml b/test/test.ml new file mode 100644 index 0000000..8ff98c0 --- /dev/null +++ b/test/test.ml @@ -0,0 +1,119 @@ +(* Functoria *) + +module DNS_client = Dns_client_mirage.Make (Mirage_random_stdlib) (Time) (Mclock) (Pclock) (Tcpip_stack_socket.V4V6) +module Happy_eyeballs = Happy_eyeballs_mirage.Make (Time) (Mclock) (Tcpip_stack_socket.V4V6) (DNS_client) +module Mimic_happy_eyeballs = Mimic_happy_eyeballs.Make (Tcpip_stack_socket.V4V6) (DNS_client) (Happy_eyeballs) +module HTTP_server = Paf_mirage.Make (Tcpip_stack_socket.V4V6.TCP) +module HTTP_client = Http_mirage_client.Make (Pclock) (Tcpip_stack_socket.V4V6.TCP) (Mimic_happy_eyeballs) + +let http_1_1_error_handler ?notify (ipaddr, port) ?request:_ error respond = + let contents = match error with + | `Bad_gateway -> Fmt.str "Bad gateway (%a:%d)" Ipaddr.pp ipaddr port + | `Bad_request -> Fmt.str "Bad request (%a:%d)" Ipaddr.pp ipaddr port + | `Exn exn -> Fmt.str "Exception %S (%a:%d)" (Printexc.to_string exn) Ipaddr.pp ipaddr port + | `Internal_server_error -> Fmt.str "Internal server error (%a:%d)" Ipaddr.pp ipaddr port in + let open Httpaf in + Option.iter (fun push -> push (Some ((ipaddr, port), error))) notify ; + let headers = Headers.of_list + [ "content-type", "text/plain" + ; "content-length", string_of_int (String.length contents) + ; "connection", "close" ] in + let body = respond headers in + Body.write_string body contents ; + Body.close_writer body + +let alpn_error_handler + : type reqd headers request response ro wo. + ?notify:(((Ipaddr.t * int) * Alpn.server_error) option -> unit) -> + (Ipaddr.t * int) -> (reqd, headers, request, response, ro, wo) Alpn.protocol -> + ?request:request -> Alpn.server_error -> (headers -> wo) -> unit + = fun ?notify (ipaddr, port) protocol ?request:_ error respond -> + let contents = match error with + | `Bad_gateway -> Fmt.str "Bad gateway (%a:%d)" Ipaddr.pp ipaddr port + | `Bad_request -> Fmt.str "Bad request (%a:%d)" Ipaddr.pp ipaddr port + | `Exn exn -> Fmt.str "Exception %S (%a:%d)" (Printexc.to_string exn) Ipaddr.pp ipaddr port + | `Internal_server_error -> Fmt.str "Internal server error (%a:%d)" Ipaddr.pp ipaddr port in + Option.iter (fun push -> push (Some ((ipaddr, port), error))) notify ; + let headers = + [ "content-type", "text/plain" + ; "content-length", string_of_int (String.length contents) ] in + match protocol with + | Alpn.HTTP_1_1 _ -> + let open Httpaf in + let headers = Headers.of_list (("connection", "close") :: headers) in + let body = respond headers in + Body.write_string body contents ; + Body.close_writer body + | Alpn.H2 _ -> + let open H2 in + let headers = Headers.of_list headers in + let body = respond headers in + H2.Body.Writer.write_string body contents ; + H2.Body.Writer.close body + +type alpn_handler = + { handler : 'reqd 'headers 'request 'response 'ro 'wo. + 'reqd -> ('reqd, 'headers, 'request, 'response, 'ro, 'wo) Alpn.protocol -> unit } +[@@unboxed] + +let server ?error ?stop stack = function + | `HTTP_1_1 (port, handler) -> + let open Lwt.Syntax in + let+ http_server = HTTP_server.init ~port stack in + let http_service = HTTP_server.http_service ~error_handler:(http_1_1_error_handler ?notify:error) + (fun _flow (_ipaddr, _port) -> handler) in + HTTP_server.serve ?stop http_service http_server + | `ALPN (tls, port, handler) -> + let open Lwt.Syntax in + let alpn_handler = + { Alpn.error= (fun edn protocol ?request v respond -> alpn_error_handler ?notify:error edn protocol ?request v respond) + ; Alpn.request= (fun _flow (_ipaddr, _port) reqd protocol -> handler.handler reqd protocol) } in + let+ http_server = HTTP_server.init ~port stack in + let alpn_service = HTTP_server.alpn_service ~tls alpn_handler in + HTTP_server.serve ?stop alpn_service http_server + +let stack ipaddr = + let open Lwt.Syntax in + let* tcpv4v6 = Tcpip_stack_socket.V4V6.TCP.connect ~ipv4_only:false ~ipv6_only:false + ipaddr None in + let* udpv4v6 = Tcpip_stack_socket.V4V6.UDP.connect ~ipv4_only:false ~ipv6_only:false + ipaddr None in + Tcpip_stack_socket.V4V6.connect udpv4v6 tcpv4v6 + +let test01 = + Alcotest_lwt.test_case "Simple Hello World! (GET)" `Quick @@ fun _sw () -> + let open Lwt.Syntax in + let stop = Lwt_switch.create () in + let handler reqd = + let open Httpaf in + let contents = "Hello World!" in + let headers = Headers.of_list + [ "content-type", "text/plain" + ; "content-length", string_of_int (String.length contents) + ; "connection", "close" ] in + let response = Response.create ~headers `OK in + Reqd.respond_with_string reqd response contents in + let* stack = stack Ipaddr.V4.Prefix.loopback in + let happy_eyeballs = Happy_eyeballs.create stack in + let* ctx = Mimic_happy_eyeballs.connect happy_eyeballs in + let* t = HTTP_client.connect ctx in + let* `Initialized _thread = server ~stop (Tcpip_stack_socket.V4V6.tcp stack) + (`HTTP_1_1 (8080, handler)) in + let* result = Http_mirage_client.request t "http://localhost:8080/" + (fun _response buf str -> Buffer.add_string buf str ; Lwt.return buf) + (Buffer.create 0x100) in + match result with + | Error err -> + let* () = Lwt_switch.turn_off stop in + let* () = Tcpip_stack_socket.V4V6.disconnect stack in + Alcotest.failf "Client error: %a" Mimic.pp_error err + | Ok (_response, buf) -> + let* () = Lwt_switch.turn_off stop in + let* () = Tcpip_stack_socket.V4V6.disconnect stack in + let body = Buffer.contents buf in + Alcotest.(check string) "body" "Hello World!" body ; + Lwt.return_unit + + let () = Alcotest_lwt.run "http-mirage-client" + [ "simple", [ test01 ] ] + |> Lwt_main.run From e3fda50bb534970e1771b49b046913acd5530469 Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Mon, 31 Oct 2022 15:30:42 +0100 Subject: [PATCH 6/6] Add a test about POST request --- test/dune | 3 ++- test/test.ml | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/test/dune b/test/dune index 34b428b..09e6f9e 100644 --- a/test/dune +++ b/test/dune @@ -6,4 +6,5 @@ (rule (alias runtest) - (action (run ./test.exe --color=always))) + (action + (run ./test.exe --color=always))) diff --git a/test/test.ml b/test/test.ml index 8ff98c0..508ecd0 100644 --- a/test/test.ml +++ b/test/test.ml @@ -113,7 +113,55 @@ let test01 = let body = Buffer.contents buf in Alcotest.(check string) "body" "Hello World!" body ; Lwt.return_unit + +let random_string ~len = + let res = Bytes.create len in + for i = 0 to len - 1 do + Bytes.set res i (Char.chr (Random.bits () land 0xff)) + done ; Bytes.unsafe_to_string res + +let test02 = + Alcotest_lwt.test_case "Repeat (POST)" `Quick @@ fun _sw () -> + let open Lwt.Syntax in + let stop = Lwt_switch.create () in + let handler reqd = + let open Httpaf in + let { Request.meth; _ } = Reqd.request reqd in + if meth <> `POST then invalid_arg "Invalid HTTP method" ; + let headers = Headers.of_list + [ "content-type", "text/plain" ] in + let response = Response.create ~headers `OK in + let src = Reqd.request_body reqd in + let dst = Reqd.respond_with_streaming reqd response in + let rec on_eof () = + Body.close_reader src ; + Body.close_writer dst + and on_read buf ~off ~len = + Body.write_bigstring dst ~off ~len buf ; + Body.schedule_read src ~on_eof ~on_read in + Body.schedule_read src ~on_eof ~on_read in + let* stack = stack Ipaddr.V4.Prefix.loopback in + let happy_eyeballs = Happy_eyeballs.create stack in + let* ctx = Mimic_happy_eyeballs.connect happy_eyeballs in + let* t = HTTP_client.connect ctx in + let* `Initialized _thread = server ~stop (Tcpip_stack_socket.V4V6.tcp stack) + (`HTTP_1_1 (8080, handler)) in + let str = random_string ~len:0x1000 in + let* result = Http_mirage_client.request ~meth:`POST ~body:str t "http://localhost:8080/" + (fun _response buf str -> Buffer.add_string buf str ; Lwt.return buf) + (Buffer.create 0x1000) in + match result with + | Error err -> + let* () = Lwt_switch.turn_off stop in + let* () = Tcpip_stack_socket.V4V6.disconnect stack in + Alcotest.failf "Client error: %a" Mimic.pp_error err + | Ok (_response, buf) -> + let* () = Lwt_switch.turn_off stop in + let* () = Tcpip_stack_socket.V4V6.disconnect stack in + let body = Buffer.contents buf in + Alcotest.(check string) "body" str body ; + Lwt.return_unit let () = Alcotest_lwt.run "http-mirage-client" - [ "simple", [ test01 ] ] + [ "http/1.1", [ test01; test02 ] ] |> Lwt_main.run