def test_get_multiple_match_headers(self): request = self.REQUEST response = self.RESPONSE archive = self.archive not_modified_response = httparchive.create_response(304) precondition_failed_response = httparchive.create_response(412) # if-match headers # If the request would, without the If-Match header field, # result in anything other than a 2xx or 412 status, # then the If-Match header MUST be ignored. request_headers = { 'if-match': self.ETAG_VALID, 'if-modified-since': self.DATE_PAST, } request = create_request(request_headers) self.assertEqual(archive.get(request), response) # Invalid etag, precondition failed request_headers = { 'if-match': self.ETAG_INVALID, 'if-modified-since': self.DATE_PAST, } request = create_request(request_headers) self.assertEqual(archive.get(request), precondition_failed_response) # 304 response; ignore if-match header request_headers = { 'if-match': self.ETAG_VALID, 'if-modified-since': self.DATE_FUTURE, } request = create_request(request_headers) self.assertEqual(archive.get(request), not_modified_response) # 304 response; ignore if-match header request_headers = { 'if-match': self.ETAG_INVALID, 'if-modified-since': self.DATE_PRESENT, } request = create_request(request_headers) self.assertEqual(archive.get(request), not_modified_response) # Invalid etag, precondition failed request_headers = { 'if-match': self.ETAG_INVALID, 'if-modified-since': self.DATE_INVALID, } request = create_request(request_headers) self.assertEqual(archive.get(request), precondition_failed_response)
def test_get_unmodified_headers(self): request = self.REQUEST response = self.RESPONSE archive = self.archive not_modified_response = httparchive.create_response(304) # Succeed check request_headers = {'if-unmodified-since': self.DATE_PAST} request = create_request(request_headers) self.assertEqual(archive.get(request), not_modified_response) # Fail check request_headers = {'if-unmodified-since': self.DATE_FUTURE} request = create_request(request_headers) self.assertEqual(archive.get(request), response) # Succeed check request_headers = {'if-unmodified-since': self.DATE_PRESENT} request = create_request(request_headers) self.assertEqual(archive.get(request), not_modified_response) # Fail check request_headers = {'if-unmodified-since': self.DATE_INVALID} request = create_request(request_headers) self.assertEqual(archive.get(request), response) # Fail check since the request is not a GET or HEAD request (as per RFC) request_headers = {'if-modified-since': self.DATE_PAST} request = httparchive.ArchivedHttpRequest('POST', 'www.test.com', '/', None, request_headers) self.assertEqual(archive.get(request), response)
def test_get_unmodified_headers(self): request = self.REQUEST response = self.RESPONSE archive = self.archive not_modified_response = httparchive.create_response(304) # Succeed check request_headers = {'if-unmodified-since': self.DATE_PAST} request = create_request(request_headers) self.assertEqual(archive.get(request), not_modified_response) # Fail check request_headers = {'if-unmodified-since': self.DATE_FUTURE} request = create_request(request_headers) self.assertEqual(archive.get(request), response) # Succeed check request_headers = {'if-unmodified-since': self.DATE_PRESENT} request = create_request(request_headers) self.assertEqual(archive.get(request), not_modified_response) # Fail check request_headers = {'if-unmodified-since': self.DATE_INVALID} request = create_request(request_headers) self.assertEqual(archive.get(request), response) # Fail check since the request is not a GET or HEAD request (as per RFC) request_headers = {'if-modified-since': self.DATE_PAST} request = httparchive.ArchivedHttpRequest( 'POST', 'www.test.com', '/', None, request_headers) self.assertEqual(archive.get(request), response)
def JsonResponse(data): """Return a ArchivedHttpResponse with |data| encoded as json in the body.""" status = 200 reason = 'OK' headers = [('content-type', 'application/json')] body = json.dumps(data) return httparchive.create_response(status, reason, headers, body)
def do_parse_and_handle_one_request(self): start_time = time.time() self.server.num_active_requests += 1 request = None try: if len(self.raw_requestline) > 65536: self.requestline = "" self.request_version = "" self.command = "" self.send_error(414) self.close_connection = 0 return if not self.raw_requestline: # This indicates that the socket has been closed by the client. self.close_connection = 1 return # self.parse_request() sets self.close_connection. There is no need to # set the property after the method is executed, unless custom behavior # is desired. if not self.parse_request(): # An error code has been sent, just exit. return try: response = None request = self.get_archived_http_request() if request is None: self.send_error(500) return response = self.server.custom_handlers.handle(request) if not response: response = self.server.http_archive_fetch(request) if ( response and response.status == 200 and self.server.allow_generate_304 and request.command in set(["GET", "HEAD"]) and ( request.headers.get("if-modified-since", None) or request.headers.get("if-none-match", None) ) ): # The WPR archive never get modified since it is not being recorded. response = httparchive.create_response(status=304, headers=response.headers) if response: self.send_archived_http_response(response) else: self.send_error(404) finally: self.wfile.flush() # Actually send the response if not already done. finally: request_time_ms = (time.time() - start_time) * 1000.0 self.server.total_request_time += request_time_ms if request: if response: logging.debug("Served: %s (%dms)", request, request_time_ms) else: logging.warning("Failed to find response for: %s (%dms)", request, request_time_ms) self.server.num_active_requests -= 1
def do_parse_and_handle_one_request(self): start_time = time.time() self.server.num_active_requests += 1 request = None try: if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) self.close_connection = 0 return if not self.raw_requestline: # This indicates that the socket has been closed by the client. self.close_connection = 1 return # self.parse_request() sets self.close_connection. There is no need to # set the property after the method is executed, unless custom behavior # is desired. if not self.parse_request(): # An error code has been sent, just exit. return try: response = None request = self.get_archived_http_request() if request is None: self.send_error(500) return response = self.server.custom_handlers.handle(request) if not response: response = self.server.http_archive_fetch(request) if (response and response.status == 200 and self.server.allow_generate_304 and request.command in set(['GET', 'HEAD']) and (request.headers.get('if-modified-since', None) or request.headers.get('if-none-match', None))): # The WPR archive never get modified since it is not being recorded. response = httparchive.create_response( status=304, headers=response.headers) if response: self.send_archived_http_response(response) else: self.send_error(404) finally: self.wfile.flush( ) # Actually send the response if not already done. finally: request_time_ms = (time.time() - start_time) * 1000.0 self.server.total_request_time += request_time_ms if request: if response: logging.debug('Served: %s (%dms)', request, request_time_ms) else: logging.warning('Failed to find response for: %s (%dms)', request, request_time_ms) self.server.num_active_requests -= 1
def test_get_etags(self): request = self.REQUEST response = self.RESPONSE archive = self.archive not_modified_response = httparchive.create_response(304) precondition_failed_response = httparchive.create_response(412) # if-match headers request_headers = {'if-match': self.ETAG_VALID} request = create_request(request_headers) self.assertEqual(archive.get(request), response) request_headers = {'if-match': self.ETAG_INVALID} request = create_request(request_headers) self.assertEqual(archive.get(request), precondition_failed_response) # if-none-match headers request_headers = {'if-none-match': self.ETAG_VALID} request = create_request(request_headers) self.assertEqual(archive.get(request), not_modified_response) request_headers = {'if-none-match': self.ETAG_INVALID} request = create_request(request_headers) self.assertEqual(archive.get(request), response)
def send_error(self, status, body=None): """Override the default send error with a version that doesn't unnecessarily close the connection. """ response = httparchive.create_response(status, body=body) self.send_archived_http_response(response)
def SimpleResponse(status): """Return a ArchivedHttpResponse with |status| code and a simple text body.""" return httparchive.create_response(status)