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 25eb8b765a
7 changed files with 45 additions and 49 deletions

View file

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

View file

@ -11,7 +11,7 @@
module Buf = Tiny_httpd_buf
(** Input channel (byte source) *)
module In_channel = struct
module Input = struct
type t = {
input: bytes -> int -> int -> int;
(** Read into the slice. Returns [0] only if the
@ -54,7 +54,7 @@ module In_channel = struct
end
(** Output channel (byte sink) *)
module Out_channel = struct
module Output = struct
type t = {
output_char: char -> unit; (** Output a single char *)
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. *)
(** [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]
instead of [close_out] to close [oc] *)
let of_out_channel ?(close_noerr = false) (oc : out_channel) : t =
@ -158,7 +158,7 @@ end
(** A writer abstraction. *)
module Writer = struct
type t = { write: Out_channel.t -> unit } [@@unboxed]
type t = { write: Output.t -> unit } [@@unboxed]
(** Writer.
A writer is a push-based stream of bytes.
@ -173,22 +173,21 @@ module Writer = struct
let[@inline] make ~write () : t = { write }
(** 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. *)
let empty : t = { write = ignore }
(** A writer that just emits the bytes from the given string. *)
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 }
end
(** A TCP server abstraction. *)
module TCP_server = struct
type conn_handler = {
handle: In_channel.t -> Out_channel.t -> unit;
(** Handle client connection *)
handle: Input.t -> Output.t -> unit; (** Handle client connection *)
}
type t = {

View file

@ -444,7 +444,7 @@ module Response = struct
Format.fprintf out "{@[code=%d;@ headers=[@[%a@]];@ body=%a@]}" self.code
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:
- print into [buffer] using [bprintf]
- transfer to [buf_] so we can output from there *)
@ -488,21 +488,21 @@ module Response = struct
Buffer.clear tmp_buffer)
headers;
IO.Out_channel.output_buf oc buf;
IO.Out_channel.output_string oc "\r\n";
IO.Output.output_buf oc buf;
IO.Output.output_string oc "\r\n";
Buf.clear buf;
(match body with
| `String "" | `Void -> ()
| `String s -> IO.Out_channel.output_string oc s
| `String s -> IO.Output.output_string oc s
| `Writer 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
IO.Writer.write oc' w;
IO.Out_channel.close oc'
IO.Output.close oc'
with e ->
IO.Out_channel.close oc';
IO.Output.close oc';
raise e)
| `Stream str ->
(try
@ -511,7 +511,7 @@ module Response = struct
with e ->
Byte_stream.close str;
raise e));
IO.Out_channel.flush oc
IO.Output.flush oc
end
(* semaphore, for limiting concurrency. *)
@ -645,7 +645,7 @@ module Middleware = struct
end
(* 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
val set_headers : Headers.t -> unit
@ -793,7 +793,7 @@ let[@inline] _opt_iter ~f o =
exception Exit_SSE
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 =
Pool.with_resource self.buf_pool @@ fun buf ->
Request.read_body_full ~buf req
@ -816,7 +816,7 @@ let add_route_server_sent_handler ?accept self route f =
in
let[@inline] writef fmt =
Printf.ksprintf (IO.Out_channel.output_string oc) fmt
Printf.ksprintf (IO.Output.output_string oc) fmt
in
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);
let l = String.split_on_char '\n' data in
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 *)
IO.Out_channel.flush oc
IO.Output.flush oc
in
let module SSG = struct
let set_headers h =
@ -841,7 +841,7 @@ let add_route_server_sent_handler ?accept self route f =
let close () = raise Exit_SSE
end in
try f req (module SSG : SERVER_SENT_GENERATOR)
with Exit_SSE -> IO.Out_channel.close oc
with Exit_SSE -> IO.Output.close oc
in
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_SNDTIMEO self.timeout);
let oc =
IO.Out_channel.of_out_channel
@@ Unix.out_channel_of_descr client_sock
IO.Output.of_out_channel @@ Unix.out_channel_of_descr client_sock
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;
_debug (fun k -> k "done with client, exiting");
(try Unix.close client_sock

View file

@ -46,28 +46,28 @@ let make ?(bs = Bytes.create @@ (16 * 1024)) ?(close = ignore) ~consume ~fill ()
in
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)
~close:(fun _ -> IO.In_channel.close ic)
~close:(fun _ -> IO.Input.close ic)
~consume:(fun self n ->
self.off <- self.off + n;
self.len <- self.len - n)
~fill:(fun self ->
if self.off >= self.len then (
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 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
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_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
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 : IO.Out_channel.t) (self : t) =
iter (IO.Out_channel.output oc) self
let to_chan' (oc : IO.Output.t) (self : t) = iter (IO.Output.output oc) self
let to_writer (self : t) : Tiny_httpd_io.Writer.t =
{ write = (fun oc -> to_chan' oc self) }
@ -299,11 +297,11 @@ let read_chunked ?(buf = Buf.create ()) ~fail (bs : t) : t =
refill := false)
()
let output_chunked' ?buf (oc : IO.Out_channel.t) (self : t) : unit =
let oc' = IO.Out_channel.chunk_encoding ?buf oc ~close_rec:false in
let output_chunked' ?buf (oc : IO.Output.t) (self : t) : unit =
let oc' = IO.Output.chunk_encoding ?buf oc ~close_rec:false in
to_chan' oc' self;
IO.Out_channel.close oc'
IO.Output.close oc'
(* print a stream as a series of chunks *)
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
(** 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.
@since NEXT_RELEASE *)
@ -94,7 +94,7 @@ val to_chan : out_channel -> t -> unit
(** Write the stream to the channel.
@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.
@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) *)
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.
@since NEXT_RELEASE *)

View file

@ -1,7 +1,7 @@
module S = Tiny_httpd_server
module BS = Tiny_httpd_stream
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 =
S._debug (fun k -> k "wrap stream with deflate.decode");

View file

@ -294,7 +294,7 @@ let prelude =
module Out : sig
type 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 add_char : t -> char -> unit
val add_string : t -> string -> unit
@ -303,14 +303,14 @@ module Out : sig
end = struct
module IO = Tiny_httpd_io
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 *)
}
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[@inline] flush self : unit = IO.Out_channel.flush self.out
let[@inline] add_char self c = IO.Out_channel.output_char self.out c
let[@inline] add_string self s = IO.Out_channel.output_string self.out s
let create_of_buffer buf : t = create_of_out (IO.Output.of_buffer buf)
let[@inline] flush self : unit = IO.Output.flush self.out
let[@inline] add_char self c = IO.Output.output_char self.out c
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 with_no_format_nl self f =
if self.fmt_nl then (