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,1344 @@
1
+ /*
2
+ * Copyright 2022 Ciro Spaciari
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ #include "libuwebsockets.h"
24
+ #include "App.h"
25
+ #include "ClientApp.h"
26
+ #include <optional>
27
+ #include <string_view>
28
+ extern "C" {
29
+
30
+ uws_app_t *uws_create_app(int ssl, struct us_socket_context_options_t options) {
31
+ if (ssl) {
32
+ uWS::SocketContextOptions sco;
33
+ sco.ca_file_name = options.ca_file_name;
34
+ sco.cert_file_name = options.cert_file_name;
35
+ sco.dh_params_file_name = options.dh_params_file_name;
36
+ sco.key_file_name = options.key_file_name;
37
+ sco.passphrase = options.passphrase;
38
+ sco.ssl_prefer_low_memory_usage = options.ssl_prefer_low_memory_usage;
39
+ sco.ssl_ciphers = options.ssl_ciphers;
40
+
41
+ return (uws_app_t *)new uWS::SSLApp(sco);
42
+ }
43
+
44
+ return (uws_app_t *)new uWS::App();
45
+ }
46
+
47
+ void uws_app_get(int ssl, uws_app_t *app, const char *pattern,
48
+ uws_method_handler handler, void *user_data) {
49
+ if (ssl) {
50
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
51
+ if (handler == nullptr) {
52
+ uwsApp->get(pattern, nullptr);
53
+ return;
54
+ }
55
+ uwsApp->get(pattern, [handler, user_data](auto *res, auto *req) {
56
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
57
+ });
58
+ } else {
59
+ uWS::App *uwsApp = (uWS::App *)app;
60
+ if (handler == nullptr) {
61
+ uwsApp->get(pattern, nullptr);
62
+ return;
63
+ }
64
+ uwsApp->get(pattern, [handler, user_data](auto *res, auto *req) {
65
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
66
+ });
67
+ }
68
+ }
69
+ void uws_app_post(int ssl, uws_app_t *app, const char *pattern,
70
+ uws_method_handler handler, void *user_data) {
71
+
72
+ if (ssl) {
73
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
74
+ if (handler == nullptr) {
75
+ uwsApp->post(pattern, nullptr);
76
+ return;
77
+ }
78
+ uwsApp->post(pattern, [handler, user_data](auto *res, auto *req) {
79
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
80
+ });
81
+ } else {
82
+ uWS::App *uwsApp = (uWS::App *)app;
83
+ if (handler == nullptr) {
84
+ uwsApp->post(pattern, nullptr);
85
+ return;
86
+ }
87
+ uwsApp->post(pattern, [handler, user_data](auto *res, auto *req) {
88
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
89
+ });
90
+ }
91
+ }
92
+ void uws_app_options(int ssl, uws_app_t *app, const char *pattern,
93
+ uws_method_handler handler, void *user_data) {
94
+ if (ssl) {
95
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
96
+ if (handler == nullptr) {
97
+ uwsApp->options(pattern, nullptr);
98
+ return;
99
+ }
100
+ uwsApp->options(pattern, [handler, user_data](auto *res, auto *req) {
101
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
102
+ });
103
+ } else {
104
+ uWS::App *uwsApp = (uWS::App *)app;
105
+ if (handler == nullptr) {
106
+ uwsApp->options(pattern, nullptr);
107
+ return;
108
+ }
109
+ uwsApp->options(pattern, [handler, user_data](auto *res, auto *req) {
110
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
111
+ });
112
+ }
113
+ }
114
+ void uws_app_delete(int ssl, uws_app_t *app, const char *pattern,
115
+ uws_method_handler handler, void *user_data) {
116
+ if (ssl) {
117
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
118
+ if (handler == nullptr) {
119
+ uwsApp->del(pattern, nullptr);
120
+ return;
121
+ }
122
+ uwsApp->del(pattern, [handler, user_data](auto *res, auto *req) {
123
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
124
+ });
125
+ } else {
126
+ uWS::App *uwsApp = (uWS::App *)app;
127
+ if (handler == nullptr) {
128
+ uwsApp->del(pattern, nullptr);
129
+ return;
130
+ }
131
+ uwsApp->del(pattern, [handler, user_data](auto *res, auto *req) {
132
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
133
+ });
134
+ }
135
+ }
136
+ void uws_app_patch(int ssl, uws_app_t *app, const char *pattern,
137
+ uws_method_handler handler, void *user_data) {
138
+ if (ssl) {
139
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
140
+ if (handler == nullptr) {
141
+ uwsApp->patch(pattern, nullptr);
142
+ return;
143
+ }
144
+ uwsApp->patch(pattern, [handler, user_data](auto *res, auto *req) {
145
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
146
+ });
147
+ } else {
148
+ uWS::App *uwsApp = (uWS::App *)app;
149
+ if (handler == nullptr) {
150
+ uwsApp->patch(pattern, nullptr);
151
+ return;
152
+ }
153
+ uwsApp->patch(pattern, [handler, user_data](auto *res, auto *req) {
154
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
155
+ });
156
+ }
157
+ }
158
+ void uws_app_put(int ssl, uws_app_t *app, const char *pattern,
159
+ uws_method_handler handler, void *user_data) {
160
+ if (ssl) {
161
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
162
+ if (handler == nullptr) {
163
+ uwsApp->put(pattern, nullptr);
164
+ return;
165
+ }
166
+ uwsApp->put(pattern, [handler, user_data](auto *res, auto *req) {
167
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
168
+ });
169
+ } else {
170
+ uWS::App *uwsApp = (uWS::App *)app;
171
+ if (handler == nullptr) {
172
+ uwsApp->put(pattern, nullptr);
173
+ return;
174
+ }
175
+ uwsApp->put(pattern, [handler, user_data](auto *res, auto *req) {
176
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
177
+ });
178
+ }
179
+ }
180
+ void uws_app_head(int ssl, uws_app_t *app, const char *pattern,
181
+ uws_method_handler handler, void *user_data) {
182
+ if (ssl) {
183
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
184
+ if (handler == nullptr) {
185
+ uwsApp->head(pattern, nullptr);
186
+ return;
187
+ }
188
+ uwsApp->head(pattern, [handler, user_data](auto *res, auto *req) {
189
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
190
+ });
191
+ } else {
192
+ uWS::App *uwsApp = (uWS::App *)app;
193
+ if (handler == nullptr) {
194
+ uwsApp->head(pattern, nullptr);
195
+ return;
196
+ }
197
+ uwsApp->head(pattern, [handler, user_data](auto *res, auto *req) {
198
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
199
+ });
200
+ }
201
+ }
202
+ void uws_app_connect(int ssl, uws_app_t *app, const char *pattern,
203
+ uws_method_handler handler, void *user_data) {
204
+ if (ssl) {
205
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
206
+ if (handler == nullptr) {
207
+ uwsApp->connect(pattern, nullptr);
208
+ return;
209
+ }
210
+ uwsApp->connect(pattern, [handler, user_data](auto *res, auto *req) {
211
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
212
+ });
213
+ } else {
214
+ uWS::App *uwsApp = (uWS::App *)app;
215
+ if (handler == nullptr) {
216
+ uwsApp->connect(pattern, nullptr);
217
+ return;
218
+ }
219
+ uwsApp->connect(pattern, [handler, user_data](auto *res, auto *req) {
220
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
221
+ });
222
+ }
223
+ }
224
+
225
+ void uws_app_trace(int ssl, uws_app_t *app, const char *pattern,
226
+ uws_method_handler handler, void *user_data) {
227
+ if (ssl) {
228
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
229
+ if (handler == nullptr) {
230
+ uwsApp->trace(pattern, nullptr);
231
+ return;
232
+ }
233
+ uwsApp->trace(pattern, [handler, user_data](auto *res, auto *req) {
234
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
235
+ });
236
+ } else {
237
+ uWS::App *uwsApp = (uWS::App *)app;
238
+ if (handler == nullptr) {
239
+ uwsApp->trace(pattern, nullptr);
240
+ return;
241
+ }
242
+ uwsApp->trace(pattern, [handler, user_data](auto *res, auto *req) {
243
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
244
+ });
245
+ }
246
+ }
247
+ void uws_app_any(int ssl, uws_app_t *app, const char *pattern,
248
+ uws_method_handler handler, void *user_data) {
249
+ if (ssl) {
250
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
251
+ if (handler == nullptr) {
252
+ uwsApp->any(pattern, nullptr);
253
+ return;
254
+ }
255
+ uwsApp->any(pattern, [handler, user_data](auto *res, auto *req) {
256
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
257
+ });
258
+ } else {
259
+ uWS::App *uwsApp = (uWS::App *)app;
260
+ if (handler == nullptr) {
261
+ uwsApp->any(pattern, nullptr);
262
+ return;
263
+ }
264
+ uwsApp->any(pattern, [handler, user_data](auto *res, auto *req) {
265
+ handler((uws_res_t *)res, (uws_req_t *)req, user_data);
266
+ });
267
+ }
268
+ }
269
+
270
+ void uws_app_run(int ssl, uws_app_t *app) {
271
+ if (ssl) {
272
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
273
+ uwsApp->run();
274
+ } else {
275
+ uWS::App *uwsApp = (uWS::App *)app;
276
+ uwsApp->run();
277
+ }
278
+ }
279
+
280
+ void uws_app_listen(int ssl, uws_app_t *app, int port,
281
+ uws_listen_handler handler, void *user_data) {
282
+ uws_app_listen_config_t config;
283
+ config.port = port;
284
+ config.host = nullptr;
285
+ config.options = 0;
286
+
287
+ if (ssl) {
288
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
289
+ uwsApp->listen(port, [handler, config,
290
+ user_data](struct us_listen_socket_t *listen_socket) {
291
+ handler((struct us_listen_socket_t *)listen_socket, config, user_data);
292
+ });
293
+ } else {
294
+ uWS::App *uwsApp = (uWS::App *)app;
295
+
296
+ uwsApp->listen(port, [handler, config,
297
+ user_data](struct us_listen_socket_t *listen_socket) {
298
+ handler((struct us_listen_socket_t *)listen_socket, config, user_data);
299
+ });
300
+ }
301
+ }
302
+
303
+ void uws_app_listen_with_config(int ssl, uws_app_t *app,
304
+ uws_app_listen_config_t config,
305
+ uws_listen_handler handler, void *user_data) {
306
+ if (ssl) {
307
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
308
+ uwsApp->listen(
309
+ config.host, config.port, config.options,
310
+ [handler, config, user_data](struct us_listen_socket_t *listen_socket) {
311
+ handler((struct us_listen_socket_t *)listen_socket, config,
312
+ user_data);
313
+ });
314
+ } else {
315
+ uWS::App *uwsApp = (uWS::App *)app;
316
+ uwsApp->listen(
317
+ config.host, config.port, config.options,
318
+ [handler, config, user_data](struct us_listen_socket_t *listen_socket) {
319
+ handler((struct us_listen_socket_t *)listen_socket, config,
320
+ user_data);
321
+ });
322
+ }
323
+ }
324
+
325
+ /* callback, path to unix domain socket */
326
+ void uws_app_listen_domain(int ssl, uws_app_t *app, const char *domain,
327
+ size_t domain_length,
328
+ uws_listen_domain_handler handler, void *user_data) {
329
+
330
+ if (ssl) {
331
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
332
+ uwsApp->listen(
333
+ [handler, domain, domain_length,
334
+ user_data](struct us_listen_socket_t *listen_socket) {
335
+ handler((struct us_listen_socket_t *)listen_socket, domain,
336
+ domain_length, 0, user_data);
337
+ },
338
+ std::string(domain, domain_length));
339
+ } else {
340
+ uWS::App *uwsApp = (uWS::App *)app;
341
+
342
+ uwsApp->listen(
343
+ [handler, domain, domain_length,
344
+ user_data](struct us_listen_socket_t *listen_socket) {
345
+ handler((struct us_listen_socket_t *)listen_socket, domain,
346
+ domain_length, 0, user_data);
347
+ },
348
+ std::string(domain, domain_length));
349
+ }
350
+ }
351
+
352
+ /* callback, path to unix domain socket */
353
+ void uws_app_listen_domain_with_options(int ssl, uws_app_t *app,
354
+ const char *domain,
355
+ size_t domain_length, int options,
356
+ uws_listen_domain_handler handler,
357
+ void *user_data) {
358
+
359
+ if (ssl) {
360
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
361
+ uwsApp->listen(
362
+ options,
363
+ [handler, domain, domain_length, options,
364
+ user_data](struct us_listen_socket_t *listen_socket) {
365
+ handler((struct us_listen_socket_t *)listen_socket, domain,
366
+ domain_length, options, user_data);
367
+ },
368
+ std::string(domain, domain_length));
369
+ } else {
370
+ uWS::App *uwsApp = (uWS::App *)app;
371
+
372
+ uwsApp->listen(
373
+ options,
374
+ [handler, domain, domain_length, options,
375
+ user_data](struct us_listen_socket_t *listen_socket) {
376
+ handler((struct us_listen_socket_t *)listen_socket, domain,
377
+ domain_length, options, user_data);
378
+ },
379
+ std::string(domain, domain_length));
380
+ }
381
+ }
382
+ void uws_app_domain(int ssl, uws_app_t *app, const char *server_name,
383
+ size_t server_name_length) {
384
+ if (ssl) {
385
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
386
+ uwsApp->domain(std::string(server_name, server_name_length));
387
+ } else {
388
+ uWS::App *uwsApp = (uWS::App *)app;
389
+ uwsApp->domain(std::string(server_name, server_name_length));
390
+ }
391
+ }
392
+ void uws_app_close(int ssl, uws_app_t *app) {
393
+ if (ssl) {
394
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
395
+ uwsApp->close();
396
+ } else {
397
+ uWS::App *uwsApp = (uWS::App *)app;
398
+ uwsApp->close();
399
+ }
400
+ }
401
+
402
+ void uws_app_destroy(int ssl, uws_app_t *app) {
403
+ if (ssl) {
404
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
405
+ delete uwsApp;
406
+ } else {
407
+
408
+ uWS::App *uwsApp = (uWS::App *)app;
409
+ delete uwsApp;
410
+ }
411
+ }
412
+
413
+ bool uws_constructor_failed(int ssl, uws_app_t *app) {
414
+ if (ssl) {
415
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
416
+ if (!uwsApp)
417
+ return true;
418
+ return uwsApp->constructorFailed();
419
+ }
420
+ uWS::App *uwsApp = (uWS::App *)app;
421
+ if (!uwsApp)
422
+ return true;
423
+ return uwsApp->constructorFailed();
424
+ }
425
+
426
+ unsigned int uws_num_subscribers(int ssl, uws_app_t *app, const char *topic,
427
+ size_t topic_length) {
428
+ if (ssl) {
429
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
430
+ return uwsApp->numSubscribers(std::string_view(topic, topic_length));
431
+ }
432
+ uWS::App *uwsApp = (uWS::App *)app;
433
+ return uwsApp->numSubscribers(std::string_view(topic, topic_length));
434
+ }
435
+ bool uws_publish(int ssl, uws_app_t *app, const char *topic,
436
+ size_t topic_length, const char *message,
437
+ size_t message_length, uws_opcode_t opcode, bool compress) {
438
+ if (ssl) {
439
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
440
+ return uwsApp->publish(std::string_view(topic, topic_length),
441
+ std::string_view(message, message_length),
442
+ (uWS::OpCode)(unsigned char)opcode, compress);
443
+ }
444
+ uWS::App *uwsApp = (uWS::App *)app;
445
+ return uwsApp->publish(std::string_view(topic, topic_length),
446
+ std::string_view(message, message_length),
447
+ (uWS::OpCode)(unsigned char)opcode, compress);
448
+ }
449
+ void *uws_get_native_handle(int ssl, uws_app_t *app) {
450
+ if (ssl) {
451
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
452
+ return uwsApp->getNativeHandle();
453
+ }
454
+ uWS::App *uwsApp = (uWS::App *)app;
455
+ return uwsApp->getNativeHandle();
456
+ }
457
+ void uws_remove_server_name(int ssl, uws_app_t *app,
458
+ const char *hostname_pattern,
459
+ size_t hostname_pattern_length) {
460
+ if (ssl) {
461
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
462
+ uwsApp->removeServerName(
463
+ std::string(hostname_pattern, hostname_pattern_length));
464
+ } else {
465
+ uWS::App *uwsApp = (uWS::App *)app;
466
+ uwsApp->removeServerName(
467
+ std::string(hostname_pattern, hostname_pattern_length));
468
+ }
469
+ }
470
+ void uws_add_server_name(int ssl, uws_app_t *app, const char *hostname_pattern,
471
+ size_t hostname_pattern_length) {
472
+ if (ssl) {
473
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
474
+ uwsApp->addServerName(
475
+ std::string(hostname_pattern, hostname_pattern_length));
476
+ } else {
477
+ uWS::App *uwsApp = (uWS::App *)app;
478
+ uwsApp->addServerName(
479
+ std::string(hostname_pattern, hostname_pattern_length));
480
+ }
481
+ }
482
+ void uws_add_server_name_with_options(
483
+ int ssl, uws_app_t *app, const char *hostname_pattern,
484
+ size_t hostname_pattern_length,
485
+ struct us_socket_context_options_t options) {
486
+ uWS::SocketContextOptions sco;
487
+ sco.ca_file_name = options.ca_file_name;
488
+ sco.cert_file_name = options.cert_file_name;
489
+ sco.dh_params_file_name = options.dh_params_file_name;
490
+ sco.key_file_name = options.key_file_name;
491
+ sco.passphrase = options.passphrase;
492
+ sco.ssl_prefer_low_memory_usage = options.ssl_prefer_low_memory_usage;
493
+
494
+ if (ssl) {
495
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
496
+ uwsApp->addServerName(
497
+ std::string(hostname_pattern, hostname_pattern_length), sco);
498
+ } else {
499
+ uWS::App *uwsApp = (uWS::App *)app;
500
+ uwsApp->addServerName(
501
+ std::string(hostname_pattern, hostname_pattern_length), sco);
502
+ }
503
+ }
504
+
505
+ void uws_missing_server_name(int ssl, uws_app_t *app,
506
+ uws_missing_server_handler handler,
507
+ void *user_data) {
508
+ if (ssl) {
509
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
510
+ uwsApp->missingServerName([handler, user_data](auto hostname) {
511
+ handler(hostname, strlen(hostname), user_data);
512
+ });
513
+ } else {
514
+ uWS::App *uwsApp = (uWS::App *)app;
515
+ uwsApp->missingServerName([handler, user_data](auto hostname) {
516
+ handler(hostname, strlen(hostname), user_data);
517
+ });
518
+ }
519
+ }
520
+ void uws_filter(int ssl, uws_app_t *app, uws_filter_handler handler,
521
+ void *user_data) {
522
+ if (ssl) {
523
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
524
+ uwsApp->filter([handler, user_data](auto res, auto i) {
525
+ handler((uws_res_t *)res, i, user_data);
526
+ });
527
+ } else {
528
+ uWS::App *uwsApp = (uWS::App *)app;
529
+
530
+ uwsApp->filter([handler, user_data](auto res, auto i) {
531
+ handler((uws_res_t *)res, i, user_data);
532
+ });
533
+ }
534
+ }
535
+
536
+ void uws_ws(int ssl, uws_app_t *app, const char *pattern,
537
+ uws_socket_behavior_t behavior, void *user_data) {
538
+ if (ssl) {
539
+ auto generic_handler = uWS::SSLApp::WebSocketBehavior<void *>{
540
+ .compression = (uWS::CompressOptions)(uint64_t)behavior.compression,
541
+ .maxPayloadLength = behavior.maxPayloadLength,
542
+ .idleTimeout = behavior.idleTimeout,
543
+ .maxBackpressure = behavior.maxBackpressure,
544
+ .closeOnBackpressureLimit = behavior.closeOnBackpressureLimit,
545
+ .resetIdleTimeoutOnSend = behavior.resetIdleTimeoutOnSend,
546
+ .sendPingsAutomatically = behavior.sendPingsAutomatically,
547
+ .maxLifetime = behavior.maxLifetime,
548
+ };
549
+
550
+ if (behavior.upgrade)
551
+ generic_handler.upgrade = [behavior, user_data](auto *res, auto *req,
552
+ auto *context) {
553
+ behavior.upgrade((uws_res_t *)res, (uws_req_t *)req,
554
+ (uws_socket_context_t *)context, user_data);
555
+ };
556
+ if (behavior.open)
557
+ generic_handler.open = [behavior, user_data](auto *ws) {
558
+ behavior.open((uws_websocket_t *)ws, user_data);
559
+ };
560
+ if (behavior.message)
561
+ generic_handler.message = [behavior, user_data](auto *ws, auto message,
562
+ auto opcode) {
563
+ behavior.message((uws_websocket_t *)ws, message.data(),
564
+ message.length(), (uws_opcode_t)opcode, user_data);
565
+ };
566
+ if (behavior.drain)
567
+ generic_handler.drain = [behavior, user_data](auto *ws) {
568
+ behavior.drain((uws_websocket_t *)ws, user_data);
569
+ };
570
+ if (behavior.ping)
571
+ generic_handler.ping = [behavior, user_data](auto *ws, auto message) {
572
+ behavior.ping((uws_websocket_t *)ws, message.data(), message.length(),
573
+ user_data);
574
+ };
575
+ if (behavior.pong)
576
+ generic_handler.pong = [behavior, user_data](auto *ws, auto message) {
577
+ behavior.pong((uws_websocket_t *)ws, message.data(), message.length(),
578
+ user_data);
579
+ };
580
+ if (behavior.close)
581
+ generic_handler.close = [behavior, user_data](auto *ws, int code,
582
+ auto message) {
583
+ behavior.close((uws_websocket_t *)ws, code, message.data(),
584
+ message.length(), user_data);
585
+ };
586
+ if (behavior.subscription)
587
+ generic_handler.subscription =
588
+ [behavior, user_data](auto *ws, auto topic, int subscribers,
589
+ int old_subscribers) {
590
+ behavior.subscription((uws_websocket_t *)ws, topic.data(),
591
+ topic.length(), subscribers, old_subscribers,
592
+ user_data);
593
+ };
594
+ uWS::SSLApp *uwsApp = (uWS::SSLApp *)app;
595
+
596
+ uwsApp->ws<void *>(pattern, std::move(generic_handler));
597
+ } else {
598
+ auto generic_handler = uWS::App::WebSocketBehavior<void *>{
599
+ .compression = (uWS::CompressOptions)(uint64_t)behavior.compression,
600
+ .maxPayloadLength = behavior.maxPayloadLength,
601
+ .idleTimeout = behavior.idleTimeout,
602
+ .maxBackpressure = behavior.maxBackpressure,
603
+ .closeOnBackpressureLimit = behavior.closeOnBackpressureLimit,
604
+ .resetIdleTimeoutOnSend = behavior.resetIdleTimeoutOnSend,
605
+ .sendPingsAutomatically = behavior.sendPingsAutomatically,
606
+ .maxLifetime = behavior.maxLifetime,
607
+ };
608
+
609
+ if (behavior.upgrade)
610
+ generic_handler.upgrade = [behavior, user_data](auto *res, auto *req,
611
+ auto *context) {
612
+ behavior.upgrade((uws_res_t *)res, (uws_req_t *)req,
613
+ (uws_socket_context_t *)context, user_data);
614
+ };
615
+ if (behavior.open)
616
+ generic_handler.open = [behavior, user_data](auto *ws) {
617
+ behavior.open((uws_websocket_t *)ws, user_data);
618
+ };
619
+ if (behavior.message)
620
+ generic_handler.message = [behavior, user_data](auto *ws, auto message,
621
+ auto opcode) {
622
+ behavior.message((uws_websocket_t *)ws, message.data(),
623
+ message.length(), (uws_opcode_t)opcode, user_data);
624
+ };
625
+ if (behavior.drain)
626
+ generic_handler.drain = [behavior, user_data](auto *ws) {
627
+ behavior.drain((uws_websocket_t *)ws, user_data);
628
+ };
629
+ if (behavior.ping)
630
+ generic_handler.ping = [behavior, user_data](auto *ws, auto message) {
631
+ behavior.ping((uws_websocket_t *)ws, message.data(), message.length(),
632
+ user_data);
633
+ };
634
+ if (behavior.pong)
635
+ generic_handler.pong = [behavior, user_data](auto *ws, auto message) {
636
+ behavior.pong((uws_websocket_t *)ws, message.data(), message.length(),
637
+ user_data);
638
+ };
639
+ if (behavior.close)
640
+ generic_handler.close = [behavior, user_data](auto *ws, int code,
641
+ auto message) {
642
+ behavior.close((uws_websocket_t *)ws, code, message.data(),
643
+ message.length(), user_data);
644
+ };
645
+ if (behavior.subscription)
646
+ generic_handler.subscription =
647
+ [behavior, user_data](auto *ws, auto topic, int subscribers,
648
+ int old_subscribers) {
649
+ behavior.subscription((uws_websocket_t *)ws, topic.data(),
650
+ topic.length(), subscribers, old_subscribers,
651
+ user_data);
652
+ };
653
+ uWS::App *uwsApp = (uWS::App *)app;
654
+ uwsApp->ws<void *>(pattern, std::move(generic_handler));
655
+ }
656
+ }
657
+
658
+ void *uws_ws_get_user_data(int ssl, uws_websocket_t *ws) {
659
+ if (ssl) {
660
+ uWS::WebSocket<true, true, void *> *uws =
661
+ (uWS::WebSocket<true, true, void *> *)ws;
662
+ return *uws->getUserData();
663
+ }
664
+ uWS::WebSocket<false, true, void *> *uws =
665
+ (uWS::WebSocket<false, true, void *> *)ws;
666
+ return *uws->getUserData();
667
+ }
668
+
669
+ void uws_ws_close(int ssl, uws_websocket_t *ws) {
670
+ if (ssl) {
671
+ uWS::WebSocket<true, true, void *> *uws =
672
+ (uWS::WebSocket<true, true, void *> *)ws;
673
+ uws->close();
674
+ } else {
675
+ uWS::WebSocket<false, true, void *> *uws =
676
+ (uWS::WebSocket<false, true, void *> *)ws;
677
+ uws->close();
678
+ }
679
+ }
680
+
681
+ uws_sendstatus_t uws_ws_send(int ssl, uws_websocket_t *ws, const char *message,
682
+ size_t length, uws_opcode_t opcode) {
683
+ if (ssl) {
684
+ uWS::WebSocket<true, true, void *> *uws =
685
+ (uWS::WebSocket<true, true, void *> *)ws;
686
+ return (uws_sendstatus_t)uws->send(std::string_view(message, length),
687
+ (uWS::OpCode)(unsigned char)opcode);
688
+ }
689
+ uWS::WebSocket<false, true, void *> *uws =
690
+ (uWS::WebSocket<false, true, void *> *)ws;
691
+ return (uws_sendstatus_t)uws->send(std::string_view(message, length),
692
+ (uWS::OpCode)(unsigned char)opcode);
693
+ }
694
+
695
+ uws_sendstatus_t uws_ws_send_with_options(int ssl, uws_websocket_t *ws,
696
+ const char *message, size_t length,
697
+ uws_opcode_t opcode, bool compress,
698
+ bool fin) {
699
+ if (ssl) {
700
+ uWS::WebSocket<true, true, void *> *uws =
701
+ (uWS::WebSocket<true, true, void *> *)ws;
702
+ return (uws_sendstatus_t)uws->send(std::string_view(message, length),
703
+ (uWS::OpCode)(unsigned char)opcode,
704
+ compress, fin);
705
+ }
706
+ uWS::WebSocket<false, true, void *> *uws =
707
+ (uWS::WebSocket<false, true, void *> *)ws;
708
+ return (uws_sendstatus_t)uws->send(std::string_view(message, length),
709
+ (uWS::OpCode)(unsigned char)opcode,
710
+ compress, fin);
711
+ }
712
+
713
+ uws_sendstatus_t uws_ws_send_fragment(int ssl, uws_websocket_t *ws,
714
+ const char *message, size_t length,
715
+ bool compress) {
716
+ if (ssl) {
717
+ uWS::WebSocket<true, true, void *> *uws =
718
+ (uWS::WebSocket<true, true, void *> *)ws;
719
+ return (uws_sendstatus_t)uws->sendFragment(
720
+ std::string_view(message, length), compress);
721
+ }
722
+ uWS::WebSocket<false, true, void *> *uws =
723
+ (uWS::WebSocket<false, true, void *> *)ws;
724
+ return (uws_sendstatus_t)uws->sendFragment(std::string_view(message, length),
725
+ compress);
726
+ }
727
+ uws_sendstatus_t uws_ws_send_first_fragment(int ssl, uws_websocket_t *ws,
728
+ const char *message, size_t length,
729
+ bool compress) {
730
+ if (ssl) {
731
+ uWS::WebSocket<true, true, void *> *uws =
732
+ (uWS::WebSocket<true, true, void *> *)ws;
733
+ return (uws_sendstatus_t)uws->sendFirstFragment(
734
+ std::string_view(message, length), uWS::OpCode::BINARY, compress);
735
+ }
736
+ uWS::WebSocket<false, true, void *> *uws =
737
+ (uWS::WebSocket<false, true, void *> *)ws;
738
+ return (uws_sendstatus_t)uws->sendFirstFragment(
739
+ std::string_view(message, length), uWS::OpCode::BINARY, compress);
740
+ }
741
+ uws_sendstatus_t
742
+ uws_ws_send_first_fragment_with_opcode(int ssl, uws_websocket_t *ws,
743
+ const char *message, size_t length,
744
+ uws_opcode_t opcode, bool compress) {
745
+ if (ssl) {
746
+ uWS::WebSocket<true, true, void *> *uws =
747
+ (uWS::WebSocket<true, true, void *> *)ws;
748
+ return (uws_sendstatus_t)uws->sendFirstFragment(
749
+ std::string_view(message, length), (uWS::OpCode)(unsigned char)opcode,
750
+ compress);
751
+ }
752
+ uWS::WebSocket<false, true, void *> *uws =
753
+ (uWS::WebSocket<false, true, void *> *)ws;
754
+ return (uws_sendstatus_t)uws->sendFirstFragment(
755
+ std::string_view(message, length), (uWS::OpCode)(unsigned char)opcode,
756
+ compress);
757
+ }
758
+ uws_sendstatus_t uws_ws_send_last_fragment(int ssl, uws_websocket_t *ws,
759
+ const char *message, size_t length,
760
+ bool compress) {
761
+ if (ssl) {
762
+ uWS::WebSocket<true, true, void *> *uws =
763
+ (uWS::WebSocket<true, true, void *> *)ws;
764
+ return (uws_sendstatus_t)uws->sendLastFragment(
765
+ std::string_view(message, length), compress);
766
+ }
767
+ uWS::WebSocket<false, true, void *> *uws =
768
+ (uWS::WebSocket<false, true, void *> *)ws;
769
+ return (uws_sendstatus_t)uws->sendLastFragment(
770
+ std::string_view(message, length), compress);
771
+ }
772
+
773
+ void uws_ws_end(int ssl, uws_websocket_t *ws, int code, const char *message,
774
+ size_t length) {
775
+ if (ssl) {
776
+ uWS::WebSocket<true, true, void *> *uws =
777
+ (uWS::WebSocket<true, true, void *> *)ws;
778
+ uws->end(code, std::string_view(message, length));
779
+ } else {
780
+ uWS::WebSocket<false, true, void *> *uws =
781
+ (uWS::WebSocket<false, true, void *> *)ws;
782
+ uws->end(code, std::string_view(message, length));
783
+ }
784
+ }
785
+
786
+ void uws_ws_cork(int ssl, uws_websocket_t *ws, void (*handler)(void *user_data),
787
+ void *user_data) {
788
+ if (ssl) {
789
+ uWS::WebSocket<true, true, void *> *uws =
790
+ (uWS::WebSocket<true, true, void *> *)ws;
791
+ uws->cork([handler, user_data]() { handler(user_data); });
792
+ } else {
793
+ uWS::WebSocket<false, true, void *> *uws =
794
+ (uWS::WebSocket<false, true, void *> *)ws;
795
+
796
+ uws->cork([handler, user_data]() { handler(user_data); });
797
+ }
798
+ }
799
+ bool uws_ws_subscribe(int ssl, uws_websocket_t *ws, const char *topic,
800
+ size_t length) {
801
+ if (ssl) {
802
+ uWS::WebSocket<true, true, void *> *uws =
803
+ (uWS::WebSocket<true, true, void *> *)ws;
804
+ return uws->subscribe(std::string_view(topic, length));
805
+ }
806
+ uWS::WebSocket<false, true, void *> *uws =
807
+ (uWS::WebSocket<false, true, void *> *)ws;
808
+ return uws->subscribe(std::string_view(topic, length));
809
+ }
810
+ bool uws_ws_unsubscribe(int ssl, uws_websocket_t *ws, const char *topic,
811
+ size_t length) {
812
+ if (ssl) {
813
+ uWS::WebSocket<true, true, void *> *uws =
814
+ (uWS::WebSocket<true, true, void *> *)ws;
815
+ return uws->unsubscribe(std::string_view(topic, length));
816
+ }
817
+ uWS::WebSocket<false, true, void *> *uws =
818
+ (uWS::WebSocket<false, true, void *> *)ws;
819
+ return uws->unsubscribe(std::string_view(topic, length));
820
+ }
821
+
822
+ bool uws_ws_is_subscribed(int ssl, uws_websocket_t *ws, const char *topic,
823
+ size_t length) {
824
+ if (ssl) {
825
+ uWS::WebSocket<true, true, void *> *uws =
826
+ (uWS::WebSocket<true, true, void *> *)ws;
827
+ return uws->isSubscribed(std::string_view(topic, length));
828
+ }
829
+ uWS::WebSocket<false, true, void *> *uws =
830
+ (uWS::WebSocket<false, true, void *> *)ws;
831
+ return uws->isSubscribed(std::string_view(topic, length));
832
+ }
833
+ void uws_ws_iterate_topics(int ssl, uws_websocket_t *ws,
834
+ void (*callback)(const char *topic, size_t length,
835
+ void *user_data),
836
+ void *user_data) {
837
+ if (ssl) {
838
+ uWS::WebSocket<true, true, void *> *uws =
839
+ (uWS::WebSocket<true, true, void *> *)ws;
840
+ uws->iterateTopics([callback, user_data](auto topic) {
841
+ callback(topic.data(), topic.length(), user_data);
842
+ });
843
+ } else {
844
+ uWS::WebSocket<false, true, void *> *uws =
845
+ (uWS::WebSocket<false, true, void *> *)ws;
846
+
847
+ uws->iterateTopics([callback, user_data](auto topic) {
848
+ callback(topic.data(), topic.length(), user_data);
849
+ });
850
+ }
851
+ }
852
+
853
+ bool uws_ws_publish(int ssl, uws_websocket_t *ws, const char *topic,
854
+ size_t topic_length, const char *message,
855
+ size_t message_length) {
856
+ if (ssl) {
857
+ uWS::WebSocket<true, true, void *> *uws =
858
+ (uWS::WebSocket<true, true, void *> *)ws;
859
+ return uws->publish(std::string_view(topic, topic_length),
860
+ std::string_view(message, message_length));
861
+ }
862
+ uWS::WebSocket<false, true, void *> *uws =
863
+ (uWS::WebSocket<false, true, void *> *)ws;
864
+ return uws->publish(std::string_view(topic, topic_length),
865
+ std::string_view(message, message_length));
866
+ }
867
+
868
+ bool uws_ws_publish_with_options(int ssl, uws_websocket_t *ws,
869
+ const char *topic, size_t topic_length,
870
+ const char *message, size_t message_length,
871
+ uws_opcode_t opcode, bool compress) {
872
+ if (ssl) {
873
+ uWS::WebSocket<true, true, void *> *uws =
874
+ (uWS::WebSocket<true, true, void *> *)ws;
875
+ return uws->publish(std::string_view(topic, topic_length),
876
+ std::string_view(message, message_length),
877
+ (uWS::OpCode)(unsigned char)opcode, compress);
878
+ }
879
+ uWS::WebSocket<false, true, void *> *uws =
880
+ (uWS::WebSocket<false, true, void *> *)ws;
881
+ return uws->publish(std::string_view(topic, topic_length),
882
+ std::string_view(message, message_length),
883
+ (uWS::OpCode)(unsigned char)opcode, compress);
884
+ }
885
+
886
+ unsigned int uws_ws_get_buffered_amount(int ssl, uws_websocket_t *ws) {
887
+ if (ssl) {
888
+ uWS::WebSocket<true, true, void *> *uws =
889
+ (uWS::WebSocket<true, true, void *> *)ws;
890
+ return uws->getBufferedAmount();
891
+ }
892
+ uWS::WebSocket<false, true, void *> *uws =
893
+ (uWS::WebSocket<false, true, void *> *)ws;
894
+ return uws->getBufferedAmount();
895
+ }
896
+
897
+ size_t uws_ws_get_remote_address(int ssl, uws_websocket_t *ws,
898
+ const char **dest) {
899
+ if (ssl) {
900
+ uWS::WebSocket<true, true, void *> *uws =
901
+ (uWS::WebSocket<true, true, void *> *)ws;
902
+ std::string_view value = uws->getRemoteAddress();
903
+ *dest = value.data();
904
+ return value.length();
905
+ }
906
+ uWS::WebSocket<false, true, void *> *uws =
907
+ (uWS::WebSocket<false, true, void *> *)ws;
908
+
909
+ std::string_view value = uws->getRemoteAddress();
910
+ *dest = value.data();
911
+ return value.length();
912
+ }
913
+
914
+ size_t uws_ws_get_remote_address_as_text(int ssl, uws_websocket_t *ws,
915
+ const char **dest) {
916
+ if (ssl) {
917
+ uWS::WebSocket<true, true, void *> *uws =
918
+ (uWS::WebSocket<true, true, void *> *)ws;
919
+
920
+ std::string_view value = uws->getRemoteAddressAsText();
921
+ *dest = value.data();
922
+ return value.length();
923
+ }
924
+ uWS::WebSocket<false, true, void *> *uws =
925
+ (uWS::WebSocket<false, true, void *> *)ws;
926
+
927
+ std::string_view value = uws->getRemoteAddressAsText();
928
+ *dest = value.data();
929
+ return value.length();
930
+ }
931
+ void uws_res_close(int ssl, uws_res_t *res) {
932
+ if (ssl) {
933
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
934
+ uwsRes->close();
935
+ } else {
936
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
937
+ uwsRes->close();
938
+ }
939
+ }
940
+ void uws_res_end(int ssl, uws_res_t *res, const char *data, size_t length,
941
+ bool close_connection) {
942
+ if (ssl) {
943
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
944
+ uwsRes->end(std::string_view(data, length), close_connection);
945
+ } else {
946
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
947
+ uwsRes->end(std::string_view(data, length), close_connection);
948
+ }
949
+ }
950
+
951
+ size_t uws_res_get_remote_address(int ssl, uws_res_t *res, const char **dest) {
952
+ if (ssl) {
953
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
954
+ std::string_view value = uwsRes->getRemoteAddress();
955
+ *dest = value.data();
956
+ return value.length();
957
+ }
958
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
959
+
960
+ std::string_view value = uwsRes->getRemoteAddress();
961
+ *dest = value.data();
962
+ return value.length();
963
+ }
964
+
965
+ size_t uws_res_get_remote_address_as_text(int ssl, uws_res_t *res,
966
+ const char **dest) {
967
+ if (ssl) {
968
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
969
+ std::string_view value = uwsRes->getRemoteAddressAsText();
970
+ *dest = value.data();
971
+ return value.length();
972
+ }
973
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
974
+
975
+ std::string_view value = uwsRes->getRemoteAddressAsText();
976
+ *dest = value.data();
977
+ return value.length();
978
+ }
979
+ #ifdef UWS_WITH_PROXY
980
+ size_t uws_res_get_proxied_remote_address(int ssl, uws_res_t *res,
981
+ const char **dest) {
982
+ if (ssl) {
983
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
984
+ std::string_view value = uwsRes->getProxiedRemoteAddress();
985
+ *dest = value.data();
986
+ return value.length();
987
+ }
988
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
989
+
990
+ std::string_view value = uwsRes->getProxiedRemoteAddress();
991
+ *dest = value.data();
992
+ return value.length();
993
+ }
994
+
995
+ size_t uws_res_get_proxied_remote_address_as_text(int ssl, uws_res_t *res,
996
+ const char **dest) {
997
+ if (ssl) {
998
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
999
+ std::string_view value = uwsRes->getProxiedRemoteAddressAsText();
1000
+ *dest = value.data();
1001
+ return value.length();
1002
+ }
1003
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1004
+
1005
+ std::string_view value = uwsRes->getProxiedRemoteAddressAsText();
1006
+ *dest = value.data();
1007
+ return value.length();
1008
+ }
1009
+ #endif
1010
+ uws_try_end_result_t uws_res_try_end(int ssl, uws_res_t *res, const char *data,
1011
+ size_t length, uintmax_t total_size,
1012
+ bool close_connection) {
1013
+ if (ssl) {
1014
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1015
+ // uwsRes->end(std::string_view(data, length), close_connection);
1016
+ std::pair<bool, bool> result = uwsRes->tryEnd(
1017
+ std::string_view(data, length), total_size, close_connection);
1018
+ return uws_try_end_result_t{
1019
+ .ok = result.first,
1020
+ .has_responded = result.second,
1021
+ };
1022
+ } else {
1023
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1024
+ std::pair<bool, bool> result =
1025
+ uwsRes->tryEnd(std::string_view(data, length), total_size);
1026
+ return uws_try_end_result_t{
1027
+ .ok = result.first,
1028
+ .has_responded = result.second,
1029
+ };
1030
+ }
1031
+ }
1032
+
1033
+ void uws_res_cork(int ssl, uws_res_t *res,
1034
+ void (*callback)(uws_res_t *res, void *user_data),
1035
+ void *user_data) {
1036
+ if (ssl) {
1037
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1038
+ uwsRes->cork([=]() { callback(res, user_data); });
1039
+ } else {
1040
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1041
+ uwsRes->cork([=]() { callback(res, user_data); });
1042
+ }
1043
+ }
1044
+
1045
+ void uws_res_pause(int ssl, uws_res_t *res) {
1046
+ if (ssl) {
1047
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1048
+ uwsRes->pause();
1049
+ } else {
1050
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1051
+ uwsRes->pause();
1052
+ }
1053
+ }
1054
+
1055
+ void uws_res_resume(int ssl, uws_res_t *res) {
1056
+ if (ssl) {
1057
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1058
+ uwsRes->resume();
1059
+ } else {
1060
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1061
+ uwsRes->resume();
1062
+ }
1063
+ }
1064
+
1065
+ void uws_res_write_continue(int ssl, uws_res_t *res) {
1066
+ if (ssl) {
1067
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1068
+ uwsRes->writeContinue();
1069
+ } else {
1070
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1071
+ uwsRes->writeContinue();
1072
+ }
1073
+ }
1074
+
1075
+ void uws_res_write_status(int ssl, uws_res_t *res, const char *status,
1076
+ size_t length) {
1077
+ if (ssl) {
1078
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1079
+ uwsRes->writeStatus(std::string_view(status, length));
1080
+ } else {
1081
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1082
+ uwsRes->writeStatus(std::string_view(status, length));
1083
+ }
1084
+ }
1085
+
1086
+ void uws_res_write_header(int ssl, uws_res_t *res, const char *key,
1087
+ size_t key_length, const char *value,
1088
+ size_t value_length) {
1089
+ if (ssl) {
1090
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1091
+ uwsRes->writeHeader(std::string_view(key, key_length),
1092
+ std::string_view(value, value_length));
1093
+ } else {
1094
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1095
+ uwsRes->writeHeader(std::string_view(key, key_length),
1096
+ std::string_view(value, value_length));
1097
+ }
1098
+ }
1099
+ void uws_res_write_header_int(int ssl, uws_res_t *res, const char *key,
1100
+ size_t key_length, uint64_t value) {
1101
+ if (ssl) {
1102
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1103
+ uwsRes->writeHeader(std::string_view(key, key_length), value);
1104
+ } else {
1105
+
1106
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1107
+ uwsRes->writeHeader(std::string_view(key, key_length), value);
1108
+ }
1109
+ }
1110
+
1111
+ void uws_res_end_without_body(int ssl, uws_res_t *res, bool close_connection) {
1112
+ if (ssl) {
1113
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1114
+ uwsRes->endWithoutBody(std::nullopt, close_connection);
1115
+ } else {
1116
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1117
+ uwsRes->endWithoutBody(std::nullopt, close_connection);
1118
+ }
1119
+ }
1120
+
1121
+ bool uws_res_write(int ssl, uws_res_t *res, const char *data, size_t length) {
1122
+ if (ssl) {
1123
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1124
+ return uwsRes->write(std::string_view(data, length));
1125
+ }
1126
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1127
+ return uwsRes->write(std::string_view(data, length));
1128
+ }
1129
+ uintmax_t uws_res_get_write_offset(int ssl, uws_res_t *res) {
1130
+ if (ssl) {
1131
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1132
+ return uwsRes->getWriteOffset();
1133
+ }
1134
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1135
+ return uwsRes->getWriteOffset();
1136
+ }
1137
+ void uws_res_override_write_offset(int ssl, uws_res_t *res, uintmax_t offset) {
1138
+ if (ssl) {
1139
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1140
+ uwsRes->overrideWriteOffset(offset);
1141
+ } else {
1142
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1143
+ uwsRes->overrideWriteOffset(offset);
1144
+ }
1145
+ }
1146
+ bool uws_res_has_responded(int ssl, uws_res_t *res) {
1147
+ if (ssl) {
1148
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1149
+ return uwsRes->hasResponded();
1150
+ }
1151
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1152
+ return uwsRes->hasResponded();
1153
+ }
1154
+
1155
+ void uws_res_on_writable(int ssl, uws_res_t *res,
1156
+ bool (*handler)(uws_res_t *res, uintmax_t,
1157
+ void *optional_data),
1158
+ void *optional_data) {
1159
+ if (ssl) {
1160
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1161
+ uwsRes->onWritable([handler, res, optional_data](uintmax_t a) {
1162
+ return handler(res, a, optional_data);
1163
+ });
1164
+ } else {
1165
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1166
+ uwsRes->onWritable([handler, res, optional_data](uintmax_t a) {
1167
+ return handler(res, a, optional_data);
1168
+ });
1169
+ }
1170
+ }
1171
+
1172
+ void uws_res_on_aborted(int ssl, uws_res_t *res,
1173
+ void (*handler)(uws_res_t *res, void *optional_data),
1174
+ void *optional_data) {
1175
+ if (ssl) {
1176
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1177
+ uwsRes->onAborted(
1178
+ [handler, res, optional_data] { handler(res, optional_data); });
1179
+ } else {
1180
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1181
+ uwsRes->onAborted(
1182
+ [handler, res, optional_data] { handler(res, optional_data); });
1183
+ }
1184
+ }
1185
+
1186
+ void uws_res_on_data(int ssl, uws_res_t *res,
1187
+ void (*handler)(uws_res_t *res, const char *chunk,
1188
+ size_t chunk_length, bool is_end,
1189
+ void *optional_data),
1190
+ void *optional_data) {
1191
+ if (ssl) {
1192
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1193
+ uwsRes->onData([handler, res, optional_data](auto chunk, bool is_end) {
1194
+ handler(res, chunk.data(), chunk.length(), is_end, optional_data);
1195
+ });
1196
+ } else {
1197
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1198
+ uwsRes->onData([handler, res, optional_data](auto chunk, bool is_end) {
1199
+ handler(res, chunk.data(), chunk.length(), is_end, optional_data);
1200
+ });
1201
+ }
1202
+ }
1203
+
1204
+ bool uws_req_is_ancient(uws_req_t *res) {
1205
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1206
+ return uwsReq->isAncient();
1207
+ }
1208
+
1209
+ bool uws_req_get_yield(uws_req_t *res) {
1210
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1211
+ return uwsReq->getYield();
1212
+ }
1213
+
1214
+ void uws_req_set_yield(uws_req_t *res, bool yield) {
1215
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1216
+ return uwsReq->setYield(yield);
1217
+ }
1218
+
1219
+ size_t uws_req_get_url(uws_req_t *res, const char **dest) {
1220
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1221
+ std::string_view value = uwsReq->getUrl();
1222
+ *dest = value.data();
1223
+ return value.length();
1224
+ }
1225
+
1226
+ size_t uws_req_get_full_url(uws_req_t *res, const char **dest) {
1227
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1228
+ std::string_view value = uwsReq->getFullUrl();
1229
+ *dest = value.data();
1230
+ return value.length();
1231
+ }
1232
+
1233
+ size_t uws_req_get_method(uws_req_t *res, const char **dest) {
1234
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1235
+ std::string_view value = uwsReq->getMethod();
1236
+ *dest = value.data();
1237
+ return value.length();
1238
+ }
1239
+
1240
+ size_t uws_req_get_case_sensitive_method(uws_req_t *res, const char **dest) {
1241
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1242
+ std::string_view value = uwsReq->getCaseSensitiveMethod();
1243
+ *dest = value.data();
1244
+ return value.length();
1245
+ }
1246
+
1247
+ void uws_req_for_each_header(uws_req_t *res,
1248
+ uws_get_headers_server_handler handler,
1249
+ void *user_data) {
1250
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1251
+ for (auto header : *uwsReq) {
1252
+ handler(header.first.data(), header.first.length(), header.second.data(),
1253
+ header.second.length(), user_data);
1254
+ }
1255
+ }
1256
+
1257
+ size_t uws_req_get_header(uws_req_t *res, const char *lower_case_header,
1258
+ size_t lower_case_header_length, const char **dest) {
1259
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1260
+
1261
+ std::string_view value = uwsReq->getHeader(
1262
+ std::string_view(lower_case_header, lower_case_header_length));
1263
+ *dest = value.data();
1264
+ return value.length();
1265
+ }
1266
+
1267
+ size_t uws_req_get_query(uws_req_t *res, const char *key, size_t key_length,
1268
+ const char **dest) {
1269
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1270
+
1271
+ std::string_view value;
1272
+ if (!key || key_length == 0)
1273
+ value = uwsReq->getQuery();
1274
+ else
1275
+ value = uwsReq->getQuery(std::string_view(key, key_length));
1276
+ *dest = value.data();
1277
+ return value.length();
1278
+ }
1279
+
1280
+ size_t uws_req_get_parameter(uws_req_t *res, unsigned short index,
1281
+ const char **dest) {
1282
+ uWS::HttpRequest *uwsReq = (uWS::HttpRequest *)res;
1283
+ std::string_view value = uwsReq->getParameter(index);
1284
+ *dest = value.data();
1285
+ return value.length();
1286
+ }
1287
+
1288
+ void uws_res_upgrade(int ssl, uws_res_t *res, void *data,
1289
+ const char *sec_web_socket_key,
1290
+ size_t sec_web_socket_key_length,
1291
+ const char *sec_web_socket_protocol,
1292
+ size_t sec_web_socket_protocol_length,
1293
+ const char *sec_web_socket_extensions,
1294
+ size_t sec_web_socket_extensions_length,
1295
+ uws_socket_context_t *ws) {
1296
+
1297
+ if (ssl) {
1298
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1299
+
1300
+ uwsRes->template upgrade<void *>(
1301
+ data ? std::move(data) : NULL,
1302
+ std::string_view(sec_web_socket_key, sec_web_socket_key_length),
1303
+ std::string_view(sec_web_socket_protocol,
1304
+ sec_web_socket_protocol_length),
1305
+ std::string_view(sec_web_socket_extensions,
1306
+ sec_web_socket_extensions_length),
1307
+ (struct us_socket_context_t *)ws);
1308
+ } else {
1309
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1310
+
1311
+ uwsRes->template upgrade<void *>(
1312
+ data ? std::move(data) : NULL,
1313
+ std::string_view(sec_web_socket_key, sec_web_socket_key_length),
1314
+ std::string_view(sec_web_socket_protocol,
1315
+ sec_web_socket_protocol_length),
1316
+ std::string_view(sec_web_socket_extensions,
1317
+ sec_web_socket_extensions_length),
1318
+ (struct us_socket_context_t *)ws);
1319
+ }
1320
+ }
1321
+
1322
+ void *uws_res_get_native_handle(int ssl, uws_res_t *res) {
1323
+ if (ssl) {
1324
+ uWS::HttpResponse<true> *uwsRes = (uWS::HttpResponse<true> *)res;
1325
+ return uwsRes->getNativeHandle();
1326
+ } else {
1327
+ uWS::HttpResponse<false> *uwsRes = (uWS::HttpResponse<false> *)res;
1328
+ return uwsRes->getNativeHandle();
1329
+ }
1330
+ }
1331
+
1332
+ struct us_loop_t *uws_get_loop() {
1333
+ return (struct us_loop_t *)uWS::Loop::get();
1334
+ }
1335
+
1336
+ struct us_loop_t *uws_get_loop_with_native(void *existing_native_loop) {
1337
+ return (struct us_loop_t *)uWS::Loop::get(existing_native_loop);
1338
+ }
1339
+ void uws_loop_defer(us_loop_t *loop, void(cb(void *user_data)),
1340
+ void *user_data) {
1341
+ uWS::Loop *loop_instance = (uWS::Loop *)loop;
1342
+ loop_instance->defer([cb, user_data]() { cb(user_data); });
1343
+ }
1344
+ }