rename IO.{In,Out}_channel to avoid confusion

these are not the standard in/out_channels so we should just use
different names.
This commit is contained in:
Simon Cruanes 2023-08-08 12:41:42 -04:00
parent 3802aad11f
commit 2378271ffb
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
7 changed files with 45 additions and 49 deletions

View file

@ -16,7 +16,7 @@ include Tiny_httpd_html_
be a "html" tag. be a "html" tag.
@since NEXT_RELEASE @since NEXT_RELEASE
*) *)
let to_out_channel ?(top = false) (self : elt) (out : IO.Out_channel.t) : unit = let to_out_channel ?(top = false) (self : elt) (out : IO.Output.t) : unit =
let out = Out.create_of_out out in let out = Out.create_of_out out in
if top then Out.add_string out "<!DOCTYPE html>\n"; if top then Out.add_string out "<!DOCTYPE html>\n";
self out; self out;
@ -28,7 +28,7 @@ let to_out_channel ?(top = false) (self : elt) (out : IO.Out_channel.t) : unit =
be a "html" tag. *) be a "html" tag. *)
let to_string ?top (self : elt) : string = let to_string ?top (self : elt) : string =
let buf = Buffer.create 64 in let buf = Buffer.create 64 in
let out = IO.Out_channel.of_buffer buf in let out = IO.Output.of_buffer buf in
to_out_channel ?top self out; to_out_channel ?top self out;
Buffer.contents buf Buffer.contents buf

View file

