miguelgrinberg/flask-sock

"Invalid frame header"

Opened this issue · 6 comments

Hello there. I've ran into a large problem with (probably) my first time use of Flask-Sock. Basically, when client or server closes the connection, the browser (client) gets a "Invalid frame header" error and the console says that the WS connection was an HTTP request, something like this:

127.0.0.1 - - [--/---/---- --:--:--] "GET /test/?arg=val HTTP/1.1" 200 -

To replicate my problem, this code should do the "trick":

from flask import *
from flask_sock import Sock as WebSocket

app = Flask(__name__, template_folder="dynamic")
sock = WebSocket(app)
connects = []

@sock.route("/test/")
def launchConnect(ws):
	if not type(request.args["arg"]) is str: return ws.close(reason="stupidity")
	arg = request.args["arg"]
	index = len(connects)
	connects.append(ws)
	while ws.connected: pass
	connects.pop(index)

app.secret_key = "pointlesspointless" # Using sessions
app.config["SESSION_TYPE"] = "filesystem"
app.run("0.0.0.0", 443, debug=False)

Browser code (ran in Chrome DevTools)

> ws = new WebSocket(location.origin.replace("http", "ws") + "/test/?arg=val")
< WebSocket {...}
> ws.send("yoohoo")
< undefined
> ws.close()
< undefined
WebSocket connection to 'ws://localhost:443/test/?arg=val' failed: Invalid frame header

I've tried doing lots of things to debug it, but nothing fought out the error. Can you help?

When I try to connect through WSS instead of WS:

127.0.0.1 - - [27/Mar/2024 20:38:05] code 400, message Bad request version ('ëÑ\x13')
127.0.0.1 - - [27/Mar/2024 20:38:05] "▬♥☺☻☺☺ü♥♥ iôú▲C ▼±×Ò(;µb5♣�àFÏA¶é�iïl�ØV→ 4J‼ȶ !¾l¢nã7�JÁ¶ó☺♫h$K#[¡l{U>t ZZ‼♥‼☺‼☻̨̩À+À/À,À0À‼À¶��/5☺☺�ºº#♥☻☻☺☺♣♣☺-☻☺☺♫♀       l
º☺♥Å 4³D�ì_◄¹2�cr�ÛFÙRùÃù!▬e±☺}:;â☼↨��[¾§� NÓn-Fõ8ÄmXÞ½s♫�¹Î�¸Ä|lnß*F§l;ÜA¹b�-»↔Þõפ©÷2�¸ý�÷ösÝßËpeÕ¬U☺ÀÛ�«v�û+ߦ%kZ�ãzi\£ËUÉQó&8�^Õ►; �§ù¾r@= °:Ó-Þî¨g�Þ▬↔ëÑ‼♂" 400 -
127.0.0.1 - - [27/Mar/2024 20:38:06] code 400, message Bad request version ('\x10¤ì')
127.0.0.1 - - [27/Mar/2024 20:38:06] "▬♥☺☻ ☺☻∟♥♥¯å­ÃwxÌ÷Jç¹→ñu[�µö§÷,Ú°�r#☺~?öXA ¼Z#↕Î�MD¾.F♠¦õè`�0�ôCá¢,ãÊ o Ý         ÚÚ‼♥‼☺‼☻̨̩À+À/À,À0À‼À¶��/5☺☺³::ÿ☺☺+♠ºº♥♦♥♥►♂
ú☺♥ õ®<☻ 4H�♥±�®ñËTMÎL£ÔN_xu#��9�»Z:Ðú�▲nFyäÒ(´*eÂåë£�c�☺¶«-çI!D2���³í±6 ►¤ì" 400 -

If you are going to use WSS you have to configure TLS in your server, you can't just send encrypted traffic to a regular server.

For the invalid frame header error, I suggest you retest using Gunicorn, which is the supported production web server. You are using Werkzeug which is not fully compatible with WebSocket. Some Werkzeug versions have minor WebSocket issues at disconnect time.

Then by any chance, do you know how to configure TLS in Windows?

Also I don't really mind whether do I want WS or WSS, I just want a WebSocket port in my Flask server. That's all I'd want.

Gunicorn does not run on Windows, unfortunately. Can you use WSL? That would allow you to run the UNIX version of Gunicorn on your Windows machine.

TLS is configured on your web server. For Gunicorn, you pass command line options to set up your certificate and key files.

Looks like my crappy code can't run locally. Too bad. 😔