示例#1
0
 def test_get_current_url_unicode(self):
     env = create_environ()
     env['QUERY_STRING'] = 'foo=bar&baz=blah&meh=\xcf'
     rv = wsgi.get_current_url(env)
     self.assertEqual(
         rv, 'http://localhost/?foo=bar&baz=blah&meh=\ufffd'
     )
示例#2
0
 def base_url(self):
     """Like :attr:`url` but without the querystring
     See also: :attr:`trusted_hosts`.
     """
     return get_current_url(self.environ,
                            strip_querystring=True,
                            trusted_hosts=self.trusted_hosts)
示例#3
0
 def host_url(self):
     """Just the host with scheme as IRI.
     See also: :attr:`trusted_hosts`.
     """
     return get_current_url(self.environ,
                            host_only=True,
                            trusted_hosts=self.trusted_hosts)
示例#4
0
 def test_get_current_url_unicode(self):
     env = create_environ()
     env['QUERY_STRING'] = 'foo=bar&baz=blah&meh=\xcf'
     rv = wsgi.get_current_url(env)
     self.assertEqual(
         rv, 'http://localhost/?foo=bar&baz=blah&meh=\ufffd'
     )
示例#5
0
 def url_root(self):
     """The full URL root (with hostname), this is the application
     root as IRI.
     See also: :attr:`trusted_hosts`.
     """
     return get_current_url(self.environ,
                            True,
                            trusted_hosts=self.trusted_hosts)
示例#6
0
 def extract_wsgi(self, environ, headers):
     """Extract the server's set-cookie headers as cookies into the
     cookie jar.
     """
     self.extract_cookies(
         _TestCookieResponse(headers),
         Request(get_current_url(environ)),
     )
示例#7
0
文件: test.py 项目: bwhmather/verktyg
 def extract_wsgi(self, environ, headers):
     """Extract the server's set-cookie headers as cookies into the
     cookie jar.
     """
     self.extract_cookies(
         _TestCookieResponse(headers),
         Request(get_current_url(environ)),
     )
示例#8
0
    def get_wsgi_headers(self, environ):
        """This is automatically called right before the response is started
        and returns headers modified for the given environment.  It returns a
        copy of the headers from the response with some modifications applied
        if necessary.

        For example the location header (if present) is joined with the root
        URL of the environment.  Also the content length is automatically set
        to zero here for certain status codes.

        :param environ: the WSGI environment of the request.
        :return: returns a new :class:`~verktyg.http.Headers`
                 object.
        """
        headers = Headers(self.headers)
        location = None
        content_location = None
        content_length = None
        status = self.status_code

        # iterate over the headers to find all values in one go.  Because
        # get_wsgi_headers is used each response that gives us a tiny
        # speedup.
        for key, value in headers:
            ikey = key.lower()
            if ikey == u'location':
                location = value
            elif ikey == u'content-location':
                content_location = value
            elif ikey == u'content-length':
                content_length = value

        # make sure the location header is an absolute URL
        if location is not None:
            old_location = location
            if isinstance(location, str):
                # Safe conversion is necessary here as we might redirect
                # to a broken URI scheme (for instance itms-services).
                location = iri_to_uri(location, safe_conversion=True)

            if self.autocorrect_location_header:
                current_url = get_current_url(environ, root_only=True)
                if isinstance(current_url, str):
                    current_url = iri_to_uri(current_url)
                location = urljoin(current_url, location)
            if location != old_location:
                headers['Location'] = location

        # make sure the content location is a URL
        if (
            content_location is not None and
            isinstance(content_location, str)
        ):
            headers['Content-Location'] = iri_to_uri(content_location)

        # remove entity headers and set content length to zero if needed.
        # Also update content_length accordingly so that the automatic
        # content length detection does not trigger in the following
        # code.
        if 100 <= status < 200 or status == 204:
            headers['Content-Length'] = content_length = u'0'
        elif status == 304:
            remove_entity_headers(headers)

        # if we can determine the content length automatically, we
        # should try to do that.  But only if this does not involve
        # flattening the iterator or encoding of unicode strings in
        # the response.  We however should not do that if we have a 304
        # response.
        if (
            self.automatically_set_content_length and
            self.is_sequence and content_length is None and status != 304
        ):
            try:
                content_length = sum(len(to_bytes(x, 'ascii'))
                                     for x in self.response)
            except UnicodeError:
                # aha, something non-bytestringy in there, too bad, we
                # can't safely figure out the length of the response.
                pass
            else:
                headers['Content-Length'] = str(content_length)

        return headers
示例#9
0
    def get_wsgi_headers(self, environ):
        """This is automatically called right before the response is started
        and returns headers modified for the given environment.  It returns a
        copy of the headers from the response with some modifications applied
        if necessary.

        For example the location header (if present) is joined with the root
        URL of the environment.  Also the content length is automatically set
        to zero here for certain status codes.

        :param environ:
            The WSGI environment of the request.
        :return:
            Returns a new :class:`~verktyg.http.Headers` object.
        """
        headers = Headers(self.headers)
        location = None
        content_location = None
        content_length = None
        status = self.status_code

        # iterate over the headers to find all values in one go.  Because
        # get_wsgi_headers is used each response that gives us a tiny
        # speedup.
        for key, value in headers:
            ikey = key.lower()
            if ikey == u'location':
                location = value
            elif ikey == u'content-location':
                content_location = value
            elif ikey == u'content-length':
                content_length = value

        # make sure the location header is an absolute URL
        if location is not None:
            old_location = location
            if isinstance(location, str):
                # Safe conversion is necessary here as we might redirect
                # to a broken URI scheme (for instance itms-services).
                location = iri_to_uri(location)

            if self.autocorrect_location_header:
                current_url = get_current_url(environ, root_only=True)
                if isinstance(current_url, str):
                    current_url = iri_to_uri(current_url)
                location = urljoin(current_url, location)
            if location != old_location:
                headers['Location'] = location

        # make sure the content location is a URL
        if (
            content_location is not None and
            isinstance(content_location, str)
        ):
            headers['Content-Location'] = iri_to_uri(content_location)

        # remove entity headers and set content length to zero if needed.
        # Also update content_length accordingly so that the automatic
        # content length detection does not trigger in the following
        # code.
        if 100 <= status < 200 or status == 204:
            headers['Content-Length'] = content_length = u'0'
        elif status == 304:
            remove_entity_headers(headers)

        # if we can determine the content length automatically, we
        # should try to do that.  But only if this does not involve
        # flattening the iterator or encoding of unicode strings in
        # the response.  We however should not do that if we have a 304
        # response.
        if (
            self.automatically_set_content_length and
            self.is_sequence and content_length is None and status != 304
        ):
            content_length = sum(len(x) for x in self.response)
            headers['Content-Length'] = str(content_length)

        return headers
示例#10
0
 def url(self):
     """The reconstructed current URL as IRI.
     See also: :attr:`trusted_hosts`.
     """
     return get_current_url(self.environ, trusted_hosts=self.trusted_hosts)