From 196a987c49c2298ba390ec1258689f9ba21e0536 Mon Sep 17 00:00:00 2001 From: Riccardo (C10uD) Date: Thu, 15 Apr 2010 23:19:56 +0200 Subject: [PATCH] allow receiving gzip-encoded soap responses this allows saving bandwidth (and faster responses as a bonus) --- papyon/gnet/message/HTTP.py | 5 ++++- papyon/service/SOAPService.py | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/papyon/gnet/message/HTTP.py b/papyon/gnet/message/HTTP.py index dbddd38..56fe58c 100644 --- a/papyon/gnet/message/HTTP.py +++ b/papyon/gnet/message/HTTP.py @@ -77,7 +77,10 @@ class HTTPMessage(object): #if "Content-Length" not in self.headers: # result.append("Content-Length: %d" % len(body)) result.append("") - result.append(str(self.body)) + if "Content-Encoding" in self.headers: + result.append("<" + self.headers.get('Content-Encoding', '') + " encoded data>") + else: + result.append(str(self.body)) return "\r\n".join(result) def __unicode__(self): diff --git a/papyon/service/SOAPService.py b/papyon/service/SOAPService.py index b0332b4..d3a032b 100644 --- a/papyon/service/SOAPService.py +++ b/papyon/service/SOAPService.py @@ -26,6 +26,7 @@ from SOAPUtils import * import papyon.gnet.protocol import papyon.util.element_tree as ElementTree import papyon.util.string_io as StringIO +import gzip import re import logging @@ -175,6 +176,7 @@ class SOAPService(object): http_headers["Accept"] = "text/*" http_headers["Proxy-Connection"] = "Keep-Alive" http_headers["Connection"] = "Keep-Alive" + http_headers["Accept-Encoding"] = "gzip" request = compress_xml(soap_template % (soap_header, soap_body)) @@ -197,7 +199,20 @@ class SOAPService(object): def _response_handler(self, transport, http_response): logger.debug("<<< " + unicode(http_response)) - soap_response = SOAPResponse(http_response.body) + + soap_response = "" + if "Content-Encoding" in http_response.headers: + if http_response.headers["Content-Encoding"] == "gzip": + body_stream = StringIO.StringIO(http_response.body) + unzipper = gzip.GzipFile(fileobj=body_stream) + unzipped_data = unzipper.read() + soap_response = SOAPResponse(unzipped_data) + elif http_response.headers["Content-Encoding"] == "": + soap_response = SOAPResponse(http_response.body) + else: + raise Exception("Invalid or unknown encoding") + else: + soap_response = SOAPResponse(http_response.body) request_id, callback, errback, user_data = self._unref_transport(transport) if not soap_response.is_valid(): -- 1.7.0.4