Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aiohttp raises ValueError for invalid Location: in redir #2630

Closed
wumpus opened this issue Dec 28, 2017 · 7 comments · Fixed by #6722
Closed

aiohttp raises ValueError for invalid Location: in redir #2630

wumpus opened this issue Dec 28, 2017 · 7 comments · Fixed by #6722

Comments

@wumpus
Copy link

wumpus commented Dec 28, 2017

Long story short

I am crawling lots of sites, and I don't expect aiohttp to be raising ValueError.

Expected behaviour

Raise an exception that's documented

Actual behaviour

Yarl's ValueError is passed up to the top

Steps to reproduce

import aiohttp
import asyncio
import sys

hostname = None

async def fetch(session, url):
        async with session.get(url) as response:
            return await response.text()

async def main():
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, hostname)
        print(html)

hostname = sys.argv[1]
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
$ python ./bug.py http://www.oberoithreesixtywest.in/
...
  File "/home/lindahl/.pyenv/versions/3.6.4/lib/python3.6/site-packages/yarl/__init__.py", line 348, in origin
    raise ValueError("URL should be absolute")
ValueError: URL should be absolute

Note:

$ curl -D /dev/tty http://www.oberoithreesixtywest.in/
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 28 Dec 2017 06:35:33 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Frame-Options: GOFORIT
Location: http://

Your environment

aiohttp==2.3.6
yarl==0.16.0

@asvetlov
Copy link
Member

Location: http:// is invalid URL, isn't it?

@wumpus
Copy link
Author

wumpus commented Dec 28, 2017

Yes. I was expecting a better-named exception than ValueError, and text like 'Redirect Location header is invalid'

@asvetlov
Copy link
Member

If you need a new feature -- please make a Pull Request ;)

@wumpus
Copy link
Author

wumpus commented Dec 29, 2017

I don't know what the exception should be named, or which library it should be named in. Those are architecture questions. Heck, for all I know you think ValueError() is a fine error for aiohttp to have, and this bug is invalid. I don't know.

@deasmhumhna
Copy link

Also encountered this problem. Shouldn't this return an instance of the aiohttp.InvalidURL exception that already exists? Is it a backward compatibility thing? Either way, it seems too general to have to catch any old ValueError to catch invalid URLs.

@mike-hart
Copy link

I've been recently similarly affected by this. Catching ValueError feels a little too broad on a call to session.get unless https://github.com/aio-libs/yarl was the only thing capable of raising ValueError?

I'm a strong second on @deasmhumhna request for InvalidURL, otherwise we have two invalid URL exceptions to manage. https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.InvalidURL seems to indicate this should occur, or perhaps something akin to InvalidRedirect should exist.

setla added a commit to setla/aiohttp that referenced this issue Feb 14, 2024
This patch introduces 5 granular user-facing exceptions that may occur
when HTTP requests are made:
* `InvalidUrlClientError`
* `RedirectClientError`
* `NonHttpUrlClientError`
* `InvalidUrlRedirectClientError`
* `NonHttpUrlRedirectClientError`

Previously `ValueError` or `InvalidURL` was raised and screening out was
complicated (a valid URL that redirects to invalid one raised the same error
as an invalid URL).

Ref: aio-libs#6722 (comment)

PR aio-libs#6722

Resolves aio-libs#2507
Resolves aio-libs#2630
Resolves aio-libs#3315

Co-authored-by: Sviatoslav Sydorenko <sviat@redhat.com>
(cherry picked from commit fb465e1)
webknjaz pushed a commit that referenced this issue Feb 14, 2024
…rchy in the HTTP client (#8158)

**This is a backport of PR #6722 as merged into master
(fb465e1).**

This patch introduces 5 granular user-facing exceptions that may occur
when HTTP requests are made:
* `InvalidUrlClientError`
* `RedirectClientError`
* `NonHttpUrlClientError`
* `InvalidUrlRedirectClientError`
* `NonHttpUrlRedirectClientError`

Previously `ValueError` or `InvalidURL` was raised and screening out was
complicated (a valid URL that redirects to invalid one raised the same
error
as an invalid URL).

Ref:
#6722 (comment)

PR #6722

Resolves #2507
Resolves #2630
Resolves #3315

Co-authored-by: Sviatoslav Sydorenko <sviat@redhat.com>
(cherry picked from commit fb465e1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants