https option for simple server

David Binger <[email protected]> Wed, 30 Mar 2005 13:07:36 -0500
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
--Apple-Mail-3-754870090
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	x-unix-mode=0664;
	name="https.txt"
Content-Disposition: attachment;
	filename=https.txt

Add --HTTPS option to simple server, so that redirects can work when running
through an SSL proxy or tunnel.

Changed:
server/util.py
server/simple_server.py
Add --HTTPS option to simple server, so that redirects can work when running
through an SSL proxy or tunnel.

Changed:
server/util.py
server/simple_server.py

Here is the diff:
Index: server/util.py
===================================================================
--- server/util.py	(revision 26410)
+++ server/util.py	(working copy)
@@ -7,9 +7,9 @@
 from optparse import OptionParser
 from quixote.util import import_object
 
-def main(run):
+def get_server_parser(doc):
     parser = OptionParser()
-    parser.set_description(run.__doc__)
+    parser.set_description(doc)
     default_host = 'localhost'
     parser.add_option(
         '--host', dest="host", default=default_host, type="string",
@@ -24,5 +24,9 @@
         default=default_factory,
         help="Path to factory function to create the site Publisher. "
              "(default=%s)" % default_factory)
+    return parser
+
+def main(run):
+    parser = get_server_parser(run.__doc__)
     (options, args) = parser.parse_args()
     run(import_object(options.factory), host=options.host, port=options.port)
Index: server/simple_server.py
===================================================================
--- server/simple_server.py	(revision 26410)
+++ server/simple_server.py	(working copy)
@@ -9,9 +9,12 @@
 import quixote
 from quixote import get_publisher
 from quixote.http_request import HTTPRequest
+from quixote.util import import_object
 
 class HTTPRequestHandler(BaseHTTPRequestHandler):
 
+    required_cgi_environment = {}
+
     def get_cgi_env(self, method):
         env = dict(
             SERVER_SOFTWARE="Quixote/%s" % quixote.__version__,
@@ -45,6 +48,7 @@
         co = filter(None, self.headers.getheaders('cookie'))
         if co:
             env['HTTP_COOKIE'] = ', '.join(co)
+        env.update(self.required_cgi_environment)
         return env
 
     def process(self, env):
@@ -64,15 +68,26 @@
         return self.process(self.get_cgi_env('GET'))
 
 
-def run(create_publisher, host='', port=80):
+def run(create_publisher, host='', port=80, HTTPS=False):
     """Runs a simple, single threaded, synchronous HTTP server that
     publishes a Quixote application.
     """
+    if HTTPS:
+        HTTPRequestHandler.required_cgi_environment['HTTPS'] = 'on'
     httpd = HTTPServer((host, port), HTTPRequestHandler)
     publisher = create_publisher()
     httpd.serve_forever()
 
 
 if __name__ == '__main__':
-    from quixote.server.util import main
-    main(run)
+    from quixote.server.util import get_server_parser
+    parser = get_server_parser(run.__doc__)
+    parser.add_option(
+        '--HTTPS', dest="https", default=False, action="store_true",
+        help=("Force the scheme for all requests to be https.  "
+              "Not that this is for running the simple server "
+              "through a proxy or tunnel that provides real SSL "
+              "support.  The simple server itself does not. "))
+    (options, args) = parser.parse_args()
+    run(import_object(options.factory), host=options.host, port=options.port,
+        HTTPS=options.https)
--Apple-Mail-3-754870090--