#NEWLINE# def decode_response( response ) : #NEWLINE# #INDENT# '\n    If the response is compressed with gzip or deflate:\n      1. decompress the response body\n      2. delete the content-encoding header\n      3. update content-length header to decompressed length\n    ' #NEWLINE# def is_compressed( headers ) : #NEWLINE# #INDENT# encoding = headers . get ( 'content-encoding' , [ ] ) #NEWLINE# return ( encoding and ( encoding [ 0 ] in ( 'gzip' , 'deflate' ) ) ) #NEWLINE# #UNINDENT# def decompress_body( body , encoding ) : #NEWLINE# #INDENT# 'Returns decompressed body according to encoding using zlib.\n        to (de-)compress gzip format, use wbits = zlib.MAX_WBITS | 16\n        ' #NEWLINE# if ( encoding == 'gzip' ) : #NEWLINE# #INDENT# return zlib . decompress ( body , ( zlib . MAX_WBITS | 16 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return zlib . decompress ( body ) #NEWLINE# #UNINDENT# #UNINDENT# headers = CaseInsensitiveDict ( response [ 'headers' ] ) #NEWLINE# if is_compressed ( headers ) : #NEWLINE# #INDENT# response = copy . deepcopy ( response ) #NEWLINE# encoding = headers [ 'content-encoding' ] [ 0 ] #NEWLINE# headers [ 'content-encoding' ] . remove ( encoding ) #NEWLINE# if ( not headers [ 'content-encoding' ] ) : #NEWLINE# #INDENT# del headers [ 'content-encoding' ] #NEWLINE# #UNINDENT# new_body = decompress_body ( response [ 'body' ] [ 'string' ] , encoding ) #NEWLINE# response [ 'body' ] [ 'string' ] = new_body #NEWLINE# headers [ 'content-length' ] = [ str ( len ( new_body ) ) ] #NEWLINE# response [ 'headers' ] = dict ( headers ) #NEWLINE# #UNINDENT# return response