lwsws conf.c migrate to lib
[platform/upstream/libwebsockets.git] / lib / libwebsockets.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
23 #define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
24
25 #ifdef __cplusplus
26 #include <cstddef>
27 #include <cstdarg>
28 #ifdef MBED_OPERATORS
29 #include "mbed-drivers/mbed.h"
30 #include "sal-iface-eth/EthernetInterface.h"
31 #include "sockets/TCPListener.h"
32 #include "sal-stack-lwip/lwipv4_init.h"
33
34 namespace {
35 }
36 using namespace mbed::Sockets::v0;
37
38
39 struct sockaddr_in;
40 struct lws;
41
42 class lws_conn {
43         public:
44         lws_conn():
45                 ts(NULL),
46                 wsi(NULL),
47                 writeable(1),
48                 awaiting_on_writeable(0)
49         {
50         }
51
52 public:
53         void set_wsi(struct lws *_wsi) { wsi = _wsi; }
54         int actual_onRX(Socket *s);
55         void onRX(Socket *s);
56         void onError(Socket *s, socket_error_t err);
57         void onDisconnect(TCPStream *s);
58         void onSent(Socket *s, uint16_t len);
59         void serialized_writeable(struct lws *wsi);
60
61 public:
62         TCPStream *ts;
63
64 public:
65         struct lws *wsi;
66         char writeable;
67         char awaiting_on_writeable;
68 };
69
70 class lws_conn_listener : lws_conn {
71 public:
72         lws_conn_listener():
73                 srv(SOCKET_STACK_LWIP_IPV4)
74         {
75                 srv.setOnError(TCPStream::ErrorHandler_t(this,
76                                 &lws_conn_listener::onError));
77         }
78
79         void start(const uint16_t port);
80
81 protected:
82         void onRX(Socket *s);
83         void onError(Socket *s, socket_error_t err);
84         void onIncoming(TCPListener *s, void *impl);
85         void onDisconnect(TCPStream *s);
86
87 public:
88         TCPListener srv;
89 };
90
91 #endif
92
93 extern "C" {
94 #else
95 #include <stdarg.h>
96 #endif
97
98 #ifdef MBED_OPERATORS
99 struct sockaddr_in;
100 #define LWS_POSIX 0
101 #else
102 #define LWS_POSIX 1
103 #endif
104
105 #include "lws_config.h"
106
107 #if defined(WIN32) || defined(_WIN32)
108 #ifndef WIN32_LEAN_AND_MEAN
109 #define WIN32_LEAN_AND_MEAN
110 #endif
111
112 #include <winsock2.h>
113 #include <ws2tcpip.h>
114 #include <stddef.h>
115 #include <basetsd.h>
116 #ifndef _WIN32_WCE
117 #include <stdint.h>
118 #include <fcntl.h>
119 #else
120 #define _O_RDONLY       0x0000
121 #define O_RDONLY        _O_RDONLY
122 #endif
123
124 // Visual studio older than 2015 and WIN_CE has only _stricmp
125 #if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(_WIN32_WCE)
126 #define strcasecmp _stricmp
127 #else
128 #define strcasecmp stricmp
129 #endif
130 #define getdtablesize() 30000
131
132 #define LWS_INLINE __inline
133 #define LWS_VISIBLE
134 #define LWS_WARN_UNUSED_RESULT
135 #define LWS_WARN_DEPRECATED
136
137 #ifdef LWS_DLL
138 #ifdef LWS_INTERNAL
139 #define LWS_EXTERN extern __declspec(dllexport)
140 #else
141 #define LWS_EXTERN extern __declspec(dllimport)
142 #endif
143 #else
144 #define LWS_EXTERN
145 #endif
146
147 #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
148 #define LWS_O_RDONLY _O_RDONLY
149
150 #if !defined(_MSC_VER) || _MSC_VER < 1900 /* Visual Studio 2015 already defines this in <stdio.h> */
151 #define snprintf _snprintf
152 #endif
153
154 #ifndef __func__
155 #define __func__ __FUNCTION__
156 #endif
157
158 #else /* NOT WIN32 */
159 #include <unistd.h>
160
161 #if defined(__NetBSD__) || defined(__FreeBSD__)
162 #include <netinet/in.h>
163 #endif
164
165 #define LWS_INLINE inline
166 #define LWS_O_RDONLY O_RDONLY
167
168 #ifndef MBED_OPERATORS
169 #include <poll.h>
170 #include <netdb.h>
171 #define LWS_INVALID_FILE -1
172 #else
173 #define getdtablesize() (20)
174 #define LWS_INVALID_FILE NULL
175 #endif
176
177 #if defined(__GNUC__)
178
179 /* warn_unused_result attribute only supported by GCC 3.4 or later */
180 #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
181 #define LWS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
182 #else
183 #define LWS_WARN_UNUSED_RESULT
184 #endif
185
186 #define LWS_VISIBLE __attribute__((visibility("default")))
187 #define LWS_WARN_DEPRECATED __attribute__ ((deprecated))
188 #else
189 #define LWS_VISIBLE
190 #define LWS_WARN_UNUSED_RESULT
191 #define LWS_WARN_DEPRECATED
192 #endif
193
194 #if defined(__ANDROID__)
195 #include <unistd.h>
196 #define getdtablesize() sysconf(_SC_OPEN_MAX)
197 #endif
198
199 #endif
200
201 #ifdef LWS_USE_LIBEV
202 #include <ev.h>
203 #endif /* LWS_USE_LIBEV */
204 #ifdef LWS_USE_LIBUV
205 #include <uv.h>
206 #endif /* LWS_USE_LIBUV */
207
208 #ifndef LWS_EXTERN
209 #define LWS_EXTERN extern
210 #endif
211
212 #ifdef _WIN32
213 #define random rand
214 #else
215 #include <sys/time.h>
216 #include <unistd.h>
217 #endif
218
219 #ifdef LWS_OPENSSL_SUPPORT
220
221 #ifdef USE_WOLFSSL
222 #ifdef USE_OLD_CYASSL
223 #include <cyassl/openssl/ssl.h>
224 #include <cyassl/error-ssl.h>
225 #else
226 #include <wolfssl/openssl/ssl.h>
227 #include <wolfssl/error-ssl.h>
228 #endif /* not USE_OLD_CYASSL */
229 #else
230 #if defined(LWS_USE_POLARSSL)
231 #include <polarssl/ssl.h>
232 struct lws_polarssl_context {
233         x509_crt ca;
234         x509_crt certificate;
235         rsa_context key;
236 };
237 typedef struct lws_polarssl_context SSL_CTX;
238 typedef ssl_context SSL;
239 #else
240 #if defined(LWS_USE_MBEDTLS)
241 #include <mbedtls/ssl.h>
242 #else
243 #include <openssl/ssl.h>
244 #include <openssl/err.h>
245 #endif /* not USE_MBEDTLS */
246 #endif /* not USE_POLARSSL */
247 #endif /* not USE_WOLFSSL */
248 #endif
249
250
251 #define CONTEXT_PORT_NO_LISTEN -1
252
253 enum lws_log_levels {
254         LLL_ERR = 1 << 0,
255         LLL_WARN = 1 << 1,
256         LLL_NOTICE = 1 << 2,
257         LLL_INFO = 1 << 3,
258         LLL_DEBUG = 1 << 4,
259         LLL_PARSER = 1 << 5,
260         LLL_HEADER = 1 << 6,
261         LLL_EXT = 1 << 7,
262         LLL_CLIENT = 1 << 8,
263         LLL_LATENCY = 1 << 9,
264
265         LLL_COUNT = 10 /* set to count of valid flags */
266 };
267
268 LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...);
269 LWS_VISIBLE LWS_EXTERN void _lws_logv(int filter, const char *format, va_list vl);
270 LWS_VISIBLE LWS_EXTERN int
271 lwsl_timestamp(int level, char *p, int len);
272
273 /* notice, warn and log are always compiled in */
274 #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
275 #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
276 #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
277 /*
278  *  weaker logging can be deselected at configure time using --disable-debug
279  *  that gets rid of the overhead of checking while keeping _warn and _err
280  *  active
281  */
282 #ifdef _DEBUG
283
284 #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
285 #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
286 #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
287 #define lwsl_header(...)  _lws_log(LLL_HEADER, __VA_ARGS__)
288 #define lwsl_ext(...)  _lws_log(LLL_EXT, __VA_ARGS__)
289 #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
290 #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
291 LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
292
293 #else /* no debug */
294
295 #define lwsl_info(...) {}
296 #define lwsl_debug(...) {}
297 #define lwsl_parser(...) {}
298 #define lwsl_header(...) {}
299 #define lwsl_ext(...) {}
300 #define lwsl_client(...) {}
301 #define lwsl_latency(...) {}
302 #define lwsl_hexdump(a, b)
303
304 #endif
305
306 #include <stddef.h>
307
308 #ifndef lws_container_of
309 #define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
310 #endif
311
312
313 struct lws;
314 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
315
316 /* api change list for user code to test against */
317
318 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
319
320 /* the struct lws_protocols has the id field present */
321 #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
322
323 /* you can call lws_get_peer_write_allowance */
324 #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
325
326 /* extra parameter introduced in 917f43ab821 */
327 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
328
329 /* File operations stuff exists */
330 #define LWS_FEATURE_FOPS
331
332 /*
333  * NOTE: These public enums are part of the abi.  If you want to add one,
334  * add it at where specified so existing users are unaffected.
335  */
336 enum lws_context_options {
337         LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT     = (1 << 1) |
338                                                                   (1 << 12),
339         LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME            = (1 << 2),
340         LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT             = (1 << 3) |
341                                                                   (1 << 12),
342         LWS_SERVER_OPTION_LIBEV                                 = (1 << 4),
343         LWS_SERVER_OPTION_DISABLE_IPV6                          = (1 << 5),
344         LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS                   = (1 << 6),
345         LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED                = (1 << 7),
346         LWS_SERVER_OPTION_VALIDATE_UTF8                         = (1 << 8),
347         LWS_SERVER_OPTION_SSL_ECDH                              = (1 << 9) |
348                                                                   (1 << 12),
349         LWS_SERVER_OPTION_LIBUV                                 = (1 << 10),
350         LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS                = (1 << 11) |
351                                                                   (1 << 3) |
352                                                                   (1 << 12),
353         LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT                    = (1 << 12),
354         LWS_SERVER_OPTION_EXPLICIT_VHOSTS                       = (1 << 13),
355         LWS_SERVER_OPTION_UNIX_SOCK                             = (1 << 14),
356         LWS_SERVER_OPTION_STS                                   = (1 << 15),
357
358         /****** add new things just above ---^ ******/
359 };
360
361 #define lws_check_opt(c, f) (((c) & (f)) == (f))
362
363 /*
364  * NOTE: These public enums are part of the abi.  If you want to add one,
365  * add it at where specified so existing users are unaffected.
366  */
367 enum lws_callback_reasons {
368         LWS_CALLBACK_ESTABLISHED                                =  0,
369         LWS_CALLBACK_CLIENT_CONNECTION_ERROR                    =  1,
370         LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH                =  2,
371         LWS_CALLBACK_CLIENT_ESTABLISHED                         =  3,
372         LWS_CALLBACK_CLOSED                                     =  4,
373         LWS_CALLBACK_CLOSED_HTTP                                =  5,
374         LWS_CALLBACK_RECEIVE                                    =  6,
375         LWS_CALLBACK_RECEIVE_PONG                               =  7,
376         LWS_CALLBACK_CLIENT_RECEIVE                             =  8,
377         LWS_CALLBACK_CLIENT_RECEIVE_PONG                        =  9,
378         LWS_CALLBACK_CLIENT_WRITEABLE                           = 10,
379         LWS_CALLBACK_SERVER_WRITEABLE                           = 11,
380         LWS_CALLBACK_HTTP                                       = 12,
381         LWS_CALLBACK_HTTP_BODY                                  = 13,
382         LWS_CALLBACK_HTTP_BODY_COMPLETION                       = 14,
383         LWS_CALLBACK_HTTP_FILE_COMPLETION                       = 15,
384         LWS_CALLBACK_HTTP_WRITEABLE                             = 16,
385         LWS_CALLBACK_FILTER_NETWORK_CONNECTION                  = 17,
386         LWS_CALLBACK_FILTER_HTTP_CONNECTION                     = 18,
387         LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED             = 19,
388         LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION                 = 20,
389         LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS     = 21,
390         LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS     = 22,
391         LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION   = 23,
392         LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER             = 24,
393         LWS_CALLBACK_CONFIRM_EXTENSION_OKAY                     = 25,
394         LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED         = 26,
395         LWS_CALLBACK_PROTOCOL_INIT                              = 27,
396         LWS_CALLBACK_PROTOCOL_DESTROY                           = 28,
397         LWS_CALLBACK_WSI_CREATE /* always protocol[0] */        = 29,
398         LWS_CALLBACK_WSI_DESTROY /* always protocol[0] */       = 30,
399         LWS_CALLBACK_GET_THREAD_ID                              = 31,
400
401         /* external poll() management support */
402         LWS_CALLBACK_ADD_POLL_FD                                = 32,
403         LWS_CALLBACK_DEL_POLL_FD                                = 33,
404         LWS_CALLBACK_CHANGE_MODE_POLL_FD                        = 34,
405         LWS_CALLBACK_LOCK_POLL                                  = 35,
406         LWS_CALLBACK_UNLOCK_POLL                                = 36,
407
408         LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY       = 37,
409         LWS_CALLBACK_WS_PEER_INITIATED_CLOSE                    = 38,
410
411         LWS_CALLBACK_WS_EXT_DEFAULTS                            = 39,
412
413         LWS_CALLBACK_CGI                                        = 40,
414         LWS_CALLBACK_CGI_TERMINATED                             = 41,
415         LWS_CALLBACK_CGI_STDIN_DATA                             = 42,
416         LWS_CALLBACK_CGI_STDIN_COMPLETED                        = 43,
417         LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP                    = 44,
418         LWS_CALLBACK_CLOSED_CLIENT_HTTP                         = 45,
419         LWS_CALLBACK_RECEIVE_CLIENT_HTTP                        = 46,
420         LWS_CALLBACK_COMPLETED_CLIENT_HTTP                      = 47,
421         LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ                   = 48,
422
423         /****** add new things just above ---^ ******/
424
425         LWS_CALLBACK_USER = 1000, /* user code can use any including / above */
426 };
427
428
429 #if defined(_WIN32)
430 typedef SOCKET lws_sockfd_type;
431 typedef HANDLE lws_filefd_type;
432 #define lws_sockfd_valid(sfd) (!!sfd)
433 struct lws_pollfd {
434         lws_sockfd_type fd;
435         SHORT events;
436         SHORT revents;
437 };
438 #else
439
440 #if defined(MBED_OPERATORS)
441 /* it's a class lws_conn * */
442 typedef void * lws_sockfd_type;
443 typedef void * lws_filefd_type;
444 #define lws_sockfd_valid(sfd) (!!sfd)
445 struct pollfd {
446         lws_sockfd_type fd;
447         short events;
448         short revents;
449 };
450 #define POLLIN          0x0001
451 #define POLLPRI         0x0002
452 #define POLLOUT         0x0004
453 #define POLLERR         0x0008
454 #define POLLHUP         0x0010
455 #define POLLNVAL        0x0020
456
457 struct lws;
458
459 void * mbed3_create_tcp_stream_socket(void);
460 void mbed3_delete_tcp_stream_socket(void *sockfd);
461 void mbed3_tcp_stream_bind(void *sock, int port, struct lws *);
462 void mbed3_tcp_stream_accept(void *sock, struct lws *);
463 #else
464 typedef int lws_sockfd_type;
465 typedef int lws_filefd_type;
466 #define lws_sockfd_valid(sfd) (sfd >= 0)
467 #endif
468
469 #define lws_pollfd pollfd
470 #endif
471
472 /* argument structure for all external poll related calls
473  * passed in via 'in'
474  */
475 struct lws_pollargs {
476         lws_sockfd_type fd;             /* applicable socket descriptor */
477         int events;                     /* the new event mask */
478         int prev_events;                /* the previous event mask */
479 };
480
481 /**
482  * struct lws_plat_file_ops - Platform-specific file operations
483  *
484  * These provide platform-agnostic ways to deal with filesystem access in the
485  * library and in the user code.
486  *
487  * @open:               Open file (always binary access if plat supports it)
488  *                       filelen is filled on exit to be the length of the file
489  *                       flags should be set to O_RDONLY or O_RDWR
490  * @close:              Close file
491  * @seek_cur:           Seek from current position
492  * @read:               Read fron file *amount is set on exit to amount read
493  * @write:              Write to file *amount is set on exit as amount written
494  */
495 struct lws_plat_file_ops {
496         lws_filefd_type (*open)(struct lws *wsi, const char *filename,
497                                 unsigned long *filelen, int flags);
498         int (*close)(struct lws *wsi, lws_filefd_type fd);
499         unsigned long (*seek_cur)(struct lws *wsi, lws_filefd_type fd,
500                                   long offset_from_cur_pos);
501         int (*read)(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
502                     unsigned char *buf, unsigned long len);
503         int (*write)(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
504                      unsigned char *buf, unsigned long len);
505
506         /* Add new things just above here ---^
507          * This is part of the ABI, don't needlessly break compatibility */
508 };
509
510 /*
511  * NOTE: These public enums are part of the abi.  If you want to add one,
512  * add it at where specified so existing users are unaffected.
513  */
514 enum lws_extension_callback_reasons {
515         LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT             =  0,
516         LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT             =  1,
517         LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT              =  2,
518         LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT              =  3,
519         LWS_EXT_CB_CONSTRUCT                            =  4,
520         LWS_EXT_CB_CLIENT_CONSTRUCT                     =  5,
521         LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE             =  6,
522         LWS_EXT_CB_CHECK_OK_TO_PROPOSE_EXTENSION        =  7,
523         LWS_EXT_CB_DESTROY                              =  8,
524         LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING              =  9,
525         LWS_EXT_CB_ANY_WSI_ESTABLISHED                  = 10,
526         LWS_EXT_CB_PACKET_RX_PREPARSE                   = 11,
527         LWS_EXT_CB_PACKET_TX_PRESEND                    = 12,
528         LWS_EXT_CB_PACKET_TX_DO_SEND                    = 13,
529         LWS_EXT_CB_HANDSHAKE_REPLY_TX                   = 14,
530         LWS_EXT_CB_FLUSH_PENDING_TX                     = 15,
531         LWS_EXT_CB_EXTENDED_PAYLOAD_RX                  = 16,
532         LWS_EXT_CB_CAN_PROXY_CLIENT_CONNECTION          = 17,
533         LWS_EXT_CB_1HZ                                  = 18,
534         LWS_EXT_CB_REQUEST_ON_WRITEABLE                 = 19,
535         LWS_EXT_CB_IS_WRITEABLE                         = 20,
536         LWS_EXT_CB_PAYLOAD_TX                           = 21,
537         LWS_EXT_CB_PAYLOAD_RX                           = 22,
538         LWS_EXT_CB_OPTION_DEFAULT                       = 23,
539         LWS_EXT_CB_OPTION_SET                           = 24,
540         LWS_EXT_CB_OPTION_CONFIRM                       = 25,
541         LWS_EXT_CB_NAMED_OPTION_SET                     = 26,
542
543         /****** add new things just above ---^ ******/
544 };
545
546 /*
547  * NOTE: These public enums are part of the abi.  If you want to add one,
548  * add it at where specified so existing users are unaffected.
549  */
550 enum lws_write_protocol {
551         LWS_WRITE_TEXT                                          = 0,
552         LWS_WRITE_BINARY                                        = 1,
553         LWS_WRITE_CONTINUATION                                  = 2,
554         LWS_WRITE_HTTP                                          = 3,
555
556         /* special 04+ opcodes */
557
558         /* LWS_WRITE_CLOSE is handled by lws_close_reason() */
559         LWS_WRITE_PING                                          = 5,
560         LWS_WRITE_PONG                                          = 6,
561
562         /* Same as write_http but we know this write ends the transaction */
563         LWS_WRITE_HTTP_FINAL                                    = 7,
564
565         /* HTTP2 */
566
567         LWS_WRITE_HTTP_HEADERS                                  = 8,
568
569         /****** add new things just above ---^ ******/
570
571         /* flags */
572
573         LWS_WRITE_NO_FIN = 0x40,
574         /*
575          * client packet payload goes out on wire unmunged
576          * only useful for security tests since normal servers cannot
577          * decode the content if used
578          */
579         LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
580 };
581
582 /*
583  * you need these to look at headers that have been parsed if using the
584  * LWS_CALLBACK_FILTER_CONNECTION callback.  If a header from the enum
585  * list below is absent, .token = NULL and token_len = 0.  Otherwise .token
586  * points to .token_len chars containing that header content.
587  */
588
589 struct lws_tokens {
590         char *token;
591         int token_len;
592 };
593
594 /*
595  * these have to be kept in sync with lextable.h / minilex.c
596  *
597  * NOTE: These public enums are part of the abi.  If you want to add one,
598  * add it at where specified so existing users are unaffected.
599  */
600 enum lws_token_indexes {
601         WSI_TOKEN_GET_URI                                       =  0,
602         WSI_TOKEN_POST_URI                                      =  1,
603         WSI_TOKEN_OPTIONS_URI                                   =  2,
604         WSI_TOKEN_HOST                                          =  3,
605         WSI_TOKEN_CONNECTION                                    =  4,
606         WSI_TOKEN_UPGRADE                                       =  5,
607         WSI_TOKEN_ORIGIN                                        =  6,
608         WSI_TOKEN_DRAFT                                         =  7,
609         WSI_TOKEN_CHALLENGE                                     =  8,
610         WSI_TOKEN_EXTENSIONS                                    =  9,
611         WSI_TOKEN_KEY1                                          = 10,
612         WSI_TOKEN_KEY2                                          = 11,
613         WSI_TOKEN_PROTOCOL                                      = 12,
614         WSI_TOKEN_ACCEPT                                        = 13,
615         WSI_TOKEN_NONCE                                         = 14,
616         WSI_TOKEN_HTTP                                          = 15,
617         WSI_TOKEN_HTTP2_SETTINGS                                = 16,
618         WSI_TOKEN_HTTP_ACCEPT                                   = 17,
619         WSI_TOKEN_HTTP_AC_REQUEST_HEADERS                       = 18,
620         WSI_TOKEN_HTTP_IF_MODIFIED_SINCE                        = 19,
621         WSI_TOKEN_HTTP_IF_NONE_MATCH                            = 20,
622         WSI_TOKEN_HTTP_ACCEPT_ENCODING                          = 21,
623         WSI_TOKEN_HTTP_ACCEPT_LANGUAGE                          = 22,
624         WSI_TOKEN_HTTP_PRAGMA                                   = 23,
625         WSI_TOKEN_HTTP_CACHE_CONTROL                            = 24,
626         WSI_TOKEN_HTTP_AUTHORIZATION                            = 25,
627         WSI_TOKEN_HTTP_COOKIE                                   = 26,
628         WSI_TOKEN_HTTP_CONTENT_LENGTH                           = 27,
629         WSI_TOKEN_HTTP_CONTENT_TYPE                             = 28,
630         WSI_TOKEN_HTTP_DATE                                     = 29,
631         WSI_TOKEN_HTTP_RANGE                                    = 30,
632         WSI_TOKEN_HTTP_REFERER                                  = 31,
633         WSI_TOKEN_KEY                                           = 32,
634         WSI_TOKEN_VERSION                                       = 33,
635         WSI_TOKEN_SWORIGIN                                      = 34,
636
637         WSI_TOKEN_HTTP_COLON_AUTHORITY                          = 35,
638         WSI_TOKEN_HTTP_COLON_METHOD                             = 36,
639         WSI_TOKEN_HTTP_COLON_PATH                               = 37,
640         WSI_TOKEN_HTTP_COLON_SCHEME                             = 38,
641         WSI_TOKEN_HTTP_COLON_STATUS                             = 39,
642
643         WSI_TOKEN_HTTP_ACCEPT_CHARSET                           = 40,
644         WSI_TOKEN_HTTP_ACCEPT_RANGES                            = 41,
645         WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN              = 42,
646         WSI_TOKEN_HTTP_AGE                                      = 43,
647         WSI_TOKEN_HTTP_ALLOW                                    = 44,
648         WSI_TOKEN_HTTP_CONTENT_DISPOSITION                      = 45,
649         WSI_TOKEN_HTTP_CONTENT_ENCODING                         = 46,
650         WSI_TOKEN_HTTP_CONTENT_LANGUAGE                         = 47,
651         WSI_TOKEN_HTTP_CONTENT_LOCATION                         = 48,
652         WSI_TOKEN_HTTP_CONTENT_RANGE                            = 49,
653         WSI_TOKEN_HTTP_ETAG                                     = 50,
654         WSI_TOKEN_HTTP_EXPECT                                   = 51,
655         WSI_TOKEN_HTTP_EXPIRES                                  = 52,
656         WSI_TOKEN_HTTP_FROM                                     = 53,
657         WSI_TOKEN_HTTP_IF_MATCH                                 = 54,
658         WSI_TOKEN_HTTP_IF_RANGE                                 = 55,
659         WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE                      = 56,
660         WSI_TOKEN_HTTP_LAST_MODIFIED                            = 57,
661         WSI_TOKEN_HTTP_LINK                                     = 58,
662         WSI_TOKEN_HTTP_LOCATION                                 = 59,
663         WSI_TOKEN_HTTP_MAX_FORWARDS                             = 60,
664         WSI_TOKEN_HTTP_PROXY_AUTHENTICATE                       = 61,
665         WSI_TOKEN_HTTP_PROXY_AUTHORIZATION                      = 62,
666         WSI_TOKEN_HTTP_REFRESH                                  = 63,
667         WSI_TOKEN_HTTP_RETRY_AFTER                              = 64,
668         WSI_TOKEN_HTTP_SERVER                                   = 65,
669         WSI_TOKEN_HTTP_SET_COOKIE                               = 66,
670         WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY                = 67,
671         WSI_TOKEN_HTTP_TRANSFER_ENCODING                        = 68,
672         WSI_TOKEN_HTTP_USER_AGENT                               = 69,
673         WSI_TOKEN_HTTP_VARY                                     = 70,
674         WSI_TOKEN_HTTP_VIA                                      = 71,
675         WSI_TOKEN_HTTP_WWW_AUTHENTICATE                         = 72,
676
677         WSI_TOKEN_PATCH_URI                                     = 73,
678         WSI_TOKEN_PUT_URI                                       = 74,
679         WSI_TOKEN_DELETE_URI                                    = 75,
680
681         WSI_TOKEN_HTTP_URI_ARGS                                 = 76,
682         WSI_TOKEN_PROXY                                         = 77,
683         WSI_TOKEN_HTTP_X_REAL_IP                                = 78,
684         WSI_TOKEN_HTTP1_0                                       = 79,
685
686         /****** add new things just above ---^ ******/
687
688         /* use token storage to stash these internally, not for
689          * user use */
690
691         _WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
692         _WSI_TOKEN_CLIENT_PEER_ADDRESS,
693         _WSI_TOKEN_CLIENT_URI,
694         _WSI_TOKEN_CLIENT_HOST,
695         _WSI_TOKEN_CLIENT_ORIGIN,
696         _WSI_TOKEN_CLIENT_METHOD,
697
698         /* always last real token index*/
699         WSI_TOKEN_COUNT,
700
701         /* parser state additions, no storage associated */
702         WSI_TOKEN_NAME_PART,
703         WSI_TOKEN_SKIPPING,
704         WSI_TOKEN_SKIPPING_SAW_CR,
705         WSI_PARSING_COMPLETE,
706         WSI_INIT_TOKEN_MUXURL,
707 };
708
709 struct lws_token_limits {
710         unsigned short token_limit[WSI_TOKEN_COUNT];
711 };
712
713 /*
714  * From RFC 6455
715    1000
716
717       1000 indicates a normal closure, meaning that the purpose for
718       which the connection was established has been fulfilled.
719
720    1001
721
722       1001 indicates that an endpoint is "going away", such as a server
723       going down or a browser having navigated away from a page.
724
725    1002
726
727       1002 indicates that an endpoint is terminating the connection due
728       to a protocol error.
729
730    1003
731
732       1003 indicates that an endpoint is terminating the connection
733       because it has received a type of data it cannot accept (e.g., an
734       endpoint that understands only text data MAY send this if it
735       receives a binary message).
736
737    1004
738
739       Reserved.  The specific meaning might be defined in the future.
740
741    1005
742
743       1005 is a reserved value and MUST NOT be set as a status code in a
744       Close control frame by an endpoint.  It is designated for use in
745       applications expecting a status code to indicate that no status
746       code was actually present.
747
748    1006
749
750       1006 is a reserved value and MUST NOT be set as a status code in a
751       Close control frame by an endpoint.  It is designated for use in
752       applications expecting a status code to indicate that the
753       connection was closed abnormally, e.g., without sending or
754       receiving a Close control frame.
755
756    1007
757
758       1007 indicates that an endpoint is terminating the connection
759       because it has received data within a message that was not
760       consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
761       data within a text message).
762
763    1008
764
765       1008 indicates that an endpoint is terminating the connection
766       because it has received a message that violates its policy.  This
767       is a generic status code that can be returned when there is no
768       other more suitable status code (e.g., 1003 or 1009) or if there
769       is a need to hide specific details about the policy.
770
771    1009
772
773       1009 indicates that an endpoint is terminating the connection
774       because it has received a message that is too big for it to
775       process.
776
777    1010
778
779       1010 indicates that an endpoint (client) is terminating the
780       connection because it has expected the server to negotiate one or
781       more extension, but the server didn't return them in the response
782       message of the WebSocket handshake.  The list of extensions that
783       are needed SHOULD appear in the /reason/ part of the Close frame.
784       Note that this status code is not used by the server, because it
785       can fail the WebSocket handshake instead.
786
787    1011
788
789       1011 indicates that a server is terminating the connection because
790       it encountered an unexpected condition that prevented it from
791       fulfilling the request.
792
793    1015
794
795       1015 is a reserved value and MUST NOT be set as a status code in a
796       Close control frame by an endpoint.  It is designated for use in
797       applications expecting a status code to indicate that the
798       connection was closed due to a failure to perform a TLS handshake
799       (e.g., the server certificate can't be verified).
800 */
801
802 /*
803  * NOTE: These public enums are part of the abi.  If you want to add one,
804  * add it at where specified so existing users are unaffected.
805  */
806 enum lws_close_status {
807         LWS_CLOSE_STATUS_NOSTATUS                               =    0,
808         LWS_CLOSE_STATUS_NORMAL                                 = 1000,
809         LWS_CLOSE_STATUS_GOINGAWAY                              = 1001,
810         LWS_CLOSE_STATUS_PROTOCOL_ERR                           = 1002,
811         LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE                    = 1003,
812         LWS_CLOSE_STATUS_RESERVED                               = 1004,
813         LWS_CLOSE_STATUS_NO_STATUS                              = 1005,
814         LWS_CLOSE_STATUS_ABNORMAL_CLOSE                         = 1006,
815         LWS_CLOSE_STATUS_INVALID_PAYLOAD                        = 1007,
816         LWS_CLOSE_STATUS_POLICY_VIOLATION                       = 1008,
817         LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE                      = 1009,
818         LWS_CLOSE_STATUS_EXTENSION_REQUIRED                     = 1010,
819         LWS_CLOSE_STATUS_UNEXPECTED_CONDITION                   = 1011,
820         LWS_CLOSE_STATUS_TLS_FAILURE                            = 1015,
821
822         /****** add new things just above ---^ ******/
823
824         LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY               = 9999,
825 };
826
827 enum http_status {
828         HTTP_STATUS_OK                                          = 200,
829         HTTP_STATUS_NO_CONTENT                                  = 204,
830
831         HTTP_STATUS_MOVED_PERMANENTLY                           = 301,
832         HTTP_STATUS_FOUND                                       = 302,
833         HTTP_STATUS_SEE_OTHER                                   = 303,
834
835         HTTP_STATUS_BAD_REQUEST                                 = 400,
836         HTTP_STATUS_UNAUTHORIZED,
837         HTTP_STATUS_PAYMENT_REQUIRED,
838         HTTP_STATUS_FORBIDDEN,
839         HTTP_STATUS_NOT_FOUND,
840         HTTP_STATUS_METHOD_NOT_ALLOWED,
841         HTTP_STATUS_NOT_ACCEPTABLE,
842         HTTP_STATUS_PROXY_AUTH_REQUIRED,
843         HTTP_STATUS_REQUEST_TIMEOUT,
844         HTTP_STATUS_CONFLICT,
845         HTTP_STATUS_GONE,
846         HTTP_STATUS_LENGTH_REQUIRED,
847         HTTP_STATUS_PRECONDITION_FAILED,
848         HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
849         HTTP_STATUS_REQ_URI_TOO_LONG,
850         HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
851         HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
852         HTTP_STATUS_EXPECTATION_FAILED,
853
854         HTTP_STATUS_INTERNAL_SERVER_ERROR                       = 500,
855         HTTP_STATUS_NOT_IMPLEMENTED,
856         HTTP_STATUS_BAD_GATEWAY,
857         HTTP_STATUS_SERVICE_UNAVAILABLE,
858         HTTP_STATUS_GATEWAY_TIMEOUT,
859         HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
860 };
861
862 struct lws;
863 struct lws_context;
864 /* needed even with extensions disabled for create context */
865 struct lws_extension;
866
867 /**
868  * typedef lws_callback_function() - User server actions
869  * @wsi:        Opaque websocket instance pointer
870  * @reason:     The reason for the call
871  * @user:       Pointer to per-session user data allocated by library
872  * @in:         Pointer used for some callback reasons
873  * @len:        Length set for some callback reasons
874  *
875  *      This callback is the way the user controls what is served.  All the
876  *      protocol detail is hidden and handled by the library.
877  *
878  *      For each connection / session there is user data allocated that is
879  *      pointed to by "user".  You set the size of this user data area when
880  *      the library is initialized with lws_create_server.
881  *
882  *      You get an opportunity to initialize user data when called back with
883  *      LWS_CALLBACK_ESTABLISHED reason.
884  *
885  *  LWS_CALLBACK_ESTABLISHED:  after the server completes a handshake with
886  *                              an incoming client.  If you built the library
887  *                              with ssl support, @in is a pointer to the
888  *                              ssl struct associated with the connection or
889  *                              NULL.
890  *
891  *  LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has
892  *        been unable to complete a handshake with the remote server.  If
893  *        in is non-NULL, you can find an error string of length len where
894  *        it points to.
895  *
896  *  LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the
897  *                              client user code to examine the http headers
898  *                              and decide to reject the connection.  If the
899  *                              content in the headers is interesting to the
900  *                              client (url, etc) it needs to copy it out at
901  *                              this point since it will be destroyed before
902  *                              the CLIENT_ESTABLISHED call
903  *
904  *  LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
905  *                              a handshake with the remote server
906  *
907  *      LWS_CALLBACK_CLOSED: when the websocket session ends
908  *
909  *      LWS_CALLBACK_CLOSED_HTTP: when a HTTP (non-websocket) session ends
910  *
911  *      LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a
912  *                              remote client, it can be found at *in and is
913  *                              len bytes long
914  *
915  *      LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets,
916  *                              they appear with this callback reason.  PONG
917  *                              packets only exist in 04+ protocol
918  *
919  *      LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the
920  *                              client connection, it can be found at *in and
921  *                              is len bytes long
922  *
923  *      LWS_CALLBACK_HTTP: an http request has come from a client that is not
924  *                              asking to upgrade the connection to a websocket
925  *                              one.  This is a chance to serve http content,
926  *                              for example, to send a script to the client
927  *                              which will then open the websockets connection.
928  *                              @in points to the URI path requested and
929  *                              lws_serve_http_file() makes it very
930  *                              simple to send back a file to the client.
931  *                              Normally after sending the file you are done
932  *                              with the http connection, since the rest of the
933  *                              activity will come by websockets from the script
934  *                              that was delivered by http, so you will want to
935  *                              return 1; to close and free up the connection.
936  *                              That's important because it uses a slot in the
937  *                              total number of client connections allowed set
938  *                              by MAX_CLIENTS.
939  *
940  *      LWS_CALLBACK_HTTP_BODY: the next @len bytes data from the http
941  *              request body HTTP connection is now available in @in.
942  *
943  *      LWS_CALLBACK_HTTP_BODY_COMPLETION: the expected amount of http request
944  *              body has been delivered
945  *
946  *      LWS_CALLBACK_HTTP_WRITEABLE: you can write more down the http protocol
947  *              link now.
948  *
949  *      LWS_CALLBACK_HTTP_FILE_COMPLETION: a file requested to be send down
950  *                              http link has completed.
951  *
952  *      LWS_CALLBACK_CLIENT_WRITEABLE:
953  *      LWS_CALLBACK_SERVER_WRITEABLE:   If you call
954  *              lws_callback_on_writable() on a connection, you will
955  *              get one of these callbacks coming when the connection socket
956  *              is able to accept another write packet without blocking.
957  *              If it already was able to take another packet without blocking,
958  *              you'll get this callback at the next call to the service loop
959  *              function.  Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
960  *              and servers get LWS_CALLBACK_SERVER_WRITEABLE.
961  *
962  *      LWS_CALLBACK_FILTER_NETWORK_CONNECTION: called when a client connects to
963  *              the server at network level; the connection is accepted but then
964  *              passed to this callback to decide whether to hang up immediately
965  *              or not, based on the client IP.  @in contains the connection
966  *              socket's descriptor. Since the client connection information is
967  *              not available yet, @wsi still pointing to the main server socket.
968  *              Return non-zero to terminate the connection before sending or
969  *              receiving anything. Because this happens immediately after the
970  *              network connection from the client, there's no websocket protocol
971  *              selected yet so this callback is issued only to protocol 0.
972  *
973  *      LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED: A new client just had
974  *              been connected, accepted, and instantiated into the pool. This
975  *              callback allows setting any relevant property to it. Because this
976  *              happens immediately after the instantiation of a new client,
977  *              there's no websocket protocol selected yet so this callback is
978  *              issued only to protocol 0. Only @wsi is defined, pointing to the
979  *              new client, and the return value is ignored.
980  *
981  *      LWS_CALLBACK_FILTER_HTTP_CONNECTION: called when the request has
982  *              been received and parsed from the client, but the response is
983  *              not sent yet.  Return non-zero to disallow the connection.
984  *              @user is a pointer to the connection user space allocation,
985  *              @in is the URI, eg, "/"
986  *              In your handler you can use the public APIs
987  *              lws_hdr_total_length() / lws_hdr_copy() to access all of the
988  *              headers using the header enums lws_token_indexes from
989  *              libwebsockets.h to check for and read the supported header
990  *              presence and content before deciding to allow the http
991  *              connection to proceed or to kill the connection.
992  *
993  *      LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: called when the handshake has
994  *              been received and parsed from the client, but the response is
995  *              not sent yet.  Return non-zero to disallow the connection.
996  *              @user is a pointer to the connection user space allocation,
997  *              @in is the requested protocol name
998  *              In your handler you can use the public APIs
999  *              lws_hdr_total_length() / lws_hdr_copy() to access all of the
1000  *              headers using the header enums lws_token_indexes from
1001  *              libwebsockets.h to check for and read the supported header
1002  *              presence and content before deciding to allow the handshake
1003  *              to proceed or to kill the connection.
1004  *
1005  *      LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: if configured for
1006  *              including OpenSSL support, this callback allows your user code
1007  *              to perform extra SSL_CTX_load_verify_locations() or similar
1008  *              calls to direct OpenSSL where to find certificates the client
1009  *              can use to confirm the remote server identity.  @user is the
1010  *              OpenSSL SSL_CTX*
1011  *
1012  *      LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: if configured for
1013  *              including OpenSSL support, this callback allows your user code
1014  *              to load extra certifcates into the server which allow it to
1015  *              verify the validity of certificates returned by clients.  @user
1016  *              is the server's OpenSSL SSL_CTX*
1017  *
1018  *      LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY: if configured for
1019  *              including OpenSSL support but no private key file has been
1020  *              specified (ssl_private_key_filepath is NULL), this is called to
1021  *              allow the user to set the private key directly via libopenssl
1022  *              and perform further operations if required; this might be useful
1023  *              in situations where the private key is not directly accessible
1024  *              by the OS, for example if it is stored on a smartcard
1025  *              @user is the server's OpenSSL SSL_CTX*
1026  *
1027  *      LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: if the
1028  *              libwebsockets context was created with the option
1029  *              LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
1030  *              callback is generated during OpenSSL verification of the cert
1031  *              sent from the client.  It is sent to protocol[0] callback as
1032  *              no protocol has been negotiated on the connection yet.
1033  *              Notice that the libwebsockets context and wsi are both NULL
1034  *              during this callback.  See
1035  *               http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
1036  *              to understand more detail about the OpenSSL callback that
1037  *              generates this libwebsockets callback and the meanings of the
1038  *              arguments passed.  In this callback, @user is the x509_ctx,
1039  *              @in is the ssl pointer and @len is preverify_ok
1040  *              Notice that this callback maintains libwebsocket return
1041  *              conventions, return 0 to mean the cert is OK or 1 to fail it.
1042  *              This also means that if you don't handle this callback then
1043  *              the default callback action of returning 0 allows the client
1044  *              certificates.
1045  *
1046  *      LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: this callback happens
1047  *              when a client handshake is being compiled.  @user is NULL,
1048  *              @in is a char **, it's pointing to a char * which holds the
1049  *              next location in the header buffer where you can add
1050  *              headers, and @len is the remaining space in the header buffer,
1051  *              which is typically some hundreds of bytes.  So, to add a canned
1052  *              cookie, your handler code might look similar to:
1053  *
1054  *              char **p = (char **)in;
1055  *
1056  *              if (len < 100)
1057  *                      return 1;
1058  *
1059  *              *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
1060  *
1061  *              return 0;
1062  *
1063  *              Notice if you add anything, you just have to take care about
1064  *              the CRLF on the line you added.  Obviously this callback is
1065  *              optional, if you don't handle it everything is fine.
1066  *
1067  *              Notice the callback is coming to protocols[0] all the time,
1068  *              because there is no specific protocol handshook yet.
1069  *
1070  *      LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: When the server handshake code
1071  *              sees that it does support a requested extension, before
1072  *              accepting the extension by additing to the list sent back to
1073  *              the client it gives this callback just to check that it's okay
1074  *              to use that extension.  It calls back to the requested protocol
1075  *              and with @in being the extension name, @len is 0 and @user is
1076  *              valid.  Note though at this time the ESTABLISHED callback hasn't
1077  *              happened yet so if you initialize @user content there, @user
1078  *              content during this callback might not be useful for anything.
1079  *              Notice this callback comes to protocols[0].
1080  *
1081  *      LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:        When a client
1082  *              connection is being prepared to start a handshake to a server,
1083  *              each supported extension is checked with protocols[0] callback
1084  *              with this reason, giving the user code a chance to suppress the
1085  *              claim to support that extension by returning non-zero.  If
1086  *              unhandled, by default 0 will be returned and the extension
1087  *              support included in the header to the server.  Notice this
1088  *              callback comes to protocols[0].
1089  *
1090  *      LWS_CALLBACK_PROTOCOL_INIT:     One-time call per protocol so it can
1091  *              do initial setup / allocations etc
1092  *
1093  *      LWS_CALLBACK_PROTOCOL_DESTROY:  One-time call per protocol indicating
1094  *              this protocol won't get used at all after this callback, the
1095  *              context is getting destroyed.  Take the opportunity to
1096  *              deallocate everything that was allocated by the protocol.
1097  *
1098  *      LWS_CALLBACK_WSI_CREATE: outermost (earliest) wsi create notification
1099  *
1100  *      LWS_CALLBACK_WSI_DESTROY: outermost (latest) wsi destroy notification
1101  *
1102  *      The next five reasons are optional and only need taking care of if you
1103  *      will be integrating libwebsockets sockets into an external polling
1104  *      array.
1105  *
1106  *      For these calls, @in points to a struct lws_pollargs that
1107  *      contains @fd, @events and @prev_events members
1108  *
1109  *      LWS_CALLBACK_ADD_POLL_FD: libwebsocket deals with its poll() loop
1110  *              internally, but in the case you are integrating with another
1111  *              server you will need to have libwebsocket sockets share a
1112  *              polling array with the other server.  This and the other
1113  *              POLL_FD related callbacks let you put your specialized
1114  *              poll array interface code in the callback for protocol 0, the
1115  *              first protocol you support, usually the HTTP protocol in the
1116  *              serving case.
1117  *              This callback happens when a socket needs to be
1118  *              added to the polling loop: @in points to a struct
1119  *              lws_pollargs; the @fd member of the struct is the file
1120  *              descriptor, and @events contains the active events.
1121  *
1122  *              If you are using the internal polling loop (the "service"
1123  *              callback), you can just ignore these callbacks.
1124  *
1125  *      LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor
1126  *              needs to be removed from an external polling array.  @in is
1127  *              again the struct lws_pollargs containing the @fd member
1128  *              to be removed.  If you are using the internal polling
1129  *              loop, you can just ignore it.
1130  *
1131  *      LWS_CALLBACK_CHANGE_MODE_POLL_FD: This callback happens when
1132  *              libwebsockets wants to modify the events for a connectiion.
1133  *              @in is the struct lws_pollargs with the @fd to change.
1134  *              The new event mask is in @events member and the old mask is in
1135  *              the @prev_events member.
1136  *              If you are using the internal polling loop, you can just ignore
1137  *              it.
1138  *
1139  *      LWS_CALLBACK_LOCK_POLL:
1140  *      LWS_CALLBACK_UNLOCK_POLL: These allow the external poll changes driven
1141  *              by libwebsockets to participate in an external thread locking
1142  *              scheme around the changes, so the whole thing is threadsafe.
1143  *              These are called around three activities in the library,
1144  *                      - inserting a new wsi in the wsi / fd table (len=1)
1145  *                      - deleting a wsi from the wsi / fd table (len=1)
1146  *                      - changing a wsi's POLLIN/OUT state (len=0)
1147  *              Locking and unlocking external synchronization objects when
1148  *              len == 1 allows external threads to be synchronized against
1149  *              wsi lifecycle changes if it acquires the same lock for the
1150  *              duration of wsi dereference from the other thread context.
1151  *
1152  *      LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
1153  *              The peer has sent an unsolicited Close WS packet.  @in and
1154  *              @len are the optional close code (first 2 bytes, network
1155  *              order) and the optional additional information which is not
1156  *              defined in the standard, and may be a string or non-human-
1157  *              readble data.
1158  *              If you return 0 lws will echo the close and then close the
1159  *              connection.  If you return nonzero lws will just close the
1160  *              connection.
1161  */
1162 typedef int
1163 lws_callback_function(struct lws *wsi, enum lws_callback_reasons reason,
1164                     void *user, void *in, size_t len);
1165
1166 /**
1167  * typedef lws_extension_callback_function() - Hooks to allow extensions to operate
1168  * @context:    Websockets context
1169  * @ext:        This extension
1170  * @wsi:        Opaque websocket instance pointer
1171  * @reason:     The reason for the call
1172  * @user:       Pointer to ptr to per-session user data allocated by library
1173  * @in:         Pointer used for some callback reasons
1174  * @len:        Length set for some callback reasons
1175  *
1176  *      Each extension that is active on a particular connection receives
1177  *      callbacks during the connection lifetime to allow the extension to
1178  *      operate on websocket data and manage itself.
1179  *
1180  *      Libwebsockets takes care of allocating and freeing "user" memory for
1181  *      each active extension on each connection.  That is what is pointed to
1182  *      by the @user parameter.
1183  *
1184  *      LWS_EXT_CB_CONSTRUCT:  called when the server has decided to
1185  *              select this extension from the list provided by the client,
1186  *              just before the server will send back the handshake accepting
1187  *              the connection with this extension active.  This gives the
1188  *              extension a chance to initialize its connection context found
1189  *              in @user.
1190  *
1191  *      LWS_EXT_CB_CLIENT_CONSTRUCT: same as LWS_EXT_CB_CONSTRUCT
1192  *              but called when client is instantiating this extension.  Some
1193  *              extensions will work the same on client and server side and then
1194  *              you can just merge handlers for both CONSTRUCTS.
1195  *
1196  *      LWS_EXT_CB_DESTROY:  called when the connection the extension was
1197  *              being used on is about to be closed and deallocated.  It's the
1198  *              last chance for the extension to deallocate anything it has
1199  *              allocated in the user data (pointed to by @user) before the
1200  *              user data is deleted.  This same callback is used whether you
1201  *              are in client or server instantiation context.
1202  *
1203  *      LWS_EXT_CB_PACKET_RX_PREPARSE: when this extension was active on
1204  *              a connection, and a packet of data arrived at the connection,
1205  *              it is passed to this callback to give the extension a chance to
1206  *              change the data, eg, decompress it.  @user is pointing to the
1207  *              extension's private connection context data, @in is pointing
1208  *              to an lws_tokens struct, it consists of a char * pointer called
1209  *              token, and an int called token_len.  At entry, these are
1210  *              set to point to the received buffer and set to the content
1211  *              length.  If the extension will grow the content, it should use
1212  *              a new buffer allocated in its private user context data and
1213  *              set the pointed-to lws_tokens members to point to its buffer.
1214  *
1215  *      LWS_EXT_CB_PACKET_TX_PRESEND: this works the same way as
1216  *              LWS_EXT_CB_PACKET_RX_PREPARSE above, except it gives the
1217  *              extension a chance to change websocket data just before it will
1218  *              be sent out.  Using the same lws_token pointer scheme in @in,
1219  *              the extension can change the buffer and the length to be
1220  *              transmitted how it likes.  Again if it wants to grow the
1221  *              buffer safely, it should copy the data into its own buffer and
1222  *              set the lws_tokens token pointer to it.
1223  *
1224  *      LWS_EXT_CB_ARGS_VALIDATE:
1225  */
1226 typedef int
1227 lws_extension_callback_function(struct lws_context *context,
1228                               const struct lws_extension *ext, struct lws *wsi,
1229                               enum lws_extension_callback_reasons reason,
1230                               void *user, void *in, size_t len);
1231
1232 /**
1233  * struct lws_protocols -       List of protocols and handlers server
1234  *                                      supports.
1235  * @name:       Protocol name that must match the one given in the client
1236  *              Javascript new WebSocket(url, 'protocol') name.
1237  * @callback:   The service callback used for this protocol.  It allows the
1238  *              service action for an entire protocol to be encapsulated in
1239  *              the protocol-specific callback
1240  * @per_session_data_size:      Each new connection using this protocol gets
1241  *              this much memory allocated on connection establishment and
1242  *              freed on connection takedown.  A pointer to this per-connection
1243  *              allocation is passed into the callback in the 'user' parameter
1244  * @rx_buffer_size: if you want atomic frames delivered to the callback, you
1245  *              should set this to the size of the biggest legal frame that
1246  *              you support.  If the frame size is exceeded, there is no
1247  *              error, but the buffer will spill to the user callback when
1248  *              full, which you can detect by using
1249  *              lws_remaining_packet_payload().  Notice that you
1250  *              just talk about frame size here, the LWS_PRE
1251  *              and post-padding are automatically also allocated on top.
1252  * @id:         ignored by lws, but useful to contain user information bound
1253  *              to the selected protocol.  For example if this protocol was
1254  *              called "myprotocol-v2", you might set id to 2, and the user
1255  *              code that acts differently according to the version can do so by
1256  *              switch (wsi->protocol->id), user code might use some bits as
1257  *              capability flags based on selected protocol version, etc.
1258  * @user:       User provided context data at the protocol level.
1259  *              Accessible via lws_get_protocol(wsi)->user
1260  *              This should not be confused with wsi->user, it is not the same.
1261  *              The library completely ignores any value in here.
1262  *
1263  *      This structure represents one protocol supported by the server.  An
1264  *      array of these structures is passed to lws_create_server()
1265  *      allows as many protocols as you like to be handled by one server.
1266  *
1267  *      The first protocol given has its callback used for user callbacks when
1268  *      there is no agreed protocol name, that's true during HTTP part of the
1269  *      connection and true if the client did not send a Protocol: header.
1270  */
1271
1272 struct lws_protocols {
1273         const char *name;
1274         lws_callback_function *callback;
1275         size_t per_session_data_size;
1276         size_t rx_buffer_size;
1277         unsigned int id;
1278         void *user;
1279
1280         /* Add new things just above here ---^
1281          * This is part of the ABI, don't needlessly break compatibility */
1282 };
1283
1284 enum lws_ext_options_types {
1285         EXTARG_NONE,
1286         EXTARG_DEC,
1287         EXTARG_OPT_DEC
1288
1289         /* Add new things just above here ---^
1290          * This is part of the ABI, don't needlessly break compatibility */
1291 };
1292
1293 /**
1294  * struct lws_ext_options -     Option arguments to the extension.  These are
1295  *                              used in the negotiation at ws upgrade time.
1296  *                              The helper function lws_ext_parse_options()
1297  *                              uses these to generate callbacks
1298  *
1299  * @name:                       Option name, eg, "server_no_context_takeover"
1300  * @type:                       What kind of args the option can take
1301  */
1302 struct lws_ext_options {
1303         const char *name;
1304         enum lws_ext_options_types type;
1305
1306         /* Add new things just above here ---^
1307          * This is part of the ABI, don't needlessly break compatibility */
1308 };
1309
1310 struct lws_ext_option_arg {
1311         const char *option_name; /* may be NULL, option_index used then */
1312         int option_index;
1313         const char *start;
1314         int len;
1315 };
1316
1317 /**
1318  * struct lws_extension -       An extension we know how to cope with
1319  *
1320  * @name:                       Formal extension name, eg, "permessage-deflate"
1321  * @callback:                   Service callback
1322  * @client_offer:               String containing exts and options client offers
1323  */
1324
1325 struct lws_extension {
1326         const char *name;
1327         lws_extension_callback_function *callback;
1328         const char *client_offer;
1329
1330         /* Add new things just above here ---^
1331          * This is part of the ABI, don't needlessly break compatibility */
1332 };
1333
1334
1335 #ifdef LWS_WITH_PLUGINS
1336
1337 /* PLUGINS implies LIBUV */
1338
1339 #define LWS_PLUGIN_API_MAGIC 180
1340
1341 struct lws_plugin_capability {
1342         unsigned int api_magic; /* caller fills this in, plugin fills rest */
1343         const struct lws_protocols *protocols;
1344         int count_protocols;
1345         const struct lws_extension *extensions;
1346         int count_extensions;
1347 };
1348
1349 typedef int (*lws_plugin_init_func)(struct lws_context *,
1350                                     struct lws_plugin_capability *);
1351 typedef int (*lws_plugin_destroy_func)(struct lws_context *);
1352 struct lws_plugin {
1353         struct lws_plugin *list;
1354 #if (UV_VERSION_MAJOR > 0)
1355         uv_lib_t lib;
1356 #else
1357         void *l;
1358 #endif
1359         char name[64];
1360         struct lws_plugin_capability caps;
1361 };
1362
1363 #endif
1364
1365 /*
1366  * The internal exts are part of the public abi
1367  * If we add more extensions, publish the callback here  ------v
1368  */
1369
1370 LWS_EXTERN
1371 int lws_extension_callback_pm_deflate(
1372         struct lws_context *context, const struct lws_extension *ext,
1373         struct lws *wsi, enum lws_extension_callback_reasons reason,
1374         void *user, void *in, size_t len);
1375
1376 LWS_VISIBLE LWS_EXTERN int
1377 lws_set_extension_option(struct lws *wsi, const char *ext_name,
1378                          const char *opt_name, const char *opt_val);
1379
1380 struct lws_protocol_vhost_options {
1381         const struct lws_protocol_vhost_options *next;
1382         const struct lws_protocol_vhost_options *options;
1383         const char *name;
1384         const char *value;
1385 };
1386
1387 struct lws_http_mount {
1388         struct lws_http_mount *mount_next;
1389         const char *mountpoint; /* mountpoint in http pathspace, eg, "/" */
1390         const char *origin; /* path to be mounted, eg, "/var/www/warmcat.com" */
1391         const char *def; /* default target, eg, "index.html" */
1392
1393         const struct lws_protocol_vhost_options *cgienv;
1394         const struct lws_protocol_vhost_options *extra_mimetypes;
1395
1396         int cgi_timeout;
1397         int cache_max_age;
1398
1399         unsigned int cache_reusable:1;
1400         unsigned int cache_revalidate:1;
1401         unsigned int cache_intermediaries:1;
1402
1403         unsigned char origin_protocol;
1404         unsigned char mountpoint_len;
1405 };
1406
1407 /**
1408  * struct lws_context_creation_info - parameters to create context with
1409  *
1410  * This is also used to create vhosts.... if LWS_SERVER_OPTION_EXPLICIT_VHOSTS
1411  * is not given, then for backwards compatibility one vhost is created at
1412  * context-creation time using the info from this struct.
1413  *
1414  * If LWS_SERVER_OPTION_EXPLICIT_VHOSTS is given, then no vhosts are created
1415  * at the same time as the context, they are expected to be created afterwards.
1416  *
1417  * @port:       VHOST: Port to listen on... you can use CONTEXT_PORT_NO_LISTEN to
1418  *              suppress listening on any port, that's what you want if you are
1419  *              not running a websocket server at all but just using it as a
1420  *              client
1421  * @iface:      VHOST: NULL to bind the listen socket to all interfaces, or the
1422  *              interface name, eg, "eth2"
1423  *              If options specifies LWS_SERVER_OPTION_UNIX_SOCK, this member is
1424  *              the pathname of a UNIX domain socket. you can use the UNIX domain
1425  *              sockets in abstract namespace, by prepending an @ symbole to the
1426  *              socket name.
1427  * @protocols:  VHOST: Array of structures listing supported protocols and a protocol-
1428  *              specific callback for each one.  The list is ended with an
1429  *              entry that has a NULL callback pointer.
1430  *              It's not const because we write the owning_server member
1431  * @extensions: VHOST: NULL or array of lws_extension structs listing the
1432  *              extensions this context supports.  If you configured with
1433  *              --without-extensions, you should give NULL here.
1434  * @token_limits: CONTEXT: NULL or struct lws_token_limits pointer which is initialized
1435  *              with a token length limit for each possible WSI_TOKEN_***
1436  * @ssl_cert_filepath:  VHOST: If libwebsockets was compiled to use ssl, and you want
1437  *                      to listen using SSL, set to the filepath to fetch the
1438  *                      server cert from, otherwise NULL for unencrypted
1439  * @ssl_private_key_filepath: VHOST: filepath to private key if wanting SSL mode;
1440  *                      if this is set to NULL but sll_cert_filepath is set, the
1441  *                      OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback is called
1442  *                      to allow setting of the private key directly via openSSL
1443  *                      library calls
1444  * @ssl_ca_filepath: VHOST: CA certificate filepath or NULL
1445  * @ssl_cipher_list:    VHOST: List of valid ciphers to use (eg,
1446  *                      "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
1447  *                      or you can leave it as NULL to get "DEFAULT"
1448  * @http_proxy_address: VHOST: If non-NULL, attempts to proxy via the given address.
1449  *                      If proxy auth is required, use format
1450  *                      "username:password@server:port"
1451  * @http_proxy_port:    VHOST: If http_proxy_address was non-NULL, uses this port at
1452  *                      the address
1453  * @gid:        CONTEXT: group id to change to after setting listen socket, or -1.
1454  * @uid:        CONTEXT: user id to change to after setting listen socket, or -1.
1455  * @options:    VHOST + CONTEXT: 0, or LWS_SERVER_OPTION_... bitfields
1456  * @user:       CONTEXT: optional user pointer that can be recovered via the context
1457  *              pointer using lws_context_user
1458  * @ka_time:    CONTEXT: 0 for no keepalive, otherwise apply this keepalive timeout to
1459  *              all libwebsocket sockets, client or server
1460  * @ka_probes:  CONTEXT: if ka_time was nonzero, after the timeout expires how many
1461  *              times to try to get a response from the peer before giving up
1462  *              and killing the connection
1463  * @ka_interval: CONTEXT: if ka_time was nonzero, how long to wait before each ka_probes
1464  *              attempt
1465  * @provided_client_ssl_ctx: CONTEXT: If non-null, swap out libwebsockets ssl
1466  *              implementation for the one provided by provided_ssl_ctx.
1467  *              Libwebsockets no longer is responsible for freeing the context
1468  *              if this option is selected.
1469  * @max_http_header_data: CONTEXT: The max amount of header payload that can be handled
1470  *              in an http request (unrecognized header payload is dropped)
1471  * @max_http_header_pool: CONTEXT: The max number of connections with http headers that
1472  *              can be processed simultaneously (the corresponding memory is
1473  *              allocated for the lifetime of the context).  If the pool is
1474  *              busy new incoming connections must wait for accept until one
1475  *              becomes free.
1476  * @count_threads: CONTEXT: how many contexts to create in an array, 0 = 1
1477  * @fd_limit_per_thread: CONTEXT: nonzero means restrict each service thread to this
1478  *              many fds, 0 means the default which is divide the process fd
1479  *              limit by the number of threads.
1480  * @timeout_secs: VHOST: various processes involving network roundtrips in the
1481  *              library are protected from hanging forever by timeouts.  If
1482  *              nonzero, this member lets you set the timeout used in seconds.
1483  *              Otherwise a default timeout is used.
1484  * @ecdh_curve: VHOST: if NULL, defaults to initializing server with "prime256v1"
1485  * @vhost_name: VHOST: name of vhost, must match external DNS name used to
1486  *              access the site, like "warmcat.com" as it's used to match
1487  *              Host: header and / or SNI name for SSL.
1488  * @plugin_dirs: CONTEXT: NULL, or NULL-terminated array of directories to
1489  *              scan for lws protocol plugins at context creation time
1490  * @pvo:        VHOST: pointer to optional linked list of per-vhost
1491  *              options made accessible to protocols
1492  * @keepalive_timeout: VHOST: (default = 0 = 60s) seconds to allow remote
1493  *              client to hold on to an idle HTTP/1.1 connection
1494  * @log_filepath: VHOST: filepath to append logs to... this is opened before
1495  *              any dropping of initial privileges
1496  * @mounts:     VHOST: optional linked list of mounts for this vhost
1497  * @server_string: CONTEXT: string used in HTTP headers to identify server
1498  *              software, if NULL, "libwebsockets".
1499  * @pt_serv_buf_size: CONTEXT: 0 = default of 4096.  This buffer is used by
1500  *              various service related features including file serving, it
1501  *              defines the max chunk of file that can be sent at once.
1502  *              At the risk of lws having to buffer failed large sends, it
1503  *              can be increased to, eg, 128KiB to improve throughput.
1504  */
1505
1506 struct lws_context_creation_info {
1507         int port;                                       /* VH */
1508         const char *iface;                              /* VH */
1509         const struct lws_protocols *protocols;          /* VH */
1510         const struct lws_extension *extensions;         /* VH */
1511         const struct lws_token_limits *token_limits;    /* context */
1512         const char *ssl_private_key_password;           /* VH */
1513         const char *ssl_cert_filepath;                  /* VH */
1514         const char *ssl_private_key_filepath;           /* VH */
1515         const char *ssl_ca_filepath;                    /* VH */
1516         const char *ssl_cipher_list;                    /* VH */
1517         const char *http_proxy_address;                 /* VH */
1518         unsigned int http_proxy_port;                   /* VH */
1519         int gid;                                        /* context */
1520         int uid;                                        /* context */
1521         unsigned int options;                           /* VH + context */
1522         void *user;                                     /* context */
1523         int ka_time;                                    /* context */
1524         int ka_probes;                                  /* context */
1525         int ka_interval;                                /* context */
1526 #ifdef LWS_OPENSSL_SUPPORT
1527         SSL_CTX *provided_client_ssl_ctx;               /* context */
1528 #else /* maintain structure layout either way */
1529         void *provided_client_ssl_ctx;
1530 #endif
1531
1532         short max_http_header_data;                     /* context */
1533         short max_http_header_pool;                     /* context */
1534
1535         unsigned int count_threads;                     /* context */
1536         unsigned int fd_limit_per_thread;               /* context */
1537         unsigned int timeout_secs;                      /* VH */
1538         const char *ecdh_curve;                         /* VH */
1539         const char *vhost_name;                         /* VH */
1540         const char * const *plugin_dirs;                /* context */
1541         const struct lws_protocol_vhost_options *pvo;   /* VH */
1542         int keepalive_timeout;                          /* VH */
1543         const char *log_filepath;                       /* VH */
1544         const struct lws_http_mount *mounts;            /* VH */
1545         const char *server_string;                      /* context */
1546         unsigned int pt_serv_buf_size;                  /* context */
1547
1548         /* Add new things just above here ---^
1549          * This is part of the ABI, don't needlessly break compatibility
1550          *
1551          * The below is to ensure later library versions with new
1552          * members added above will see 0 (default) even if the app
1553          * was not built against the newer headers.
1554          */
1555
1556         void *_unused[8];
1557 };
1558
1559 /**
1560  * struct lws_client_connect_info - parameters to connect with when using
1561  *                                  lws_client_connect_via_info()
1562  *
1563  * @context:    lws context to create connection in
1564  * @address:    remote address to connect to
1565  * @port:       remote port to connect to
1566  * @ssl_connection: nonzero for ssl
1567  * @path:       uri path
1568  * @host:       content of host header
1569  * @origin:     content of origin header
1570  * @protocol:   list of ws protocols
1571  * @ietf_version_or_minus_one: currently leave at 0 or -1
1572  * @userdata:   if non-NULL, use this as wsi user_data instead of malloc it
1573  * @client_exts: array of extensions that may be used on connection
1574  * @method:     if non-NULL, do this http method instead of ws[s] upgrade.
1575  *              use "GET" to be a simple http client connection
1576  * @parent_wsi: if another wsi is responsible for this connection, give it here.
1577  *              this is used to make sure if the parent closes so do any
1578  *              child connections first.
1579  * @uri_replace_from: if non-NULL, when this string is found in URIs in
1580  *              text/html content-encoding, it's replaced with @uri_replace_to
1581  * @uri_replace_to: see above
1582  * @vhost:      vhost to bind to (used to determine related SSL_CTX)
1583  */
1584
1585 struct lws_client_connect_info {
1586         struct lws_context *context;
1587         const char *address;
1588         int port;
1589         int ssl_connection;
1590         const char *path;
1591         const char *host;
1592         const char *origin;
1593         const char *protocol;
1594         int ietf_version_or_minus_one;
1595         void *userdata;
1596         const struct lws_extension *client_exts;
1597         const char *method;
1598         struct lws *parent_wsi;
1599         const char *uri_replace_from;
1600         const char *uri_replace_to;
1601         struct lws_vhost *vhost;
1602
1603         /* Add new things just above here ---^
1604          * This is part of the ABI, don't needlessly break compatibility
1605          *
1606          * The below is to ensure later library versions with new
1607          * members added above will see 0 (default) even if the app
1608          * was not built against the newer headers.
1609          */
1610
1611         void *_unused[4];
1612 };
1613
1614 enum {
1615         LWSMPRO_HTTP            = 0,
1616         LWSMPRO_HTTPS           = 1,
1617         LWSMPRO_FILE            = 2,
1618         LWSMPRO_CGI             = 3,
1619         LWSMPRO_REDIR_HTTP      = 4,
1620         LWSMPRO_REDIR_HTTPS     = 5,
1621         LWSMPRO_CALLBACK        = 6,
1622 };
1623
1624 LWS_VISIBLE LWS_EXTERN int
1625 lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len);
1626
1627 LWS_VISIBLE LWS_EXTERN int
1628 lws_json_dump_context(const struct lws_context *context, char *buf, int len);
1629
1630 LWS_VISIBLE LWS_EXTERN void
1631 lws_set_log_level(int level,
1632                   void (*log_emit_function)(int level, const char *line));
1633
1634 LWS_VISIBLE LWS_EXTERN void
1635 lwsl_emit_syslog(int level, const char *line);
1636
1637 LWS_VISIBLE LWS_EXTERN struct lws_context *
1638 lws_create_context(struct lws_context_creation_info *info);
1639
1640 struct lws_vhost;
1641
1642 LWS_EXTERN LWS_VISIBLE struct lws_vhost *
1643 lws_create_vhost(struct lws_context *context,
1644                  struct lws_context_creation_info *info);
1645
1646 LWS_VISIBLE LWS_EXTERN int
1647 lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
1648                           struct lws_vhost *vhost);
1649
1650 /* deprecated: use lws_get_vhost() */
1651 LWS_VISIBLE LWS_EXTERN struct lws_vhost *
1652 lws_vhost_get(struct lws *wsi) LWS_WARN_DEPRECATED;
1653
1654 LWS_VISIBLE LWS_EXTERN struct lws_vhost *
1655 lws_get_vhost(struct lws *wsi);
1656
1657 /* deprecated: use lws_get_protocol */
1658 LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
1659 lws_protocol_get(struct lws *wsi) LWS_WARN_DEPRECATED;
1660
1661 LWS_VISIBLE LWS_EXTERN void *
1662 lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost, const struct lws_protocols *prot,
1663                             int size);
1664 LWS_VISIBLE LWS_EXTERN void *
1665 lws_protocol_vh_priv_get(struct lws_vhost *vhost, const struct lws_protocols *prot);
1666
1667 LWS_VISIBLE LWS_EXTERN int
1668 lws_finalize_startup(struct lws_context *context);
1669
1670 LWS_VISIBLE LWS_EXTERN int
1671 lws_set_proxy(struct lws_vhost *vhost, const char *proxy);
1672
1673 LWS_VISIBLE LWS_EXTERN void
1674 lws_context_destroy(struct lws_context *context);
1675
1676 LWS_VISIBLE LWS_EXTERN int
1677 lws_service(struct lws_context *context, int timeout_ms);
1678
1679 LWS_VISIBLE LWS_EXTERN int
1680 lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
1681
1682 LWS_VISIBLE LWS_EXTERN void
1683 lws_cancel_service_pt(struct lws *wsi);
1684
1685 LWS_VISIBLE LWS_EXTERN void
1686 lws_cancel_service(struct lws_context *context);
1687
1688 LWS_VISIBLE LWS_EXTERN int
1689 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
1690                     size_t addrlen);
1691
1692 LWS_VISIBLE LWS_EXTERN const unsigned char *
1693 lws_token_to_string(enum lws_token_indexes token);
1694
1695 /* all the below must be LWS_WARN_UNUSED_RESULT as they can run short */
1696
1697 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1698 lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
1699                             const unsigned char *value, int length,
1700                             unsigned char **p, unsigned char *end);
1701 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1702 lws_finalize_http_header(struct lws *wsi, unsigned char **p,
1703                          unsigned char *end);
1704 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1705 lws_add_http_header_by_token(struct lws *wsi, enum lws_token_indexes token,
1706                              const unsigned char *value, int length,
1707                              unsigned char **p, unsigned char *end);
1708 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1709 lws_add_http_header_content_length(struct lws *wsi,
1710                                    unsigned long content_length,
1711                                    unsigned char **p, unsigned char *end);
1712 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1713 lws_add_http_header_status(struct lws *wsi,
1714                            unsigned int code, unsigned char **p,
1715                            unsigned char *end);
1716
1717 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1718 lws_http_redirect(struct lws *wsi, int code, const unsigned char *loc, int len,
1719                   unsigned char **p, unsigned char *end);
1720
1721 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1722 lws_http_transaction_completed(struct lws *wsi);
1723
1724 #ifdef LWS_USE_LIBEV
1725 typedef void (lws_ev_signal_cb_t)(EV_P_ struct ev_signal *w, int revents);
1726
1727 LWS_VISIBLE LWS_EXTERN int
1728 lws_ev_sigint_cfg(struct lws_context *context, int use_ev_sigint,
1729                   lws_ev_signal_cb_t *cb);
1730
1731 LWS_VISIBLE LWS_EXTERN int
1732 lws_ev_initloop(struct lws_context *context, struct ev_loop *loop, int tsi);
1733
1734 LWS_VISIBLE LWS_EXTERN void
1735 lws_ev_sigint_cb(struct ev_loop *loop, struct ev_signal *watcher, int revents);
1736 #endif /* LWS_USE_LIBEV */
1737
1738 #ifdef LWS_USE_LIBUV
1739 LWS_VISIBLE LWS_EXTERN int
1740 lws_uv_sigint_cfg(struct lws_context *context, int use_uv_sigint,
1741                   uv_signal_cb cb);
1742
1743 LWS_VISIBLE LWS_EXTERN void
1744 lws_libuv_run(const struct lws_context *context, int tsi);
1745
1746 LWS_VISIBLE LWS_EXTERN void
1747 lws_libuv_stop(struct lws_context *context);
1748
1749 LWS_VISIBLE LWS_EXTERN int
1750 lws_uv_initloop(struct lws_context *context, uv_loop_t *loop, int tsi);
1751
1752 LWS_VISIBLE LWS_EXTERN uv_loop_t *
1753 lws_uv_getloop(struct lws_context *context, int tsi);
1754
1755 LWS_VISIBLE LWS_EXTERN void
1756 lws_uv_sigint_cb(uv_signal_t *watcher, int signum);
1757 #endif /* LWS_USE_LIBUV */
1758
1759 LWS_VISIBLE LWS_EXTERN int
1760 lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd);
1761
1762 LWS_VISIBLE LWS_EXTERN int
1763 lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd,
1764                    int tsi);
1765
1766 LWS_VISIBLE LWS_EXTERN void *
1767 lws_context_user(struct lws_context *context);
1768
1769 LWS_VISIBLE LWS_EXTERN void *
1770 lws_wsi_user(struct lws *wsi);
1771
1772 /*
1773  * NOTE: These public enums are part of the abi.  If you want to add one,
1774  * add it at where specified so existing users are unaffected.
1775  */
1776 enum pending_timeout {
1777         NO_PENDING_TIMEOUT                                      =  0,
1778         PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE                 =  1,
1779         PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE               =  2,
1780         PENDING_TIMEOUT_ESTABLISH_WITH_SERVER                   =  3,
1781         PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE                =  4,
1782         PENDING_TIMEOUT_AWAITING_PING                           =  5,
1783         PENDING_TIMEOUT_CLOSE_ACK                               =  6,
1784         PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE     =  7,
1785         PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE                   =  8,
1786         PENDING_TIMEOUT_SSL_ACCEPT                              =  9,
1787         PENDING_TIMEOUT_HTTP_CONTENT                            = 10,
1788         PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND                 = 11,
1789         PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE                  = 12,
1790         PENDING_TIMEOUT_SHUTDOWN_FLUSH                          = 13,
1791         PENDING_TIMEOUT_CGI                                     = 14,
1792         PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE                     = 15,
1793
1794         /****** add new things just above ---^ ******/
1795 };
1796
1797 LWS_VISIBLE LWS_EXTERN void
1798 lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
1799
1800 /*
1801  * IMPORTANT NOTICE!
1802  *
1803  * When sending with websocket protocol
1804  *
1805  * LWS_WRITE_TEXT,
1806  * LWS_WRITE_BINARY,
1807  * LWS_WRITE_CONTINUATION,
1808  * LWS_WRITE_PING,
1809  * LWS_WRITE_PONG
1810  *
1811  * the send buffer has to have LWS_PRE bytes valid BEFORE
1812  * the buffer pointer you pass to lws_write().
1813  *
1814  * This allows us to add protocol info before and after the data, and send as
1815  * one packet on the network without payload copying, for maximum efficiency.
1816  *
1817  * So for example you need this kind of code to use lws_write with a
1818  * 128-byte payload
1819  *
1820  *   char buf[LWS_PRE + 128];
1821  *
1822  *   // fill your part of the buffer... for example here it's all zeros
1823  *   memset(&buf[LWS_PRE], 0, 128);
1824  *
1825  *   lws_write(wsi, &buf[LWS_PRE], 128, LWS_WRITE_TEXT);
1826  *
1827  * When sending HTTP, with
1828  *
1829  * LWS_WRITE_HTTP,
1830  * LWS_WRITE_HTTP_HEADERS
1831  * LWS_WRITE_HTTP_FINAL
1832  *
1833  * there is no protocol data prepended, and don't need to take care about the
1834  * LWS_PRE bytes valid before the buffer pointer.
1835  *
1836  * LWS_PRE is at least the frame nonce + 2 header + 8 length
1837  * LWS_SEND_BUFFER_POST_PADDING is deprecated, it's now 0 and can be left off.
1838  * The example apps no longer use it.
1839  *
1840  * Pad LWS_PRE to the CPU word size, so that word references
1841  * to the address immediately after the padding won't cause an unaligned access
1842  * error. Sometimes for performance reasons the recommended padding is even
1843  * larger than sizeof(void *).
1844  */
1845
1846 #if !defined(LWS_SIZEOFPTR)
1847 #define LWS_SIZEOFPTR (sizeof (void *))
1848 #endif
1849 #if !defined(u_int64_t)
1850 #define u_int64_t unsigned long long
1851 #endif
1852
1853 #if __x86_64__
1854 #define _LWS_PAD_SIZE 16        /* Intel recommended for best performance */
1855 #else
1856 #define _LWS_PAD_SIZE LWS_SIZEOFPTR   /* Size of a pointer on the target arch */
1857 #endif
1858 #define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? \
1859                 ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n))
1860 #define LWS_PRE _LWS_PAD(4 + 10)
1861 /* used prior to 1.7 and retained for backward compatibility */
1862 #define LWS_SEND_BUFFER_PRE_PADDING LWS_PRE
1863 #define LWS_SEND_BUFFER_POST_PADDING 0
1864
1865 LWS_VISIBLE LWS_EXTERN int
1866 lws_write(struct lws *wsi, unsigned char *buf, size_t len,
1867           enum lws_write_protocol protocol);
1868
1869 /**
1870  * lws_close_reason - Set reason and aux data to send with Close packet
1871  *              If you are going to return nonzero from the callback
1872  *              requesting the connection to close, you can optionally
1873  *              call this to set the reason the peer will be told if
1874  *              possible.
1875  *
1876  * @wsi:        The websocket connection to set the close reason on
1877  * @status:     A valid close status from websocket standard
1878  * @buf:        NULL or buffer containing up to 124 bytes of auxiliary data
1879  * @len:        Length of data in @buf to send
1880  */
1881 LWS_VISIBLE LWS_EXTERN void
1882 lws_close_reason(struct lws *wsi, enum lws_close_status status,
1883                  unsigned char *buf, size_t len);
1884
1885 /* helper for case where buffer may be const */
1886 #define lws_write_http(wsi, buf, len) \
1887         lws_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP)
1888
1889 LWS_VISIBLE LWS_EXTERN int
1890 lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
1891                     const char *other_headers, int other_headers_len);
1892 LWS_VISIBLE LWS_EXTERN int
1893 lws_serve_http_file_fragment(struct lws *wsi);
1894
1895 LWS_VISIBLE LWS_EXTERN int
1896 lws_return_http_status(struct lws *wsi, unsigned int code,
1897                        const char *html_body);
1898
1899 LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
1900 lws_get_protocol(struct lws *wsi);
1901
1902 LWS_VISIBLE LWS_EXTERN int
1903 lws_callback_on_writable(struct lws *wsi);
1904
1905 LWS_VISIBLE LWS_EXTERN int
1906 lws_callback_on_writable_all_protocol(const struct lws_context *context,
1907                                       const struct lws_protocols *protocol);
1908
1909 LWS_VISIBLE LWS_EXTERN int
1910 lws_callback_on_writable_all_protocol_vhost(const struct lws_vhost *vhost,
1911                                       const struct lws_protocols *protocol);
1912
1913 LWS_VISIBLE LWS_EXTERN int
1914 lws_callback_all_protocol(struct lws_context *context,
1915                           const struct lws_protocols *protocol, int reason);
1916
1917 LWS_VISIBLE LWS_EXTERN int
1918 lws_callback_all_protocol_vhost(struct lws_vhost *vh,
1919                           const struct lws_protocols *protocol, int reason);
1920
1921 LWS_VISIBLE LWS_EXTERN int
1922 lws_get_socket_fd(struct lws *wsi);
1923
1924 LWS_VISIBLE LWS_EXTERN int
1925 lws_is_final_fragment(struct lws *wsi);
1926
1927 LWS_VISIBLE LWS_EXTERN unsigned char
1928 lws_get_reserved_bits(struct lws *wsi);
1929
1930 LWS_VISIBLE LWS_EXTERN int
1931 lws_rx_flow_control(struct lws *wsi, int enable);
1932
1933 LWS_VISIBLE LWS_EXTERN void
1934 lws_rx_flow_allow_all_protocol(const struct lws_context *context,
1935                                const struct lws_protocols *protocol);
1936
1937 LWS_VISIBLE LWS_EXTERN size_t
1938 lws_remaining_packet_payload(struct lws *wsi);
1939
1940 /*
1941  * if the protocol does not have any guidance, returns -1.  Currently only
1942  * http2 connections get send window information from this API.  But your code
1943  * should use it so it can work properly with any protocol.
1944  *
1945  * If nonzero return is the amount of payload data the peer or intermediary has
1946  * reported it has buffer space for.  That has NO relationship with the amount
1947  * of buffer space your OS can accept on this connection for a write action.
1948  *
1949  * This number represents the maximum you could send to the peer or intermediary
1950  * on this connection right now without it complaining.
1951  *
1952  * lws manages accounting for send window updates and payload writes
1953  * automatically, so this number reflects the situation at the peer or
1954  * intermediary dynamically.
1955  */
1956 LWS_VISIBLE LWS_EXTERN size_t
1957 lws_get_peer_write_allowance(struct lws *wsi);
1958
1959 /* deprecated, use lws_client_connect_via_info() */
1960 LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1961 lws_client_connect(struct lws_context *clients, const char *address,
1962                    int port, int ssl_connection, const char *path,
1963                    const char *host, const char *origin, const char *protocol,
1964                    int ietf_version_or_minus_one) LWS_WARN_DEPRECATED;
1965 /* deprecated, use lws_client_connect_via_info() */
1966 LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1967 lws_client_connect_extended(struct lws_context *clients, const char *address,
1968                             int port, int ssl_connection, const char *path,
1969                             const char *host, const char *origin,
1970                             const char *protocol, int ietf_version_or_minus_one,
1971                             void *userdata) LWS_WARN_DEPRECATED;
1972
1973 LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1974 lws_client_connect_via_info(struct lws_client_connect_info * ccinfo);
1975
1976 LWS_VISIBLE LWS_EXTERN struct lws *
1977 lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd);
1978 LWS_VISIBLE LWS_EXTERN struct lws *
1979 lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
1980                 const char *readbuf, size_t len);
1981
1982 LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
1983 lws_canonical_hostname(struct lws_context *context);
1984
1985
1986 LWS_VISIBLE LWS_EXTERN void
1987 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
1988                        int name_len, char *rip, int rip_len);
1989
1990 LWS_VISIBLE LWS_EXTERN const char *
1991 lws_get_peer_simple(struct lws *wsi, char *name, int namelen);
1992
1993 LWS_VISIBLE LWS_EXTERN int
1994 lws_get_random(struct lws_context *context, void *buf, int len);
1995
1996 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1997 lws_daemonize(const char *_lock_path);
1998
1999 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
2000 lws_send_pipe_choked(struct lws *wsi);
2001
2002 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
2003 lws_partial_buffered(struct lws *wsi);
2004
2005 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
2006 lws_frame_is_binary(struct lws *wsi);
2007
2008 LWS_VISIBLE LWS_EXTERN int
2009 lws_is_ssl(struct lws *wsi);
2010
2011 LWS_VISIBLE LWS_EXTERN int
2012 lws_is_cgi(struct lws *wsi);
2013
2014 #ifdef LWS_SHA1_USE_OPENSSL_NAME
2015 #define lws_SHA1 SHA1
2016 #else
2017 LWS_VISIBLE LWS_EXTERN unsigned char *
2018 lws_SHA1(const unsigned char *d, size_t n, unsigned char *md);
2019 #endif
2020
2021 LWS_VISIBLE LWS_EXTERN int
2022 lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
2023
2024 LWS_VISIBLE LWS_EXTERN int
2025 lws_b64_decode_string(const char *in, char *out, int out_size);
2026
2027 LWS_VISIBLE LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
2028 lws_get_library_version(void);
2029
2030 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
2031 lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
2032               const char **path);
2033
2034 /*
2035  *  Access to http headers
2036  *
2037  *  In lws the client http headers are temporarily malloc'd only for the
2038  *  duration of the http part of the handshake.  It's because in most cases,
2039  *  the header content is ignored for the whole rest of the connection lifetime
2040  *  and would then just be taking up space needlessly.
2041  *
2042  *  During LWS_CALLBACK_HTTP when the URI path is delivered is the last time
2043  *  the http headers are still allocated, you can use these apis then to
2044  *  look at and copy out interesting header content (cookies, etc)
2045  *
2046  *  Notice that the header total length reported does not include a terminating
2047  *  '\0', however you must allocate for it when using the _copy apis.  So the
2048  *  length reported for a header containing "123" is 3, but you must provide
2049  *  a buffer of length 4 so that "123\0" may be copied into it, or the copy
2050  *  will fail with a nonzero return code.
2051  */
2052
2053 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
2054 lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h);
2055
2056 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
2057 lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx);
2058
2059 /*
2060  * copies the whole, aggregated header, even if it was delivered in
2061  * several actual headers piece by piece
2062  */
2063 LWS_VISIBLE LWS_EXTERN int
2064 lws_hdr_copy(struct lws *wsi, char *dest, int len, enum lws_token_indexes h);
2065
2066 /*
2067  * copies only fragment frag_idx of a header.  Normally this is only useful
2068  * to parse URI arguments like ?x=1&y=2, token index WSI_TOKEN_HTTP_URI_ARGS
2069  * fragment 0 will contain "x=1" and fragment 1 "y=2"
2070  */
2071 LWS_VISIBLE LWS_EXTERN int
2072 lws_hdr_copy_fragment(struct lws *wsi, char *dest, int len,
2073                       enum lws_token_indexes h, int frag_idx);
2074
2075
2076 /* get the active file operations struct */
2077 LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops * LWS_WARN_UNUSED_RESULT
2078 lws_get_fops(struct lws_context *context);
2079
2080 LWS_VISIBLE LWS_EXTERN struct lws_context * LWS_WARN_UNUSED_RESULT
2081 lws_get_context(const struct lws *wsi);
2082
2083 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
2084 lws_get_count_threads(struct lws_context *context);
2085
2086 LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
2087 lws_get_parent(const struct lws *wsi);
2088
2089 LWS_VISIBLE LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
2090 lws_get_child(const struct lws *wsi);
2091
2092 #ifdef LWS_WITH_CGI
2093 enum lws_enum_stdinouterr {
2094         LWS_STDIN = 0,
2095         LWS_STDOUT = 1,
2096         LWS_STDERR = 2,
2097 };
2098
2099 enum lws_cgi_hdr_state {
2100         LCHS_HEADER,
2101         LCHS_CR1,
2102         LCHS_LF1,
2103         LCHS_CR2,
2104         LCHS_LF2,
2105         LHCS_PAYLOAD,
2106         LCHS_SINGLE_0A,
2107 };
2108
2109 struct lws_cgi_args {
2110         struct lws **stdwsi; /* get fd with lws_get_socket_fd() */
2111         enum lws_enum_stdinouterr ch;
2112         unsigned char *data; /* for messages with payload */
2113         enum lws_cgi_hdr_state hdr_state;
2114         int len;
2115 };
2116
2117 LWS_VISIBLE LWS_EXTERN int
2118 lws_cgi(struct lws *wsi, const char * const *exec_array,
2119         int script_uri_path_len, int timeout_secs,
2120         const struct lws_protocol_vhost_options *mp_cgienv);
2121
2122 LWS_VISIBLE LWS_EXTERN int
2123 lws_cgi_write_split_stdout_headers(struct lws *wsi);
2124
2125 LWS_VISIBLE LWS_EXTERN int
2126 lws_cgi_kill(struct lws *wsi);
2127 #endif
2128
2129 LWS_VISIBLE LWS_EXTERN int
2130 lws_http_client_read(struct lws *wsi, char **buf, int *len);
2131
2132 /*
2133  * lejp conf parsers
2134  */
2135 LWS_VISIBLE LWS_EXTERN int
2136 lwsws_get_config_globals(struct lws_context_creation_info *info, const char *d,
2137                          char **config_strings, int *len);
2138
2139 LWS_VISIBLE LWS_EXTERN int
2140 lwsws_get_config_vhosts(struct lws_context *context,
2141                         struct lws_context_creation_info *info, const char *d,
2142                         char **config_strings, int *len);
2143
2144 /*
2145  * Wsi-associated File Operations access helpers
2146  *
2147  * Use these helper functions if you want to access a file from the perspective
2148  * of a specific wsi, which is usually the case.  If you just want contextless
2149  * file access, use the fops callbacks directly with NULL wsi instead of these
2150  * helpers.
2151  *
2152  * If so, then it calls the platform handler or user overrides where present
2153  * (as defined in info->fops)
2154  *
2155  * The advantage from all this is user code can be portable for file operations
2156  * without having to deal with differences between platforms.
2157  */
2158
2159 static LWS_INLINE lws_filefd_type LWS_WARN_UNUSED_RESULT
2160 lws_plat_file_open(struct lws *wsi, const char *filename,
2161                    unsigned long *filelen, int flags)
2162 {
2163         return lws_get_fops(lws_get_context(wsi))->open(wsi, filename,
2164                                                     filelen, flags);
2165 }
2166
2167 static LWS_INLINE int
2168 lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
2169 {
2170         return lws_get_fops(lws_get_context(wsi))->close(wsi, fd);
2171 }
2172
2173 static LWS_INLINE unsigned long
2174 lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
2175 {
2176         return lws_get_fops(lws_get_context(wsi))->seek_cur(wsi, fd, offset);
2177 }
2178
2179 static LWS_INLINE int LWS_WARN_UNUSED_RESULT
2180 lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
2181                    unsigned char *buf, unsigned long len)
2182 {
2183         return lws_get_fops(lws_get_context(wsi))->read(wsi, fd, amount, buf,
2184                                                         len);
2185 }
2186
2187 static LWS_INLINE int LWS_WARN_UNUSED_RESULT
2188 lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
2189                     unsigned char *buf, unsigned long len)
2190 {
2191         return lws_get_fops(lws_get_context(wsi))->write(wsi, fd, amount, buf,
2192                                                          len);
2193 }
2194
2195 /*
2196  * Note: this is not normally needed as a user api.  It's provided in case it is
2197  * useful when integrating with other app poll loop service code.
2198  */
2199 LWS_VISIBLE LWS_EXTERN int
2200 lws_read(struct lws *wsi, unsigned char *buf, size_t len);
2201
2202 #ifndef LWS_NO_EXTENSIONS
2203 /* Deprecated
2204  *
2205  * There is no longer a set internal extensions table.  The table is provided
2206  * by user code along with application-specific settings.  See the test
2207  * client and server for how to do.
2208  */
2209 static LWS_INLINE LWS_WARN_DEPRECATED const struct lws_extension *
2210 lws_get_internal_extensions() { return NULL; }
2211 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
2212 lws_ext_parse_options(const struct lws_extension *ext, struct lws *wsi,
2213                        void *ext_user, const struct lws_ext_options *opts,
2214                        const char *o, int len);
2215 #endif
2216
2217 /*
2218  * custom allocator support
2219  */
2220 LWS_VISIBLE LWS_EXTERN void
2221 lws_set_allocator(void *(*realloc)(void *ptr, size_t size));
2222
2223 #ifdef __cplusplus
2224 }
2225 #endif
2226
2227 #endif