@@ -299,6 +299,9 @@ parse_header(Line, St) ->
299
299
<<" transfer-encoding" >> ->
300
300
TE = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
301
301
St # hparser {te = TE };
302
+ <<" content-encoding" >> ->
303
+ CE = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
304
+ St # hparser {ce = CE };
302
305
<<" connection" >> ->
303
306
Connection = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
304
307
St # hparser {connection = Connection };
@@ -331,9 +334,31 @@ parse_trailers(St, Acc) ->
331
334
{more , St2 } -> {more , St2 }
332
335
end .
333
336
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 }) ->
337
362
case {TE , Length } of
338
363
{<<" chunked" >>, _ } ->
339
364
parse_body (St # hparser {body_state =
@@ -347,14 +372,13 @@ parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length, buffer=Buffer}) -
347
372
St # hparser {body_state = {stream , fun te_identity /2 , {0 , Length }, fun ce_identity /1 }}
348
373
)
349
374
end ;
350
- parse_body (# hparser {body_state = done , buffer = Buffer }) ->
375
+ parse_body2 (# hparser {body_state = done , buffer = Buffer }) ->
351
376
{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 ->
353
378
transfer_decode (Buffer , St # hparser {buffer = <<>>});
354
- parse_body (St ) ->
379
+ parse_body2 (St ) ->
355
380
{more , St , <<>>}.
356
381
357
-
358
382
-spec transfer_decode (binary (), # hparser {})
359
383
-> {ok , binary (), # hparser {}} | {done , binary ()} | {error , atom ()}.
360
384
transfer_decode (Data , St = # hparser {
@@ -514,6 +538,8 @@ get_property(method, #hparser{method=Method}) ->
514
538
Method ;
515
539
get_property (transfer_encoding , # hparser {te = TE }) ->
516
540
TE ;
541
+ get_property (content_encoding , # hparser {ce = CE }) ->
542
+ CE ;
517
543
get_property (content_length , # hparser {clen = CLen }) ->
518
544
CLen ;
519
545
get_property (connection , # hparser {connection = Connection }) ->
0 commit comments