Guest User

Untitled

a guest
Dec 10th, 2019
180
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async def proxy_handler(request: web.Request) -> web.Response:
  2.     """
  3.    Check request contains http url in query args:
  4.        /fetch?url=http%3A%2F%2Fexample.com%2F
  5.    and trying to fetch it and return body with http status.
  6.    If url passed without scheme or is invalid raise 400 Bad request.
  7.    On failure raise 502 Bad gateway.
  8.    :param request: aiohttp.web.Request to handle
  9.    :return: aiohttp.web.Response
  10.    """
  11.     url = yarl.URL(request.url).query.get('url')
  12.     url_scheme = yarl.URL(url).scheme if url else None
  13.     if not url:
  14.         return web.Response(text='No url to fetch', status=400)
  15.     if not url_scheme:
  16.         return web.Response(text='Empty url scheme', status=400)
  17.     if url_scheme != 'http':
  18.         return web.Response(text='Bad url scheme: {}'.format(yarl.URL(url).scheme), status=400)
  19.     async with request.app['session'] as session:
  20.         response = await session.get(url)
  21.         return web.Response(text=await response.text(), status=response.status)
RAW Paste Data