@@ -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,7 +328,40 @@ parse_trailers(St, Acc) ->
325
328
_ -> error
326
329
end .
327
330
328
- parse_body (St = # hparser {body_state = waiting , te = TE , clen = Length ,
331
+ -define (MAX_WBITS , 15 ). % zconf.h
332
+ parse_body (St = # hparser {body_state = waiting , ce = CE , clen = Length , method = Method }) ->
333
+ St2 = case CE of
334
+ _ when Length =:= 0 orelse Method =:= <<" HEAD" >> ->
335
+ St ;
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 ,
329
365
method = Method , buffer = Buffer }) ->
330
366
case TE of
331
367
<<" chunked" >> ->
@@ -340,15 +376,14 @@ parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length,
340
376
_ ->
341
377
{done , Buffer }
342
378
end ;
343
- parse_body (# hparser {body_state = done , buffer = Buffer }) ->
379
+ parse_body2 (# hparser {body_state = done , buffer = Buffer }) ->
344
380
{done , Buffer };
345
- parse_body (St = # hparser {buffer = Buffer , body_state = {stream , _ , _ , _ }})
381
+ parse_body2 (St = # hparser {buffer = Buffer , body_state = {stream , _ , _ , _ }})
346
382
when byte_size (Buffer ) > 0 ->
347
383
transfer_decode (Buffer , St # hparser {buffer = <<>>});
348
- parse_body (St ) ->
384
+ parse_body2 (St ) ->
349
385
{more , St , <<>>}.
350
386
351
-
352
387
-spec transfer_decode (binary (), # hparser {})
353
388
-> {ok , binary (), # hparser {}} | {error , atom ()}.
354
389
transfer_decode (Data , St = # hparser {
@@ -510,6 +545,8 @@ get_property(method, #hparser{method=Method}) ->
510
545
Method ;
511
546
get_property (transfer_encoding , # hparser {te = TE }) ->
512
547
TE ;
548
+ get_property (content_encoding , # hparser {ce = CE }) ->
549
+ CE ;
513
550
get_property (content_length , # hparser {clen = CLen }) ->
514
551
CLen ;
515
552
get_property (connection , # hparser {connection = Connection }) ->
0 commit comments