Skip to content

Commit 21b1d81

Browse files
committed
transparent (content-encoding) compression
1 parent ea56195 commit 21b1d81

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

include/hackney_lib.hrl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
partial_headers = [] :: list(),
2727
clen = undefined :: integer() | undefined | bad_int,
2828
te = <<>> :: binary(),
29+
ce = <<>> :: binary(),
2930
connection = <<>> :: binary(),
3031
ctype = <<>> :: binary(),
3132
location = <<>> :: binary(),
32-
body_state = waiting :: atom() | tuple()
33+
body_state = waiting :: atom() | tuple(),
34+
encoding :: {atom(), any()} | undefined
3335
}).

src/hackney.erl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ cancel_request(Ref) ->
9494

9595
%% @doc set client options.
9696
%% Options are:
97+
%% - `compress': request compression and transparently decompress
9798
%% - `async': to fetch the response asynchronously
9899
%% - `{async, once}': to receive the response asynchronously once time.
99100
%% To receive the next message use the function `hackney:stream_next/1'.
@@ -211,6 +212,8 @@ request(Method, URL, Headers, Body) ->
211212
%% directly. The response is `{ok, Status, Headers, Body}'</li>
212213
%% <li>`max_body': sets maximum allowed size of the body if
213214
%% with_body is true</li>
215+
%% <li>`compress': request that the server sends the body compressed
216+
%% and instructs hackney to transparently decompress it</li>
214217
%% <li>`async': receive the response asynchronously
215218
%% The function return {ok, StreamRef}.
216219
%% When {async, once} is used the response will be received only once. To
@@ -985,6 +988,9 @@ maybe_update_req(State) ->
985988

986989
parse_options([], State) ->
987990
State;
991+
parse_options([compress | Rest], State = #client{headers=Headers}) ->
992+
Headers2 = hackney_headers:store(<<"accept-encoding">>, <<"gzip, deflate">>, Headers),
993+
parse_options(Rest, State#client{headers=Headers2});
988994
parse_options([async | Rest], State) ->
989995
parse_options(Rest, State#client{async=true});
990996
parse_options([{async, Async} | Rest], State) ->

src/hackney_http.erl

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ parse_header(Line, St) ->
305305
<<"transfer-encoding">> ->
306306
TE = hackney_bstr:to_lower(hackney_bstr:trim(Value)),
307307
St#hparser{te=TE};
308+
<<"content-encoding">> ->
309+
CE = hackney_bstr:to_lower(hackney_bstr:trim(Value)),
310+
St#hparser{ce=CE};
308311
<<"connection">> ->
309312
Connection = hackney_bstr:to_lower(hackney_bstr:trim(Value)),
310313
St#hparser{connection=Connection};
@@ -330,9 +333,33 @@ parse_trailers(St, Acc) ->
330333
_ -> error
331334
end.
332335

333-
parse_body(#hparser{body_state=waiting, method= <<"HEAD">>, buffer=Buffer}) ->
334-
{done, Buffer};
335-
parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length, buffer=Buffer}) ->
336+
parse_body(#hparser{body_state=waiting, method=Method, buffer=Buffer, clen=Length}) when Method == <<"HEAD">>; Length =:= 0 ->
337+
{done, Buffer};
338+
parse_body(St=#hparser{body_state=waiting, ce=CE}) when CE == <<"gzip">>; CE == <<"deflate">> ->
339+
MaxWBITS = 15, % zconf.h
340+
WB = MaxWBITS + if CE == <<"gzip">> -> 16; true -> 0 end, % http://www.zlib.net/manual.html#Advanced
341+
Z = zlib:open(),
342+
ok = zlib:inflateInit(Z, WB),
343+
parse_body2(St#hparser{encoding={zlib,Z}});
344+
parse_body(St=#hparser{body_state=waiting}) ->
345+
parse_body2(St);
346+
parse_body(St=#hparser{encoding={zlib,Z}}) ->
347+
case parse_body2(St) of
348+
{ok, Chunk, St2} ->
349+
Chunk2 = iolist_to_binary(zlib:inflate(Z, Chunk)),
350+
{ok, Chunk2, St2};
351+
{done, Rest} ->
352+
Rest2 = iolist_to_binary(zlib:inflate(Z, Rest)),
353+
ok = zlib:inflateEnd(Z),
354+
ok = zlib:close(Z),
355+
{done, Rest2};
356+
Else ->
357+
Else
358+
end;
359+
parse_body(St) ->
360+
parse_body2(St).
361+
362+
parse_body2(St=#hparser{body_state=waiting, te=TE, clen=Length, buffer=Buffer}) ->
336363
case {TE, Length} of
337364
{<<"chunked">>, _} ->
338365
parse_body(St#hparser{body_state=
@@ -346,14 +373,13 @@ parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length, buffer=Buffer}) -
346373
St#hparser{body_state={stream, fun te_identity/2, {0, Length}, fun ce_identity/1}}
347374
)
348375
end;
349-
parse_body(#hparser{body_state=done, buffer=Buffer}) ->
376+
parse_body2(#hparser{body_state=done, buffer=Buffer}) ->
350377
{done, Buffer};
351-
parse_body(St=#hparser{buffer=Buffer, body_state={stream, _, _, _}}) when byte_size(Buffer) > 0 ->
378+
parse_body2(St=#hparser{buffer=Buffer, body_state={stream, _, _, _}}) when byte_size(Buffer) > 0 ->
352379
transfer_decode(Buffer, St#hparser{buffer= <<>>});
353-
parse_body(St) ->
380+
parse_body2(St) ->
354381
{more, St, <<>>}.
355382

356-
357383
-spec transfer_decode(binary(), #hparser{})
358384
-> {ok, binary(), #hparser{}} | {done, binary()} | {error, atom()}.
359385
transfer_decode(Data, St=#hparser{
@@ -518,6 +544,8 @@ get_property(method, #hparser{method=Method}) ->
518544
Method;
519545
get_property(transfer_encoding, #hparser{te=TE}) ->
520546
TE;
547+
get_property(content_encoding, #hparser{ce=CE}) ->
548+
CE;
521549
get_property(content_length, #hparser{clen=CLen}) ->
522550
CLen;
523551
get_property(connection, #hparser{connection=Connection}) ->

src/hackney_request.erl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,35 @@ perform(Client0, {Method0, Path, Headers0, Body0}) ->
5757
%% add host eventually
5858
Headers2 = maybe_add_host(Headers1, Client0#client.netloc),
5959

60+
Compress = proplists:get_value(compress, Options, false),
61+
Headers3 = if Compress ->
62+
hackney_headers_new:store(<<"accept-encoding">>, <<"gzip, deflate">>, Headers2);
63+
true -> Headers2 end,
64+
6065
%% get expect headers
61-
Expect = expectation(Headers2),
66+
Expect = expectation(Headers3),
6267

6368
%% build headers with the body.
6469
{FinalHeaders, ReqType, Body, Client1} = case Body0 of
6570
stream ->
66-
{Headers2, ReqType0, stream, Client0};
71+
{Headers3, ReqType0, stream, Client0};
6772
stream_multipart ->
68-
handle_multipart_body(Headers2, ReqType0, Client0);
73+
handle_multipart_body(Headers3, ReqType0, Client0);
6974
{stream_multipart, Size} ->
70-
handle_multipart_body(Headers2, ReqType0, Size, Client0);
75+
handle_multipart_body(Headers3, ReqType0, Size, Client0);
7176
{stream_multipart, Size, Boundary} ->
72-
handle_multipart_body(Headers2, ReqType0,
77+
handle_multipart_body(Headers3, ReqType0,
7378
Size, Boundary, Client0);
7479
<<>> when Method =:= <<"POST">> orelse Method =:= <<"PUT">> ->
75-
handle_body(Headers2, ReqType0, Body0, Client0);
80+
handle_body(Headers3, ReqType0, Body0, Client0);
7681
[] when Method =:= <<"POST">> orelse Method =:= <<"PUT">> ->
77-
handle_body(Headers2, ReqType0, Body0, Client0);
82+
handle_body(Headers3, ReqType0, Body0, Client0);
7883
<<>> ->
79-
{Headers2, ReqType0, Body0, Client0};
84+
{Headers3, ReqType0, Body0, Client0};
8085
[] ->
81-
{Headers2, ReqType0, Body0, Client0};
86+
{Headers3, ReqType0, Body0, Client0};
8287
_ ->
83-
handle_body(Headers2, ReqType0, Body0, Client0)
88+
handle_body(Headers3, ReqType0, Body0, Client0)
8489
end,
8590

8691
%% build final client record

0 commit comments

Comments
 (0)