opal-up 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +209 -0
  3. data/README.md +97 -29
  4. data/bin/up_ruby +4 -0
  5. data/bin/up_ruby_cluster +4 -0
  6. data/ext/up_ext/App.h +606 -0
  7. data/ext/up_ext/AsyncSocket.h +355 -0
  8. data/ext/up_ext/AsyncSocketData.h +87 -0
  9. data/ext/up_ext/BloomFilter.h +83 -0
  10. data/ext/up_ext/ChunkedEncoding.h +236 -0
  11. data/ext/up_ext/ClientApp.h +36 -0
  12. data/ext/up_ext/HttpContext.h +502 -0
  13. data/ext/up_ext/HttpContextData.h +56 -0
  14. data/ext/up_ext/HttpErrors.h +53 -0
  15. data/ext/up_ext/HttpParser.h +680 -0
  16. data/ext/up_ext/HttpResponse.h +578 -0
  17. data/ext/up_ext/HttpResponseData.h +95 -0
  18. data/ext/up_ext/HttpRouter.h +380 -0
  19. data/ext/up_ext/Loop.h +204 -0
  20. data/ext/up_ext/LoopData.h +112 -0
  21. data/ext/up_ext/MoveOnlyFunction.h +377 -0
  22. data/ext/up_ext/PerMessageDeflate.h +315 -0
  23. data/ext/up_ext/ProxyParser.h +163 -0
  24. data/ext/up_ext/QueryParser.h +120 -0
  25. data/ext/up_ext/TopicTree.h +363 -0
  26. data/ext/up_ext/Utilities.h +66 -0
  27. data/ext/up_ext/WebSocket.h +381 -0
  28. data/ext/up_ext/WebSocketContext.h +434 -0
  29. data/ext/up_ext/WebSocketContextData.h +109 -0
  30. data/ext/up_ext/WebSocketData.h +86 -0
  31. data/ext/up_ext/WebSocketExtensions.h +256 -0
  32. data/ext/up_ext/WebSocketHandshake.h +145 -0
  33. data/ext/up_ext/WebSocketProtocol.h +506 -0
  34. data/ext/up_ext/bsd.c +767 -0
  35. data/ext/up_ext/bsd.h +109 -0
  36. data/ext/up_ext/context.c +524 -0
  37. data/ext/up_ext/epoll_kqueue.c +458 -0
  38. data/ext/up_ext/epoll_kqueue.h +67 -0
  39. data/ext/up_ext/extconf.rb +5 -0
  40. data/ext/up_ext/internal.h +224 -0
  41. data/ext/up_ext/libusockets.h +350 -0
  42. data/ext/up_ext/libuwebsockets.cpp +1344 -0
  43. data/ext/up_ext/libuwebsockets.h +396 -0
  44. data/ext/up_ext/loop.c +386 -0
  45. data/ext/up_ext/loop_data.h +38 -0
  46. data/ext/up_ext/socket.c +231 -0
  47. data/ext/up_ext/up_ext.c +930 -0
  48. data/lib/up/bun/rack_env.rb +1 -13
  49. data/lib/up/bun/server.rb +93 -19
  50. data/lib/up/cli.rb +3 -0
  51. data/lib/up/client.rb +68 -0
  52. data/lib/up/ruby/cluster.rb +39 -0
  53. data/lib/up/ruby/cluster_cli.rb +10 -0
  54. data/lib/up/{node → ruby}/rack_cluster.rb +5 -4
  55. data/lib/up/{node → ruby}/rack_server.rb +4 -4
  56. data/lib/up/ruby/server_cli.rb +10 -0
  57. data/lib/up/u_web_socket/cluster.rb +18 -3
  58. data/lib/up/u_web_socket/server.rb +108 -15
  59. data/lib/up/version.rb +1 -1
  60. metadata +72 -30
  61. data/.gitignore +0 -5
  62. data/Gemfile +0 -2
  63. data/bin/up_node +0 -12
  64. data/bin/up_node_cluster +0 -12
  65. data/example_rack_app/Gemfile +0 -3
  66. data/example_rack_app/config.ru +0 -6
  67. data/example_rack_app/rack_app.rb +0 -5
  68. data/example_roda_app/Gemfile +0 -6
  69. data/example_roda_app/config.ru +0 -6
  70. data/example_roda_app/roda_app.rb +0 -37
  71. data/example_sinatra_app/Gemfile +0 -6
  72. data/example_sinatra_app/config.ru +0 -6
  73. data/example_sinatra_app/sinatra_app.rb +0 -7
  74. data/lib/up/node/cluster.rb +0 -39
  75. data/lib/up/node/cluster_cli.rb +0 -15
  76. data/lib/up/node/rack_env.rb +0 -106
  77. data/lib/up/node/server.rb +0 -84
  78. data/lib/up/node/server_cli.rb +0 -15
  79. data/lib/up/u_web_socket/rack_env.rb +0 -101
  80. data/opal-up.gemspec +0 -27
  81. data/up_logo.svg +0 -256
