Changeset 2431
- Timestamp:
- 06/14/09 13:57:47
- Files:
-
- trunk/cherrypy/lib/httputil.py (modified) (4 diffs)
- trunk/cherrypy/wsgiserver/__init__.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/lib/httputil.py
r2429 r2431 7 7 # to a public caning. 8 8 9 from binascii import b2a_base64 9 10 from BaseHTTPServer import BaseHTTPRequestHandler 10 11 response_codes = BaseHTTPRequestHandler.responses.copy() … … 375 376 """ 376 377 378 protocol=(1, 1) 379 377 380 def elements(self, key): 378 381 """Return a sorted list of HeaderElements for the given header.""" … … 381 384 return header_elements(key, value) 382 385 383 def output(self , protocol=(1, 1)):386 def output(self): 384 387 """Transform self into a list of (name, value) tuples.""" 385 388 header_list = [] … … 392 395 393 396 if isinstance(v, unicode): 394 # HTTP/1.0 says, "Words of *TEXT may contain octets 395 # from character sets other than US-ASCII." and 396 # "Recipients of header field TEXT containing octets 397 # outside the US-ASCII character set may assume that 398 # they represent ISO-8859-1 characters." 399 try: 400 v = v.encode("ISO-8859-1") 401 except UnicodeEncodeError: 402 if protocol == (1, 1): 403 # Encode RFC-2047 TEXT 404 # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?="). 405 # We do our own here instead of using the email module 406 # because we never want to fold lines--folding has 407 # been deprecated by the HTTP working group. 408 from binascii import b2a_base64 409 v = '=?utf-8?b?%s?=' % b2a_base64(v.encode('utf-8')).strip('\n') 410 else: 411 raise 397 v = self.encode(v) 412 398 header_list.append((k, v)) 413 399 return header_list 414 415 400 401 def encode(self, v): 402 """Return the given header value, encoded for HTTP output.""" 403 # HTTP/1.0 says, "Words of *TEXT may contain octets 404 # from character sets other than US-ASCII." and 405 # "Recipients of header field TEXT containing octets 406 # outside the US-ASCII character set may assume that 407 # they represent ISO-8859-1 characters." 408 try: 409 v = v.encode("ISO-8859-1") 410 except UnicodeEncodeError: 411 if self.protocol == (1, 1): 412 # Encode RFC-2047 TEXT 413 # (e.g. u"\u8200" -> "=?utf-8?b?6IiA?="). 414 # We do our own here instead of using the email module 415 # because we never want to fold lines--folding has 416 # been deprecated by the HTTP working group. 417 v = b2a_base64(v.encode('utf-8')) 418 v = ('=?utf-8?b?' + v.strip('\n') + '?=') 419 else: 420 raise 421 return v 416 422 417 423 class Host(object): trunk/cherrypy/wsgiserver/__init__.py
r2429 r2431 150 150 apps: a dict or list of (path_prefix, app) pairs. 151 151 """ 152 152 153 153 def __init__(self, apps): 154 154 try: … … 156 156 except AttributeError: 157 157 pass 158 158 159 159 # Sort the apps by len(path), descending 160 160 apps.sort(cmp=lambda x,y: cmp(len(x[0]), len(y[0]))) 161 161 apps.reverse() 162 162 163 163 # The path_prefix strings must start, but not end, with a slash. 164 164 # Use "" instead of "/". … … 707 707 except TypeError: 708 708 if not isinstance(k, str): 709 raise TypeError("WSGI response header key %r is not a string.")709 raise TypeError("WSGI response header key %r is not a byte string.") 710 710 if not isinstance(v, str): 711 raise TypeError("WSGI response header value %r is not a string.")711 raise TypeError("WSGI response header value %r is not a byte string.") 712 712 else: 713 713 raise

