cgi: 5s grace to send buffered if chunked
[platform/upstream/libwebsockets.git] / lib / private-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 #include "lws_config.h"
23 #include "lws_config_private.h"
24
25
26 #if defined(LWS_WITH_CGI) && defined(LWS_HAVE_VFORK)
27 #define  _GNU_SOURCE
28 #endif
29
30 #ifdef LWS_HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <ctype.h>
39 #include <limits.h>
40 #include <stdarg.h>
41 #if defined(LWS_WITH_ESP8266)
42 #include <user_interface.h>
43 #define assert(n)
44
45 /* rom-provided stdc functions for free, ensure use these instead of libc ones */
46
47 int ets_vsprintf(char *str, const char *format, va_list argptr);
48 int ets_vsnprintf(char *buffer, size_t sizeOfBuffer,  const char *format, va_list argptr);
49 int ets_snprintf(char *str, size_t size, const char *format, ...);
50 int ets_sprintf(char *str, const char *format, ...);
51 int os_printf_plus(const char *format, ...);
52 #undef malloc
53 #undef realloc
54 #undef free
55 void *pvPortMalloc(size_t s, const char *f, int line);
56 #define malloc(s) pvPortMalloc(s, "", 0)
57 void *pvPortRealloc(void *p, size_t s, const char *f, int line);
58 #define realloc(p, s) pvPortRealloc(p, s, "", 0)
59 void vPortFree(void *p, const char *f, int line);
60 #define free(p) vPortFree(p, "", 0)
61 #undef memcpy
62 void *ets_memcpy(void *dest, const void *src, size_t n);
63 #define memcpy ets_memcpy
64 void *ets_memset(void *dest, int v, size_t n);
65 #define memset ets_memset
66 char *ets_strcpy(char *dest, const char *src);
67 #define strcpy ets_strcpy
68 char *ets_strncpy(char *dest, const char *src, size_t n);
69 #define strncpy ets_strncpy
70 char *ets_strstr(const char *haystack, const char *needle);
71 #define strstr ets_strstr
72 int ets_strcmp(const char *s1, const char *s2);
73 int ets_strncmp(const char *s1, const char *s2, size_t n);
74 #define strcmp ets_strcmp
75 #define strncmp ets_strncmp
76 size_t ets_strlen(const char *s);
77 #define strlen ets_strlen
78 void *ets_memmove(void *dest, const void *src, size_t n);
79 #define memmove ets_memmove
80 char *ets_strchr(const char *s, int c);
81 #define strchr_ets_strchr
82 #undef _DEBUG
83 #include <osapi.h>
84
85 #else
86 #define STORE_IN_ROM
87 #include <assert.h>
88 #endif
89 #if LWS_MAX_SMP > 1
90 #include <pthread.h>
91 #endif
92
93 #ifdef LWS_HAVE_SYS_STAT_H
94 #include <sys/stat.h>
95 #endif
96
97 #if defined(WIN32) || defined(_WIN32)
98 #if (WINVER < 0x0501)
99 #undef WINVER
100 #undef _WIN32_WINNT
101 #define WINVER 0x0501
102 #define _WIN32_WINNT WINVER
103 #endif
104 #define LWS_NO_DAEMONIZE
105 #define LWS_ERRNO WSAGetLastError()
106 #define LWS_EAGAIN WSAEWOULDBLOCK
107 #define LWS_EALREADY WSAEALREADY
108 #define LWS_EINPROGRESS WSAEINPROGRESS
109 #define LWS_EINTR WSAEINTR
110 #define LWS_EISCONN WSAEISCONN
111 #define LWS_EWOULDBLOCK WSAEWOULDBLOCK
112 #define MSG_NOSIGNAL 0
113 #define SHUT_RDWR SD_BOTH
114 #define SOL_TCP IPPROTO_TCP
115 #define SHUT_WR SD_SEND
116
117 #define compatible_close(fd) closesocket(fd)
118 #define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
119 #define lws_socket_is_valid(x) (!!x)
120 #define LWS_SOCK_INVALID 0
121 #include <winsock2.h>
122 #include <ws2tcpip.h>
123 #include <windows.h>
124 #include <tchar.h>
125 #ifdef LWS_HAVE_IN6ADDR_H
126 #include <in6addr.h>
127 #endif
128 #include <mstcpip.h>
129 #include <io.h>
130
131 #ifndef __func__
132 #define __func__ __FUNCTION__
133 #endif
134
135 #ifdef LWS_HAVE__VSNPRINTF
136 #define vsnprintf _vsnprintf
137 #endif
138
139 /* we don't have an implementation for this on windows... */
140 int kill(int pid, int sig);
141 int fork(void);
142 #ifndef SIGINT
143 #define SIGINT 2
144 #endif
145
146 #else /* not windows --> */
147
148 #include <fcntl.h>
149 #include <strings.h>
150 #include <unistd.h>
151 #include <sys/types.h>
152 #ifndef MBED_OPERATORS
153 #ifndef __cplusplus
154 #include <errno.h>
155 #endif
156 #include <netdb.h>
157 #include <signal.h>
158 #ifdef LWS_WITH_ESP8266
159 #include <sockets.h>
160 #define vsnprintf ets_vsnprintf
161 #define snprintf ets_snprintf
162 #define sprintf ets_sprintf
163
164 int kill(int pid, int sig);
165
166 #else
167 #include <sys/socket.h>
168 #endif
169 #ifdef LWS_WITH_HTTP_PROXY
170 #include <hubbub/hubbub.h>
171 #include <hubbub/parser.h>
172 #endif
173 #if defined(LWS_BUILTIN_GETIFADDRS)
174  #include <getifaddrs.h>
175 #else
176  #if !defined(LWS_WITH_ESP8266)
177  #include <ifaddrs.h>
178  #endif
179 #endif
180 #if defined (__ANDROID__)
181 #include <syslog.h>
182 #include <sys/resource.h>
183 #elif defined (__sun)
184 #include <syslog.h>
185 #else
186 #if !defined(LWS_WITH_ESP8266)
187 #include <sys/syslog.h>
188 #endif
189 #endif
190 #include <netdb.h>
191 #if !defined(LWS_WITH_ESP8266)
192 #include <sys/mman.h>
193 #include <sys/un.h>
194 #include <netinet/in.h>
195 #include <netinet/tcp.h>
196 #include <arpa/inet.h>
197 #include <poll.h>
198 #endif
199 #ifdef LWS_USE_LIBEV
200 #include <ev.h>
201 #endif
202 #ifdef LWS_USE_LIBUV
203 #include <uv.h>
204 #endif
205
206 #endif /* MBED */
207
208 #ifndef LWS_NO_FORK
209 #ifdef LWS_HAVE_SYS_PRCTL_H
210 #include <sys/prctl.h>
211 #endif
212 #endif
213
214 #include <sys/time.h>
215
216 #define LWS_ERRNO errno
217 #define LWS_EAGAIN EAGAIN
218 #define LWS_EALREADY EALREADY
219 #define LWS_EINPROGRESS EINPROGRESS
220 #define LWS_EINTR EINTR
221 #define LWS_EISCONN EISCONN
222 #define LWS_EWOULDBLOCK EWOULDBLOCK
223
224 #define lws_set_blocking_send(wsi)
225
226 #if defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
227 #define lws_socket_is_valid(x) ((x) != NULL)
228 #define LWS_SOCK_INVALID (NULL)
229 struct lws;
230 const char *
231 lws_plat_get_peer_simple(struct lws *wsi, char *name, int namelen);
232 #else
233 #define lws_socket_is_valid(x) (x >= 0)
234 #define LWS_SOCK_INVALID (-1)
235 #endif
236 #endif
237
238 #ifndef LWS_HAVE_BZERO
239 #ifndef bzero
240 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
241 #endif
242 #endif
243
244 #ifndef LWS_HAVE_STRERROR
245 #define strerror(x) ""
246 #endif
247
248 #ifdef LWS_OPENSSL_SUPPORT
249
250 #ifdef USE_WOLFSSL
251 #ifdef USE_OLD_CYASSL
252 #include <cyassl/openssl/ssl.h>
253 #include <cyassl/error-ssl.h>
254 #else
255 #include <wolfssl/openssl/ssl.h>
256 #include <wolfssl/error-ssl.h>
257 #endif /* not USE_OLD_CYASSL */
258 #else
259 #if defined(LWS_USE_POLARSSL)
260 #include <polarssl/ssl.h>
261 #include <polarssl/error.h>
262 #include <polarssl/md5.h>
263 #include <polarssl/sha1.h>
264 #include <polarssl/ecdh.h>
265 #define SSL_ERROR_WANT_READ POLARSSL_ERR_NET_WANT_READ
266 #define SSL_ERROR_WANT_WRITE POLARSSL_ERR_NET_WANT_WRITE
267 #define OPENSSL_VERSION_NUMBER  0x10002000L
268 #else
269 #if defined(LWS_USE_MBEDTLS)
270 #include <mbedtls/ssl.h>
271 #include <mbedtls/error.h>
272 #include <mbedtls/md5.h>
273 #include <mbedtls/sha1.h>
274 #include <mbedtls/ecdh.h>
275 #else
276 #include <openssl/ssl.h>
277 #include <openssl/evp.h>
278 #include <openssl/err.h>
279 #include <openssl/md5.h>
280 #include <openssl/sha.h>
281 #ifdef LWS_HAVE_OPENSSL_ECDH_H
282 #include <openssl/ecdh.h>
283 #endif
284 #include <openssl/x509v3.h>
285 #endif /* not USE_MBEDTLS */
286 #endif /* not USE_POLARSSL */
287 #endif /* not USE_WOLFSSL */
288 #endif
289
290 #include "libwebsockets.h"
291 #if defined(WIN32) || defined(_WIN32)
292 #else
293 static inline int compatible_close(int fd) { return close(fd); }
294 #endif
295
296 #if defined(WIN32) || defined(_WIN32)
297 #include <gettimeofday.h>
298 #endif
299
300 #if defined(MBED_OPERATORS)
301 #undef compatible_close
302 #define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
303 #ifndef BIG_ENDIAN
304 #define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
305 #endif
306 #ifndef LITTLE_ENDIAN
307 #define LITTLE_ENDIAN 1234
308 #endif
309 #ifndef BYTE_ORDER
310 #define BYTE_ORDER LITTLE_ENDIAN
311 #endif
312 #endif
313
314 #if defined(LWS_WITH_ESP8266)
315 #undef compatible_close
316 #define compatible_close(fd) { fd->state=ESPCONN_CLOSE; espconn_delete(fd); }
317 lws_sockfd_type
318 esp8266_create_tcp_stream_socket(void);
319 void
320 esp8266_tcp_stream_bind(lws_sockfd_type fd, int port, struct lws *wsi);
321 #ifndef BIG_ENDIAN
322 #define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
323 #endif
324 #ifndef LITTLE_ENDIAN
325 #define LITTLE_ENDIAN 1234
326 #endif
327 #ifndef BYTE_ORDER
328 #define BYTE_ORDER LITTLE_ENDIAN
329 #endif
330 #endif
331
332
333 #if defined(WIN32) || defined(_WIN32)
334
335 #ifndef BIG_ENDIAN
336 #define BIG_ENDIAN    4321  /* to show byte order (taken from gcc) */
337 #endif
338 #ifndef LITTLE_ENDIAN
339 #define LITTLE_ENDIAN 1234
340 #endif
341 #ifndef BYTE_ORDER
342 #define BYTE_ORDER LITTLE_ENDIAN
343 #endif
344 #ifndef u_int64_t
345 typedef unsigned __int64 u_int64_t;
346 #endif
347
348 #undef __P
349 #ifndef __P
350 #if __STDC__
351 #define __P(protos) protos
352 #else
353 #define __P(protos) ()
354 #endif
355 #endif
356
357 #else
358
359 #include <sys/stat.h>
360 #include <sys/time.h>
361
362 #if defined(__APPLE__)
363 #include <machine/endian.h>
364 #elif defined(__FreeBSD__)
365 #include <sys/endian.h>
366 #elif defined(__linux__)
367 #include <endian.h>
368 #endif
369
370 #ifdef __cplusplus
371 extern "C" {
372 #endif
373
374 #if defined(__QNX__)
375         #include <gulliver.h>
376         #if defined(__LITTLEENDIAN__)
377                 #define BYTE_ORDER __LITTLEENDIAN__
378                 #define LITTLE_ENDIAN __LITTLEENDIAN__
379                 #define BIG_ENDIAN 4321  /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
380         #endif
381         #if defined(__BIGENDIAN__)
382                 #define BYTE_ORDER __BIGENDIAN__
383                 #define LITTLE_ENDIAN 1234  /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
384                 #define BIG_ENDIAN __BIGENDIAN__
385         #endif
386 #endif
387
388 #if defined(__sun) && defined(__GNUC__)
389 # define BYTE_ORDER __BYTE_ORDER__
390 # define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
391 # define BIG_ENDIAN __ORDER_BIG_ENDIAN__
392 #endif
393
394 #if !defined(BYTE_ORDER)
395 # define BYTE_ORDER __BYTE_ORDER
396 #endif
397 #if !defined(LITTLE_ENDIAN)
398 # define LITTLE_ENDIAN __LITTLE_ENDIAN
399 #endif
400 #if !defined(BIG_ENDIAN)
401 # define BIG_ENDIAN __BIG_ENDIAN
402 #endif
403
404 #endif
405
406 /*
407  * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
408  * but happily have something equivalent in the SO_NOSIGPIPE flag.
409  */
410 #ifdef __APPLE__
411 #define MSG_NOSIGNAL SO_NOSIGPIPE
412 #endif
413
414 /*
415  * Solaris 11.X only supports POSIX 2001, MSG_NOSIGNAL appears in
416  * POSIX 2008.
417  */
418 #ifdef __sun
419 #define MSG_NOSIGNAL 0
420 #endif
421
422 #ifdef _WIN32
423 #ifndef FD_HASHTABLE_MODULUS
424 #define FD_HASHTABLE_MODULUS 32
425 #endif
426 #endif
427
428 #ifndef LWS_DEF_HEADER_LEN
429 #define LWS_DEF_HEADER_LEN 4096
430 #endif
431 #ifndef LWS_DEF_HEADER_POOL
432 #define LWS_DEF_HEADER_POOL 4
433 #endif
434 #ifndef LWS_MAX_PROTOCOLS
435 #define LWS_MAX_PROTOCOLS 5
436 #endif
437 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
438 #define LWS_MAX_EXTENSIONS_ACTIVE 2
439 #endif
440 #ifndef LWS_MAX_EXT_OFFERS
441 #define LWS_MAX_EXT_OFFERS 8
442 #endif
443 #ifndef SPEC_LATEST_SUPPORTED
444 #define SPEC_LATEST_SUPPORTED 13
445 #endif
446 #ifndef AWAITING_TIMEOUT
447 #define AWAITING_TIMEOUT 20
448 #endif
449 #ifndef CIPHERS_LIST_STRING
450 #define CIPHERS_LIST_STRING "DEFAULT"
451 #endif
452 #ifndef LWS_SOMAXCONN
453 #define LWS_SOMAXCONN SOMAXCONN
454 #endif
455
456 #define MAX_WEBSOCKET_04_KEY_LEN 128
457
458 #ifndef SYSTEM_RANDOM_FILEPATH
459 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
460 #endif
461
462 enum lws_websocket_opcodes_07 {
463         LWSWSOPC_CONTINUATION = 0,
464         LWSWSOPC_TEXT_FRAME = 1,
465         LWSWSOPC_BINARY_FRAME = 2,
466
467         LWSWSOPC_NOSPEC__MUX = 7,
468
469         /* control extensions 8+ */
470
471         LWSWSOPC_CLOSE = 8,
472         LWSWSOPC_PING = 9,
473         LWSWSOPC_PONG = 0xa,
474 };
475
476
477 enum lws_connection_states {
478         LWSS_HTTP,
479         LWSS_HTTP_ISSUING_FILE,
480         LWSS_HTTP_HEADERS,
481         LWSS_HTTP_BODY,
482         LWSS_DEAD_SOCKET,
483         LWSS_ESTABLISHED,
484         LWSS_CLIENT_HTTP_ESTABLISHED,
485         LWSS_CLIENT_UNCONNECTED,
486         LWSS_RETURNED_CLOSE_ALREADY,
487         LWSS_AWAITING_CLOSE_ACK,
488         LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
489         LWSS_SHUTDOWN,
490
491         LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
492         LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
493         LWSS_HTTP2_ESTABLISHED,
494
495         LWSS_CGI,
496 };
497
498 enum http_version {
499         HTTP_VERSION_1_0,
500         HTTP_VERSION_1_1,
501         HTTP_VERSION_2
502 };
503
504 enum http_connection_type {
505         HTTP_CONNECTION_CLOSE,
506         HTTP_CONNECTION_KEEP_ALIVE
507 };
508
509 enum lws_pending_protocol_send {
510         LWS_PPS_NONE,
511         LWS_PPS_HTTP2_MY_SETTINGS,
512         LWS_PPS_HTTP2_ACK_SETTINGS,
513         LWS_PPS_HTTP2_PONG,
514 };
515
516 enum lws_rx_parse_state {
517         LWS_RXPS_NEW,
518
519         LWS_RXPS_04_mask_1,
520         LWS_RXPS_04_mask_2,
521         LWS_RXPS_04_mask_3,
522
523         LWS_RXPS_04_FRAME_HDR_1,
524         LWS_RXPS_04_FRAME_HDR_LEN,
525         LWS_RXPS_04_FRAME_HDR_LEN16_2,
526         LWS_RXPS_04_FRAME_HDR_LEN16_1,
527         LWS_RXPS_04_FRAME_HDR_LEN64_8,
528         LWS_RXPS_04_FRAME_HDR_LEN64_7,
529         LWS_RXPS_04_FRAME_HDR_LEN64_6,
530         LWS_RXPS_04_FRAME_HDR_LEN64_5,
531         LWS_RXPS_04_FRAME_HDR_LEN64_4,
532         LWS_RXPS_04_FRAME_HDR_LEN64_3,
533         LWS_RXPS_04_FRAME_HDR_LEN64_2,
534         LWS_RXPS_04_FRAME_HDR_LEN64_1,
535
536         LWS_RXPS_07_COLLECT_FRAME_KEY_1,
537         LWS_RXPS_07_COLLECT_FRAME_KEY_2,
538         LWS_RXPS_07_COLLECT_FRAME_KEY_3,
539         LWS_RXPS_07_COLLECT_FRAME_KEY_4,
540
541         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
542 };
543
544 #define LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP 32
545
546 enum connection_mode {
547         LWSCM_HTTP_SERVING,
548         LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
549         LWSCM_PRE_WS_SERVING_ACCEPT,
550
551         LWSCM_WS_SERVING,
552         LWSCM_WS_CLIENT,
553
554         LWSCM_HTTP2_SERVING,
555
556         /* transient, ssl delay hiding */
557         LWSCM_SSL_ACK_PENDING,
558         LWSCM_SSL_INIT,
559
560         /* special internal types */
561         LWSCM_SERVER_LISTENER,
562         LWSCM_CGI, /* stdin, stdout, stderr for another cgi master wsi */
563
564         /* HTTP Client related */
565         LWSCM_HTTP_CLIENT = LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP,
566         LWSCM_HTTP_CLIENT_ACCEPTED, /* actual HTTP service going on */
567         LWSCM_WSCL_WAITING_CONNECT,
568         LWSCM_WSCL_WAITING_PROXY_REPLY,
569         LWSCM_WSCL_ISSUE_HANDSHAKE,
570         LWSCM_WSCL_ISSUE_HANDSHAKE2,
571         LWSCM_WSCL_ISSUE_HTTP_BODY,
572         LWSCM_WSCL_WAITING_SSL,
573         LWSCM_WSCL_WAITING_SERVER_REPLY,
574         LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
575         LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
576
577         /****** add new things just above ---^ ******/
578
579
580 };
581
582 enum {
583         LWS_RXFLOW_ALLOW = (1 << 0),
584         LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
585 };
586
587 /* this is not usable directly by user code any more, lws_close_reason() */
588 #define LWS_WRITE_CLOSE 4
589
590 struct lws_protocols;
591 struct lws;
592
593 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
594
595 struct lws_io_watcher {
596 #ifdef LWS_USE_LIBEV
597         ev_io ev_watcher;
598 #endif
599 #ifdef LWS_USE_LIBUV
600         uv_poll_t uv_watcher;
601 #endif
602         struct lws_context *context;
603 };
604
605 struct lws_signal_watcher {
606 #ifdef LWS_USE_LIBEV
607         ev_signal ev_watcher;
608 #endif
609 #ifdef LWS_USE_LIBUV
610         uv_signal_t uv_watcher;
611 #endif
612         struct lws_context *context;
613 };
614 #endif
615
616 #ifdef _WIN32
617 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
618 struct lws_fd_hashtable {
619         struct lws **wsi;
620         int length;
621 };
622 #endif
623
624 /*
625  * This is totally opaque to code using the library.  It's exported as a
626  * forward-reference pointer-only declaration; the user can use the pointer with
627  * other APIs to get information out of it.
628  */
629
630 struct lws_fragments {
631         unsigned int offset;
632         unsigned short len;
633         unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
634 };
635
636 /*
637  * these are assigned from a pool held in the context.
638  * Both client and server mode uses them for http header analysis
639  */
640
641 struct allocated_headers {
642         struct lws *wsi; /* owner */
643         char *data; /* prepared by context init to point to dedicated storage */
644         /*
645          * the randomly ordered fragments, indexed by frag_index and
646          * lws_fragments->nfrag for continuation.
647          */
648         struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
649         time_t assigned;
650         /*
651          * for each recognized token, frag_index says which frag[] his data
652          * starts in (0 means the token did not appear)
653          * the actual header data gets dumped as it comes in, into data[]
654          */
655         unsigned char frag_index[WSI_TOKEN_COUNT];
656         unsigned char rx[2048];
657
658         unsigned int rxpos;
659         unsigned int rxlen;
660         unsigned int pos;
661
662         unsigned int http_response;
663
664 #ifndef LWS_NO_CLIENT
665         char initial_handshake_hash_base64[30];
666 #endif
667
668         unsigned char in_use;
669         unsigned char nfrag;
670 };
671
672 /*
673  * so we can have n connections being serviced simultaneously,
674  * these things need to be isolated per-thread.
675  */
676
677 struct lws_context_per_thread {
678 #if LWS_MAX_SMP > 1
679         pthread_mutex_t lock;
680 #endif
681         struct lws_pollfd *fds;
682 #if defined(LWS_WITH_ESP8266)
683         struct lws **lws_vs_fds_index;
684 #endif
685         struct lws *rx_draining_ext_list;
686         struct lws *tx_draining_ext_list;
687         struct lws *timeout_list;
688 #ifdef LWS_USE_LIBUV
689         struct lws_context *context;
690 #endif
691 #ifdef LWS_WITH_CGI
692         struct lws_cgi *cgi_list;
693 #endif
694         void *http_header_data;
695         struct allocated_headers *ah_pool;
696         struct lws *ah_wait_list;
697         int ah_wait_list_length;
698 #ifdef LWS_OPENSSL_SUPPORT
699         struct lws *pending_read_list; /* linked list */
700 #endif
701 #if defined(LWS_USE_LIBEV)
702         struct ev_loop *io_loop_ev;
703 #endif
704 #if defined(LWS_USE_LIBUV)
705         uv_loop_t *io_loop_uv;
706         uv_signal_t signals[8];
707         uv_timer_t uv_timeout_watcher;
708         uv_idle_t uv_idle;
709 #endif
710 #if defined(LWS_USE_LIBEV)
711         struct lws_io_watcher w_accept;
712 #endif
713 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
714         struct lws_signal_watcher w_sigint;
715         unsigned char ev_loop_foreign:1;
716 #endif
717
718         unsigned long count_conns;
719         /*
720          * usable by anything in the service code, but only if the scope
721          * does not last longer than the service action (since next service
722          * of any socket can likewise use it and overwrite)
723          */
724         unsigned char *serv_buf;
725 #ifdef _WIN32
726         WSAEVENT *events;
727 #else
728         lws_sockfd_type dummy_pipe_fds[2];
729 #endif
730         unsigned int fds_count;
731
732         short ah_count_in_use;
733         unsigned char tid;
734         unsigned char lock_depth;
735 };
736
737 struct lws_conn_stats {
738         unsigned long long rx, tx;
739         unsigned long conn, trans, ws_upg, http2_upg, rejected;
740 };
741
742 void
743 lws_sum_stats(const struct lws_context *ctx, struct lws_conn_stats *cs);
744
745 /*
746  * virtual host -related context information
747  *   vhostwide SSL context
748  *   vhostwide proxy
749  *
750  * hierarchy:
751  *
752  * context -> vhost -> wsi
753  *
754  * incoming connection non-SSL vhost binding:
755  *
756  *    listen socket -> wsi -> select vhost after first headers
757  *
758  * incoming connection SSL vhost binding:
759  *
760  *    SSL SNI -> wsi -> bind after SSL negotiation
761  */
762
763 struct lws_vhost {
764 #if !defined(LWS_WITH_ESP8266)
765         char http_proxy_address[128];
766         char proxy_basic_auth_token[128];
767 #endif
768 #if defined(LWS_WITH_ESP8266)
769         /* listen sockets need a place to hang their hat */
770         esp_tcp tcp;
771 #endif
772         struct lws_conn_stats conn_stats;
773         struct lws_context *context;
774         struct lws_vhost *vhost_next;
775         const struct lws_http_mount *mount_list;
776         struct lws *lserv_wsi;
777         const char *name;
778         const char *iface;
779         const struct lws_protocols *protocols;
780         void **protocol_vh_privs;
781         const struct lws_protocol_vhost_options *pvo;
782         const struct lws_protocol_vhost_options *headers;
783         struct lws **same_vh_protocol_list;
784 #ifdef LWS_OPENSSL_SUPPORT
785         SSL_CTX *ssl_ctx;
786         SSL_CTX *ssl_client_ctx;
787 #endif
788 #ifndef LWS_NO_EXTENSIONS
789         const struct lws_extension *extensions;
790 #endif
791
792         int listen_port;
793         unsigned int http_proxy_port;
794         unsigned int options;
795         int count_protocols;
796         int ka_time;
797         int ka_probes;
798         int ka_interval;
799         int keepalive_timeout;
800 #ifdef LWS_WITH_ACCESS_LOG
801         int log_fd;
802 #endif
803
804 #ifdef LWS_OPENSSL_SUPPORT
805         int use_ssl;
806         int allow_non_ssl_on_ssl_port;
807         unsigned int user_supplied_ssl_ctx:1;
808 #endif
809
810         unsigned int created_vhost_protocols:1;
811
812         unsigned char default_protocol_index;
813 };
814
815 /*
816  * the rest is managed per-context, that includes
817  *
818  *  - processwide single fd -> wsi lookup
819  *  - contextwide headers pool
820  */
821
822 struct lws_context {
823         time_t last_timeout_check_s;
824         time_t last_ws_ping_pong_check_s;
825         time_t time_up;
826         struct lws_plat_file_ops fops;
827         struct lws_context_per_thread pt[LWS_MAX_SMP];
828         struct lws_conn_stats conn_stats;
829 #ifdef _WIN32
830 /* different implementation between unix and windows */
831         struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
832 #else
833 #if defined(LWS_WITH_ESP8266)
834         struct espconn **connpool; /* .reverse points to the wsi */
835         void *rxd;
836         int rxd_len;
837         os_timer_t to_timer;
838 #else
839         struct lws **lws_lookup;  /* fd to wsi */
840 #endif
841 #endif
842         struct lws_vhost *vhost_list;
843         struct lws_plugin *plugin_list;
844
845         void *external_baggage_free_on_destroy;
846         const struct lws_token_limits *token_limits;
847         void *user_space;
848         const char *server_string;
849         const struct lws_protocol_vhost_options *reject_service_keywords;
850         lws_reload_func deprecation_cb;
851
852 #if defined(LWS_USE_LIBEV)
853         lws_ev_signal_cb_t * lws_ev_sigint_cb;
854 #endif
855 #if defined(LWS_USE_LIBUV)
856         uv_signal_cb lws_uv_sigint_cb;
857 #endif
858         char canonical_hostname[128];
859 #ifdef LWS_LATENCY
860         unsigned long worst_latency;
861         char worst_latency_info[256];
862 #endif
863
864         int max_fds;
865 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
866         int use_ev_sigint;
867 #endif
868         int started_with_parent;
869         int uid, gid;
870
871         int fd_random;
872 #ifdef LWS_OPENSSL_SUPPORT
873 #define lws_ssl_anybody_has_buffered_read(w) \
874                 (w->vhost->use_ssl && \
875                  w->context->pt[(int)w->tsi].pending_read_list)
876 #define lws_ssl_anybody_has_buffered_read_tsi(c, t) \
877                 (/*c->use_ssl && */ \
878                  c->pt[(int)t].pending_read_list)
879 #else
880 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
881 #define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
882 #endif
883         int count_wsi_allocated;
884         int count_cgi_spawned;
885         unsigned int options;
886         unsigned int fd_limit_per_thread;
887         unsigned int timeout_secs;
888         unsigned int pt_serv_buf_size;
889         int max_http_header_data;
890
891         unsigned int deprecated:1;
892         unsigned int being_destroyed:1;
893         unsigned int being_destroyed1:1;
894         unsigned int requested_kill:1;
895         unsigned int protocol_init_done:1;
896
897         /*
898          * set to the Thread ID that's doing the service loop just before entry
899          * to poll indicates service thread likely idling in poll()
900          * volatile because other threads may check it as part of processing
901          * for pollfd event change.
902          */
903         volatile int service_tid;
904         int service_tid_detected;
905
906         short max_http_header_pool;
907         short count_threads;
908         short plugin_protocol_count;
909         short plugin_extension_count;
910         short server_string_len;
911         unsigned short ws_ping_pong_interval;
912         unsigned short deprecation_pending_listen_close_count;
913 };
914
915 #define lws_get_context_protocol(ctx, x) ctx->vhost_list->protocols[x]
916 #define lws_get_vh_protocol(vh, x) vh->protocols[x]
917
918 LWS_EXTERN void
919 lws_close_free_wsi_final(struct lws *wsi);
920 LWS_EXTERN void
921 lws_libuv_closehandle(struct lws *wsi);
922
923 LWS_VISIBLE LWS_EXTERN int
924 lws_plat_plugins_init(struct lws_context * context, const char * const *d);
925
926 LWS_VISIBLE LWS_EXTERN int
927 lws_plat_plugins_destroy(struct lws_context * context);
928
929 LWS_EXTERN void
930 lws_restart_ws_ping_pong_timer(struct lws *wsi);
931
932 struct lws *
933 lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
934
935
936 enum {
937         LWS_EV_READ = (1 << 0),
938         LWS_EV_WRITE = (1 << 1),
939         LWS_EV_START = (1 << 2),
940         LWS_EV_STOP = (1 << 3),
941
942         LWS_EV_PREPARE_DELETION = (1 << 31),
943 };
944
945 #if defined(LWS_USE_LIBEV)
946 LWS_EXTERN void
947 lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
948 LWS_EXTERN void
949 lws_libev_io(struct lws *wsi, int flags);
950 LWS_EXTERN int
951 lws_libev_init_fd_table(struct lws_context *context);
952 LWS_EXTERN void
953 lws_libev_destroyloop(struct lws_context *context, int tsi);
954 LWS_EXTERN void
955 lws_libev_run(const struct lws_context *context, int tsi);
956 #define LWS_LIBEV_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEV)
957 LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
958 #else
959 #define lws_libev_accept(_a, _b) ((void) 0)
960 #define lws_libev_io(_a, _b) ((void) 0)
961 #define lws_libev_init_fd_table(_a) (0)
962 #define lws_libev_run(_a, _b) ((void) 0)
963 #define lws_libev_destroyloop(_a, _b) ((void) 0)
964 #define LWS_LIBEV_ENABLED(context) (0)
965 #if LWS_POSIX
966 #define lws_feature_status_libev(_a) \
967                         lwsl_notice("libev support not compiled in\n")
968 #else
969 #define lws_feature_status_libev(_a)
970 #endif
971 #endif
972
973 #if defined(LWS_USE_LIBUV)
974 LWS_EXTERN void
975 lws_libuv_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
976 LWS_EXTERN void
977 lws_libuv_io(struct lws *wsi, int flags);
978 LWS_EXTERN int
979 lws_libuv_init_fd_table(struct lws_context *context);
980 LWS_EXTERN void
981 lws_libuv_run(const struct lws_context *context, int tsi);
982 LWS_EXTERN void
983 lws_libuv_destroyloop(struct lws_context *context, int tsi);
984 LWS_EXTERN int
985 lws_uv_initvhost(struct lws_vhost* vh, struct lws*);
986 #define LWS_LIBUV_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV)
987 LWS_EXTERN void lws_feature_status_libuv(struct lws_context_creation_info *info);
988 #else
989 #define lws_libuv_accept(_a, _b) ((void) 0)
990 #define lws_libuv_io(_a, _b) ((void) 0)
991 #define lws_libuv_init_fd_table(_a) (0)
992 #define lws_libuv_run(_a, _b) ((void) 0)
993 #define lws_libuv_destroyloop(_a, _b) ((void) 0)
994 #define LWS_LIBUV_ENABLED(context) (0)
995 #if LWS_POSIX
996 #define lws_feature_status_libuv(_a) \
997                         lwsl_notice("libuv support not compiled in\n")
998 #else
999 #define lws_feature_status_libuv(_a)
1000 #endif
1001 #endif
1002
1003
1004 #ifdef LWS_USE_IPV6
1005 #define LWS_IPV6_ENABLED(vh) \
1006         (!lws_check_opt(vh->context->options, LWS_SERVER_OPTION_DISABLE_IPV6) && \
1007          !lws_check_opt(vh->options, LWS_SERVER_OPTION_DISABLE_IPV6))
1008 #else
1009 #define LWS_IPV6_ENABLED(context) (0)
1010 #endif
1011
1012 #ifdef LWS_USE_UNIX_SOCK
1013 #define LWS_UNIX_SOCK_ENABLED(vhost) \
1014         (vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
1015 #else
1016 #define LWS_UNIX_SOCK_ENABLED(vhost) (0)
1017 #endif
1018 enum uri_path_states {
1019         URIPS_IDLE,
1020         URIPS_SEEN_SLASH,
1021         URIPS_SEEN_SLASH_DOT,
1022         URIPS_SEEN_SLASH_DOT_DOT,
1023 };
1024
1025 enum uri_esc_states {
1026         URIES_IDLE,
1027         URIES_SEEN_PERCENT,
1028         URIES_SEEN_PERCENT_H1,
1029 };
1030
1031 /* notice that these union members:
1032  *
1033  *  hdr
1034  *  http
1035  *  http2
1036  *
1037  * all have a pointer to allocated_headers struct as their first member.
1038  *
1039  * It means for allocated_headers access, the three union paths can all be
1040  * used interchangeably to access the same data
1041  */
1042
1043
1044 #ifndef LWS_NO_CLIENT
1045 struct client_info_stash {
1046         char address[256];
1047         char path[4096];
1048         char host[256];
1049         char origin[256];
1050         char protocol[256];
1051         char method[16];
1052 };
1053 #endif
1054
1055 struct _lws_header_related {
1056         /* MUST be first in struct */
1057         struct allocated_headers *ah;
1058         struct lws *ah_wait_list;
1059         unsigned char *preamble_rx;
1060 #ifndef LWS_NO_CLIENT
1061         struct client_info_stash *stash;
1062 #endif
1063         unsigned int preamble_rx_len;
1064         enum uri_path_states ups;
1065         enum uri_esc_states ues;
1066         short lextable_pos;
1067         unsigned int current_token_limit;
1068 #ifndef LWS_NO_CLIENT
1069         unsigned short c_port;
1070 #endif
1071         char esc_stash;
1072         char post_literal_equal;
1073         unsigned char parser_state; /* enum lws_token_indexes */
1074         char redirects;
1075 };
1076
1077 #if defined(LWS_WITH_RANGES)
1078 enum range_states {
1079         LWSRS_NO_ACTIVE_RANGE,
1080         LWSRS_BYTES_EQ,
1081         LWSRS_FIRST,
1082         LWSRS_STARTING,
1083         LWSRS_ENDING,
1084         LWSRS_COMPLETED,
1085         LWSRS_SYNTAX,
1086 };
1087
1088 struct lws_range_parsing {
1089         unsigned long long start, end, extent, agg, budget;
1090         const char buf[128];
1091         int pos;
1092         enum range_states state;
1093         char start_valid, end_valid, ctr, count_ranges, did_try, inside, send_ctr;
1094 };
1095
1096 int
1097 lws_ranges_init(struct lws *wsi, struct lws_range_parsing *rp, unsigned long long extent);
1098 int
1099 lws_ranges_next(struct lws_range_parsing *rp);
1100 void
1101 lws_ranges_reset(struct lws_range_parsing *rp);
1102 #endif
1103
1104 struct _lws_http_mode_related {
1105         /* MUST be first in struct */
1106         struct allocated_headers *ah; /* mirroring  _lws_header_related */
1107         struct lws *ah_wait_list;
1108         unsigned char *preamble_rx;
1109 #ifndef LWS_NO_CLIENT
1110         struct client_info_stash *stash;
1111 #endif
1112         unsigned int preamble_rx_len;
1113         struct lws *new_wsi_list;
1114         unsigned long filepos;
1115         unsigned long filelen;
1116         lws_filefd_type fd;
1117
1118 #if defined(LWS_WITH_RANGES)
1119         struct lws_range_parsing range;
1120         char multipart_content_type[64];
1121 #endif
1122
1123         enum http_version request_version;
1124         enum http_connection_type connection_type;
1125         unsigned int content_length;
1126         unsigned int content_remain;
1127 };
1128
1129 #ifdef LWS_USE_HTTP2
1130
1131 enum lws_http2_settings {
1132         LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
1133         LWS_HTTP2_SETTINGS__ENABLE_PUSH,
1134         LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
1135         LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
1136         LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
1137         LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
1138
1139         LWS_HTTP2_SETTINGS__COUNT /* always last */
1140 };
1141
1142 enum lws_http2_wellknown_frame_types {
1143         LWS_HTTP2_FRAME_TYPE_DATA,
1144         LWS_HTTP2_FRAME_TYPE_HEADERS,
1145         LWS_HTTP2_FRAME_TYPE_PRIORITY,
1146         LWS_HTTP2_FRAME_TYPE_RST_STREAM,
1147         LWS_HTTP2_FRAME_TYPE_SETTINGS,
1148         LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
1149         LWS_HTTP2_FRAME_TYPE_PING,
1150         LWS_HTTP2_FRAME_TYPE_GOAWAY,
1151         LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
1152         LWS_HTTP2_FRAME_TYPE_CONTINUATION,
1153
1154         LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
1155 };
1156
1157 enum lws_http2_flags {
1158         LWS_HTTP2_FLAG_END_STREAM = 1,
1159         LWS_HTTP2_FLAG_END_HEADERS = 4,
1160         LWS_HTTP2_FLAG_PADDED = 8,
1161         LWS_HTTP2_FLAG_PRIORITY = 0x20,
1162
1163         LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
1164 };
1165
1166 #define LWS_HTTP2_STREAM_ID_MASTER 0
1167 #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
1168 #define LWS_HTTP2_SETTINGS_LENGTH 6
1169
1170 struct http2_settings {
1171         unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
1172 };
1173
1174 enum http2_hpack_state {
1175
1176         /* optional before first header block */
1177         HPKS_OPT_PADDING,
1178         HKPS_OPT_E_DEPENDENCY,
1179         HKPS_OPT_WEIGHT,
1180
1181         /* header block */
1182         HPKS_TYPE,
1183
1184         HPKS_IDX_EXT,
1185
1186         HPKS_HLEN,
1187         HPKS_HLEN_EXT,
1188
1189         HPKS_DATA,
1190
1191         /* optional after last header block */
1192         HKPS_OPT_DISCARD_PADDING,
1193 };
1194
1195 enum http2_hpack_type {
1196         HPKT_INDEXED_HDR_7,
1197         HPKT_INDEXED_HDR_6_VALUE_INCR,
1198         HPKT_LITERAL_HDR_VALUE_INCR,
1199         HPKT_INDEXED_HDR_4_VALUE,
1200         HPKT_LITERAL_HDR_VALUE,
1201         HPKT_SIZE_5
1202 };
1203
1204 struct hpack_dt_entry {
1205         int token; /* additions that don't map to a token are ignored */
1206         int arg_offset;
1207         int arg_len;
1208 };
1209
1210 struct hpack_dynamic_table {
1211         struct hpack_dt_entry *entries;
1212         char *args;
1213         int pos;
1214         int next;
1215         int num_entries;
1216         int args_length;
1217 };
1218
1219 struct _lws_http2_related {
1220         /*
1221          * having this first lets us also re-use all HTTP union code
1222          * and in turn, http_mode_related has allocated headers in right
1223          * place so we can use the header apis on the wsi directly still
1224          */
1225         struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
1226
1227         struct http2_settings my_settings;
1228         struct http2_settings peer_settings;
1229
1230         struct lws *parent_wsi;
1231         struct lws *next_child_wsi;
1232
1233         struct hpack_dynamic_table *hpack_dyn_table;
1234         struct lws *stream_wsi;
1235         unsigned char ping_payload[8];
1236         unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
1237
1238         unsigned int count;
1239         unsigned int length;
1240         unsigned int stream_id;
1241         enum http2_hpack_state hpack;
1242         enum http2_hpack_type hpack_type;
1243         unsigned int header_index;
1244         unsigned int hpack_len;
1245         unsigned int hpack_e_dep;
1246         int tx_credit;
1247         unsigned int my_stream_id;
1248         unsigned int child_count;
1249         int my_priority;
1250
1251         unsigned int END_STREAM:1;
1252         unsigned int END_HEADERS:1;
1253         unsigned int send_END_STREAM:1;
1254         unsigned int GOING_AWAY;
1255         unsigned int requested_POLLOUT:1;
1256         unsigned int waiting_tx_credit:1;
1257         unsigned int huff:1;
1258         unsigned int value:1;
1259
1260         unsigned short round_robin_POLLOUT;
1261         unsigned short count_POLLOUT_children;
1262         unsigned short hpack_pos;
1263
1264         unsigned char type;
1265         unsigned char flags;
1266         unsigned char frame_state;
1267         unsigned char padding;
1268         unsigned char hpack_m;
1269         unsigned char initialized;
1270 };
1271
1272 #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
1273
1274 #endif
1275
1276 struct _lws_websocket_related {
1277         /* cheapest way to deal with ah overlap with ws union transition */
1278         struct _lws_header_related hdr;
1279         char *rx_ubuf;
1280         unsigned int rx_ubuf_alloc;
1281         struct lws *rx_draining_ext_list;
1282         struct lws *tx_draining_ext_list;
1283         time_t time_next_ping_check;
1284         size_t rx_packet_length;
1285         unsigned int rx_ubuf_head;
1286         unsigned char mask[4];
1287         /* Also used for close content... control opcode == < 128 */
1288         unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
1289
1290         unsigned char ping_payload_len;
1291         unsigned char mask_idx;
1292         unsigned char opcode;
1293         unsigned char rsv;
1294         unsigned char rsv_first_msg;
1295         /* zero if no info, or length including 2-byte close code */
1296         unsigned char close_in_ping_buffer_len;
1297         unsigned char utf8;
1298         unsigned char stashed_write_type;
1299         unsigned char tx_draining_stashed_wp;
1300
1301         unsigned int final:1;
1302         unsigned int frame_is_binary:1;
1303         unsigned int all_zero_nonce:1;
1304         unsigned int this_frame_masked:1;
1305         unsigned int inside_frame:1; /* next write will be more of frame */
1306         unsigned int clean_buffer:1; /* buffer not rewritten by extension */
1307         unsigned int payload_is_close:1; /* process as PONG, but it is close */
1308         unsigned int ping_pending_flag:1;
1309         unsigned int continuation_possible:1;
1310         unsigned int owed_a_fin:1;
1311         unsigned int check_utf8:1;
1312         unsigned int defeat_check_utf8:1;
1313         unsigned int pmce_compressed_message:1;
1314         unsigned int stashed_write_pending:1;
1315         unsigned int rx_draining_ext:1;
1316         unsigned int tx_draining_ext:1;
1317         unsigned int send_check_ping:1;
1318 };
1319
1320 #ifdef LWS_WITH_CGI
1321
1322 /* wsi who is master of the cgi points to an lws_cgi */
1323
1324 struct lws_cgi {
1325         struct lws_cgi *cgi_list;
1326         struct lws *stdwsi[3]; /* points to the associated stdin/out/err wsis */
1327         struct lws *wsi; /* owner */
1328         unsigned long content_length;
1329         unsigned long content_length_seen;
1330         int pipe_fds[3][2];
1331         int pid;
1332
1333         unsigned int being_closed:1;
1334
1335         unsigned char chunked_grace;
1336 };
1337 #endif
1338
1339 signed char char_to_hex(const char c);
1340
1341 #ifndef LWS_NO_CLIENT
1342 enum lws_chunk_parser {
1343         ELCP_HEX,
1344         ELCP_CR,
1345         ELCP_CONTENT,
1346         ELCP_POST_CR,
1347         ELCP_POST_LF,
1348 };
1349 #endif
1350
1351 struct lws_rewrite;
1352
1353 #ifdef LWS_WITH_ACCESS_LOG
1354 struct lws_access_log {
1355         char *header_log;
1356         char *user_agent;
1357         unsigned long sent;
1358         int response;
1359 };
1360 #endif
1361
1362 struct lws {
1363
1364         /* structs */
1365         /* members with mutually exclusive lifetimes are unionized */
1366
1367         union u {
1368                 struct _lws_http_mode_related http;
1369 #ifdef LWS_USE_HTTP2
1370                 struct _lws_http2_related http2;
1371 #endif
1372                 struct _lws_header_related hdr;
1373                 struct _lws_websocket_related ws;
1374         } u;
1375
1376         /* lifetime members */
1377
1378 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
1379         struct lws_io_watcher w_read;
1380 #endif
1381 #if defined(LWS_USE_LIBEV)
1382         struct lws_io_watcher w_write;
1383 #endif
1384         time_t pending_timeout_limit;
1385
1386         /* pointers */
1387
1388         struct lws_context *context;
1389         struct lws_vhost *vhost;
1390         struct lws *parent; /* points to parent, if any */
1391         struct lws *child_list; /* points to first child */
1392         struct lws *sibling_list; /* subsequent children at same level */
1393 #ifdef LWS_WITH_CGI
1394         struct lws_cgi *cgi; /* wsi being cgi master have one of these */
1395 #endif
1396         const struct lws_protocols *protocol;
1397         struct lws **same_vh_protocol_prev, *same_vh_protocol_next;
1398         struct lws *timeout_list;
1399         struct lws **timeout_list_prev;
1400 #ifdef LWS_WITH_ACCESS_LOG
1401         struct lws_access_log access_log;
1402 #endif
1403         void *user_space;
1404         /* rxflow handling */
1405         unsigned char *rxflow_buffer;
1406         /* truncated send handling */
1407         unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
1408
1409 #if defined (LWS_WITH_ESP8266)
1410         void *premature_rx;
1411         unsigned short prem_rx_size, prem_rx_pos;
1412 #endif
1413
1414 #ifndef LWS_NO_EXTENSIONS
1415         const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
1416         void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
1417 #endif
1418 #ifdef LWS_OPENSSL_SUPPORT
1419         SSL *ssl;
1420 #if !defined(LWS_USE_POLARSSL) && !defined(LWS_USE_MBEDTLS)
1421         BIO *client_bio;
1422 #endif
1423         struct lws *pending_read_list_prev, *pending_read_list_next;
1424 #endif
1425 #ifdef LWS_WITH_HTTP_PROXY
1426         struct lws_rewrite *rw;
1427 #endif
1428 #ifdef LWS_LATENCY
1429         unsigned long action_start;
1430         unsigned long latency_start;
1431 #endif
1432         /* pointer / int */
1433         lws_sockfd_type sock;
1434
1435         /* ints */
1436         int position_in_fds_table;
1437         int rxflow_len;
1438         int rxflow_pos;
1439         unsigned int trunc_alloc_len; /* size of malloc */
1440         unsigned int trunc_offset; /* where we are in terms of spilling */
1441         unsigned int trunc_len; /* how much is buffered */
1442 #ifndef LWS_NO_CLIENT
1443         int chunk_remaining;
1444 #endif
1445         unsigned int cache_secs;
1446
1447         unsigned int hdr_parsing_completed:1;
1448         unsigned int http2_substream:1;
1449         unsigned int listener:1;
1450         unsigned int user_space_externally_allocated:1;
1451         unsigned int socket_is_permanently_unusable:1;
1452         unsigned int rxflow_change_to:2;
1453         unsigned int more_rx_waiting:1; /* has to live here since ah may stick to end */
1454         unsigned int conn_stat_done:1;
1455         unsigned int cache_reuse:1;
1456         unsigned int cache_revalidate:1;
1457         unsigned int cache_intermediaries:1;
1458         unsigned int favoured_pollin:1;
1459         unsigned int sending_chunked:1;
1460         unsigned int already_did_cce:1;
1461         unsigned int told_user_closed:1;
1462         unsigned int :1;
1463 #if defined(LWS_WITH_ESP8266)
1464         unsigned int pending_send_completion:3;
1465         unsigned int close_is_pending_send_completion:1;
1466 #endif
1467 #ifdef LWS_WITH_ACCESS_LOG
1468         unsigned int access_log_pending:1;
1469 #endif
1470 #ifndef LWS_NO_CLIENT
1471         unsigned int do_ws:1; /* whether we are doing http or ws flow */
1472         unsigned int chunked:1; /* if the clientside connection is chunked */
1473         unsigned int client_rx_avail:1;
1474         unsigned int client_http_body_pending:1;
1475 #endif
1476 #ifdef LWS_WITH_HTTP_PROXY
1477         unsigned int perform_rewrite:1;
1478 #endif
1479 #ifndef LWS_NO_EXTENSIONS
1480         unsigned int extension_data_pending:1;
1481 #endif
1482 #ifdef LWS_OPENSSL_SUPPORT
1483         unsigned int use_ssl:3;
1484         unsigned int upgraded:1;
1485 #endif
1486 #ifdef _WIN32
1487         unsigned int sock_send_blocking:1;
1488 #endif
1489 #ifdef LWS_OPENSSL_SUPPORT
1490         unsigned int redirect_to_https:1;
1491 #endif
1492
1493         /* chars */
1494 #ifndef LWS_NO_EXTENSIONS
1495         unsigned char count_act_ext;
1496 #endif
1497         unsigned char ietf_spec_revision;
1498         char mode; /* enum connection_mode */
1499         char state; /* enum lws_connection_states */
1500         char state_pre_close;
1501         char lws_rx_parse_state; /* enum lws_rx_parse_state */
1502         char rx_frame_type; /* enum lws_write_protocol */
1503         char pending_timeout; /* enum pending_timeout */
1504         char pps; /* enum lws_pending_protocol_send */
1505         char tsi; /* thread service index we belong to */
1506         char protocol_interpret_idx;
1507 #ifdef LWS_WITH_CGI
1508         char cgi_channel; /* which of stdin/out/err */
1509         char hdr_state;
1510 #endif
1511 #ifndef LWS_NO_CLIENT
1512         char chunk_parser; /* enum lws_chunk_parser */
1513 #endif
1514 #if defined(LWS_WITH_CGI) || !defined(LWS_NO_CLIENT)
1515         char reason_bf; /* internal writeable callback reason bitfield */
1516 #endif
1517 };
1518
1519 LWS_EXTERN int log_level;
1520
1521 LWS_EXTERN int
1522 lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
1523                 const char *iface);
1524
1525 LWS_EXTERN void
1526 lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
1527
1528 LWS_EXTERN int
1529 remove_wsi_socket_from_fds(struct lws *wsi);
1530 LWS_EXTERN int
1531 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
1532
1533 #ifndef LWS_LATENCY
1534 static inline void
1535 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1536             int ret, int completion) {
1537         do {
1538                 (void)context; (void)wsi; (void)action; (void)ret;
1539                 (void)completion;
1540         } while (0);
1541 }
1542 static inline void
1543 lws_latency_pre(struct lws_context *context, struct lws *wsi) {
1544         do { (void)context; (void)wsi; } while (0);
1545 }
1546 #else
1547 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
1548 extern void
1549 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1550             int ret, int completion);
1551 #endif
1552
1553 LWS_EXTERN void
1554 lws_set_protocol_write_pending(struct lws *wsi,
1555                                enum lws_pending_protocol_send pend);
1556 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1557 lws_client_rx_sm(struct lws *wsi, unsigned char c);
1558
1559 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1560 lws_parse(struct lws *wsi, unsigned char c);
1561
1562 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1563 lws_http_action(struct lws *wsi);
1564
1565 LWS_EXTERN int
1566 lws_b64_selftest(void);
1567
1568 LWS_EXTERN int
1569 lws_service_flag_pending(struct lws_context *context, int tsi);
1570
1571 #if defined(_WIN32) || defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
1572 LWS_EXTERN struct lws *
1573 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
1574
1575 LWS_EXTERN int
1576 insert_wsi(struct lws_context *context, struct lws *wsi);
1577
1578 LWS_EXTERN int
1579 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
1580 #else
1581 #define wsi_from_fd(A,B)  A->lws_lookup[B]
1582 #define insert_wsi(A,B)   assert(A->lws_lookup[B->sock] == 0); A->lws_lookup[B->sock]=B
1583 #define delete_from_fd(A,B) A->lws_lookup[B]=0
1584 #endif
1585
1586 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1587 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
1588
1589 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1590 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
1591
1592
1593 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1594 lws_service_timeout_check(struct lws *wsi, unsigned int sec);
1595
1596 LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1597 lws_client_connect_2(struct lws *wsi);
1598
1599 LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
1600 lws_client_reset(struct lws *wsi, int ssl, const char *address, int port,
1601                  const char *path, const char *host);
1602
1603 LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1604 lws_create_new_server_wsi(struct lws_vhost *vhost);
1605
1606 LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
1607 lws_generate_client_handshake(struct lws *wsi, char *pkt);
1608
1609 LWS_EXTERN int
1610 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
1611
1612 LWS_EXTERN struct lws *
1613 lws_client_connect_via_info2(struct lws *wsi);
1614
1615 /*
1616  * EXTENSIONS
1617  */
1618
1619 #ifndef LWS_NO_EXTENSIONS
1620 LWS_VISIBLE void
1621 lws_context_init_extensions(struct lws_context_creation_info *info,
1622                             struct lws_context *context);
1623 LWS_EXTERN int
1624 lws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
1625                           void *v, size_t len);
1626
1627 LWS_EXTERN int
1628 lws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
1629 LWS_EXTERN int
1630 lws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
1631                     void *arg, int len);
1632
1633 #else
1634 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
1635 #define lws_ext_cb_active(_a, _b, _c, _d) (0)
1636 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
1637 #define lws_issue_raw_ext_access lws_issue_raw
1638 #define lws_context_init_extensions(_a, _b)
1639 #endif
1640
1641 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1642 lws_client_interpret_server_handshake(struct lws *wsi);
1643
1644 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1645 lws_rx_sm(struct lws *wsi, unsigned char c);
1646
1647 LWS_EXTERN int
1648 lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf, size_t *len);
1649
1650 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1651 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1652
1653 LWS_EXTERN void
1654 lws_union_transition(struct lws *wsi, enum connection_mode mode);
1655
1656 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1657 user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
1658                             enum lws_callback_reasons reason, void *user,
1659                             void *in, size_t len);
1660 #ifdef LWS_USE_HTTP2
1661 LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1662 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
1663 LWS_EXTERN int
1664 lws_http2_interpret_settings_payload(struct http2_settings *settings,
1665                                      unsigned char *buf, int len);
1666 LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1667 LWS_EXTERN int
1668 lws_http2_parser(struct lws *wsi, unsigned char c);
1669 LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1670                                      struct lws *wsi);
1671 LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1672                                      unsigned int sid, unsigned int len,
1673                                      unsigned char *buf);
1674 LWS_EXTERN struct lws *
1675 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
1676 LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
1677                                    unsigned char c);
1678 LWS_EXTERN int
1679 lws_add_http2_header_by_name(struct lws *wsi,
1680                              const unsigned char *name,
1681                              const unsigned char *value, int length,
1682                              unsigned char **p, unsigned char *end);
1683 LWS_EXTERN int
1684 lws_add_http2_header_by_token(struct lws *wsi,
1685                             enum lws_token_indexes token,
1686                             const unsigned char *value, int length,
1687                             unsigned char **p, unsigned char *end);
1688 LWS_EXTERN int
1689 lws_add_http2_header_status(struct lws *wsi,
1690                             unsigned int code, unsigned char **p,
1691                             unsigned char *end);
1692 LWS_EXTERN
1693 void lws_http2_configure_if_upgraded(struct lws *wsi);
1694 #else
1695 #define lws_http2_configure_if_upgraded(x)
1696 #endif
1697
1698 LWS_EXTERN int
1699 lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd);
1700
1701 LWS_EXTERN int
1702 lws_plat_check_connection_error(struct lws *wsi);
1703
1704 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1705 lws_header_table_attach(struct lws *wsi, int autoservice);
1706
1707 LWS_EXTERN int
1708 lws_header_table_detach(struct lws *wsi, int autoservice);
1709
1710 LWS_EXTERN void
1711 lws_header_table_reset(struct lws *wsi, int autoservice);
1712
1713 LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
1714 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1715
1716 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1717 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1718
1719 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1720 lws_ensure_user_space(struct lws *wsi);
1721
1722 LWS_EXTERN int
1723 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1724
1725 #ifndef LWS_NO_SERVER
1726 int lws_context_init_server(struct lws_context_creation_info *info,
1727                             struct lws_vhost *vhost);
1728 LWS_EXTERN struct lws_vhost *
1729 lws_select_vhost(struct lws_context *context, int port, const char *servername);
1730 LWS_EXTERN int
1731 handshake_0405(struct lws_context *context, struct lws *wsi);
1732 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1733 lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len);
1734 LWS_EXTERN void
1735 lws_server_get_canonical_hostname(struct lws_context *context,
1736                                   struct lws_context_creation_info *info);
1737 #else
1738 #define lws_context_init_server(_a, _b) (0)
1739 #define lws_interpret_incoming_packet(_a, _b, _c) (0)
1740 #define lws_server_get_canonical_hostname(_a, _b)
1741 #endif
1742
1743 #ifndef LWS_NO_DAEMONIZE
1744 LWS_EXTERN int get_daemonize_pid();
1745 #else
1746 #define get_daemonize_pid() (0)
1747 #endif
1748
1749 #if !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
1750 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1751 interface_to_sa(struct lws_vhost *vh, const char *ifname,
1752                 struct sockaddr_in *addr, size_t addrlen);
1753 #endif
1754 LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1755
1756 enum lws_ssl_capable_status {
1757         LWS_SSL_CAPABLE_ERROR = -1,
1758         LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1759 };
1760
1761 #ifndef LWS_OPENSSL_SUPPORT
1762 #define LWS_SSL_ENABLED(context) (0)
1763 #define lws_context_init_server_ssl(_a, _b) (0)
1764 #define lws_ssl_destroy(_a)
1765 #define lws_context_init_http2_ssl(_a)
1766 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1767 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
1768 #define lws_ssl_pending lws_ssl_pending_no_ssl
1769 #define lws_server_socket_service_ssl(_b, _c) (0)
1770 #define lws_ssl_close(_a) (0)
1771 #define lws_ssl_context_destroy(_a)
1772 #define lws_ssl_SSL_CTX_destroy(_a)
1773 #define lws_ssl_remove_wsi_from_buffered_list(_a)
1774 #define lws_context_init_ssl_library(_a)
1775 #else
1776 #define LWS_SSL_ENABLED(context) (context->use_ssl)
1777 LWS_EXTERN int openssl_websocket_private_data_index;
1778 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1779 lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
1780 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1781 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
1782 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1783 lws_ssl_pending(struct lws *wsi);
1784 LWS_EXTERN int
1785 lws_context_init_ssl_library(struct lws_context_creation_info *info);
1786 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1787 lws_server_socket_service_ssl(struct lws *new_wsi, lws_sockfd_type accept_fd);
1788 LWS_EXTERN int
1789 lws_ssl_close(struct lws *wsi);
1790 LWS_EXTERN void
1791 lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost);
1792 LWS_EXTERN void
1793 lws_ssl_context_destroy(struct lws_context *context);
1794 LWS_VISIBLE void
1795 lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
1796 LWS_EXTERN int
1797 lws_ssl_client_bio_create(struct lws *wsi);
1798 LWS_EXTERN int
1799 lws_ssl_client_connect1(struct lws *wsi);
1800 LWS_EXTERN int
1801 lws_ssl_client_connect2(struct lws *wsi);
1802 LWS_EXTERN void
1803 lws_ssl_elaborate_error(void);
1804 #ifndef LWS_NO_SERVER
1805 LWS_EXTERN int
1806 lws_context_init_server_ssl(struct lws_context_creation_info *info,
1807                             struct lws_vhost *vhost);
1808 #else
1809 #define lws_context_init_server_ssl(_a, _b) (0)
1810 #endif
1811 LWS_EXTERN void
1812 lws_ssl_destroy(struct lws_vhost *vhost);
1813
1814 /* HTTP2-related */
1815
1816 #ifdef LWS_USE_HTTP2
1817 LWS_EXTERN void
1818 lws_context_init_http2_ssl(struct lws_vhost *vhost);
1819 #else
1820 #define lws_context_init_http2_ssl(_a)
1821 #endif
1822 #endif
1823
1824 #if LWS_MAX_SMP > 1
1825 static LWS_INLINE void
1826 lws_pt_mutex_init(struct lws_context_per_thread *pt)
1827 {
1828         pthread_mutex_init(&pt->lock, NULL);
1829 }
1830
1831 static LWS_INLINE void
1832 lws_pt_mutex_destroy(struct lws_context_per_thread *pt)
1833 {
1834         pthread_mutex_destroy(&pt->lock);
1835 }
1836
1837 static LWS_INLINE void
1838 lws_pt_lock(struct lws_context_per_thread *pt)
1839 {
1840         if (!pt->lock_depth++)
1841                 pthread_mutex_lock(&pt->lock);
1842 }
1843
1844 static LWS_INLINE void
1845 lws_pt_unlock(struct lws_context_per_thread *pt)
1846 {
1847         if (!(--pt->lock_depth))
1848                 pthread_mutex_unlock(&pt->lock);
1849 }
1850 #else
1851 #define lws_pt_mutex_init(_a) (void)(_a)
1852 #define lws_pt_mutex_destroy(_a) (void)(_a)
1853 #define lws_pt_lock(_a) (void)(_a)
1854 #define lws_pt_unlock(_a) (void)(_a)
1855 #endif
1856
1857 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1858 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1859
1860 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1861 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1862
1863 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1864 lws_ssl_pending_no_ssl(struct lws *wsi);
1865
1866 #ifdef LWS_WITH_HTTP_PROXY
1867 struct lws_rewrite {
1868         hubbub_parser *parser;
1869         hubbub_parser_optparams params;
1870         const char *from, *to;
1871         int from_len, to_len;
1872         unsigned char *p, *end;
1873         struct lws *wsi;
1874 };
1875 static LWS_INLINE int hstrcmp(hubbub_string *s, const char *p, int len)
1876 {
1877         if (s->len != len)
1878                 return 1;
1879
1880         return strncmp((const char *)s->ptr, p, len);
1881 }
1882 typedef hubbub_error (*hubbub_callback_t)(const hubbub_token *token, void *pw);
1883 LWS_EXTERN struct lws_rewrite *
1884 lws_rewrite_create(struct lws *wsi, hubbub_callback_t cb, const char *from, const char *to);
1885 LWS_EXTERN void
1886 lws_rewrite_destroy(struct lws_rewrite *r);
1887 LWS_EXTERN int
1888 lws_rewrite_parse(struct lws_rewrite *r, const unsigned char *in, int in_len);
1889 #endif
1890
1891 #ifndef LWS_NO_CLIENT
1892 LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1893                                          struct lws *wsi,
1894                                          struct lws_pollfd *pollfd);
1895 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1896 lws_http_transaction_completed_client(struct lws *wsi);
1897 #ifdef LWS_OPENSSL_SUPPORT
1898 LWS_EXTERN int
1899 lws_context_init_client_ssl(struct lws_context_creation_info *info,
1900                             struct lws_vhost *vhost);
1901 #else
1902         #define lws_context_init_client_ssl(_a, _b) (0)
1903 #endif
1904 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1905 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1906 LWS_EXTERN void
1907 lws_decode_ssl_error(void);
1908 #else
1909 #define lws_context_init_client_ssl(_a, _b) (0)
1910 #define lws_handshake_client(_a, _b, _c) (0)
1911 #endif
1912
1913 LWS_EXTERN int
1914 _lws_rx_flow_control(struct lws *wsi);
1915
1916 LWS_EXTERN int
1917 _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
1918
1919 #ifndef LWS_NO_SERVER
1920 LWS_EXTERN int
1921 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1922                           struct lws_pollfd *pollfd);
1923 LWS_EXTERN int
1924 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1925 #else
1926 #define lws_server_socket_service(_a, _b, _c) (0)
1927 #define lws_handshake_server(_a, _b, _c) (0)
1928 #endif
1929
1930 #ifdef LWS_WITH_ACCESS_LOG
1931 LWS_EXTERN int
1932 lws_access_log(struct lws *wsi);
1933 #else
1934 #define lws_access_log(_a)
1935 #endif
1936
1937 LWS_EXTERN int
1938 lws_cgi_kill_terminated(struct lws_context_per_thread *pt);
1939
1940 int
1941 lws_protocol_init(struct lws_context *context);
1942
1943 int
1944 lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p);
1945
1946 const struct lws_http_mount *
1947 lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len);
1948
1949 /*
1950  * custom allocator
1951  */
1952 LWS_EXTERN void *
1953 lws_realloc(void *ptr, size_t size);
1954
1955 LWS_EXTERN void * LWS_WARN_UNUSED_RESULT
1956 lws_zalloc(size_t size);
1957
1958 #define lws_malloc(S)   lws_realloc(NULL, S)
1959 #define lws_free(P)     lws_realloc(P, 0)
1960 #define lws_free_set_NULL(P)    do { lws_realloc(P, 0); (P) = NULL; } while(0)
1961
1962 /* lws_plat_ */
1963 LWS_EXTERN void
1964 lws_plat_delete_socket_from_fds(struct lws_context *context,
1965                                 struct lws *wsi, int m);
1966 LWS_EXTERN void
1967 lws_plat_insert_socket_into_fds(struct lws_context *context,
1968                                 struct lws *wsi);
1969 LWS_EXTERN void
1970 lws_plat_service_periodic(struct lws_context *context);
1971
1972 LWS_EXTERN int
1973 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1974                        struct lws_pollfd *pfd);
1975 LWS_EXTERN int
1976 lws_plat_context_early_init(void);
1977 LWS_EXTERN void
1978 lws_plat_context_early_destroy(struct lws_context *context);
1979 LWS_EXTERN void
1980 lws_plat_context_late_destroy(struct lws_context *context);
1981 LWS_EXTERN int
1982 lws_poll_listen_fd(struct lws_pollfd *fd);
1983 LWS_EXTERN int
1984 lws_plat_service(struct lws_context *context, int timeout_ms);
1985 LWS_EXTERN LWS_VISIBLE int
1986 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
1987 LWS_EXTERN int
1988 lws_plat_init(struct lws_context *context,
1989               struct lws_context_creation_info *info);
1990 LWS_EXTERN void
1991 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1992 LWS_EXTERN unsigned long long
1993 time_in_microseconds(void);
1994 LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
1995 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
1996
1997 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1998 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);
1999
2000 #ifdef __cplusplus
2001 };
2002 #endif