Skip to content

Commit

Permalink
Merge pull request #22 from hallvors/gzip-pipe
Browse files Browse the repository at this point in the history
Adding gzip pipe feature
  • Loading branch information
jgraham committed May 8, 2014
2 parents 601cf5e + 490cbfc commit 6d87014
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions wptserve/pipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import re
import time
import types
import gzip as gzip_module
from cStringIO import StringIO


logger = logging.getLogger("wptserve")
Expand Down Expand Up @@ -369,3 +371,23 @@ def config_replacement(match):

response.content = new_content
return response

@pipe()
def gzip(request, response):
"""This pipe gzip-encodes response data.
It sets (or overwrites) these HTTP headers:
Content-Encoding is set to gzip
Content-Length is set to the length of the compressed content
"""
content = resolve_content(response)
response.headers.set("Content-Encoding", "gzip")

out = StringIO.StringIO()
with gzip_module.GzipFile(fileobj=out, mode="w") as f:
f.write(content)
response.content = out.getvalue()

response.headers.set("Content-Length", len(response.content))

return response

0 comments on commit 6d87014

Please sign in to comment.