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

Response doesn't deal well with CIMultiDictProxy headers #3207

Closed
ArthurKantor opened this issue Aug 21, 2018 · 6 comments
Closed

Response doesn't deal well with CIMultiDictProxy headers #3207

ArthurKantor opened this issue Aug 21, 2018 · 6 comments
Assignees
Labels

Comments

@ArthurKantor
Copy link

ArthurKantor commented Aug 21, 2018

Long story short

Example badness:

from aiohttp.web_exceptions import HTTPNotFound
from multidict import CIMultiDictProxy, CIMultiDict
raise HTTPNotFound(text='hi',headers=CIMultiDictProxy(CIMultiDict()))

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-51ed331c9efe> in <module>()
      1 from aiohttp.web_exceptions import HTTPNotFound
      2 from multidict import CIMultiDictProxy, CIMultiDict
----> 3 raise HTTPNotFound(text='hi',headers=CIMultiDictProxy(CIMultiDict()))

/usr/local/lib/python3.7/dist-packages/aiohttp/web_exceptions.py in __init__(self, headers, reason, body, text, content_type)
     79         Response.__init__(self, status=self.status_code,
     80                           headers=headers, reason=reason,
---> 81                           body=body, text=text, content_type=content_type)
     82         Exception.__init__(self, self.reason)
     83         if self.body is None and not self.empty_body:

/usr/local/lib/python3.7/dist-packages/aiohttp/web_response.py in __init__(self, body, status, reason, text, headers, content_type, charset)
    467                     charset = 'utf-8'
    468                 headers[hdrs.CONTENT_TYPE] = (
--> 469                     content_type + '; charset=' + charset)
    470                 body = text.encode(charset)
    471                 text = None

TypeError: 'CIMultiDictProxy' object does not support item assignment

This is useful e.g. when an aiohttp app is acting like a proxy and is relaying errors from backends.

Expected behaviour

same as when using CIMultiDict:

from aiohttp.web_exceptions import HTTPNotFound
from multidict import CIMultiDictProxy, CIMultiDict
raise HTTPNotFound(text='hi',headers=CIMultiDict(CIMultiDict()))

---------------------------------------------------------------------------
HTTPNotFound                              Traceback (most recent call last)
<ipython-input-14-cc0e33a6117b> in <module>()
      1 from aiohttp.web_exceptions import HTTPNotFound
      2 from multidict import CIMultiDictProxy, CIMultiDict
----> 3 raise HTTPNotFound(text='hi',headers=CIMultiDict(CIMultiDict()))

HTTPNotFound: Not Found

Actual behaviour

see above

Steps to reproduce

Your environment

AIOHTTP 3.3.2
python3.7

@webknjaz
Copy link
Member

This might need to go to multidict project itself.

@ArthurKantor
Copy link
Author

it seems to me that CIMultiDictProxy is actually behaving correctly. It's supposed to be immutable.

@webknjaz
Copy link
Member

Well... Yeah. That's why it's called "proxy" :)

@ArthurKantor
Copy link
Author

I think a simple fix would be here:
by changing

        elif not isinstance(headers, (CIMultiDict, CIMultiDictProxy)):
            headers = CIMultiDict(headers)

to

        elif not isinstance(headers, CIMultiDict):
            headers = CIMultiDict(headers)

@webknjaz
Copy link
Member

Feel free to create a PR and invite @asvetlov to give more feedback on it.

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants