@@ -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,9 +328,40 @@ parse_trailers(St, Acc) ->
325328 _ -> error
326329 end .
327330
328- parse_body (# hparser {body_state = waiting , method = <<" HEAD" >>, buffer = Buffer }) ->
329- {done , Buffer };
330- parse_body (St = # hparser {body_state = waiting , te = TE , clen = Length , buffer = Buffer }) ->
331+ -define (MAX_WBITS , 15 ). % zconf.h
332+ parse_body (# hparser {body_state = waiting , method = Method , buffer = Buffer , clen = Length }) when Method == <<" HEAD" >>; Length =:= 0 ->
333+ {done , Buffer };
334+ parse_body (St = # hparser {body_state = waiting , ce = CE }) ->
335+ St2 = case CE of
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 , buffer = Buffer }) ->
331365 case {TE , Length } of
332366 {<<" chunked" >>, _ } ->
333367 parse_body (St # hparser {body_state =
@@ -341,14 +375,13 @@ parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length, buffer=Buffer}) -
341375 St # hparser {body_state = {stream , fun te_identity /2 , {0 , Length }, fun ce_identity /1 }}
342376 )
343377 end ;
344- parse_body (# hparser {body_state = done , buffer = Buffer }) ->
378+ parse_body2 (# hparser {body_state = done , buffer = Buffer }) ->
345379 {done , Buffer };
346- parse_body (St = # hparser {buffer = Buffer , body_state = {stream , _ , _ , _ }}) when byte_size (Buffer ) > 0 ->
380+ parse_body2 (St = # hparser {buffer = Buffer , body_state = {stream , _ , _ , _ }}) when byte_size (Buffer ) > 0 ->
347381 transfer_decode (Buffer , St # hparser {buffer = <<>>});
348- parse_body (St ) ->
382+ parse_body2 (St ) ->
349383 {more , St , <<>>}.
350384
351-
352385-spec transfer_decode (binary (), # hparser {})
353386 -> {ok , binary (), # hparser {}} | {error , atom ()}.
354387transfer_decode (Data , St = # hparser {
@@ -513,6 +546,8 @@ get_property(method, #hparser{method=Method}) ->
513546 Method ;
514547get_property (transfer_encoding , # hparser {te = TE }) ->
515548 TE ;
549+ get_property (content_encoding , # hparser {ce = CE }) ->
550+ CE ;
516551get_property (content_length , # hparser {clen = CLen }) ->
517552 CLen ;
518553get_property (connection , # hparser {connection = Connection }) ->
0 commit comments