Edgewall Software
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 Request.send not set Content-Length header for a bytes instance.

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):  
    10281028        self.send_header('Cache-Control', 'must-revalidate')
    10291029        self.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT')
    10301030        self.send_header('Content-Type', content_type + ';charset=utf-8')
    1031         if isinstance(content, str):
     1031        if isinstance(content, bytes):
    10321032            self.send_header('Content-Length', len(content))
    10331033        self.end_headers(exc_info)
    10341034
  • 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\  
    443443        with self.assertRaises(ValueError):
    444444            req.write((b'F', 'öo'))
    445445
     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
    446457    def test_send_iterable(self):
    447458        def iterable():
    448459            yield b'line1,'

Attachments (0)

Change History (1)

comment:1 by Jun Omae, 5 months ago

Release Notes: modified (diff)
Resolution: fixed
Status: assignedclosed

Fixed in [17831] and merged in [17833].

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Jun Omae.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Jun Omae to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.