summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Brewer <fumanchu@aminus.org>2008-11-08 23:30:21 +0000
committerRobert Brewer <fumanchu@aminus.org>2008-11-08 23:30:21 +0000
commit4cba9acfc2af2ec487d36452060c651c4472a4e9 (patch)
treed7450ac7b684b0b94ec06d9ec5d976cd1b4954f6
parent7d39e1464b57ff28a8e87312079c38e5c71db2ab (diff)
downloadcherrypy-git-cherrypy-3.1.1.tar.gz
Merge from trunk for today's fixes [2072] to [2080].cherrypy-3.1.1
--HG-- branch : cherrypy-3.1.x
-rw-r--r--cherrypy/__init__.py4
-rw-r--r--cherrypy/lib/safemime.py2
-rw-r--r--cherrypy/test/test_conn.py6
-rw-r--r--cherrypy/test/test_http.py12
-rw-r--r--cherrypy/test/test_logging.py18
-rw-r--r--cherrypy/test/test_objectmapping.py2
-rw-r--r--cherrypy/test/test_refleaks.py2
-rwxr-xr-xcherrypy/test/test_session.py4
-rw-r--r--cherrypy/test/test_xmlrpc.py12
-rw-r--r--cherrypy/test/webtest.py14
-rw-r--r--cherrypy/wsgiserver/__init__.py21
11 files changed, 57 insertions, 40 deletions
diff --git a/cherrypy/__init__.py b/cherrypy/__init__.py
index 613a5a18..274f0069 100644
--- a/cherrypy/__init__.py
+++ b/cherrypy/__init__.py
@@ -337,6 +337,10 @@ class _ThreadLocalProxy(object):
def __len__(self):
child = getattr(serving, self.__attrname__)
return len(child)
+
+ def __nonzero__(self):
+ child = getattr(serving, self.__attrname__)
+ return bool(child)
# Create request and response object (the same objects will be used
diff --git a/cherrypy/lib/safemime.py b/cherrypy/lib/safemime.py
index cf41dbf2..0d13ae9a 100644
--- a/cherrypy/lib/safemime.py
+++ b/cherrypy/lib/safemime.py
@@ -109,7 +109,7 @@ class MultipartWrapper(object):
def safe_multipart(flash_only=False):
"""Wrap request.rfile in a reader that won't crash on no trailing CRLF."""
h = cherrypy.request.headers
- if not h.get('Content-Type').startswith('multipart/'):
+ if not h.get('Content-Type','').startswith('multipart/'):
return
if flash_only and not 'Shockwave Flash' in h.get('User-Agent', ''):
return
diff --git a/cherrypy/test/test_conn.py b/cherrypy/test/test_conn.py
index 8a9bff29..c1f902c5 100644
--- a/cherrypy/test/test_conn.py
+++ b/cherrypy/test/test_conn.py
@@ -171,6 +171,12 @@ class ConnectionTests(helper.CPWebCase):
# Make another request on the same connection, which should error.
self.assertRaises(httplib.NotConnected, self.getPage, "/")
+
+ # Try HEAD. See http://www.cherrypy.org/ticket/864.
+ self.getPage("/stream", method='HEAD')
+ self.assertStatus('200 OK')
+ self.assertBody('')
+ self.assertNoHeader("Transfer-Encoding")
else:
self.PROTOCOL = "HTTP/1.0"
diff --git a/cherrypy/test/test_http.py b/cherrypy/test/test_http.py
index 92162bcc..ae7886c7 100644
--- a/cherrypy/test/test_http.py
+++ b/cherrypy/test/test_http.py
@@ -70,9 +70,9 @@ class HTTPTests(helper.CPWebCase):
# will hang. Verify that CP times out the socket and responds
# with 411 Length Required.
if self.scheme == "https":
- c = httplib.HTTPSConnection("127.0.0.1:%s" % self.PORT)
+ c = httplib.HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
else:
- c = httplib.HTTPConnection("127.0.0.1:%s" % self.PORT)
+ c = httplib.HTTPConnection('%s:%s' % (self.interface(), self.PORT))
c.request("POST", "/")
self.assertEqual(c.getresponse().status, 411)
@@ -87,9 +87,9 @@ class HTTPTests(helper.CPWebCase):
# post file
if self.scheme == 'https':
- c = httplib.HTTPS('127.0.0.1:%s' % self.PORT)
+ c = httplib.HTTPS('%s:%s' % (self.interface(), self.PORT))
else:
- c = httplib.HTTP('127.0.0.1:%s' % self.PORT)
+ c = httplib.HTTP('%s:%s' % (self.interface(), self.PORT))
c.putrequest('POST', '/post_multipart')
c.putheader('Content-Type', content_type)
c.putheader('Content-Length', str(len(body)))
@@ -110,9 +110,9 @@ class HTTPTests(helper.CPWebCase):
# Test missing version in Request-Line
if self.scheme == 'https':
- c = httplib.HTTPSConnection('127.0.0.1:%s' % self.PORT)
+ c = httplib.HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
else:
- c = httplib.HTTPConnection('127.0.0.1:%s' % self.PORT)
+ c = httplib.HTTPConnection('%s:%s' % (self.interface(), self.PORT))
c._output('GET /')
c._send_output()
response = c.response_class(c.sock, strict=c.strict, method='GET')
diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py
index 58b7f5da..cb560e33 100644
--- a/cherrypy/test/test_logging.py
+++ b/cherrypy/test/test_logging.py
@@ -79,14 +79,7 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
self.assertBody('content')
self.assertStatus(200)
- host = self.HOST
- if host == '0.0.0.0':
- # INADDR_ANY, which should respond on localhost.
- host = "127.0.0.1"
- if host == '::':
- # IN6ADDR_ANY, which should respond on localhost.
- host = "::1"
- intro = '%s - - [' % host
+ intro = '%s - - [' % self.interface()
self.assertLog(-1, intro)
@@ -105,14 +98,7 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
self.assertBody('content')
self.assertStatus(200)
- host = self.HOST
- if host == '0.0.0.0':
- # INADDR_ANY, which should respond on localhost.
- host = "127.0.0.1"
- if host == '::':
- # IN6ADDR_ANY, which should respond on localhost.
- host = "::1"
- intro = '%s - - [' % host
+ intro = '%s - - [' % self.interface()
self.assertLog(-1, intro)
if [k for k, v in self.headers if k.lower() == 'content-length']:
diff --git a/cherrypy/test/test_objectmapping.py b/cherrypy/test/test_objectmapping.py
index 1c163c30..7883cd96 100644
--- a/cherrypy/test/test_objectmapping.py
+++ b/cherrypy/test/test_objectmapping.py
@@ -244,7 +244,7 @@ class ObjectMappingTest(helper.CPWebCase):
self.script_name = ""
# Test absoluteURI's in the Request-Line
- self.getPage('http://127.0.0.1/')
+ self.getPage('http://%s:%s/' % (self.interface(), self.PORT))
self.assertBody('world')
# Test that the "isolated" app doesn't leak url's into the root app.
diff --git a/cherrypy/test/test_refleaks.py b/cherrypy/test/test_refleaks.py
index c4941817..a71f9d25 100644
--- a/cherrypy/test/test_refleaks.py
+++ b/cherrypy/test/test_refleaks.py
@@ -88,7 +88,7 @@ class ReferenceTests(helper.CPWebCase):
success = []
def getpage():
- host = '%s:%s' % (self.HOST, self.PORT)
+ host = '%s:%s' % (self.interface(), self.PORT)
if self.scheme == 'https':
c = httplib.HTTPSConnection(host)
else:
diff --git a/cherrypy/test/test_session.py b/cherrypy/test/test_session.py
index 927d79f4..778725aa 100755
--- a/cherrypy/test/test_session.py
+++ b/cherrypy/test/test_session.py
@@ -193,9 +193,9 @@ class SessionTest(helper.CPWebCase):
def request(index):
if self.scheme == 'https':
- c = httplib.HTTPSConnection('127.0.0.1:%s' % self.PORT)
+ c = httplib.HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
else:
- c = httplib.HTTPConnection('127.0.0.1:%s' % self.PORT)
+ c = httplib.HTTPConnection('%s:%s' % (self.interface(), self.PORT))
for i in xrange(request_count):
c.putrequest('GET', '/')
for k, v in cookies:
diff --git a/cherrypy/test/test_xmlrpc.py b/cherrypy/test/test_xmlrpc.py
index 08ebfa3a..5b50d40c 100644
--- a/cherrypy/test/test_xmlrpc.py
+++ b/cherrypy/test/test_xmlrpc.py
@@ -116,19 +116,11 @@ class XmlRpcTest(helper.CPWebCase):
except AttributeError:
pass
- host = self.HOST
- if host == '0.0.0.0':
- # INADDR_ANY, which should respond on localhost.
- host = "127.0.0.1"
- if host == '::':
- # IN6ADDR_ANY, which should respond on localhost.
- host = "::1"
-
if scheme == "https":
- url = 'https://%s:%s/xmlrpc/' % (host, self.PORT)
+ url = 'https://%s:%s/xmlrpc/' % (self.interface(), self.PORT)
proxy = xmlrpclib.ServerProxy(url, transport=HTTPSTransport())
else:
- url = 'http://%s:%s/xmlrpc/' % (host, self.PORT)
+ url = 'http://%s:%s/xmlrpc/' % (self.interface(), self.PORT)
proxy = xmlrpclib.ServerProxy(url)
# begin the tests ...
diff --git a/cherrypy/test/webtest.py b/cherrypy/test/webtest.py
index cfcdcd56..ab622d5f 100644
--- a/cherrypy/test/webtest.py
+++ b/cherrypy/test/webtest.py
@@ -192,6 +192,20 @@ class WebCase(TestCase):
self.set_persistent(on)
persistent = property(_get_persistent, _set_persistent)
+ def interface(self):
+ """Return an IP address for a client connection.
+
+ If the server is listening on '0.0.0.0' (INADDR_ANY)
+ or '::' (IN6ADDR_ANY), this will return the proper localhost."""
+ host = self.HOST
+ if host == '0.0.0.0':
+ # INADDR_ANY, which should respond on localhost.
+ return "127.0.0.1"
+ if host == '::':
+ # IN6ADDR_ANY, which should respond on localhost.
+ return "::1"
+ return host
+
def getPage(self, url, headers=None, method="GET", body=None, protocol=None):
"""Open the url with debugging support. Return status, headers, body."""
ServerError.on = False
diff --git a/cherrypy/wsgiserver/__init__.py b/cherrypy/wsgiserver/__init__.py
index dc1be9ae..a92869f5 100644
--- a/cherrypy/wsgiserver/__init__.py
+++ b/cherrypy/wsgiserver/__init__.py
@@ -652,7 +652,8 @@ class HTTPRequest(object):
if status < 200 or status in (204, 205, 304):
pass
else:
- if self.response_protocol == 'HTTP/1.1':
+ if (self.response_protocol == 'HTTP/1.1'
+ and self.environ["REQUEST_METHOD"] != 'HEAD'):
# Use the chunked transfer-coding
self.chunked_write = True
self.outheaders.append(("Transfer-Encoding", "chunked"))
@@ -745,7 +746,8 @@ if not _fileobject_uses_str_type:
try:
return self._sock.recv(size)
except socket.error, e:
- if e.args[0] not in socket_errors_nonblocking:
+ if (e.args[0] not in socket_errors_nonblocking
+ and e.args[0] not in socket_error_eintr):
raise
def read(self, size=-1):
@@ -922,7 +924,8 @@ else:
try:
return self._sock.recv(size)
except socket.error, e:
- if e.args[0] not in socket_errors_nonblocking:
+ if (e.args[0] not in socket_errors_nonblocking
+ and e.args[0] not in socket_error_eintr):
raise
def read(self, size=-1):
@@ -1615,6 +1618,18 @@ class CherryPyWSGIServer(object):
ctx.use_certificate_file(self.ssl_certificate)
self.socket = SSLConnection(ctx, self.socket)
self.populate_ssl_environ()
+
+ # If listening on the IPV6 any address ('::' = IN6ADDR_ANY),
+ # activate dual-stack. See http://www.cherrypy.org/ticket/871.
+ if (not isinstance(self.bind_addr, basestring)
+ and self.bind_addr[0] == '::' and family == socket.AF_INET6):
+ try:
+ self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
+ except (AttributeError, socket.error):
+ # Apparently, the socket option is not available in
+ # this machine's TCP stack
+ pass
+
self.socket.bind(self.bind_addr)
def tick(self):