def finish_response(self, app_iter): if self.compressible: output = gzip.GzipFile( mode='wb', compresslevel=self.compress_level, fileobj=self.buffer, ) else: output = self.buffer try: for s in app_iter: output.write(s) if self.compressible: output.close() finally: if hasattr(app_iter, 'close'): try: app_iter.close() except Exception: pass content_length = self.buffer.tell() update_header(self.headers, smart_str('Content-Length'), smart_str(content_length)) self.start_response(self.status, self.headers)
def server_static(path): response.headers.update({ smart_str('Expires'): smart_str( time.strftime( "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time() + 60 * 60 * 24 * 7), )), smart_str('Cache-control'): smart_str('public'), }) return static_file(path, root=join(PROJECT_DIR, "media"))
def test_smart_str(self): value_to_expected_tuples = ( (b'testing', b'testing', u'testing'), (u'testing', b'testing', u'testing'), (b'A\xc3\xb1o', b'A\xc3\xb1o', u'Año'), (u'Año', b'A\xc3\xb1o', u'Año'), (123, b'123', u'123'), (None, b'None', u'None'), ) expected_type = six.text_type if six.PY3 else six.binary_type for value, expected_py2, expected_py3 in value_to_expected_tuples: result = smart_str(value) self.assertEqual(result, expected_py3 if six.PY3 else expected_py2) self.assertIsInstance(result, expected_type)
def gzip_start_response(self, status, headers, exc_info=None): self.headers = headers ct = header_value(headers, smart_str('content-type')) ce = header_value(headers, smart_str('content-encoding')) cl = header_value(headers, smart_str('content-length')) cl = int(cl) if cl else 201 self.compressible = False if (ct and any( ct.startswith(smart_str(pattern)) for pattern in ('text/', 'application/')) and smart_str('zip') not in ct and cl > 200): self.compressible = True if ce: self.compressible = False if self.compressible: headers.append((smart_str('content-encoding'), smart_str('gzip'))) remove_header(headers, smart_str('content-length')) self.headers = headers self.status = status return self.buffer.write
def js_dynamic(path): response.headers.update({ smart_str('Expires'): smart_str( time.strftime( "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time() + 60 * 60 * 24 * 2), )), smart_str('Cache-control'): smart_str('public'), smart_str('Content-Type'): smart_str('text/javascript; charset=UTF-8'), }) try: # static files are not rendered if "static" not in path and "mootools" not in path: t = env.get_template("js/%s" % path) return t.render() else: return static_file(path, root=join(PROJECT_DIR, "media", "js")) except Exception: return HTTPError(404, "Not Found")