async def fetch_content(client: aiohttp.ClientSession, request: Request, response: Response) -> None: url = request.query_params["url"] proxy_query_header = make_request_headers(request.headers) try: proxy_response = await client.get(url, headers=proxy_query_header, timeout=TIMEOUT) except ClientSSLError: # Invalid SSL Certificate status = 526 except ConnectionError: status = 523 except ClientTimeout: status = 524 except TooManyRedirects: status = 520 else: response.body = await proxy_response.content.read() if proxy_response.status == 500: response.status = 520 else: copy_proxy_headers(proxy_response, response) response.headers["Access-Control-Allow-Origin"] = get_access_url( request.headers)
async def handler(self, request: Request, method: str) -> Response: _ = self.__server __response = Response(status_code=404) _endpoint_method_data = request.app.state.mock.get(request.url.path) _header = dict(zip(request.headers.keys(), request.headers.values())) _query = dict(zip(request.query_params.keys(), request.query_params.values())) _body = {} __raw_body = await request.body() try: _form = await request.form() _keys = _form.keys() _values = _form.values() _body = dict(zip(_keys, _values)) finally: pass try: _body = await request.json() except JSONDecodeError: pass if _endpoint_method_data: _endpoint: Dict = _endpoint_method_data.get(method, {'responses': [], 'delay': 0}) _responses: List[SwaVanResponse] = _endpoint.get('responses', '') for _response in _responses: _matched_rules = rule_matcher(_response.rules, _header, _query, _body) _and_rules = _response.connector.lower() == "and" and all(_matched_rules) _or_rules = _response.connector.lower() == "or" and any(_matched_rules) _have_rules = len(_response.rules) != 0 _and_or_rules_and_has_rules = _and_rules or _or_rules or not _have_rules _and_or_rule_matched = RuleStatus.Matched if _and_or_rules_and_has_rules else RuleStatus.Unmatched _code_rule_matched = filter_by_code( _response.filter_by, query=_query, headers=_header, body=_body, rule_status=_and_or_rule_matched) if _response.filter_by else _and_or_rule_matched if _code_rule_matched == RuleStatus.Matched: if _response.redirect: try: __response_data = await SwaVanRestEndpoint.proxy_request( _response, method, request.app.state.proxies) if __response_data: __response = __response_data except requests.exceptions.ConnectionError as err: __response.status_code = 500 __response.body = b"Unable to redirect request, please check the log message" SwaVanLogRecorder.send_log(f"Unable to redirect request {err}") elif _response.content_path: __response = await SwaVanRestEndpoint.file_response(_response) else: __response = await SwaVanRestEndpoint.make_response(_response) break sleeping_period = _endpoint.get('delay') or request.app.state.delay or 0 time.sleep(sleeping_period) return __response
async def post_solr(rest_of_path: str, request: Request, response: Response): async with aiohttp.ClientSession() as session: solr_url = f"{SOLR_URL}/solr/{rest_of_path}" proxy = await session.post(solr_url, json=await request.json(), params=request.query_params) response.body = await proxy.read() response.status_code = proxy.status response.charset = proxy.charset response.headers.update(proxy.headers) return response
async def tile_request(path: str, response: Response): async with httpx.AsyncClient() as client: proxy = await client.get(f'http://127.0.0.1:8082/{path}') response.body = proxy.content response.status_code = proxy.status_code return response