def simpleapp(environ, start_response): status = '200 OK' response_headers = [('Content-type','text/plain')] start_response(status, response_headers) request = WSGIRequest(environ) return [ 'Hello world!\n', 'The get is %s' % str(request.GET), ' and Val is %s\n' % request.GET.get('name'), 'The languages are: %s\n' % request.languages, 'The accepttypes is: %s\n' % request.match_accept(['text/html', 'application/xml'])]
def simpleapp(environ, start_response): status = '200 OK' response_headers = [('Content-type', 'text/plain')] start_response(status, response_headers) request = WSGIRequest(environ) return [ 'Hello world!\n', 'The get is %s' % str(request.GET), ' and Val is %s\n' % request.GET.get('name'), 'The languages are: %s\n' % request.languages, 'The accepttypes is: %s\n' % request.match_accept(['text/html', 'application/xml']) ]
def handle_fileupload(environ, start_response): start_response('200 OK', [('Content-type', 'text/plain')]) request = WSGIRequest(environ) assert len(request.POST) == 1 assert isinstance(request.POST.keys()[0], str) fs = request.POST['thefile'] assert isinstance(fs, cgi.FieldStorage) assert isinstance(fs.filename, str) assert fs.filename == '寿司.txt' assert fs.value == 'Sushi' request.charset = 'UTF-8' assert len(request.POST) == 1 assert isinstance(request.POST.keys()[0], str) fs = request.POST['thefile'] assert isinstance(fs, cgi.FieldStorage) assert isinstance(fs.filename, unicode) assert fs.filename == u'寿司.txt' assert fs.value == 'Sushi' request.charset = None assert fs.value == 'Sushi' return []
def handle_fileupload(environ, start_response): start_response('200 OK', [('Content-type','text/plain')]) request = WSGIRequest(environ) assert len(request.POST) == 1 assert isinstance(request.POST.keys()[0], str) fs = request.POST['thefile'] assert isinstance(fs, cgi.FieldStorage) assert isinstance(fs.filename, str) assert fs.filename == '寿司.txt' assert fs.value == 'Sushi' request.charset = 'UTF-8' assert len(request.POST) == 1 assert isinstance(request.POST.keys()[0], str) fs = request.POST['thefile'] assert isinstance(fs, cgi.FieldStorage) assert isinstance(fs.filename, unicode) assert fs.filename == u'寿司.txt' assert fs.value == 'Sushi' request.charset = None assert fs.value == 'Sushi' return []
def assert_valid_name(environ): if encoding is not no_encoding: WSGIRequest.defaults._push_object( dict(content_type='text/html', charset=encoding)) try: request = WSGIRequest(environ) if post: params = request.POST else: params = request.GET assert params['name'] == name assert request.params['name'] == name finally: if encoding is not no_encoding: WSGIRequest.defaults._pop_object()
def __call__(self, environ, start_response): registry = environ['paste.registry'] environ_config = environ.setdefault('pylons.environ_config', {}) if self.setup_cache: registry.register(pylons.cache, environ['beaker.cache']) environ_config['cache'] = 'beaker.cache' if self.setup_session: registry.register(pylons.session, environ['beaker.session']) environ_config['session'] = 'beaker.session' if self.setup_g: registry.register(pylons.g, self.g) # Update the environ environ.update(self.environ) registry.register(pylons.request, WSGIRequest(environ)) registry.register(pylons.response, WSGIResponse()) return self.app(environ, start_response)
def setup_app_env(self, environ, start_response): """Setup and register all the Pylons objects with the registry""" if self.log_debug: log.debug("Setting up Pylons stacked object globals") registry = environ['paste.registry'] registry.register(WSGIRequest.defaults, self.request_options) registry.register(WSGIResponse.defaults, self.response_options) req = WSGIRequest(environ) # Setup the basic pylons global objects registry.register(pylons.request, req) registry.register(pylons.response, WSGIResponse()) registry.register(pylons.buffet, self.buffet) registry.register(pylons.g, self.globals) registry.register(pylons.config, self.config) registry.register(pylons.h, self.helpers or \ pylons.legacy.load_h(self.package_name)) # Setup the translator global object registry.register(pylons.translator, gettext.NullTranslations()) lang = self.config.get('lang') if lang: set_lang(lang) if self.config['pylons.strict_c']: registry.register(pylons.c, ContextObj()) else: registry.register(pylons.c, AttribSafeContextObj()) econf = environ['pylons.environ_config'] if econf.get('session'): registry.register(pylons.session, environ[econf['session']]) if econf.get('cache'): registry.register(pylons.cache, environ[econf['cache']])
def __call__(self, environ, start_response): start_response("204 No content", []) self.request = WSGIRequest(environ) return [b'']
def __call__(self, environ, start_response): """Resolves the URL in PATH_INFO, and uses wsgi.routing_args to pass on URL resolver results.""" config = request_config() config.mapper = self.mapper old_method = None if self.use_method_override: req = WSGIRequest(environ) req.errors = 'ignore' if '_method' in environ.get('QUERY_STRING', '') and \ '_method' in req.GET: old_method = environ['REQUEST_METHOD'] environ['REQUEST_METHOD'] = req.GET['_method'].upper() log.debug( "_method found in QUERY_STRING, altering request" " method to %s", environ['REQUEST_METHOD']) elif is_form_post(environ) and '_method' in req.POST: old_method = environ['REQUEST_METHOD'] environ['REQUEST_METHOD'] = req.POST['_method'].upper() log.debug( "_method found in POST data, altering request " "method to %s", environ['REQUEST_METHOD']) # Run the actual route matching # -- Assignment of environ to config triggers route matching config.environ = environ match = config.mapper_dict route = config.route if old_method: environ['REQUEST_METHOD'] = old_method urlinfo = "%s %s" % (environ['REQUEST_METHOD'], environ['PATH_INFO']) if not match: match = {} log.debug("No route matched for %s", urlinfo) else: log.debug("Matched %s", urlinfo) log.debug("Route path: '%s', defaults: %s", route.routepath, route.defaults) log.debug("Match dict: %s", match) for key, val in match.iteritems(): if val and isinstance(val, basestring): match[key] = urllib.unquote_plus(val) environ['wsgiorg.routing_args'] = ((), match) environ['routes.route'] = route # If the route included a path_info attribute and it should be used to # alter the environ, we'll pull it out if self.path_info and match.get('path_info'): oldpath = environ['PATH_INFO'] newpath = match.get('path_info') or '' environ['PATH_INFO'] = newpath if not environ['PATH_INFO'].startswith('/'): environ['PATH_INFO'] = '/' + environ['PATH_INFO'] environ['SCRIPT_NAME'] += re.sub(r'^(.*?)/' + newpath + '$', r'\1', oldpath) if environ['SCRIPT_NAME'].endswith('/'): environ['SCRIPT_NAME'] = environ['SCRIPT_NAME'][:-1] response = self.app(environ, start_response) del config.environ del self.mapper.environ return response
def __call__(self, environ, start_response): request = WSGIRequest(environ) url = self.redirect_url(environ) log.debug("Sending redirect to %s", url) return HTTPSeeOther(url).wsgi_application(environ, start_response)
def __call__(self, environ, start_response): """Resolves the URL in PATH_INFO, and uses wsgi.routing_args to pass on URL resolver results.""" config = request_config() config.mapper = self.mapper old_method = None if self.use_method_override: req = WSGIRequest(environ) req.errors = 'ignore' if '_method' in environ.get('QUERY_STRING', '') and \ '_method' in req.GET: old_method = environ['REQUEST_METHOD'] environ['REQUEST_METHOD'] = req.GET['_method'].upper() if self.log_debug: log.debug("_method found in QUERY_STRING, altering request" " method to %s", environ['REQUEST_METHOD']) elif is_form_post(environ) and '_method' in req.POST: old_method = environ['REQUEST_METHOD'] environ['REQUEST_METHOD'] = req.POST['_method'].upper() if self.log_debug: log.debug("_method found in POST data, altering request " "method to %s", environ['REQUEST_METHOD']) # Run the actual route matching # -- Assignment of environ to config triggers route matching config.environ = environ match = config.mapper_dict route = config.route if old_method: environ['REQUEST_METHOD'] = old_method urlinfo = "%s %s" % (environ['REQUEST_METHOD'], environ['PATH_INFO']) if not match: match = {} if self.log_debug: log.debug("No route matched for %s", urlinfo) elif self.log_debug: log.debug("Matched %s", urlinfo) log.debug("Route path: '%s', defaults: %s", route.routepath, route.defaults) log.debug("Match dict: %s", match) url = URLGenerator(self.mapper, environ) environ['wsgiorg.routing_args'] = ((url), match) environ['routes.route'] = route environ['routes.url'] = url if hasattr(route, 'redirect'): route_name = '_redirect_%s' % id(route) location = url_for(route_name, **match) log.debug("Using redirect route, redirect to '%s' with status" "code: %s", location, route.redirect_status) start_response(route.redirect_status, [('Content-Type', 'text/plain; charset=utf8'), ('Location', location)]) return [] # If the route included a path_info attribute and it should be used to # alter the environ, we'll pull it out if self.path_info and match.get('path_info'): oldpath = environ['PATH_INFO'] newpath = match.get('path_info') or '' environ['PATH_INFO'] = newpath if not environ['PATH_INFO'].startswith('/'): environ['PATH_INFO'] = '/' + environ['PATH_INFO'] environ['SCRIPT_NAME'] += re.sub(r'^(.*?)/' + newpath + '$', r'\1', oldpath) if environ['SCRIPT_NAME'].endswith('/'): environ['SCRIPT_NAME'] = environ['SCRIPT_NAME'][:-1] response = self.app(environ, start_response) # Wrapped in try as in rare cases the attribute will be gone already try: del config.environ del self.mapper.environ except AttributeError: pass return response
def __call__(self, environ, start_response): """Resolves the URL in PATH_INFO, and uses wsgi.routing_args to pass on URL resolver results.""" config = request_config() config.mapper = self.mapper old_method = None if self.use_method_override: req = WSGIRequest(environ) req.errors = "ignore" if "_method" in environ.get("QUERY_STRING", "") and "_method" in req.GET: old_method = environ["REQUEST_METHOD"] environ["REQUEST_METHOD"] = req.GET["_method"].upper() log.debug("_method found in QUERY_STRING, altering request" " method to %s", environ["REQUEST_METHOD"]) elif ( environ["REQUEST_METHOD"] == "POST" and "application/x-www-form-urlencoded" in environ.get("CONTENT_TYPE", "") and "_method" in req.POST ): old_method = environ["REQUEST_METHOD"] environ["REQUEST_METHOD"] = req.POST["_method"].upper() log.debug("_method found in POST data, altering request " "method to %s", environ["REQUEST_METHOD"]) config.environ = environ match = config.mapper_dict route = config.route if old_method: environ["REQUEST_METHOD"] = old_method urlinfo = "%s %s" % (environ["REQUEST_METHOD"], environ["PATH_INFO"]) if not match: match = {} log.debug("No route matched for %s", urlinfo) else: log.debug("Matched %s", urlinfo) log.debug("Route path: '%s', defaults: %s", route.routepath, route.defaults) log.debug("Match dict: %s", match) for key, val in match.iteritems(): if val and isinstance(val, basestring): match[key] = urllib.unquote_plus(val) environ["wsgiorg.routing_args"] = ((), match) environ["routes.route"] = route # If the route included a path_info attribute and it should be used to # alter the environ, we'll pull it out if self.path_info and match.get("path_info"): oldpath = environ["PATH_INFO"] newpath = match.get("path_info") or "" environ["PATH_INFO"] = newpath if not environ["PATH_INFO"].startswith("/"): environ["PATH_INFO"] = "/" + environ["PATH_INFO"] environ["SCRIPT_NAME"] += re.sub(r"^(.*?)/" + newpath + "$", r"\1", oldpath) if environ["SCRIPT_NAME"].endswith("/"): environ["SCRIPT_NAME"] = environ["SCRIPT_NAME"][:-1] response = self.app(environ, start_response) del config.environ del self.mapper.environ return response
def __call__(self, environ, start_response): """Resolves the URL in PATH_INFO, and uses wsgi.routing_args to pass on URL resolver results.""" config = request_config() config.mapper = self.mapper old_method = None if self.use_method_override: req = WSGIRequest(environ) req.errors = 'ignore' if '_method' in environ.get('QUERY_STRING', '') and \ '_method' in req.GET: old_method = environ['REQUEST_METHOD'] environ['REQUEST_METHOD'] = req.GET['_method'].upper() log.debug("_method found in QUERY_STRING, altering request" " method to %s", environ['REQUEST_METHOD']) elif is_form_post(environ) and '_method' in req.POST: old_method = environ['REQUEST_METHOD'] environ['REQUEST_METHOD'] = req.POST['_method'].upper() log.debug("_method found in POST data, altering request " "method to %s", environ['REQUEST_METHOD']) # Run the actual route matching # -- Assignment of environ to config triggers route matching config.environ = environ match = config.mapper_dict route = config.route if old_method: environ['REQUEST_METHOD'] = old_method urlinfo = "%s %s" % (environ['REQUEST_METHOD'], environ['PATH_INFO']) if not match: match = {} log.debug("No route matched for %s", urlinfo) else: log.debug("Matched %s", urlinfo) log.debug("Route path: '%s', defaults: %s", route.routepath, route.defaults) log.debug("Match dict: %s", match) environ['wsgiorg.routing_args'] = ((), match) environ['routes.route'] = route # If the route included a path_info attribute and it should be used to # alter the environ, we'll pull it out if self.path_info and match.get('path_info'): oldpath = environ['PATH_INFO'] newpath = match.get('path_info') or '' environ['PATH_INFO'] = newpath if not environ['PATH_INFO'].startswith('/'): environ['PATH_INFO'] = '/' + environ['PATH_INFO'] environ['SCRIPT_NAME'] += re.sub(r'^(.*?)/' + newpath + '$', r'\1', oldpath) if environ['SCRIPT_NAME'].endswith('/'): environ['SCRIPT_NAME'] = environ['SCRIPT_NAME'][:-1] response = self.app(environ, start_response) del config.environ del self.mapper.environ return response
def __init__(self, environ, start_response): WSGIRequest.__init__(self, environ) self.start_response = start_response self.dbsession = environ["dbsession"] self.datamodel = environ["datamodel"]