def __init__(self, server, priv_request): log.Logger.__init__(self, server) log.LogProxy.__init__(self, server) self._server = server self._ref = priv_request self._secured = server.is_secured content_type = self.get_header("content-type") mime_type, encoding = http.parse_content_type(content_type) language = self.get_header("content-language") or http.DEFAULT_LANGUAGE location = http.path2tuple(self._ref.path) accept = self.get_header("accept") accepted_mime_types = http.parse_accepted_types(accept) accept_tree = http.build_mime_tree(accepted_mime_types) accept_charset = self.get_header("accept-charset") accepted_encodings = http.parse_accepted_charsets(accept_charset) accept_languages = self.get_header("accept-languages") accepted_languages = http.parse_accepted_languages(accept_languages) method = http.Methods[self._ref.method] protocol = _protocol_lookup[self._ref.clientproto] self._mime_type = mime_type self._encoding = encoding self._language = language self._location = location self._accepted_mime_types = accepted_mime_types self._accept_tree = accept_tree self._accepted_encodings = accepted_encodings self._accepted_languages = accepted_languages self._method = method self._protocol = protocol self._credentials = None self._context = {} # Black box self._reading = False self._objects = [] # Look for URI arguments only, the POST is content, not arguments uri_parts = self._ref.uri.split("?", 1) if len(uri_parts) > 1: arguments = twhttp.parse_qs(uri_parts[1], 1) else: arguments = {} # Look for domain information domain = self.get_header("host") if not domain: domain = "%s:%s" % (self._ref.host.host, self._ref.host.port) domain = self._decode(domain) content_length = self.get_header("content-length") length = content_length and int(content_length) # To prevent content being consumed # when it's application/x-www-form-urlencoded self._ref.content.seek(0, 0) self._arguments = arguments self._domain = domain self._length = length
def check_expected(request): if status is not None: self.assertEqual(request.code, status) if content is not None: self.assertEqual(request.content.getvalue(), content) if (mime is not None) or (encoding is not None): content_type = http.DEFAULT_MIMETYPE charset = compat.http2python(http.DEFAULT_ENCODING) if "content-type" in request.response_headers: header = request.response_headers["content-type"] content_type, charset = http.parse_content_type(header) if mime is not None: self.assertEqual(content_type, mime) if encoding is not None: self.assertEqual(charset, encoding) if language is not None: content_language = http.DEFAULT_LANGUAGE if "content-language" in request.response_headers: header = request.response_headers["content-language"] content_language = header self.assertEqual(content_language, language) if allowed_encodings is not None: self.assertTrue("allow-charset" in request.response_headers) header = request.response_headers["allow-charset"] values = [e.strip() for e in header.split(",")] self.assertEqual(tuple(values), allowed_encodings) if allowed_languages is not None: self.assertTrue("allow-language" in request.response_headers) header = request.response_headers["allow-language"] values = [e.strip() for e in header.split(",")] self.assertEqual(tuple(values), allowed_languages) if allowed_methods is not None: self.assertTrue("allow" in request.response_headers) header = request.response_headers["allow"] values = [e.strip() for e in header.split(",")] self.assertEqual(tuple(values), allowed_methods) if www_auth is not None: self.assertTrue("www-authenticate" in request.response_headers) header = request.response_headers["www-authenticate"] self.assertEqual(header, www_auth) if location is not None: self.assertTrue("location" in request.response_headers) header = request.response_headers["location"] self.assertEqual(header, location)
def check(ct, expected): result = http.parse_content_type(ct) self.assertEqual(result, expected)