Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Invalid frame header" #77

Closed
RixInGithub opened this issue Mar 27, 2024 · 6 comments
Closed

"Invalid frame header" #77

RixInGithub opened this issue Mar 27, 2024 · 6 comments
Labels
question Further information is requested

Comments

@RixInGithub
Copy link

RixInGithub commented Mar 27, 2024

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
Copy link
Author

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
Copy link
Owner

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.

@miguelgrinberg miguelgrinberg added the question Further information is requested label Mar 27, 2024
@RixInGithub
Copy link
Author

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

@RixInGithub
Copy link
Author

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
Copy link
Owner

miguelgrinberg commented Mar 28, 2024

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
Copy link
Author

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

Repository owner locked and limited conversation to collaborators Oct 31, 2024
@miguelgrinberg miguelgrinberg converted this issue into discussion #96 Oct 31, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants