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 #ifdef LWS_HAVE__VSNPRINTF
136 #define vsnprintf _vsnprintf
139 /* we don't have an implementation for this on windows... */
140 int kill(int pid, int sig);
146 #else /* not windows --> */
151 #include <sys/types.h>
152 #ifndef MBED_OPERATORS
158 #ifdef LWS_WITH_ESP8266
160 #define vsnprintf ets_vsnprintf
161 #define snprintf ets_snprintf
162 #define sprintf ets_sprintf
164 int kill(int pid, int sig);
167 #include <sys/socket.h>
169 #ifdef LWS_WITH_HTTP_PROXY
170 #include <hubbub/hubbub.h>
171 #include <hubbub/parser.h>
173 #if defined(LWS_BUILTIN_GETIFADDRS)
174 #include <getifaddrs.h>
176 #if !defined(LWS_WITH_ESP8266)
180 #if defined (__ANDROID__)
182 #include <sys/resource.h>
183 #elif defined (__sun)
186 #if !defined(LWS_WITH_ESP8266)
187 #include <sys/syslog.h>
191 #if !defined(LWS_WITH_ESP8266)
192 #include <sys/mman.h>
194 #include <netinet/in.h>
195 #include <netinet/tcp.h>
196 #include <arpa/inet.h>
209 #ifdef LWS_HAVE_SYS_PRCTL_H
210 #include <sys/prctl.h>
214 #include <sys/time.h>
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
224 #define lws_set_blocking_send(wsi)
226 #if defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
227 #define lws_socket_is_valid(x) ((x) != NULL)
228 #define LWS_SOCK_INVALID (NULL)
231 lws_plat_get_peer_simple(struct lws *wsi, char *name, int namelen);
233 #define lws_socket_is_valid(x) (x >= 0)
234 #define LWS_SOCK_INVALID (-1)
238 #ifndef LWS_HAVE_BZERO
240 #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
244 #ifndef LWS_HAVE_STRERROR
245 #define strerror(x) ""
248 #ifdef LWS_OPENSSL_SUPPORT
251 #ifdef USE_OLD_CYASSL
252 #include <cyassl/openssl/ssl.h>
253 #include <cyassl/error-ssl.h>
255 #include <wolfssl/openssl/ssl.h>
256 #include <wolfssl/error-ssl.h>
257 #endif /* not USE_OLD_CYASSL */
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
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>
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>
284 #include <openssl/x509v3.h>
285 #endif /* not USE_MBEDTLS */
286 #endif /* not USE_POLARSSL */
287 #endif /* not USE_WOLFSSL */
290 #include "libwebsockets.h"
291 #if defined(WIN32) || defined(_WIN32)
293 static inline int compatible_close(int fd) { return close(fd); }
296 #if defined(WIN32) || defined(_WIN32)
297 #include <gettimeofday.h>
300 #if defined(MBED_OPERATORS)
301 #undef compatible_close
302 #define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
304 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
306 #ifndef LITTLE_ENDIAN
307 #define LITTLE_ENDIAN 1234
310 #define BYTE_ORDER LITTLE_ENDIAN
314 #if defined(LWS_WITH_ESP8266)
315 #undef compatible_close
316 #define compatible_close(fd) { fd->state=ESPCONN_CLOSE; espconn_delete(fd); }
318 esp8266_create_tcp_stream_socket(void);
320 esp8266_tcp_stream_bind(lws_sockfd_type fd, int port, struct lws *wsi);
322 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
324 #ifndef LITTLE_ENDIAN
325 #define LITTLE_ENDIAN 1234
328 #define BYTE_ORDER LITTLE_ENDIAN
333 #if defined(WIN32) || defined(_WIN32)
336 #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
338 #ifndef LITTLE_ENDIAN
339 #define LITTLE_ENDIAN 1234
342 #define BYTE_ORDER LITTLE_ENDIAN
345 typedef unsigned __int64 u_int64_t;
351 #define __P(protos) protos
353 #define __P(protos) ()
359 #include <sys/stat.h>
360 #include <sys/time.h>
362 #if defined(__APPLE__)
363 #include <machine/endian.h>
364 #elif defined(__FreeBSD__)
365 #include <sys/endian.h>
366 #elif defined(__linux__)
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. */
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__
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__
394 #if !defined(BYTE_ORDER)
395 # define BYTE_ORDER __BYTE_ORDER
397 #if !defined(LITTLE_ENDIAN)
398 # define LITTLE_ENDIAN __LITTLE_ENDIAN
400 #if !defined(BIG_ENDIAN)
401 # define BIG_ENDIAN __BIG_ENDIAN
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.
411 #define MSG_NOSIGNAL SO_NOSIGPIPE
415 * Solaris 11.X only supports POSIX 2001, MSG_NOSIGNAL appears in
419 #define MSG_NOSIGNAL 0
423 #ifndef FD_HASHTABLE_MODULUS
424 #define FD_HASHTABLE_MODULUS 32
428 #ifndef LWS_DEF_HEADER_LEN
429 #define LWS_DEF_HEADER_LEN 4096
431 #ifndef LWS_DEF_HEADER_POOL
432 #define LWS_DEF_HEADER_POOL 4
434 #ifndef LWS_MAX_PROTOCOLS
435 #define LWS_MAX_PROTOCOLS 5
437 #ifndef LWS_MAX_EXTENSIONS_ACTIVE
438 #define LWS_MAX_EXTENSIONS_ACTIVE 2
440 #ifndef LWS_MAX_EXT_OFFERS
441 #define LWS_MAX_EXT_OFFERS 8
443 #ifndef SPEC_LATEST_SUPPORTED
444 #define SPEC_LATEST_SUPPORTED 13
446 #ifndef AWAITING_TIMEOUT
447 #define AWAITING_TIMEOUT 20
449 #ifndef CIPHERS_LIST_STRING
450 #define CIPHERS_LIST_STRING "DEFAULT"
452 #ifndef LWS_SOMAXCONN
453 #define LWS_SOMAXCONN SOMAXCONN
456 #define MAX_WEBSOCKET_04_KEY_LEN 128
458 #ifndef SYSTEM_RANDOM_FILEPATH
459 #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
462 enum lws_websocket_opcodes_07 {
463 LWSWSOPC_CONTINUATION = 0,
464 LWSWSOPC_TEXT_FRAME = 1,
465 LWSWSOPC_BINARY_FRAME = 2,
467 LWSWSOPC_NOSPEC__MUX = 7,
469 /* control extensions 8+ */
477 enum lws_connection_states {
479 LWSS_HTTP_ISSUING_FILE,
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,
491 LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
492 LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
493 LWSS_HTTP2_ESTABLISHED,
504 enum http_connection_type {
505 HTTP_CONNECTION_CLOSE,
506 HTTP_CONNECTION_KEEP_ALIVE
509 enum lws_pending_protocol_send {
511 LWS_PPS_HTTP2_MY_SETTINGS,
512 LWS_PPS_HTTP2_ACK_SETTINGS,
516 enum lws_rx_parse_state {
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,
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,
541 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
544 #define LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP 32
546 enum connection_mode {
548 LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
549 LWSCM_PRE_WS_SERVING_ACCEPT,
556 /* transient, ssl delay hiding */
557 LWSCM_SSL_ACK_PENDING,
560 /* special internal types */
561 LWSCM_SERVER_LISTENER,
562 LWSCM_CGI, /* stdin, stdout, stderr for another cgi master wsi */
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,
577 /****** add new things just above ---^ ******/
583 LWS_RXFLOW_ALLOW = (1 << 0),
584 LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
587 /* this is not usable directly by user code any more, lws_close_reason() */
588 #define LWS_WRITE_CLOSE 4
590 struct lws_protocols;
593 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
595 struct lws_io_watcher {
600 uv_poll_t uv_watcher;
602 struct lws_context *context;
605 struct lws_signal_watcher {
607 ev_signal ev_watcher;
610 uv_signal_t uv_watcher;
612 struct lws_context *context;
617 #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
618 struct lws_fd_hashtable {
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.
630 struct lws_fragments {
633 unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
637 * these are assigned from a pool held in the context.
638 * Both client and server mode uses them for http header analysis
641 struct allocated_headers {
642 struct lws *wsi; /* owner */
643 char *data; /* prepared by context init to point to dedicated storage */
645 * the randomly ordered fragments, indexed by frag_index and
646 * lws_fragments->nfrag for continuation.
648 struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
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[]
655 unsigned char frag_index[WSI_TOKEN_COUNT];
656 unsigned char rx[2048];
662 unsigned int http_response;
664 #ifndef LWS_NO_CLIENT
665 char initial_handshake_hash_base64[30];
668 unsigned char in_use;
673 * so we can have n connections being serviced simultaneously,
674 * these things need to be isolated per-thread.
677 struct lws_context_per_thread {
679 pthread_mutex_t lock;
681 struct lws_pollfd *fds;
682 #if defined(LWS_WITH_ESP8266)
683 struct lws **lws_vs_fds_index;
685 struct lws *rx_draining_ext_list;
686 struct lws *tx_draining_ext_list;
687 struct lws *timeout_list;
689 struct lws_context *context;
692 struct lws_cgi *cgi_list;
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 */
701 #if defined(LWS_USE_LIBEV)
702 struct ev_loop *io_loop_ev;
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;
710 #if defined(LWS_USE_LIBEV)
711 struct lws_io_watcher w_accept;
713 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
714 struct lws_signal_watcher w_sigint;
715 unsigned char ev_loop_foreign:1;
718 unsigned long count_conns;
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)
724 unsigned char *serv_buf;
728 lws_sockfd_type dummy_pipe_fds[2];
730 unsigned int fds_count;
732 short ah_count_in_use;
734 unsigned char lock_depth;
737 struct lws_conn_stats {
738 unsigned long long rx, tx;
739 unsigned long conn, trans, ws_upg, http2_upg, rejected;
743 lws_sum_stats(const struct lws_context *ctx, struct lws_conn_stats *cs);
746 * virtual host -related context information
747 * vhostwide SSL context
752 * context -> vhost -> wsi
754 * incoming connection non-SSL vhost binding:
756 * listen socket -> wsi -> select vhost after first headers
758 * incoming connection SSL vhost binding:
760 * SSL SNI -> wsi -> bind after SSL negotiation
764 #if !defined(LWS_WITH_ESP8266)
765 char http_proxy_address[128];
766 char proxy_basic_auth_token[128];
768 #if defined(LWS_WITH_ESP8266)
769 /* listen sockets need a place to hang their hat */
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;
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
786 SSL_CTX *ssl_client_ctx;
788 #ifndef LWS_NO_EXTENSIONS
789 const struct lws_extension *extensions;
793 unsigned int http_proxy_port;
794 unsigned int options;
799 int keepalive_timeout;
800 #ifdef LWS_WITH_ACCESS_LOG
804 #ifdef LWS_OPENSSL_SUPPORT
806 int allow_non_ssl_on_ssl_port;
807 unsigned int user_supplied_ssl_ctx:1;
810 unsigned int created_vhost_protocols:1;
812 unsigned char default_protocol_index;
816 * the rest is managed per-context, that includes
818 * - processwide single fd -> wsi lookup
819 * - contextwide headers pool
823 time_t last_timeout_check_s;
824 time_t last_ws_ping_pong_check_s;
826 struct lws_plat_file_ops fops;
827 struct lws_context_per_thread pt[LWS_MAX_SMP];
828 struct lws_conn_stats conn_stats;
830 /* different implementation between unix and windows */
831 struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
833 #if defined(LWS_WITH_ESP8266)
834 struct espconn **connpool; /* .reverse points to the wsi */
839 struct lws **lws_lookup; /* fd to wsi */
842 struct lws_vhost *vhost_list;
843 struct lws_plugin *plugin_list;
845 void *external_baggage_free_on_destroy;
846 const struct lws_token_limits *token_limits;
848 const char *server_string;
849 const struct lws_protocol_vhost_options *reject_service_keywords;
850 lws_reload_func deprecation_cb;
852 #if defined(LWS_USE_LIBEV)
853 lws_ev_signal_cb_t * lws_ev_sigint_cb;
855 #if defined(LWS_USE_LIBUV)
856 uv_signal_cb lws_uv_sigint_cb;
858 char canonical_hostname[128];
860 unsigned long worst_latency;
861 char worst_latency_info[256];
865 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
868 int started_with_parent;
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)
880 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
881 #define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
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;
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;
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.
903 volatile int service_tid;
904 int service_tid_detected;
906 short max_http_header_pool;
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;
915 #define lws_get_context_protocol(ctx, x) ctx->vhost_list->protocols[x]
916 #define lws_get_vh_protocol(vh, x) vh->protocols[x]
919 lws_close_free_wsi_final(struct lws *wsi);
921 lws_libuv_closehandle(struct lws *wsi);
923 LWS_VISIBLE LWS_EXTERN int
924 lws_plat_plugins_init(struct lws_context * context, const char * const *d);
926 LWS_VISIBLE LWS_EXTERN int
927 lws_plat_plugins_destroy(struct lws_context * context);
930 lws_restart_ws_ping_pong_timer(struct lws *wsi);
933 lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
937 LWS_EV_READ = (1 << 0),
938 LWS_EV_WRITE = (1 << 1),
939 LWS_EV_START = (1 << 2),
940 LWS_EV_STOP = (1 << 3),
942 LWS_EV_PREPARE_DELETION = (1 << 31),
945 #if defined(LWS_USE_LIBEV)
947 lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
949 lws_libev_io(struct lws *wsi, int flags);
951 lws_libev_init_fd_table(struct lws_context *context);
953 lws_libev_destroyloop(struct lws_context *context, int tsi);
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);
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)
966 #define lws_feature_status_libev(_a) \
967 lwsl_notice("libev support not compiled in\n")
969 #define lws_feature_status_libev(_a)
973 #if defined(LWS_USE_LIBUV)
975 lws_libuv_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
977 lws_libuv_io(struct lws *wsi, int flags);
979 lws_libuv_init_fd_table(struct lws_context *context);
981 lws_libuv_run(const struct lws_context *context, int tsi);
983 lws_libuv_destroyloop(struct lws_context *context, int tsi);
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);
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)
996 #define lws_feature_status_libuv(_a) \
997 lwsl_notice("libuv support not compiled in\n")
999 #define lws_feature_status_libuv(_a)
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))
1009 #define LWS_IPV6_ENABLED(context) (0)
1012 #ifdef LWS_USE_UNIX_SOCK
1013 #define LWS_UNIX_SOCK_ENABLED(vhost) \
1014 (vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
1016 #define LWS_UNIX_SOCK_ENABLED(vhost) (0)
1018 enum uri_path_states {
1021 URIPS_SEEN_SLASH_DOT,
1022 URIPS_SEEN_SLASH_DOT_DOT,
1025 enum uri_esc_states {
1028 URIES_SEEN_PERCENT_H1,
1031 /* notice that these union members:
1037 * all have a pointer to allocated_headers struct as their first member.
1039 * It means for allocated_headers access, the three union paths can all be
1040 * used interchangeably to access the same data
1044 #ifndef LWS_NO_CLIENT
1045 struct client_info_stash {
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;
1063 unsigned int preamble_rx_len;
1064 enum uri_path_states ups;
1065 enum uri_esc_states ues;
1067 unsigned int current_token_limit;
1068 #ifndef LWS_NO_CLIENT
1069 unsigned short c_port;
1072 char post_literal_equal;
1073 unsigned char parser_state; /* enum lws_token_indexes */
1077 #if defined(LWS_WITH_RANGES)
1079 LWSRS_NO_ACTIVE_RANGE,
1088 struct lws_range_parsing {
1089 unsigned long long start, end, extent, agg, budget;
1090 const char buf[128];
1092 enum range_states state;
1093 char start_valid, end_valid, ctr, count_ranges, did_try, inside, send_ctr;
1097 lws_ranges_init(struct lws *wsi, struct lws_range_parsing *rp, unsigned long long extent);
1099 lws_ranges_next(struct lws_range_parsing *rp);
1101 lws_ranges_reset(struct lws_range_parsing *rp);
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;
1112 unsigned int preamble_rx_len;
1113 struct lws *new_wsi_list;
1114 unsigned long filepos;
1115 unsigned long filelen;
1118 #if defined(LWS_WITH_RANGES)
1119 struct lws_range_parsing range;
1120 char multipart_content_type[64];
1123 enum http_version request_version;
1124 enum http_connection_type connection_type;
1125 unsigned int content_length;
1126 unsigned int content_remain;
1129 #ifdef LWS_USE_HTTP2
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,
1139 LWS_HTTP2_SETTINGS__COUNT /* always last */
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,
1154 LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
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,
1163 LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
1166 #define LWS_HTTP2_STREAM_ID_MASTER 0
1167 #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
1168 #define LWS_HTTP2_SETTINGS_LENGTH 6
1170 struct http2_settings {
1171 unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
1174 enum http2_hpack_state {
1176 /* optional before first header block */
1178 HKPS_OPT_E_DEPENDENCY,
1191 /* optional after last header block */
1192 HKPS_OPT_DISCARD_PADDING,
1195 enum http2_hpack_type {
1197 HPKT_INDEXED_HDR_6_VALUE_INCR,
1198 HPKT_LITERAL_HDR_VALUE_INCR,
1199 HPKT_INDEXED_HDR_4_VALUE,
1200 HPKT_LITERAL_HDR_VALUE,
1204 struct hpack_dt_entry {
1205 int token; /* additions that don't map to a token are ignored */
1210 struct hpack_dynamic_table {
1211 struct hpack_dt_entry *entries;
1219 struct _lws_http2_related {
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
1225 struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
1227 struct http2_settings my_settings;
1228 struct http2_settings peer_settings;
1230 struct lws *parent_wsi;
1231 struct lws *next_child_wsi;
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];
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;
1247 unsigned int my_stream_id;
1248 unsigned int child_count;
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;
1260 unsigned short round_robin_POLLOUT;
1261 unsigned short count_POLLOUT_children;
1262 unsigned short hpack_pos;
1265 unsigned char flags;
1266 unsigned char frame_state;
1267 unsigned char padding;
1268 unsigned char hpack_m;
1269 unsigned char initialized;
1272 #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
1276 struct _lws_websocket_related {
1277 /* cheapest way to deal with ah overlap with ws union transition */
1278 struct _lws_header_related hdr;
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];
1290 unsigned char ping_payload_len;
1291 unsigned char mask_idx;
1292 unsigned char opcode;
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;
1298 unsigned char stashed_write_type;
1299 unsigned char tx_draining_stashed_wp;
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;
1322 /* wsi who is master of the cgi points to an 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;
1333 unsigned int being_closed:1;
1335 unsigned char chunked_grace;
1339 signed char char_to_hex(const char c);
1341 #ifndef LWS_NO_CLIENT
1342 enum lws_chunk_parser {
1353 #ifdef LWS_WITH_ACCESS_LOG
1354 struct lws_access_log {
1365 /* members with mutually exclusive lifetimes are unionized */
1368 struct _lws_http_mode_related http;
1369 #ifdef LWS_USE_HTTP2
1370 struct _lws_http2_related http2;
1372 struct _lws_header_related hdr;
1373 struct _lws_websocket_related ws;
1376 /* lifetime members */
1378 #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
1379 struct lws_io_watcher w_read;
1381 #if defined(LWS_USE_LIBEV)
1382 struct lws_io_watcher w_write;
1384 time_t pending_timeout_limit;
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 */
1394 struct lws_cgi *cgi; /* wsi being cgi master have one of these */
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;
1404 /* rxflow handling */
1405 unsigned char *rxflow_buffer;
1406 /* truncated send handling */
1407 unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
1409 #if defined (LWS_WITH_ESP8266)
1411 unsigned short prem_rx_size, prem_rx_pos;
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];
1418 #ifdef LWS_OPENSSL_SUPPORT
1420 #if !defined(LWS_USE_POLARSSL) && !defined(LWS_USE_MBEDTLS)
1423 struct lws *pending_read_list_prev, *pending_read_list_next;
1425 #ifdef LWS_WITH_HTTP_PROXY
1426 struct lws_rewrite *rw;
1429 unsigned long action_start;
1430 unsigned long latency_start;
1433 lws_sockfd_type sock;
1436 int position_in_fds_table;
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;
1445 unsigned int cache_secs;
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;
1463 #if defined(LWS_WITH_ESP8266)
1464 unsigned int pending_send_completion:3;
1465 unsigned int close_is_pending_send_completion:1;
1467 #ifdef LWS_WITH_ACCESS_LOG
1468 unsigned int access_log_pending:1;
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;
1476 #ifdef LWS_WITH_HTTP_PROXY
1477 unsigned int perform_rewrite:1;
1479 #ifndef LWS_NO_EXTENSIONS
1480 unsigned int extension_data_pending:1;
1482 #ifdef LWS_OPENSSL_SUPPORT
1483 unsigned int use_ssl:3;
1484 unsigned int upgraded:1;
1487 unsigned int sock_send_blocking:1;
1489 #ifdef LWS_OPENSSL_SUPPORT
1490 unsigned int redirect_to_https:1;
1494 #ifndef LWS_NO_EXTENSIONS
1495 unsigned char count_act_ext;
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;
1508 char cgi_channel; /* which of stdin/out/err */
1511 #ifndef LWS_NO_CLIENT
1512 char chunk_parser; /* enum lws_chunk_parser */
1514 #if defined(LWS_WITH_CGI) || !defined(LWS_NO_CLIENT)
1515 char reason_bf; /* internal writeable callback reason bitfield */
1519 LWS_EXTERN int log_level;
1522 lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
1526 lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
1529 remove_wsi_socket_from_fds(struct lws *wsi);
1531 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
1535 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1536 int ret, int completion) {
1538 (void)context; (void)wsi; (void)action; (void)ret;
1543 lws_latency_pre(struct lws_context *context, struct lws *wsi) {
1544 do { (void)context; (void)wsi; } while (0);
1547 #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
1549 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1550 int ret, int completion);
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);
1559 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1560 lws_parse(struct lws *wsi, unsigned char c);
1562 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1563 lws_http_action(struct lws *wsi);
1566 lws_b64_selftest(void);
1569 lws_service_flag_pending(struct lws_context *context, int tsi);
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);
1576 insert_wsi(struct lws_context *context, struct lws *wsi);
1579 delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
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
1586 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1587 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
1589 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1590 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
1593 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1594 lws_service_timeout_check(struct lws *wsi, unsigned int sec);
1596 LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1597 lws_client_connect_2(struct lws *wsi);
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);
1603 LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
1604 lws_create_new_server_wsi(struct lws_vhost *vhost);
1606 LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
1607 lws_generate_client_handshake(struct lws *wsi, char *pkt);
1610 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
1612 LWS_EXTERN struct lws *
1613 lws_client_connect_via_info2(struct lws *wsi);
1619 #ifndef LWS_NO_EXTENSIONS
1621 lws_context_init_extensions(struct lws_context_creation_info *info,
1622 struct lws_context *context);
1624 lws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
1625 void *v, size_t len);
1628 lws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
1630 lws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
1631 void *arg, int len);
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)
1641 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1642 lws_client_interpret_server_handshake(struct lws *wsi);
1644 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1645 lws_rx_sm(struct lws *wsi, unsigned char c);
1648 lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf, size_t *len);
1650 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1651 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1654 lws_union_transition(struct lws *wsi, enum connection_mode mode);
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);
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);
1668 lws_http2_parser(struct lws *wsi, unsigned char c);
1669 LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
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,
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);
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);
1689 lws_add_http2_header_status(struct lws *wsi,
1690 unsigned int code, unsigned char **p,
1691 unsigned char *end);
1693 void lws_http2_configure_if_upgraded(struct lws *wsi);
1695 #define lws_http2_configure_if_upgraded(x)
1699 lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd);
1702 lws_plat_check_connection_error(struct lws *wsi);
1704 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1705 lws_header_table_attach(struct lws *wsi, int autoservice);
1708 lws_header_table_detach(struct lws *wsi, int autoservice);
1711 lws_header_table_reset(struct lws *wsi, int autoservice);
1713 LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
1714 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1716 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1717 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1719 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1720 lws_ensure_user_space(struct lws *wsi);
1723 lws_change_pollfd(struct lws *wsi, int _and, int _or);
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);
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);
1735 lws_server_get_canonical_hostname(struct lws_context *context,
1736 struct lws_context_creation_info *info);
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)
1743 #ifndef LWS_NO_DAEMONIZE
1744 LWS_EXTERN int get_daemonize_pid();
1746 #define get_daemonize_pid() (0)
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);
1754 LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
1756 enum lws_ssl_capable_status {
1757 LWS_SSL_CAPABLE_ERROR = -1,
1758 LWS_SSL_CAPABLE_MORE_SERVICE = -2,
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)
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);
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);
1789 lws_ssl_close(struct lws *wsi);
1791 lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost);
1793 lws_ssl_context_destroy(struct lws_context *context);
1795 lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
1797 lws_ssl_client_bio_create(struct lws *wsi);
1799 lws_ssl_client_connect1(struct lws *wsi);
1801 lws_ssl_client_connect2(struct lws *wsi);
1803 lws_ssl_elaborate_error(void);
1804 #ifndef LWS_NO_SERVER
1806 lws_context_init_server_ssl(struct lws_context_creation_info *info,
1807 struct lws_vhost *vhost);
1809 #define lws_context_init_server_ssl(_a, _b) (0)
1812 lws_ssl_destroy(struct lws_vhost *vhost);
1816 #ifdef LWS_USE_HTTP2
1818 lws_context_init_http2_ssl(struct lws_vhost *vhost);
1820 #define lws_context_init_http2_ssl(_a)
1825 static LWS_INLINE void
1826 lws_pt_mutex_init(struct lws_context_per_thread *pt)
1828 pthread_mutex_init(&pt->lock, NULL);
1831 static LWS_INLINE void
1832 lws_pt_mutex_destroy(struct lws_context_per_thread *pt)
1834 pthread_mutex_destroy(&pt->lock);
1837 static LWS_INLINE void
1838 lws_pt_lock(struct lws_context_per_thread *pt)
1840 if (!pt->lock_depth++)
1841 pthread_mutex_lock(&pt->lock);
1844 static LWS_INLINE void
1845 lws_pt_unlock(struct lws_context_per_thread *pt)
1847 if (!(--pt->lock_depth))
1848 pthread_mutex_unlock(&pt->lock);
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)
1857 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1858 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1860 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1861 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
1863 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1864 lws_ssl_pending_no_ssl(struct lws *wsi);
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;
1875 static LWS_INLINE int hstrcmp(hubbub_string *s, const char *p, int len)
1880 return strncmp((const char *)s->ptr, p, len);
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);
1886 lws_rewrite_destroy(struct lws_rewrite *r);
1888 lws_rewrite_parse(struct lws_rewrite *r, const unsigned char *in, int in_len);
1891 #ifndef LWS_NO_CLIENT
1892 LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
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
1899 lws_context_init_client_ssl(struct lws_context_creation_info *info,
1900 struct lws_vhost *vhost);
1902 #define lws_context_init_client_ssl(_a, _b) (0)
1904 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1905 lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
1907 lws_decode_ssl_error(void);
1909 #define lws_context_init_client_ssl(_a, _b) (0)
1910 #define lws_handshake_client(_a, _b, _c) (0)
1914 _lws_rx_flow_control(struct lws *wsi);
1917 _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
1919 #ifndef LWS_NO_SERVER
1921 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
1922 struct lws_pollfd *pollfd);
1924 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1926 #define lws_server_socket_service(_a, _b, _c) (0)
1927 #define lws_handshake_server(_a, _b, _c) (0)
1930 #ifdef LWS_WITH_ACCESS_LOG
1932 lws_access_log(struct lws *wsi);
1934 #define lws_access_log(_a)
1938 lws_cgi_kill_terminated(struct lws_context_per_thread *pt);
1941 lws_protocol_init(struct lws_context *context);
1944 lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p);
1946 const struct lws_http_mount *
1947 lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len);
1953 lws_realloc(void *ptr, size_t size);
1955 LWS_EXTERN void * LWS_WARN_UNUSED_RESULT
1956 lws_zalloc(size_t size);
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)
1964 lws_plat_delete_socket_from_fds(struct lws_context *context,
1965 struct lws *wsi, int m);
1967 lws_plat_insert_socket_into_fds(struct lws_context *context,
1970 lws_plat_service_periodic(struct lws_context *context);
1973 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1974 struct lws_pollfd *pfd);
1976 lws_plat_context_early_init(void);
1978 lws_plat_context_early_destroy(struct lws_context *context);
1980 lws_plat_context_late_destroy(struct lws_context *context);
1982 lws_poll_listen_fd(struct lws_pollfd *fd);
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);
1988 lws_plat_init(struct lws_context *context,
1989 struct lws_context_creation_info *info);
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);
1997 LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1998 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);