mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 11:15:35 -05:00
improve API for tiny_httpd
This commit is contained in:
parent
989971bb04
commit
3245647dc9
2 changed files with 10 additions and 4 deletions
|
|
@ -149,7 +149,7 @@ module Stream_ = struct
|
||||||
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 *)
|
||||||
|
|
@ -233,6 +233,7 @@ module Headers = struct
|
||||||
type t = (string * string) list
|
type t = (string * string) list
|
||||||
let contains = List.mem_assoc
|
let contains = List.mem_assoc
|
||||||
let get ?(f=fun x->x) x h = try Some (List.assoc x h |> f) with Not_found -> None
|
let get ?(f=fun x->x) x h = try Some (List.assoc x h |> f) with Not_found -> None
|
||||||
|
let remove x h = List.filter (fun (k,_) -> k<>x) h
|
||||||
let set x y h = (x,y) :: List.filter (fun (k,_) -> k<>x) h
|
let set x y h = (x,y) :: List.filter (fun (k,_) -> k<>x) h
|
||||||
let pp out l =
|
let pp out l =
|
||||||
let pp_pair out (k,v) = Format.fprintf out "@[<h>%s: %s@]" k v in
|
let pp_pair out (k,v) = Format.fprintf out "@[<h>%s: %s@]" k v in
|
||||||
|
|
@ -452,7 +453,7 @@ module Response = struct
|
||||||
self.code Headers.pp self.headers pp_body self.body
|
self.code Headers.pp self.headers pp_body self.body
|
||||||
|
|
||||||
(* print a stream as a series of chunks *)
|
(* print a stream as a series of chunks *)
|
||||||
let output_stream_ (oc:out_channel) (str:stream) : unit =
|
let output_stream_chunked_ (oc:out_channel) (str:stream) : unit =
|
||||||
let continue = ref true in
|
let continue = ref true in
|
||||||
while !continue do
|
while !continue do
|
||||||
(* next chunk *)
|
(* next chunk *)
|
||||||
|
|
@ -476,7 +477,7 @@ module Response = struct
|
||||||
begin match self.body with
|
begin match self.body with
|
||||||
| `String "" -> ()
|
| `String "" -> ()
|
||||||
| `String s -> output_string oc s;
|
| `String s -> output_string oc s;
|
||||||
| `Stream str -> output_stream_ oc str;
|
| `Stream str -> output_stream_chunked_ oc str;
|
||||||
end;
|
end;
|
||||||
flush oc
|
flush oc
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@ module Headers : sig
|
||||||
type t = (string * string) list
|
type t = (string * string) list
|
||||||
val get : ?f:(string->string) -> string -> t -> string option
|
val get : ?f:(string->string) -> string -> t -> string option
|
||||||
val set : string -> string -> t -> t
|
val set : string -> string -> t -> t
|
||||||
|
val remove : string -> t -> t
|
||||||
val contains : string -> t -> bool
|
val contains : string -> t -> bool
|
||||||
val pp : Format.formatter -> t -> unit
|
val pp : Format.formatter -> t -> unit
|
||||||
end
|
end
|
||||||
|
|
@ -81,7 +82,11 @@ end
|
||||||
|
|
||||||
module Response : sig
|
module Response : sig
|
||||||
type body = [`String of string | `Stream of stream]
|
type body = [`String of string | `Stream of stream]
|
||||||
type t
|
type t = {
|
||||||
|
code: Response_code.t;
|
||||||
|
headers: Headers.t;
|
||||||
|
body: body;
|
||||||
|
}
|
||||||
|
|
||||||
val make_raw :
|
val make_raw :
|
||||||
?headers:Headers.t ->
|
?headers:Headers.t ->
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue