1 import struct
2 import time
3
4 import cherrypy
5 from cherrypy._cpcompat import basestring, BytesIO, ntob, set, unicodestr
6 from cherrypy.lib import file_generator
7 from cherrypy.lib import set_vary_header
8
9
10 -def decode(encoding=None, default_encoding='utf-8'):
35
36
38
39 default_encoding = 'utf-8'
40 failmsg = "Response body could not be encoded with %r."
41 encoding = None
42 errors = 'strict'
43 text_only = True
44 add_charset = True
45 debug = False
46
59
61 """Encode a streaming response body.
62
63 Use a generator wrapper, and just pray it works as the stream is
64 being written out.
65 """
66 if encoding in self.attempted_charsets:
67 return False
68 self.attempted_charsets.add(encoding)
69
70 def encoder(body):
71 for chunk in body:
72 if isinstance(chunk, unicodestr):
73 chunk = chunk.encode(encoding, self.errors)
74 yield chunk
75 self.body = encoder(self.body)
76 return True
77
79 """Encode a buffered response body."""
80 if encoding in self.attempted_charsets:
81 return False
82 self.attempted_charsets.add(encoding)
83
84 try:
85 body = []
86 for chunk in self.body:
87 if isinstance(chunk, unicodestr):
88 chunk = chunk.encode(encoding, self.errors)
89 body.append(chunk)
90 self.body = body
91 except (LookupError, UnicodeError):
92 return False
93 else:
94 return True
95
97 request = cherrypy.serving.request
98 response = cherrypy.serving.response
99
100 if self.debug:
101 cherrypy.log('response.stream %r' % response.stream, 'TOOLS.ENCODE')
102 if response.stream:
103 encoder = self.encode_stream
104 else:
105 encoder = self.encode_string
106 if "Content-Length" in response.headers:
107
108
109
110
111
112
113
114
115
116
117 del response.headers["Content-Length"]
118
119
120
121 encs = request.headers.elements('Accept-Charset')
122 charsets = [enc.value.lower() for enc in encs]
123 if self.debug:
124 cherrypy.log('charsets %s' % repr(charsets), 'TOOLS.ENCODE')
125
126 if self.encoding is not None:
127
128 encoding = self.encoding.lower()
129 if self.debug:
130 cherrypy.log('Specified encoding %r' % encoding, 'TOOLS.ENCODE')
131 if (not charsets) or "*" in charsets or encoding in charsets:
132 if self.debug:
133 cherrypy.log('Attempting encoding %r' % encoding, 'TOOLS.ENCODE')
134 if encoder(encoding):
135 return encoding
136 else:
137 if not encs:
138 if self.debug:
139 cherrypy.log('Attempting default encoding %r' %
140 self.default_encoding, 'TOOLS.ENCODE')
141
142 if encoder(self.default_encoding):
143 return self.default_encoding
144 else:
145 raise cherrypy.HTTPError(500, self.failmsg % self.default_encoding)
146 else:
147 for element in encs:
148 if element.qvalue > 0:
149 if element.value == "*":
150
151 if self.debug:
152 cherrypy.log('Attempting default encoding due '
153 'to %r' % element, 'TOOLS.ENCODE')
154 if encoder(self.default_encoding):
155 return self.default_encoding
156 else:
157 encoding = element.value
158 if self.debug:
159 cherrypy.log('Attempting encoding %s (qvalue >'
160 '0)' % element, 'TOOLS.ENCODE')
161 if encoder(encoding):
162 return encoding
163
164 if "*" not in charsets:
165
166
167
168
169 iso = 'iso-8859-1'
170 if iso not in charsets:
171 if self.debug:
172 cherrypy.log('Attempting ISO-8859-1 encoding',
173 'TOOLS.ENCODE')
174 if encoder(iso):
175 return iso
176
177
178 ac = request.headers.get('Accept-Charset')
179 if ac is None:
180 msg = "Your client did not send an Accept-Charset header."
181 else:
182 msg = "Your client sent this Accept-Charset header: %s." % ac
183 msg += " We tried these charsets: %s." % ", ".join(self.attempted_charsets)
184 raise cherrypy.HTTPError(406, msg)
185
236
237
238
240 """Compress 'body' at the given compress_level."""
241 import zlib
242
243
244 yield ntob('\x1f\x8b')
245 yield ntob('\x08')
246 yield ntob('\x00')
247
248 yield struct.pack("<L", int(time.time()) & int('FFFFFFFF', 16))
249 yield ntob('\x02')
250 yield ntob('\xff')
251
252 crc = zlib.crc32(ntob(""))
253 size = 0
254 zobj = zlib.compressobj(compress_level,
255 zlib.DEFLATED, -zlib.MAX_WBITS,
256 zlib.DEF_MEM_LEVEL, 0)
257 for line in body:
258 size += len(line)
259 crc = zlib.crc32(line, crc)
260 yield zobj.compress(line)
261 yield zobj.flush()
262
263
264 yield struct.pack("<L", crc & int('FFFFFFFF', 16))
265
266 yield struct.pack("<L", size & int('FFFFFFFF', 16))
267
269 import gzip
270
271 zbuf = BytesIO()
272 zbuf.write(body)
273 zbuf.seek(0)
274 zfile = gzip.GzipFile(mode='rb', fileobj=zbuf)
275 data = zfile.read()
276 zfile.close()
277 return data
278
279
280 -def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False):
281 """Try to gzip the response body if Content-Type in mime_types.
282
283 cherrypy.response.headers['Content-Type'] must be set to one of the
284 values in the mime_types arg before calling this function.
285
286 The provided list of mime-types must be of one of the following form:
287 * type/subtype
288 * type/*
289 * type/*+subtype
290
291 No compression is performed if any of the following hold:
292 * The client sends no Accept-Encoding request header
293 * No 'gzip' or 'x-gzip' is present in the Accept-Encoding header
294 * No 'gzip' or 'x-gzip' with a qvalue > 0 is present
295 * The 'identity' value is given with a qvalue > 0.
296
297 """
298 request = cherrypy.serving.request
299 response = cherrypy.serving.response
300
301 set_vary_header(response, "Accept-Encoding")
302
303 if not response.body:
304
305 if debug:
306 cherrypy.log('No response body', context='TOOLS.GZIP')
307 return
308
309
310
311 if getattr(request, "cached", False):
312 if debug:
313 cherrypy.log('Not gzipping cached response', context='TOOLS.GZIP')
314 return
315
316 acceptable = request.headers.elements('Accept-Encoding')
317 if not acceptable:
318
319
320
321
322
323
324
325 if debug:
326 cherrypy.log('No Accept-Encoding', context='TOOLS.GZIP')
327 return
328
329 ct = response.headers.get('Content-Type', '').split(';')[0]
330 for coding in acceptable:
331 if coding.value == 'identity' and coding.qvalue != 0:
332 if debug:
333 cherrypy.log('Non-zero identity qvalue: %s' % coding,
334 context='TOOLS.GZIP')
335 return
336 if coding.value in ('gzip', 'x-gzip'):
337 if coding.qvalue == 0:
338 if debug:
339 cherrypy.log('Zero gzip qvalue: %s' % coding,
340 context='TOOLS.GZIP')
341 return
342
343 if ct not in mime_types:
344
345
346
347
348
349
350
351 found = False
352 if '/' in ct:
353 ct_media_type, ct_sub_type = ct.split('/')
354 for mime_type in mime_types:
355 if '/' in mime_type:
356 media_type, sub_type = mime_type.split('/')
357 if ct_media_type == media_type:
358 if sub_type == '*':
359 found = True
360 break
361 elif '+' in sub_type and '+' in ct_sub_type:
362 ct_left, ct_right = ct_sub_type.split('+')
363 left, right = sub_type.split('+')
364 if left == '*' and ct_right == right:
365 found = True
366 break
367
368 if not found:
369 if debug:
370 cherrypy.log('Content-Type %s not in mime_types %r' %
371 (ct, mime_types), context='TOOLS.GZIP')
372 return
373
374 if debug:
375 cherrypy.log('Gzipping', context='TOOLS.GZIP')
376
377 response.headers['Content-Encoding'] = 'gzip'
378 response.body = compress(response.body, compress_level)
379 if "Content-Length" in response.headers:
380
381 del response.headers["Content-Length"]
382
383 return
384
385 if debug:
386 cherrypy.log('No acceptable encoding found.', context='GZIP')
387 cherrypy.HTTPError(406, "identity, gzip").set_response()
388