@@ -300,6 +300,9 @@ parse_header(Line, St) ->
300
300
<<" transfer-encoding" >> ->
301
301
TE = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
302
302
St # hparser {te = TE };
303
+ <<" content-encoding" >> ->
304
+ CE = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
305
+ St # hparser {ce = CE };
303
306
<<" connection" >> ->
304
307
Connection = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
305
308
St # hparser {connection = Connection };
@@ -325,9 +328,40 @@ parse_trailers(St, Acc) ->
325
328
_ -> error
326
329
end .
327
330
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 }) ->
331
365
case {TE , Length } of
332
366
{<<" chunked" >>, _ } ->
333
367
parse_body (St # hparser {body_state =
@@ -341,14 +375,13 @@ parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length, buffer=Buffer}) -
341
375
St # hparser {body_state = {stream , fun te_identity /2 , {0 , Length }, fun ce_identity /1 }}
342
376
)
343
377
end ;
344
- parse_body (# hparser {body_state = done , buffer = Buffer }) ->
378
+ parse_body2 (# hparser {body_state = done , buffer = Buffer }) ->
345
379
{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 ->
347
381
transfer_decode (Buffer , St # hparser {buffer = <<>>});
348
- parse_body (St ) ->
382
+ parse_body2 (St ) ->
349
383
{more , St , <<>>}.
350
384
351
-
352
385
-spec transfer_decode (binary (), # hparser {})
353
386
-> {ok , binary (), # hparser {}} | {error , atom ()}.
354
387
transfer_decode (Data , St = # hparser {
@@ -513,6 +546,8 @@ get_property(method, #hparser{method=Method}) ->
513
546
Method ;
514
547
get_property (transfer_encoding , # hparser {te = TE }) ->
515
548
TE ;
549
+ get_property (content_encoding , # hparser {ce = CE }) ->
550
+ CE ;
516
551
get_property (content_length , # hparser {clen = CLen }) ->
517
552
CLen ;
518
553
get_property (connection , # hparser {connection = Connection }) ->
0 commit comments