-
Notifications
You must be signed in to change notification settings - Fork 51
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
permessage-deflate server implementation #110
Draft
wishdev
wants to merge
4
commits into
boazsegev:master
Choose a base branch
from
wishdev:permessage-deflate-server
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
84f4a8d
Update rake version to allow Ruby 3.0+ to compile
wishdev 46146c4
Fix Ruby 3.0 depreciation / Ruby 3.1 removal of rb_cData
wishdev 1f67412
WebSocket permessage-deflate server implementation
wishdev 8333cec
Update websocket deflate initialization
wishdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
copyright: Boaz Segev, 2017-2019 | ||
license: MIT | ||
|
||
Feel free to copy, use and enjoy according to the license specified. | ||
*/ | ||
#ifndef H_WEBSOCKET_DEFLATE_H | ||
/**\file | ||
|
||
A single file WebSocket permessage-deflate wrapper | ||
|
||
*/ | ||
#define H_WEBSOCKET_DEFLATE_H | ||
#include <fiobj.h> | ||
#include <stdlib.h> | ||
#include <zlib.h> | ||
|
||
#define WS_CHUNK 16384 | ||
|
||
z_stream *new_z_stream() { | ||
z_stream *strm = malloc(sizeof(*strm)); | ||
|
||
*strm = (z_stream){ | ||
.zalloc = Z_NULL, | ||
.zfree = Z_NULL, | ||
.opaque = Z_NULL, | ||
}; | ||
|
||
return strm; | ||
} | ||
|
||
z_stream *new_deflator() { | ||
z_stream *strm = new_z_stream(); | ||
|
||
int ret = deflateInit2(strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, | ||
-MAX_WBITS, 4, Z_DEFAULT_STRATEGY); | ||
|
||
return strm; | ||
} | ||
|
||
z_stream *new_inflator() { | ||
z_stream *strm = new_z_stream(); | ||
|
||
inflateInit2(strm, -MAX_WBITS); | ||
|
||
return strm; | ||
} | ||
|
||
int deflate_message(fio_str_info_s src, FIOBJ dest, z_stream *strm) { | ||
int ret, flush; | ||
unsigned have; | ||
unsigned char out[WS_CHUNK]; | ||
|
||
strm->avail_in = src.len; | ||
strm->next_in = src.data; | ||
|
||
do { | ||
strm->avail_out = WS_CHUNK; | ||
strm->next_out = out; | ||
ret = deflate(strm, Z_SYNC_FLUSH); | ||
have = WS_CHUNK - strm->avail_out; | ||
fiobj_str_write(dest, out, have); | ||
} while (strm->avail_out == 0); | ||
|
||
return Z_OK; | ||
} | ||
|
||
int inflate_message(fio_str_info_s src, FIOBJ dest, z_stream *strm) { | ||
int ret; | ||
unsigned have; | ||
unsigned char out[WS_CHUNK]; | ||
|
||
strm->avail_in = src.len; | ||
strm->next_in = src.data; | ||
|
||
do { | ||
strm->avail_out = WS_CHUNK; | ||
strm->next_out = out; | ||
ret = inflate(strm, Z_SYNC_FLUSH); | ||
switch (ret) { | ||
case Z_NEED_DICT: | ||
ret = Z_DATA_ERROR; | ||
case Z_DATA_ERROR: | ||
case Z_MEM_ERROR: | ||
(void)inflateEnd(strm); | ||
return ret; | ||
} | ||
have = WS_CHUNK - strm->avail_out; | ||
fiobj_str_write(dest, out, have); | ||
} while (strm->avail_out == 0); | ||
|
||
return ret; | ||
} | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In addition to testing the
deflate
setting, this should be automated to test for thepermessage-deflate
value in theSec-WebSocket-Extensions
header.Web app developers shouldn't have to know anything about the
permessage-deflate
WebSocket extension. The choice to enable or disabledeflate
support should be limited to the settings where the value0
is reserved to be replaced with a "smart" default and the "disabled" value should be -1.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.
I believe you are mistaking what the deflate variable within websocket_settings_s does (at least currently).
When a connection is upgraded - there needs to be something to store whether or not we want to send out the
sec-websocket-extensions: permessage-deflate
header. That is the job of deflate within websocket_settings_s.It also informs the iodine websocket struct later on via the websocket_attach method within websockets.c
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.
Yes, there is a place for global settings, it sets the HTTP and WebSocket behavior. For example, it limits WebSocket message sizes to avoid memory drain attacks on the server. This is usually set by the CLI (
iodine_cli_parse
) or theIodine.listen
method which sets the value in theiodine_http_listen
function.I believe this setting should definitely be settable using the CLI where current behavior should be preserved by setting
-ws-deflate = -1
.I also believe this value should be a numerical value representing the minimum length a text message would have to be before it is compressed. Wasting CPU cycles to compress a 16 byte long message (i.e. `""updates":{}") would be regrettable.