porting revision 74098 from trunk:

http://bugs.python.org/issue6499
zlib/gzip may not be present for all builds.  Make xmlrpclib gracefully not supporg gzip encoding in this case
parent 22101aa0
...@@ -799,7 +799,11 @@ def test_main(): ...@@ -799,7 +799,11 @@ def test_main():
xmlrpc_tests.append(SimpleServerTestCase) xmlrpc_tests.append(SimpleServerTestCase)
xmlrpc_tests.append(KeepaliveServerTestCase1) xmlrpc_tests.append(KeepaliveServerTestCase1)
xmlrpc_tests.append(KeepaliveServerTestCase2) xmlrpc_tests.append(KeepaliveServerTestCase2)
xmlrpc_tests.append(GzipServerTestCase) try:
import gzip
xmlrpc_tests.append(GzipServerTestCase)
except ImportError:
pass #gzip not supported in this build
xmlrpc_tests.append(ServerProxyTestCase) xmlrpc_tests.append(ServerProxyTestCase)
xmlrpc_tests.append(FailingServerTestCase) xmlrpc_tests.append(FailingServerTestCase)
xmlrpc_tests.append(CGIHandlerTestCase) xmlrpc_tests.append(CGIHandlerTestCase)
......
...@@ -136,10 +136,13 @@ Exported functions: ...@@ -136,10 +136,13 @@ Exported functions:
import re, time, operator import re, time, operator
import http.client import http.client
from xml.parsers import expat from xml.parsers import expat
import gzip
import socket import socket
import errno import errno
from io import BytesIO from io import BytesIO
try:
import gzip
except ImportError:
gzip = None #python can be built without zlib/gzip support
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# Internal stuff # Internal stuff
...@@ -1030,6 +1033,8 @@ def gzip_encode(data): ...@@ -1030,6 +1033,8 @@ def gzip_encode(data):
Encode data using the gzip content encoding as described in RFC 1952 Encode data using the gzip content encoding as described in RFC 1952
""" """
if not gzip:
raise NotImplementedError
f = BytesIO() f = BytesIO()
gzf = gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1) gzf = gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1)
gzf.write(data) gzf.write(data)
...@@ -1052,6 +1057,8 @@ def gzip_decode(data): ...@@ -1052,6 +1057,8 @@ def gzip_decode(data):
Decode data using the gzip content encoding as described in RFC 1952 Decode data using the gzip content encoding as described in RFC 1952
""" """
if not gzip:
raise NotImplementedError
f = BytesIO(data) f = BytesIO(data)
gzf = gzip.GzipFile(mode="rb", fileobj=f) gzf = gzip.GzipFile(mode="rb", fileobj=f)
try: try:
...@@ -1076,6 +1083,8 @@ class GzipDecodedResponse(gzip.GzipFile): ...@@ -1076,6 +1083,8 @@ class GzipDecodedResponse(gzip.GzipFile):
def __init__(self, response): def __init__(self, response):
#response doesn't support tell() and read(), required by #response doesn't support tell() and read(), required by
#GzipFile #GzipFile
if not gzip:
raise NotImplementedError
self.io = BytesIO(response.read()) self.io = BytesIO(response.read())
gzip.GzipFile.__init__(self, mode="rb", fileobj=self.io) gzip.GzipFile.__init__(self, mode="rb", fileobj=self.io)
...@@ -1253,7 +1262,7 @@ class Transport: ...@@ -1253,7 +1262,7 @@ class Transport:
headers = self._extra_headers[:] headers = self._extra_headers[:]
if debug: if debug:
connection.set_debuglevel(1) connection.set_debuglevel(1)
if self.accept_gzip_encoding: if self.accept_gzip_encoding and gzip:
connection.putrequest("POST", handler, skip_accept_encoding=True) connection.putrequest("POST", handler, skip_accept_encoding=True)
headers.append(("Accept-Encoding", "gzip")) headers.append(("Accept-Encoding", "gzip"))
else: else:
...@@ -1285,7 +1294,8 @@ class Transport: ...@@ -1285,7 +1294,8 @@ class Transport:
def send_content(self, connection, request_body): def send_content(self, connection, request_body):
#optionally encode the request #optionally encode the request
if (self.encode_threshold is not None and if (self.encode_threshold is not None and
self.encode_threshold < len(request_body)): self.encode_threshold < len(request_body) and
gzip):
connection.putheader("Content-Encoding", "gzip") connection.putheader("Content-Encoding", "gzip")
request_body = gzip_encode(request_body) request_body = gzip_encode(request_body)
......
...@@ -509,8 +509,11 @@ class SimpleXMLRPCRequestHandler(BaseHTTPRequestHandler): ...@@ -509,8 +509,11 @@ class SimpleXMLRPCRequestHandler(BaseHTTPRequestHandler):
if len(response) > self.encode_threshold: if len(response) > self.encode_threshold:
q = self.accept_encodings().get("gzip", 0) q = self.accept_encodings().get("gzip", 0)
if q: if q:
response = gzip_encode(response) try:
self.send_header("Content-Encoding", "gzip") response = gzip_encode(response)
self.send_header("Content-Encoding", "gzip")
except NotImplementedError:
pass
self.send_header("Content-length", str(len(response))) self.send_header("Content-length", str(len(response)))
self.end_headers() self.end_headers()
self.wfile.write(response) self.wfile.write(response)
...@@ -523,6 +526,8 @@ class SimpleXMLRPCRequestHandler(BaseHTTPRequestHandler): ...@@ -523,6 +526,8 @@ class SimpleXMLRPCRequestHandler(BaseHTTPRequestHandler):
if encoding == "gzip": if encoding == "gzip":
try: try:
return gzip_decode(data) return gzip_decode(data)
except NotImplementedError:
self.send_response(501, "encoding %r not supported" % encoding)
except ValueError: except ValueError:
self.send_response(400, "error decoding gzip content") self.send_response(400, "error decoding gzip content")
else: else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment