Skip to content

Commit

Permalink
Merge pull request #85 from inhabitedtype/remove-handle-type-param
Browse files Browse the repository at this point in the history
remove-handle-type-param: approach has been abandoned
  • Loading branch information
seliopou authored Jan 12, 2019
2 parents dd9a412 + 4e51248 commit 1bd293e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 49 deletions.
4 changes: 2 additions & 2 deletions async/httpaf_async.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Httpaf
module Server : sig
val create_connection_handler
: ?config : Config.t
-> request_handler : ('a -> Fd.t Server_connection.request_handler)
-> request_handler : ('a -> Server_connection.request_handler)
-> error_handler : ('a -> Server_connection.error_handler)
-> ([< Socket.Address.t] as 'a)
-> ([`Active], 'a) Socket.t
Expand All @@ -15,7 +15,7 @@ end

module Client : sig
val request
: ?config : Config.t
: ?config : Config.t
-> ([`Active], [< Socket.Address.t]) Socket.t
-> Request.t
-> error_handler : Client_connection.error_handler
Expand Down
50 changes: 25 additions & 25 deletions lib/httpaf.mli
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,13 @@ end

(** {2 Request Descriptor} *)
module Reqd : sig
type 'handle t
type t

val request : _ t -> Request.t
val request_body : _ t -> [`read] Body.t
val request : t -> Request.t
val request_body : t -> [`read] Body.t

val response : _ t -> Response.t option
val response_exn : _ t -> Response.t
val response : t -> Response.t option
val response_exn : t -> Response.t

(** Responding
Expand All @@ -679,14 +679,14 @@ module Reqd : sig
See {{:https://tools.ietf.org/html/rfc7230#section-6.3} RFC7230§6.3} for
more details. *)

val respond_with_string : _ t -> Response.t -> string -> unit
val respond_with_bigstring : _ t -> Response.t -> Bigstring.t -> unit
val respond_with_streaming : ?flush_headers_immediately:bool -> _ t -> Response.t -> [`write] Body.t
val respond_with_string : t -> Response.t -> string -> unit
val respond_with_bigstring : t -> Response.t -> Bigstring.t -> unit
val respond_with_streaming : ?flush_headers_immediately:bool -> t -> Response.t -> [`write] Body.t

(** Exception Handling *)

val report_exn : _ t -> exn -> unit
val try_with : _ t -> (unit -> unit) -> (unit, exn) result
val report_exn : t -> exn -> unit
val try_with : t -> (unit -> unit) -> (unit, exn) result
end

(** {2 Buffer Size Configuration} *)
Expand All @@ -706,56 +706,56 @@ end
(** {2 Server Connection} *)

module Server_connection : sig
type 'handle t
type t

type error =
[ `Bad_request | `Bad_gateway | `Internal_server_error | `Exn of exn ]

type 'handle request_handler = 'handle Reqd.t -> unit
type request_handler = Reqd.t -> unit

type error_handler =
?request:Request.t -> error -> (Headers.t -> [`write] Body.t) -> unit

val create
: ?config:Config.t
-> ?error_handler:error_handler
-> 'handle request_handler
-> 'handle t
-> request_handler
-> t
(** [create ?config ?error_handler ~request_handler] creates a connection
handler that will service individual requests with [request_handler]. *)

val next_read_operation : _ t -> [ `Read | `Yield | `Close ]
val next_read_operation : t -> [ `Read | `Yield | `Close ]
(** [next_read_operation t] returns a value describing the next operation
that the caller should conduct on behalf of the connection. *)

val read : _ t -> Bigstring.t -> off:int -> len:int -> int
val read : t -> Bigstring.t -> off:int -> len:int -> int
(** [read t bigstring ~off ~len] reads bytes of input from the provided range
of [bigstring] and returns the number of bytes consumed by the
connection. {!read} should be called after {!next_read_operation}
returns a [`Read] value and additional input is available for the
connection to consume. *)

val read_eof : _ t -> Bigstring.t -> off:int -> len:int -> int
val read_eof : t -> Bigstring.t -> off:int -> len:int -> int
(** [read t bigstring ~off ~len] reads bytes of input from the provided range
of [bigstring] and returns the number of bytes consumed by the
connection. {!read} should be called after {!next_read_operation}
returns a [`Read] and an EOF has been received from the communication
channel. The connection will attempt to consume any buffered input and
then shutdown the HTTP parser for the connection. *)

val yield_reader : _ t -> (unit -> unit) -> unit
val yield_reader : t -> (unit -> unit) -> unit
(** [yield_reader t continue] registers with the connection to call
[continue] when reading should resume. {!yield_reader} should be called
after {next_read_operation} returns a [`Yield] value. *)

val next_write_operation : _ t -> [
val next_write_operation : t -> [
| `Write of Bigstring.t IOVec.t list
| `Yield
| `Close of int ]
(** [next_write_operation t] returns a value describing the next operation
that the caller should conduct on behalf of the connection. *)

val report_write_result : _ t -> [`Ok of int | `Closed] -> unit
val report_write_result : t -> [`Ok of int | `Closed] -> unit
(** [report_write_result t result] reports the result of the latest write
attempt to the connection. {report_write_result} should be called after a
call to {next_write_operation} that returns a [`Write buffer] value.
Expand All @@ -767,29 +767,29 @@ module Server_connection : sig
{- [`Closed] indicates that the output destination will no longer
accept bytes from the write processor. }} *)

val yield_writer : _ t -> (unit -> unit) -> unit
val yield_writer : t -> (unit -> unit) -> unit
(** [yield_writer t continue] registers with the connection to call
[continue] when writing should resume. {!yield_writer} should be called
after {next_write_operation} returns a [`Yield] value. *)

val report_exn : _ t -> exn -> unit
val report_exn : t -> exn -> unit
(** [report_exn t exn] reports that an error [exn] has been caught and
that it has been attributed to [t]. Calling this function will switch [t]
into an error state. Depending on the state [t] is transitioning from, it
may call its error handler before terminating the connection. *)

val is_closed : _ t -> bool
val is_closed : t -> bool
(** [is_closed t] is [true] if both the read and write processors have been
shutdown. When this is the case {!next_read_operation} will return
[`Close _] and {!next_write_operation} will do the same will return a
[`Write _] until all buffered output has been flushed. *)

val error_code : _ t -> error option
val error_code : t -> error option
(** [error_code t] returns the [error_code] that caused the connection to
close, if one exists. *)

(**/**)
val shutdown : _ t -> unit
val shutdown : t -> unit
(**/**)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/reqd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
type error =
[ `Bad_request | `Bad_gateway | `Internal_server_error | `Exn of exn ]

type 'handle response_state =
type response_state =
| Waiting of (unit -> unit) ref
| Complete of Response.t
| Streaming of Response.t * [`write] Body.t
Expand Down Expand Up @@ -67,14 +67,14 @@ module Writer = Serialize.Writer
* ]}
*
* *)
type 'handle t =
type t =
{ request : Request.t
; request_body : [`read] Body.t
; writer : Writer.t
; response_body_buffer : Bigstring.t
; error_handler : error_handler
; mutable persistent : bool
; mutable response_state : 'handle response_state
; mutable response_state : response_state
; mutable error_code : [`Ok | error ]
; mutable wait_for_first_flush : bool
}
Expand Down
8 changes: 4 additions & 4 deletions lib/server_connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ module Reader = Parse.Reader
module Writer = Serialize.Writer


type 'fd request_handler = 'fd Reqd.t -> unit
type request_handler = Reqd.t -> unit

type error =
[ `Bad_gateway | `Bad_request | `Internal_server_error | `Exn of exn]

type error_handler =
?request:Request.t -> error -> (Headers.t -> [`write] Body.t) -> unit

type 'fd t =
type t =
{ reader : Reader.request
; writer : Writer.t
; response_body_buffer : Bigstring.t
; request_handler : 'fd request_handler
; request_handler : request_handler
; error_handler : error_handler
; request_queue : 'fd Reqd.t Queue.t
; request_queue : Reqd.t Queue.t
(* invariant: If [request_queue] is not empty, then the head of the queue
has already had [request_handler] called on it. *)
; wakeup_writer : (unit -> unit) list ref
Expand Down
3 changes: 0 additions & 3 deletions lwt/httpaf_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ let shutdown socket command =
module Config = Httpaf.Config

module Server = struct
type request_handler =
Lwt_unix.file_descr Httpaf.Server_connection.request_handler

let create_connection_handler ?(config=Config.default) ~request_handler ~error_handler =
fun client_addr socket ->
let module Server_connection = Httpaf.Server_connection in
Expand Down
26 changes: 14 additions & 12 deletions lwt/httpaf_lwt.mli
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
open Httpaf


(* The function that results from [create_connection_handler] should be passed
to [Lwt_io.establish_server_with_client_socket]. For an example, see
[examples/lwt_echo_server.ml]. *)
module Server : sig
type request_handler =
Lwt_unix.file_descr Httpaf.Server_connection.request_handler

val create_connection_handler
: ?config : Httpaf.Config.t
-> request_handler : (Unix.sockaddr -> request_handler)
-> error_handler : (Unix.sockaddr -> Httpaf.Server_connection.error_handler)
-> (Unix.sockaddr -> Lwt_unix.file_descr -> unit Lwt.t)
: ?config : Config.t
-> request_handler : (Unix.sockaddr -> Server_connection.request_handler)
-> error_handler : (Unix.sockaddr -> Server_connection.error_handler)
-> Unix.sockaddr
-> Lwt_unix.file_descr
-> unit Lwt.t
end

(* For an example, see [examples/lwt_get.ml]. *)
module Client : sig
val request
: ?config : Httpaf.Config.t
: ?config : Httpaf.Config.t
-> Lwt_unix.file_descr
-> Httpaf.Request.t
-> error_handler : Httpaf.Client_connection.error_handler
-> response_handler : Httpaf.Client_connection.response_handler
-> [`write] Httpaf.Body.t
-> Request.t
-> error_handler : Client_connection.error_handler
-> response_handler : Client_connection.response_handler
-> [`write] Httpaf.Body.t
end

0 comments on commit 1bd293e

Please sign in to comment.