@@ -299,6 +299,9 @@ parse_header(Line, St) ->
299299 <<" transfer-encoding" >> ->
300300 TE = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
301301 St # hparser {te = TE };
302+ <<" content-encoding" >> ->
303+ CE = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
304+ St # hparser {ce = CE };
302305 <<" connection" >> ->
303306 Connection = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
304307 St # hparser {connection = Connection };
@@ -331,9 +334,31 @@ parse_trailers(St, Acc) ->
331334 {more , St2 } -> {more , St2 }
332335 end .
333336
334- parse_body (# hparser {body_state = waiting , method = <<" HEAD" >>, buffer = Buffer }) ->
335- {done , Buffer };
336- parse_body (St = # hparser {body_state = waiting , te = TE , clen = Length , buffer = Buffer }) ->
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 }) when CE == <<" gzip" >>; CE == <<" deflate" >> ->
340+ MaxWBITS = 15 , % zconf.h
341+ WB = MaxWBITS + if CE == <<" gzip" >> -> 16 ; true -> 0 end , % http://www.zlib.net/manual.html#Advanced
342+ Z = zlib :open (),
343+ ok = zlib :inflateInit (Z , WB ),
344+ parse_body2 (St # hparser {encoding = {zlib ,Z }});
345+ parse_body (St = # hparser {encoding = {zlib ,Z }}) ->
346+ case parse_body2 (St ) of
347+ {ok , Chunk , St2 } ->
348+ Chunk2 = iolist_to_binary (zlib :inflate (Z , Chunk )),
349+ {ok , Chunk2 , St2 };
350+ {done , Rest } ->
351+ Rest2 = iolist_to_binary (zlib :inflate (Z , Rest )),
352+ ok = zlib :inflateEnd (Z ),
353+ ok = zlib :close (Z ),
354+ {done , Rest2 };
355+ Else ->
356+ Else
357+ end ;
358+ parse_body (St ) ->
359+ parse_body2 (St ).
360+
361+ parse_body2 (St = # hparser {body_state = waiting , te = TE , clen = Length , buffer = Buffer }) ->
337362 case {TE , Length } of
338363 {<<" chunked" >>, _ } ->
339364 parse_body (St # hparser {body_state =
@@ -347,14 +372,13 @@ parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length, buffer=Buffer}) -
347372 St # hparser {body_state = {stream , fun te_identity /2 , {0 , Length }, fun ce_identity /1 }}
348373 )
349374 end ;
350- parse_body (# hparser {body_state = done , buffer = Buffer }) ->
375+ parse_body2 (# hparser {body_state = done , buffer = Buffer }) ->
351376 {done , Buffer };
352- parse_body (St = # hparser {buffer = Buffer , body_state = {stream , _ , _ , _ }}) when byte_size (Buffer ) > 0 ->
377+ parse_body2 (St = # hparser {buffer = Buffer , body_state = {stream , _ , _ , _ }}) when byte_size (Buffer ) > 0 ->
353378 transfer_decode (Buffer , St # hparser {buffer = <<>>});
354- parse_body (St ) ->
379+ parse_body2 (St ) ->
355380 {more , St , <<>>}.
356381
357-
358382-spec transfer_decode (binary (), # hparser {})
359383 -> {ok , binary (), # hparser {}} | {done , binary ()} | {error , atom ()}.
360384transfer_decode (Data , St = # hparser {
@@ -514,6 +538,8 @@ get_property(method, #hparser{method=Method}) ->
514538 Method ;
515539get_property (transfer_encoding , # hparser {te = TE }) ->
516540 TE ;
541+ get_property (content_encoding , # hparser {ce = CE }) ->
542+ CE ;
517543get_property (content_length , # hparser {clen = CLen }) ->
518544 CLen ;
519545get_property (connection , # hparser {connection = Connection }) ->
0 commit comments