python-hyper / hyper

HTTP/2 for Python.
http://hyper.rtfd.org/en/latest/
MIT License
1.05k stars 191 forks source link

HTTP20Connection.request has no mechanism to send duplicate headers #437

Closed bneradt closed 3 years ago

bneradt commented 3 years ago

The HTTP20Connection.request takes a dictionary of headers. This is problematic because, far as I can tell, this prevents the ability to send a request with duplicate headers (multiple header fields with the same name). The spec explicitly allows for duplicate headers. See for example RFC 7541 section 2.3.2:

The dynamic table can contain duplicate entries (i.e., entries with the same name and same value). Therefore, duplicate entries MUST NOT be treated as an error by a decoder.

h2.H2Connection.send_headers addresses this by taking an iterable of tuples, allowing for duplicates:

headers (An iterable of two tuples of bytestrings or HeaderTuple objects.) – The request/response headers to send.

Can request be updated to allow for the same (i.e., an iterable of tuples)? As a suggestion, I believe this can easily be done in the implementation here:

https://github.com/python-hyper/hyper/blob/75f25f73c17a16cb1f2ca7d46f390b0dfe4925dc/hyper/http20/connection.py#L261

We can wrap that in a try/except block, handling AttributeError to simply set all_headers to headers. Something like:

    try:
        all_headers = headers.items()
    except AttributeError:
        all_headers = headers
sethmlarson commented 3 years ago

This project is no longer maintained, please find an alternative like HTTPX.

bneradt commented 3 years ago

I appreciate the reply.

I should point out that the README gives no indication of this. To the contrary, it even says:

hyper is maintained by Cory Benfield, with contributions from others. For more details about the contributors, please see CONTRIBUTORS.rst.

It might be helpful to you and others to indicate that the project is abandoned to avoid further issues like this.

bneradt commented 3 years ago

In case this helps someone else: in parallel, I posted a StackOverflow question about this issue to see whether there is a practical work around, and the suggestion which works for me is to use email.Message.EmailMessage:

https://stackoverflow.com/a/65604524/629530