@@ -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,40 @@ 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+ -define (MAX_WBITS , 15 ). % zconf.h
337+ parse_body (# hparser {body_state = waiting , method = Method , buffer = Buffer , clen = Length }) when Method == <<" HEAD" >>; Length =:= 0 ->
338+ {done , Buffer };
339+ parse_body (St = # hparser {body_state = waiting , ce = CE }) ->
340+ St2 = case CE of
341+ <<" gzip" >> ->
342+ Z = zlib :open (),
343+ ok = zlib :inflateInit (Z , ? MAX_WBITS + 16 ), % http://www.zlib.net/manual.html#Advanced
344+ St # hparser {encoding = {zlib ,Z }};
345+ <<" deflate" >> ->
346+ Z = zlib :open (),
347+ ok = zlib :inflateInit (Z , ? MAX_WBITS ),
348+ St # hparser {encoding = {zlib ,Z }};
349+ _ ->
350+ St
351+ end ,
352+ parse_body2 (St2 );
353+ parse_body (St = # hparser {encoding = {zlib ,Z }}) ->
354+ case parse_body2 (St ) of
355+ {ok , Chunk , St2 } ->
356+ Chunk2 = iolist_to_binary (zlib :inflate (Z , Chunk )),
357+ {ok , Chunk2 , St2 };
358+ {done , Rest } ->
359+ Rest2 = iolist_to_binary (zlib :inflate (Z , Rest )),
360+ ok = zlib :inflateEnd (Z ),
361+ ok = zlib :close (Z ),
362+ {done , Rest2 };
363+ Else ->
364+ Else
365+ end ;
366+ parse_body (St ) ->
367+ parse_body2 (St ).
368+
369+ parse_body2 (St = # hparser {body_state = waiting , te = TE , clen = Length , buffer = Buffer }) ->
336370 case {TE , Length } of
337371 {<<" chunked" >>, _ } ->
338372 parse_body (St # hparser {body_state =
@@ -346,14 +380,13 @@ parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length, buffer=Buffer}) -
346380 St # hparser {body_state = {stream , fun te_identity /2 , {0 , Length }, fun ce_identity /1 }}
347381 )
348382 end ;
349- parse_body (# hparser {body_state = done , buffer = Buffer }) ->
383+ parse_body2 (# hparser {body_state = done , buffer = Buffer }) ->
350384 {done , Buffer };
351- parse_body (St = # hparser {buffer = Buffer , body_state = {stream , _ , _ , _ }}) when byte_size (Buffer ) > 0 ->
385+ parse_body2 (St = # hparser {buffer = Buffer , body_state = {stream , _ , _ , _ }}) when byte_size (Buffer ) > 0 ->
352386 transfer_decode (Buffer , St # hparser {buffer = <<>>});
353- parse_body (St ) ->
387+ parse_body2 (St ) ->
354388 {more , St , <<>>}.
355389
356-
357390-spec transfer_decode (binary (), # hparser {})
358391 -> {ok , binary (), # hparser {}} | {done , binary ()} | {error , atom ()}.
359392transfer_decode (Data , St = # hparser {
@@ -518,6 +551,8 @@ get_property(method, #hparser{method=Method}) ->
518551 Method ;
519552get_property (transfer_encoding , # hparser {te = TE }) ->
520553 TE ;
554+ get_property (content_encoding , # hparser {ce = CE }) ->
555+ CE ;
521556get_property (content_length , # hparser {clen = CLen }) ->
522557 CLen ;
523558get_property (connection , # hparser {connection = Connection }) ->
0 commit comments