@ -11,7 +11,7 @@
module Buf = Tiny_httpd_buf module Buf = Tiny_httpd_buf
(** Input channel (byte source) *) (** Input channel (byte source) *)
module In_channel = struct module Input = struct
type t = { type t = {
input: bytes -> int -> int -> int; input: bytes -> int -> int -> int;
(** Read into the slice. Returns [0] only if the (** Read into the slice. Returns [0] only if the
@ -54,7 +54,7 @@ module In_channel = struct
end end
(** Output channel (byte sink) *) (** Output channel (byte sink) *)
module Out_channel = struct module Output = struct
type t = { type t = {
output_char: char -> unit; (** Output a single char *) output_char: char -> unit; (** Output a single char *)
output: bytes -> int -> int -> unit; (** Output slice *) output: bytes -> int -> int -> unit; (** Output slice *)
@ -65,7 +65,7 @@ module Out_channel = struct
This can be a [Buffer.t], an [out_channel], a [Unix.file_descr], etc. *) This can be a [Buffer.t], an [out_channel], a [Unix.file_descr], etc. *)
(** [of_out_channel oc] wraps the channel into a {!Out_channel.t}. (** [of_out_channel oc] wraps the channel into a {!Output.t}.
@param close_noerr if true, then closing the result uses [close_out_noerr] @param close_noerr if true, then closing the result uses [close_out_noerr]
instead of [close_out] to close [oc] *) instead of [close_out] to close [oc] *)
let of_out_channel ?(close_noerr = false) (oc : out_channel) : t = let of_out_channel ?(close_noerr = false) (oc : out_channel) : t =
@ -158,7 +158,7 @@ end
(** A writer abstraction. *) (** A writer abstraction. *)
module Writer = struct module Writer = struct
type t = { write: Out_channel.t -> unit } [@@unboxed] type t = { write: Output.t -> unit } [@@unboxed]
(** Writer. (** Writer.
A writer is a push-based stream of bytes. A writer is a push-based stream of bytes.
@ -173,22 +173,21 @@ module Writer = struct
let[@inline] make ~write () : t = { write } let[@inline] make ~write () : t = { write }
(** Write into the channel. *) (** Write into the channel. *)
let[@inline] write (oc : Out_channel.t) (self : t) : unit = self.write oc let[@inline] write (oc : Output.t) (self : t) : unit = self.write oc
(** Empty writer, will output 0 bytes. *) (** Empty writer, will output 0 bytes. *)
let empty : t = { write = ignore } let empty : t = { write = ignore }
(** A writer that just emits the bytes from the given string. *) (** A writer that just emits the bytes from the given string. *)
let[@inline] of_string (str : string) : t = let[@inline] of_string (str : string) : t =
let write oc = Out_channel.output_string oc str in let write oc = Output.output_string oc str in
{ write } { write }
end end
(** A TCP server abstraction. *) (** A TCP server abstraction. *)
module TCP_server = struct module TCP_server = struct
type conn_handler = { type conn_handler = {
handle: In_channel.t -> Out_channel.t -> unit; handle: Input.t -> Output.t -> unit; (** Handle client connection *)
(** Handle client connection *)
} }
type t = { type t = {

View file

@ -444,7 +444,7 @@ module Response = struct
Format.fprintf out "{@[code=%d;@ headers=[@[%a@]];@ body=%a@]}" self.code Format.fprintf out "{@[code=%d;@ headers=[@[%a@]];@ body=%a@]}" self.code
Headers.pp self.headers pp_body self.body Headers.pp self.headers pp_body self.body
let output_ ~buf (oc : IO.Out_channel.t) (self : t) : unit = let output_ ~buf (oc : IO.Output.t) (self : t) : unit =
(* double indirection: (* double indirection:
- print into [buffer] using [bprintf] - print into [buffer] using [bprintf]
- transfer to [buf_] so we can output from there *) - transfer to [buf_] so we can output from there *)
@ -488,21 +488,21 @@ module Response = struct
Buffer.clear tmp_buffer) Buffer.clear tmp_buffer)
headers; headers;
IO.Out_channel.output_buf oc buf; IO.Output.output_buf oc buf;
IO.Out_channel.output_string oc "\r\n"; IO.Output.output_string oc "\r\n";
Buf.clear buf; Buf.clear buf;
(match body with (match body with
| `String "" | `Void -> () | `String "" | `Void -> ()
| `String s -> IO.Out_channel.output_string oc s | `String s -> IO.Output.output_string oc s
| `Writer w -> | `Writer w ->
(* use buffer to chunk encode [w] *) (* use buffer to chunk encode [w] *)
let oc' = IO.Out_channel.chunk_encoding ~buf ~close_rec:false oc in let oc' = IO.Output.chunk_encoding ~buf ~close_rec:false oc in
(try (try
IO.Writer.write oc' w; IO.Writer.write oc' w;
IO.Out_channel.close oc' IO.Output.close oc'
with e -> with e ->
IO.Out_channel.close oc'; IO.Output.close oc';
raise e) raise e)
| `Stream str -> | `Stream str ->
(try (try
@ -511,7 +511,7 @@ module Response = struct
with e -> with e ->
Byte_stream.close str; Byte_stream.close str;
raise e)); raise e));
IO.Out_channel.flush oc IO.Output.flush oc
end end
(* semaphore, for limiting concurrency. *) (* semaphore, for limiting concurrency. *)
@ -645,7 +645,7 @@ module Middleware = struct
end end
(* a request handler. handles a single request. *) (* a request handler. handles a single request. *)
type cb_path_handler = IO.Out_channel.t -> Middleware.handler type cb_path_handler = IO.Output.t -> Middleware.handler
module type SERVER_SENT_GENERATOR = sig module type SERVER_SENT_GENERATOR = sig
val set_headers : Headers.t -> unit val set_headers : Headers.t -> unit
@ -793,7 +793,7 @@ let[@inline] _opt_iter ~f o =
exception Exit_SSE exception Exit_SSE
let add_route_server_sent_handler ?accept self route f = let add_route_server_sent_handler ?accept self route f =
let tr_req (oc : IO.Out_channel.t) req ~resp f = let tr_req (oc : IO.Output.t) req ~resp f =
let req = let req =
Pool.with_resource self.buf_pool @@ fun buf -> Pool.with_resource self.buf_pool @@ fun buf ->
Request.read_body_full ~buf req Request.read_body_full ~buf req
@ -816,7 +816,7 @@ let add_route_server_sent_handler ?accept self route f =
in in
let[@inline] writef fmt = let[@inline] writef fmt =
Printf.ksprintf (IO.Out_channel.output_string oc) fmt Printf.ksprintf (IO.Output.output_string oc) fmt
in in
let send_event ?event ?id ?retry ~data () : unit = let send_event ?event ?id ?retry ~data () : unit =
@ -826,9 +826,9 @@ let add_route_server_sent_handler ?accept self route f =
_opt_iter retry ~f:(fun e -> writef "retry: %s\n" e); _opt_iter retry ~f:(fun e -> writef "retry: %s\n" e);
let l = String.split_on_char '\n' data in let l = String.split_on_char '\n' data in
List.iter (fun s -> writef "data: %s\n" s) l; List.iter (fun s -> writef "data: %s\n" s) l;
IO.Out_channel.output_string oc "\n"; IO.Output.output_string oc "\n";
(* finish group *) (* finish group *)
IO.Out_channel.flush oc IO.Output.flush oc
in in
let module SSG = struct let module SSG = struct
let set_headers h = let set_headers h =
@ -841,7 +841,7 @@ let add_route_server_sent_handler ?accept self route f =
let close () = raise Exit_SSE let close () = raise Exit_SSE
end in end in
try f req (module SSG : SERVER_SENT_GENERATOR) try f req (module SSG : SERVER_SENT_GENERATOR)
with Exit_SSE -> IO.Out_channel.close oc with Exit_SSE -> IO.Output.close oc
in in
add_route_handler_ self ?accept ~meth:`GET route ~tr_req f add_route_handler_ self ?accept ~meth:`GET route ~tr_req f
@ -938,10 +938,9 @@ module Unix_tcp_server_ = struct
Unix.(setsockopt_float client_sock SO_RCVTIMEO self.timeout); Unix.(setsockopt_float client_sock SO_RCVTIMEO self.timeout);
Unix.(setsockopt_float client_sock SO_SNDTIMEO self.timeout); Unix.(setsockopt_float client_sock SO_SNDTIMEO self.timeout);
let oc = let oc =
IO.Out_channel.of_out_channel IO.Output.of_out_channel @@ Unix.out_channel_of_descr client_sock
@@ Unix.out_channel_of_descr client_sock
in in
let ic = IO.In_channel.of_unix_fd client_sock in let ic = IO.Input.of_unix_fd client_sock in
handle.handle ic oc; handle.handle ic oc;
_debug (fun k -> k "done with client, exiting"); _debug (fun k -> k "done with client, exiting");
(try Unix.close client_sock (try Unix.close client_sock

View file

@ -46,28 +46,28 @@ let make ?(bs = Bytes.create @@ (16 * 1024)) ?(close = ignore) ~consume ~fill ()
in in
self self
let of_input ?(buf_size = 16 * 1024) (ic : IO.In_channel.t) : t = let of_input ?(buf_size = 16 * 1024) (ic : IO.Input.t) : t =
make ~bs:(Bytes.create buf_size) make ~bs:(Bytes.create buf_size)
~close:(fun _ -> IO.In_channel.close ic) ~close:(fun _ -> IO.Input.close ic)
~consume:(fun self n -> ~consume:(fun self n ->
self.off <- self.off + n; self.off <- self.off + n;
self.len <- self.len - n) self.len <- self.len - n)
~fill:(fun self -> ~fill:(fun self ->
if self.off >= self.len then ( if self.off >= self.len then (
self.off <- 0; self.off <- 0;
self.len <- IO.In_channel.input ic self.bs 0 (Bytes.length self.bs) self.len <- IO.Input.input ic self.bs 0 (Bytes.length self.bs)
)) ))
() ()
let of_chan_ ?buf_size ic ~close_noerr : t = let of_chan_ ?buf_size ic ~close_noerr : t =
let inc = IO.In_channel.of_in_channel ~close_noerr ic in let inc = IO.Input.of_in_channel ~close_noerr ic in
of_input ?buf_size inc of_input ?buf_size inc
let of_chan ?buf_size ic = of_chan_ ?buf_size ic ~close_noerr:false let of_chan ?buf_size ic = of_chan_ ?buf_size ic ~close_noerr:false
let of_chan_close_noerr ?buf_size ic = of_chan_ ?buf_size ic ~close_noerr:true let of_chan_close_noerr ?buf_size ic = of_chan_ ?buf_size ic ~close_noerr:true
let of_fd_ ?buf_size ~close_noerr ic : t = let of_fd_ ?buf_size ~close_noerr ic : t =
let inc = IO.In_channel.of_unix_fd ~close_noerr ic in let inc = IO.Input.of_unix_fd ~close_noerr ic in
of_input ?buf_size inc of_input ?buf_size inc
let of_fd ?buf_size fd : t = of_fd_ ?buf_size ~close_noerr:false fd let of_fd ?buf_size fd : t = of_fd_ ?buf_size ~close_noerr:false fd
@ -84,9 +84,7 @@ let rec iter f (self : t) : unit =
) )
let to_chan (oc : out_channel) (self : t) = iter (output oc) self let to_chan (oc : out_channel) (self : t) = iter (output oc) self
let to_chan' (oc : IO.Output.t) (self : t) = iter (IO.Output.output oc) self
let to_chan' (oc : IO.Out_channel.t) (self : t) =
iter (IO.Out_channel.output oc) self
let to_writer (self : t) : Tiny_httpd_io.Writer.t = let to_writer (self : t) : Tiny_httpd_io.Writer.t =
{ write = (fun oc -> to_chan' oc self) } { write = (fun oc -> to_chan' oc self) }
@ -299,11 +297,11 @@ let read_chunked ?(buf = Buf.create ()) ~fail (bs : t) : t =
refill := false) refill := false)
() ()
let output_chunked' ?buf (oc : IO.Out_channel.t) (self : t) : unit = let output_chunked' ?buf (oc : IO.Output.t) (self : t) : unit =
let oc' = IO.Out_channel.chunk_encoding ?buf oc ~close_rec:false in let oc' = IO.Output.chunk_encoding ?buf oc ~close_rec:false in
to_chan' oc' self; to_chan' oc' self;
IO.Out_channel.close oc' IO.Output.close oc'
(* print a stream as a series of chunks *) (* print a stream as a series of chunks *)
let output_chunked ?buf (oc : out_channel) (self : t) : unit = let output_chunked ?buf (oc : out_channel) (self : t) : unit =
output_chunked' ?buf (IO.Out_channel.of_out_channel oc) self output_chunked' ?buf (IO.Output.of_out_channel oc) self

View file

@ -64,7 +64,7 @@ val close : t -> unit
val empty : t val empty : t
(** Stream with 0 bytes inside *) (** Stream with 0 bytes inside *)
val of_input : ?buf_size:int -> Tiny_httpd_io.In_channel.t -> t val of_input : ?buf_size:int -> Tiny_httpd_io.Input.t -> t
(** Make a buffered stream from the given channel. (** Make a buffered stream from the given channel.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
@ -94,7 +94,7 @@ val to_chan : out_channel -> t -> unit
(** Write the stream to the channel. (** Write the stream to the channel.
@since 0.3 *) @since 0.3 *)
val to_chan' : Tiny_httpd_io.Out_channel.t -> t -> unit val to_chan' : Tiny_httpd_io.Output.t -> t -> unit
(** Write to the IO channel. (** Write to the IO channel.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)
@ -154,6 +154,6 @@ val output_chunked : ?buf:Tiny_httpd_buf.t -> out_channel -> t -> unit
@param buf optional buffer for chunking (since NEXT_RELEASE) *) @param buf optional buffer for chunking (since NEXT_RELEASE) *)
val output_chunked' : val output_chunked' :
?buf:Tiny_httpd_buf.t -> Tiny_httpd_io.Out_channel.t -> t -> unit ?buf:Tiny_httpd_buf.t -> Tiny_httpd_io.Output.t -> t -> unit
(** Write the stream into the channel, using the chunked encoding. (** Write the stream into the channel, using the chunked encoding.
@since NEXT_RELEASE *) @since NEXT_RELEASE *)

View file

@ -1,7 +1,7 @@
module S = Tiny_httpd_server module S = Tiny_httpd_server
module BS = Tiny_httpd_stream module BS = Tiny_httpd_stream
module W = Tiny_httpd_io.Writer module W = Tiny_httpd_io.Writer
module Out = Tiny_httpd_io.Out_channel module Out = Tiny_httpd_io.Output
let decode_deflate_stream_ ~buf_size (is : S.byte_stream) : S.byte_stream = let decode_deflate_stream_ ~buf_size (is : S.byte_stream) : S.byte_stream =
S._debug (fun k -> k "wrap stream with deflate.decode"); S._debug (fun k -> k "wrap stream with deflate.decode");

View file

@ -294,7 +294,7 @@ let prelude =
module Out : sig module Out : sig
type t type t
val create_of_buffer : Buffer.t -> t val create_of_buffer : Buffer.t -> t
val create_of_out: Tiny_httpd_io.Out_channel.t -> t val create_of_out: Tiny_httpd_io.Output.t -> t
val flush : t -> unit val flush : t -> unit
val add_char : t -> char -> unit val add_char : t -> char -> unit
val add_string : t -> string -> unit val add_string : t -> string -> unit
@ -303,14 +303,14 @@ module Out : sig
end = struct end = struct
module IO = Tiny_httpd_io module IO = Tiny_httpd_io
type t = { type t = {
out: IO.Out_channel.t; out: IO.Output.t;
mutable fmt_nl: bool; (* if true, we print [\n] around tags to format the html *) mutable fmt_nl: bool; (* if true, we print [\n] around tags to format the html *)
} }
let create_of_out out = {out; fmt_nl=true} let create_of_out out = {out; fmt_nl=true}
let create_of_buffer buf : t = create_of_out (IO.Out_channel.of_buffer buf) let create_of_buffer buf : t = create_of_out (IO.Output.of_buffer buf)
let[@inline] flush self : unit = IO.Out_channel.flush self.out let[@inline] flush self : unit = IO.Output.flush self.out
let[@inline] add_char self c = IO.Out_channel.output_char self.out c let[@inline] add_char self c = IO.Output.output_char self.out c
let[@inline] add_string self s = IO.Out_channel.output_string self.out s let[@inline] add_string self s = IO.Output.output_string self.out s
let[@inline] add_format_nl self = if self.fmt_nl then add_char self '\n' let[@inline] add_format_nl self = if self.fmt_nl then add_char self '\n'
let with_no_format_nl self f = let with_no_format_nl self f =
if self.fmt_nl then ( if self.fmt_nl then (