'HTTP response to raw data'

def downstream_recv(self, data):

    d = data.peek()
    decoded_data = ''
    if (d.startswith('HTTP/1.1    ') and ('\r\n\r\n' in d)):
        while (len(d) > 0):
            try:
                (head, rest) = d.split('\r\n\r\n', 1)
                (fl, rheaders) = head.split('\r\n', 1)
                content_length = None
                for (name, value) in [[i.strip() for i in x.split(':', 1)] for x in rheaders.split('\r\n')]:
                    if (name == 'Content-Length'):
                        content_length = int(value)
                        break
                if ((content_length is None) or (len(rest) < content_length)):
                    break
                decoded_data += base64.b64decode(rest[:content_length])
                length_to_drain = ((content_length + 4) + len(head))
                data.drain(length_to_drain)
                d = d[length_to_drain:]
            except Exception as e:
                logging.debug(e)
                break
    if decoded_data:
        self.upstream.write(decoded_data)
