python-hyper / hyper

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

Upgrade from HTTP/1.1. to HTTP2 not working #420

Closed kfreter closed 4 years ago

kfreter commented 4 years ago

I was trying to send a POST message to an HTTP server. It seems the protocol was doing an upgrade. I added a print line to the file "/usr/lib/python2.7/site-packages/hyper/http11/connection.py": print 'KEF: status = ' + str(response.status) + ', body = ' + response.msg.tobytes() + ', headers = ' + str(headers)

The output I receive is: KEF: status = 101, body = Switching Protocols, headers = HTTPHeaderMap([('Connection', 'Upgrade'), ('Upgrade', 'h2c'), ('Date', 'Mon, 11 Nov 2019 04:15:45 GMT')])

The default code for executing the "raise HTTPUpgrade(H2C_PROTOCOL, self._sock)" line wasn't firing. this checked the status code and "b'upgrade' in headers['connection'] and H2C_PROTOCOL.encode('utf-8')". Instead it was falling to the HTTP11Response() call which asserted: File "/usr/lib/python2.7/site-packages/hyper/http11/response.py", line 65, in init assert self._expect_close or self._length is not None or self._chunked AssertionError

I changed the code to only look for the status code of 101 and it works fine:

and b'upgrade' in headers['connection'] and H2C_PROTOCOL.encode('utf-8') in headers['upgrade']):

    if (response.status == 101):
        raise HTTPUpgrade(H2C_PROTOCOL, self._sock)

    return HTTP11Response(
        response.status,
        response.msg.tobytes(),
        headers,
        self._sock,
        self
    )

I don't know the HTTP/2 protocol in any detail, but this seems to either be a code bug given where the error occurred or the HTTP server returned the wrong data.

sethmlarson commented 4 years ago

Hyper is no longer maintained so this is unlikely to be fixed even if it is a bug. If you're looking for a maintained HTTP/2-capable HTTP client I'd recommend HTTPX although HTTPX doesn't support non-TLS HTTP/2 negotiation.

kfreter commented 4 years ago

Seth, thanks. I was looking for a simple HTTP/2 server as well as client. FYI I have hyper working both as a HTTP/2 server and HTTP/1.1 + HTTP/2 client (in non-TLS mode), with a code change as per the issue raised. I'll look to migrate to something else.

On Wed, Nov 13, 2019 at 7:32 AM Seth Michael Larson < notifications@github.com> wrote:

Closed #420 https://github.com/python-hyper/hyper/issues/420.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/python-hyper/hyper/issues/420?email_source=notifications&email_token=AEAQXMU7UUXGCJV73ON3DT3QTQFYFA5CNFSM4JLQQ4G2YY3PNVWWK3TUL52HS4DFWZEXG43VMVCXMZLOORHG65DJMZUWGYLUNFXW5KTDN5WW2ZLOORPWSZGOU2QAS7Q#event-2795506046, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEAQXMXJWYVP6BBSLDWFAX3QTQFYFANCNFSM4JLQQ4GQ .

-- Thanks,

Karl