2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 - 2016 Andy Green <andy@warmcat.com>
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.
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.
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,
22 #include "lws_config.h"
23 #include "lws_config_private.h"
26 #if defined(LWS_WITH_CGI) && defined(LWS_HAVE_VFORK)
30 #ifdef LWS_HAVE_SYS_TYPES_H
31 #include <sys/types.h>
41 #if defined(LWS_WITH_ESP8266)
42 #include <user_interface.h>
45 /* rom-provided stdc functions for free, ensure use these instead of libc ones */
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, ...);
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)
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
93 #ifdef LWS_HAVE_SYS_STAT_H
97 #if defined(WIN32) || defined(_WIN32)
101 #define WINVER 0x0501
102 #define _WIN32_WINNT WINVER
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
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>
125 #ifdef LWS_HAVE_IN6ADDR_H
132 #define __func__ __FUNCTION__
135 #if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(_WIN32_WCE)
136 #define vsnprintf _vsnprintf
138 #ifdef LWS_HAVE__VSNPRINTF
139 #define vsnprintf _vsnprintf
143 #ifdef LWS_HAVE__SNPRINTF
144 #define snprintf _snprintf
147 #else /* not windows --> */
152 #include <sys/types.h>
153 #ifndef MBED_OPERATORS
159 #ifdef LWS_WITH_ESP8266
161 #define vsnprintf ets_vsnprintf
162 #define snprintf ets_snprintf
163 #define sprintf ets_sprintf
165 #include <sys/socket.h>
167 #ifdef LWS_WITH_HTTP_PROXY
168 #include <hubbub/hubbub.h>
169 #include <hubbub/parser.h>
171 #if defined(LWS_BUILTIN_GETIFADDRS)
172 #include <getifaddrs.h>
174 #if !defined(LWS_WITH_ESP8266)
178 #if defined (__ANDROID__)
180 #include <sys/resource.h>
182 #if !defined(LWS_WITH_ESP8266)
183 #include <sys/syslog.h>
187 #if !defined(LWS_WITH_ESP8266)
188 #include <sys/mman.h>
190 #include <netinet/in.h>
191 #include <netinet/tcp.h>
192 #include <arpa/inet.h>
205 #ifdef LWS_HAVE_SYS_PRCTL_H
206 #include <sys/prctl.h>
210 #include <sys/time.h>
212 #define LWS_ERRNO errno
213 #define LWS_EAGAIN EAGAIN
214 #define LWS_EALREADY EALREADY
215 #define LWS_EINPROGRESS EINPROGRESS
216 #define LWS_EINTR EINTR
217 #define LWS_EISCONN EISCONN
218 #define LWS_EWOULDBLOCK EWOULDBLOCK
220 #define lws_set_blocking_send(wsi)
222 #if defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
223 #define lws_socket_is_valid(x) ((x) != NULL)
224 #define LWS_SOCK_INVALID (NULL)
227 lws_plat_get_peer_simple(struct lws *wsi, char *name, int namelen);
229 #define lws_socket_is_valid(x) (x >= 0)
230 #define LWS_SOCK_INVALID (-1)
234 #ifndef LWS_HAVE_BZERO
236 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
240 #ifndef LWS_HAVE_STRERROR
241 #define strerror(x) ""
244 #ifdef LWS_OPENSSL_SUPPORT
247 #ifdef USE_OLD_CYASSL
248 #include <cyassl/openssl/ssl.h>
249 #include <cyassl/error-ssl.h>
251 #include <wolfssl/openssl/ssl.h>
252 #include <wolfssl/error-ssl.h>
253 #endif /* not USE_OLD_CYASSL */
255 #if defined(LWS_USE_POLARSSL)
256 #include <polarssl/ssl.h>
257 #include <polarssl/error.h>
258 #include <polarssl/md5.h>
259 #include <polarssl/sha1.h>
260 #include <polarssl/ecdh.h>
261 #define SSL_ERROR_WANT_READ POLARSSL_ERR_NET_WANT_READ
262 #define SSL_ERROR_WANT_WRITE POLARSSL_ERR_NET_WANT_WRITE
263 #define OPENSSL_VERSION_NUMBER 0x10002000L
265 #if defined(LWS_USE_MBEDTLS)
266 #include <mbedtls/ssl.h>
267 #include <mbedtls/error.h>
268 #include <mbedtls/md5.h>
269 #include <mbedtls/sha1.h>
270 #include <mbedtls/ecdh.h>
272 #include <openssl/ssl.h>
273 #include <openssl/evp.h>
274 #include <openssl/err.h>
275 #include <openssl/md5.h>
276 #include <openssl/sha.h>
277 #ifdef LWS_HAVE_OPENSSL_ECDH_H
278 #include <openssl/ecdh.h>
280 #include <openssl/x509v3.h>
281 #endif /* not USE_MBEDTLS */
282 #endif /* not USE_POLARSSL */
283 #endif /* not USE_WOLFSSL */
286 #include "libwebsockets.h"
287 #if defined(WIN32) || defined(_WIN32)
289 static inline int compatible_close(int fd) { return close(fd); }
292 #if defined(MBED_OPERATORS)
293 #undef compatible_close
294 #define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
296 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
298 #ifndef LITTLE_ENDIAN
299 #define LITTLE_ENDIAN 1234
302 #define BYTE_ORDER LITTLE_ENDIAN
306 #if defined(LWS_WITH_ESP8266)
307 #undef compatible_close
308 #define compatible_close(fd) { fd->state=ESPCONN_CLOSE; espconn_delete(fd); }
310 esp8266_create_tcp_stream_socket(void);
312 esp8266_tcp_stream_bind(lws_sockfd_type fd, int port, struct lws *wsi);
314 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
316 #ifndef LITTLE_ENDIAN
317 #define LITTLE_ENDIAN 1234
320 #define BYTE_ORDER LITTLE_ENDIAN
325 #if defined(WIN32) || defined(_WIN32)
328 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
330 #ifndef LITTLE_ENDIAN
331 #define LITTLE_ENDIAN 1234
334 #define BYTE_ORDER LITTLE_ENDIAN
337 typedef unsigned __int64 u_int64_t;
343 #define __P(protos) protos
345 #define __P(protos) ()
351 #include <sys/stat.h>
352 #include <sys/time.h>
354 #if defined(__APPLE__)
355 #include <machine/endian.h>
356 #elif defined(__FreeBSD__)
357 #include <sys/endian.h>
358 #elif defined(__linux__)
367 #include <gulliver.h>
368 #if defined(__LITTLEENDIAN__)
369 #define BYTE_ORDER __LITTLEENDIAN__
370 #define LITTLE_ENDIAN __LITTLEENDIAN__
371 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
373 #if defined(__BIGENDIAN__)
374 #define BYTE_ORDER __BIGENDIAN__
375 #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
376 #define BIG_ENDIAN __BIGENDIAN__
380 #if !defined(BYTE_ORDER)
381 # define BYTE_ORDER __BYTE_ORDER
383 #if !defined(LITTLE_ENDIAN)
384 # define LITTLE_ENDIAN __LITTLE_ENDIAN
386 #if !defined(BIG_ENDIAN)
387 # define BIG_ENDIAN __BIG_ENDIAN
393 * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
394 * but happily have something equivalent in the SO_NOSIGPIPE flag.
397 #define MSG_NOSIGNAL SO_NOSIGPIPE
401 #ifndef FD_HASHTABLE_MODULUS
402 #define FD_HASHTABLE_MODULUS 32
406 #ifndef LWS_DEF_HEADER_LEN
407 #define LWS_DEF_HEADER_LEN 4096
409 #ifndef LWS_DEF_HEADER_POOL
410 #define LWS_DEF_HEADER_POOL 4
412 #ifndef LWS_MAX_PROTOCOLS
413 #define LWS_MAX_PROTOCOLS 5
415 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
416 #define LWS_MAX_EXTENSIONS_ACTIVE 2
418 #ifndef LWS_MAX_EXT_OFFERS
419 #define LWS_MAX_EXT_OFFERS 8
421 #ifndef SPEC_LATEST_SUPPORTED
422 #define SPEC_LATEST_SUPPORTED 13
424 #ifndef AWAITING_TIMEOUT
425 #define AWAITING_TIMEOUT 20
427 #ifndef CIPHERS_LIST_STRING
428 #define CIPHERS_LIST_STRING "DEFAULT"
430 #ifndef LWS_SOMAXCONN
431 #define LWS_SOMAXCONN SOMAXCONN
434 #define MAX_WEBSOCKET_04_KEY_LEN 128
436 #ifndef SYSTEM_RANDOM_FILEPATH
437 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
440 enum lws_websocket_opcodes_07 {
441 LWSWSOPC_CONTINUATION = 0,
442 LWSWSOPC_TEXT_FRAME = 1,
443 LWSWSOPC_BINARY_FRAME = 2,
445 LWSWSOPC_NOSPEC__MUX = 7,
447 /* control extensions 8+ */
455 enum lws_connection_states {
457 LWSS_HTTP_ISSUING_FILE,
462 LWSS_CLIENT_HTTP_ESTABLISHED,
463 LWSS_CLIENT_UNCONNECTED,
464 LWSS_RETURNED_CLOSE_ALREADY,
465 LWSS_AWAITING_CLOSE_ACK,
466 LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
469 LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
470 LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
471 LWSS_HTTP2_ESTABLISHED,
482 enum http_connection_type {
483 HTTP_CONNECTION_CLOSE,
484 HTTP_CONNECTION_KEEP_ALIVE
487 enum lws_pending_protocol_send {
489 LWS_PPS_HTTP2_MY_SETTINGS,
490 LWS_PPS_HTTP2_ACK_SETTINGS,
494 enum lws_rx_parse_state {
501 LWS_RXPS_04_FRAME_HDR_1,
502 LWS_RXPS_04_FRAME_HDR_LEN,
503 LWS_RXPS_04_FRAME_HDR_LEN16_2,
504 LWS_RXPS_04_FRAME_HDR_LEN16_1,
505 LWS_RXPS_04_FRAME_HDR_LEN64_8,
506 LWS_RXPS_04_FRAME_HDR_LEN64_7,
507 LWS_RXPS_04_FRAME_HDR_LEN64_6,
508 LWS_RXPS_04_FRAME_HDR_LEN64_5,
509 LWS_RXPS_04_FRAME_HDR_LEN64_4,
510 LWS_RXPS_04_FRAME_HDR_LEN64_3,
511 LWS_RXPS_04_FRAME_HDR_LEN64_2,
512 LWS_RXPS_04_FRAME_HDR_LEN64_1,
514 LWS_RXPS_07_COLLECT_FRAME_KEY_1,
515 LWS_RXPS_07_COLLECT_FRAME_KEY_2,
516 LWS_RXPS_07_COLLECT_FRAME_KEY_3,
517 LWS_RXPS_07_COLLECT_FRAME_KEY_4,
519 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
523 enum connection_mode {
525 LWSCM_HTTP_CLIENT, /* we are client to someone else's server */
526 LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
527 LWSCM_HTTP_CLIENT_ACCEPTED, /* actual HTTP service going on */
528 LWSCM_PRE_WS_SERVING_ACCEPT,
535 /* transient, ssl delay hiding */
536 LWSCM_SSL_ACK_PENDING,
539 /* transient modes */
540 LWSCM_WSCL_WAITING_CONNECT,
541 LWSCM_WSCL_WAITING_PROXY_REPLY,
542 LWSCM_WSCL_ISSUE_HANDSHAKE,
543 LWSCM_WSCL_ISSUE_HANDSHAKE2,
544 LWSCM_WSCL_WAITING_SSL,
545 LWSCM_WSCL_WAITING_SERVER_REPLY,
546 LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
547 LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
549 /* special internal types */
550 LWSCM_SERVER_LISTENER,
551 LWSCM_CGI, /* stdin, stdout, stderr for another cgi master wsi */
555 LWS_RXFLOW_ALLOW = (1 << 0),
556 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
559 /* this is not usable directly by user code any more, lws_close_reason() */
560 #define LWS_WRITE_CLOSE 4
562 struct lws_protocols;
565 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
567 struct lws_io_watcher {
572 uv_poll_t uv_watcher;
574 struct lws_context *context;
577 struct lws_signal_watcher {
579 ev_signal ev_watcher;
582 uv_signal_t uv_watcher;
584 struct lws_context *context;
589 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
590 struct lws_fd_hashtable {
597 * This is totally opaque to code using the library. It's exported as a
598 * forward-reference pointer-only declaration; the user can use the pointer with
599 * other APIs to get information out of it.
602 struct lws_fragments {
605 unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
609 * these are assigned from a pool held in the context.
610 * Both client and server mode uses them for http header analysis
613 struct allocated_headers {
614 struct lws *wsi; /* owner */
615 char *data; /* prepared by context init to point to dedicated storage */
617 * the randomly ordered fragments, indexed by frag_index and
618 * lws_fragments->nfrag for continuation.
620 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
623 * for each recognized token, frag_index says which frag[] his data
624 * starts in (0 means the token did not appear)
625 * the actual header data gets dumped as it comes in, into data[]
627 unsigned char frag_index[WSI_TOKEN_COUNT];
628 unsigned char rx[2048];
633 #ifndef LWS_NO_CLIENT
634 char initial_handshake_hash_base64[30];
637 unsigned char in_use;
642 * so we can have n connections being serviced simultaneously,
643 * these things need to be isolated per-thread.
646 struct lws_context_per_thread {
648 pthread_mutex_t lock;
650 struct lws_pollfd *fds;
651 #if defined(LWS_WITH_ESP8266)
652 struct lws **lws_vs_fds_index;
654 struct lws *rx_draining_ext_list;
655 struct lws *tx_draining_ext_list;
656 struct lws *timeout_list;
657 struct lws_context *context;
659 struct lws_cgi *cgi_list;
661 void *http_header_data;
662 struct allocated_headers *ah_pool;
663 struct lws *ah_wait_list;
664 int ah_wait_list_length;
665 #ifdef LWS_OPENSSL_SUPPORT
666 struct lws *pending_read_list; /* linked list */
668 #ifndef LWS_NO_SERVER
669 struct lws *wsi_listening;
671 #if defined(LWS_USE_LIBEV)
672 struct ev_loop *io_loop_ev;
674 #if defined(LWS_USE_LIBUV)
675 uv_loop_t *io_loop_uv;
676 uv_signal_t signals[8];
677 uv_timer_t uv_timeout_watcher;
680 #if defined(LWS_USE_LIBEV)
681 struct lws_io_watcher w_accept;
683 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
684 struct lws_signal_watcher w_sigint;
685 unsigned char ev_loop_foreign:1;
688 unsigned long count_conns;
690 * usable by anything in the service code, but only if the scope
691 * does not last longer than the service action (since next service
692 * of any socket can likewise use it and overwrite)
694 unsigned char *serv_buf;
698 lws_sockfd_type dummy_pipe_fds[2];
700 unsigned int fds_count;
702 short ah_count_in_use;
704 unsigned char lock_depth;
708 * virtual host -related context information
709 * vhostwide SSL context
714 * context -> vhost -> wsi
716 * incoming connection non-SSL vhost binding:
718 * listen socket -> wsi -> select vhost after first headers
720 * incoming connection SSL vhost binding:
722 * SSL SNI -> wsi -> bind after SSL negotiation
726 #if !defined(LWS_WITH_ESP8266)
727 char http_proxy_address[128];
728 char proxy_basic_auth_token[128];
730 #if defined(LWS_WITH_ESP8266)
731 /* listen sockets need a place to hang their hat */
734 struct lws_context *context;
735 struct lws_vhost *vhost_next;
736 const struct lws_http_mount *mount_list;
737 struct lws *lserv_wsi;
740 const struct lws_protocols *protocols;
741 void **protocol_vh_privs;
742 const struct lws_protocol_vhost_options *pvo;
743 struct lws **same_vh_protocol_list;
744 #ifdef LWS_OPENSSL_SUPPORT
746 SSL_CTX *ssl_client_ctx;
748 #ifndef LWS_NO_EXTENSIONS
749 const struct lws_extension *extensions;
751 unsigned long long rx, tx;
752 unsigned long conn, trans, ws_upgrades, http2_upgrades;
755 unsigned int http_proxy_port;
756 unsigned int options;
761 int keepalive_timeout;
762 #ifdef LWS_WITH_ACCESS_LOG
766 #ifdef LWS_OPENSSL_SUPPORT
768 int allow_non_ssl_on_ssl_port;
769 unsigned int user_supplied_ssl_ctx:1;
772 unsigned char default_protocol_index;
776 * the rest is managed per-context, that includes
778 * - processwide single fd -> wsi lookup
779 * - contextwide headers pool
783 time_t last_timeout_check_s;
784 time_t last_ws_ping_pong_check_s;
786 struct lws_plat_file_ops fops;
787 struct lws_context_per_thread pt[LWS_MAX_SMP];
789 /* different implementation between unix and windows */
790 struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
792 #if defined(LWS_WITH_ESP8266)
793 struct espconn **connpool; /* .reverse points to the wsi */
798 struct lws **lws_lookup; /* fd to wsi */
801 struct lws_vhost *vhost_list;
802 struct lws_plugin *plugin_list;
803 const struct lws_token_limits *token_limits;
805 const char *server_string;
807 #if defined(LWS_USE_LIBEV)
808 lws_ev_signal_cb_t * lws_ev_sigint_cb;
810 #if defined(LWS_USE_LIBUV)
811 uv_signal_cb lws_uv_sigint_cb;
813 char canonical_hostname[128];
815 unsigned long worst_latency;
816 char worst_latency_info[256];
820 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
823 int started_with_parent;
827 #ifdef LWS_OPENSSL_SUPPORT
828 #define lws_ssl_anybody_has_buffered_read(w) \
829 (w->vhost->use_ssl && \
830 w->context->pt[(int)w->tsi].pending_read_list)
831 #define lws_ssl_anybody_has_buffered_read_tsi(c, t) \
832 (/*c->use_ssl && */ \
833 c->pt[(int)t].pending_read_list)
835 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
836 #define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
838 int count_wsi_allocated;
839 int count_cgi_spawned;
840 unsigned int options;
841 unsigned int fd_limit_per_thread;
842 unsigned int timeout_secs;
843 unsigned int pt_serv_buf_size;
844 int max_http_header_data;
847 * set to the Thread ID that's doing the service loop just before entry
848 * to poll indicates service thread likely idling in poll()
849 * volatile because other threads may check it as part of processing
850 * for pollfd event change.
852 volatile int service_tid;
853 int service_tid_detected;
855 short max_http_header_pool;
857 short plugin_protocol_count;
858 short plugin_extension_count;
859 short server_string_len;
860 unsigned short ws_ping_pong_interval;
862 unsigned int being_destroyed:1;
863 unsigned int requested_kill:1;
864 unsigned int protocol_init_done:1;
867 #define lws_get_context_protocol(ctx, x) ctx->vhost_list->protocols[x]
868 #define lws_get_vh_protocol(vh, x) vh->protocols[x]
871 lws_close_free_wsi_final(struct lws *wsi);
873 lws_libuv_closehandle(struct lws *wsi);
875 LWS_VISIBLE LWS_EXTERN int
876 lws_plat_plugins_init(struct lws_context * context, const char * const *d);
878 LWS_VISIBLE LWS_EXTERN int
879 lws_plat_plugins_destroy(struct lws_context * context);
882 lws_restart_ws_ping_pong_timer(struct lws *wsi);
885 lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
889 LWS_EV_READ = (1 << 0),
890 LWS_EV_WRITE = (1 << 1),
891 LWS_EV_START = (1 << 2),
892 LWS_EV_STOP = (1 << 3),
894 LWS_EV_PREPARE_DELETION = (1 << 31),
897 #if defined(LWS_USE_LIBEV)
899 lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
901 lws_libev_io(struct lws *wsi, int flags);
903 lws_libev_init_fd_table(struct lws_context *context);
905 lws_libev_destroyloop(struct lws_context *context, int tsi);
907 lws_libev_run(const struct lws_context *context, int tsi);
908 #define LWS_LIBEV_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEV)
909 LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
911 #define lws_libev_accept(_a, _b) ((void) 0)
912 #define lws_libev_io(_a, _b) ((void) 0)
913 #define lws_libev_init_fd_table(_a) (0)
914 #define lws_libev_run(_a, _b) ((void) 0)
915 #define lws_libev_destroyloop(_a, _b) ((void) 0)
916 #define LWS_LIBEV_ENABLED(context) (0)
918 #define lws_feature_status_libev(_a) \
919 lwsl_notice("libev support not compiled in\n")
921 #define lws_feature_status_libev(_a)
925 #if defined(LWS_USE_LIBUV)
927 lws_libuv_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
929 lws_libuv_io(struct lws *wsi, int flags);
931 lws_libuv_init_fd_table(struct lws_context *context);
933 lws_libuv_run(const struct lws_context *context, int tsi);
935 lws_libuv_destroyloop(struct lws_context *context, int tsi);
936 #define LWS_LIBUV_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV)
937 LWS_EXTERN void lws_feature_status_libuv(struct lws_context_creation_info *info);
939 #define lws_libuv_accept(_a, _b) ((void) 0)
940 #define lws_libuv_io(_a, _b) ((void) 0)
941 #define lws_libuv_init_fd_table(_a) (0)
942 #define lws_libuv_run(_a, _b) ((void) 0)
943 #define lws_libuv_destroyloop(_a, _b) ((void) 0)
944 #define LWS_LIBUV_ENABLED(context) (0)
946 #define lws_feature_status_libuv(_a) \
947 lwsl_notice("libuv support not compiled in\n")
949 #define lws_feature_status_libuv(_a)
955 #define LWS_IPV6_ENABLED(vh) \
956 (!lws_check_opt(vh->context->options, LWS_SERVER_OPTION_DISABLE_IPV6) && \
957 !lws_check_opt(vh->options, LWS_SERVER_OPTION_DISABLE_IPV6))
959 #define LWS_IPV6_ENABLED(context) (0)
962 #ifdef LWS_USE_UNIX_SOCK
963 #define LWS_UNIX_SOCK_ENABLED(vhost) \
964 (vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
966 #define LWS_UNIX_SOCK_ENABLED(vhost) (0)
968 enum uri_path_states {
971 URIPS_SEEN_SLASH_DOT,
972 URIPS_SEEN_SLASH_DOT_DOT,
975 enum uri_esc_states {
978 URIES_SEEN_PERCENT_H1,
981 /* notice that these union members:
987 * all have a pointer to allocated_headers struct as their first member.
989 * It means for allocated_headers access, the three union paths can all be
990 * used interchangeably to access the same data
994 #ifndef LWS_NO_CLIENT
995 struct client_info_stash {
1005 struct _lws_header_related {
1006 /* MUST be first in struct */
1007 struct allocated_headers *ah;
1008 struct lws *ah_wait_list;
1009 unsigned char *preamble_rx;
1010 #ifndef LWS_NO_CLIENT
1011 struct client_info_stash *stash;
1013 unsigned int preamble_rx_len;
1014 enum uri_path_states ups;
1015 enum uri_esc_states ues;
1017 unsigned int current_token_limit;
1018 #ifndef LWS_NO_CLIENT
1019 unsigned short c_port;
1022 char post_literal_equal;
1023 unsigned char parser_state; /* enum lws_token_indexes */
1027 struct _lws_http_mode_related {
1028 /* MUST be first in struct */
1029 struct allocated_headers *ah; /* mirroring _lws_header_related */
1030 struct lws *ah_wait_list;
1031 unsigned char *preamble_rx;
1032 #ifndef LWS_NO_CLIENT
1033 struct client_info_stash *stash;
1035 unsigned int preamble_rx_len;
1036 struct lws *new_wsi_list;
1037 unsigned long filepos;
1038 unsigned long filelen;
1041 enum http_version request_version;
1042 enum http_connection_type connection_type;
1043 unsigned int content_length;
1044 unsigned int content_remain;
1047 #ifdef LWS_USE_HTTP2
1049 enum lws_http2_settings {
1050 LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
1051 LWS_HTTP2_SETTINGS__ENABLE_PUSH,
1052 LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
1053 LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
1054 LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
1055 LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
1057 LWS_HTTP2_SETTINGS__COUNT /* always last */
1060 enum lws_http2_wellknown_frame_types {
1061 LWS_HTTP2_FRAME_TYPE_DATA,
1062 LWS_HTTP2_FRAME_TYPE_HEADERS,
1063 LWS_HTTP2_FRAME_TYPE_PRIORITY,
1064 LWS_HTTP2_FRAME_TYPE_RST_STREAM,
1065 LWS_HTTP2_FRAME_TYPE_SETTINGS,
1066 LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
1067 LWS_HTTP2_FRAME_TYPE_PING,
1068 LWS_HTTP2_FRAME_TYPE_GOAWAY,
1069 LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
1070 LWS_HTTP2_FRAME_TYPE_CONTINUATION,
1072 LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
1075 enum lws_http2_flags {
1076 LWS_HTTP2_FLAG_END_STREAM = 1,
1077 LWS_HTTP2_FLAG_END_HEADERS = 4,
1078 LWS_HTTP2_FLAG_PADDED = 8,
1079 LWS_HTTP2_FLAG_PRIORITY = 0x20,
1081 LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
1084 #define LWS_HTTP2_STREAM_ID_MASTER 0
1085 #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
1086 #define LWS_HTTP2_SETTINGS_LENGTH 6
1088 struct http2_settings {
1089 unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
1092 enum http2_hpack_state {
1094 /* optional before first header block */
1096 HKPS_OPT_E_DEPENDENCY,
1109 /* optional after last header block */
1110 HKPS_OPT_DISCARD_PADDING,
1113 enum http2_hpack_type {
1115 HPKT_INDEXED_HDR_6_VALUE_INCR,
1116 HPKT_LITERAL_HDR_VALUE_INCR,
1117 HPKT_INDEXED_HDR_4_VALUE,
1118 HPKT_LITERAL_HDR_VALUE,
1122 struct hpack_dt_entry {
1123 int token; /* additions that don't map to a token are ignored */
1128 struct hpack_dynamic_table {
1129 struct hpack_dt_entry *entries;
1137 struct _lws_http2_related {
1139 * having this first lets us also re-use all HTTP union code
1140 * and in turn, http_mode_related has allocated headers in right
1141 * place so we can use the header apis on the wsi directly still
1143 struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
1145 struct http2_settings my_settings;
1146 struct http2_settings peer_settings;
1148 struct lws *parent_wsi;
1149 struct lws *next_child_wsi;
1151 struct hpack_dynamic_table *hpack_dyn_table;
1152 struct lws *stream_wsi;
1153 unsigned char ping_payload[8];
1154 unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
1157 unsigned int length;
1158 unsigned int stream_id;
1159 enum http2_hpack_state hpack;
1160 enum http2_hpack_type hpack_type;
1161 unsigned int header_index;
1162 unsigned int hpack_len;
1163 unsigned int hpack_e_dep;
1165 unsigned int my_stream_id;
1166 unsigned int child_count;
1169 unsigned int END_STREAM:1;
1170 unsigned int END_HEADERS:1;
1171 unsigned int send_END_STREAM:1;
1172 unsigned int GOING_AWAY;
1173 unsigned int requested_POLLOUT:1;
1174 unsigned int waiting_tx_credit:1;
1175 unsigned int huff:1;
1176 unsigned int value:1;
1178 unsigned short round_robin_POLLOUT;
1179 unsigned short count_POLLOUT_children;
1180 unsigned short hpack_pos;
1183 unsigned char flags;
1184 unsigned char frame_state;
1185 unsigned char padding;
1186 unsigned char hpack_m;
1187 unsigned char initialized;
1190 #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
1194 struct _lws_websocket_related {
1195 /* cheapest way to deal with ah overlap with ws union transition */
1196 struct _lws_header_related hdr;
1198 unsigned int rx_ubuf_alloc;
1199 struct lws *rx_draining_ext_list;
1200 struct lws *tx_draining_ext_list;
1201 time_t time_next_ping_check;
1202 size_t rx_packet_length;
1203 unsigned int rx_ubuf_head;
1204 unsigned char mask[4];
1205 /* Also used for close content... control opcode == < 128 */
1206 unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
1208 unsigned char ping_payload_len;
1209 unsigned char mask_idx;
1210 unsigned char opcode;
1212 unsigned char rsv_first_msg;
1213 /* zero if no info, or length including 2-byte close code */
1214 unsigned char close_in_ping_buffer_len;
1216 unsigned char stashed_write_type;
1217 unsigned char tx_draining_stashed_wp;
1219 unsigned int final:1;
1220 unsigned int frame_is_binary:1;
1221 unsigned int all_zero_nonce:1;
1222 unsigned int this_frame_masked:1;
1223 unsigned int inside_frame:1; /* next write will be more of frame */
1224 unsigned int clean_buffer:1; /* buffer not rewritten by extension */
1225 unsigned int payload_is_close:1; /* process as PONG, but it is close */
1226 unsigned int ping_pending_flag:1;
1227 unsigned int continuation_possible:1;
1228 unsigned int owed_a_fin:1;
1229 unsigned int check_utf8:1;
1230 unsigned int defeat_check_utf8:1;
1231 unsigned int pmce_compressed_message:1;
1232 unsigned int stashed_write_pending:1;
1233 unsigned int rx_draining_ext:1;
1234 unsigned int tx_draining_ext:1;
1235 unsigned int send_check_ping:1;
1240 /* wsi who is master of the cgi points to an lws_cgi */
1243 struct lws_cgi *cgi_list;
1244 struct lws *stdwsi[3]; /* points to the associated stdin/out/err wsis */
1245 struct lws *wsi; /* owner */
1246 unsigned long content_length;
1247 unsigned long content_length_seen;
1251 unsigned int being_closed:1;
1255 signed char char_to_hex(const char c);
1257 #ifndef LWS_NO_CLIENT
1258 enum lws_chunk_parser {
1269 #ifdef LWS_WITH_ACCESS_LOG
1270 struct lws_access_log {
1281 /* members with mutually exclusive lifetimes are unionized */
1284 struct _lws_http_mode_related http;
1285 #ifdef LWS_USE_HTTP2
1286 struct _lws_http2_related http2;
1288 struct _lws_header_related hdr;
1289 struct _lws_websocket_related ws;
1292 /* lifetime members */
1294 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
1295 struct lws_io_watcher w_read;
1297 #if defined(LWS_USE_LIBEV)
1298 struct lws_io_watcher w_write;
1300 time_t pending_timeout_limit;
1304 struct lws_context *context;
1305 struct lws_vhost *vhost;
1306 struct lws *parent; /* points to parent, if any */
1307 struct lws *child_list; /* points to first child */
1308 struct lws *sibling_list; /* subsequent children at same level */
1310 struct lws_cgi *cgi; /* wsi being cgi master have one of these */
1312 const struct lws_protocols *protocol;
1313 struct lws **same_vh_protocol_prev, *same_vh_protocol_next;
1314 struct lws *timeout_list;
1315 struct lws **timeout_list_prev;
1316 #ifdef LWS_WITH_ACCESS_LOG
1317 struct lws_access_log access_log;
1320 /* rxflow handling */
1321 unsigned char *rxflow_buffer;
1322 /* truncated send handling */
1323 unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
1325 #if defined (LWS_WITH_ESP8266)
1327 unsigned short prem_rx_size, prem_rx_pos;
1330 #ifndef LWS_NO_EXTENSIONS
1331 const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
1332 void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
1334 #ifdef LWS_OPENSSL_SUPPORT
1336 #if !defined(LWS_USE_POLARSSL) && !defined(LWS_USE_MBEDTLS)
1339 struct lws *pending_read_list_prev, *pending_read_list_next;
1341 #ifdef LWS_WITH_HTTP_PROXY
1342 struct lws_rewrite *rw;
1345 unsigned long action_start;
1346 unsigned long latency_start;
1349 lws_sockfd_type sock;
1352 int position_in_fds_table;
1355 unsigned int trunc_alloc_len; /* size of malloc */
1356 unsigned int trunc_offset; /* where we are in terms of spilling */
1357 unsigned int trunc_len; /* how much is buffered */
1358 #ifndef LWS_NO_CLIENT
1359 int chunk_remaining;
1361 unsigned int cache_secs;
1363 unsigned int hdr_parsing_completed:1;
1364 unsigned int http2_substream:1;
1365 unsigned int listener:1;
1366 unsigned int user_space_externally_allocated:1;
1367 unsigned int socket_is_permanently_unusable:1;
1368 unsigned int rxflow_change_to:2;
1369 unsigned int more_rx_waiting:1; /* has to live here since ah may stick to end */
1370 unsigned int conn_stat_done:1;
1371 unsigned int cache_reuse:1;
1372 unsigned int cache_revalidate:1;
1373 unsigned int cache_intermediaries:1;
1374 unsigned int favoured_pollin:1;
1375 unsigned int sending_chunked:1;
1376 unsigned int already_did_cce:1;
1377 #if defined(LWS_WITH_ESP8266)
1378 unsigned int pending_send_completion:3;
1379 unsigned int close_is_pending_send_completion:1;
1381 #ifdef LWS_WITH_ACCESS_LOG
1382 unsigned int access_log_pending:1;
1384 #ifndef LWS_NO_CLIENT
1385 unsigned int do_ws:1; /* whether we are doing http or ws flow */
1386 unsigned int chunked:1; /* if the clientside connection is chunked */
1387 unsigned int client_rx_avail:1;
1389 #ifdef LWS_WITH_HTTP_PROXY
1390 unsigned int perform_rewrite:1;
1392 #ifndef LWS_NO_EXTENSIONS
1393 unsigned int extension_data_pending:1;
1395 #ifdef LWS_OPENSSL_SUPPORT
1396 unsigned int use_ssl:3;
1397 unsigned int upgraded:1;
1400 unsigned int sock_send_blocking:1;
1402 #ifdef LWS_OPENSSL_SUPPORT
1403 unsigned int redirect_to_https:1;
1407 #ifndef LWS_NO_EXTENSIONS
1408 unsigned char count_act_ext;
1410 unsigned char ietf_spec_revision;
1411 char mode; /* enum connection_mode */
1412 char state; /* enum lws_connection_states */
1413 char state_pre_close;
1414 char lws_rx_parse_state; /* enum lws_rx_parse_state */
1415 char rx_frame_type; /* enum lws_write_protocol */
1416 char pending_timeout; /* enum pending_timeout */
1417 char pps; /* enum lws_pending_protocol_send */
1418 char tsi; /* thread service index we belong to */
1419 char protocol_interpret_idx;
1421 char cgi_channel; /* which of stdin/out/err */
1424 #ifndef LWS_NO_CLIENT
1425 char chunk_parser; /* enum lws_chunk_parser */
1427 #if defined(LWS_WITH_CGI) || !defined(LWS_NO_CLIENT)
1428 char reason_bf; /* internal writeable callback reason bitfield */
1432 LWS_EXTERN int log_level;
1435 lws_socket_bind(struct lws_vhost *vhost, int sockfd, int port,
1439 lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
1442 remove_wsi_socket_from_fds(struct lws *wsi);
1444 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
1448 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1449 int ret, int completion) {
1451 (void)context; (void)wsi; (void)action; (void)ret;
1456 lws_latency_pre(struct lws_context *context, struct lws *wsi) {
1457 do { (void)context; (void)wsi; } while (0);
1460 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
1462 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1463 int ret, int completion);
1467 lws_set_protocol_write_pending(struct lws *wsi,
1468 enum lws_pending_protocol_send pend);
1469 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1470 lws_client_rx_sm(struct lws *wsi, unsigned char c);
1472 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1473 lws_parse(struct lws *wsi, unsigned char c);
1475 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1476 lws_http_action(struct lws *wsi);
1479 lws_b64_selftest(void);
1482 lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi);
1485 lws_service_flag_pending(struct lws_context *context, int tsi);
1487 #if defined(_WIN32) || defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
1488 LWS_EXTERN struct lws *
1489 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
1492 insert_wsi(struct lws_context *context, struct lws *wsi);
1495 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
1497 #define wsi_from_fd(A,B) A->lws_lookup[B]
1498 #define insert_wsi(A,B) assert(A->lws_lookup[B->sock] == 0); A->lws_lookup[B->sock]=B
1499 #define delete_from_fd(A,B) A->lws_lookup[B]=0
1502 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1503 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
1505 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1506 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
1509 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1510 lws_service_timeout_check(struct lws *wsi, unsigned int sec);
1512 LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1513 lws_client_connect_2(struct lws *wsi);
1515 LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
1516 lws_client_reset(struct lws *wsi, int ssl, const char *address, int port,
1517 const char *path, const char *host);
1519 LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1520 lws_create_new_server_wsi(struct lws_vhost *vhost);
1522 LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
1523 lws_generate_client_handshake(struct lws *wsi, char *pkt);
1526 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
1528 LWS_EXTERN struct lws *
1529 lws_client_connect_via_info2(struct lws *wsi);
1535 #ifndef LWS_NO_EXTENSIONS
1537 lws_context_init_extensions(struct lws_context_creation_info *info,
1538 struct lws_context *context);
1540 lws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
1541 void *v, size_t len);
1544 lws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
1546 lws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
1547 void *arg, int len);
1550 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
1551 #define lws_ext_cb_active(_a, _b, _c, _d) (0)
1552 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
1553 #define lws_issue_raw_ext_access lws_issue_raw
1554 #define lws_context_init_extensions(_a, _b)
1557 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1558 lws_client_interpret_server_handshake(struct lws *wsi);
1560 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1561 lws_rx_sm(struct lws *wsi, unsigned char c);
1564 lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf, size_t *len);
1566 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1567 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1570 lws_union_transition(struct lws *wsi, enum connection_mode mode);
1572 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1573 user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
1574 enum lws_callback_reasons reason, void *user,
1575 void *in, size_t len);
1576 #ifdef LWS_USE_HTTP2
1577 LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
1578 struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
1580 lws_http2_interpret_settings_payload(struct http2_settings *settings,
1581 unsigned char *buf, int len);
1582 LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
1584 lws_http2_parser(struct lws *wsi, unsigned char c);
1585 LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
1587 LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
1588 unsigned int sid, unsigned int len,
1589 unsigned char *buf);
1590 LWS_EXTERN struct lws *
1591 lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
1592 LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
1595 lws_add_http2_header_by_name(struct lws *wsi,
1596 const unsigned char *name,
1597 const unsigned char *value, int length,
1598 unsigned char **p, unsigned char *end);
1600 lws_add_http2_header_by_token(struct lws *wsi,
1601 enum lws_token_indexes token,
1602 const unsigned char *value, int length,
1603 unsigned char **p, unsigned char *end);
1605 lws_add_http2_header_status(struct lws *wsi,
1606 unsigned int code, unsigned char **p,
1607 unsigned char *end);
1609 void lws_http2_configure_if_upgraded(struct lws *wsi);
1611 #define lws_http2_configure_if_upgraded(x)
1615 lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd);
1618 lws_plat_check_connection_error(struct lws *wsi);
1620 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1621 lws_header_table_attach(struct lws *wsi, int autoservice);
1624 lws_header_table_detach(struct lws *wsi, int autoservice);
1627 lws_header_table_reset(struct lws *wsi, int autoservice);
1629 LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
1630 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1632 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1633 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1635 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1636 lws_ensure_user_space(struct lws *wsi);
1639 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1641 #ifndef LWS_NO_SERVER
1642 int lws_context_init_server(struct lws_context_creation_info *info,
1643 struct lws_vhost *vhost);
1644 LWS_EXTERN struct lws_vhost *
1645 lws_select_vhost(struct lws_context *context, int port, const char *servername);
1647 handshake_0405(struct lws_context *context, struct lws *wsi);
1648 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1649 lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len);
1651 lws_server_get_canonical_hostname(struct lws_context *context,
1652 struct lws_context_creation_info *info);
1654 #define lws_context_init_server(_a, _b) (0)
1655 #define lws_interpret_incoming_packet(_a, _b, _c) (0)
1656 #define lws_server_get_canonical_hostname(_a, _b)
1659 #ifndef LWS_NO_DAEMONIZE
1660 LWS_EXTERN int get_daemonize_pid();
1662 #define get_daemonize_pid() (0)
1665 #if !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
1666 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1667 interface_to_sa(struct lws_vhost *vh, const char *ifname,
1668 struct sockaddr_in *addr, size_t addrlen);
1670 LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1672 enum lws_ssl_capable_status {
1673 LWS_SSL_CAPABLE_ERROR = -1,
1674 LWS_SSL_CAPABLE_MORE_SERVICE = -2,
1677 #ifndef LWS_OPENSSL_SUPPORT
1678 #define LWS_SSL_ENABLED(context) (0)
1679 #define lws_context_init_server_ssl(_a, _b) (0)
1680 #define lws_ssl_destroy(_a)
1681 #define lws_context_init_http2_ssl(_a)
1682 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
1683 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
1684 #define lws_ssl_pending lws_ssl_pending_no_ssl
1685 #define lws_server_socket_service_ssl(_b, _c) (0)
1686 #define lws_ssl_close(_a) (0)
1687 #define lws_ssl_context_destroy(_a)
1688 #define lws_ssl_SSL_CTX_destroy(_a)
1689 #define lws_ssl_remove_wsi_from_buffered_list(_a)
1690 #define lws_context_init_ssl_library(_a)
1692 #define LWS_SSL_ENABLED(context) (context->use_ssl)
1693 LWS_EXTERN int openssl_websocket_private_data_index;
1694 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1695 lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
1696 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1697 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
1698 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1699 lws_ssl_pending(struct lws *wsi);
1701 lws_context_init_ssl_library(struct lws_context_creation_info *info);
1702 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1703 lws_server_socket_service_ssl(struct lws *new_wsi, lws_sockfd_type accept_fd);
1705 lws_ssl_close(struct lws *wsi);
1707 lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost);
1709 lws_ssl_context_destroy(struct lws_context *context);
1711 lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
1713 lws_ssl_client_bio_create(struct lws *wsi);
1715 lws_ssl_client_connect1(struct lws *wsi);
1717 lws_ssl_client_connect2(struct lws *wsi);
1719 lws_ssl_elaborate_error(void);
1720 #ifndef LWS_NO_SERVER
1722 lws_context_init_server_ssl(struct lws_context_creation_info *info,
1723 struct lws_vhost *vhost);
1725 #define lws_context_init_server_ssl(_a, _b) (0)
1728 lws_ssl_destroy(struct lws_vhost *vhost);
1732 #ifdef LWS_USE_HTTP2
1734 lws_context_init_http2_ssl(struct lws_vhost *vhost);
1736 #define lws_context_init_http2_ssl(_a)
1741 static LWS_INLINE void
1742 lws_pt_mutex_init(struct lws_context_per_thread *pt)
1744 pthread_mutex_init(&pt->lock, NULL);
1747 static LWS_INLINE void
1748 lws_pt_mutex_destroy(struct lws_context_per_thread *pt)
1750 pthread_mutex_destroy(&pt->lock);
1753 static LWS_INLINE void
1754 lws_pt_lock(struct lws_context_per_thread *pt)
1756 if (!pt->lock_depth++)
1757 pthread_mutex_lock(&pt->lock);
1760 static LWS_INLINE void
1761 lws_pt_unlock(struct lws_context_per_thread *pt)
1763 if (!(--pt->lock_depth))
1764 pthread_mutex_unlock(&pt->lock);
1767 #define lws_pt_mutex_init(_a) (void)(_a)
1768 #define lws_pt_mutex_destroy(_a) (void)(_a)
1769 #define lws_pt_lock(_a) (void)(_a)
1770 #define lws_pt_unlock(_a) (void)(_a)
1773 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1774 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1776 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1777 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1779 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1780 lws_ssl_pending_no_ssl(struct lws *wsi);
1782 #ifdef LWS_WITH_HTTP_PROXY
1783 struct lws_rewrite {
1784 hubbub_parser *parser;
1785 hubbub_parser_optparams params;
1786 const char *from, *to;
1787 int from_len, to_len;
1788 unsigned char *p, *end;
1791 static LWS_INLINE int hstrcmp(hubbub_string *s, const char *p, int len)
1796 return strncmp((const char *)s->ptr, p, len);
1798 typedef hubbub_error (*hubbub_callback_t)(const hubbub_token *token, void *pw);
1799 LWS_EXTERN struct lws_rewrite *
1800 lws_rewrite_create(struct lws *wsi, hubbub_callback_t cb, const char *from, const char *to);
1802 lws_rewrite_destroy(struct lws_rewrite *r);
1804 lws_rewrite_parse(struct lws_rewrite *r, const unsigned char *in, int in_len);
1807 #ifndef LWS_NO_CLIENT
1808 LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
1810 struct lws_pollfd *pollfd);
1811 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1812 lws_http_transaction_completed_client(struct lws *wsi);
1813 #ifdef LWS_OPENSSL_SUPPORT
1815 lws_context_init_client_ssl(struct lws_context_creation_info *info,
1816 struct lws_vhost *vhost);
1818 #define lws_context_init_client_ssl(_a, _b) (0)
1820 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1821 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1823 lws_decode_ssl_error(void);
1825 #define lws_context_init_client_ssl(_a, _b) (0)
1826 #define lws_handshake_client(_a, _b, _c) (0)
1830 _lws_rx_flow_control(struct lws *wsi);
1833 _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
1835 #ifndef LWS_NO_SERVER
1837 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1838 struct lws_pollfd *pollfd);
1840 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1842 _lws_server_listen_accept_flow_control(struct lws *twsi, int on);
1844 #define lws_server_socket_service(_a, _b, _c) (0)
1845 #define lws_handshake_server(_a, _b, _c) (0)
1846 #define _lws_server_listen_accept_flow_control(a, b) (0)
1849 #ifdef LWS_WITH_ACCESS_LOG
1851 lws_access_log(struct lws *wsi);
1853 #define lws_access_log(_a)
1857 lws_cgi_kill_terminated(struct lws_context_per_thread *pt);
1860 lws_protocol_init(struct lws_context *context);
1863 lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p);
1865 const struct lws_http_mount *
1866 lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len);
1872 lws_realloc(void *ptr, size_t size);
1874 LWS_EXTERN void * LWS_WARN_UNUSED_RESULT
1875 lws_zalloc(size_t size);
1877 #define lws_malloc(S) lws_realloc(NULL, S)
1878 #define lws_free(P) lws_realloc(P, 0)
1879 #define lws_free_set_NULL(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
1883 lws_plat_delete_socket_from_fds(struct lws_context *context,
1884 struct lws *wsi, int m);
1886 lws_plat_insert_socket_into_fds(struct lws_context *context,
1889 lws_plat_service_periodic(struct lws_context *context);
1892 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1893 struct lws_pollfd *pfd);
1895 lws_plat_context_early_init(void);
1897 lws_plat_context_early_destroy(struct lws_context *context);
1899 lws_plat_context_late_destroy(struct lws_context *context);
1901 lws_poll_listen_fd(struct lws_pollfd *fd);
1903 lws_plat_service(struct lws_context *context, int timeout_ms);
1904 LWS_EXTERN LWS_VISIBLE int
1905 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
1907 lws_plat_init(struct lws_context *context,
1908 struct lws_context_creation_info *info);
1910 lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
1911 LWS_EXTERN unsigned long long
1912 time_in_microseconds(void);
1913 LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
1914 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
1916 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1917 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);