Issue #421 resolved

Incorrect border condition in readline of SizeCheckWrapper

anonymous
created an issue

The following piece of code: {{{ while True: data = self.rfile.readline(256) self.bytes_read += len(data) self._check_length() res.append(data) if len(data) < 256: return ''.join(res) }}} should be like: {{{ while True: data = self.rfile.readline(256) self.bytes_read += len(data) self._check_length() res.append(data) if len(data) < 256 or data[-1] == '\n': return ''.join(res) }}} Reason: When '' '''self.rfile.readline(256)''' '' returns a line of exactly 256 characters including '''\r\n''' (let's call it 256line) then the original code thinks it should continue to read. This behaviour causes that the next line is getting merged with 256line and the method returns two lines merged. In case when the 256line is the last line of request's head then the merged result contains the last non empty line and the following empty line that should have indicated the end of request's head. As a result of that mimetools code keeps reading in search of an empty line indicating header's end and the web server locks up waiting for an empty line that never comes...

Greetings.

Comments (6)

  1. Anonymous

    This solution doesn't work with Python3. For Python3 the condition should be instead:

    if len(data) < 256 or data[-1:].decode() == "\n":
    

    or, for compatibility with Python2 maybe like this:

    if len(data) < 256 or type(data) == str and data[-1:] == "\n" or data[-1] == b'\n'[0]:
    

    Haven't tried that with Python2 however.

    This seems like a pretty serious issue. Looks like not many guys out there use Python3.

    Regards, Stefan

  2. Log in to comment