def application(environ, start_response): # The WSGI server puts content length and type in the environment # even when not provided with the request. Drop them if they are empty. if environ.get('CONTENT_LENGTH') == '': del environ['CONTENT_LENGTH'] if environ.get('CONTENT_TYPE') == '': del environ['CONTENT_TYPE'] wrequest = WerkzeugRequest(environ) data = wrequest.get_data() request = Request( method=wrequest.method, url=wrequest.url, headers=wrequest.headers, data=data, ) prepared = request.prepare() stream = streams.build_output_stream( args, env, prepared, response=None, output_options=args.output_options) streams.write_stream(stream, env.stdout, env.stdout_isatty) # When there is data in the request, give the next one breathing room. if data: print("\n", file=env.stdout) # Make dreams come true. response = Response(headers={'Server': server}) return response(environ, start_response)
def _request(self, path, method="POST", data=None, headers=None): """yield request, endpoint for given http request data.""" werkzeug_env = EnvironBuilder( method=method, path=path, data=data, headers=[ ("cookie", "session_id=%s" % self.session.sid), ("content-type", "application/json"), ] + (headers or []), environ_base={ "HTTP_HOST": "localhost", "REMOTE_ADDR": "127.0.0.1" }, ).get_environ() werkzeug_request = WerkzeugRequest(werkzeug_env) self.odoo_root.setup_session(werkzeug_request) werkzeug_request.session.db = self.env.cr.dbname self.odoo_root.setup_db(werkzeug_request) self.odoo_root.setup_lang(werkzeug_request) request = self.odoo_root.get_request(werkzeug_request) request._env = self.env with request: routing_map = self.env["ir.http"].routing_map() endpoint, dummy = routing_map.bind_to_environ(werkzeug_env).match( return_rule=False, ) yield request, partial(endpoint, **request.params)
def __init__(self, environ, start_response=None): self._wz = WerkzeugRequest(environ) self.start_response = start_response self.headers = CaseInsensitiveDict( self._wz.headers.to_wsgi_list() ) #: A case-insensitive dictionary, containg all headers sent in the Request. self.method = ( self._wz.method.lower() ) #: The incoming HTTP method used for the request, lower-cased. self.full_url = ( self._wz.url ) #: The full URL of the Request, query parameters and all. self.url = (self._wz.base_url ) #: The URL of the Request, without query parameters. self.full_path = ( self._wz.full_path ) #: The full path portion of the URL of the Request, query parameters and all. self.path = ( self._wz.path ) #: The path portion of the URL of the Request, without query parameters. self.params = flatten( parse_qs(self._wz.query_string.decode("utf-8")) ) #: A dictionary of the parsed query paramaters used for the Request. self.query = self._wz.query_string.decode( "utf-8" ) #: A string containing only the query paramaters of the Request. self.raw = self._wz.stream #: A raw file-like stream of the incoming Request. self.content = self._wz.get_data( cache=True, as_text=False) #: The Request body, as bytes. self.mimetype = self._wz.mimetype #: The mimetype of the incoming Request. # TODO: rip that out self.text = self._wz.get_data( cache=False, as_text=True) #: The Request body, as unicode. self.formats = None
def to_request(self, environ): werkzeug_request = WerkzeugRequest(environ) request = Request(server_port=environ.get('SERVER_PORT', None), server_name=environ.get('SERVER_NAME', None), remote_addr=werkzeug_request.remote_addr, uri=werkzeug_request.path, script_name=werkzeug_request.script_root, query_string=werkzeug_request.query_string, scheme=werkzeug_request.scheme, method=werkzeug_request.method, headers=dict(werkzeug_request.headers), body=get_input_stream(environ).read()) return request
def from_environ(kls, environ): self = kls() self._wz = WerkzeugRequest(environ) self.headers = CaseInsensitiveDict(self._wz.headers.to_list()) self.method = self._wz.method self.full_url = self._wz.url self.url = self._wz.base_url self.full_path = self._wz.full_path self.path = self._wz.path self.params = parse_qs(self._wz.query_string.decode("utf-8")) self.raw = self._wz.stream self.content = self._wz.get_data(cache=True, as_text=False) self.text = self._wz.get_data(cache=True, as_text=True) self.data = self._wz.get_data(cache=True, as_text=True, parse_form_data=True) return self
def _fetch_image(self, path): public_user = self.env.ref('base.public_user') session_store = root_wsgi.session_store session = session_store.new() session.update({ 'db': threading.current_thread().dbname, 'login': public_user.login, 'uid': public_user.id, 'context': self.env.context, }) werkzeug_env = EnvironBuilder(path).get_environ() werkzeug_request = WerkzeugRequest(werkzeug_env) werkzeug_request.session = session # construct an odoo request with this werkzeug request. request = http.HttpRequest(werkzeug_request) with request: request._env = self.env(user=public_user) endpoint, arguments = http.routing_map( self.env.registry._init_modules, False, self.env['ir.http']._get_converters()).bind_to_environ( werkzeug_env).match(return_rule=False, ) yield endpoint, arguments
def from_environ(kls, environ, start_response=None): self = kls() self._wz = WerkzeugRequest(environ) self.headers = CaseInsensitiveDict(self._wz.headers.to_wsgi_list()) self.method = self._wz.method.lower() self.full_url = self._wz.url self.url = self._wz.base_url self.full_path = self._wz.full_path self.path = self._wz.path self.params = flatten(parse_qs(self._wz.query_string.decode("utf-8"))) self.query = self._wz.query_string.decode("utf-8") self.raw = self._wz.stream self.content = self._wz.get_data(cache=True, as_text=False) self.mimetype = self._wz.mimetype self.accepts_mimetypes = self._wz.accept_mimetypes self.text = self._wz.get_data(cache=False, as_text=True) # self.dispatched = False self._start_response = start_response self._environ = environ self.formats = None return self