'Decode the \'chunked\' transfer coding.'

def decode_chunked(self):

    cl = 0
    data = StringIO.StringIO()
    while True:
        line = self.rfile.readline().strip().split(';', 1)
        chunk_size = int(line.pop(0), 16)
        if (chunk_size <= 0):
            break
        cl += chunk_size
        data.write(self.rfile.read(chunk_size))
        crlf = self.rfile.read(2)
        if (crlf != '\r\n'):
            self.simple_response('400    Bad    Request', ("Bad    chunked    transfer    coding    (expected    '\\r\\n',    got    %r)" % crlf))
            return
    self.read_headers()
    data.seek(0)
    self.environ['wsgi.input'] = data
    self.environ['CONTENT_LENGTH'] = (str(cl) or '')
    return True
