summaryrefslogtreecommitdiff
path: root/cherrypy/lib/encoding.py
diff options
context:
space:
mode:
authorGustavo Picon <tabo@tabo.pe>2012-04-03 00:02:27 -0500
committerGustavo Picon <tabo@tabo.pe>2012-04-03 00:02:27 -0500
commit0d9bf9db6e6a12dc1a94c0153723f67d33e57673 (patch)
tree23e0b1a01a9515e833f7f7c83013dfc2262c46bd /cherrypy/lib/encoding.py
parentef56af7d85746127e38b3d37c2c413e72d42a401 (diff)
downloadcherrypy-git-0d9bf9db6e6a12dc1a94c0153723f67d33e57673.tar.gz
Removed trailing whitespace from the codebase.
sed -i '' -e 's/ *$//' `find cherrypy -name '*.py'`
Diffstat (limited to 'cherrypy/lib/encoding.py')
-rw-r--r--cherrypy/lib/encoding.py78
1 files changed, 39 insertions, 39 deletions
diff --git a/cherrypy/lib/encoding.py b/cherrypy/lib/encoding.py
index 64597465..1f68143b 100644
--- a/cherrypy/lib/encoding.py
+++ b/cherrypy/lib/encoding.py
@@ -9,19 +9,19 @@ from cherrypy.lib import set_vary_header
def decode(encoding=None, default_encoding='utf-8'):
"""Replace or extend the list of charsets used to decode a request entity.
-
+
Either argument may be a single string or a list of strings.
-
+
encoding
If not None, restricts the set of charsets attempted while decoding
a request entity to the given set (even if a different charset is given in
the Content-Type request header).
-
+
default_encoding
Only in effect if the 'encoding' argument is not given.
If given, the set of charsets attempted while decoding a request entity is
*extended* with the given value(s).
-
+
"""
body = cherrypy.request.body
if encoding is not None:
@@ -35,7 +35,7 @@ def decode(encoding=None, default_encoding='utf-8'):
class ResponseEncoder:
-
+
default_encoding = 'utf-8'
failmsg = "Response body could not be encoded with %r."
encoding = None
@@ -43,11 +43,11 @@ class ResponseEncoder:
text_only = True
add_charset = True
debug = False
-
+
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
-
+
self.attempted_charsets = set()
request = cherrypy.serving.request
if request.handler is not None:
@@ -56,17 +56,17 @@ class ResponseEncoder:
cherrypy.log('Replacing request.handler', 'TOOLS.ENCODE')
self.oldhandler = request.handler
request.handler = self
-
+
def encode_stream(self, encoding):
"""Encode a streaming response body.
-
+
Use a generator wrapper, and just pray it works as the stream is
being written out.
"""
if encoding in self.attempted_charsets:
return False
self.attempted_charsets.add(encoding)
-
+
def encoder(body):
for chunk in body:
if isinstance(chunk, unicodestr):
@@ -74,13 +74,13 @@ class ResponseEncoder:
yield chunk
self.body = encoder(self.body)
return True
-
+
def encode_string(self, encoding):
"""Encode a buffered response body."""
if encoding in self.attempted_charsets:
return False
self.attempted_charsets.add(encoding)
-
+
try:
body = []
for chunk in self.body:
@@ -92,11 +92,11 @@ class ResponseEncoder:
return False
else:
return True
-
+
def find_acceptable_charset(self):
request = cherrypy.serving.request
response = cherrypy.serving.response
-
+
if self.debug:
cherrypy.log('response.stream %r' % response.stream, 'TOOLS.ENCODE')
if response.stream:
@@ -115,14 +115,14 @@ class ResponseEncoder:
# >>> len(t.encode("utf7"))
# 8
del response.headers["Content-Length"]
-
+
# Parse the Accept-Charset request header, and try to provide one
# of the requested charsets (in order of user preference).
encs = request.headers.elements('Accept-Charset')
charsets = [enc.value.lower() for enc in encs]
if self.debug:
cherrypy.log('charsets %s' % repr(charsets), 'TOOLS.ENCODE')
-
+
if self.encoding is not None:
# If specified, force this encoding to be used, or fail.
encoding = self.encoding.lower()
@@ -160,7 +160,7 @@ class ResponseEncoder:
'0)' % element, 'TOOLS.ENCODE')
if encoder(encoding):
return encoding
-
+
if "*" not in charsets:
# If no "*" is present in an Accept-Charset field, then all
# character sets not explicitly mentioned get a quality
@@ -173,7 +173,7 @@ class ResponseEncoder:
'TOOLS.ENCODE')
if encoder(iso):
return iso
-
+
# No suitable encoding found.
ac = request.headers.get('Accept-Charset')
if ac is None:
@@ -182,11 +182,11 @@ class ResponseEncoder:
msg = "Your client sent this Accept-Charset header: %s." % ac
msg += " We tried these charsets: %s." % ", ".join(self.attempted_charsets)
raise cherrypy.HTTPError(406, msg)
-
+
def __call__(self, *args, **kwargs):
response = cherrypy.serving.response
self.body = self.oldhandler(*args, **kwargs)
-
+
if isinstance(self.body, basestring):
# strings get wrapped in a list because iterating over a single
# item list is much faster than iterating over every character
@@ -200,7 +200,7 @@ class ResponseEncoder:
self.body = file_generator(self.body)
elif self.body is None:
self.body = []
-
+
ct = response.headers.elements("Content-Type")
if self.debug:
cherrypy.log('Content-Type: %r' % [str(h) for h in ct], 'TOOLS.ENCODE')
@@ -222,7 +222,7 @@ class ResponseEncoder:
if self.debug:
cherrypy.log('Finding because not text_only', 'TOOLS.ENCODE')
do_find = True
-
+
if do_find:
# Set "charset=..." param on response Content-Type header
ct.params['charset'] = self.find_acceptable_charset()
@@ -231,7 +231,7 @@ class ResponseEncoder:
cherrypy.log('Setting Content-Type %s' % ct,
'TOOLS.ENCODE')
response.headers["Content-Type"] = str(ct)
-
+
return self.body
# GZIP
@@ -239,7 +239,7 @@ class ResponseEncoder:
def compress(body, compress_level):
"""Compress 'body' at the given compress_level."""
import zlib
-
+
# See http://www.gzip.org/zlib/rfc-gzip.html
yield ntob('\x1f\x8b') # ID1 and ID2: gzip marker
yield ntob('\x08') # CM: compression method
@@ -248,7 +248,7 @@ def compress(body, compress_level):
yield struct.pack("<L", int(time.time()) & int('FFFFFFFF', 16))
yield ntob('\x02') # XFL: max compression, slowest algo
yield ntob('\xff') # OS: unknown
-
+
crc = zlib.crc32(ntob(""))
size = 0
zobj = zlib.compressobj(compress_level,
@@ -259,7 +259,7 @@ def compress(body, compress_level):
crc = zlib.crc32(line, crc)
yield zobj.compress(line)
yield zobj.flush()
-
+
# CRC32: 4 bytes
yield struct.pack("<L", crc & int('FFFFFFFF', 16))
# ISIZE: 4 bytes
@@ -267,7 +267,7 @@ def compress(body, compress_level):
def decompress(body):
import gzip
-
+
zbuf = BytesIO()
zbuf.write(body)
zbuf.seek(0)
@@ -279,7 +279,7 @@ def decompress(body):
def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
"""Try to gzip the response body if Content-Type in mime_types.
-
+
cherrypy.response.headers['Content-Type'] must be set to one of the
values in the mime_types arg before calling this function.
@@ -287,32 +287,32 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
* type/subtype
* type/*
* type/*+subtype
-
+
No compression is performed if any of the following hold:
* The client sends no Accept-Encoding request header
* No 'gzip' or 'x-gzip' is present in the Accept-Encoding header
* No 'gzip' or 'x-gzip' with a qvalue > 0 is present
* The 'identity' value is given with a qvalue > 0.
-
+
"""
request = cherrypy.serving.request
response = cherrypy.serving.response
-
+
set_vary_header(response, "Accept-Encoding")
-
+
if not response.body:
# Response body is empty (might be a 304 for instance)
if debug:
cherrypy.log('No response body', context='TOOLS.GZIP')
return
-
+
# If returning cached content (which should already have been gzipped),
# don't re-zip.
if getattr(request, "cached", False):
if debug:
cherrypy.log('Not gzipping cached response', context='TOOLS.GZIP')
return
-
+
acceptable = request.headers.elements('Accept-Encoding')
if not acceptable:
# If no Accept-Encoding field is present in a request,
@@ -325,7 +325,7 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
if debug:
cherrypy.log('No Accept-Encoding', context='TOOLS.GZIP')
return
-
+
ct = response.headers.get('Content-Type', '').split(';')[0]
for coding in acceptable:
if coding.value == 'identity' and coding.qvalue != 0:
@@ -339,7 +339,7 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
cherrypy.log('Zero gzip qvalue: %s' % coding,
context='TOOLS.GZIP')
return
-
+
if ct not in mime_types:
# If the list of provided mime-types contains tokens
# such as 'text/*' or 'application/*+xml',
@@ -370,7 +370,7 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
cherrypy.log('Content-Type %s not in mime_types %r' %
(ct, mime_types), context='TOOLS.GZIP')
return
-
+
if debug:
cherrypy.log('Gzipping', context='TOOLS.GZIP')
# Return a generator that compresses the page
@@ -379,9 +379,9 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
if "Content-Length" in response.headers:
# Delete Content-Length header so finalize() recalcs it.
del response.headers["Content-Length"]
-
+
return
-
+
if debug:
cherrypy.log('No acceptable encoding found.', context='GZIP')
cherrypy.HTTPError(406, "identity, gzip").set_response()