def parsed_url(self): """\ Return the current URL as a tuple of the form:: (addressing scheme, network location, path, parameters, query, fragment identifier) Synopsis:: >>> from fresco import FrescoApp >>> app = FrescoApp() >>> with app.requestcontext( ... 'https://example.com/animals/view?name=lion') as c: ... c.request.parsed_url # doctest: +ELLIPSIS ParseResult(scheme=u'https', netloc=u'example.com', ...) Components are returned as unicode strings """ if self._parsed_url: return self._parsed_url env = self.environ.get script_name = quote(self.script_name.encode(self.charset)) path_info = quote(self.path_info.encode(self.charset)) query_string = self.query_string scheme = environ_to_unicode(env('wsgi.url_scheme', 'http')) try: host = environ_to_unicode(self.environ['HTTP_HOST']) if ':' in host: host, port = host.split(':', 1) else: if scheme == 'https': port = '443' else: port = '80' except KeyError: host = environ_to_unicode(self.environ['SERVER_NAME']) port = environ_to_unicode(self.environ['SERVER_PORT']) if (scheme == 'http' and port == '80') \ or (scheme == 'https' and port == '443'): netloc = host else: netloc = host + ':' + port self._parsed_url = ParseResult( scheme, netloc, script_name + path_info, '', # Params query_string, '', # Fragment ) return self._parsed_url
def path(self): """\ Return the path component of the requested URL """ return environ_to_unicode( self.environ.get('SCRIPT_NAME', '') + self.environ.get('PATH_INFO', ''))
def query( self, environ_to_unicode=environ_to_unicode ) -> MultiDict: # type: ignore """ Return a ``MultiDict`` of any querystring submitted data. This is available regardless of whether the original request was a ``GET`` request. Synopsis:: >>> from fresco import FrescoApp >>> with FrescoApp().requestcontext('/?animal=moose') as c: ... c.request.query['animal'] u'moose' This property always returns querystring data, regardless of the request method used. """ if self._query is None: query = environ_to_unicode(self.environ.get('QUERY_STRING', '')) self._query = MultiDict(parse_querystring(query, self.charset)) return self._query
def remote_addr(self): """ Return the remote address of the client """ s = self.environ.get('REMOTE_ADDR') if s is None: return s return environ_to_unicode(s)
def referrer(self): """ Return the HTTP referer header, or ``None`` if this is not available. """ s = self.environ.get('HTTP_REFERER') if s is None: return s return environ_to_unicode(s)
def query_string( self, # type: ignore environ_to_unicode=environ_to_unicode) -> Optional[str]: """\ The QUERY_STRING value as a unicode string """ s = self.environ.get('QUERY_STRING') if s is None: return s return environ_to_unicode(s)
def script_name(self): """\ The SCRIPT_NAME value as a unicode string Note that SCRIPT_NAME is already unquoted by the server """ try: return environ_to_unicode(self.environ.get('SCRIPT_NAME', ''), self.charset) except UnicodeDecodeError: raise exceptions.BadRequest
def path_info( self, environ_to_unicode=environ_to_unicode) -> str: # type: ignore """ The PATH_INFO value as a string Note that PATH_INFO is already unquoted by the server """ try: return environ_to_unicode(self.environ.get('PATH_INFO', ''), self.charset) except UnicodeDecodeError: raise exceptions.BadRequest
def form(self): """ Return the contents of submitted form data This will return the ``POST`` or ``PUT`` data when available, otherwise querystring (``GET``) data. Querystring data is always available via the ``query`` property. """ if self._form is None: if self.environ['REQUEST_METHOD'] in ('PUT', 'POST'): self._form = MultiDict( parse_post( self.environ, self.environ['wsgi.input'], self.charset, self.MAX_SIZE, self.MAX_MULTIPART_SIZE, ie_workaround=self.IE_CONTENT_DISPOSITION_WORKAROUND)) else: data = environ_to_unicode(self.environ.get('QUERY_STRING', '')) self._form = MultiDict(parse_querystring(data, self.charset)) return self._form