mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 19:25:32 -05:00
feat: simplify type of streams
This commit is contained in:
parent
e75e97a559
commit
9c84f95e12
2 changed files with 40 additions and 45 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
type stream = {
|
type stream = {
|
||||||
is_fill_buf: 'a. (bytes -> int -> int -> 'a) -> 'a;
|
is_fill_buf: unit -> (bytes * int * int);
|
||||||
is_consume: int -> unit;
|
is_consume: int -> unit;
|
||||||
is_close: unit -> unit;
|
is_close: unit -> unit;
|
||||||
}
|
}
|
||||||
|
|
@ -63,12 +63,12 @@ module Stream_ = struct
|
||||||
let i = ref 0 in
|
let i = ref 0 in
|
||||||
let len = ref 0 in
|
let len = ref 0 in
|
||||||
let buf = Bytes.make 4096 ' ' in
|
let buf = Bytes.make 4096 ' ' in
|
||||||
{ is_fill_buf=(fun k ->
|
{ is_fill_buf=(fun () ->
|
||||||
if !i >= !len then (
|
if !i >= !len then (
|
||||||
i := 0;
|
i := 0;
|
||||||
len := input ic buf 0 (Bytes.length buf);
|
len := input ic buf 0 (Bytes.length buf);
|
||||||
);
|
);
|
||||||
k buf !i (!len - !i));
|
buf, !i,!len - !i);
|
||||||
is_consume=(fun n -> i := !i + n);
|
is_consume=(fun n -> i := !i + n);
|
||||||
is_close=(fun () -> close ic)
|
is_close=(fun () -> close ic)
|
||||||
}
|
}
|
||||||
|
|
@ -85,7 +85,7 @@ module Stream_ = struct
|
||||||
)
|
)
|
||||||
in
|
in
|
||||||
let i = ref i in
|
let i = ref i in
|
||||||
{ is_fill_buf=(fun k -> k s !i !len);
|
{ is_fill_buf=(fun () -> s, !i, !len);
|
||||||
is_close=(fun () -> ());
|
is_close=(fun () -> ());
|
||||||
is_consume=(fun n -> i := !i + n; len := !len - n);
|
is_consume=(fun n -> i := !i + n; len := !len - n);
|
||||||
}
|
}
|
||||||
|
|
@ -102,13 +102,12 @@ module Stream_ = struct
|
||||||
|
|
||||||
(* Read as much as possible into [buf]. *)
|
(* Read as much as possible into [buf]. *)
|
||||||
let read_into_buf (self:t) (buf:Buf_.t) : int =
|
let read_into_buf (self:t) (buf:Buf_.t) : int =
|
||||||
self.is_fill_buf
|
let s, i, len = self.is_fill_buf () in
|
||||||
(fun s i len ->
|
if len > 0 then (
|
||||||
if len > 0 then (
|
Buf_.add_bytes buf s i len;
|
||||||
Buf_.add_bytes buf s i len;
|
self.is_consume len;
|
||||||
self.is_consume len;
|
);
|
||||||
);
|
len
|
||||||
len)
|
|
||||||
|
|
||||||
let read_all ?(buf=Buf_.create()) (self:t) : string =
|
let read_all ?(buf=Buf_.create()) (self:t) : string =
|
||||||
let continue = ref true in
|
let continue = ref true in
|
||||||
|
|
@ -126,13 +125,12 @@ module Stream_ = struct
|
||||||
let offset = ref 0 in
|
let offset = ref 0 in
|
||||||
while !offset < n do
|
while !offset < n do
|
||||||
let n_read =
|
let n_read =
|
||||||
self.is_fill_buf
|
let s, i, len = self.is_fill_buf () in
|
||||||
(fun s i len ->
|
let n_read = min len (n- !offset) in
|
||||||
let n_read = min len (n- !offset) in
|
Bytes.blit s i bytes !offset n_read;
|
||||||
Bytes.blit s i bytes !offset n_read;
|
offset := !offset + n_read;
|
||||||
offset := !offset + n_read;
|
self.is_consume n_read;
|
||||||
self.is_consume n_read;
|
n_read
|
||||||
n_read)
|
|
||||||
in
|
in
|
||||||
if n_read=0 then too_short();
|
if n_read=0 then too_short();
|
||||||
done
|
done
|
||||||
|
|
@ -142,21 +140,20 @@ module Stream_ = struct
|
||||||
Buf_.clear buf;
|
Buf_.clear buf;
|
||||||
let continue = ref true in
|
let continue = ref true in
|
||||||
while !continue do
|
while !continue do
|
||||||
self.is_fill_buf
|
let s, i, len = self.is_fill_buf () in
|
||||||
(fun s i len ->
|
let j = ref i in
|
||||||
let j = ref i in
|
while !j < i+len && Bytes.get s !j <> '\n' do
|
||||||
while !j < i+len && Bytes.get s !j <> '\n' do
|
incr j
|
||||||
incr j
|
done;
|
||||||
done;
|
if !j-i < len then (
|
||||||
if !j-i < len then (
|
assert (Bytes.get s !j = '\n');
|
||||||
assert (Bytes.get s !j = '\n');
|
Buf_.add_bytes buf s i (!j-i); (* without \n *)
|
||||||
Buf_.add_bytes buf s i (!j-i); (* without \n *)
|
self.is_consume (!j-i+1); (* remove \n *)
|
||||||
self.is_consume (!j-i+1); (* remove \n *)
|
continue := false
|
||||||
continue := false
|
) else (
|
||||||
) else (
|
Buf_.add_bytes buf s i len;
|
||||||
Buf_.add_bytes buf s i len;
|
self.is_consume len;
|
||||||
self.is_consume len;
|
)
|
||||||
));
|
|
||||||
done
|
done
|
||||||
|
|
||||||
let read_line ?(buf=Buf_.create()) self : string =
|
let read_line ?(buf=Buf_.create()) self : string =
|
||||||
|
|
@ -307,7 +304,7 @@ module Request = struct
|
||||||
let len = ref 0 in
|
let len = ref 0 in
|
||||||
let chunk_size = ref 0 in
|
let chunk_size = ref 0 in
|
||||||
{ is_fill_buf=
|
{ is_fill_buf=
|
||||||
(fun k ->
|
(fun () ->
|
||||||
(* do we need to refill? *)
|
(* do we need to refill? *)
|
||||||
if !offset >= !len then (
|
if !offset >= !len then (
|
||||||
if !chunk_size = 0 && !refill then (
|
if !chunk_size = 0 && !refill then (
|
||||||
|
|
@ -327,7 +324,7 @@ module Request = struct
|
||||||
refill := false; (* stream is finished *)
|
refill := false; (* stream is finished *)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
k bytes !offset !len
|
bytes, !offset, !len
|
||||||
);
|
);
|
||||||
is_consume=(fun n -> offset := !offset + n);
|
is_consume=(fun n -> offset := !offset + n);
|
||||||
is_close=(fun () -> Stream_.close is);
|
is_close=(fun () -> Stream_.close is);
|
||||||
|
|
@ -456,15 +453,13 @@ module Response = struct
|
||||||
let continue = ref true in
|
let continue = ref true in
|
||||||
while !continue do
|
while !continue do
|
||||||
(* next chunk *)
|
(* next chunk *)
|
||||||
str.is_fill_buf
|
let s, i, len = str.is_fill_buf () in
|
||||||
(fun s i len ->
|
Printf.fprintf oc "%x\r\n" len;
|
||||||
Printf.fprintf oc "%x\r\n" len;
|
output oc s i len;
|
||||||
output oc s i len;
|
str.is_consume len;
|
||||||
str.is_consume len;
|
if len = 0 then (
|
||||||
if len = 0 then (
|
continue := false;
|
||||||
continue := false;
|
);
|
||||||
)
|
|
||||||
);
|
|
||||||
output_string oc "\r\n";
|
output_string oc "\r\n";
|
||||||
done;
|
done;
|
||||||
()
|
()
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
type stream = {
|
type stream = {
|
||||||
is_fill_buf: 'a. (bytes -> int -> int -> 'a) -> 'a;
|
is_fill_buf: unit -> (bytes * int * int);
|
||||||
is_consume: int -> unit;
|
is_consume: int -> unit;
|
||||||
is_close: unit -> unit;
|
is_close: unit -> unit;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue