mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-10 05:03:57 -05:00
feat(gzip): also compress string responses if they're big
This commit is contained in:
parent
b640c0da30
commit
4300e8dcf0
1 changed files with 18 additions and 7 deletions
|
|
@ -143,18 +143,29 @@ let cb_decode_compressed_stream ~buf_size (req:unit S.Request.t) : _ option =
|
||||||
let cb_encode_compressed_stream
|
let cb_encode_compressed_stream
|
||||||
~buf_size (req:_ S.Request.t) (resp:S.Response.t) : _ option =
|
~buf_size (req:_ S.Request.t) (resp:S.Response.t) : _ option =
|
||||||
if accept_deflate req then (
|
if accept_deflate req then (
|
||||||
|
let set_headers h =
|
||||||
|
h
|
||||||
|
|> S.Headers.remove "Content-Length"
|
||||||
|
|> S.Headers.set "Content-Encoding" "deflate"
|
||||||
|
in
|
||||||
match resp.body with
|
match resp.body with
|
||||||
| `String _ -> None
|
| `String s when String.length s > 500 * 1024 ->
|
||||||
| `Stream str ->
|
S._debug (fun k->k "encode str response with deflate");
|
||||||
S._debug (fun k->k "encode response with deflate");
|
let body =
|
||||||
|
encode_deflate_stream_ ~buf_size @@ S.Byte_stream.of_string s
|
||||||
|
in
|
||||||
Some {
|
Some {
|
||||||
resp with
|
resp with
|
||||||
headers=
|
headers= set_headers resp.headers; body=`Stream body;
|
||||||
(resp.headers
|
}
|
||||||
|> S.Headers.remove "Content-Length"
|
| `Stream str ->
|
||||||
|> S.Headers.set "Content-Encoding" "deflate");
|
S._debug (fun k->k "encode stream response with deflate");
|
||||||
|
Some {
|
||||||
|
resp with
|
||||||
|
headers= set_headers resp.headers;
|
||||||
body=`Stream (encode_deflate_stream_ ~buf_size str);
|
body=`Stream (encode_deflate_stream_ ~buf_size str);
|
||||||
}
|
}
|
||||||
|
| `String _ -> None
|
||||||
) else None
|
) else None
|
||||||
|
|
||||||
let setup ?(buf_size=48 * 1_024) (server:S.t) : unit =
|
let setup ?(buf_size=48 * 1_024) (server:S.t) : unit =
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue