miguelgrinberg / flask-sock

Modern WebSocket support for Flask.
MIT License
272 stars 24 forks source link

"Invalid frame header" #77

Open RixInGithub opened 4 months ago

RixInGithub commented 4 months ago

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?

RixInGithub commented 4 months ago

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 -
miguelgrinberg commented 4 months ago

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.

RixInGithub commented 4 months ago

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

RixInGithub commented 4 months ago

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.

miguelgrinberg commented 4 months ago

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.

RixInGithub commented 4 months ago

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