SHARE
TWEET

Untitled

a guest Dec 11th, 2019 97 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.         text, status = 'No url to fetch', 400
  15.     elif not url_scheme:
  16.         text, status = 'Empty url scheme', 400
  17.     elif url_scheme != 'http':
  18.         text, status = 'Bad url scheme: {}'.format(yarl.URL(url).scheme), 400
  19.     else:
  20.         async with request.app['session'] as session:
  21.             response = await session.get(url)
  22.             text, status = await response.text(), response.status
  23.     return web.Response(text=text, status=status)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top