Skip to content

Commit c309e2b

Browse files
committed
transparent (content-encoding) compression
1 parent c3be698 commit c309e2b

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
lines changed

include/hackney_lib.hrl

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
partial_headers = [] :: list(),
2727
clen :: integer() | undefined,
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

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ cancel_request(Ref) ->
9292

9393
%% @doc set client options.
9494
%% Options are:
95+
%% - `compress': request compression and transparently decompress
9596
%% - `async': to fetch the response asynchronously
9697
%% - `{async, once}': to receive the response asynchronously once time.
9798
%% To receive the next message use the function `hackney:stream_next/1'.
@@ -200,6 +201,8 @@ request(Method, URL, Headers, Body) ->
200201
%% directly. The response is `{ok, Status, Headers, Body}'</li>
201202
%% <li>`max_body': sets maximum allowed size of the body if
202203
%% with_body is true</li>
204+
%% <li>`compress': request that the server sends the body compressed
205+
%% and instructs hackney to transparently decompress it</li>
203206
%% <li>`async': receive the response asynchronously
204207
%% The function return {ok, StreamRef}.
205208
%% When {async, once} is used the response will be received only once. To
@@ -968,6 +971,9 @@ maybe_update_req(State) ->
968971

969972
parse_options([], State) ->
970973
State;
974+
parse_options([compress | Rest], State = #client{headers=Headers}) ->
975+
Headers2 = hackney_headers:store(<<"accept-encoding">>, <<"gzip, deflate">>, Headers),
976+
parse_options(Rest, State#client{headers=Headers2});
971977
parse_options([async | Rest], State) ->
972978
parse_options(Rest, State#client{async=true});
973979
parse_options([{async, Async} | Rest], State) ->

src/hackney_http.erl

+42-5
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ parse_header(Line, St) ->
300300
<<"transfer-encoding">> ->
301301
TE = hackney_bstr:to_lower(hackney_bstr:trim(Value)),
302302
St#hparser{te=TE};
303+
<<"content-encoding">> ->
304+
CE = hackney_bstr:to_lower(hackney_bstr:trim(Value)),
305+
St#hparser{ce=CE};
303306
<<"connection">> ->
304307
Connection = hackney_bstr:to_lower(hackney_bstr:trim(Value)),
305308
St#hparser{connection=Connection};
@@ -325,7 +328,40 @@ parse_trailers(St, Acc) ->
325328
_ -> error
326329
end.
327330

328-
parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length,
331+
-define(MAX_WBITS, 15). % zconf.h
332+
parse_body(St=#hparser{body_state=waiting, ce=CE, clen=Length, method=Method}) ->
333+
St2 = case CE of
334+
_ when Length =:= 0 orelse Method =:= <<"HEAD">> ->
335+
St;
336+
<<"gzip">> ->
337+
Z = zlib:open(),
338+
ok = zlib:inflateInit(Z, ?MAX_WBITS + 16), % http://www.zlib.net/manual.html#Advanced
339+
St#hparser{encoding={zlib,Z}};
340+
<<"deflate">> ->
341+
Z = zlib:open(),
342+
ok = zlib:inflateInit(Z, ?MAX_WBITS),
343+
St#hparser{encoding={zlib,Z}};
344+
_ ->
345+
St
346+
end,
347+
parse_body2(St2);
348+
parse_body(St=#hparser{encoding={zlib,Z}}) ->
349+
case parse_body2(St) of
350+
{ok, Chunk, St2} ->
351+
Chunk2 = iolist_to_binary(zlib:inflate(Z, Chunk)),
352+
{ok, Chunk2, St2};
353+
{done, Rest} ->
354+
Rest2 = iolist_to_binary(zlib:inflate(Z, Rest)),
355+
ok = zlib:inflateEnd(Z),
356+
ok = zlib:close(Z),
357+
{done, Rest2};
358+
Else ->
359+
Else
360+
end;
361+
parse_body(St) ->
362+
parse_body2(St).
363+
364+
parse_body2(St=#hparser{body_state=waiting, te=TE, clen=Length,
329365
method=Method, buffer=Buffer}) ->
330366
case TE of
331367
<<"chunked">> ->
@@ -340,15 +376,14 @@ parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length,
340376
_ ->
341377
{done, Buffer}
342378
end;
343-
parse_body(#hparser{body_state=done, buffer=Buffer}) ->
379+
parse_body2(#hparser{body_state=done, buffer=Buffer}) ->
344380
{done, Buffer};
345-
parse_body(St=#hparser{buffer=Buffer, body_state={stream, _, _, _}})
381+
parse_body2(St=#hparser{buffer=Buffer, body_state={stream, _, _, _}})
346382
when byte_size(Buffer) > 0 ->
347383
transfer_decode(Buffer, St#hparser{buffer= <<>>});
348-
parse_body(St) ->
384+
parse_body2(St) ->
349385
{more, St, <<>>}.
350386

351-
352387
-spec transfer_decode(binary(), #hparser{})
353388
-> {ok, binary(), #hparser{}} | {error, atom()}.
354389
transfer_decode(Data, St=#hparser{
@@ -510,6 +545,8 @@ get_property(method, #hparser{method=Method}) ->
510545
Method;
511546
get_property(transfer_encoding, #hparser{te=TE}) ->
512547
TE;
548+
get_property(content_encoding, #hparser{ce=CE}) ->
549+
CE;
513550
get_property(content_length, #hparser{clen=CLen}) ->
514551
CLen;
515552
get_property(connection, #hparser{connection=Connection}) ->

src/hackney_request.erl

+14-9
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,33 @@ 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
<<>> ->
77-
{Headers2, ReqType0, Body0, Client0};
82+
{Headers3, ReqType0, Body0, Client0};
7883
[] ->
79-
{Headers2, ReqType0, Body0, Client0};
84+
{Headers3, ReqType0, Body0, Client0};
8085
_ ->
81-
handle_body(Headers2, ReqType0, Body0, Client0)
86+
handle_body(Headers3, ReqType0, Body0, Client0)
8287
end,
8388

8489
%% build final client record

0 commit comments

Comments
 (0)