add gzip encoding to responses that are just text

This commit is contained in:
Damien Churchill 2010-01-22 12:39:33 +00:00
parent 2073ae0221
commit 76fcfa498d
4 changed files with 101 additions and 85 deletions

View File

@ -8,6 +8,10 @@
==== Blocklist ====
* Implement local blocklist support
==== Web ====
* Migrate to ExtJS 3.1
* Add gzip compression of HTTP data to the server
=== Deluge 1.2.0 - "Bursting like an infected kidney" (10 January 2010) ===
==== Core ====
* Implement new RPC protocol DelugeRPC replacing XMLRPC

View File

@ -33,6 +33,7 @@
#
#
import zlib
import gettext
from mako.template import Template as MakoTemplate
from deluge import common
@ -50,6 +51,14 @@ def escape(text):
text = text.replace('\n', '\\n')
return text
def compress(contents, request):
request.setHeader("content-encoding", "gzip")
compress = zlib.compressobj(6, zlib.DEFLATED, zlib.MAX_WBITS + 16,
zlib.DEF_MEM_LEVEL,0)
contents = compress.compress(contents)
contents += compress.flush()
return contents
class Template(MakoTemplate):
"""
A template that adds some built-ins to the rendering

View File

@ -51,7 +51,7 @@ from deluge.ui import common as uicommon
from deluge.ui.client import client, Client
from deluge.ui.coreconfig import CoreConfig
from deluge.ui.web.common import _
from deluge.ui.web.common import _, compress
json = common.json
log = logging.getLogger(__name__)
@ -257,7 +257,7 @@ class JSON(resource.Resource, component.Component):
def _send_response(self, request, response):
response = json.dumps(response)
request.setHeader("content-type", "application/x-json")
request.write(response)
request.write(compress(response, request))
request.finish()
def render(self, request):
@ -490,11 +490,14 @@ class WebApi(JSONComponent):
def got_torrents(torrents):
ui_info["torrents"] = torrents
for id in torrents:
torrent = torrents[id]
torrent["id"] = id
def on_complete(result):
d.callback(ui_info)
d1 = client.core.get_torrents_status(filter_dict, keys)
d1 = client.core.get_torrents_status({}, keys)
d1.addCallback(got_torrents)
d2 = client.core.get_filter_tree()

View File

@ -56,7 +56,7 @@ from deluge.log import setupLogger, LOG as _log
from deluge.ui import common as uicommon
from deluge.ui.tracker_icons import TrackerIcons
from deluge.ui.web.auth import Auth
from deluge.ui.web.common import Template
from deluge.ui.web.common import Template, compress
from deluge.ui.web.json_api import JSON, WebApi
from deluge.ui.web.pluginmanager import PluginManager
log = logging.getLogger(__name__)
@ -129,17 +129,17 @@ class Config(resource.Resource):
def render(self, request):
web_config = component.get("Web").get_config()
config = dict([(key, web_config[key]) for key in UI_CONFIG_KEYS])
return """Deluge = {
return compress("""Deluge = {
author: 'Damien Churchill <damoxc@gmail.com>',
version: '1.2-dev',
config: %s
}""" % common.json.dumps(config)
}""" % common.json.dumps(config), request)
class GetText(resource.Resource):
def render(self, request):
request.setHeader("content-type", "text/javascript; encoding=utf-8")
template = Template(filename=rpath("gettext.js"))
return template.render()
return compress(template.render(), request)
class Upload(resource.Resource):
"""
@ -196,7 +196,7 @@ class Render(resource.Resource):
template = Template(filename=rpath(filename))
request.setHeader("content-type", "text/html")
request.setResponseCode(http.OK)
return template.render()
return compress(template.render(), request)
class Tracker(resource.Resource):
tracker_icons = TrackerIcons()
@ -285,7 +285,7 @@ class LookupResource(resource.Resource, component.Component):
log.debug("Serving path: '%s'", path)
mime_type = mimetypes.guess_type(path)
request.setHeader("content-type", mime_type[0])
return open(path, "rb").read()
return compress(open(path, "rb").read(), request)
request.setResponseCode(http.NOT_FOUND)
return "<h1>404 - Not Found</h1>"