diff --git a/async/httpaf_async.ml b/async/httpaf_async.ml index accd3eec..6e32fa9c 100644 --- a/async/httpaf_async.ml +++ b/async/httpaf_async.ml @@ -178,6 +178,7 @@ module Client = struct |> ignore; reader_thread () end + | `Close when Client_connection.is_persistent conn -> () | `Close -> (* Log.Global.printf "read_close(%d)%!" (Fd.to_int_exn fd); *) Ivar.fill read_complete (); diff --git a/lib/client_connection.ml b/lib/client_connection.ml index 443aad5e..208a6168 100644 --- a/lib/client_connection.ml +++ b/lib/client_connection.ml @@ -53,6 +53,7 @@ module Oneshot = struct ; reader : Reader.response ; writer : Writer.t ; state : state ref + ; mutable is_persistent: bool ; mutable error_code : [ `Ok | error ] } @@ -81,6 +82,7 @@ module Oneshot = struct ; error_code = `Ok ; reader = Reader.response ~request_method handler ; writer + ; is_persistent = false ; state } in Writer.write_request t.writer request; @@ -152,12 +154,17 @@ module Oneshot = struct let _next_read_operation t = match !(t.state) with | Awaiting_response | Closed -> Reader.next t.reader - | Received_response(_, response_body) -> + | Received_response(response, response_body) -> if not (Body.Reader.is_closed response_body) then Reader.next t.reader else begin Reader.force_close t.reader; - Reader.next t.reader + match Reader.next t.reader with + | `Read -> assert false + | `Error _ as err -> err + | `Close -> + t.is_persistent <- Response.persistent_connection response; + `Close end ;; @@ -215,4 +222,6 @@ module Oneshot = struct Writer.report_result t.writer result let is_closed t = Reader.is_closed t.reader && Writer.is_closed t.writer + + let is_persistent t = t.is_persistent end diff --git a/lib/httpaf.mli b/lib/httpaf.mli index 25fd3499..d821f40f 100644 --- a/lib/httpaf.mli +++ b/lib/httpaf.mli @@ -837,6 +837,9 @@ module Client_connection : sig val is_closed : t -> bool + val is_persistent : t -> bool + (** [is_persistent t] is [true] if the last response indicated a keep-alive connection *) + (**/**) val shutdown : t -> unit (**/**) diff --git a/lwt-unix/httpaf_lwt_unix.ml b/lwt-unix/httpaf_lwt_unix.ml index 545fbc6a..9784a2c7 100644 --- a/lwt-unix/httpaf_lwt_unix.ml +++ b/lwt-unix/httpaf_lwt_unix.ml @@ -230,6 +230,7 @@ module Client = struct read_loop_step () end + | `Close when Client_connection.is_persistent connection -> Lwt.return_unit | `Close -> Lwt.wakeup_later notify_read_loop_exited (); if not (Lwt_unix.state socket = Lwt_unix.Closed) then begin