Skip to content

Commit

Permalink
- Improve detection of HTTPS requests (fixes #680)
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed Feb 20, 2020
1 parent 3a0a423 commit b087137
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
5.0a1 (unreleased)
------------------

- Improve detection of HTTPS requests
(`#680 <https://github.com/zopefoundation/Zope/issues/680>`_)

- Fix several ZMI links so they respect virtual hosting
(`#788 <https://github.com/zopefoundation/Zope/issues/788>`_)

Expand Down
4 changes: 4 additions & 0 deletions src/ZPublisher/HTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ def __init__(self, stdin, environ, response, clean=0):
protocol = 'https'
elif environ.get('SERVER_PORT_SECURE', None) == 1:
protocol = 'https'
elif (environ.get('REQUEST_SCHEME', '') or '').lower() == 'https':
protocol = 'https'
elif environ.get('wsgi.url_scheme') == 'https':
protocol = 'https'
else:
protocol = 'http'

Expand Down
35 changes: 34 additions & 1 deletion src/ZPublisher/tests/testHTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _makeOne(self, stdin=None, environ=None, response=None, clean=1):
environ['REQUEST_METHOD'] = 'GET'

if 'SERVER_NAME' not in environ:
environ['SERVER_NAME'] = 'http://localhost'
environ['SERVER_NAME'] = 'localhost'

if 'SERVER_PORT' not in environ:
environ['SERVER_PORT'] = '8080'
Expand Down Expand Up @@ -1231,6 +1231,39 @@ def _xmlrpc_control(self, allow):
yield
gsm.unregisterUtility(allow, IXmlrpcChecker)

def test_url_scheme(self):
# The default is http
env = {'SERVER_NAME': 'myhost', 'SERVER_PORT': 80}
req = self._makeOne(environ=env)
self.assertEqual(req['SERVER_URL'], 'http://myhost')

# If we bang a SERVER_URL into the environment it is retained
env = {'SERVER_URL': 'https://anotherserver:8443'}
req = self._makeOne(environ=env)
self.assertEqual(req['SERVER_URL'], 'https://anotherserver:8443')

# Now go through the various environment values that signal
# a request uses the https URL scheme
for val in ('on', 'ON', '1'):
env = {'SERVER_NAME': 'myhost', 'SERVER_PORT': 443, 'HTTPS': val}
req = self._makeOne(environ=env)
self.assertEqual(req['SERVER_URL'], 'https://myhost')

env = {'SERVER_NAME': 'myhost', 'SERVER_PORT': 443,
'SERVER_PORT_SECURE': 1}
req = self._makeOne(environ=env)
self.assertEqual(req['SERVER_URL'], 'https://myhost')

env = {'SERVER_NAME': 'myhost', 'SERVER_PORT': 443,
'REQUEST_SCHEME': 'HTTPS'}
req = self._makeOne(environ=env)
self.assertEqual(req['SERVER_URL'], 'https://myhost')

env = {'SERVER_NAME': 'myhost', 'SERVER_PORT': 443,
'wsgi.url_scheme': 'https'}
req = self._makeOne(environ=env)
self.assertEqual(req['SERVER_URL'], 'https://myhost')


class TestHTTPRequestZope3Views(TestRequestViewsBase):

Expand Down

0 comments on commit b087137

Please sign in to comment.