Modify ↓
Opened 5 months ago
Closed 5 months ago
#13761 closed defect (fixed)
Content-Length not sent from Request.send with a bytes instance
Reported by: | Jun Omae | Owned by: | Jun Omae |
---|---|---|---|
Priority: | normal | Milestone: | 1.6.1 |
Component: | general | Version: | 1.6 |
Severity: | normal | Keywords: | |
Cc: | Branch: | ||
Release Notes: |
Fixed |
||
API Changes: | |||
Internal Changes: |
Description
When a bytes
instance is passed to Request.send
, Content-Length
header is not sent. When a unicode
instance is passed, the header wrongly is set (and the method fails with ValueError
).
>>> from trac.test import EnvironmentStub, MockRequest >>> env = EnvironmentStub() >>> req = MockRequest(env) >>> req.send(b'bytes data') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/jun66j5/src/tracdev/git/trac/web/api.py", line 900, in send self._send(content, content_type, status) File "/home/jun66j5/src/tracdev/git/trac/web/api.py", line 1037, in _send raise RequestDone trac.web.api.RequestDone >>> req.response_sent.getvalue() b'bytes data' >>> req.headers_sent {'Cache-Control': 'must-revalidate', 'Expires': 'Fri, 01 Jan 1999 00:00:00 GMT', 'Content-Type': 'text/html;charset=utf-8'} >>> >>> req = MockRequest(env) >>> req.send(u'unicode data') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/jun66j5/src/tracdev/git/trac/web/api.py", line 900, in send self._send(content, content_type, status) File "/home/jun66j5/src/tracdev/git/trac/web/api.py", line 1036, in _send self.write(content) File "/home/jun66j5/src/tracdev/git/trac/web/api.py", line 996, in write raise ValueError("Can't send str content") ValueError: Can't send str content >>> req.headers_sent {'Cache-Control': 'must-revalidate', 'Expires': 'Fri, 01 Jan 1999 00:00:00 GMT', 'Content-Type': 'text/html;charset=utf-8', 'Content-Length': '12'}
-
trac/web/api.py
diff --git a/trac/web/api.py b/trac/web/api.py index 383db0a06..95a060ce0 100644
a b class Request(object): 1028 1028 self.send_header('Cache-Control', 'must-revalidate') 1029 1029 self.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT') 1030 1030 self.send_header('Content-Type', content_type + ';charset=utf-8') 1031 if isinstance(content, str):1031 if isinstance(content, bytes): 1032 1032 self.send_header('Content-Length', len(content)) 1033 1033 self.end_headers(exc_info) 1034 1034 -
trac/web/tests/api.py
diff --git a/trac/web/tests/api.py b/trac/web/tests/api.py index 01010e120..589dd8650 100644
a b new\r\n\ 443 443 with self.assertRaises(ValueError): 444 444 req.write((b'F', 'öo')) 445 445 446 def test_send_bytes(self): 447 req = _make_req(_make_environ(method='GET')) 448 with self.assertRaises(RequestDone): 449 req.send(b'\xef\xbb\xbf') 450 self.assertEqual('3', req.headers_sent.get('Content-Length')) 451 452 def test_send_unicode(self): 453 req = _make_req(_make_environ(method='GET')) 454 with self.assertRaises(ValueError): 455 req.send(u'\ufeff') 456 446 457 def test_send_iterable(self): 447 458 def iterable(): 448 459 yield b'line1,'
Attachments (0)
Change History (1)
comment:1 by , 5 months ago
Release Notes: | modified (diff) |
---|---|
Resolution: | → fixed |
Status: | assigned → closed |
Note:
See TracTickets
for help on using tickets.
Fixed in [17831] and merged in [17833].