def log_and_continue_request(route: Route, request: Request): resource_type = request.resource_type '''请求过滤''' if resource_type in ['image', 'media', 'eventsource', 'websocket']: route.abort() else: url_origin = request.url headers = request.headers method = request.method post_data_json = request.post_data_json print(url_origin, headers, method, post_data_json) if not url_origin: route.continue_() return if not self.filter_url_by_domain(url_origin) or self.filter_ext(url_origin): route.continue_() return http_har = dict() if method == 'POST' or method == 'PUT': post_data_origin = post_data_json post_data_handled = self._parse_post_data(post_data_origin) content_type = headers['content-type'] if 'content-type' in headers else '' http_har['originPostData'] = post_data_origin http_har['postData'] = post_data_handled http_har['contentType'] = content_type http_har['url'] = url_origin http_har['originUrl'] = url_origin http_har['method'] = method if method == 'GET': http_har = self.parse_static_url(url_origin) results.append(http_har) route.continue_()
def intercept(route: Route, request: Request): # 拦截前端跳转,主要方法是修改请求响应为204 TODO: 后续在遇到前端跳转的时候,优化hook逻辑 if request.is_navigation_request() and request.frame.parent_frame: request.response().status = 204 route.continue_() return # 尝试拦截后端跳转 if request.redirected_to: if request.post_data_json: request.response().status = 200 self.waiting_queue.put_nowait(request.redirected_to.url) else: ... route.continue_() return resource_type = request.resource_type # 过滤动态请求 if resource_type in ['image', 'media', 'eventsource', 'websocket']: route.abort() else: url_origin = request.url if not url_origin: route.continue_() return if not self.filter_ext(url=url_origin) and self.filter_url_by_domain(url=url_origin): headers = request.headers method = request.method post_data_json: dict = request.post_data_json http_har = dict() if method == 'POST' or method == 'PUT': post_data_origin = post_data_json post_data_handled = self._parse_post_data(post_data_origin) content_type = headers['content-type'] if 'content-type' in headers else '' http_har['originPostData'] = post_data_origin http_har['postData'] = post_data_handled http_har['contentType'] = content_type http_har['url'] = url_origin http_har['originUrl'] = url_origin http_har['method'] = method if method == 'GET': http_har = self.parse_static_url(url_origin) result.append(http_har) route.continue_()