opal-up 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/up_ext/App.h +665 -544
- data/ext/up_ext/AsyncSocket.h +307 -284
- data/ext/up_ext/AsyncSocketData.h +35 -51
- data/ext/up_ext/BloomFilter.h +37 -42
- data/ext/up_ext/ChunkedEncoding.h +174 -175
- data/ext/up_ext/ClientApp.h +20 -23
- data/ext/up_ext/HttpContext.h +476 -381
- data/ext/up_ext/HttpContextData.h +20 -20
- data/ext/up_ext/HttpErrors.h +14 -10
- data/ext/up_ext/HttpParser.h +631 -563
- data/ext/up_ext/HttpResponse.h +526 -460
- data/ext/up_ext/HttpResponseData.h +59 -55
- data/ext/up_ext/HttpRouter.h +328 -310
- data/ext/up_ext/Loop.h +174 -168
- data/ext/up_ext/LoopData.h +60 -67
- data/ext/up_ext/MoveOnlyFunction.h +71 -80
- data/ext/up_ext/PerMessageDeflate.h +218 -198
- data/ext/up_ext/ProxyParser.h +100 -99
- data/ext/up_ext/QueryParser.h +91 -84
- data/ext/up_ext/TopicTree.h +273 -268
- data/ext/up_ext/Utilities.h +25 -25
- data/ext/up_ext/WebSocket.h +376 -310
- data/ext/up_ext/WebSocketContext.h +487 -372
- data/ext/up_ext/WebSocketContextData.h +74 -62
- data/ext/up_ext/WebSocketData.h +53 -46
- data/ext/up_ext/WebSocketExtensions.h +194 -178
- data/ext/up_ext/WebSocketHandshake.h +115 -110
- data/ext/up_ext/WebSocketProtocol.h +441 -398
- data/ext/up_ext/up_ext.c +43 -5
- data/lib/up/ruby/cluster.rb +29 -6
- data/lib/up/version.rb +1 -1
- metadata +2 -2
@@ -21,14 +21,15 @@
|
|
21
21
|
/* There is a new, huge bug scenario that needs to be fixed:
|
22
22
|
* pub/sub does not support being in DEDICATED_COMPRESSOR-mode while having
|
23
23
|
* some clients downgraded to SHARED_COMPRESSOR - we cannot allow the client to
|
24
|
-
* demand a downgrade to SHARED_COMPRESSOR (yet) until we fix that scenario in
|
24
|
+
* demand a downgrade to SHARED_COMPRESSOR (yet) until we fix that scenario in
|
25
|
+
* pub/sub */
|
25
26
|
// #define UWS_ALLOW_SHARED_AND_DEDICATED_COMPRESSOR_MIX
|
26
27
|
|
27
28
|
/* We forbid negotiating 8 windowBits since Zlib has a bug with this */
|
28
29
|
// #define UWS_ALLOW_8_WINDOW_BITS
|
29
30
|
|
30
|
-
#include <climits>
|
31
31
|
#include <cctype>
|
32
|
+
#include <climits>
|
32
33
|
#include <string>
|
33
34
|
#include <string_view>
|
34
35
|
#include <tuple>
|
@@ -36,221 +37,236 @@
|
|
36
37
|
namespace uWS {
|
37
38
|
|
38
39
|
enum ExtensionTokens {
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
40
|
+
/* Standard permessage-deflate tokens */
|
41
|
+
TOK_PERMESSAGE_DEFLATE = 1838,
|
42
|
+
TOK_SERVER_NO_CONTEXT_TAKEOVER = 2807,
|
43
|
+
TOK_CLIENT_NO_CONTEXT_TAKEOVER = 2783,
|
44
|
+
TOK_SERVER_MAX_WINDOW_BITS = 2372,
|
45
|
+
TOK_CLIENT_MAX_WINDOW_BITS = 2348,
|
46
|
+
/* Non-standard alias for Safari */
|
47
|
+
TOK_X_WEBKIT_DEFLATE_FRAME = 2149,
|
48
|
+
TOK_NO_CONTEXT_TAKEOVER = 2049,
|
49
|
+
TOK_MAX_WINDOW_BITS = 1614
|
49
50
|
|
50
51
|
};
|
51
52
|
|
52
53
|
struct ExtensionsParser {
|
53
54
|
private:
|
54
|
-
|
55
|
+
int *lastInteger = nullptr;
|
55
56
|
|
56
57
|
public:
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
58
|
+
/* Standard */
|
59
|
+
bool perMessageDeflate = false;
|
60
|
+
bool serverNoContextTakeover = false;
|
61
|
+
bool clientNoContextTakeover = false;
|
62
|
+
int serverMaxWindowBits = 0;
|
63
|
+
int clientMaxWindowBits = 0;
|
64
|
+
|
65
|
+
/* Non-standard Safari */
|
66
|
+
bool xWebKitDeflateFrame = false;
|
67
|
+
bool noContextTakeover = false;
|
68
|
+
int maxWindowBits = 0;
|
69
|
+
|
70
|
+
int getToken(const char *&in, const char *stop) {
|
71
|
+
while (in != stop && !isalnum(*in)) {
|
72
|
+
in++;
|
73
|
+
}
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
} else {
|
86
|
-
hashedToken += *in;
|
87
|
-
}
|
88
|
-
in++;
|
75
|
+
/* Don't care more than this for now */
|
76
|
+
static_assert(SHRT_MIN > INT_MIN, "Integer overflow fix is invalid for "
|
77
|
+
"this platform, report this as a bug!");
|
78
|
+
|
79
|
+
int hashedToken = 0;
|
80
|
+
while (in != stop && (isalnum(*in) || *in == '-' || *in == '_')) {
|
81
|
+
if (isdigit(*in)) {
|
82
|
+
/* This check is a quick and incorrect fix for integer overflow
|
83
|
+
* in oss-fuzz but we don't care as it doesn't matter either way */
|
84
|
+
if (hashedToken > SHRT_MIN && hashedToken < SHRT_MAX) {
|
85
|
+
hashedToken = hashedToken * 10 - (*in - '0');
|
89
86
|
}
|
90
|
-
|
87
|
+
} else {
|
88
|
+
hashedToken += *in;
|
89
|
+
}
|
90
|
+
in++;
|
91
91
|
}
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
92
|
+
return hashedToken;
|
93
|
+
}
|
94
|
+
|
95
|
+
ExtensionsParser(const char *data, size_t length) {
|
96
|
+
const char *stop = data + length;
|
97
|
+
int token = 1;
|
98
|
+
|
99
|
+
/* Ignore anything before permessage-deflate or x-webkit-deflate-frame */
|
100
|
+
for (; token && token != TOK_PERMESSAGE_DEFLATE &&
|
101
|
+
token != TOK_X_WEBKIT_DEFLATE_FRAME;
|
102
|
+
token = getToken(data, stop))
|
103
|
+
;
|
104
|
+
|
105
|
+
/* What protocol are we going to use? */
|
106
|
+
perMessageDeflate = (token == TOK_PERMESSAGE_DEFLATE);
|
107
|
+
xWebKitDeflateFrame = (token == TOK_X_WEBKIT_DEFLATE_FRAME);
|
108
|
+
|
109
|
+
while ((token = getToken(data, stop))) {
|
110
|
+
switch (token) {
|
111
|
+
case TOK_X_WEBKIT_DEFLATE_FRAME:
|
112
|
+
/* Duplicates not allowed/supported */
|
113
|
+
return;
|
114
|
+
case TOK_NO_CONTEXT_TAKEOVER:
|
115
|
+
noContextTakeover = true;
|
116
|
+
break;
|
117
|
+
case TOK_MAX_WINDOW_BITS:
|
118
|
+
maxWindowBits = 1;
|
119
|
+
lastInteger = &maxWindowBits;
|
120
|
+
break;
|
121
|
+
case TOK_PERMESSAGE_DEFLATE:
|
122
|
+
/* Duplicates not allowed/supported */
|
123
|
+
return;
|
124
|
+
case TOK_SERVER_NO_CONTEXT_TAKEOVER:
|
125
|
+
serverNoContextTakeover = true;
|
126
|
+
break;
|
127
|
+
case TOK_CLIENT_NO_CONTEXT_TAKEOVER:
|
128
|
+
clientNoContextTakeover = true;
|
129
|
+
break;
|
130
|
+
case TOK_SERVER_MAX_WINDOW_BITS:
|
131
|
+
serverMaxWindowBits = 1;
|
132
|
+
lastInteger = &serverMaxWindowBits;
|
133
|
+
break;
|
134
|
+
case TOK_CLIENT_MAX_WINDOW_BITS:
|
135
|
+
clientMaxWindowBits = 1;
|
136
|
+
lastInteger = &clientMaxWindowBits;
|
137
|
+
break;
|
138
|
+
default:
|
139
|
+
if (token < 0 && lastInteger) {
|
140
|
+
*lastInteger = -token;
|
139
141
|
}
|
142
|
+
break;
|
143
|
+
}
|
140
144
|
}
|
145
|
+
}
|
141
146
|
};
|
142
147
|
|
143
148
|
/* Takes what we (the server) wants, returns what we got */
|
144
|
-
static inline std::tuple<bool, int, int, std::string_view>
|
149
|
+
static inline std::tuple<bool, int, int, std::string_view>
|
150
|
+
negotiateCompression(bool wantCompression, int wantedCompressionWindow,
|
151
|
+
int wantedInflationWindow, std::string_view offer) {
|
145
152
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
153
|
+
/* If we don't want compression then we are done here */
|
154
|
+
if (!wantCompression) {
|
155
|
+
return {false, 0, 0, ""};
|
156
|
+
}
|
150
157
|
|
151
|
-
|
158
|
+
ExtensionsParser ep(offer.data(), offer.length());
|
152
159
|
|
153
|
-
|
154
|
-
|
160
|
+
static thread_local std::string response;
|
161
|
+
response = "";
|
155
162
|
|
156
|
-
|
157
|
-
|
158
|
-
|
163
|
+
int compressionWindow = wantedCompressionWindow;
|
164
|
+
int inflationWindow = wantedInflationWindow;
|
165
|
+
bool compression = false;
|
159
166
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
167
|
+
if (ep.xWebKitDeflateFrame) {
|
168
|
+
/* We now have compression */
|
169
|
+
compression = true;
|
170
|
+
response = "x-webkit-deflate-frame";
|
164
171
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
172
|
+
/* If the other peer has DEMANDED us no sliding window,
|
173
|
+
* we cannot compress with anything other than shared compressor */
|
174
|
+
if (ep.noContextTakeover) {
|
175
|
+
/* We must fail here right now (fix pub/sub) */
|
169
176
|
#ifndef UWS_ALLOW_SHARED_AND_DEDICATED_COMPRESSOR_MIX
|
170
|
-
|
171
|
-
|
172
|
-
|
177
|
+
if (wantedCompressionWindow != 0) {
|
178
|
+
return {false, 0, 0, ""};
|
179
|
+
}
|
173
180
|
#endif
|
174
181
|
|
175
|
-
|
176
|
-
|
182
|
+
compressionWindow = 0;
|
183
|
+
}
|
177
184
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
185
|
+
/* If the other peer has DEMANDED us to use a limited sliding window,
|
186
|
+
* we have to limit out compression sliding window */
|
187
|
+
if (ep.maxWindowBits && ep.maxWindowBits < compressionWindow) {
|
188
|
+
compressionWindow = ep.maxWindowBits;
|
182
189
|
#ifndef UWS_ALLOW_8_WINDOW_BITS
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
190
|
+
/* We cannot really deny this, so we have to disable compression in this
|
191
|
+
* case */
|
192
|
+
if (compressionWindow == 8) {
|
193
|
+
return {false, 0, 0, ""};
|
194
|
+
}
|
187
195
|
#endif
|
188
|
-
|
196
|
+
}
|
189
197
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
198
|
+
/* We decide our own inflation sliding window (and their compression sliding
|
199
|
+
* window) */
|
200
|
+
if (wantedInflationWindow < 15) {
|
201
|
+
if (!wantedInflationWindow) {
|
202
|
+
response += "; no_context_takeover";
|
203
|
+
} else {
|
204
|
+
response +=
|
205
|
+
"; max_window_bits=" + std::to_string(wantedInflationWindow);
|
206
|
+
}
|
207
|
+
}
|
208
|
+
} else if (ep.perMessageDeflate) {
|
209
|
+
/* We now have compression */
|
210
|
+
compression = true;
|
211
|
+
response = "permessage-deflate";
|
212
|
+
|
213
|
+
if (ep.clientNoContextTakeover) {
|
214
|
+
inflationWindow = 0;
|
215
|
+
} else if (ep.clientMaxWindowBits && ep.clientMaxWindowBits != 1) {
|
216
|
+
inflationWindow = std::min<int>(ep.clientMaxWindowBits, inflationWindow);
|
217
|
+
}
|
208
218
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
219
|
+
/* Whatever we have now, write */
|
220
|
+
if (inflationWindow < 15) {
|
221
|
+
if (!inflationWindow || !ep.clientMaxWindowBits) {
|
222
|
+
response += "; client_no_context_takeover";
|
223
|
+
inflationWindow = 0;
|
224
|
+
} else {
|
225
|
+
response +=
|
226
|
+
"; client_max_window_bits=" + std::to_string(inflationWindow);
|
227
|
+
}
|
228
|
+
}
|
218
229
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
230
|
+
/* This block basically lets the client lower it */
|
231
|
+
if (ep.serverNoContextTakeover) {
|
232
|
+
/* This is an important (temporary) fix since we haven't allowed
|
233
|
+
* these two modes to mix, and pub/sub will not handle this case (yet) */
|
223
234
|
#ifdef UWS_ALLOW_SHARED_AND_DEDICATED_COMPRESSOR_MIX
|
224
|
-
|
235
|
+
compressionWindow = 0;
|
225
236
|
#endif
|
226
|
-
|
227
|
-
|
237
|
+
} else if (ep.serverMaxWindowBits) {
|
238
|
+
compressionWindow =
|
239
|
+
std::min<int>(ep.serverMaxWindowBits, compressionWindow);
|
228
240
|
#ifndef UWS_ALLOW_8_WINDOW_BITS
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
241
|
+
/* Zlib cannot do windowBits=8, memLevel=1 so we raise it up to 9 minimum
|
242
|
+
*/
|
243
|
+
if (compressionWindow == 8) {
|
244
|
+
compressionWindow = 9;
|
245
|
+
}
|
233
246
|
#endif
|
234
|
-
}
|
235
|
-
|
236
|
-
/* Whatever we have now, write */
|
237
|
-
if (compressionWindow < 15) {
|
238
|
-
if (!compressionWindow) {
|
239
|
-
response += "; server_no_context_takeover";
|
240
|
-
} else {
|
241
|
-
response += "; server_max_window_bits=" + std::to_string(compressionWindow);
|
242
|
-
}
|
243
|
-
}
|
244
247
|
}
|
245
248
|
|
246
|
-
/*
|
247
|
-
if (
|
248
|
-
|
249
|
+
/* Whatever we have now, write */
|
250
|
+
if (compressionWindow < 15) {
|
251
|
+
if (!compressionWindow) {
|
252
|
+
response += "; server_no_context_takeover";
|
253
|
+
} else {
|
254
|
+
response +=
|
255
|
+
"; server_max_window_bits=" + std::to_string(compressionWindow);
|
256
|
+
}
|
249
257
|
}
|
258
|
+
}
|
250
259
|
|
251
|
-
|
252
|
-
|
260
|
+
/* A final sanity check (this check does not actually catch too high values!)
|
261
|
+
*/
|
262
|
+
if ((compressionWindow && compressionWindow < 8) || compressionWindow > 15 ||
|
263
|
+
(inflationWindow && inflationWindow < 8) || inflationWindow > 15) {
|
264
|
+
return {false, 0, 0, ""};
|
265
|
+
}
|
253
266
|
|
267
|
+
return {compression, compressionWindow, inflationWindow, response};
|
254
268
|
}
|
255
269
|
|
270
|
+
} // namespace uWS
|
271
|
+
|
256
272
|
#endif // UWS_WEBSOCKETEXTENSIONS_H
|