File tree 2 files changed +24
-2
lines changed
2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change 62
62
from asyncio import Future # noqa: F401
63
63
import unittest # noqa: F401
64
64
65
+ # To be used with str.strip() and related methods.
66
+ HTTP_WHITESPACE = " \t "
67
+
65
68
66
69
@lru_cache (1000 )
67
70
def _normalize_header (name : str ) -> str :
@@ -171,15 +174,15 @@ def parse_line(self, line: str) -> None:
171
174
# continuation of a multi-line header
172
175
if self ._last_key is None :
173
176
raise HTTPInputError ("first header line cannot start with whitespace" )
174
- new_part = " " + line .lstrip ()
177
+ new_part = " " + line .lstrip (HTTP_WHITESPACE )
175
178
self ._as_list [self ._last_key ][- 1 ] += new_part
176
179
self ._dict [self ._last_key ] += new_part
177
180
else :
178
181
try :
179
182
name , value = line .split (":" , 1 )
180
183
except ValueError :
181
184
raise HTTPInputError ("no colon in header line" )
182
- self .add (name , value .strip ())
185
+ self .add (name , value .strip (HTTP_WHITESPACE ))
183
186
184
187
@classmethod
185
188
def parse (cls , headers : str ) -> "HTTPHeaders" :
Original file line number Diff line number Diff line change @@ -334,6 +334,25 @@ def test_unicode_newlines(self):
334
334
gen_log .warning ("failed while trying %r in %s" , newline , encoding )
335
335
raise
336
336
337
+ def test_unicode_whitespace (self ):
338
+ # Only tabs and spaces are to be stripped according to the HTTP standard.
339
+ # Other unicode whitespace is to be left as-is. In the context of headers,
340
+ # this specifically means the whitespace characters falling within the
341
+ # latin1 charset.
342
+ whitespace = [
343
+ (" " , True ), # SPACE
344
+ ("\t " , True ), # TAB
345
+ ("\u00a0 " , False ), # NON-BREAKING SPACE
346
+ ("\u0085 " , False ), # NEXT LINE
347
+ ]
348
+ for c , stripped in whitespace :
349
+ headers = HTTPHeaders .parse ("Transfer-Encoding: %schunked" % c )
350
+ if stripped :
351
+ expected = [("Transfer-Encoding" , "chunked" )]
352
+ else :
353
+ expected = [("Transfer-Encoding" , "%schunked" % c )]
354
+ self .assertEqual (expected , list (headers .get_all ()))
355
+
337
356
def test_optional_cr (self ):
338
357
# Both CRLF and LF should be accepted as separators. CR should not be
339
358
# part of the data when followed by LF, but it is a normal char
You can’t perform that action at this time.
0 commit comments