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

Consider removing assert about HTTP Reason-Phrase being always present #3906

Closed
WouldYouKindly opened this issue Jul 15, 2019 · 3 comments · Fixed by #3909
Closed

Consider removing assert about HTTP Reason-Phrase being always present #3906

WouldYouKindly opened this issue Jul 15, 2019 · 3 comments · Fixed by #3909

Comments

@WouldYouKindly
Copy link
Contributor

WouldYouKindly commented Jul 15, 2019

Hi!

aiohttp has the following code:

def raise_for_status(self) -> None:
    if 400 <= self.status:
        assert self.reason  # always not None for started response
        self.release()
        raise ClientResponseError(
            self.request_info,
            self.history,
            status=self.status,
            message=self.reason,
            headers=self.headers)

This assertion fails if the server omits Reason-Phrase, i.e. if response looks like HTTP/1.1 404 (no Not Found).

HTTP spec is not clear about whether reason must be provided, but e.g. this answer suggests that it may not be: https://stackoverflow.com/questions/17517086/can-an-http-response-omit-the-reason-phrase

Maybe aiohttp should be more permissive in this case. For me, the problem is that hoverfly drops the reason phrase, which leads to aiohttp raising AssertionError. Reason can be set to an empty string if it is not found in response.

@asvetlov
Copy link
Member

asvetlov commented Jul 16, 2019

That's interesting.
I expected that http parsers (both cython and pure python versions) set reason to empty string if omitted.
If it is not -- the PR for parsers fix is welcome.

@guyskk
Copy link

guyskk commented Jul 16, 2019

I find some website response without reason-phrase. I checked response.reason is None, is empty string OK? (assert "" still fail)

import aiohttp
import asyncio

url = 'http://pyconcn.qiniudn.com/images/volunteer/zhouqi.png'

async def main():
    async with aiohttp.ClientSession() as client:
        async with client.get(url) as resp:
            print(resp.status)
            resp.raise_for_status()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
>>>
530
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kk/.pyenv/versions/3.6.5/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
    return future.result()
  File "<stdin>", line 5, in main
  File "/Users/kk/.pyenv/versions/rssant/lib/python3.6/site-packages/aiohttp/client_reqrep.py", line 935, in raise_for_status
    assert self.reason  # always not None for started response
AssertionError

@achimnol
Copy link
Member

I just encountered this issue while doing integration with one of my customer site.
Hoping to see the next release soon!

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.

4 participants