Merge pull request #49 from craff/use_fd

Use fd
This commit is contained in:
Simon Cruanes 2022-12-13 21:19:11 -05:00 committed by GitHub
commit 471810cf70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 10 deletions

View file

@ -82,8 +82,8 @@ let vfs_of_dir (top:string) : vfs =
let contains f = Sys.file_exists (top // f)
let list_dir f = Sys.readdir (top // f)
let read_file_content f =
let ic = open_in_bin (top // f) in
Tiny_httpd_stream.of_chan ic
let ic = Unix.(openfile (top // f) [O_RDONLY] 0) in
Tiny_httpd_stream.of_fd ic
let create f =
let oc = open_out_bin (top // f) in
let write = output oc in
@ -398,4 +398,3 @@ module Embedded_fs = struct
end in (module M)
end

View file

@ -258,7 +258,7 @@ module Request = struct
} in
Ok (Some req)
with
| End_of_file | Sys_error _ -> Ok None
| End_of_file | Sys_error _ | Unix.Unix_error _ -> Ok None
| Bad_req (c,s) -> Error (c,s)
| e ->
Error (400, Printexc.to_string e)
@ -756,10 +756,9 @@ let find_map f l =
let handle_client_ (self:t) (client_sock:Unix.file_descr) : unit =
Unix.(setsockopt_float client_sock SO_RCVTIMEO self.timeout);
Unix.(setsockopt_float client_sock SO_SNDTIMEO self.timeout);
let ic = Unix.in_channel_of_descr client_sock in
let oc = Unix.out_channel_of_descr client_sock in
let buf = Buf.create ~size:self.buf_size () in
let is = Byte_stream.of_chan ~buf_size:self.buf_size ic in
let is = Byte_stream.of_fd ~buf_size:self.buf_size client_sock in
let continue = ref true in
while !continue && self.running do
_debug (fun k->k "read next request");

View file

@ -61,6 +61,24 @@ let of_chan_ ?(buf_size=16 * 1024) ~close ic : t =
let of_chan = of_chan_ ~close:close_in
let of_chan_close_noerr = of_chan_ ~close:close_in_noerr
let of_fd_ ?(buf_size=16 * 1024) ~close ic : t =
make
~bs:(Bytes.create buf_size)
~close:(fun _ -> 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 <- Unix.read ic self.bs 0 (Bytes.length self.bs);
)
)
()
let of_fd = of_fd_ ~close:Unix.close
let of_fd_close_noerr = of_fd_ ~close:(fun f -> try Unix.close f with _ -> ())
let rec iter f (self:t) : unit =
self.fill_buf();
if self.len=0 then (
@ -102,13 +120,13 @@ let of_string s : t =
of_bytes (Bytes.unsafe_of_string s)
let with_file ?buf_size file f =
let ic = open_in file in
let ic = Unix.(openfile file [O_RDONLY] 0) in
try
let x = f (of_chan ?buf_size ic) in
close_in ic;
let x = f (of_fd ?buf_size ic) in
Unix.close ic;
x
with e ->
close_in_noerr ic;
Unix.close ic;
raise e
let read_all ?(buf=Buf.create()) (self:t) : string =

View file

@ -50,6 +50,12 @@ val of_chan : ?buf_size:int -> in_channel -> t
val of_chan_close_noerr : ?buf_size:int -> in_channel -> t
(** Same as {!of_chan} but the [close] method will never fail. *)
val of_fd : ?buf_size:int -> Unix.file_descr -> t
(** Make a buffered stream from the given file descriptor. *)
val of_fd_close_noerr : ?buf_size:int -> Unix.file_descr -> t
(** Same as {!of_fd} but the [close] method will never fail. *)
val of_bytes : ?i:int -> ?len:int -> bytes -> t
(** A stream that just returns the slice of bytes starting from [i]
and of length [len]. *)