Back to index

moin  1.9.0~rc2
_fallback_cgi.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Fallback CGI WSGI interface
00004 
00005     Provide a simple WSGI-to-CGI adapter in case flup is not available.
00006     It is taken from http://www.python.org/dev/peps/pep-0333/.
00007 
00008     @copyright: 2003-2006 Phillip J. Eby <pje at telecommunity.com>
00009     @license: Public Domain
00010 """
00011 import os, sys
00012 
00013 
00014 __all__ = ['WSGIServer']
00015 
00016 
00017 class WSGIServer(object):
00018 
00019     def __init__(self, application):
00020         self.application = application
00021 
00022     def run(self):
00023 
00024         environ = dict(os.environ.items())
00025         environ['wsgi.input']        = sys.stdin
00026         environ['wsgi.errors']       = sys.stderr
00027         environ['wsgi.version']      = (1, 0)
00028         environ['wsgi.multithread']  = False
00029         environ['wsgi.multiprocess'] = True
00030         environ['wsgi.run_once']     = True
00031 
00032         if environ.get('HTTPS', 'off') in ('on', '1'):
00033             environ['wsgi.url_scheme'] = 'https'
00034         else:
00035             environ['wsgi.url_scheme'] = 'http'
00036 
00037         headers_set = []
00038         headers_sent = []
00039 
00040         def write(data):
00041             if not headers_set:
00042                 raise AssertionError("write() before start_response()")
00043 
00044             elif not headers_sent:
00045                 # Before the first output, send the stored headers
00046                 status, response_headers = headers_sent[:] = headers_set
00047                 sys.stdout.write('Status: %s\r\n' % status)
00048                 for header in response_headers:
00049                     sys.stdout.write('%s: %s\r\n' % header)
00050                 sys.stdout.write('\r\n')
00051 
00052             sys.stdout.write(data)
00053             sys.stdout.flush()
00054 
00055         def start_response(status, response_headers, exc_info=None):
00056             if exc_info:
00057                 try:
00058                     if headers_sent:
00059                         # Re-raise original exception if headers sent
00060                         raise exc_info[0], exc_info[1], exc_info[2]
00061                 finally:
00062                     exc_info = None     # avoid dangling circular ref
00063             elif headers_set:
00064                 raise AssertionError("Headers already set!")
00065 
00066             headers_set[:] = [status, response_headers]
00067             return write
00068 
00069         result = self.application(environ, start_response)
00070         try:
00071             for data in result:
00072                 if data:    # don't send headers until body appears
00073                     write(data)
00074             if not headers_sent:
00075                 write('')   # send headers now if body was empty
00076         finally:
00077             if hasattr(result, 'close'):
00078                 result.close()