-
Notifications
You must be signed in to change notification settings - Fork 142
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
Support for WebSocket Protocol header #99
base: master
Are you sure you want to change the base?
Conversation
Hi @joeyhoek , Thank you for opening this PR! I love the general approach and the desire to make things easy for all users using sensible defaults. However, I suspect that the use of the After all, different applications might use the I wonder, do you think that having each application handle and set the Kindly, P.S.
AFAIK, it's possible to set a cookie with an authentication token (such as one with a one-time token or an OAuth2 token). This cookie will be available in the HTTP handshake (the Of course, you can also send the token with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I'm not yet sold on the idea that the sec-websocket-protocol
header should be handled by the facil.io framework (rather than handled by the app developer in the on_update
callback), I thought I'd review the code and see if it might help me make up my mind about the best approach.
Most of the code looks nice 👏🏻, though there are some minor concerns that could effect performance (as well as a debug message I suspect you intended to delete).
if (tmp_protocol) { | ||
char *protocol_str = fiobj_obj2cstr(tmp_protocol).data; | ||
|
||
if (strlen(protocol_str)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid strlen
for strings with a known length. Length data is returned as part of the fiobj_obj2cstr
return value and there's no need to re-compute the string length (it is cached).
char *protocol_str = fiobj_obj2cstr(tmp_protocol).data; | ||
|
||
if (strlen(protocol_str)) { | ||
printf("HALLO\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this some debugging line? if required, it is better to use FIO_LOG_DEBUG
with a proper message.
|
||
tmp_ptr = strchr(protocol_str, ','); | ||
if (tmp_ptr != NULL) | ||
*tmp_ptr = '\0'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I agree that it's better to avoid an additional string copy, I think this is destructive to the original copy which someone might still want to access later on.
It is better to use tmp_protocol = fiobj_str_new(protocol_str, (tmp_ptr - protocol_str))
instead of destructively adding a NUL char just to recalculate the length (especially since the length is already known by the value of tmp_ptr
).
Hi @boazsegev, I think that every application should be able to set it personally, however I'm unaware of this feature in the
Therefore the content is unknown. It is best to be able to set that data per request and like I said I'm unaware we can set that header for response in the As for the written code. I only just read the documentation part of how I could get the length of the string, so I could address the Just let me know if its possible to set the response in the |
Hi @joeyhoek ,
The exact same code you used in the commit should be possible in the |
The problem I was facing was that I'm using Javascript running in a browser. Therefore I need to connect to a WebSocket with the default JS functions and unable to use a 3rd party alternative for NodeJS. I needed to authorize before the WebSocket connection is opened else it leaves you vulnerable to DDoS attacks. This leaves one secure option: passing the token in the sec-websocket-protocol header. I found out there was no support in Facil.io yet.
There are three different types of requests coming in:
Any other way would fail to connect. Normally would there be checks if the WebSocket Server actually supports those protocols. This code just makes it possible that no matter the value you are still able to connect.