Back to index

moin  1.9.0~rc2
request.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - New slimmed down WSGI Request.
00004 
00005     @copyright: 2008-2008 MoinMoin:FlorianKrupicka
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 import re
00010 from StringIO import StringIO
00011 
00012 from werkzeug import Request as RequestBase
00013 from werkzeug import Response as ResponseBase
00014 from werkzeug import EnvironHeaders, Headers, HeaderSet
00015 from werkzeug import Href, create_environ, url_encode, cached_property
00016 from werkzeug import Client # used by tests
00017 
00018 from MoinMoin import config
00019 
00020 from MoinMoin import log
00021 logging = log.getLogger(__name__)
00022 
00023 class MoinMoinFinish(Exception):
00024     """ Raised to jump directly to end of run() function, where finish is called """
00025 
00026 class Request(ResponseBase, RequestBase):
00027     """ A full featured Request/Response object.
00028 
00029     To better distinguish incoming and outgoing data/headers,
00030     incoming versions are prefixed with 'in_' in contrast to
00031     original Werkzeug implementation.
00032     """
00033     charset = config.charset
00034     encoding_errors = 'replace'
00035     default_mimetype = 'text/html'
00036     given_config = None # if None, load wiki config from disk
00037 
00038     # get rid of some inherited descriptors
00039     headers = None
00040 
00041     def __init__(self, environ, populate_request=True, shallow=False):
00042         ResponseBase.__init__(self)
00043         RequestBase.__init__(self, environ, populate_request, shallow)
00044         self.href = Href(self.script_root or '/', self.charset)
00045         self.abs_href = Href(self.url_root, self.charset)
00046         self.headers = Headers([('Content-Type', 'text/html')])
00047         self.response = []
00048         self.status_code = 200
00049 
00050     in_stream = RequestBase.stream
00051 
00052     @cached_property
00053     def in_data(self):
00054         return self.in_stream.read()
00055 
00056     @cached_property
00057     def in_headers(self):
00058         return EnvironHeaders(self.environ)
00059 
00060 
00061 class TestRequest(Request):
00062     """ Request with customized `environ` for test purposes. """
00063     def __init__(self, path="/", query_string=None, method='GET',
00064                  content_type=None, content_length=0, form_data=None,
00065                  environ_overrides=None):
00066         """
00067         For parameter reference see the documentation of the werkzeug
00068         package, especially the functions `url_encode` and `create_environ`.
00069         """
00070         input_stream = None
00071 
00072         if form_data is not None:
00073             form_data = url_encode(form_data)
00074             content_type = 'application/x-www-form-urlencoded'
00075             content_length = len(form_data)
00076             input_stream = StringIO(form_data)
00077         environ = create_environ(path=path, query_string=query_string,
00078                                  method=method, input_stream=input_stream,
00079                                  content_type=content_type,
00080                                  content_length=content_length)
00081 
00082         environ['HTTP_USER_AGENT'] = 'MoinMoin/TestRequest'
00083         # must have reverse lookup or tests will be extremely slow:
00084         environ['REMOTE_ADDR'] = '127.0.0.1'
00085 
00086         if environ_overrides:
00087             environ.update(environ_overrides)
00088 
00089         super(TestRequest, self).__init__(environ)
00090 
00091 def evaluate_request(request):
00092     """ Evaluate a request and returns a tuple of application iterator,
00093     status code and list of headers. This method is meant for testing
00094     purposes.
00095     """
00096     output = []
00097     headers_set = []
00098     def start_response(status, headers, exc_info=None):
00099         headers_set[:] = [status, headers]
00100         return output.append
00101     result = request(request.environ, start_response)
00102 
00103     # any output via (WSGI-deprecated) write-callable?
00104     if output:
00105         result = output
00106     return (result, headers_set[0], headers_set[1])