WebwareForPython / w4py3

Webware for Python 3
MIT License
20 stars 6 forks source link

Payloads of type application/json with content-length specified now fail if length > 8192 bytes #15

Closed zarquon5 closed 1 year ago

zarquon5 commented 1 year ago

With the recent fix for handling binary payloads, we've discovered a new issue that did not occur with version 3.0.6: if you try to send a payload with a content-type of application/json, and a specified content length > 8192 bytes, then the first 8192 bytes is not preserved.

We have created the following 2 additional test cases for TestFieldStorageModified.py that demonstrates this issue; the first test without an explicit content-length passes, but the second does not: `

def testRequestJSONLargeWithoutContentLength(self):
    value = 'a'*8200
    import json
    d = {'test':value}
    payload = json.dumps(d)
    fs = FieldStorage(
        fp=BytesIO(payload.encode()),
        environ={'REQUEST_METHOD': 'POST',
                 'CONTENT_TYPE': 'application/json'})
    self.assertEqual(fs.headers, {
        'content-type': 'application/json'})
    self.assertEqual(fs.type, 'application/json')
    self.assertEqual(fs.length, -1)
    self.assertEqual(fs.bytes_read, 8212)
    assert fs.file.read() == payload

def testRequestJSONLargeWithContentLength(self):
    value = 'a'*8200
    import json
    d = {'test':value}
    payload = json.dumps(d)
    fs = FieldStorage(
        fp=BytesIO(payload.encode()),
        environ={'REQUEST_METHOD': 'POST',
                 'CONTENT_TYPE': 'application/json',
                 'CONTENT_LENGTH': len(payload)})
    self.assertEqual(fs.headers, {
        'content-type': 'application/json', 'content-length': 8212})
    self.assertEqual(fs.type, 'application/json')
    self.assertEqual(fs.length, 8212)
    self.assertEqual(fs.bytes_read, 8212)
    assert fs.file.read() == payload

`

It isn't clear to me yet how the recent changes caused this problem, but these both pass with 3.0.6.

Cito commented 1 year ago

Thanks for reporting and providing test cases, will look into this later.

Cito commented 1 year ago

This has been fixed now in version 3.0.9. Thanks again for the bug report.