Parse empty cookies (Fixes #308)

This commit is contained in:
Miguel Grinberg
2025-08-13 23:30:09 +01:00
parent cca0b0f693
commit c12d465809
2 changed files with 6 additions and 5 deletions

View File

@@ -366,8 +366,8 @@ class Request:
self.content_type = self.headers['Content-Type'] self.content_type = self.headers['Content-Type']
if 'Cookie' in self.headers: if 'Cookie' in self.headers:
for cookie in self.headers['Cookie'].split(';'): for cookie in self.headers['Cookie'].split(';'):
name, value = cookie.strip().split('=', 1) c = cookie.strip().split('=', 1)
self.cookies[name] = value self.cookies[c[0]] = c[1] if len(c) > 1 else ''
self._body = body self._body = body
self.body_used = False self.body_used = False

View File

@@ -35,16 +35,17 @@ class TestRequest(unittest.TestCase):
def test_headers(self): def test_headers(self):
fd = get_async_request_fd('GET', '/foo', headers={ fd = get_async_request_fd('GET', '/foo', headers={
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Cookie': 'foo=bar;abc=def', 'Cookie': 'foo=bar;nothing;abc=def;',
'Content-Length': '3'}, body='aaa') 'Content-Length': '3'}, body='aaa')
req = self._run(Request.create('app', fd, 'writer', 'addr')) req = self._run(Request.create('app', fd, 'writer', 'addr'))
self.assertEqual(req.headers, { self.assertEqual(req.headers, {
'Host': 'example.com:1234', 'Host': 'example.com:1234',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Cookie': 'foo=bar;abc=def', 'Cookie': 'foo=bar;nothing;abc=def;',
'Content-Length': '3'}) 'Content-Length': '3'})
self.assertEqual(req.content_type, 'application/json') self.assertEqual(req.content_type, 'application/json')
self.assertEqual(req.cookies, {'foo': 'bar', 'abc': 'def'}) self.assertEqual(req.cookies, {'foo': 'bar', 'nothing': '',
'abc': 'def', '': ''})
self.assertEqual(req.content_length, 3) self.assertEqual(req.content_length, 3)
self.assertEqual(req.body, b'aaa') self.assertEqual(req.body, b'aaa')