Visitar URL original
Uses HTTP/1.1 and HOST header for CONNECT method by GtTmy · Pull Request #677 · websocket-client/websocket-client · GitHub
Skip to content

Uses HTTP/1.1 and HOST header for CONNECT method - #677

Merged
engn33r merged 1 commit into
websocket-client:masterfrom
GtTmy:master
Apr 19, 2021
Merged

engn33r merged 1 commit into
websocket-client:masterfrom
GtTmy:master

Conversation

@GtTmy

@GtTmy GtTmy commented Apr 19, 2021 •

Copy link
Copy Markdown
Contributor

1. What is the problem with the current code

I'm behind a proxy and I have a problem that the proxy server blocks websocket-client's connections.
The proxy server reads HOST header in order to filter connections, but I found that websocket-client does not add HOST header to CONNECT method.

cpython's library also has the same problem that websocket-client has. python/cpython#8305 will solve it.

2. How your changes make it better

This adds HOST header to CONNECT method and uses HTTP/1.1.

3. Provide some example code that can allow someone else to recreate the problem with the current code and test your solution (if possible to recreate).

https://github.com/websocket-client/websocket-client/blob/master/examples/echo_client.py can be used to recreate.

Without this PR

> python .\examples\echo_client.py
Connecting proxy...
--- request header ---
CONNECT echo.websocket.org:80 HTTP/1.0


-----------------------
--- response header ---
HTTP/1.1 200 OK
Content-Length: 0
Connection: Keep-Alive
-----------------------
--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Host: echo.websocket.org
Origin: http://echo.websocket.org
Sec-WebSocket-Key: PdFFTTmR04GcFC8kbmY3cw==
Sec-WebSocket-Version: 13
Connection: Upgrade


-----------------------
--- response header ---
HTTP/1.1 101 Web Socket Protocol Handshake
Date: Mon, 19 Apr 2021 06:24:51 GMT
Connection: upgrade
Sec-WebSocket-Accept: KA2gx8B0ybeSszDlJMCIy8KnwVs=
Server: Kaazing Gateway
Upgrade: websocket
-----------------------
Sending 'Hello, World'...
send: b'\x81\x8c\xc2v\n6\x8a\x13fZ\xadZ*a\xad\x04fR'
Sent
Receiving...
Received 'Hello, World'
send: b'\x88\x82\xbd\xcd\xfc\xb4\xbe%'

With this PR

> python .\examples\echo_client.py
Connecting proxy...
--- request header ---
CONNECT echo.websocket.org:80 HTTP/1.1
Host: echo.websocket.org:80


-----------------------
--- response header ---
HTTP/1.1 200 OK
Content-Length: 0
Connection: Keep-Alive
-----------------------
--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Host: echo.websocket.org
Origin: http://echo.websocket.org
Sec-WebSocket-Key: dSbfvFfqZVo/xBg6eXZEzw==
Sec-WebSocket-Version: 13
Connection: Upgrade


-----------------------
--- response header ---
HTTP/1.1 101 Web Socket Protocol Handshake
Date: Mon, 19 Apr 2021 06:23:39 GMT
Connection: upgrade
Sec-WebSocket-Accept: uQhGtHvbo/ZC9Am8RlfiEMpCjvs=
Server: Kaazing Gateway
Upgrade: websocket
-----------------------
Sending 'Hello, World'...
send: b'\x81\x8c6v\xa9i~\x13\xc5\x05YZ\x89>Y\x04\xc5\r'
Sent
Receiving...
Received 'Hello, World'
send: b'\x88\x82\xb9\x05\x98\xc7\xba\xed'

@codecov

codecov Bot commented Apr 19, 2021

Copy link
Copy Markdown

Codecov Report

Merging #677 (e6439ac) into master (910e6e1) will increase coverage by 0.32%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #677      +/-   ##
==========================================
+ Coverage   71.31%   71.64%   +0.32%     
==========================================
  Files          13       13              
  Lines        1304     1305       +1     
  Branches      259      259              
==========================================
+ Hits          930      935       +5     
+ Misses        249      246       -3     
+ Partials      125      124       -1     
Impacted Files Coverage Δ
websocket/_http.py 60.19% <100.00%> (+0.19%) ⬆️
websocket/_core.py 78.21% <0.00%> (+0.99%) ⬆️
websocket/_utils.py 84.61% <0.00%> (+5.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 910e6e1...e6439ac. Read the comment docs.

@engn33r

engn33r commented Apr 19, 2021

Copy link
Copy Markdown
Collaborator

Thank you for the detailed PR 👍

If you are using a public or open source proxy, could you share which proxy you are using? I am not sure if any edge case issues may be encountered by adding this specific Host header value in certain proxies, so I would like to document this for future knowledge. As for using HTTP/1.1, websocket-client already uses HTTP/1.1 for non-proxy connections, so I don't see a problem doing the same for proxy connections.

@engn33r
engn33r merged commit 96f7941 into websocket-client:master Apr 19, 2021
@GtTmy

GtTmy commented Apr 21, 2021 •

Copy link
Copy Markdown
Contributor Author

Thank you for merging it!

I'm sorry that I don't know well which proxy is used since our company manages it.

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 this pull request may close these issues.

3 participants