Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async def proxy_handler(request: web.Request) -> web.Response:
- """
- Check request contains http url in query args:
- /fetch?url=http%3A%2F%2Fexample.com%2F
- and trying to fetch it and return body with http status.
- If url passed without scheme or is invalid raise 400 Bad request.
- On failure raise 502 Bad gateway.
- :param request: aiohttp.web.Request to handle
- :return: aiohttp.web.Response
- """
- url = yarl.URL(request.url).query.get('url')
- url_scheme = yarl.URL(url).scheme if url else None
- if not url:
- text, status = 'No url to fetch', 400
- elif not url_scheme:
- text, status = 'Empty url scheme', 400
- elif url_scheme != 'http':
- text, status = 'Bad url scheme: {}'.format(yarl.URL(url).scheme), 400
- else:
- async with request.app['session'] as session:
- response = await session.get(url)
- text, status = await response.text(), response.status
- return web.Response(text=text, status=status)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement