aboutsummaryrefslogtreecommitdiff
path: root/src/deps/_libusockets.h
blob: 7042d3d14fb05aef9d69b637ef78bb14ffe508ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298

#ifndef LIBUWS_CAPI_HEADER
#define LIBUWS_CAPI_HEADER

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <uws/uSockets/src/libusockets.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
  /* These are not actual compression options */
  _COMPRESSOR_MASK = 0x00FF,
  _DECOMPRESSOR_MASK = 0x0F00,
  /* Disabled, shared, shared are "special" values */
  DISABLED = 0,
  SHARED_COMPRESSOR = 1,
  SHARED_DECOMPRESSOR = 1 << 8,
  /* Highest 4 bits describe decompressor */
  DEDICATED_DECOMPRESSOR_32KB = 15 << 8,
  DEDICATED_DECOMPRESSOR_16KB = 14 << 8,
  DEDICATED_DECOMPRESSOR_8KB = 13 << 8,
  DEDICATED_DECOMPRESSOR_4KB = 12 << 8,
  DEDICATED_DECOMPRESSOR_2KB = 11 << 8,
  DEDICATED_DECOMPRESSOR_1KB = 10 << 8,
  DEDICATED_DECOMPRESSOR_512B = 9 << 8,
  /* Same as 32kb */
  DEDICATED_DECOMPRESSOR = 15 << 8,

  /* Lowest 8 bit describe compressor */
  DEDICATED_COMPRESSOR_3KB = 9 << 4 | 1,
  DEDICATED_COMPRESSOR_4KB = 9 << 4 | 2,
  DEDICATED_COMPRESSOR_8KB = 10 << 4 | 3,
  DEDICATED_COMPRESSOR_16KB = 11 << 4 | 4,
  DEDICATED_COMPRESSOR_32KB = 12 << 4 | 5,
  DEDICATED_COMPRESSOR_64KB = 13 << 4 | 6,
  DEDICATED_COMPRESSOR_128KB = 14 << 4 | 7,
  DEDICATED_COMPRESSOR_256KB = 15 << 4 | 8,
  /* Same as 256kb */
  DEDICATED_COMPRESSOR = 15 << 4 | 8
} uws_compress_options_t;

typedef enum {
  CONTINUATION = 0,
  TEXT = 1,
  BINARY = 2,
  CLOSE = 8,
  PING = 9,
  PONG = 10
} uws_opcode_t;

typedef enum { BACKPRESSURE, SUCCESS, DROPPED } uws_sendstatus_t;

typedef struct {

  int port;
  const char *host;
  int options;
} uws_app_listen_config_t;

struct uws_app_s;
struct uws_req_s;
struct uws_res_s;
struct uws_websocket_s;
struct uws_header_iterator_s;
typedef struct uws_app_s uws_app_t;
typedef struct uws_req_s uws_req_t;
typedef struct uws_res_s uws_res_t;
typedef struct uws_socket_context_s uws_socket_context_t;
typedef struct uws_websocket_s uws_websocket_t;

typedef void (*uws_websocket_handler)(uws_websocket_t *ws);
typedef void (*uws_websocket_message_handler)(uws_websocket_t *ws,
                                              const char *message,
                                              size_t length,
                                              uws_opcode_t opcode);
typedef void (*uws_websocket_ping_pong_handler)(uws_websocket_t *ws,
                                                const char *message,
                                                size_t length);
typedef void (*uws_websocket_close_handler)(uws_websocket_t *ws, int code,
                                            const char *message, size_t length);
typedef void (*uws_websocket_upgrade_handler)(uws_res_t *response,
                                              uws_req_t *request,
                                              uws_socket_context_t *context);

typedef struct {
  uws_compress_options_t compression;
  /* Maximum message size we can receive */
  unsigned int maxPayloadLength;
  /* 2 minutes timeout is good */
  unsigned short idleTimeout;
  /* 64kb backpressure is probably good */
  unsigned int maxBackpressure;
  bool closeOnBackpressureLimit;
  /* This one depends on kernel timeouts and is a bad default */
  bool resetIdleTimeoutOnSend;
  /* A good default, esp. for newcomers */
  bool sendPingsAutomatically;
  /* Maximum socket lifetime in seconds before forced closure (defaults to
   * disabled) */
  unsigned short maxLifetime;

  uws_websocket_upgrade_handler upgrade;
  uws_websocket_handler open;
  uws_websocket_message_handler message;
  uws_websocket_handler drain;
  uws_websocket_ping_pong_handler ping;
  uws_websocket_ping_pong_handler pong;
  uws_websocket_close_handler close;
} uws_socket_behavior_t;

typedef void (*uws_listen_handler)(struct us_listen_socket_t *listen_socket,
                                   uws_app_listen_config_t config,
                                   void *user_data);
typedef void (*uws_method_handler)(uws_res_t *response, uws_req_t *request,
                                   void *user_data);
typedef void (*uws_filter_handler)(uws_res_t *response, int, void *user_data);
typedef void (*uws_missing_server_handler)(const char *hostname,
                                           void *user_data);
// Basic HTTP
uws_app_t *uws_create_app(int ssl, struct us_socket_context_options_t options);
void uws_app_destroy(int ssl, uws_app_t *app);
void uws_app_get(int ssl, uws_app_t *app, const char *pattern,
                 uws_method_handler handler, void *user_data);
void uws_app_post(int ssl, uws_app_t *app, const char *pattern,
                  uws_method_handler handler, void *user_data);
void uws_app_options(int ssl, uws_app_t *app, const char *pattern,
                     uws_method_handler handler, void *user_data);
void uws_app_delete(int ssl, uws_app_t *app, const char *pattern,
                    uws_method_handler handler, void *user_data);
void uws_app_patch(int ssl, uws_app_t *app, const char *pattern,
                   uws_method_handler handler, void *user_data);
void uws_app_put(int ssl, uws_app_t *app, const char *pattern,
                 uws_method_handler handler, void *user_data);
void uws_app_head(int ssl, uws_app_t *app, const char *pattern,
                  uws_method_handler handler, void *user_data);
void uws_app_connect(int ssl, uws_app_t *app, const char *pattern,
                     uws_method_handler handler, void *user_data);
void uws_app_trace(int ssl, uws_app_t *app, const char *pattern,
                   uws_method_handler handler, void *user_data);
void uws_app_any(int ssl, uws_app_t *app, const char *pattern,
                 uws_method_handler handler, void *user_data);

void uws_app_run(int ssl, uws_app_t *);

void uws_app_listen(int ssl, uws_app_t *app, int port,
                    uws_listen_handler handler, void *user_data);
void uws_app_listen_with_config(int ssl, uws_app_t *app,
                                uws_app_listen_config_t config,
                                uws_listen_handler handler, void *user_data);
bool uws_constructor_failed(int ssl, uws_app_t *app);
unsigned int uws_num_subscribers(int ssl, uws_app_t *app, const char *topic);
bool uws_publish(int ssl, uws_app_t *app, const char *topic,
                 size_t topic_length, const char *message,
                 size_t message_length, uws_opcode_t opcode, bool compress);
void *uws_get_native_handle(int ssl, uws_app_t *app);
void uws_remove_server_name(int ssl, uws_app_t *app,
                            const char *hostname_pattern);
void uws_add_server_name(int ssl, uws_app_t *app, const char *hostname_pattern);
void uws_add_server_name_with_options(
    int ssl, uws_app_t *app, const char *hostname_pattern,
    struct us_socket_context_options_t options);
void uws_missing_server_name(int ssl, uws_app_t *app,
                             uws_missing_server_handler handler,
                             void *user_data);
void uws_filter(int ssl, uws_app_t *app, uws_filter_handler handler,
                void *user_data);

// WebSocket
void uws_ws(int ssl, uws_app_t *app, const char *pattern,
            uws_socket_behavior_t behavior);
void *uws_ws_get_user_data(int ssl, uws_websocket_t *ws);
void uws_ws_close(int ssl, uws_websocket_t *ws);
uws_sendstatus_t uws_ws_send(int ssl, uws_websocket_t *ws, const char *message,
                             size_t length, uws_opcode_t opcode);
uws_sendstatus_t uws_ws_send_with_options(int ssl, uws_websocket_t *ws,
                                          const char *message, size_t length,
                                          uws_opcode_t opcode, bool compress,
                                          bool fin);
uws_sendstatus_t uws_ws_send_fragment(int ssl, uws_websocket_t *ws,
                                      const char *message, size_t length,
                                      bool compress);
uws_sendstatus_t uws_ws_send_first_fragment(int ssl, uws_websocket_t *ws,
                                            const char *message, size_t length,
                                            bool compress);
uws_sendstatus_t
uws_ws_send_first_fragment_with_opcode(int ssl, uws_websocket_t *ws,
                                       const char *message, size_t length,
                                       uws_opcode_t opcode, bool compress);
uws_sendstatus_t uws_ws_send_last_fragment(int ssl, uws_websocket_t *ws,
                                           const char *message, size_t length,
                                           bool compress);
void uws_ws_end(int ssl, uws_websocket_t *ws, int code, const char *message,
                size_t length);
void uws_ws_cork(int ssl, uws_websocket_t *ws, void (*handler)(void *user_data),
                 void *user_data);
bool uws_ws_subscribe(int ssl, uws_websocket_t *ws, const char *topic,
                      size_t length);
bool uws_ws_unsubscribe(int ssl, uws_websocket_t *ws, const char *topic,
                        size_t length);
bool uws_ws_is_subscribed(int ssl, uws_websocket_t *ws, const char *topic,
                          size_t length);
void uws_ws_iterate_topics(int ssl, uws_websocket_t *ws,
                           void (*callback)(const char *topic, size_t length,
                                            void *user_data),
                           void *user_data);
bool uws_ws_publish(int ssl, uws_websocket_t *ws, const char *topic,
                    size_t topic_length, const char *message,
                    size_t message_length);
bool uws_ws_publish_with_options(int ssl, uws_websocket_t *ws,
                                 const char *topic, size_t topic_length,
                                 const char *message, size_t message_length,
                                 uws_opcode_t opcode, bool compress);
unsigned int uws_ws_get_buffered_amount(int ssl, uws_websocket_t *ws);
size_t uws_ws_get_remote_address(int ssl, uws_websocket_t *ws,
                                 const char **dest);
size_t uws_ws_get_remote_address_as_text(int ssl, uws_websocket_t *ws,
                                         const char **dest);

// Response
void uws_res_end(int ssl, uws_res_t *res, const char *data, size_t length,
                 bool close_connection);
void uws_res_pause(int ssl, uws_res_t *res);
void uws_res_resume(int ssl, uws_res_t *res);
void uws_res_write_continue(int ssl, uws_res_t *res);
void uws_res_write_status(int ssl, uws_res_t *res, const char *status,
                          size_t length);
void uws_res_write_header(int ssl, uws_res_t *res, const char *key,
                          size_t key_length, const char *value,
                          size_t value_length);

void uws_res_write_header_int(int ssl, uws_res_t *res, const char *key,
                              size_t key_length, uint64_t value);
void uws_res_end_without_body(int ssl, uws_res_t *res);
bool uws_res_write(int ssl, uws_res_t *res, const char *data, size_t length);
uintmax_t uws_res_get_write_offset(int ssl, uws_res_t *res);
bool uws_res_has_responded(int ssl, uws_res_t *res);
void uws_res_on_writable(int ssl, uws_res_t *res,
                         bool (*handler)(uws_res_t *res, uintmax_t,
                                         void *opcional_data),
                         void *user_data);
void uws_res_on_aborted(int ssl, uws_res_t *res,
                        void (*handler)(uws_res_t *res, void *opcional_data),
                        void *opcional_data);
void uws_res_on_data(int ssl, uws_res_t *res,
                     void (*handler)(uws_res_t *res, const char *chunk,
                                     size_t chunk_length, bool is_end,
                                     void *opcional_data),
                     void *opcional_data);
void uws_res_upgrade(int ssl, uws_res_t *res, void *data,
                     const char *sec_web_socket_key,
                     size_t sec_web_socket_key_length,
                     const char *sec_web_socket_protocol,
                     size_t sec_web_socket_protocol_length,
                     const char *sec_web_socket_extensions,
                     size_t sec_web_socket_extensions_length,
                     uws_socket_context_t *ws);

// Request
bool uws_req_is_ancient(uws_req_t *res);
bool uws_req_get_yield(uws_req_t *res);
void uws_req_set_field(uws_req_t *res, bool yield);
size_t uws_req_get_url(uws_req_t *res, const char **dest);
size_t uws_req_get_method(uws_req_t *res, const char **dest);
size_t uws_req_get_header(uws_req_t *res, const char *lower_case_header,
                          size_t lower_case_header_length, const char **dest);
size_t uws_req_get_query(uws_req_t *res, const char *key, size_t key_length,
                         const char **dest);
size_t uws_req_get_parameter(uws_req_t *res, unsigned short index,
                             const char **dest);

struct us_loop_t *uws_get_loop();

void uws_loop_addPostHandler(us_loop_t *loop, void *ctx_,
                             void (*cb)(void *ctx, us_loop_t *loop));
void uws_loop_removePostHandler(us_loop_t *loop, void *key);
void uws_loop_addPreHandler(us_loop_t *loop, void *key,
                            void (*cb)(void *ctx, us_loop_t *loop));
void uws_loop_removePreHandler(us_loop_t *loop, void *ctx_);
void uws_loop_defer(us_loop_t *loop, void *ctx, void (*cb)(void *ctx));

typedef struct StringPointer {
  uint32_t off;
  uint32_t len;
} StringPointer;

void uws_res_write_headers(int ssl, uws_res_t *res, const StringPointer *names,
                           const StringPointer *values, size_t count,
                           const char *buf);

#ifdef __cplusplus
}
#endif

#endif