def add_entry(self, resource: HttpResource, page_ref: int=None) -> None: entry = { "startedDateTime": isoformat(resource.request.start_time), "time": int((resource.response.complete_time - resource.request.start_time) * 1000), "_red_messages": self.format_notes(resource) } if page_ref: entry['pageref'] = "page%s" % page_ref request = { 'method': resource.request.method, 'url': resource.request.uri, 'httpVersion': "HTTP/1.1", 'cookies': [], 'headers': self.format_headers(resource.request.headers), 'queryString': [], 'headersSize': -1, 'bodySize': -1, } response = { 'status': resource.response.status_code, 'statusText': resource.response.status_phrase, 'httpVersion': "HTTP/%s" % resource.response.version, 'cookies': [], 'headers': self.format_headers(resource.response.headers), 'content': { 'size': resource.response.decoded_len, 'compression': resource.response.decoded_len - resource.response.payload_len, 'mimeType': (get_header(resource.response.headers, 'content-type') or [""])[0], }, 'redirectURL': ( get_header(resource.response.headers, 'location') or [""])[0], 'headersSize': resource.response.header_length, 'bodySize': resource.response.payload_len, } cache = {} # type: Dict[None, None] timings = { 'dns': -1, 'connect': -1, 'blocked': 0, 'send': 0, 'wait': int((resource.response.start_time - resource.request.start_time) * 1000), 'receive': int((resource.response.complete_time - resource.response.start_time) * 1000), } entry.update({ 'request': request, 'response': response, 'cache': cache, 'timings': timings, }) self.har['log']['entries'].append(entry) # type: ignore
def add_entry(self, red, page_ref=None): entry = { "startedDateTime": isoformat(red.req_ts), "time": int((red.res_done_ts - red.req_ts) * 1000), "_red_messages": self.format_messages(red) } if page_ref: entry['pageref'] = "page%s" % page_ref request = { 'method': red.method, 'url': red.uri, 'httpVersion': "HTTP/1.1", 'cookies': [], 'headers': self.format_headers(red.req_hdrs), 'queryString': [], 'headersSize': -1, 'bodySize': -1, } response = { 'status': red.res_status, 'statusText': red.res_phrase, 'httpVersion': "HTTP/%s" % red.res_version, 'cookies': [], 'headers': self.format_headers(red.res_hdrs), 'content': { 'size': red.res_body_decode_len, 'compression': red.res_body_decode_len - red.res_body_len, 'mimeType': (get_header(red.res_hdrs, 'content-type') or [""])[0], }, 'redirectURL': (get_header(red.res_hdrs, 'location') or [""])[0], 'headersSize': red.header_length, 'bodySize': red.res_body_len, } cache = {} timings = { 'dns': -1, 'connect': -1, 'blocked': 0, 'send': 0, 'wait': int((red.res_ts - red.req_ts) * 1000), 'receive': int((red.res_done_ts - red.res_ts) * 1000), } entry.update({ 'request': request, 'response': response, 'cache': cache, 'timings': timings, }) self.har['log']['entries'].append(entry)
def status100(self): # Continue if self.request and not "100-continue" in get_header( self.request.headers, 'expect'): self.add_note('', rs.UNEXPECTED_CONTINUE)
def __init__( self, config: SectionProxy, method: str, query_string: bytes, req_headers: RawHeaderListType, req_body: bytes, exchange: HttpResponseExchange, error_log: Callable[[str], int] = sys.stderr.write, ) -> None: self.config = config # type: SectionProxy self.query_string = parse_qs( query_string.decode(self.config["charset"], "replace")) self.req_headers = req_headers self.req_body = req_body self.body_args = {} self.exchange = exchange self.error_log = error_log # function to log errors to # query processing self.test_uri = self.query_string.get("uri", [""])[0] self.test_id = self.query_string.get("id", [None])[0] self.req_hdrs = [ tuple(h.split(":", 1)) # type: ignore for h in self.query_string.get("req_hdr", []) if h.find(":") > 0 ] # type: StrHeaderListType self.format = self.query_string.get("format", ["html"])[0] self.descend = "descend" in self.query_string self.check_name = None # type: str if not self.descend: self.check_name = self.query_string.get("check_name", [None])[0] self.charset_bytes = self.config["charset"].encode("ascii") self.save_path = None # type: str self.timeout = None # type: Any self.start = time.time() if method == "POST": req_ct = get_header(self.req_headers, b"content-type") if req_ct and req_ct[-1].lower( ) == b"application/x-www-form-urlencoded": self.body_args = parse_qs( req_body.decode(self.config["charset"], "replace")) if ("save" in self.query_string and self.config.get("save_dir", "") and self.test_id): extend_saved_test(self) elif "slack" in self.query_string: slack_run(self) elif "client_error" in self.query_string: self.dump_client_error() elif self.test_uri: self.run_test() else: self.show_default() elif method in ["GET", "HEAD"]: if self.test_id: load_saved_test(self) elif "code" in self.query_string: slack_auth(self) else: self.show_default() else: self.error_response( find_formatter("html")(self.config, None, self.output), b"405", b"Method Not Allowed", "Method Not Allowed", )