@@ -305,6 +305,9 @@ parse_header(Line, St) ->
305
305
<<" transfer-encoding" >> ->
306
306
TE = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
307
307
St # hparser {te = TE };
308
+ <<" content-encoding" >> ->
309
+ CE = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
310
+ St # hparser {ce = CE };
308
311
<<" connection" >> ->
309
312
Connection = hackney_bstr :to_lower (hackney_bstr :trim (Value )),
310
313
St # hparser {connection = Connection };
@@ -330,9 +333,40 @@ parse_trailers(St, Acc) ->
330
333
_ -> error
331
334
end .
332
335
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 }) ->
336
370
case {TE , Length } of
337
371
{<<" chunked" >>, _ } ->
338
372
parse_body (St # hparser {body_state =
@@ -346,14 +380,13 @@ parse_body(St=#hparser{body_state=waiting, te=TE, clen=Length, buffer=Buffer}) -
346
380
St # hparser {body_state = {stream , fun te_identity /2 , {0 , Length }, fun ce_identity /1 }}
347
381
)
348
382
end ;
349
- parse_body (# hparser {body_state = done , buffer = Buffer }) ->
383
+ parse_body2 (# hparser {body_state = done , buffer = Buffer }) ->
350
384
{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 ->
352
386
transfer_decode (Buffer , St # hparser {buffer = <<>>});
353
- parse_body (St ) ->
387
+ parse_body2 (St ) ->
354
388
{more , St , <<>>}.
355
389
356
-
357
390
-spec transfer_decode (binary (), # hparser {})
358
391
-> {ok , binary (), # hparser {}} | {done , binary ()} | {error , atom ()}.
359
392
transfer_decode (Data , St = # hparser {
@@ -518,6 +551,8 @@ get_property(method, #hparser{method=Method}) ->
518
551
Method ;
519
552
get_property (transfer_encoding , # hparser {te = TE }) ->
520
553
TE ;
554
+ get_property (content_encoding , # hparser {ce = CE }) ->
555
+ CE ;
521
556
get_property (content_length , # hparser {clen = CLen }) ->
522
557
CLen ;
523
558
get_property (connection , # hparser {connection = Connection }) ->
0 commit comments