mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 03:05:29 -05:00
commit
471810cf70
4 changed files with 32 additions and 10 deletions
|
|
@ -82,8 +82,8 @@ let vfs_of_dir (top:string) : vfs =
|
||||||
let contains f = Sys.file_exists (top // f)
|
let contains f = Sys.file_exists (top // f)
|
||||||
let list_dir f = Sys.readdir (top // f)
|
let list_dir f = Sys.readdir (top // f)
|
||||||
let read_file_content f =
|
let read_file_content f =
|
||||||
let ic = open_in_bin (top // f) in
|
let ic = Unix.(openfile (top // f) [O_RDONLY] 0) in
|
||||||
Tiny_httpd_stream.of_chan ic
|
Tiny_httpd_stream.of_fd ic
|
||||||
let create f =
|
let create f =
|
||||||
let oc = open_out_bin (top // f) in
|
let oc = open_out_bin (top // f) in
|
||||||
let write = output oc in
|
let write = output oc in
|
||||||
|
|
@ -398,4 +398,3 @@ module Embedded_fs = struct
|
||||||
end in (module M)
|
end in (module M)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -258,7 +258,7 @@ module Request = struct
|
||||||
} in
|
} in
|
||||||
Ok (Some req)
|
Ok (Some req)
|
||||||
with
|
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)
|
| Bad_req (c,s) -> Error (c,s)
|
||||||
| e ->
|
| e ->
|
||||||
Error (400, Printexc.to_string 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 =
|
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_RCVTIMEO self.timeout);
|
||||||
Unix.(setsockopt_float client_sock SO_SNDTIMEO 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 oc = Unix.out_channel_of_descr client_sock in
|
||||||
let buf = Buf.create ~size:self.buf_size () 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
|
let continue = ref true in
|
||||||
while !continue && self.running do
|
while !continue && self.running do
|
||||||
_debug (fun k->k "read next request");
|
_debug (fun k->k "read next request");
|
||||||
|
|
|
||||||
|
|
@ -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 = of_chan_ ~close:close_in
|
||||||
let of_chan_close_noerr = of_chan_ ~close:close_in_noerr
|
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 =
|
let rec iter f (self:t) : unit =
|
||||||
self.fill_buf();
|
self.fill_buf();
|
||||||
if self.len=0 then (
|
if self.len=0 then (
|
||||||
|
|
@ -102,13 +120,13 @@ let of_string s : t =
|
||||||
of_bytes (Bytes.unsafe_of_string s)
|
of_bytes (Bytes.unsafe_of_string s)
|
||||||
|
|
||||||
let with_file ?buf_size file f =
|
let with_file ?buf_size file f =
|
||||||
let ic = open_in file in
|
let ic = Unix.(openfile file [O_RDONLY] 0) in
|
||||||
try
|
try
|
||||||
let x = f (of_chan ?buf_size ic) in
|
let x = f (of_fd ?buf_size ic) in
|
||||||
close_in ic;
|
Unix.close ic;
|
||||||
x
|
x
|
||||||
with e ->
|
with e ->
|
||||||
close_in_noerr ic;
|
Unix.close ic;
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
let read_all ?(buf=Buf.create()) (self:t) : string =
|
let read_all ?(buf=Buf.create()) (self:t) : string =
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,12 @@ val of_chan : ?buf_size:int -> in_channel -> t
|
||||||
val of_chan_close_noerr : ?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. *)
|
(** 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
|
val of_bytes : ?i:int -> ?len:int -> bytes -> t
|
||||||
(** A stream that just returns the slice of bytes starting from [i]
|
(** A stream that just returns the slice of bytes starting from [i]
|
||||||
and of length [len]. *)
|
and of length [len]. *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue