Skip to content

Commit

Permalink
Patch CherryPy to support 301 redirection.
Browse files Browse the repository at this point in the history
Needed to support the broken Bonjour/ZeroConfig protocol that
only allows an HTTP address to set, even for a HTTPS-only server.
  • Loading branch information
shypike committed Aug 12, 2015
1 parent 1889ecd commit da9e393
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions cherrypy/wsgiserver/wsgiserver2.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def my_crazy_app(environ, start_response):
'WorkerThread', 'ThreadPool', 'SSLAdapter',
'CherryPyWSGIServer',
'Gateway', 'WSGIGateway', 'WSGIGateway_10', 'WSGIGateway_u0',
'WSGIPathInfoDispatcher', 'get_ssl_adapter_class']
'WSGIPathInfoDispatcher', 'get_ssl_adapter_class', 'redirect_url']

import os
try:
Expand All @@ -97,6 +97,7 @@ def my_crazy_app(environ, start_response):
import StringIO
DEFAULT_BUFFER_SIZE = -1

REDIRECT_URL = None # Application can write its HTTP-->HTTPS redirection URL here

class FauxSocket(object):

Expand Down Expand Up @@ -167,6 +168,12 @@ def ntob(n, encoding='ISO-8859-1'):

import errno

def redirect_url(url=None):
global REDIRECT_URL
if url and '%s' in url:
REDIRECT_URL = url
return REDIRECT_URL


def plat_specific_errors(*errnames):
"""Return error numbers for all errors in errnames on this platform.
Expand Down Expand Up @@ -881,6 +888,9 @@ def simple_response(self, status, msg=""):
"Content-Length: %s\r\n" % len(msg),
"Content-Type: text/plain\r\n"]

if status[:3] in ("301",):
buf.append("Location: %s" % msg)

if status[:3] in ("413", "414"):
# Request Entity Too Large / Request-URI Too Long
self.close_connection = True
Expand Down Expand Up @@ -1394,10 +1404,13 @@ def communicate(self):
# Unwrap our wfile
self.wfile = CP_fileobject(
self.socket._sock, "wb", self.wbufsize)
req.simple_response(
"400 Bad Request",
"The client sent a plain HTTP request, but "
"this server only speaks HTTPS on this port.")
if REDIRECT_URL:
req.simple_response("301 Moved Permanently", REDIRECT_URL % self.remote_addr)
else:
req.simple_response(
"400 Bad Request",
"The client sent a plain HTTP request, but "
"this server only speaks HTTPS on this port.")
self.linger = True
except Exception:
e = sys.exc_info()[1]
Expand Down

0 comments on commit da9e393

Please sign in to comment.