@@ -0,0 +1,256 @@
1
+ /*
2
+ * Authored by Alex Hultman, 2018-2021.
3
+ * Intellectual property of third-party.
4
+
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #ifndef UWS_WEBSOCKETEXTENSIONS_H
19
+ #define UWS_WEBSOCKETEXTENSIONS_H
20
+
21
+ /* There is a new, huge bug scenario that needs to be fixed:
22
+ * pub/sub does not support being in DEDICATED_COMPRESSOR-mode while having
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 pub/sub */
25
+ // #define UWS_ALLOW_SHARED_AND_DEDICATED_COMPRESSOR_MIX
26
+
27
+ /* We forbid negotiating 8 windowBits since Zlib has a bug with this */
28
+ // #define UWS_ALLOW_8_WINDOW_BITS
29
+
30
+ #include <climits>
31
+ #include <cctype>
32
+ #include <string>
33
+ #include <string_view>
34
+ #include <tuple>
35
+
36
+ namespace uWS {
37
+
38
+ enum ExtensionTokens {
39
+ /* Standard permessage-deflate tokens */
40
+ TOK_PERMESSAGE_DEFLATE = 1838,
41
+ TOK_SERVER_NO_CONTEXT_TAKEOVER = 2807,
42
+ TOK_CLIENT_NO_CONTEXT_TAKEOVER = 2783,
43
+ TOK_SERVER_MAX_WINDOW_BITS = 2372,
44
+ TOK_CLIENT_MAX_WINDOW_BITS = 2348,
45
+ /* Non-standard alias for Safari */
46
+ TOK_X_WEBKIT_DEFLATE_FRAME = 2149,
47
+ TOK_NO_CONTEXT_TAKEOVER = 2049,
48
+ TOK_MAX_WINDOW_BITS = 1614
49
+
50
+ };
51
+
52
+ struct ExtensionsParser {
53
+ private:
54
+ int *lastInteger = nullptr;
55
+
56
+ public:
57
+ /* Standard */
58
+ bool perMessageDeflate = false;
59
+ bool serverNoContextTakeover = false;
60
+ bool clientNoContextTakeover = false;
61
+ int serverMaxWindowBits = 0;
62
+ int clientMaxWindowBits = 0;
63
+
64
+ /* Non-standard Safari */
65
+ bool xWebKitDeflateFrame = false;
66
+ bool noContextTakeover = false;
67
+ int maxWindowBits = 0;
68
+
69
+ int getToken(const char *&in, const char *stop) {
70
+ while (in != stop && !isalnum(*in)) {
71
+ in++;
72
+ }
73
+
74
+ /* Don't care more than this for now */
75
+ static_assert(SHRT_MIN > INT_MIN, "Integer overflow fix is invalid for this platform, report this as a bug!");
76
+
77
+ int hashedToken = 0;
78
+ while (in != stop && (isalnum(*in) || *in == '-' || *in == '_')) {
79
+ if (isdigit(*in)) {
80
+ /* This check is a quick and incorrect fix for integer overflow
81
+ * in oss-fuzz but we don't care as it doesn't matter either way */
82
+ if (hashedToken > SHRT_MIN && hashedToken < SHRT_MAX) {
83
+ hashedToken = hashedToken * 10 - (*in - '0');
84
+ }
85
+ } else {
86
+ hashedToken += *in;
87
+ }
88
+ in++;
89
+ }
90
+ return hashedToken;
91
+ }
92
+
93
+ ExtensionsParser(const char *data, size_t length) {
94
+ const char *stop = data + length;
95
+ int token = 1;
96
+
97
+ /* Ignore anything before permessage-deflate or x-webkit-deflate-frame */
98
+ for (; token && token != TOK_PERMESSAGE_DEFLATE && token != TOK_X_WEBKIT_DEFLATE_FRAME; token = getToken(data, stop));
99
+
100
+ /* What protocol are we going to use? */
101
+ perMessageDeflate = (token == TOK_PERMESSAGE_DEFLATE);
102
+ xWebKitDeflateFrame = (token == TOK_X_WEBKIT_DEFLATE_FRAME);
103
+
104
+ while ((token = getToken(data, stop))) {
105
+ switch (token) {
106
+ case TOK_X_WEBKIT_DEFLATE_FRAME:
107
+ /* Duplicates not allowed/supported */
108
+ return;
109
+ case TOK_NO_CONTEXT_TAKEOVER:
110
+ noContextTakeover = true;
111
+ break;
112
+ case TOK_MAX_WINDOW_BITS:
113
+ maxWindowBits = 1;
114
+ lastInteger = &maxWindowBits;
115
+ break;
116
+ case TOK_PERMESSAGE_DEFLATE:
117
+ /* Duplicates not allowed/supported */
118
+ return;
119
+ case TOK_SERVER_NO_CONTEXT_TAKEOVER:
120
+ serverNoContextTakeover = true;
121
+ break;
122
+ case TOK_CLIENT_NO_CONTEXT_TAKEOVER:
123
+ clientNoContextTakeover = true;
124
+ break;
125
+ case TOK_SERVER_MAX_WINDOW_BITS:
126
+ serverMaxWindowBits = 1;
127
+ lastInteger = &serverMaxWindowBits;
128
+ break;
129
+ case TOK_CLIENT_MAX_WINDOW_BITS:
130
+ clientMaxWindowBits = 1;
131
+ lastInteger = &clientMaxWindowBits;
132
+ break;
133
+ default:
134
+ if (token < 0 && lastInteger) {
135
+ *lastInteger = -token;
136
+ }
137
+ break;
138
+ }
139
+ }
140
+ }
141
+ };
142
+
143
+ /* Takes what we (the server) wants, returns what we got */
144
+ static inline std::tuple<bool, int, int, std::string_view> negotiateCompression(bool wantCompression, int wantedCompressionWindow, int wantedInflationWindow, std::string_view offer) {
145
+
146
+ /* If we don't want compression then we are done here */
147
+ if (!wantCompression) {
148
+ return {false, 0, 0, ""};
149
+ }
150
+
151
+ ExtensionsParser ep(offer.data(), offer.length());
152
+
153
+ static thread_local std::string response;
154
+ response = "";
155
+
156
+ int compressionWindow = wantedCompressionWindow;
157
+ int inflationWindow = wantedInflationWindow;
158
+ bool compression = false;
159
+
160
+ if (ep.xWebKitDeflateFrame) {
161
+ /* We now have compression */
162
+ compression = true;
163
+ response = "x-webkit-deflate-frame";
164
+
165
+ /* If the other peer has DEMANDED us no sliding window,
166
+ * we cannot compress with anything other than shared compressor */
167
+ if (ep.noContextTakeover) {
168
+ /* We must fail here right now (fix pub/sub) */
169
+ #ifndef UWS_ALLOW_SHARED_AND_DEDICATED_COMPRESSOR_MIX
170
+ if (wantedCompressionWindow != 0) {
171
+ return {false, 0, 0, ""};
172
+ }
173
+ #endif
174
+
175
+ compressionWindow = 0;
176
+ }
177
+
178
+ /* If the other peer has DEMANDED us to use a limited sliding window,
179
+ * we have to limit out compression sliding window */
180
+ if (ep.maxWindowBits && ep.maxWindowBits < compressionWindow) {
181
+ compressionWindow = ep.maxWindowBits;
182
+ #ifndef UWS_ALLOW_8_WINDOW_BITS
183
+ /* We cannot really deny this, so we have to disable compression in this case */
184
+ if (compressionWindow == 8) {
185
+ return {false, 0, 0, ""};
186
+ }
187
+ #endif
188
+ }
189
+
190
+ /* We decide our own inflation sliding window (and their compression sliding window) */
191
+ if (wantedInflationWindow < 15) {
192
+ if (!wantedInflationWindow) {
193
+ response += "; no_context_takeover";
194
+ } else {
195
+ response += "; max_window_bits=" + std::to_string(wantedInflationWindow);
196
+ }
197
+ }
198
+ } else if (ep.perMessageDeflate) {
199
+ /* We now have compression */
200
+ compression = true;
201
+ response = "permessage-deflate";
202
+
203
+ if (ep.clientNoContextTakeover) {
204
+ inflationWindow = 0;
205
+ } else if (ep.clientMaxWindowBits && ep.clientMaxWindowBits != 1) {
206
+ inflationWindow = std::min<int>(ep.clientMaxWindowBits, inflationWindow);
207
+ }
208
+
209
+ /* Whatever we have now, write */
210
+ if (inflationWindow < 15) {
211
+ if (!inflationWindow || !ep.clientMaxWindowBits) {
212
+ response += "; client_no_context_takeover";
213
+ inflationWindow = 0;
214
+ } else {
215
+ response += "; client_max_window_bits=" + std::to_string(inflationWindow);
216
+ }
217
+ }
218
+
219
+ /* This block basically lets the client lower it */
220
+ if (ep.serverNoContextTakeover) {
221
+ /* This is an important (temporary) fix since we haven't allowed
222
+ * these two modes to mix, and pub/sub will not handle this case (yet) */
223
+ #ifdef UWS_ALLOW_SHARED_AND_DEDICATED_COMPRESSOR_MIX
224
+ compressionWindow = 0;
225
+ #endif
226
+ } else if (ep.serverMaxWindowBits) {
227
+ compressionWindow = std::min<int>(ep.serverMaxWindowBits, compressionWindow);
228
+ #ifndef UWS_ALLOW_8_WINDOW_BITS
229
+ /* Zlib cannot do windowBits=8, memLevel=1 so we raise it up to 9 minimum */
230
+ if (compressionWindow == 8) {
231
+ compressionWindow = 9;
232
+ }
233
+ #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
+ }
245
+
246
+ /* A final sanity check (this check does not actually catch too high values!) */
247
+ if ((compressionWindow && compressionWindow < 8) || compressionWindow > 15 || (inflationWindow && inflationWindow < 8) || inflationWindow > 15) {
248
+ return {false, 0, 0, ""};
249
+ }
250
+
251
+ return {compression, compressionWindow, inflationWindow, response};
252
+ }
253
+
254
+ }
255
+
256
+ #endif // UWS_WEBSOCKETEXTENSIONS_H
@@ -0,0 +1,145 @@
1
+ /*
2
+ * Authored by Alex Hultman, 2018-2020.
3
+ * Intellectual property of third-party.
4
+
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #ifndef UWS_WEBSOCKETHANDSHAKE_H
19
+ #define UWS_WEBSOCKETHANDSHAKE_H
20
+
21
+ #include <cstdint>
22
+ #include <cstddef>
23
+
24
+ namespace uWS {
25
+
26
+ struct WebSocketHandshake {
27
+ template <int N, typename T>
28
+ struct static_for {
29
+ void operator()(uint32_t *a, uint32_t *b) {
30
+ static_for<N - 1, T>()(a, b);
31
+ T::template f<N - 1>(a, b);
32
+ }
33
+ };
34
+
35
+ template <typename T>
36
+ struct static_for<0, T> {
37
+ void operator()(uint32_t */*a*/, uint32_t */*hash*/) {}
38
+ };
39
+
40
+ static inline uint32_t rol(uint32_t value, size_t bits) {return (value << bits) | (value >> (32 - bits));}
41
+ static inline uint32_t blk(uint32_t b[16], size_t i) {
42
+ return rol(b[(i + 13) & 15] ^ b[(i + 8) & 15] ^ b[(i + 2) & 15] ^ b[i], 1);
43
+ }
44
+
45
+ struct Sha1Loop1 {
46
+ template <int i>
47
+ static inline void f(uint32_t *a, uint32_t *b) {
48
+ a[i % 5] += ((a[(3 + i) % 5] & (a[(2 + i) % 5] ^ a[(1 + i) % 5])) ^ a[(1 + i) % 5]) + b[i] + 0x5a827999 + rol(a[(4 + i) % 5], 5);
49
+ a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
50
+ }
51
+ };
52
+ struct Sha1Loop2 {
53
+ template <int i>
54
+ static inline void f(uint32_t *a, uint32_t *b) {
55
+ b[i] = blk(b, i);
56
+ a[(1 + i) % 5] += ((a[(4 + i) % 5] & (a[(3 + i) % 5] ^ a[(2 + i) % 5])) ^ a[(2 + i) % 5]) + b[i] + 0x5a827999 + rol(a[(5 + i) % 5], 5);
57
+ a[(4 + i) % 5] = rol(a[(4 + i) % 5], 30);
58
+ }
59
+ };
60
+ struct Sha1Loop3 {
61
+ template <int i>
62
+ static inline void f(uint32_t *a, uint32_t *b) {
63
+ b[(i + 4) % 16] = blk(b, (i + 4) % 16);
64
+ a[i % 5] += (a[(3 + i) % 5] ^ a[(2 + i) % 5] ^ a[(1 + i) % 5]) + b[(i + 4) % 16] + 0x6ed9eba1 + rol(a[(4 + i) % 5], 5);
65
+ a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
66
+ }
67
+ };
68
+ struct Sha1Loop4 {
69
+ template <int i>
70
+ static inline void f(uint32_t *a, uint32_t *b) {
71
+ b[(i + 8) % 16] = blk(b, (i + 8) % 16);
72
+ a[i % 5] += (((a[(3 + i) % 5] | a[(2 + i) % 5]) & a[(1 + i) % 5]) | (a[(3 + i) % 5] & a[(2 + i) % 5])) + b[(i + 8) % 16] + 0x8f1bbcdc + rol(a[(4 + i) % 5], 5);
73
+ a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
74
+ }
75
+ };
76
+ struct Sha1Loop5 {
77
+ template <int i>
78
+ static inline void f(uint32_t *a, uint32_t *b) {
79
+ b[(i + 12) % 16] = blk(b, (i + 12) % 16);
80
+ a[i % 5] += (a[(3 + i) % 5] ^ a[(2 + i) % 5] ^ a[(1 + i) % 5]) + b[(i + 12) % 16] + 0xca62c1d6 + rol(a[(4 + i) % 5], 5);
81
+ a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
82
+ }
83
+ };
84
+ struct Sha1Loop6 {
85
+ template <int i>
86
+ static inline void f(uint32_t *a, uint32_t *b) {
87
+ b[i] += a[4 - i];
88
+ }
89
+ };
90
+
91
+ static inline void sha1(uint32_t hash[5], uint32_t b[16]) {
92
+ uint32_t a[5] = {hash[4], hash[3], hash[2], hash[1], hash[0]};
93
+ static_for<16, Sha1Loop1>()(a, b);
94
+ static_for<4, Sha1Loop2>()(a, b);
95
+ static_for<20, Sha1Loop3>()(a, b);
96
+ static_for<20, Sha1Loop4>()(a, b);
97
+ static_for<20, Sha1Loop5>()(a, b);
98
+ static_for<5, Sha1Loop6>()(a, hash);
99
+ }
100
+
101
+ static inline void base64(unsigned char *src, char *dst) {
102
+ const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
103
+ for (int i = 0; i < 18; i += 3) {
104
+ *dst++ = b64[(src[i] >> 2) & 63];
105
+ *dst++ = b64[((src[i] & 3) << 4) | ((src[i + 1] & 240) >> 4)];
106
+ *dst++ = b64[((src[i + 1] & 15) << 2) | ((src[i + 2] & 192) >> 6)];
107
+ *dst++ = b64[src[i + 2] & 63];
108
+ }
109
+ *dst++ = b64[(src[18] >> 2) & 63];
110
+ *dst++ = b64[((src[18] & 3) << 4) | ((src[19] & 240) >> 4)];
111
+ *dst++ = b64[((src[19] & 15) << 2)];
112
+ *dst++ = '=';
113
+ }
114
+
115
+ public:
116
+ static inline void generate(const char input[24], char output[28]) {
117
+ uint32_t b_output[5] = {
118
+ 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
119
+ };
120
+ uint32_t b_input[16] = {
121
+ 0, 0, 0, 0, 0, 0, 0x32353845, 0x41464135, 0x2d453931, 0x342d3437, 0x44412d39,
122
+ 0x3543412d, 0x43354142, 0x30444338, 0x35423131, 0x80000000
123
+ };
124
+
125
+ for (int i = 0; i < 6; i++) {
126
+ b_input[i] = (uint32_t) ((input[4 * i + 3] & 0xff) | (input[4 * i + 2] & 0xff) << 8 | (input[4 * i + 1] & 0xff) << 16 | (input[4 * i + 0] & 0xff) << 24);
127
+ }
128
+ sha1(b_output, b_input);
129
+ uint32_t last_b[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 480};
130
+ sha1(b_output, last_b);
131
+ for (int i = 0; i < 5; i++) {
132
+ uint32_t tmp = b_output[i];
133
+ char *bytes = (char *) &b_output[i];
134
+ bytes[3] = (char) (tmp & 0xff);
135
+ bytes[2] = (char) ((tmp >> 8) & 0xff);
136
+ bytes[1] = (char) ((tmp >> 16) & 0xff);
137
+ bytes[0] = (char) ((tmp >> 24) & 0xff);
138
+ }
139
+ base64((unsigned char *) b_output, output);
140
+ }
141
+ };
142
+
143
+ }
144
+
145
+ #endif // UWS_WEBSOCKETHANDSHAKE_H