Commit b3af08f8 authored by Senthil Kumaran's avatar Senthil Kumaran

Fix for issue5040. Adding support for unicode message passing and tests for...

Fix for issue5040. Adding support for unicode message passing and tests for unicode message and test for Content-Length.
parent df8709d7
...@@ -9,6 +9,7 @@ import threading ...@@ -9,6 +9,7 @@ import threading
import http.client import http.client
import socket import socket
import os import os
import re
from test import support from test import support
alist = [{'astring': 'foo@bar.baz.spam', alist = [{'astring': 'foo@bar.baz.spam',
...@@ -352,6 +353,19 @@ class SimpleServerTestCase(unittest.TestCase): ...@@ -352,6 +353,19 @@ class SimpleServerTestCase(unittest.TestCase):
# protocol error; provide additional information in test output # protocol error; provide additional information in test output
self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
def test_nonascii(self):
start_string = 'P\N{LATIN SMALL LETTER Y WITH CIRCUMFLEX}t'
end_string = 'h\N{LATIN SMALL LETTER O WITH HORN}n'
try:
p = xmlrpclib.ServerProxy(URL)
self.assertEqual(p.add(start_string, end_string),
start_string + end_string)
except (xmlrpclib.ProtocolError, socket.error) as e:
# ignore failures due to non-blocking socket 'unavailable' errors
if not is_unavailable_exception(e):
# protocol error; provide additional information in test output
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
# [ch] The test 404 is causing lots of false alarms. # [ch] The test 404 is causing lots of false alarms.
def XXXtest_404(self): def XXXtest_404(self):
# send POST with http.client, it should return 404 header and # send POST with http.client, it should return 404 header and
...@@ -616,9 +630,21 @@ class CGIHandlerTestCase(unittest.TestCase): ...@@ -616,9 +630,21 @@ class CGIHandlerTestCase(unittest.TestCase):
# need only xml # need only xml
self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, handle[44:]) self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, handle[44:])
# Also test the content-length returned by handle_request
# Using the same test method inorder to avoid all the datapassing
# boilerplate code.
# Test for bug: http://bugs.python.org/issue5040
content = handle[handle.find("<?xml"):]
self.assertEquals(
int(re.search('Content-Length: (\d+)', handle).group(1)),
len(content))
os.remove("xmldata.txt") os.remove("xmldata.txt")
os.remove(support.TESTFN) os.remove(support.TESTFN)
def test_main(): def test_main():
xmlrpc_tests = [XMLRPCTestCase, HelperTestCase, DateTimeTestCase, xmlrpc_tests = [XMLRPCTestCase, HelperTestCase, DateTimeTestCase,
BinaryTestCase, FaultTestCase] BinaryTestCase, FaultTestCase]
......
...@@ -1316,7 +1316,7 @@ class ServerProxy: ...@@ -1316,7 +1316,7 @@ class ServerProxy:
transport = Transport(use_datetime=use_datetime) transport = Transport(use_datetime=use_datetime)
self.__transport = transport self.__transport = transport
self.__encoding = encoding self.__encoding = encoding or 'utf-8'
self.__verbose = verbose self.__verbose = verbose
self.__allow_none = allow_none self.__allow_none = allow_none
...@@ -1324,7 +1324,7 @@ class ServerProxy: ...@@ -1324,7 +1324,7 @@ class ServerProxy:
# call a method on the remote server # call a method on the remote server
request = dumps(params, methodname, encoding=self.__encoding, request = dumps(params, methodname, encoding=self.__encoding,
allow_none=self.__allow_none) allow_none=self.__allow_none).encode(self.__encoding)
response = self.__transport.request( response = self.__transport.request(
self.__host, self.__host,
......
...@@ -163,7 +163,7 @@ class SimpleXMLRPCDispatcher: ...@@ -163,7 +163,7 @@ class SimpleXMLRPCDispatcher:
self.funcs = {} self.funcs = {}
self.instance = None self.instance = None
self.allow_none = allow_none self.allow_none = allow_none
self.encoding = encoding self.encoding = encoding or 'utf-8'
def register_instance(self, instance, allow_dotted_names=False): def register_instance(self, instance, allow_dotted_names=False):
"""Registers an instance to respond to XML-RPC requests. """Registers an instance to respond to XML-RPC requests.
...@@ -266,7 +266,7 @@ class SimpleXMLRPCDispatcher: ...@@ -266,7 +266,7 @@ class SimpleXMLRPCDispatcher:
encoding=self.encoding, allow_none=self.allow_none, encoding=self.encoding, allow_none=self.allow_none,
) )
return response return response.encode(self.encoding)
def system_listMethods(self): def system_listMethods(self):
"""system.listMethods() => ['add', 'subtract', 'multiple'] """system.listMethods() => ['add', 'subtract', 'multiple']
...@@ -473,8 +473,6 @@ class SimpleXMLRPCRequestHandler(BaseHTTPRequestHandler): ...@@ -473,8 +473,6 @@ class SimpleXMLRPCRequestHandler(BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
else: else:
# Got a valid XML RPC response; convert to bytes first
response = response.encode("utf-8")
self.send_response(200) self.send_response(200)
self.send_header("Content-type", "text/xml") self.send_header("Content-type", "text/xml")
self.send_header("Content-length", str(len(response))) self.send_header("Content-length", str(len(response)))
...@@ -551,7 +549,9 @@ class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher): ...@@ -551,7 +549,9 @@ class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher):
print('Content-Type: text/xml') print('Content-Type: text/xml')
print('Content-Length: %d' % len(response)) print('Content-Length: %d' % len(response))
print() print()
sys.stdout.write(response) sys.stdout.flush()
sys.stdout.buffer.write(response)
sys.stdout.buffer.flush()
def handle_get(self): def handle_get(self):
"""Handle a single HTTP GET request. """Handle a single HTTP GET request.
...@@ -569,11 +569,14 @@ class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher): ...@@ -569,11 +569,14 @@ class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher):
'message' : message, 'message' : message,
'explain' : explain 'explain' : explain
} }
response = response.encode('utf-8')
print('Status: %d %s' % (code, message)) print('Status: %d %s' % (code, message))
print('Content-Type: text/html') print('Content-Type: %s' % http.server.DEFAULT_ERROR_CONTENT_TYPE)
print('Content-Length: %d' % len(response)) print('Content-Length: %d' % len(response))
print() print()
sys.stdout.write(response) sys.stdout.flush()
sys.stdout.buffer.write(response)
sys.stdout.buffer.flush()
def handle_request(self, request_text = None): def handle_request(self, request_text = None):
"""Handle a single XML-RPC request passed through a CGI post method. """Handle a single XML-RPC request passed through a CGI post method.
...@@ -814,12 +817,12 @@ class DocXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): ...@@ -814,12 +817,12 @@ class DocXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
self.report_404() self.report_404()
return return
response = self.server.generate_html_documentation() response = self.server.generate_html_documentation().encode('utf-8')
self.send_response(200) self.send_response(200)
self.send_header("Content-type", "text/html") self.send_header("Content-type", "text/html")
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.encode()) self.wfile.write(response)
# shut down the connection # shut down the connection
self.wfile.flush() self.wfile.flush()
...@@ -852,12 +855,14 @@ class DocCGIXMLRPCRequestHandler( CGIXMLRPCRequestHandler, ...@@ -852,12 +855,14 @@ class DocCGIXMLRPCRequestHandler( CGIXMLRPCRequestHandler,
documentation. documentation.
""" """
response = self.generate_html_documentation() response = self.generate_html_documentation().encode('utf-8')
print('Content-Type: text/html') print('Content-Type: text/html')
print('Content-Length: %d' % len(response)) print('Content-Length: %d' % len(response))
print() print()
sys.stdout.write(response) sys.stdout.flush()
sys.stdout.buffer.write(response)
sys.stdout.buffer.flush()
def __init__(self): def __init__(self):
CGIXMLRPCRequestHandler.__init__(self) CGIXMLRPCRequestHandler.__init__(self)
......
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