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 "private-libwebsockets.h"
24 #ifdef LWS_HAVE_SYS_TYPES_H
25 #include <sys/types.h>
28 #if defined(WIN32) || defined(_WIN32)
34 #if defined(WIN32) || defined(_WIN32)
41 int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
42 static void (*lwsl_emit)(int level, const char *line)
43 #ifndef LWS_PLAT_OPTEE
47 #ifndef LWS_PLAT_OPTEE
48 static const char * const log_level_names[] = {
66 lws_free_wsi(struct lws *wsi)
71 /* Protocol user data may be allocated either internally by lws
72 * or by specified the user.
73 * We should only free what we allocated. */
74 if (wsi->protocol && wsi->protocol->per_session_data_size &&
75 wsi->user_space && !wsi->user_space_externally_allocated)
76 lws_free(wsi->user_space);
78 lws_free_set_NULL(wsi->rxflow_buffer);
79 lws_free_set_NULL(wsi->trunc_alloc);
81 /* we may not have an ah, but may be on the waiting list... */
82 lwsl_info("ah det due to close\n");
83 lws_header_table_detach(wsi, 0);
85 wsi->context->count_wsi_allocated--;
86 lwsl_debug("%s: %p, remaining wsi %d\n", __func__, wsi,
87 wsi->context->count_wsi_allocated);
93 lws_remove_from_timeout_list(struct lws *wsi)
95 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
97 if (!wsi->timeout_list_prev) /* ie, not part of the list */
101 /* if we have a next guy, set his prev to our prev */
102 if (wsi->timeout_list)
103 wsi->timeout_list->timeout_list_prev = wsi->timeout_list_prev;
104 /* set our prev guy to our next guy instead of us */
105 *wsi->timeout_list_prev = wsi->timeout_list;
107 /* we're out of the list, we should not point anywhere any more */
108 wsi->timeout_list_prev = NULL;
109 wsi->timeout_list = NULL;
114 lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs)
116 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
123 if (reason && !wsi->timeout_list_prev) {
124 /* our next guy is current first guy */
125 wsi->timeout_list = pt->timeout_list;
126 /* if there is a next guy, set his prev ptr to our next ptr */
127 if (wsi->timeout_list)
128 wsi->timeout_list->timeout_list_prev = &wsi->timeout_list;
129 /* our prev ptr is first ptr */
130 wsi->timeout_list_prev = &pt->timeout_list;
131 /* set the first guy to be us */
132 *wsi->timeout_list_prev = wsi;
135 lwsl_debug("%s: %p: %d secs\n", __func__, wsi, secs);
136 wsi->pending_timeout_limit = now + secs;
137 wsi->pending_timeout = reason;
142 lws_remove_from_timeout_list(wsi);
146 lws_remove_child_from_any_parent(struct lws *wsi)
154 /* detach ourselves from parent's child list */
155 pwsi = &wsi->parent->child_list;
158 lwsl_info("%s: detach %p from parent %p\n",
159 __func__, wsi, wsi->parent);
160 *pwsi = wsi->sibling_list;
164 pwsi = &(*pwsi)->sibling_list;
167 lwsl_err("%s: failed to detach from parent\n", __func__);
173 lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p)
175 // if (wsi->protocol == p)
177 const struct lws_protocols *vp = wsi->vhost->protocols, *vpo;
180 wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_DROP_PROTOCOL,
181 wsi->user_space, NULL, 0);
182 if (!wsi->user_space_externally_allocated)
183 lws_free_set_NULL(wsi->user_space);
185 lws_same_vh_protocol_remove(wsi);
191 if (lws_ensure_user_space(wsi))
194 if (p > vp && p < &vp[wsi->vhost->count_protocols])
195 lws_same_vh_protocol_insert(wsi, p - vp);
197 int n = wsi->vhost->count_protocols;
203 if (p->name && vp->name && !strcmp(p->name, vp->name)) {
205 lws_same_vh_protocol_insert(wsi, vp - vpo);
211 lwsl_err("%s: protocol %p is not in vhost %s protocols list\n",
212 __func__, p, wsi->vhost->name);
215 if (wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_BIND_PROTOCOL,
216 wsi->user_space, NULL, 0))
223 lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason)
225 struct lws_context_per_thread *pt;
226 struct lws *wsi1, *wsi2;
227 struct lws_context *context;
228 struct lws_tokens eff_buf;
235 #if defined(LWS_WITH_ESP8266)
236 if (wsi->premature_rx)
237 lws_free(wsi->premature_rx);
239 if (wsi->pending_send_completion && !wsi->close_is_pending_send_completion) {
240 lwsl_notice("delaying close\n");
241 wsi->close_is_pending_send_completion = 1;
246 /* we're closing, losing some rx is OK */
247 lws_header_table_force_to_detachable_state(wsi);
249 context = wsi->context;
250 pt = &context->pt[(int)wsi->tsi];
251 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_API_CLOSE, 1);
253 /* if we have children, close them first */
254 if (wsi->child_list) {
255 wsi2 = wsi->child_list;
257 //lwsl_notice("%s: closing %p: close child %p\n",
258 // __func__, wsi, wsi2);
259 wsi1 = wsi2->sibling_list;
260 //lwsl_notice("%s: closing %p: next sibling %p\n",
261 // __func__, wsi2, wsi1);
263 /* stop it doing shutdown processing */
264 wsi2->socket_is_permanently_unusable = 1;
265 lws_close_free_wsi(wsi2, reason);
268 wsi->child_list = NULL;
271 if (wsi->mode == LWSCM_RAW_FILEDESC) {
272 lws_remove_child_from_any_parent(wsi);
273 remove_wsi_socket_from_fds(wsi);
274 wsi->protocol->callback(wsi,
275 LWS_CALLBACK_RAW_CLOSE_FILE,
276 wsi->user_space, NULL, 0);
281 if (wsi->mode == LWSCM_CGI) {
282 /* we are not a network connection, but a handler for CGI io */
283 if (wsi->parent && wsi->parent->cgi)
284 /* end the binding between us and master */
285 wsi->parent->cgi->stdwsi[(int)wsi->cgi_channel] = NULL;
286 wsi->socket_is_permanently_unusable = 1;
288 lwsl_debug("------ %s: detected cgi fdhandler wsi %p\n", __func__, wsi);
289 goto just_kill_connection;
293 struct lws_cgi **pcgi = &pt->cgi_list;
294 /* remove us from the cgi list */
295 lwsl_debug("%s: remove cgi %p from list\n", __func__, wsi->cgi);
297 if (*pcgi == wsi->cgi) {
298 /* drop us from the pt cgi list */
299 *pcgi = (*pcgi)->cgi_list;
302 pcgi = &(*pcgi)->cgi_list;
304 if (wsi->cgi->headers_buf) {
305 lwsl_debug("close: freed cgi headers\n");
306 lws_free_set_NULL(wsi->cgi->headers_buf);
308 /* we have a cgi going, we must kill it */
309 wsi->cgi->being_closed = 1;
314 if (wsi->mode == LWSCM_RAW) {
315 wsi->protocol->callback(wsi,
316 LWS_CALLBACK_RAW_CLOSE, wsi->user_space, NULL, 0);
317 wsi->socket_is_permanently_unusable = 1;
318 goto just_kill_connection;
321 if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED &&
322 wsi->u.http.fop_fd != NULL) {
323 lws_vfs_file_close(&wsi->u.http.fop_fd);
324 wsi->vhost->protocols->callback(wsi,
325 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
326 wsi->told_user_closed = 1;
328 if (wsi->socket_is_permanently_unusable ||
329 reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY ||
330 wsi->state == LWSS_SHUTDOWN)
331 goto just_kill_connection;
333 wsi->state_pre_close = wsi->state;
335 switch (wsi->state_pre_close) {
336 case LWSS_DEAD_SOCKET:
339 /* we tried the polite way... */
340 case LWSS_WAITING_TO_SEND_CLOSE_NOTIFICATION:
341 case LWSS_AWAITING_CLOSE_ACK:
342 goto just_kill_connection;
344 case LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE:
345 if (wsi->trunc_len) {
346 lws_callback_on_writable(wsi);
349 lwsl_info("wsi %p completed LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
350 goto just_kill_connection;
352 if (wsi->trunc_len) {
353 lwsl_info("wsi %p entering LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
354 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
355 lws_set_timeout(wsi, PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5);
361 if (wsi->mode == LWSCM_WSCL_WAITING_CONNECT ||
362 wsi->mode == LWSCM_WSCL_ISSUE_HANDSHAKE)
363 goto just_kill_connection;
365 if (wsi->mode == LWSCM_HTTP_SERVING) {
367 wsi->vhost->protocols->callback(wsi,
368 LWS_CALLBACK_HTTP_DROP_PROTOCOL,
369 wsi->user_space, NULL, 0);
370 wsi->vhost->protocols->callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
371 wsi->user_space, NULL, 0);
372 wsi->told_user_closed = 1;
374 if (wsi->mode & LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP) {
375 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_CLIENT_HTTP,
376 wsi->user_space, NULL, 0);
377 wsi->told_user_closed = 1;
381 * are his extensions okay with him closing? Eg he might be a mux
382 * parent and just his ch1 aspect is closing?
385 if (lws_ext_cb_active(wsi,
386 LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE, NULL, 0) > 0) {
387 lwsl_ext("extension vetoed close\n");
392 * flush any tx pending from extensions, since we may send close packet
393 * if there are problems with send, just nuke the connection
398 eff_buf.token = NULL;
399 eff_buf.token_len = 0;
401 /* show every extension the new incoming data */
403 m = lws_ext_cb_active(wsi,
404 LWS_EXT_CB_FLUSH_PENDING_TX, &eff_buf, 0);
406 lwsl_ext("Extension reports fatal error\n");
407 goto just_kill_connection;
411 * at least one extension told us he has more
412 * to spill, so we will go around again after
416 /* assuming they left us something to send, send it */
418 if (eff_buf.token_len)
419 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
420 eff_buf.token_len) !=
422 lwsl_debug("close: ext spill failed\n");
423 goto just_kill_connection;
428 * signal we are closing, lws_write will
429 * add any necessary version-specific stuff. If the write fails,
430 * no worries we are closing anyway. If we didn't initiate this
431 * close, then our state has been changed to
432 * LWSS_RETURNED_CLOSE_ALREADY and we will skip this.
434 * Likewise if it's a second call to close this connection after we
435 * sent the close indication to the peer already, we are in state
436 * LWSS_AWAITING_CLOSE_ACK and will skip doing this a second time.
439 if (wsi->state_pre_close == LWSS_ESTABLISHED &&
440 (wsi->u.ws.close_in_ping_buffer_len || /* already a reason */
441 (reason != LWS_CLOSE_STATUS_NOSTATUS &&
442 (reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)))) {
443 lwsl_debug("sending close indication...\n");
445 /* if no prepared close reason, use 1000 and no aux data */
446 if (!wsi->u.ws.close_in_ping_buffer_len) {
447 wsi->u.ws.close_in_ping_buffer_len = 2;
448 wsi->u.ws.ping_payload_buf[LWS_PRE] =
449 (reason >> 8) & 0xff;
450 wsi->u.ws.ping_payload_buf[LWS_PRE + 1] =
454 #if defined (LWS_WITH_ESP8266)
455 wsi->close_is_pending_send_completion = 1;
458 lwsl_debug("waiting for chance to send close\n");
459 wsi->waiting_to_send_close_frame = 1;
460 wsi->state = LWSS_WAITING_TO_SEND_CLOSE_NOTIFICATION;
461 lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_SEND, 2);
462 lws_callback_on_writable(wsi);
467 just_kill_connection:
469 lws_remove_child_from_any_parent(wsi);
472 /* manage the vhost same protocol list entry */
474 if (wsi->same_vh_protocol_prev) { // we are on the vh list
476 // make guy who pointed to us, point to what our next was pointing to
477 *wsi->same_vh_protocol_prev = wsi->same_vh_protocol_next;
479 // if we had a next guy...
480 if (wsi->same_vh_protocol_next)
481 // have him point back to our prev
482 wsi->same_vh_protocol_next->same_vh_protocol_prev =
483 wsi->same_vh_protocol_prev;
489 * Testing with ab shows that we have to stage the socket close when
490 * the system is under stress... shutdown any further TX, change the
491 * state to one that won't emit anything more, and wait with a timeout
492 * for the POLLIN to show a zero-size rx before coming back and doing
495 if (wsi->mode != LWSCM_RAW &&
496 !(wsi->mode & LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP) &&
497 wsi->state != LWSS_SHUTDOWN &&
498 wsi->state != LWSS_CLIENT_UNCONNECTED &&
499 reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY &&
500 !wsi->socket_is_permanently_unusable) {
501 #ifdef LWS_OPENSSL_SUPPORT
502 if (lws_is_ssl(wsi) && wsi->ssl)
504 lwsl_info("%s: shutting down SSL connection: %p (ssl %p, sock %d, state %d)\n", __func__, wsi, wsi->ssl, (int)(long)wsi->desc.sockfd, wsi->state);
505 n = SSL_shutdown(wsi->ssl);
506 if (n == 1) /* If finished the SSL shutdown, then do socket shutdown, else need to retry SSL shutdown */
507 n = shutdown(wsi->desc.sockfd, SHUT_WR);
509 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
512 int shutdown_error = SSL_get_error(wsi->ssl, n);
513 lwsl_debug("SSL_shutdown returned %d, SSL_get_error: %d\n", n, shutdown_error);
514 if (shutdown_error == SSL_ERROR_WANT_READ) {
515 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
517 } else if (shutdown_error == SSL_ERROR_WANT_WRITE) {
518 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLOUT);
520 } else { // actual error occurred, just close the connection
521 n = shutdown(wsi->desc.sockfd, SHUT_WR);
528 lwsl_info("%s: shutting down connection: %p (sock %d, state %d)\n", __func__, wsi, (int)(long)wsi->desc.sockfd, wsi->state);
529 n = shutdown(wsi->desc.sockfd, SHUT_WR);
532 lwsl_debug("closing: shutdown (state %d) ret %d\n", wsi->state, LWS_ERRNO);
534 // This causes problems with disconnection when the events are half closing connection
535 // FD_READ | FD_CLOSE (33)
536 #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP32)
537 /* libuv: no event available to guarantee completion */
538 if (!LWS_LIBUV_ENABLED(context)) {
540 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
541 wsi->state = LWSS_SHUTDOWN;
542 lws_set_timeout(wsi, PENDING_TIMEOUT_SHUTDOWN_FLUSH,
543 context->timeout_secs);
551 lwsl_info("%s: real just_kill_connection: %p (sockfd %d)\n", __func__,
552 wsi, wsi->desc.sockfd);
554 #ifdef LWS_WITH_HTTP_PROXY
556 lws_rewrite_destroy(wsi->rw);
561 * we won't be servicing or receiving anything further from this guy
562 * delete socket from the internal poll list if still present
564 lws_ssl_remove_wsi_from_buffered_list(wsi);
566 lws_remove_from_timeout_list(wsi);
568 /* checking return redundant since we anyway close */
569 if (wsi->desc.sockfd != LWS_SOCK_INVALID)
570 remove_wsi_socket_from_fds(wsi);
572 #if defined(LWS_WITH_ESP8266)
573 espconn_disconnect(wsi->desc.sockfd);
576 wsi->state = LWSS_DEAD_SOCKET;
578 lws_free_set_NULL(wsi->rxflow_buffer);
579 if (wsi->state_pre_close == LWSS_ESTABLISHED ||
580 wsi->mode == LWSCM_WS_SERVING ||
581 wsi->mode == LWSCM_WS_CLIENT) {
583 if (wsi->u.ws.rx_draining_ext) {
584 struct lws **w = &pt->rx_draining_ext_list;
586 wsi->u.ws.rx_draining_ext = 0;
587 /* remove us from context draining ext list */
590 *w = wsi->u.ws.rx_draining_ext_list;
593 w = &((*w)->u.ws.rx_draining_ext_list);
595 wsi->u.ws.rx_draining_ext_list = NULL;
598 if (wsi->u.ws.tx_draining_ext) {
599 struct lws **w = &pt->tx_draining_ext_list;
601 wsi->u.ws.tx_draining_ext = 0;
602 /* remove us from context draining ext list */
605 *w = wsi->u.ws.tx_draining_ext_list;
608 w = &((*w)->u.ws.tx_draining_ext_list);
610 wsi->u.ws.tx_draining_ext_list = NULL;
612 lws_free_set_NULL(wsi->u.ws.rx_ubuf);
614 if (wsi->trunc_alloc)
615 /* not going to be completed... nuke it */
616 lws_free_set_NULL(wsi->trunc_alloc);
618 wsi->u.ws.ping_payload_len = 0;
619 wsi->u.ws.ping_pending_flag = 0;
622 /* tell the user it's all over for this guy */
624 if (wsi->mode != LWSCM_RAW && wsi->protocol && wsi->protocol->callback &&
625 ((wsi->state_pre_close == LWSS_ESTABLISHED) ||
626 (wsi->state_pre_close == LWSS_RETURNED_CLOSE_ALREADY) ||
627 (wsi->state_pre_close == LWSS_AWAITING_CLOSE_ACK) ||
628 (wsi->state_pre_close == LWSS_WAITING_TO_SEND_CLOSE_NOTIFICATION) ||
629 (wsi->state_pre_close == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) ||
630 (wsi->mode == LWSCM_WS_CLIENT && wsi->state_pre_close == LWSS_HTTP) ||
631 (wsi->mode == LWSCM_WS_SERVING && wsi->state_pre_close == LWSS_HTTP))) {
633 if (wsi->user_space) {
634 lwsl_debug("%s: doing LWS_CALLBACK_HTTP_DROP_PROTOCOL for %p prot %s\n", __func__, wsi, wsi->protocol->name);
635 wsi->protocol->callback(wsi,
636 LWS_CALLBACK_HTTP_DROP_PROTOCOL,
637 wsi->user_space, NULL, 0);
639 lwsl_debug("calling back CLOSED\n");
640 wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
641 wsi->user_space, NULL, 0);
642 } else if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED) {
643 lwsl_debug("calling back CLOSED_HTTP\n");
644 wsi->vhost->protocols->callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
645 wsi->user_space, NULL, 0 );
646 } else if ((wsi->mode == LWSCM_WSCL_WAITING_SERVER_REPLY ||
647 wsi->mode == LWSCM_WSCL_WAITING_CONNECT) &&
648 !wsi->already_did_cce) {
649 wsi->vhost->protocols[0].callback(wsi,
650 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
651 wsi->user_space, NULL, 0);
653 lwsl_debug("not calling back closed mode=%d state=%d\n",
654 wsi->mode, wsi->state_pre_close);
656 /* deallocate any active extension contexts */
658 if (lws_ext_cb_active(wsi, LWS_EXT_CB_DESTROY, NULL, 0) < 0)
659 lwsl_warn("extension destruction failed\n");
661 * inform all extensions in case they tracked this guy out of band
662 * even though not active on him specifically
664 if (lws_ext_cb_all_exts(context, wsi,
665 LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING, NULL, 0) < 0)
666 lwsl_warn("ext destroy wsi failed\n");
670 wsi->socket_is_permanently_unusable = 1;
673 if (LWS_LIBUV_ENABLED(context)) {
675 lwsl_debug("%s: stopping listner libuv poll\n", __func__);
676 uv_poll_stop(&wsi->w_read.uv_watcher);
678 lwsl_debug("%s: lws_libuv_closehandle: wsi %p\n", __func__, wsi);
679 /* libuv has to do his own close handle processing asynchronously */
680 lws_libuv_closehandle(wsi);
686 lws_close_free_wsi_final(wsi);
690 lws_close_free_wsi_final(struct lws *wsi)
694 if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->desc.sockfd)) {
696 //lwsl_err("*** closing sockfd %d\n", wsi->desc.sockfd);
697 n = compatible_close(wsi->desc.sockfd);
699 lwsl_debug("closing: close ret %d\n", LWS_ERRNO);
702 compatible_close(wsi->desc.sockfd);
705 wsi->desc.sockfd = LWS_SOCK_INVALID;
708 /* outermost destroy notification for wsi (user_space still intact) */
709 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
710 wsi->user_space, NULL, 0);
714 for (n = 0; n < 6; n++) {
715 if (wsi->cgi->pipe_fds[n / 2][n & 1] == 0)
716 lwsl_err("ZERO FD IN CGI CLOSE");
718 if (wsi->cgi->pipe_fds[n / 2][n & 1] >= 0)
719 close(wsi->cgi->pipe_fds[n / 2][n & 1]);
729 LWS_VISIBLE LWS_EXTERN const char *
730 lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len)
732 int n = 0, sl = strlen(name);
734 while (lws_hdr_copy_fragment(wsi, buf, len,
735 WSI_TOKEN_HTTP_URI_ARGS, n) >= 0) {
737 if (!strncmp(buf, name, sl))
746 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
748 interface_to_sa(struct lws_vhost *vh, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
752 ipv6 = LWS_IPV6_ENABLED(vh);
756 return lws_interface_to_sa(ipv6, ifname, addr, addrlen);
760 #ifndef LWS_PLAT_OPTEE
763 lws_get_addresses(struct lws_vhost *vh, void *ads, char *name,
764 int name_len, char *rip, int rip_len)
767 struct addrinfo ai, *res;
768 struct sockaddr_in addr4;
773 addr4.sin_family = AF_UNSPEC;
776 if (LWS_IPV6_ENABLED(vh)) {
777 if (!lws_plat_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ads)->sin6_addr, rip, rip_len)) {
778 lwsl_err("inet_ntop: %s", strerror(LWS_ERRNO));
782 // Strip off the IPv4 to IPv6 header if one exists
783 if (strncmp(rip, "::ffff:", 7) == 0)
784 memmove(rip, rip + 7, strlen(rip) - 6);
786 getnameinfo((struct sockaddr *)ads,
787 sizeof(struct sockaddr_in6), name,
788 name_len, NULL, 0, 0);
794 struct addrinfo *result;
796 memset(&ai, 0, sizeof ai);
797 ai.ai_family = PF_UNSPEC;
798 ai.ai_socktype = SOCK_STREAM;
799 ai.ai_flags = AI_CANONNAME;
800 #if !defined(LWS_WITH_ESP32)
801 if (getnameinfo((struct sockaddr *)ads,
802 sizeof(struct sockaddr_in),
803 name, name_len, NULL, 0, 0))
809 if (getaddrinfo(name, NULL, &ai, &result))
813 while (addr4.sin_family == AF_UNSPEC && res) {
814 switch (res->ai_family) {
816 addr4.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
817 addr4.sin_family = AF_INET;
823 freeaddrinfo(result);
826 if (addr4.sin_family == AF_UNSPEC)
829 if (lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len) == NULL)
847 LWS_VISIBLE const char *
848 lws_get_peer_simple(struct lws *wsi, char *name, int namelen)
853 struct sockaddr_in6 sin6;
855 struct sockaddr_in sin4;
860 if (LWS_IPV6_ENABLED(wsi->vhost)) {
874 if (getpeername(wsi->desc.sockfd, p, &len) < 0 || len > olen) {
875 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
879 return lws_plat_inet_ntop(af, q, name, namelen);
881 #if defined(LWS_WITH_ESP8266)
882 return lws_plat_get_peer_simple(wsi, name, namelen);
891 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
892 int name_len, char *rip, int rip_len)
894 #ifndef LWS_PLAT_OPTEE
898 struct sockaddr_in6 sin6;
900 struct sockaddr_in sin4;
901 struct lws_context *context = wsi->context;
908 lws_latency_pre(context, wsi);
911 if (LWS_IPV6_ENABLED(wsi->vhost)) {
921 if (getpeername(fd, p, &len) < 0) {
922 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
926 ret = lws_get_addresses(wsi->vhost, p, name, name_len, rip, rip_len);
929 lws_latency(context, wsi, "lws_get_peer_addresses", ret, 1);
942 lws_context_user(struct lws_context *context)
944 return context->user_space;
947 LWS_VISIBLE struct lws_vhost *
948 lws_vhost_get(struct lws *wsi)
953 LWS_VISIBLE struct lws_vhost *
954 lws_get_vhost(struct lws *wsi)
959 LWS_VISIBLE const struct lws_protocols *
960 lws_protocol_get(struct lws *wsi)
962 return wsi->protocol;
965 LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
966 lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name)
970 for (n = 0; n < vh->count_protocols; n++)
971 if (!strcmp(name, vh->protocols[n].name))
972 return &vh->protocols[n];
978 lws_callback_all_protocol(struct lws_context *context,
979 const struct lws_protocols *protocol, int reason)
981 struct lws_context_per_thread *pt = &context->pt[0];
982 unsigned int n, m = context->count_threads;
986 for (n = 0; n < pt->fds_count; n++) {
987 wsi = wsi_from_fd(context, pt->fds[n].fd);
990 if (wsi->protocol == protocol)
991 protocol->callback(wsi, reason, wsi->user_space,
1001 lws_callback_all_protocol_vhost(struct lws_vhost *vh,
1002 const struct lws_protocols *protocol, int reason)
1004 struct lws_context *context = vh->context;
1005 struct lws_context_per_thread *pt = &context->pt[0];
1006 unsigned int n, m = context->count_threads;
1010 for (n = 0; n < pt->fds_count; n++) {
1011 wsi = wsi_from_fd(context, pt->fds[n].fd);
1014 if (wsi->vhost == vh && wsi->protocol == protocol)
1015 protocol->callback(wsi, reason, wsi->user_space,
1024 LWS_VISIBLE LWS_EXTERN int
1025 lws_callback_vhost_protocols(struct lws *wsi, int reason, void *in, int len)
1029 for (n = 0; n < wsi->vhost->count_protocols; n++)
1030 if (wsi->vhost->protocols[n].callback(wsi, reason, NULL, in, len))
1036 LWS_VISIBLE LWS_EXTERN void
1037 lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops)
1039 context->fops = fops;
1042 LWS_VISIBLE LWS_EXTERN lws_filepos_t
1043 lws_vfs_tell(lws_fop_fd_t fop_fd)
1048 LWS_VISIBLE LWS_EXTERN lws_filepos_t
1049 lws_vfs_get_length(lws_fop_fd_t fop_fd)
1054 LWS_VISIBLE LWS_EXTERN uint32_t
1055 lws_vfs_get_mod_time(lws_fop_fd_t fop_fd)
1057 return fop_fd->mod_time;
1060 LWS_VISIBLE lws_fileofs_t
1061 lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
1064 lwsl_debug("%s: seeking to %ld, len %ld\n", __func__, (long)offset, (long)fop_fd->len);
1065 ofs = fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset - fop_fd->pos);
1066 lwsl_debug("%s: result %ld, fop_fd pos %ld\n", __func__, (long)ofs, (long)fop_fd->pos);
1071 LWS_VISIBLE lws_fileofs_t
1072 lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
1074 return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, fop_fd->len + fop_fd->pos + offset);
1078 const struct lws_plat_file_ops *
1079 lws_vfs_select_fops(const struct lws_plat_file_ops *fops, const char *vfs_path,
1082 const struct lws_plat_file_ops *pf;
1083 const char *p = vfs_path;
1088 /* no non-platform fops, just use that */
1094 * scan the vfs path looking for indications we are to be
1095 * handled by a specific fops
1103 /* the first one is always platform fops, so skip */
1107 while (n < ARRAY_SIZE(pf->fi) && pf->fi[n].sig) {
1108 if (p >= vfs_path + pf->fi[n].len)
1109 if (!strncmp(p - (pf->fi[n].len - 1),
1111 pf->fi[n].len - 1)) {
1126 LWS_VISIBLE LWS_EXTERN lws_fop_fd_t LWS_WARN_UNUSED_RESULT
1127 lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
1128 lws_fop_flags_t *flags)
1130 const char *vpath = "";
1131 const struct lws_plat_file_ops *selected = lws_vfs_select_fops(
1132 fops, vfs_path, &vpath);
1134 return selected->LWS_FOP_OPEN(fops, vfs_path, vpath, flags);
1139 * lws_now_secs() - seconds since 1970-1-1
1142 LWS_VISIBLE LWS_EXTERN unsigned long
1147 gettimeofday(&tv, NULL);
1156 lws_get_socket_fd(struct lws *wsi)
1158 return wsi->desc.sockfd;
1165 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1166 int ret, int completed)
1168 unsigned long long u;
1171 u = time_in_microseconds();
1174 wsi->latency_start = u;
1175 if (!wsi->action_start)
1176 wsi->action_start = u;
1180 if (wsi->action_start == wsi->latency_start)
1182 "Completion first try lat %lluus: %p: ret %d: %s\n",
1183 u - wsi->latency_start,
1184 (void *)wsi, ret, action);
1187 "Completion %lluus: lat %lluus: %p: ret %d: %s\n",
1188 u - wsi->action_start,
1189 u - wsi->latency_start,
1190 (void *)wsi, ret, action);
1191 wsi->action_start = 0;
1193 sprintf(buf, "lat %lluus: %p: ret %d: %s\n",
1194 u - wsi->latency_start, (void *)wsi, ret, action);
1196 if (u - wsi->latency_start > context->worst_latency) {
1197 context->worst_latency = u - wsi->latency_start;
1198 strcpy(context->worst_latency_info, buf);
1200 lwsl_latency("%s", buf);
1205 lws_rx_flow_control(struct lws *wsi, int enable)
1207 if (enable == (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
1210 lwsl_info("%s: (0x%p, %d)\n", __func__, wsi, enable);
1211 wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
1217 lws_rx_flow_allow_all_protocol(const struct lws_context *context,
1218 const struct lws_protocols *protocol)
1220 const struct lws_context_per_thread *pt = &context->pt[0];
1222 unsigned int n, m = context->count_threads;
1225 for (n = 0; n < pt->fds_count; n++) {
1226 wsi = wsi_from_fd(context, pt->fds[n].fd);
1229 if (wsi->protocol == protocol)
1230 lws_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1236 LWS_VISIBLE extern const char *
1237 lws_canonical_hostname(struct lws_context *context)
1239 return (const char *)context->canonical_hostname;
1242 int user_callback_handle_rxflow(lws_callback_function callback_function,
1244 enum lws_callback_reasons reason, void *user,
1245 void *in, size_t len)
1249 n = callback_function(wsi, reason, user, in, len);
1251 n = _lws_rx_flow_control(wsi);
1256 #if defined(LWS_WITH_ESP8266)
1258 #define strchr ets_strchr
1262 lws_set_proxy(struct lws_vhost *vhost, const char *proxy)
1264 #if !defined(LWS_WITH_ESP8266)
1266 char authstring[96];
1271 /* we have to deal with a possible redundant leading http:// */
1272 if (!strncmp(proxy, "http://", 7))
1275 p = strchr(proxy, '@');
1276 if (p) { /* auth is around */
1278 if ((unsigned int)(p - proxy) > sizeof(authstring) - 1)
1281 strncpy(authstring, proxy, p - proxy);
1282 // null termination not needed on input
1283 if (lws_b64_encode_string(authstring, (p - proxy),
1284 vhost->proxy_basic_auth_token,
1285 sizeof vhost->proxy_basic_auth_token) < 0)
1288 lwsl_info(" Proxy auth in use\n");
1292 vhost->proxy_basic_auth_token[0] = '\0';
1294 strncpy(vhost->http_proxy_address, proxy,
1295 sizeof(vhost->http_proxy_address) - 1);
1296 vhost->http_proxy_address[
1297 sizeof(vhost->http_proxy_address) - 1] = '\0';
1299 p = strchr(vhost->http_proxy_address, ':');
1300 if (!p && !vhost->http_proxy_port) {
1301 lwsl_err("http_proxy needs to be ads:port\n");
1307 vhost->http_proxy_port = atoi(p + 1);
1311 lwsl_info(" Proxy %s:%u\n", vhost->http_proxy_address,
1312 vhost->http_proxy_port);
1317 lwsl_err("proxy auth too long\n");
1322 #if defined(LWS_WITH_SOCKS5)
1324 lws_set_socks(struct lws_vhost *vhost, const char *socks)
1326 #if !defined(LWS_WITH_ESP8266)
1327 char *p_at, *p_colon;
1334 vhost->socks_user[0] = '\0';
1335 vhost->socks_password[0] = '\0';
1337 p_at = strchr(socks, '@');
1338 if (p_at) { /* auth is around */
1339 if ((unsigned int)(p_at - socks) > (sizeof(user)
1340 + sizeof(password) - 2)) {
1341 lwsl_err("Socks auth too long\n");
1345 p_colon = strchr(socks, ':');
1347 if ((unsigned int)(p_colon - socks) > (sizeof(user)
1349 lwsl_err("Socks user too long\n");
1352 if ((unsigned int)(p_at - p_colon) > (sizeof(password)
1354 lwsl_err("Socks password too long\n");
1358 strncpy(vhost->socks_user, socks, p_colon - socks);
1359 strncpy(vhost->socks_password, p_colon + 1,
1360 p_at - (p_colon + 1));
1362 lwsl_info(" Socks auth, user: %s, password: %s\n",
1363 vhost->socks_user, vhost->socks_password );
1368 strncpy(vhost->socks_proxy_address, socks,
1369 sizeof(vhost->socks_proxy_address) - 1);
1370 vhost->socks_proxy_address[sizeof(vhost->socks_proxy_address) - 1]
1373 p_colon = strchr(vhost->socks_proxy_address, ':');
1374 if (!p_colon && !vhost->socks_proxy_port) {
1375 lwsl_err("socks_proxy needs to be address:port\n");
1380 vhost->socks_proxy_port = atoi(p_colon + 1);
1384 lwsl_info(" Socks %s:%u\n", vhost->socks_proxy_address,
1385 vhost->socks_proxy_port);
1395 LWS_VISIBLE const struct lws_protocols *
1396 lws_get_protocol(struct lws *wsi)
1398 return wsi->protocol;
1402 lws_is_final_fragment(struct lws *wsi)
1404 lwsl_info("%s: final %d, rx pk length %ld, draining %ld\n", __func__,
1405 wsi->u.ws.final, (long)wsi->u.ws.rx_packet_length,
1406 (long)wsi->u.ws.rx_draining_ext);
1407 return wsi->u.ws.final && !wsi->u.ws.rx_packet_length && !wsi->u.ws.rx_draining_ext;
1410 LWS_VISIBLE unsigned char
1411 lws_get_reserved_bits(struct lws *wsi)
1413 return wsi->u.ws.rsv;
1417 lws_ensure_user_space(struct lws *wsi)
1419 lwsl_info("%s: %p protocol %p\n", __func__, wsi, wsi->protocol);
1423 /* allocate the per-connection user memory (if any) */
1425 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
1426 wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size);
1427 if (wsi->user_space == NULL) {
1428 lwsl_err("Out of memory for conn user space\n");
1432 lwsl_info("%s: %p protocol pss %lu, user_space=%p\n",
1433 __func__, wsi, (long)wsi->protocol->per_session_data_size,
1439 lwsl_timestamp(int level, char *p, int len)
1441 #ifndef LWS_PLAT_OPTEE
1442 time_t o_now = time(NULL);
1443 unsigned long long now;
1444 struct tm *ptm = NULL;
1452 ptm = localtime(&o_now);
1454 if (localtime_r(&o_now, &tm))
1459 for (n = 0; n < LLL_COUNT; n++) {
1460 if (level != (1 << n))
1462 now = time_in_microseconds() / 100;
1464 n = lws_snprintf(p, len,
1465 "[%04d/%02d/%02d %02d:%02d:%02d:%04d] %s: ",
1466 ptm->tm_year + 1900,
1472 (int)(now % 10000), log_level_names[n]);
1474 n = lws_snprintf(p, len, "[%llu:%04d] %s: ",
1475 (unsigned long long) now / 10000,
1476 (int)(now % 10000), log_level_names[n]);
1483 #ifndef LWS_PLAT_OPTEE
1484 LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line)
1486 #if !defined(LWS_WITH_ESP8266)
1489 lwsl_timestamp(level, buf, sizeof(buf));
1490 fprintf(stderr, "%s%s", buf, line);
1495 LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl)
1497 #if defined(LWS_WITH_ESP8266)
1504 if (!(log_level & filter))
1507 n = vsnprintf(buf, sizeof(buf) - 1, format, vl);
1509 #if defined(LWS_WITH_ESP8266)
1510 buf[sizeof(buf) - 1] = '\0';
1512 /* vnsprintf returns what it would have written, even if truncated */
1513 if (n > sizeof(buf) - 1)
1514 n = sizeof(buf) - 1;
1519 lwsl_emit(filter, buf);
1522 LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
1526 va_start(ap, format);
1527 _lws_logv(filter, format, ap);
1531 LWS_VISIBLE void lws_set_log_level(int level,
1532 void (*func)(int level, const char *line))
1539 LWS_VISIBLE int lwsl_visible(int level)
1541 return log_level & level;
1545 lws_is_ssl(struct lws *wsi)
1547 #ifdef LWS_OPENSSL_SUPPORT
1548 return wsi->use_ssl;
1555 #ifdef LWS_OPENSSL_SUPPORT
1557 lws_get_ssl(struct lws *wsi)
1564 lws_partial_buffered(struct lws *wsi)
1566 return !!wsi->trunc_len;
1569 void lws_set_protocol_write_pending(struct lws *wsi,
1570 enum lws_pending_protocol_send pend)
1572 lwsl_info("setting pps %d\n", pend);
1575 lwsl_err("pps overwrite\n");
1577 lws_rx_flow_control(wsi, 0);
1578 lws_callback_on_writable(wsi);
1582 lws_get_peer_write_allowance(struct lws *wsi)
1584 #ifdef LWS_USE_HTTP2
1585 /* only if we are using HTTP2 on this connection */
1586 if (wsi->mode != LWSCM_HTTP2_SERVING)
1588 /* user is only interested in how much he can send, or that he can't */
1589 if (wsi->u.http2.tx_credit <= 0)
1592 return wsi->u.http2.tx_credit;
1600 lws_union_transition(struct lws *wsi, enum connection_mode mode)
1602 lwsl_debug("%s: %p: mode %d\n", __func__, wsi, mode);
1603 memset(&wsi->u, 0, sizeof(wsi->u));
1607 LWS_VISIBLE struct lws_plat_file_ops *
1608 lws_get_fops(struct lws_context *context)
1610 return (struct lws_plat_file_ops *)context->fops;
1613 LWS_VISIBLE LWS_EXTERN struct lws_context *
1614 lws_get_context(const struct lws *wsi)
1616 return wsi->context;
1619 LWS_VISIBLE LWS_EXTERN int
1620 lws_get_count_threads(struct lws_context *context)
1622 return context->count_threads;
1625 LWS_VISIBLE LWS_EXTERN void *
1626 lws_wsi_user(struct lws *wsi)
1628 return wsi->user_space;
1631 LWS_VISIBLE LWS_EXTERN void
1632 lws_set_wsi_user(struct lws *wsi, void *data)
1634 if (wsi->user_space_externally_allocated)
1635 wsi->user_space = data;
1637 lwsl_err("%s: Cannot set internally-allocated user_space\n",
1641 LWS_VISIBLE LWS_EXTERN struct lws *
1642 lws_get_parent(const struct lws *wsi)
1647 LWS_VISIBLE LWS_EXTERN struct lws *
1648 lws_get_child(const struct lws *wsi)
1650 return wsi->child_list;
1653 LWS_VISIBLE LWS_EXTERN void
1654 lws_close_reason(struct lws *wsi, enum lws_close_status status,
1655 unsigned char *buf, size_t len)
1657 unsigned char *p, *start;
1658 int budget = sizeof(wsi->u.ws.ping_payload_buf) - LWS_PRE;
1660 assert(wsi->mode == LWSCM_WS_SERVING || wsi->mode == LWSCM_WS_CLIENT);
1662 start = p = &wsi->u.ws.ping_payload_buf[LWS_PRE];
1664 *p++ = (((int)status) >> 8) & 0xff;
1665 *p++ = ((int)status) & 0xff;
1668 while (len-- && p < start + budget)
1671 wsi->u.ws.close_in_ping_buffer_len = p - start;
1675 _lws_rx_flow_control(struct lws *wsi)
1677 struct lws *wsic = wsi->child_list;
1679 /* if he has children, do those if they were changed */
1681 if (wsic->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)
1682 _lws_rx_flow_control(wsic);
1684 wsic = wsic->sibling_list;
1687 /* there is no pending change */
1688 if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) {
1689 // lwsl_debug("%s: no pending change\n", __func__);
1693 /* stuff is still buffered, not ready to really accept new input */
1694 if (wsi->rxflow_buffer) {
1695 /* get ourselves called back to deal with stashed buffer */
1696 lws_callback_on_writable(wsi);
1700 /* pending is cleared, we can change rxflow state */
1702 wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1704 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1705 wsi->rxflow_change_to & LWS_RXFLOW_ALLOW);
1707 /* adjust the pollfd for this wsi */
1709 if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) {
1710 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
1711 lwsl_info("%s: fail\n", __func__);
1715 if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
1722 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len)
1724 static const unsigned char e0f4[] = {
1725 0xa0 | ((2 - 1) << 2) | 1, /* e0 */
1726 0x80 | ((4 - 1) << 2) | 1, /* e1 */
1727 0x80 | ((4 - 1) << 2) | 1, /* e2 */
1728 0x80 | ((4 - 1) << 2) | 1, /* e3 */
1729 0x80 | ((4 - 1) << 2) | 1, /* e4 */
1730 0x80 | ((4 - 1) << 2) | 1, /* e5 */
1731 0x80 | ((4 - 1) << 2) | 1, /* e6 */
1732 0x80 | ((4 - 1) << 2) | 1, /* e7 */
1733 0x80 | ((4 - 1) << 2) | 1, /* e8 */
1734 0x80 | ((4 - 1) << 2) | 1, /* e9 */
1735 0x80 | ((4 - 1) << 2) | 1, /* ea */
1736 0x80 | ((4 - 1) << 2) | 1, /* eb */
1737 0x80 | ((4 - 1) << 2) | 1, /* ec */
1738 0x80 | ((2 - 1) << 2) | 1, /* ed */
1739 0x80 | ((4 - 1) << 2) | 1, /* ee */
1740 0x80 | ((4 - 1) << 2) | 1, /* ef */
1741 0x90 | ((3 - 1) << 2) | 2, /* f0 */
1742 0x80 | ((4 - 1) << 2) | 2, /* f1 */
1743 0x80 | ((4 - 1) << 2) | 2, /* f2 */
1744 0x80 | ((4 - 1) << 2) | 2, /* f3 */
1745 0x80 | ((1 - 1) << 2) | 2, /* f4 */
1748 0x80 | ((4 - 1) << 2) | 0, /* s2 */
1749 0x80 | ((4 - 1) << 2) | 1, /* s3 */
1751 unsigned char s = *state;
1754 unsigned char c = *buf++;
1758 if (c < 0xc2 || c > 0xf4)
1761 s = 0x80 | ((4 - 1) << 2);
1766 if (c < (s & 0xf0) ||
1767 c >= (s & 0xf0) + 0x10 + ((s << 2) & 0x30))
1769 s = e0f4[21 + (s & 3)];
1778 LWS_VISIBLE LWS_EXTERN int
1779 lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
1783 static const char *slash = "/";
1785 /* cut up the location into address, port and path */
1787 while (*p && (*p != ':' || p[1] != '/' || p[2] != '/'))
1798 if (!strcmp(*prot, "http") || !strcmp(*prot, "ws"))
1800 else if (!strcmp(*prot, "https") || !strcmp(*prot, "wss"))
1806 while (*p && *p != ']')
1813 while (*p && *p != ':' && *p != '/')
1819 while (*p && *p != '/')
1832 #ifdef LWS_NO_EXTENSIONS
1834 /* we need to provide dummy callbacks for internal exts
1835 * so user code runs when faced with a lib compiled with
1836 * extensions disabled.
1840 lws_extension_callback_pm_deflate(struct lws_context *context,
1841 const struct lws_extension *ext,
1843 enum lws_extension_callback_reasons reason,
1844 void *user, void *in, size_t len)
1859 lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
1863 #ifdef LWS_USE_UNIX_SOCK
1864 struct sockaddr_un serv_unix;
1867 struct sockaddr_in6 serv_addr6;
1869 struct sockaddr_in serv_addr4;
1870 #ifndef LWS_PLAT_OPTEE
1871 socklen_t len = sizeof(struct sockaddr_storage);
1874 struct sockaddr_storage sin;
1877 #ifdef LWS_USE_UNIX_SOCK
1878 if (LWS_UNIX_SOCK_ENABLED(vhost)) {
1879 v = (struct sockaddr *)&serv_unix;
1880 n = sizeof(struct sockaddr_un);
1881 bzero((char *) &serv_unix, sizeof(serv_unix));
1882 serv_unix.sun_family = AF_UNIX;
1883 if (sizeof(serv_unix.sun_path) <= strlen(iface)) {
1884 lwsl_err("\"%s\" too long for UNIX domain socket\n",
1888 strcpy(serv_unix.sun_path, iface);
1889 if (serv_unix.sun_path[0] == '@')
1890 serv_unix.sun_path[0] = '\0';
1894 #if defined(LWS_USE_IPV6) && !defined(LWS_WITH_ESP32)
1895 if (LWS_IPV6_ENABLED(vhost)) {
1896 v = (struct sockaddr *)&serv_addr6;
1897 n = sizeof(struct sockaddr_in6);
1898 bzero((char *) &serv_addr6, sizeof(serv_addr6));
1900 if (interface_to_sa(vhost, iface,
1901 (struct sockaddr_in *)v, n) < 0) {
1902 lwsl_err("Unable to find interface %s\n", iface);
1905 serv_addr6.sin6_scope_id = lws_get_addr_scope(iface);
1908 serv_addr6.sin6_family = AF_INET6;
1909 serv_addr6.sin6_port = htons(port);
1913 v = (struct sockaddr *)&serv_addr4;
1914 n = sizeof(serv_addr4);
1915 bzero((char *) &serv_addr4, sizeof(serv_addr4));
1916 serv_addr4.sin_addr.s_addr = INADDR_ANY;
1917 serv_addr4.sin_family = AF_INET;
1918 #if !defined(LWS_WITH_ESP32)
1921 interface_to_sa(vhost, iface,
1922 (struct sockaddr_in *)v, n) < 0) {
1923 lwsl_err("Unable to find interface %s\n", iface);
1927 serv_addr4.sin_port = htons(port);
1930 n = bind(sockfd, v, n);
1931 #ifdef LWS_USE_UNIX_SOCK
1932 if (n < 0 && LWS_UNIX_SOCK_ENABLED(vhost)) {
1933 lwsl_err("ERROR on binding fd %d to \"%s\" (%d %d)\n",
1934 sockfd, iface, n, LWS_ERRNO);
1939 lwsl_err("ERROR on binding fd %d to port %d (%d %d)\n",
1940 sockfd, port, n, LWS_ERRNO);
1944 #ifndef LWS_PLAT_OPTEE
1945 if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
1946 lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
1949 #if defined(LWS_USE_IPV6)
1950 port = (sin.ss_family == AF_INET6) ?
1951 ntohs(((struct sockaddr_in6 *) &sin)->sin6_port) :
1952 ntohs(((struct sockaddr_in *) &sin)->sin_port);
1954 port = ntohs(((struct sockaddr_in *) &sin)->sin_port);
1961 #if defined(LWS_USE_IPV6)
1962 LWS_EXTERN unsigned long
1963 lws_get_addr_scope(const char *ipaddr)
1965 unsigned long scope = 0;
1968 struct ifaddrs *addrs, *addr;
1969 char ip[NI_MAXHOST];
1973 for (addr = addrs; addr; addr = addr->ifa_next) {
1974 if (!addr->ifa_addr ||
1975 addr->ifa_addr->sa_family != AF_INET6)
1978 getnameinfo(addr->ifa_addr,
1979 sizeof(struct sockaddr_in6),
1981 NULL, 0, NI_NUMERICHOST);
1985 if (ip[i++] == '%') {
1990 if (!strcmp(ip, ipaddr)) {
1991 scope = if_nametoindex(addr->ifa_name);
1997 PIP_ADAPTER_ADDRESSES adapter, addrs = NULL;
1998 PIP_ADAPTER_UNICAST_ADDRESS addr;
2001 struct sockaddr_in6 *sockaddr;
2002 char ip[NI_MAXHOST];
2006 for (i = 0; i < 5; i++)
2008 ret = GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX,
2009 NULL, addrs, &size);
2010 if ((ret == NO_ERROR) || (ret == ERROR_NO_DATA)) {
2012 } else if (ret == ERROR_BUFFER_OVERFLOW)
2016 addrs = (IP_ADAPTER_ADDRESSES *) malloc(size);
2024 lwsl_err("Failed to get IPv6 address table (%d)", ret);
2029 if ((ret == NO_ERROR) && (addrs))
2032 while ((adapter) && (!found))
2034 addr = adapter->FirstUnicastAddress;
2035 while ((addr) && (!found))
2037 if (addr->Address.lpSockaddr->sa_family == AF_INET6)
2039 sockaddr = (struct sockaddr_in6 *) (addr->Address.lpSockaddr);
2041 lws_plat_inet_ntop(sockaddr->sin6_family, &sockaddr->sin6_addr,
2044 if (!strcmp(ip, ipaddr)) {
2045 scope = sockaddr->sin6_scope_id;
2052 adapter = adapter->Next;
2064 lws_restart_ws_ping_pong_timer(struct lws *wsi)
2066 if (!wsi->context->ws_ping_pong_interval)
2068 if (wsi->state != LWSS_ESTABLISHED)
2071 wsi->u.ws.time_next_ping_check = (time_t)lws_now_secs() +
2072 wsi->context->ws_ping_pong_interval;
2075 static const char *hex = "0123456789ABCDEF";
2077 LWS_VISIBLE LWS_EXTERN const char *
2078 lws_sql_purify(char *escaped, const char *string, int len)
2080 const char *p = string;
2083 while (*p && len-- > 2) {
2097 LWS_VISIBLE LWS_EXTERN const char *
2098 lws_json_purify(char *escaped, const char *string, int len)
2100 const char *p = string;
2108 while (*p && len-- > 6) {
2109 if (*p == '\"' || *p == '\\' || *p < 0x20) {
2114 *q++ = hex[((*p) >> 4) & 15];
2115 *q++ = hex[(*p) & 15];
2126 LWS_VISIBLE LWS_EXTERN const char *
2127 lws_urlencode(char *escaped, const char *string, int len)
2129 const char *p = string;
2132 while (*p && len-- > 3) {
2138 if ((*p >= '0' && *p <= '9') ||
2139 (*p >= 'A' && *p <= 'Z') ||
2140 (*p >= 'a' && *p <= 'z')) {
2145 *q++ = hex[(*p >> 4) & 0xf];
2146 *q++ = hex[*p & 0xf];
2156 LWS_VISIBLE LWS_EXTERN int
2157 lws_urldecode(char *string, const char *escaped, int len)
2162 while (*escaped && len) {
2165 if (*escaped == '%') {
2170 if (*escaped == '+') {
2176 *string++ = *escaped++;
2180 n = char_to_hex(*escaped);
2189 n = char_to_hex(*escaped);
2193 *string++ = sum | n;
2205 LWS_VISIBLE LWS_EXTERN int
2206 lws_finalize_startup(struct lws_context *context)
2208 struct lws_context_creation_info info;
2210 info.uid = context->uid;
2211 info.gid = context->gid;
2213 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
2214 memcpy(info.caps, context->caps, sizeof(info.caps));
2215 info.count_caps = context->count_caps;
2218 if (lws_check_opt(context->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
2219 lws_plat_drop_app_privileges(&info);
2225 lws_snprintf(char *str, size_t size, const char *format, ...)
2233 va_start(ap, format);
2234 n = vsnprintf(str, size, format, ap);
2244 LWS_VISIBLE LWS_EXTERN int
2245 lws_is_cgi(struct lws *wsi) {
2256 urlencode(const char *in, int inlen, char *out, int outlen)
2258 char *start = out, *end = out + outlen;
2260 while (inlen-- && out < end - 4) {
2261 if ((*in >= 'A' && *in <= 'Z') ||
2262 (*in >= 'a' && *in <= 'z') ||
2263 (*in >= '0' && *in <= '9') ||
2277 *out++ = hex[(*in) >> 4];
2278 *out++ = hex[(*in++) & 15];
2289 lws_create_basic_wsi(struct lws_context *context, int tsi)
2291 struct lws *new_wsi;
2293 if ((unsigned int)context->pt[tsi].fds_count ==
2294 context->fd_limit_per_thread - 1) {
2295 lwsl_err("no space for new conn\n");
2299 new_wsi = lws_zalloc(sizeof(struct lws));
2300 if (new_wsi == NULL) {
2301 lwsl_err("Out of memory for new connection\n");
2306 new_wsi->context = context;
2307 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
2308 new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
2310 /* initialize the instance struct */
2312 new_wsi->state = LWSS_CGI;
2313 new_wsi->mode = LWSCM_CGI;
2314 new_wsi->hdr_parsing_completed = 0;
2315 new_wsi->position_in_fds_table = -1;
2318 * these can only be set once the protocol is known
2319 * we set an unestablished connection's protocol pointer
2320 * to the start of the defauly vhost supported list, so it can look
2321 * for matching ones during the handshake
2323 new_wsi->protocol = context->vhost_list->protocols;
2324 new_wsi->user_space = NULL;
2325 new_wsi->ietf_spec_revision = 0;
2326 new_wsi->desc.sockfd = LWS_SOCK_INVALID;
2327 context->count_wsi_allocated++;
2332 LWS_VISIBLE LWS_EXTERN int
2333 lws_cgi(struct lws *wsi, const char * const *exec_array, int script_uri_path_len,
2334 int timeout_secs, const struct lws_protocol_vhost_options *mp_cgienv)
2336 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
2337 char *env_array[30], cgi_path[400], e[1024], *p = e,
2338 *end = p + sizeof(e) - 1, tok[256], *t;
2339 struct lws_cgi *cgi;
2340 int n, m, i, uritok = -1;
2343 * give the master wsi a cgi struct
2346 wsi->cgi = lws_zalloc(sizeof(*wsi->cgi));
2348 lwsl_err("%s: OOM\n", __func__);
2352 wsi->cgi->response_code = HTTP_STATUS_OK;
2355 cgi->wsi = wsi; /* set cgi's owning wsi */
2357 /* create pipes for [stdin|stdout] and [stderr] */
2359 for (n = 0; n < 3; n++)
2360 if (pipe(cgi->pipe_fds[n]) == -1)
2363 /* create cgi wsis for each stdin/out/err fd */
2365 for (n = 0; n < 3; n++) {
2366 cgi->stdwsi[n] = lws_create_basic_wsi(wsi->context, wsi->tsi);
2367 if (!cgi->stdwsi[n])
2369 cgi->stdwsi[n]->cgi_channel = n;
2370 cgi->stdwsi[n]->vhost = wsi->vhost;
2372 // lwsl_err("%s: cgi %p: pipe fd %d -> fd %d / %d\n", __func__, wsi, n,
2373 // cgi->pipe_fds[n][!!(n == 0)], cgi->pipe_fds[n][!(n == 0)]);
2375 /* read side is 0, stdin we want the write side, others read */
2376 cgi->stdwsi[n]->desc.sockfd = cgi->pipe_fds[n][!!(n == 0)];
2377 if (fcntl(cgi->pipe_fds[n][!!(n == 0)], F_SETFL, O_NONBLOCK) < 0) {
2378 lwsl_err("%s: setting NONBLOCK failed\n", __func__);
2383 for (n = 0; n < 3; n++) {
2384 lws_libuv_accept(cgi->stdwsi[n], cgi->stdwsi[n]->desc);
2385 if (insert_wsi_socket_into_fds(wsi->context, cgi->stdwsi[n]))
2387 cgi->stdwsi[n]->parent = wsi;
2388 cgi->stdwsi[n]->sibling_list = wsi->child_list;
2389 wsi->child_list = cgi->stdwsi[n];
2392 lws_change_pollfd(cgi->stdwsi[LWS_STDIN], LWS_POLLIN, LWS_POLLOUT);
2393 lws_change_pollfd(cgi->stdwsi[LWS_STDOUT], LWS_POLLOUT, LWS_POLLIN);
2394 lws_change_pollfd(cgi->stdwsi[LWS_STDERR], LWS_POLLOUT, LWS_POLLIN);
2396 lwsl_debug("%s: fds in %d, out %d, err %d\n", __func__,
2397 cgi->stdwsi[LWS_STDIN]->desc.sockfd,
2398 cgi->stdwsi[LWS_STDOUT]->desc.sockfd,
2399 cgi->stdwsi[LWS_STDERR]->desc.sockfd);
2401 lws_set_timeout(wsi, PENDING_TIMEOUT_CGI, timeout_secs);
2403 /* the cgi stdout is always sending us http1.x header data first */
2404 wsi->hdr_state = LCHS_HEADER;
2406 /* add us to the pt list of active cgis */
2407 lwsl_debug("%s: adding cgi %p to list\n", __func__, wsi->cgi);
2408 cgi->cgi_list = pt->cgi_list;
2411 /* prepare his CGI env */
2415 if (lws_is_ssl(wsi))
2416 env_array[n++] = "HTTPS=ON";
2417 if (wsi->u.hdr.ah) {
2418 static const unsigned char meths[] = {
2421 WSI_TOKEN_OPTIONS_URI,
2423 WSI_TOKEN_PATCH_URI,
2424 WSI_TOKEN_DELETE_URI,
2426 static const char * const meth_names[] = {
2427 "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE",
2430 for (m = 0; m < ARRAY_SIZE(meths); m++)
2431 if (lws_hdr_total_length(wsi, meths[m]) >=
2432 script_uri_path_len) {
2440 lws_snprintf(cgi_path, sizeof(cgi_path) - 1, "REQUEST_URI=%s",
2441 lws_hdr_simple_ptr(wsi, uritok));
2442 cgi_path[sizeof(cgi_path) - 1] = '\0';
2443 env_array[n++] = cgi_path;
2446 p += lws_snprintf(p, end - p, "REQUEST_METHOD=%s",
2451 p += lws_snprintf(p, end - p, "QUERY_STRING=");
2452 /* dump the individual URI Arg parameters */
2455 i = lws_hdr_copy_fragment(wsi, tok, sizeof(tok),
2456 WSI_TOKEN_HTTP_URI_ARGS, m);
2460 while (*t && *t != '=' && p < end - 4)
2464 i = urlencode(t, i- (t - tok), p, end - p);
2476 p += lws_snprintf(p, end - p, "PATH_INFO=%s",
2477 lws_hdr_simple_ptr(wsi, uritok) +
2478 script_uri_path_len);
2481 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_REFERER)) {
2483 p += lws_snprintf(p, end - p, "HTTP_REFERER=%s",
2484 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_REFERER));
2487 if (lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
2489 p += lws_snprintf(p, end - p, "HTTP_HOST=%s",
2490 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
2493 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) {
2495 p += lws_snprintf(p, end - p, "HTTP_COOKIE=%s",
2496 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COOKIE));
2499 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT)) {
2501 p += lws_snprintf(p, end - p, "USER_AGENT=%s",
2502 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_USER_AGENT));
2505 if (uritok == WSI_TOKEN_POST_URI) {
2506 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE)) {
2508 p += lws_snprintf(p, end - p, "CONTENT_TYPE=%s",
2509 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE));
2512 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
2514 p += lws_snprintf(p, end - p, "CONTENT_LENGTH=%s",
2515 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH));
2520 p += lws_snprintf(p, end - p, "SCRIPT_PATH=%s", exec_array[0]) + 1;
2524 p += lws_snprintf(p, end - p, "%s=%s", mp_cgienv->name,
2526 lwsl_debug(" Applying mount-specific cgi env '%s'\n",
2529 mp_cgienv = mp_cgienv->next;
2532 env_array[n++] = "SERVER_SOFTWARE=libwebsockets";
2533 env_array[n++] = "PATH=/bin:/usr/bin:/usr/local/bin:/var/www/cgi-bin";
2534 env_array[n] = NULL;
2537 for (m = 0; m < n; m++)
2538 lwsl_err(" %s\n", env_array[m]);
2542 * Actually having made the env, as a cgi we don't need the ah
2545 if (lws_header_table_is_in_detachable_state(wsi))
2546 lws_header_table_detach(wsi, 0);
2548 /* we are ready with the redirection pipes... run the thing */
2549 #if !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
2555 lwsl_err("fork failed, errno %d", errno);
2559 #if defined(__linux__)
2560 prctl(PR_SET_PDEATHSIG, SIGTERM);
2562 setpgrp(); /* stops on-daemonized main processess getting SIGINT from TTY */
2565 /* we are the parent process */
2566 wsi->context->count_cgi_spawned++;
2567 lwsl_debug("%s: cgi %p spawned PID %d\n", __func__, cgi, cgi->pid);
2571 /* somewhere we can at least read things and enter it */
2573 lwsl_notice("%s: Failed to chdir\n", __func__);
2575 /* We are the forked process, redirect and kill inherited things.
2577 * Because of vfork(), we cannot do anything that changes pages in
2578 * the parent environment. Stuff that changes kernel state for the
2579 * process is OK. Stuff that happens after the execvpe() is OK.
2582 for (n = 0; n < 3; n++) {
2583 if (dup2(cgi->pipe_fds[n][!(n == 0)], n) < 0) {
2584 lwsl_err("%s: stdin dup2 failed\n", __func__);
2587 close(cgi->pipe_fds[n][!(n == 0)]);
2590 #if !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
2591 for (m = 0; m < n; m++) {
2592 p = strchr(env_array[m], '=');
2594 setenv(env_array[m], p, 1);
2596 execvp(exec_array[0], (char * const *)&exec_array[0]);
2598 execvpe(exec_array[0], (char * const *)&exec_array[0], &env_array[0]);
2604 /* drop us from the pt cgi list */
2605 pt->cgi_list = cgi->cgi_list;
2608 remove_wsi_socket_from_fds(wsi->cgi->stdwsi[n]);
2610 for (n = 0; n < 3; n++)
2611 if (wsi->cgi->stdwsi[n])
2612 lws_free_wsi(cgi->stdwsi[n]);
2615 for (n = 0; n < 3; n++) {
2616 if (cgi->pipe_fds[n][0])
2617 close(cgi->pipe_fds[n][0]);
2618 if (cgi->pipe_fds[n][1])
2619 close(cgi->pipe_fds[n][1]);
2622 lws_free_set_NULL(wsi->cgi);
2624 lwsl_err("%s: failed\n", __func__);
2629 /* we have to parse out these headers in the CGI output */
2631 static const char * const significant_hdr[SIGNIFICANT_HDR_COUNT] = {
2635 "transfer-encoding: chunked",
2638 LWS_VISIBLE LWS_EXTERN int
2639 lws_cgi_write_split_stdout_headers(struct lws *wsi)
2642 unsigned char buf[LWS_PRE + 1024], *start = &buf[LWS_PRE], *p = start,
2643 *end = &buf[sizeof(buf) - 1 - LWS_PRE];
2649 while (wsi->hdr_state != LHCS_PAYLOAD) {
2650 /* we have to separate header / finalize and
2651 * payload chunks, since they need to be
2652 * handled separately
2655 switch (wsi->hdr_state) {
2658 lwsl_info("LHCS_RESPONSE: issuing response %d\n",
2659 wsi->cgi->response_code);
2660 if (lws_add_http_header_status(wsi, wsi->cgi->response_code, &p, end))
2662 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
2663 (unsigned char *)"close", 5, &p, end))
2665 n = lws_write(wsi, start, p - start,
2666 LWS_WRITE_HTTP_HEADERS);
2668 /* finalize cached headers before dumping them */
2669 if (lws_finalize_http_header(wsi,
2670 (unsigned char **)&wsi->cgi->headers_pos,
2671 (unsigned char *)wsi->cgi->headers_end)) {
2673 lwsl_notice("finalize failed\n");
2677 wsi->hdr_state = LHCS_DUMP_HEADERS;
2678 wsi->reason_bf |= 8;
2679 lws_callback_on_writable(wsi);
2680 /* back to the loop for writeability again */
2683 case LHCS_DUMP_HEADERS:
2685 n = wsi->cgi->headers_pos - wsi->cgi->headers_dumped;
2689 lwsl_debug("LHCS_DUMP_HEADERS: %d\n", n);
2691 m = lws_write(wsi, (unsigned char *)wsi->cgi->headers_dumped,
2692 n, LWS_WRITE_HTTP_HEADERS);
2694 lwsl_debug("%s: write says %d\n", __func__, m);
2697 wsi->cgi->headers_dumped += n;
2698 if (wsi->cgi->headers_dumped == wsi->cgi->headers_pos) {
2699 wsi->hdr_state = LHCS_PAYLOAD;
2700 lws_free_set_NULL(wsi->cgi->headers_buf);
2701 lwsl_debug("freed cgi headers\n");
2703 wsi->reason_bf |= 8;
2704 lws_callback_on_writable(wsi);
2707 /* writeability becomes uncertain now we wrote
2708 * something, we must return to the event loop
2713 if (!wsi->cgi->headers_buf) {
2714 /* if we don't already have a headers buf, cook one up */
2716 wsi->cgi->headers_buf = malloc(n);
2717 if (!wsi->cgi->headers_buf) {
2722 lwsl_debug("allocated cgi hdrs\n");
2723 wsi->cgi->headers_pos = wsi->cgi->headers_buf;
2724 wsi->cgi->headers_dumped = wsi->cgi->headers_pos;
2725 wsi->cgi->headers_end = wsi->cgi->headers_buf + n - 1;
2727 for (n = 0; n < SIGNIFICANT_HDR_COUNT; n++) {
2728 wsi->cgi->match[n] = 0;
2733 n = read(lws_get_socket_fd(wsi->cgi->stdwsi[LWS_STDOUT]), &c, 1);
2735 if (errno != EAGAIN) {
2736 lwsl_debug("%s: read says %d\n", __func__, n);
2742 if (wsi->cgi->headers_pos >= wsi->cgi->headers_end - 4) {
2743 lwsl_notice("CGI headers larger than buffer size\n");
2749 lwsl_debug("-- 0x%02X %c %d %d\n", (unsigned char)c, c, wsi->cgi->match[1], wsi->hdr_state);
2752 switch (wsi->hdr_state) {
2755 for (n = 0; n < SIGNIFICANT_HDR_COUNT; n++) {
2756 /* significant headers with numeric decimal payloads */
2757 if (!significant_hdr[n][wsi->cgi->match[n]] &&
2758 (c >= '0' && c <= '9') &&
2759 wsi->cgi->lp < sizeof(wsi->cgi->l) - 1) {
2760 wsi->cgi->l[wsi->cgi->lp++] = c;
2761 wsi->cgi->l[wsi->cgi->lp] = '\0';
2763 case SIGNIFICANT_HDR_CONTENT_LENGTH:
2764 wsi->cgi->content_length = atoll(wsi->cgi->l);
2766 case SIGNIFICANT_HDR_STATUS:
2767 wsi->cgi->response_code = atol(wsi->cgi->l);
2768 lwsl_debug("Status set to %d\n", wsi->cgi->response_code);
2774 /* hits up to the NUL are sticky until next hdr */
2775 if (significant_hdr[n][wsi->cgi->match[n]]) {
2776 if (tolower(c) == significant_hdr[n][wsi->cgi->match[n]])
2777 wsi->cgi->match[n]++;
2779 wsi->cgi->match[n] = 0;
2783 /* some cgi only send us \x0a for EOL */
2785 wsi->hdr_state = LCHS_SINGLE_0A;
2786 *wsi->cgi->headers_pos++ = '\x0d';
2788 *wsi->cgi->headers_pos++ = c;
2790 wsi->hdr_state = LCHS_LF1;
2792 if (wsi->hdr_state != LCHS_HEADER &&
2793 !significant_hdr[SIGNIFICANT_HDR_TRANSFER_ENCODING][wsi->cgi->match[SIGNIFICANT_HDR_TRANSFER_ENCODING]]) {
2794 lwsl_debug("cgi produced chunked\n");
2795 wsi->cgi->explicitly_chunked = 1;
2798 /* presence of Location: mandates 302 retcode */
2799 if (wsi->hdr_state != LCHS_HEADER &&
2800 !significant_hdr[SIGNIFICANT_HDR_LOCATION][wsi->cgi->match[SIGNIFICANT_HDR_LOCATION]]) {
2801 lwsl_debug("CGI: Location hdr seen\n");
2802 wsi->cgi->response_code = 302;
2807 *wsi->cgi->headers_pos++ = c;
2809 wsi->hdr_state = LCHS_CR2;
2812 /* we got \r[^\n]... it's unreasonable */
2813 lwsl_debug("%s: funny CRLF 0x%02X\n", __func__, (unsigned char)c);
2819 wsi->hdr_state = LCHS_LF2;
2822 wsi->hdr_state = LCHS_HEADER;
2823 for (n = 0; n < SIGNIFICANT_HDR_COUNT; n++)
2824 wsi->cgi->match[n] = 0;
2829 case LCHS_SINGLE_0A:
2832 lwsl_debug("Content-Length: %lld\n", (unsigned long long)wsi->cgi->content_length);
2833 wsi->hdr_state = LHCS_RESPONSE;
2834 /* drop the \0xa ... finalize will add it if needed */
2838 /* we got \r\n\r[^\n]... it's unreasonable */
2840 /* we got \x0anext header, it's reasonable */
2841 *wsi->cgi->headers_pos++ = c;
2842 wsi->hdr_state = LCHS_HEADER;
2843 for (n = 0; n < SIGNIFICANT_HDR_COUNT; n++)
2844 wsi->cgi->match[n] = 0;
2852 /* ran out of input, ended the headers, or filled up the headers buf */
2853 if (!n || wsi->hdr_state == LHCS_PAYLOAD)
2857 /* payload processing */
2859 n = read(lws_get_socket_fd(wsi->cgi->stdwsi[LWS_STDOUT]),
2860 start, sizeof(buf) - LWS_PRE);
2862 if (n < 0 && errno != EAGAIN) {
2863 lwsl_debug("%s: stdout read says %d\n", __func__, n);
2867 m = lws_write(wsi, (unsigned char *)start, n, LWS_WRITE_HTTP);
2868 //lwsl_notice("write %d\n", m);
2870 lwsl_debug("%s: stdout write says %d\n", __func__, m);
2873 wsi->cgi->content_length_seen += m;
2879 LWS_VISIBLE LWS_EXTERN int
2880 lws_cgi_kill(struct lws *wsi)
2882 struct lws_cgi_args args;
2885 lwsl_debug("%s: %p\n", __func__, wsi);
2890 if (wsi->cgi->pid > 0) {
2891 n = waitpid(wsi->cgi->pid, &status, WNOHANG);
2893 lwsl_debug("%s: PID %d reaped\n", __func__,
2897 /* kill the process group */
2898 n = kill(-wsi->cgi->pid, SIGTERM);
2899 lwsl_debug("%s: SIGTERM child PID %d says %d (errno %d)\n", __func__,
2900 wsi->cgi->pid, n, errno);
2903 * hum seen errno=3 when process is listed in ps,
2904 * it seems we don't always retain process grouping
2906 * Direct these fallback attempt to the exact child
2908 n = kill(wsi->cgi->pid, SIGTERM);
2910 n = kill(wsi->cgi->pid, SIGPIPE);
2912 n = kill(wsi->cgi->pid, SIGKILL);
2914 lwsl_err("%s: SIGKILL PID %d failed errno %d (maybe zombie)\n",
2915 __func__, wsi->cgi->pid, errno);
2919 /* He could be unkillable because he's a zombie */
2922 n = waitpid(-wsi->cgi->pid, &status, WNOHANG);
2924 lwsl_debug("%s: reaped PID %d\n", __func__, n);
2926 n = waitpid(wsi->cgi->pid, &status, WNOHANG);
2928 lwsl_debug("%s: reaped PID %d\n", __func__, n);
2934 args.stdwsi = &wsi->cgi->stdwsi[0];
2936 if (wsi->cgi->pid != -1 && user_callback_handle_rxflow(
2937 wsi->protocol->callback,
2938 wsi, LWS_CALLBACK_CGI_TERMINATED,
2940 (void *)&args, 0)) {
2942 if (!wsi->cgi->being_closed)
2943 lws_close_free_wsi(wsi, 0);
2950 lws_cgi_kill_terminated(struct lws_context_per_thread *pt)
2952 struct lws_cgi **pcgi, *cgi = NULL;
2956 /* find finished guys but don't reap yet */
2957 n = waitpid(-1, &status, WNOHANG);
2960 lwsl_debug("%s: observed PID %d terminated\n", __func__, n);
2962 pcgi = &pt->cgi_list;
2964 /* check all the subprocesses on the cgi list */
2966 /* get the next one first as list may change */
2968 pcgi = &(*pcgi)->cgi_list;
2973 /* finish sending cached headers */
2974 if (cgi->headers_buf)
2977 /* wait for stdout to be drained */
2978 if (cgi->content_length > cgi->content_length_seen)
2981 if (cgi->content_length) {
2982 lwsl_debug("%s: wsi %p: expected content length seen: %lld\n",
2983 __func__, cgi->wsi, (unsigned long long)cgi->content_length_seen);
2987 waitpid(n, &status, WNOHANG);
2989 * he's already terminated so no need for kill()
2990 * but we should do the terminated cgi callback
2991 * and close him if he's not already closing
2993 if (n == cgi->pid) {
2994 lwsl_debug("%s: found PID %d on cgi list\n",
2997 if (!cgi->content_length && cgi->explicitly_chunked) {
2999 * well, if he sends chunked... give him 5s after the
3000 * cgi terminated to send buffered
3002 cgi->chunked_grace++;
3008 lws_cgi_kill(cgi->wsi);
3014 /* if not found on the cgi list, as he's one of ours, reap */
3016 lwsl_debug("%s: reading PID %d although no cgi match\n",
3018 waitpid(n, &status, WNOHANG);
3022 /* disable this to confirm timeout cgi cleanup flow */
3024 pcgi = &pt->cgi_list;
3026 /* check all the subprocesses on the cgi list */
3028 /* get the next one first as list may change */
3030 pcgi = &(*pcgi)->cgi_list;
3035 /* we deferred killing him after reaping his PID */
3036 if (cgi->chunked_grace) {
3037 cgi->chunked_grace++;
3038 if (cgi->chunked_grace < 5)
3043 /* finish sending cached headers */
3044 if (cgi->headers_buf)
3047 /* wait for stdout to be drained */
3048 if (cgi->content_length > cgi->content_length_seen)
3051 if (cgi->content_length)
3052 lwsl_debug("%s: wsi %p: expected content length seen: %lld\n",
3053 __func__, cgi->wsi, (unsigned long long)cgi->content_length_seen);
3056 if (waitpid(cgi->pid, &status, WNOHANG) > 0) {
3058 if (!cgi->content_length) {
3060 * well, if he sends chunked... give him 5s after the
3061 * cgi terminated to send buffered
3063 cgi->chunked_grace++;
3067 lwsl_debug("%s: found PID %d on cgi list\n",
3068 __func__, cgi->pid);
3071 lws_cgi_kill(cgi->wsi);
3078 /* general anti zombie defence */
3079 // n = waitpid(-1, &status, WNOHANG);
3081 // lwsl_notice("%s: anti-zombie wait says %d\n", __func__, n);
3087 #ifdef LWS_NO_EXTENSIONS
3089 lws_set_extension_option(struct lws *wsi, const char *ext_name,
3090 const char *opt_name, const char *opt_val)
3096 #ifdef LWS_WITH_ACCESS_LOG
3098 lws_access_log(struct lws *wsi)
3100 char *p = wsi->access_log.user_agent, ass[512];
3103 if (!wsi->access_log_pending)
3106 if (!wsi->access_log.header_log)
3112 l = lws_snprintf(ass, sizeof(ass) - 1, "%s %d %lu %s\n",
3113 wsi->access_log.header_log,
3114 wsi->access_log.response, wsi->access_log.sent, p);
3116 if (wsi->vhost->log_fd != (int)LWS_INVALID_FILE) {
3117 if (write(wsi->vhost->log_fd, ass, l) != l)
3118 lwsl_err("Failed to write log\n");
3120 lwsl_err("%s", ass);
3122 if (wsi->access_log.header_log) {
3123 lws_free(wsi->access_log.header_log);
3124 wsi->access_log.header_log = NULL;
3126 if (wsi->access_log.user_agent) {
3127 lws_free(wsi->access_log.user_agent);
3128 wsi->access_log.user_agent = NULL;
3130 wsi->access_log_pending = 0;
3137 lws_sum_stats(const struct lws_context *ctx, struct lws_conn_stats *cs)
3139 const struct lws_vhost *vh = ctx->vhost_list;
3143 cs->rx += vh->conn_stats.rx;
3144 cs->tx += vh->conn_stats.tx;
3145 cs->conn += vh->conn_stats.conn;
3146 cs->trans += vh->conn_stats.trans;
3147 cs->ws_upg += vh->conn_stats.ws_upg;
3148 cs->http2_upg += vh->conn_stats.http2_upg;
3149 cs->rejected += vh->conn_stats.rejected;
3151 vh = vh->vhost_next;
3155 #ifdef LWS_WITH_SERVER_STATUS
3158 lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len)
3160 static const char * const prots[] = {
3169 char *orig = buf, *end = buf + len - 1, first = 1;
3175 buf += lws_snprintf(buf, end - buf,
3176 "{\n \"name\":\"%s\",\n"
3177 " \"port\":\"%d\",\n"
3178 " \"use_ssl\":\"%d\",\n"
3179 " \"sts\":\"%d\",\n"
3180 " \"rx\":\"%llu\",\n"
3181 " \"tx\":\"%llu\",\n"
3182 " \"conn\":\"%lu\",\n"
3183 " \"trans\":\"%lu\",\n"
3184 " \"ws_upg\":\"%lu\",\n"
3185 " \"rejected\":\"%lu\",\n"
3186 " \"http2_upg\":\"%lu\""
3188 vh->name, vh->listen_port,
3189 #ifdef LWS_OPENSSL_SUPPORT
3194 !!(vh->options & LWS_SERVER_OPTION_STS),
3195 vh->conn_stats.rx, vh->conn_stats.tx,
3196 vh->conn_stats.conn, vh->conn_stats.trans,
3197 vh->conn_stats.ws_upg,
3198 vh->conn_stats.rejected,
3199 vh->conn_stats.http2_upg
3202 if (vh->mount_list) {
3203 const struct lws_http_mount *m = vh->mount_list;
3205 buf += lws_snprintf(buf, end - buf, ",\n \"mounts\":[");
3208 buf += lws_snprintf(buf, end - buf, ",");
3209 buf += lws_snprintf(buf, end - buf,
3210 "\n {\n \"mountpoint\":\"%s\",\n"
3211 " \"origin\":\"%s%s\",\n"
3212 " \"cache_max_age\":\"%d\",\n"
3213 " \"cache_reuse\":\"%d\",\n"
3214 " \"cache_revalidate\":\"%d\",\n"
3215 " \"cache_intermediaries\":\"%d\"\n"
3218 prots[m->origin_protocol],
3222 m->cache_revalidate,
3223 m->cache_intermediaries);
3225 buf += lws_snprintf(buf, end - buf,
3226 ",\n \"default\":\"%s\"",
3228 buf += lws_snprintf(buf, end - buf, "\n }");
3232 buf += lws_snprintf(buf, end - buf, "\n ]");
3235 if (vh->protocols) {
3239 buf += lws_snprintf(buf, end - buf, ",\n \"ws-protocols\":[");
3240 while (n < vh->count_protocols) {
3242 buf += lws_snprintf(buf, end - buf, ",");
3243 buf += lws_snprintf(buf, end - buf,
3245 " \"status\":\"ok\"\n }\n }"
3247 vh->protocols[n].name);
3251 buf += lws_snprintf(buf, end - buf, "\n ]");
3254 buf += lws_snprintf(buf, end - buf, "\n}");
3260 LWS_EXTERN LWS_VISIBLE int
3261 lws_json_dump_context(const struct lws_context *context, char *buf, int len,
3264 char *orig = buf, *end = buf + len - 1, first = 1;
3265 const struct lws_vhost *vh = context->vhost_list;
3266 const struct lws_context_per_thread *pt;
3267 time_t t = time(NULL);
3268 int n, listening = 0, cgi_count = 0;
3269 struct lws_conn_stats cs;
3272 struct lws_cgi * const *pcgi;
3275 #ifdef LWS_USE_LIBUV
3279 buf += lws_snprintf(buf, end - buf, "{ "
3280 "\"version\":\"%s\",\n"
3281 "\"uptime\":\"%ld\",\n",
3282 lws_get_library_version(),
3285 #ifdef LWS_HAVE_GETLOADAVG
3290 m = getloadavg(d, 3);
3291 for (n = 0; n < m; n++) {
3292 buf += lws_snprintf(buf, end - buf,
3293 "\"l%d\":\"%.2f\",\n",
3299 buf += lws_snprintf(buf, end - buf, "\"contexts\":[\n");
3301 buf += lws_snprintf(buf, end - buf, "{ "
3302 "\"context_uptime\":\"%ld\",\n"
3303 "\"cgi_spawned\":\"%d\",\n"
3304 "\"pt_fd_max\":\"%d\",\n"
3305 "\"ah_pool_max\":\"%d\",\n"
3306 "\"deprecated\":\"%d\",\n"
3307 "\"wsi_alive\":\"%d\",\n",
3308 (unsigned long)(t - context->time_up),
3309 context->count_cgi_spawned,
3310 context->fd_limit_per_thread,
3311 context->max_http_header_pool,
3312 context->deprecated,
3313 context->count_wsi_allocated);
3315 buf += lws_snprintf(buf, end - buf, "\"pt\":[\n ");
3316 for (n = 0; n < context->count_threads; n++) {
3317 pt = &context->pt[n];
3319 buf += lws_snprintf(buf, end - buf, ",");
3320 buf += lws_snprintf(buf, end - buf,
3322 " \"fds_count\":\"%d\",\n"
3323 " \"ah_pool_inuse\":\"%d\",\n"
3324 " \"ah_wait_list\":\"%d\"\n"
3327 pt->ah_count_in_use,
3328 pt->ah_wait_list_length);
3331 buf += lws_snprintf(buf, end - buf, "]");
3333 buf += lws_snprintf(buf, end - buf, ", \"vhosts\":[\n ");
3336 vh = context->vhost_list;
3338 cs = context->conn_stats;
3339 lws_sum_stats(context, &cs);
3346 buf += lws_json_dump_vhost(vh, buf, end - buf);
3351 vh = vh->vhost_next;
3354 buf += lws_snprintf(buf, end - buf,
3355 "],\n\"listen_wsi\":\"%d\",\n"
3356 " \"rx\":\"%llu\",\n"
3357 " \"tx\":\"%llu\",\n"
3358 " \"conn\":\"%lu\",\n"
3359 " \"trans\":\"%lu\",\n"
3360 " \"ws_upg\":\"%lu\",\n"
3361 " \"rejected\":\"%lu\",\n"
3362 " \"http2_upg\":\"%lu\"",
3364 cs.rx, cs.tx, cs.conn, cs.trans,
3365 cs.ws_upg, cs.rejected, cs.http2_upg);
3368 for (n = 0; n < context->count_threads; n++) {
3369 pt = &context->pt[n];
3370 pcgi = &pt->cgi_list;
3373 pcgi = &(*pcgi)->cgi_list;
3379 buf += lws_snprintf(buf, end - buf, ",\n \"cgi_alive\":\"%d\"\n ",
3382 buf += lws_snprintf(buf, end - buf, "}");
3385 buf += lws_snprintf(buf, end - buf, "]}\n ");
3392 #if defined(LWS_WITH_STATS)
3394 LWS_VISIBLE LWS_EXTERN uint64_t
3395 lws_stats_get(struct lws_context *context, int index)
3397 if (index >= LWSSTATS_SIZE)
3400 return context->lws_stats[index];
3403 LWS_VISIBLE LWS_EXTERN void
3404 lws_stats_log_dump(struct lws_context *context)
3406 struct lws_vhost *v = context->vhost_list;
3409 if (!context->updated)
3412 context->updated = 0;
3415 lwsl_notice("LWS internal statistics dump ----->\n");
3416 lwsl_notice("LWSSTATS_C_CONNECTIONS: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_CONNECTIONS));
3417 lwsl_notice("LWSSTATS_C_API_CLOSE: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_API_CLOSE));
3418 lwsl_notice("LWSSTATS_C_API_READ: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_API_READ));
3419 lwsl_notice("LWSSTATS_C_API_LWS_WRITE: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_API_LWS_WRITE));
3420 lwsl_notice("LWSSTATS_C_API_WRITE: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_API_WRITE));
3421 lwsl_notice("LWSSTATS_C_WRITE_PARTIALS: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_WRITE_PARTIALS));
3422 lwsl_notice("LWSSTATS_C_WRITEABLE_CB_REQ: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB_REQ));
3423 lwsl_notice("LWSSTATS_C_WRITEABLE_CB_EFF_REQ: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB_EFF_REQ));
3424 lwsl_notice("LWSSTATS_C_WRITEABLE_CB: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB));
3425 lwsl_notice("LWSSTATS_C_SSL_CONNECTIONS_FAILED: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_SSL_CONNECTIONS_FAILED));
3426 lwsl_notice("LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED));
3427 lwsl_notice("LWSSTATS_C_SSL_CONNS_HAD_RX: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_SSL_CONNS_HAD_RX));
3429 lwsl_notice("LWSSTATS_C_TIMEOUTS: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_TIMEOUTS));
3430 lwsl_notice("LWSSTATS_C_SERVICE_ENTRY: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_SERVICE_ENTRY));
3431 lwsl_notice("LWSSTATS_B_READ: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_B_READ));
3432 lwsl_notice("LWSSTATS_B_WRITE: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_B_WRITE));
3433 lwsl_notice("LWSSTATS_B_PARTIALS_ACCEPTED_PARTS: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_B_PARTIALS_ACCEPTED_PARTS));
3434 lwsl_notice("LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY: %8llums\n", (unsigned long long)lws_stats_get(context, LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY) / 1000);
3435 if (lws_stats_get(context, LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED))
3436 lwsl_notice(" Avg accept delay: %8llums\n",
3437 (unsigned long long)(lws_stats_get(context, LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY) /
3438 lws_stats_get(context, LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED)) / 1000);
3439 lwsl_notice("LWSSTATS_MS_SSL_RX_DELAY: %8llums\n", (unsigned long long)lws_stats_get(context, LWSSTATS_MS_SSL_RX_DELAY) / 1000);
3440 if (lws_stats_get(context, LWSSTATS_C_SSL_CONNS_HAD_RX))
3441 lwsl_notice(" Avg accept-rx delay: %8llums\n",
3442 (unsigned long long)(lws_stats_get(context, LWSSTATS_MS_SSL_RX_DELAY) /
3443 lws_stats_get(context, LWSSTATS_C_SSL_CONNS_HAD_RX)) / 1000);
3445 lwsl_notice("LWSSTATS_MS_WRITABLE_DELAY: %8lluus\n",
3446 (unsigned long long)lws_stats_get(context, LWSSTATS_MS_WRITABLE_DELAY));
3447 lwsl_notice("LWSSTATS_MS_WORST_WRITABLE_DELAY: %8lluus\n",
3448 (unsigned long long)lws_stats_get(context, LWSSTATS_MS_WORST_WRITABLE_DELAY));
3449 if (lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB))
3450 lwsl_notice(" Avg writable delay: %8lluus\n",
3451 (unsigned long long)(lws_stats_get(context, LWSSTATS_MS_WRITABLE_DELAY) /
3452 lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB)));
3453 lwsl_notice("Simultaneous SSL restriction: %8d/%d/%d\n", context->simultaneous_ssl,
3454 context->simultaneous_ssl_restriction, context->ssl_gate_accepts);
3456 lwsl_notice("Live wsi: %8d\n", context->count_wsi_allocated);
3458 #if defined(LWS_WITH_STATS)
3459 context->updated = 1;
3465 struct lws_context_per_thread *pt = &context->pt[(int)v->lserv_wsi->tsi];
3466 struct lws_pollfd *pfd;
3468 pfd = &pt->fds[v->lserv_wsi->position_in_fds_table];
3470 lwsl_notice(" Listen port %d actual POLLIN: %d\n",
3471 v->listen_port, (int)pfd->events & LWS_POLLIN);
3477 for (n = 0; n < context->count_threads; n++) {
3478 struct lws_context_per_thread *pt = &context->pt[n];
3482 lwsl_notice("PT %d\n", n + 1);
3486 lwsl_notice(" AH in use / max: %d / %d\n",
3487 pt->ah_count_in_use,
3488 context->max_http_header_pool);
3490 wl = pt->ah_wait_list;
3493 wl = wl->u.hdr.ah_wait_list;
3496 lwsl_notice(" AH wait list count / actual: %d / %d\n",
3497 pt->ah_wait_list_length, m);
3506 lws_stats_atomic_bump(struct lws_context * context,
3507 struct lws_context_per_thread *pt, int index, uint64_t bump)
3510 context->lws_stats[index] += bump;
3511 if (index != LWSSTATS_C_SERVICE_ENTRY)
3512 context->updated = 1;
3517 lws_stats_atomic_max(struct lws_context * context,
3518 struct lws_context_per_thread *pt, int index, uint64_t val)
3521 if (val > context->lws_stats[index]) {
3522 context->lws_stats[index] = val;
3523 context->updated = 1;