Skip to content

Commit

Permalink
Merge pull request #137 from kxepal/ascii-headers
Browse files Browse the repository at this point in the history
Strictly require ASCII headers names and values
fafhrd91 committed Aug 30, 2014
2 parents a673281 + 29501d8 commit 90f0589
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 8 additions & 2 deletions aiohttp/protocol.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
import http.server
import itertools
import re
import string
import sys
import zlib
from wsgiref.handlers import format_date_time
@@ -20,6 +21,7 @@
from aiohttp import multidict
from aiohttp.log import internal_log

ASCIISET = set(string.printable)
METHRE = re.compile('[A-Z0-9$-_.]+')
VERSRE = re.compile('HTTP/(\d+).(\d+)')
HDRRE = re.compile('[\x00-\x1F\x7F()<>@,;:\[\]={} \t\\\\\"]')
@@ -572,8 +574,12 @@ def add_header(self, name, value):
"""Analyze headers. Calculate content length,
removes hop headers, etc."""
assert not self.headers_sent, 'headers have been sent already'
assert isinstance(name, str), '{!r} is not a string'.format(name)
assert isinstance(value, str), '{!r} is not a string'.format(value)
assert isinstance(name, str), \
'Header name should be a string, got {!r}'.format(name)
assert set(name).issubset(ASCIISET), \
'Header name should contain ASCII chars, got {!r}'.format(name)
assert isinstance(value, str), \
'Header {!r} should have string value, got {!r}'.format(name, value)

name = name.strip().upper()
value = value.strip()
12 changes: 11 additions & 1 deletion tests/test_http_protocol.py
Original file line number Diff line number Diff line change
@@ -76,12 +76,22 @@ def test_add_header_with_spaces(self):
self.assertEqual(
[('CONTENT-TYPE', 'plain/html')], list(msg.headers.items()))

def test_add_header_non_ascii(self):
msg = protocol.Response(self.transport, 200)
self.assertEqual([], list(msg.headers))

with self.assertRaises(AssertionError):
msg.add_header('тип-контента', 'текст/плейн')

def test_add_header_invalid_value_type(self):
msg = protocol.Response(self.transport, 200)
self.assertEqual([], list(msg.headers))

with self.assertRaises(AssertionError):
msg.add_header('content-type', b'value')
msg.add_header('content-type', {'test': 'plain'})

with self.assertRaises(AssertionError):
msg.add_header(list('content-type'), 'text/plain')

def test_add_headers(self):
msg = protocol.Response(self.transport, 200)

0 comments on commit 90f0589

Please sign in to comment.