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_AWAITING_CLOSE_ACK:
341 goto just_kill_connection;
343 case LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE:
344 if (wsi->trunc_len) {
345 lws_callback_on_writable(wsi);
348 lwsl_info("wsi %p completed LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
349 goto just_kill_connection;
351 if (wsi->trunc_len) {
352 lwsl_info("wsi %p entering LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
353 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
354 lws_set_timeout(wsi, PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5);
360 if (wsi->mode == LWSCM_WSCL_WAITING_CONNECT ||
361 wsi->mode == LWSCM_WSCL_ISSUE_HANDSHAKE)
362 goto just_kill_connection;
364 if (wsi->mode == LWSCM_HTTP_SERVING) {
366 wsi->vhost->protocols->callback(wsi,
367 LWS_CALLBACK_HTTP_DROP_PROTOCOL,
368 wsi->user_space, NULL, 0);
369 wsi->vhost->protocols->callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
370 wsi->user_space, NULL, 0);
371 wsi->told_user_closed = 1;
373 if (wsi->mode & LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP) {
374 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_CLIENT_HTTP,
375 wsi->user_space, NULL, 0);
376 wsi->told_user_closed = 1;
380 * are his extensions okay with him closing? Eg he might be a mux
381 * parent and just his ch1 aspect is closing?
384 if (lws_ext_cb_active(wsi,
385 LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE, NULL, 0) > 0) {
386 lwsl_ext("extension vetoed close\n");
391 * flush any tx pending from extensions, since we may send close packet
392 * if there are problems with send, just nuke the connection
397 eff_buf.token = NULL;
398 eff_buf.token_len = 0;
400 /* show every extension the new incoming data */
402 m = lws_ext_cb_active(wsi,
403 LWS_EXT_CB_FLUSH_PENDING_TX, &eff_buf, 0);
405 lwsl_ext("Extension reports fatal error\n");
406 goto just_kill_connection;
410 * at least one extension told us he has more
411 * to spill, so we will go around again after
415 /* assuming they left us something to send, send it */
417 if (eff_buf.token_len)
418 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
419 eff_buf.token_len) !=
421 lwsl_debug("close: ext spill failed\n");
422 goto just_kill_connection;
427 * signal we are closing, lws_write will
428 * add any necessary version-specific stuff. If the write fails,
429 * no worries we are closing anyway. If we didn't initiate this
430 * close, then our state has been changed to
431 * LWSS_RETURNED_CLOSE_ALREADY and we will skip this.
433 * Likewise if it's a second call to close this connection after we
434 * sent the close indication to the peer already, we are in state
435 * LWSS_AWAITING_CLOSE_ACK and will skip doing this a second time.
438 if (wsi->state_pre_close == LWSS_ESTABLISHED &&
439 (wsi->u.ws.close_in_ping_buffer_len || /* already a reason */
440 (reason != LWS_CLOSE_STATUS_NOSTATUS &&
441 (reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)))) {
442 lwsl_debug("sending close indication...\n");
444 /* if no prepared close reason, use 1000 and no aux data */
445 if (!wsi->u.ws.close_in_ping_buffer_len) {
446 wsi->u.ws.close_in_ping_buffer_len = 2;
447 wsi->u.ws.ping_payload_buf[LWS_PRE] =
448 (reason >> 8) & 0xff;
449 wsi->u.ws.ping_payload_buf[LWS_PRE + 1] =
453 #if defined (LWS_WITH_ESP8266)
454 wsi->close_is_pending_send_completion = 1;
456 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
457 wsi->u.ws.close_in_ping_buffer_len,
461 * we have sent a nice protocol level indication we
462 * now wish to close, we should not send anything more
464 wsi->state = LWSS_AWAITING_CLOSE_ACK;
467 * ...and we should wait for a reply for a bit
470 lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_ACK, 1);
471 lwsl_debug("sent close indication, awaiting ack\n");
476 lwsl_info("close: sending close packet failed, hanging up\n");
478 /* else, the send failed and we should just hang up */
481 just_kill_connection:
483 lws_remove_child_from_any_parent(wsi);
486 /* manage the vhost same protocol list entry */
488 if (wsi->same_vh_protocol_prev) { // we are on the vh list
490 // make guy who pointed to us, point to what our next was pointing to
491 *wsi->same_vh_protocol_prev = wsi->same_vh_protocol_next;
493 // if we had a next guy...
494 if (wsi->same_vh_protocol_next)
495 // have him point back to our prev
496 wsi->same_vh_protocol_next->same_vh_protocol_prev =
497 wsi->same_vh_protocol_prev;
503 * Testing with ab shows that we have to stage the socket close when
504 * the system is under stress... shutdown any further TX, change the
505 * state to one that won't emit anything more, and wait with a timeout
506 * for the POLLIN to show a zero-size rx before coming back and doing
509 if (wsi->mode != LWSCM_RAW &&
510 !(wsi->mode & LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP) &&
511 wsi->state != LWSS_SHUTDOWN &&
512 wsi->state != LWSS_CLIENT_UNCONNECTED &&
513 reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY &&
514 !wsi->socket_is_permanently_unusable) {
515 #ifdef LWS_OPENSSL_SUPPORT
516 if (lws_is_ssl(wsi) && wsi->ssl)
518 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);
519 n = SSL_shutdown(wsi->ssl);
520 if (n == 1) /* If finished the SSL shutdown, then do socket shutdown, else need to retry SSL shutdown */
521 n = shutdown(wsi->desc.sockfd, SHUT_WR);
523 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
526 int shutdown_error = SSL_get_error(wsi->ssl, n);
527 lwsl_debug("SSL_shutdown returned %d, SSL_get_error: %d\n", n, shutdown_error);
528 if (shutdown_error == SSL_ERROR_WANT_READ) {
529 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
531 } else if (shutdown_error == SSL_ERROR_WANT_WRITE) {
532 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLOUT);
534 } else { // actual error occurred, just close the connection
535 n = shutdown(wsi->desc.sockfd, SHUT_WR);
542 lwsl_info("%s: shutting down connection: %p (sock %d, state %d)\n", __func__, wsi, (int)(long)wsi->desc.sockfd, wsi->state);
543 n = shutdown(wsi->desc.sockfd, SHUT_WR);
546 lwsl_debug("closing: shutdown (state %d) ret %d\n", wsi->state, LWS_ERRNO);
548 // This causes problems with disconnection when the events are half closing connection
549 // FD_READ | FD_CLOSE (33)
550 #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP32)
551 /* libuv: no event available to guarantee completion */
552 if (!LWS_LIBUV_ENABLED(context)) {
554 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
555 wsi->state = LWSS_SHUTDOWN;
556 lws_set_timeout(wsi, PENDING_TIMEOUT_SHUTDOWN_FLUSH,
557 context->timeout_secs);
565 lwsl_info("%s: real just_kill_connection: %p (sockfd %d)\n", __func__,
566 wsi, wsi->desc.sockfd);
568 #ifdef LWS_WITH_HTTP_PROXY
570 lws_rewrite_destroy(wsi->rw);
575 * we won't be servicing or receiving anything further from this guy
576 * delete socket from the internal poll list if still present
578 lws_ssl_remove_wsi_from_buffered_list(wsi);
580 lws_remove_from_timeout_list(wsi);
582 /* checking return redundant since we anyway close */
583 if (wsi->desc.sockfd != LWS_SOCK_INVALID)
584 remove_wsi_socket_from_fds(wsi);
586 #if defined(LWS_WITH_ESP8266)
587 espconn_disconnect(wsi->desc.sockfd);
590 wsi->state = LWSS_DEAD_SOCKET;
592 lws_free_set_NULL(wsi->rxflow_buffer);
593 if (wsi->state_pre_close == LWSS_ESTABLISHED ||
594 wsi->mode == LWSCM_WS_SERVING ||
595 wsi->mode == LWSCM_WS_CLIENT) {
597 if (wsi->u.ws.rx_draining_ext) {
598 struct lws **w = &pt->rx_draining_ext_list;
600 wsi->u.ws.rx_draining_ext = 0;
601 /* remove us from context draining ext list */
604 *w = wsi->u.ws.rx_draining_ext_list;
607 w = &((*w)->u.ws.rx_draining_ext_list);
609 wsi->u.ws.rx_draining_ext_list = NULL;
612 if (wsi->u.ws.tx_draining_ext) {
613 struct lws **w = &pt->tx_draining_ext_list;
615 wsi->u.ws.tx_draining_ext = 0;
616 /* remove us from context draining ext list */
619 *w = wsi->u.ws.tx_draining_ext_list;
622 w = &((*w)->u.ws.tx_draining_ext_list);
624 wsi->u.ws.tx_draining_ext_list = NULL;
626 lws_free_set_NULL(wsi->u.ws.rx_ubuf);
628 if (wsi->trunc_alloc)
629 /* not going to be completed... nuke it */
630 lws_free_set_NULL(wsi->trunc_alloc);
632 wsi->u.ws.ping_payload_len = 0;
633 wsi->u.ws.ping_pending_flag = 0;
636 /* tell the user it's all over for this guy */
638 if (wsi->mode != LWSCM_RAW && wsi->protocol && wsi->protocol->callback &&
639 ((wsi->state_pre_close == LWSS_ESTABLISHED) ||
640 (wsi->state_pre_close == LWSS_RETURNED_CLOSE_ALREADY) ||
641 (wsi->state_pre_close == LWSS_AWAITING_CLOSE_ACK) ||
642 (wsi->state_pre_close == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) ||
643 (wsi->mode == LWSCM_WS_CLIENT && wsi->state_pre_close == LWSS_HTTP) ||
644 (wsi->mode == LWSCM_WS_SERVING && wsi->state_pre_close == LWSS_HTTP))) {
646 if (wsi->user_space) {
647 lwsl_debug("%s: doing LWS_CALLBACK_HTTP_DROP_PROTOCOL for %p prot %s\n", __func__, wsi, wsi->protocol->name);
648 wsi->protocol->callback(wsi,
649 LWS_CALLBACK_HTTP_DROP_PROTOCOL,
650 wsi->user_space, NULL, 0);
652 lwsl_debug("calling back CLOSED\n");
653 wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
654 wsi->user_space, NULL, 0);
655 } else if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED) {
656 lwsl_debug("calling back CLOSED_HTTP\n");
657 wsi->vhost->protocols->callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
658 wsi->user_space, NULL, 0 );
659 } else if ((wsi->mode == LWSCM_WSCL_WAITING_SERVER_REPLY ||
660 wsi->mode == LWSCM_WSCL_WAITING_CONNECT) &&
661 !wsi->already_did_cce) {
662 wsi->vhost->protocols[0].callback(wsi,
663 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
664 wsi->user_space, NULL, 0);
666 lwsl_debug("not calling back closed mode=%d state=%d\n",
667 wsi->mode, wsi->state_pre_close);
669 /* deallocate any active extension contexts */
671 if (lws_ext_cb_active(wsi, LWS_EXT_CB_DESTROY, NULL, 0) < 0)
672 lwsl_warn("extension destruction failed\n");
674 * inform all extensions in case they tracked this guy out of band
675 * even though not active on him specifically
677 if (lws_ext_cb_all_exts(context, wsi,
678 LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING, NULL, 0) < 0)
679 lwsl_warn("ext destroy wsi failed\n");
683 wsi->socket_is_permanently_unusable = 1;
686 if (LWS_LIBUV_ENABLED(context)) {
687 lwsl_debug("%s: lws_libuv_closehandle: wsi %p\n", __func__, wsi);
688 /* libuv has to do his own close handle processing asynchronously */
689 lws_libuv_closehandle(wsi);
695 lws_close_free_wsi_final(wsi);
699 lws_close_free_wsi_final(struct lws *wsi)
703 if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->desc.sockfd)) {
705 //lwsl_err("*** closing sockfd %d\n", wsi->desc.sockfd);
706 n = compatible_close(wsi->desc.sockfd);
708 lwsl_debug("closing: close ret %d\n", LWS_ERRNO);
711 compatible_close(wsi->desc.sockfd);
714 wsi->desc.sockfd = LWS_SOCK_INVALID;
717 /* outermost destroy notification for wsi (user_space still intact) */
718 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
719 wsi->user_space, NULL, 0);
723 for (n = 0; n < 6; n++) {
724 if (wsi->cgi->pipe_fds[n / 2][n & 1] == 0)
725 lwsl_err("ZERO FD IN CGI CLOSE");
727 if (wsi->cgi->pipe_fds[n / 2][n & 1] >= 0)
728 close(wsi->cgi->pipe_fds[n / 2][n & 1]);
738 LWS_VISIBLE LWS_EXTERN const char *
739 lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len)
741 int n = 0, sl = strlen(name);
743 while (lws_hdr_copy_fragment(wsi, buf, len,
744 WSI_TOKEN_HTTP_URI_ARGS, n) >= 0) {
746 if (!strncmp(buf, name, sl))
755 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
757 interface_to_sa(struct lws_vhost *vh, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
761 ipv6 = LWS_IPV6_ENABLED(vh);
765 return lws_interface_to_sa(ipv6, ifname, addr, addrlen);
769 #ifndef LWS_PLAT_OPTEE
772 lws_get_addresses(struct lws_vhost *vh, void *ads, char *name,
773 int name_len, char *rip, int rip_len)
776 struct addrinfo ai, *res;
777 struct sockaddr_in addr4;
782 addr4.sin_family = AF_UNSPEC;
785 if (LWS_IPV6_ENABLED(vh)) {
786 if (!lws_plat_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ads)->sin6_addr, rip, rip_len)) {
787 lwsl_err("inet_ntop: %s", strerror(LWS_ERRNO));
791 // Strip off the IPv4 to IPv6 header if one exists
792 if (strncmp(rip, "::ffff:", 7) == 0)
793 memmove(rip, rip + 7, strlen(rip) - 6);
795 getnameinfo((struct sockaddr *)ads,
796 sizeof(struct sockaddr_in6), name,
797 name_len, NULL, 0, 0);
803 struct addrinfo *result;
805 memset(&ai, 0, sizeof ai);
806 ai.ai_family = PF_UNSPEC;
807 ai.ai_socktype = SOCK_STREAM;
808 ai.ai_flags = AI_CANONNAME;
809 #if !defined(LWS_WITH_ESP32)
810 if (getnameinfo((struct sockaddr *)ads,
811 sizeof(struct sockaddr_in),
812 name, name_len, NULL, 0, 0))
818 if (getaddrinfo(name, NULL, &ai, &result))
822 while (addr4.sin_family == AF_UNSPEC && res) {
823 switch (res->ai_family) {
825 addr4.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
826 addr4.sin_family = AF_INET;
832 freeaddrinfo(result);
835 if (addr4.sin_family == AF_UNSPEC)
838 if (lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len) == NULL)
856 LWS_VISIBLE const char *
857 lws_get_peer_simple(struct lws *wsi, char *name, int namelen)
862 struct sockaddr_in6 sin6;
864 struct sockaddr_in sin4;
869 if (LWS_IPV6_ENABLED(wsi->vhost)) {
883 if (getpeername(wsi->desc.sockfd, p, &len) < 0 || len > olen) {
884 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
888 return lws_plat_inet_ntop(af, q, name, namelen);
890 #if defined(LWS_WITH_ESP8266)
891 return lws_plat_get_peer_simple(wsi, name, namelen);
900 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
901 int name_len, char *rip, int rip_len)
903 #ifndef LWS_PLAT_OPTEE
907 struct sockaddr_in6 sin6;
909 struct sockaddr_in sin4;
910 struct lws_context *context = wsi->context;
917 lws_latency_pre(context, wsi);
920 if (LWS_IPV6_ENABLED(wsi->vhost)) {
930 if (getpeername(fd, p, &len) < 0) {
931 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
935 ret = lws_get_addresses(wsi->vhost, p, name, name_len, rip, rip_len);
938 lws_latency(context, wsi, "lws_get_peer_addresses", ret, 1);
951 lws_context_user(struct lws_context *context)
953 return context->user_space;
956 LWS_VISIBLE struct lws_vhost *
957 lws_vhost_get(struct lws *wsi)
962 LWS_VISIBLE struct lws_vhost *
963 lws_get_vhost(struct lws *wsi)
968 LWS_VISIBLE const struct lws_protocols *
969 lws_protocol_get(struct lws *wsi)
971 return wsi->protocol;
974 LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
975 lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name)
979 for (n = 0; n < vh->count_protocols; n++)
980 if (!strcmp(name, vh->protocols[n].name))
981 return &vh->protocols[n];
987 lws_callback_all_protocol(struct lws_context *context,
988 const struct lws_protocols *protocol, int reason)
990 struct lws_context_per_thread *pt = &context->pt[0];
991 unsigned int n, m = context->count_threads;
995 for (n = 0; n < pt->fds_count; n++) {
996 wsi = wsi_from_fd(context, pt->fds[n].fd);
999 if (wsi->protocol == protocol)
1000 protocol->callback(wsi, reason, wsi->user_space,
1010 lws_callback_all_protocol_vhost(struct lws_vhost *vh,
1011 const struct lws_protocols *protocol, int reason)
1013 struct lws_context *context = vh->context;
1014 struct lws_context_per_thread *pt = &context->pt[0];
1015 unsigned int n, m = context->count_threads;
1019 for (n = 0; n < pt->fds_count; n++) {
1020 wsi = wsi_from_fd(context, pt->fds[n].fd);
1023 if (wsi->vhost == vh && wsi->protocol == protocol)
1024 protocol->callback(wsi, reason, wsi->user_space,
1033 LWS_VISIBLE LWS_EXTERN int
1034 lws_callback_vhost_protocols(struct lws *wsi, int reason, void *in, int len)
1038 for (n = 0; n < wsi->vhost->count_protocols; n++)
1039 if (wsi->vhost->protocols[n].callback(wsi, reason, NULL, in, len))
1045 LWS_VISIBLE LWS_EXTERN void
1046 lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops)
1048 context->fops = fops;
1051 LWS_VISIBLE LWS_EXTERN lws_filepos_t
1052 lws_vfs_tell(lws_fop_fd_t fop_fd)
1057 LWS_VISIBLE LWS_EXTERN lws_filepos_t
1058 lws_vfs_get_length(lws_fop_fd_t fop_fd)
1063 LWS_VISIBLE LWS_EXTERN uint32_t
1064 lws_vfs_get_mod_time(lws_fop_fd_t fop_fd)
1066 return fop_fd->mod_time;
1069 LWS_VISIBLE lws_fileofs_t
1070 lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
1073 lwsl_debug("%s: seeking to %ld, len %ld\n", __func__, (long)offset, (long)fop_fd->len);
1074 ofs = fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset - fop_fd->pos);
1075 lwsl_debug("%s: result %ld, fop_fd pos %ld\n", __func__, (long)ofs, (long)fop_fd->pos);
1080 LWS_VISIBLE lws_fileofs_t
1081 lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
1083 return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, fop_fd->len + fop_fd->pos + offset);
1087 const struct lws_plat_file_ops *
1088 lws_vfs_select_fops(const struct lws_plat_file_ops *fops, const char *vfs_path,
1091 const struct lws_plat_file_ops *pf;
1092 const char *p = vfs_path;
1097 /* no non-platform fops, just use that */
1103 * scan the vfs path looking for indications we are to be
1104 * handled by a specific fops
1112 /* the first one is always platform fops, so skip */
1116 while (n < ARRAY_SIZE(pf->fi) && pf->fi[n].sig) {
1117 if (p >= vfs_path + pf->fi[n].len)
1118 if (!strncmp(p - (pf->fi[n].len - 1),
1120 pf->fi[n].len - 1)) {
1135 LWS_VISIBLE LWS_EXTERN lws_fop_fd_t LWS_WARN_UNUSED_RESULT
1136 lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
1137 lws_fop_flags_t *flags)
1139 const char *vpath = "";
1140 const struct lws_plat_file_ops *selected = lws_vfs_select_fops(
1141 fops, vfs_path, &vpath);
1143 return selected->LWS_FOP_OPEN(fops, vfs_path, vpath, flags);
1148 * lws_now_secs() - seconds since 1970-1-1
1151 LWS_VISIBLE LWS_EXTERN unsigned long
1156 gettimeofday(&tv, NULL);
1165 lws_get_socket_fd(struct lws *wsi)
1167 return wsi->desc.sockfd;
1174 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
1175 int ret, int completed)
1177 unsigned long long u;
1180 u = time_in_microseconds();
1183 wsi->latency_start = u;
1184 if (!wsi->action_start)
1185 wsi->action_start = u;
1189 if (wsi->action_start == wsi->latency_start)
1191 "Completion first try lat %lluus: %p: ret %d: %s\n",
1192 u - wsi->latency_start,
1193 (void *)wsi, ret, action);
1196 "Completion %lluus: lat %lluus: %p: ret %d: %s\n",
1197 u - wsi->action_start,
1198 u - wsi->latency_start,
1199 (void *)wsi, ret, action);
1200 wsi->action_start = 0;
1202 sprintf(buf, "lat %lluus: %p: ret %d: %s\n",
1203 u - wsi->latency_start, (void *)wsi, ret, action);
1205 if (u - wsi->latency_start > context->worst_latency) {
1206 context->worst_latency = u - wsi->latency_start;
1207 strcpy(context->worst_latency_info, buf);
1209 lwsl_latency("%s", buf);
1214 lws_rx_flow_control(struct lws *wsi, int enable)
1216 if (enable == (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
1219 lwsl_info("%s: (0x%p, %d)\n", __func__, wsi, enable);
1220 wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
1226 lws_rx_flow_allow_all_protocol(const struct lws_context *context,
1227 const struct lws_protocols *protocol)
1229 const struct lws_context_per_thread *pt = &context->pt[0];
1231 unsigned int n, m = context->count_threads;
1234 for (n = 0; n < pt->fds_count; n++) {
1235 wsi = wsi_from_fd(context, pt->fds[n].fd);
1238 if (wsi->protocol == protocol)
1239 lws_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1245 LWS_VISIBLE extern const char *
1246 lws_canonical_hostname(struct lws_context *context)
1248 return (const char *)context->canonical_hostname;
1251 int user_callback_handle_rxflow(lws_callback_function callback_function,
1253 enum lws_callback_reasons reason, void *user,
1254 void *in, size_t len)
1258 n = callback_function(wsi, reason, user, in, len);
1260 n = _lws_rx_flow_control(wsi);
1265 #if defined(LWS_WITH_ESP8266)
1267 #define strchr ets_strchr
1271 lws_set_proxy(struct lws_vhost *vhost, const char *proxy)
1273 #if !defined(LWS_WITH_ESP8266)
1275 char authstring[96];
1280 /* we have to deal with a possible redundant leading http:// */
1281 if (!strncmp(proxy, "http://", 7))
1284 p = strchr(proxy, '@');
1285 if (p) { /* auth is around */
1287 if ((unsigned int)(p - proxy) > sizeof(authstring) - 1)
1290 strncpy(authstring, proxy, p - proxy);
1291 // null termination not needed on input
1292 if (lws_b64_encode_string(authstring, (p - proxy),
1293 vhost->proxy_basic_auth_token,
1294 sizeof vhost->proxy_basic_auth_token) < 0)
1297 lwsl_info(" Proxy auth in use\n");
1301 vhost->proxy_basic_auth_token[0] = '\0';
1303 strncpy(vhost->http_proxy_address, proxy,
1304 sizeof(vhost->http_proxy_address) - 1);
1305 vhost->http_proxy_address[
1306 sizeof(vhost->http_proxy_address) - 1] = '\0';
1308 p = strchr(vhost->http_proxy_address, ':');
1309 if (!p && !vhost->http_proxy_port) {
1310 lwsl_err("http_proxy needs to be ads:port\n");
1316 vhost->http_proxy_port = atoi(p + 1);
1320 lwsl_info(" Proxy %s:%u\n", vhost->http_proxy_address,
1321 vhost->http_proxy_port);
1326 lwsl_err("proxy auth too long\n");
1331 #if defined(LWS_WITH_SOCKS5)
1333 lws_set_socks(struct lws_vhost *vhost, const char *socks)
1335 #if !defined(LWS_WITH_ESP8266)
1336 char *p_at, *p_colon;
1343 vhost->socks_user[0] = '\0';
1344 vhost->socks_password[0] = '\0';
1346 p_at = strchr(socks, '@');
1347 if (p_at) { /* auth is around */
1348 if ((unsigned int)(p_at - socks) > (sizeof(user)
1349 + sizeof(password) - 2)) {
1350 lwsl_err("Socks auth too long\n");
1354 p_colon = strchr(socks, ':');
1356 if ((unsigned int)(p_colon - socks) > (sizeof(user)
1358 lwsl_err("Socks user too long\n");
1361 if ((unsigned int)(p_at - p_colon) > (sizeof(password)
1363 lwsl_err("Socks password too long\n");
1367 strncpy(vhost->socks_user, socks, p_colon - socks);
1368 strncpy(vhost->socks_password, p_colon + 1,
1369 p_at - (p_colon + 1));
1371 lwsl_info(" Socks auth, user: %s, password: %s\n",
1372 vhost->socks_user, vhost->socks_password );
1377 strncpy(vhost->socks_proxy_address, socks,
1378 sizeof(vhost->socks_proxy_address) - 1);
1379 vhost->socks_proxy_address[sizeof(vhost->socks_proxy_address) - 1]
1382 p_colon = strchr(vhost->socks_proxy_address, ':');
1383 if (!p_colon && !vhost->socks_proxy_port) {
1384 lwsl_err("socks_proxy needs to be address:port\n");
1389 vhost->socks_proxy_port = atoi(p_colon + 1);
1393 lwsl_info(" Socks %s:%u\n", vhost->socks_proxy_address,
1394 vhost->socks_proxy_port);
1404 LWS_VISIBLE const struct lws_protocols *
1405 lws_get_protocol(struct lws *wsi)
1407 return wsi->protocol;
1411 lws_is_final_fragment(struct lws *wsi)
1413 lwsl_info("%s: final %d, rx pk length %ld, draining %ld\n", __func__,
1414 wsi->u.ws.final, (long)wsi->u.ws.rx_packet_length,
1415 (long)wsi->u.ws.rx_draining_ext);
1416 return wsi->u.ws.final && !wsi->u.ws.rx_packet_length && !wsi->u.ws.rx_draining_ext;
1419 LWS_VISIBLE unsigned char
1420 lws_get_reserved_bits(struct lws *wsi)
1422 return wsi->u.ws.rsv;
1426 lws_ensure_user_space(struct lws *wsi)
1428 lwsl_info("%s: %p protocol %p\n", __func__, wsi, wsi->protocol);
1432 /* allocate the per-connection user memory (if any) */
1434 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
1435 wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size);
1436 if (wsi->user_space == NULL) {
1437 lwsl_err("Out of memory for conn user space\n");
1441 lwsl_info("%s: %p protocol pss %lu, user_space=%p\n",
1442 __func__, wsi, (long)wsi->protocol->per_session_data_size,
1448 lwsl_timestamp(int level, char *p, int len)
1450 #ifndef LWS_PLAT_OPTEE
1451 time_t o_now = time(NULL);
1452 unsigned long long now;
1453 struct tm *ptm = NULL;
1461 ptm = localtime(&o_now);
1463 if (localtime_r(&o_now, &tm))
1468 for (n = 0; n < LLL_COUNT; n++) {
1469 if (level != (1 << n))
1471 now = time_in_microseconds() / 100;
1473 n = lws_snprintf(p, len,
1474 "[%04d/%02d/%02d %02d:%02d:%02d:%04d] %s: ",
1475 ptm->tm_year + 1900,
1481 (int)(now % 10000), log_level_names[n]);
1483 n = lws_snprintf(p, len, "[%llu:%04d] %s: ",
1484 (unsigned long long) now / 10000,
1485 (int)(now % 10000), log_level_names[n]);
1492 #ifndef LWS_PLAT_OPTEE
1493 LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line)
1495 #if !defined(LWS_WITH_ESP8266)
1498 lwsl_timestamp(level, buf, sizeof(buf));
1499 fprintf(stderr, "%s%s", buf, line);
1504 LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl)
1506 #if defined(LWS_WITH_ESP8266)
1513 if (!(log_level & filter))
1516 n = vsnprintf(buf, sizeof(buf) - 1, format, vl);
1518 #if defined(LWS_WITH_ESP8266)
1519 buf[sizeof(buf) - 1] = '\0';
1521 /* vnsprintf returns what it would have written, even if truncated */
1522 if (n > sizeof(buf) - 1)
1523 n = sizeof(buf) - 1;
1528 lwsl_emit(filter, buf);
1531 LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
1535 va_start(ap, format);
1536 _lws_logv(filter, format, ap);
1540 LWS_VISIBLE void lws_set_log_level(int level,
1541 void (*func)(int level, const char *line))
1548 LWS_VISIBLE int lwsl_visible(int level)
1550 return log_level & level;
1554 lws_is_ssl(struct lws *wsi)
1556 #ifdef LWS_OPENSSL_SUPPORT
1557 return wsi->use_ssl;
1564 #ifdef LWS_OPENSSL_SUPPORT
1566 lws_get_ssl(struct lws *wsi)
1573 lws_partial_buffered(struct lws *wsi)
1575 return !!wsi->trunc_len;
1578 void lws_set_protocol_write_pending(struct lws *wsi,
1579 enum lws_pending_protocol_send pend)
1581 lwsl_info("setting pps %d\n", pend);
1584 lwsl_err("pps overwrite\n");
1586 lws_rx_flow_control(wsi, 0);
1587 lws_callback_on_writable(wsi);
1591 lws_get_peer_write_allowance(struct lws *wsi)
1593 #ifdef LWS_USE_HTTP2
1594 /* only if we are using HTTP2 on this connection */
1595 if (wsi->mode != LWSCM_HTTP2_SERVING)
1597 /* user is only interested in how much he can send, or that he can't */
1598 if (wsi->u.http2.tx_credit <= 0)
1601 return wsi->u.http2.tx_credit;
1609 lws_union_transition(struct lws *wsi, enum connection_mode mode)
1611 lwsl_debug("%s: %p: mode %d\n", __func__, wsi, mode);
1612 memset(&wsi->u, 0, sizeof(wsi->u));
1616 LWS_VISIBLE struct lws_plat_file_ops *
1617 lws_get_fops(struct lws_context *context)
1619 return (struct lws_plat_file_ops *)context->fops;
1622 LWS_VISIBLE LWS_EXTERN struct lws_context *
1623 lws_get_context(const struct lws *wsi)
1625 return wsi->context;
1628 LWS_VISIBLE LWS_EXTERN int
1629 lws_get_count_threads(struct lws_context *context)
1631 return context->count_threads;
1634 LWS_VISIBLE LWS_EXTERN void *
1635 lws_wsi_user(struct lws *wsi)
1637 return wsi->user_space;
1640 LWS_VISIBLE LWS_EXTERN void
1641 lws_set_wsi_user(struct lws *wsi, void *data)
1643 if (wsi->user_space_externally_allocated)
1644 wsi->user_space = data;
1646 lwsl_err("%s: Cannot set internally-allocated user_space\n",
1650 LWS_VISIBLE LWS_EXTERN struct lws *
1651 lws_get_parent(const struct lws *wsi)
1656 LWS_VISIBLE LWS_EXTERN struct lws *
1657 lws_get_child(const struct lws *wsi)
1659 return wsi->child_list;
1662 LWS_VISIBLE LWS_EXTERN void
1663 lws_close_reason(struct lws *wsi, enum lws_close_status status,
1664 unsigned char *buf, size_t len)
1666 unsigned char *p, *start;
1667 int budget = sizeof(wsi->u.ws.ping_payload_buf) - LWS_PRE;
1669 assert(wsi->mode == LWSCM_WS_SERVING || wsi->mode == LWSCM_WS_CLIENT);
1671 start = p = &wsi->u.ws.ping_payload_buf[LWS_PRE];
1673 *p++ = (((int)status) >> 8) & 0xff;
1674 *p++ = ((int)status) & 0xff;
1677 while (len-- && p < start + budget)
1680 wsi->u.ws.close_in_ping_buffer_len = p - start;
1684 _lws_rx_flow_control(struct lws *wsi)
1686 struct lws *wsic = wsi->child_list;
1688 /* if he has children, do those if they were changed */
1690 if (wsic->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)
1691 _lws_rx_flow_control(wsic);
1693 wsic = wsic->sibling_list;
1696 /* there is no pending change */
1697 if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) {
1698 // lwsl_debug("%s: no pending change\n", __func__);
1702 /* stuff is still buffered, not ready to really accept new input */
1703 if (wsi->rxflow_buffer) {
1704 /* get ourselves called back to deal with stashed buffer */
1705 lws_callback_on_writable(wsi);
1709 /* pending is cleared, we can change rxflow state */
1711 wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1713 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1714 wsi->rxflow_change_to & LWS_RXFLOW_ALLOW);
1716 /* adjust the pollfd for this wsi */
1718 if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) {
1719 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
1720 lwsl_info("%s: fail\n", __func__);
1724 if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
1731 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len)
1733 static const unsigned char e0f4[] = {
1734 0xa0 | ((2 - 1) << 2) | 1, /* e0 */
1735 0x80 | ((4 - 1) << 2) | 1, /* e1 */
1736 0x80 | ((4 - 1) << 2) | 1, /* e2 */
1737 0x80 | ((4 - 1) << 2) | 1, /* e3 */
1738 0x80 | ((4 - 1) << 2) | 1, /* e4 */
1739 0x80 | ((4 - 1) << 2) | 1, /* e5 */
1740 0x80 | ((4 - 1) << 2) | 1, /* e6 */
1741 0x80 | ((4 - 1) << 2) | 1, /* e7 */
1742 0x80 | ((4 - 1) << 2) | 1, /* e8 */
1743 0x80 | ((4 - 1) << 2) | 1, /* e9 */
1744 0x80 | ((4 - 1) << 2) | 1, /* ea */
1745 0x80 | ((4 - 1) << 2) | 1, /* eb */
1746 0x80 | ((4 - 1) << 2) | 1, /* ec */
1747 0x80 | ((2 - 1) << 2) | 1, /* ed */
1748 0x80 | ((4 - 1) << 2) | 1, /* ee */
1749 0x80 | ((4 - 1) << 2) | 1, /* ef */
1750 0x90 | ((3 - 1) << 2) | 2, /* f0 */
1751 0x80 | ((4 - 1) << 2) | 2, /* f1 */
1752 0x80 | ((4 - 1) << 2) | 2, /* f2 */
1753 0x80 | ((4 - 1) << 2) | 2, /* f3 */
1754 0x80 | ((1 - 1) << 2) | 2, /* f4 */
1757 0x80 | ((4 - 1) << 2) | 0, /* s2 */
1758 0x80 | ((4 - 1) << 2) | 1, /* s3 */
1760 unsigned char s = *state;
1763 unsigned char c = *buf++;
1767 if (c < 0xc2 || c > 0xf4)
1770 s = 0x80 | ((4 - 1) << 2);
1775 if (c < (s & 0xf0) ||
1776 c >= (s & 0xf0) + 0x10 + ((s << 2) & 0x30))
1778 s = e0f4[21 + (s & 3)];
1787 LWS_VISIBLE LWS_EXTERN int
1788 lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
1792 static const char *slash = "/";
1794 /* cut up the location into address, port and path */
1796 while (*p && (*p != ':' || p[1] != '/' || p[2] != '/'))
1807 if (!strcmp(*prot, "http") || !strcmp(*prot, "ws"))
1809 else if (!strcmp(*prot, "https") || !strcmp(*prot, "wss"))
1815 while (*p && *p != ']')
1822 while (*p && *p != ':' && *p != '/')
1828 while (*p && *p != '/')
1841 #ifdef LWS_NO_EXTENSIONS
1843 /* we need to provide dummy callbacks for internal exts
1844 * so user code runs when faced with a lib compiled with
1845 * extensions disabled.
1849 lws_extension_callback_pm_deflate(struct lws_context *context,
1850 const struct lws_extension *ext,
1852 enum lws_extension_callback_reasons reason,
1853 void *user, void *in, size_t len)
1868 lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
1872 #ifdef LWS_USE_UNIX_SOCK
1873 struct sockaddr_un serv_unix;
1876 struct sockaddr_in6 serv_addr6;
1878 struct sockaddr_in serv_addr4;
1879 #ifndef LWS_PLAT_OPTEE
1880 socklen_t len = sizeof(struct sockaddr_storage);
1883 struct sockaddr_storage sin;
1886 #ifdef LWS_USE_UNIX_SOCK
1887 if (LWS_UNIX_SOCK_ENABLED(vhost)) {
1888 v = (struct sockaddr *)&serv_unix;
1889 n = sizeof(struct sockaddr_un);
1890 bzero((char *) &serv_unix, sizeof(serv_unix));
1891 serv_unix.sun_family = AF_UNIX;
1892 if (sizeof(serv_unix.sun_path) <= strlen(iface)) {
1893 lwsl_err("\"%s\" too long for UNIX domain socket\n",
1897 strcpy(serv_unix.sun_path, iface);
1898 if (serv_unix.sun_path[0] == '@')
1899 serv_unix.sun_path[0] = '\0';
1903 #if defined(LWS_USE_IPV6) && !defined(LWS_WITH_ESP32)
1904 if (LWS_IPV6_ENABLED(vhost)) {
1905 v = (struct sockaddr *)&serv_addr6;
1906 n = sizeof(struct sockaddr_in6);
1907 bzero((char *) &serv_addr6, sizeof(serv_addr6));
1909 if (interface_to_sa(vhost, iface,
1910 (struct sockaddr_in *)v, n) < 0) {
1911 lwsl_err("Unable to find interface %s\n", iface);
1914 serv_addr6.sin6_scope_id = lws_get_addr_scope(iface);
1917 serv_addr6.sin6_family = AF_INET6;
1918 serv_addr6.sin6_port = htons(port);
1922 v = (struct sockaddr *)&serv_addr4;
1923 n = sizeof(serv_addr4);
1924 bzero((char *) &serv_addr4, sizeof(serv_addr4));
1925 serv_addr4.sin_addr.s_addr = INADDR_ANY;
1926 serv_addr4.sin_family = AF_INET;
1927 #if !defined(LWS_WITH_ESP32)
1930 interface_to_sa(vhost, iface,
1931 (struct sockaddr_in *)v, n) < 0) {
1932 lwsl_err("Unable to find interface %s\n", iface);
1936 serv_addr4.sin_port = htons(port);
1939 n = bind(sockfd, v, n);
1940 #ifdef LWS_USE_UNIX_SOCK
1941 if (n < 0 && LWS_UNIX_SOCK_ENABLED(vhost)) {
1942 lwsl_err("ERROR on binding fd %d to \"%s\" (%d %d)\n",
1943 sockfd, iface, n, LWS_ERRNO);
1948 lwsl_err("ERROR on binding fd %d to port %d (%d %d)\n",
1949 sockfd, port, n, LWS_ERRNO);
1953 #ifndef LWS_PLAT_OPTEE
1954 if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
1955 lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
1958 #if defined(LWS_USE_IPV6)
1959 port = (sin.ss_family == AF_INET6) ?
1960 ntohs(((struct sockaddr_in6 *) &sin)->sin6_port) :
1961 ntohs(((struct sockaddr_in *) &sin)->sin_port);
1963 port = ntohs(((struct sockaddr_in *) &sin)->sin_port);
1970 #if defined(LWS_USE_IPV6)
1971 LWS_EXTERN unsigned long
1972 lws_get_addr_scope(const char *ipaddr)
1974 unsigned long scope = 0;
1977 struct ifaddrs *addrs, *addr;
1978 char ip[NI_MAXHOST];
1982 for (addr = addrs; addr; addr = addr->ifa_next) {
1983 if (!addr->ifa_addr ||
1984 addr->ifa_addr->sa_family != AF_INET6)
1987 getnameinfo(addr->ifa_addr,
1988 sizeof(struct sockaddr_in6),
1990 NULL, 0, NI_NUMERICHOST);
1994 if (ip[i++] == '%') {
1999 if (!strcmp(ip, ipaddr)) {
2000 scope = if_nametoindex(addr->ifa_name);
2006 PIP_ADAPTER_ADDRESSES adapter, addrs = NULL;
2007 PIP_ADAPTER_UNICAST_ADDRESS addr;
2010 struct sockaddr_in6 *sockaddr;
2011 char ip[NI_MAXHOST];
2015 for (i = 0; i < 5; i++)
2017 ret = GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX,
2018 NULL, addrs, &size);
2019 if ((ret == NO_ERROR) || (ret == ERROR_NO_DATA)) {
2021 } else if (ret == ERROR_BUFFER_OVERFLOW)
2025 addrs = (IP_ADAPTER_ADDRESSES *) malloc(size);
2033 lwsl_err("Failed to get IPv6 address table (%d)", ret);
2038 if ((ret == NO_ERROR) && (addrs))
2041 while ((adapter) && (!found))
2043 addr = adapter->FirstUnicastAddress;
2044 while ((addr) && (!found))
2046 if (addr->Address.lpSockaddr->sa_family == AF_INET6)
2048 sockaddr = (struct sockaddr_in6 *) (addr->Address.lpSockaddr);
2050 lws_plat_inet_ntop(sockaddr->sin6_family, &sockaddr->sin6_addr,
2053 if (!strcmp(ip, ipaddr)) {
2054 scope = sockaddr->sin6_scope_id;
2061 adapter = adapter->Next;
2073 lws_restart_ws_ping_pong_timer(struct lws *wsi)
2075 if (!wsi->context->ws_ping_pong_interval)
2077 if (wsi->state != LWSS_ESTABLISHED)
2080 wsi->u.ws.time_next_ping_check = (time_t)lws_now_secs() +
2081 wsi->context->ws_ping_pong_interval;
2084 static const char *hex = "0123456789ABCDEF";
2086 LWS_VISIBLE LWS_EXTERN const char *
2087 lws_sql_purify(char *escaped, const char *string, int len)
2089 const char *p = string;
2092 while (*p && len-- > 2) {
2106 LWS_VISIBLE LWS_EXTERN const char *
2107 lws_json_purify(char *escaped, const char *string, int len)
2109 const char *p = string;
2117 while (*p && len-- > 6) {
2118 if (*p == '\"' || *p == '\\' || *p < 0x20) {
2123 *q++ = hex[((*p) >> 4) & 15];
2124 *q++ = hex[(*p) & 15];
2135 LWS_VISIBLE LWS_EXTERN const char *
2136 lws_urlencode(char *escaped, const char *string, int len)
2138 const char *p = string;
2141 while (*p && len-- > 3) {
2147 if ((*p >= '0' && *p <= '9') ||
2148 (*p >= 'A' && *p <= 'Z') ||
2149 (*p >= 'a' && *p <= 'z')) {
2154 *q++ = hex[(*p >> 4) & 0xf];
2155 *q++ = hex[*p & 0xf];
2165 LWS_VISIBLE LWS_EXTERN int
2166 lws_urldecode(char *string, const char *escaped, int len)
2171 while (*escaped && len) {
2174 if (*escaped == '%') {
2179 if (*escaped == '+') {
2185 *string++ = *escaped++;
2189 n = char_to_hex(*escaped);
2198 n = char_to_hex(*escaped);
2202 *string++ = sum | n;
2214 LWS_VISIBLE LWS_EXTERN int
2215 lws_finalize_startup(struct lws_context *context)
2217 struct lws_context_creation_info info;
2219 info.uid = context->uid;
2220 info.gid = context->gid;
2222 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
2223 memcpy(info.caps, context->caps, sizeof(info.caps));
2224 info.count_caps = context->count_caps;
2227 if (lws_check_opt(context->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
2228 lws_plat_drop_app_privileges(&info);
2234 lws_snprintf(char *str, size_t size, const char *format, ...)
2242 va_start(ap, format);
2243 n = vsnprintf(str, size, format, ap);
2253 LWS_VISIBLE LWS_EXTERN int
2254 lws_is_cgi(struct lws *wsi) {
2265 urlencode(const char *in, int inlen, char *out, int outlen)
2267 char *start = out, *end = out + outlen;
2269 while (inlen-- && out < end - 4) {
2270 if ((*in >= 'A' && *in <= 'Z') ||
2271 (*in >= 'a' && *in <= 'z') ||
2272 (*in >= '0' && *in <= '9') ||
2286 *out++ = hex[(*in) >> 4];
2287 *out++ = hex[(*in++) & 15];
2298 lws_create_basic_wsi(struct lws_context *context, int tsi)
2300 struct lws *new_wsi;
2302 if ((unsigned int)context->pt[tsi].fds_count ==
2303 context->fd_limit_per_thread - 1) {
2304 lwsl_err("no space for new conn\n");
2308 new_wsi = lws_zalloc(sizeof(struct lws));
2309 if (new_wsi == NULL) {
2310 lwsl_err("Out of memory for new connection\n");
2315 new_wsi->context = context;
2316 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
2317 new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
2319 /* initialize the instance struct */
2321 new_wsi->state = LWSS_CGI;
2322 new_wsi->mode = LWSCM_CGI;
2323 new_wsi->hdr_parsing_completed = 0;
2324 new_wsi->position_in_fds_table = -1;
2327 * these can only be set once the protocol is known
2328 * we set an unestablished connection's protocol pointer
2329 * to the start of the defauly vhost supported list, so it can look
2330 * for matching ones during the handshake
2332 new_wsi->protocol = context->vhost_list->protocols;
2333 new_wsi->user_space = NULL;
2334 new_wsi->ietf_spec_revision = 0;
2335 new_wsi->desc.sockfd = LWS_SOCK_INVALID;
2336 context->count_wsi_allocated++;
2341 LWS_VISIBLE LWS_EXTERN int
2342 lws_cgi(struct lws *wsi, const char * const *exec_array, int script_uri_path_len,
2343 int timeout_secs, const struct lws_protocol_vhost_options *mp_cgienv)
2345 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
2346 char *env_array[30], cgi_path[400], e[1024], *p = e,
2347 *end = p + sizeof(e) - 1, tok[256], *t;
2348 struct lws_cgi *cgi;
2349 int n, m, i, uritok = -1;
2352 * give the master wsi a cgi struct
2355 wsi->cgi = lws_zalloc(sizeof(*wsi->cgi));
2357 lwsl_err("%s: OOM\n", __func__);
2361 wsi->cgi->response_code = HTTP_STATUS_OK;
2364 cgi->wsi = wsi; /* set cgi's owning wsi */
2366 /* create pipes for [stdin|stdout] and [stderr] */
2368 for (n = 0; n < 3; n++)
2369 if (pipe(cgi->pipe_fds[n]) == -1)
2372 /* create cgi wsis for each stdin/out/err fd */
2374 for (n = 0; n < 3; n++) {
2375 cgi->stdwsi[n] = lws_create_basic_wsi(wsi->context, wsi->tsi);
2376 if (!cgi->stdwsi[n])
2378 cgi->stdwsi[n]->cgi_channel = n;
2379 cgi->stdwsi[n]->vhost = wsi->vhost;
2381 // lwsl_err("%s: cgi %p: pipe fd %d -> fd %d / %d\n", __func__, wsi, n,
2382 // cgi->pipe_fds[n][!!(n == 0)], cgi->pipe_fds[n][!(n == 0)]);
2384 /* read side is 0, stdin we want the write side, others read */
2385 cgi->stdwsi[n]->desc.sockfd = cgi->pipe_fds[n][!!(n == 0)];
2386 if (fcntl(cgi->pipe_fds[n][!!(n == 0)], F_SETFL, O_NONBLOCK) < 0) {
2387 lwsl_err("%s: setting NONBLOCK failed\n", __func__);
2392 for (n = 0; n < 3; n++) {
2393 lws_libuv_accept(cgi->stdwsi[n], cgi->stdwsi[n]->desc);
2394 if (insert_wsi_socket_into_fds(wsi->context, cgi->stdwsi[n]))
2396 cgi->stdwsi[n]->parent = wsi;
2397 cgi->stdwsi[n]->sibling_list = wsi->child_list;
2398 wsi->child_list = cgi->stdwsi[n];
2401 lws_change_pollfd(cgi->stdwsi[LWS_STDIN], LWS_POLLIN, LWS_POLLOUT);
2402 lws_change_pollfd(cgi->stdwsi[LWS_STDOUT], LWS_POLLOUT, LWS_POLLIN);
2403 lws_change_pollfd(cgi->stdwsi[LWS_STDERR], LWS_POLLOUT, LWS_POLLIN);
2405 lwsl_debug("%s: fds in %d, out %d, err %d\n", __func__,
2406 cgi->stdwsi[LWS_STDIN]->desc.sockfd,
2407 cgi->stdwsi[LWS_STDOUT]->desc.sockfd,
2408 cgi->stdwsi[LWS_STDERR]->desc.sockfd);
2410 lws_set_timeout(wsi, PENDING_TIMEOUT_CGI, timeout_secs);
2412 /* the cgi stdout is always sending us http1.x header data first */
2413 wsi->hdr_state = LCHS_HEADER;
2415 /* add us to the pt list of active cgis */
2416 lwsl_debug("%s: adding cgi %p to list\n", __func__, wsi->cgi);
2417 cgi->cgi_list = pt->cgi_list;
2420 /* prepare his CGI env */
2424 if (lws_is_ssl(wsi))
2425 env_array[n++] = "HTTPS=ON";
2426 if (wsi->u.hdr.ah) {
2427 static const unsigned char meths[] = {
2430 WSI_TOKEN_OPTIONS_URI,
2432 WSI_TOKEN_PATCH_URI,
2433 WSI_TOKEN_DELETE_URI,
2435 static const char * const meth_names[] = {
2436 "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE",
2439 for (m = 0; m < ARRAY_SIZE(meths); m++)
2440 if (lws_hdr_total_length(wsi, meths[m]) >=
2441 script_uri_path_len) {
2449 lws_snprintf(cgi_path, sizeof(cgi_path) - 1, "REQUEST_URI=%s",
2450 lws_hdr_simple_ptr(wsi, uritok));
2451 cgi_path[sizeof(cgi_path) - 1] = '\0';
2452 env_array[n++] = cgi_path;
2455 p += lws_snprintf(p, end - p, "REQUEST_METHOD=%s",
2460 p += lws_snprintf(p, end - p, "QUERY_STRING=");
2461 /* dump the individual URI Arg parameters */
2464 i = lws_hdr_copy_fragment(wsi, tok, sizeof(tok),
2465 WSI_TOKEN_HTTP_URI_ARGS, m);
2469 while (*t && *t != '=' && p < end - 4)
2473 i = urlencode(t, i- (t - tok), p, end - p);
2485 p += lws_snprintf(p, end - p, "PATH_INFO=%s",
2486 lws_hdr_simple_ptr(wsi, uritok) +
2487 script_uri_path_len);
2490 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_REFERER)) {
2492 p += lws_snprintf(p, end - p, "HTTP_REFERER=%s",
2493 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_REFERER));
2496 if (lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
2498 p += lws_snprintf(p, end - p, "HTTP_HOST=%s",
2499 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
2502 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) {
2504 p += lws_snprintf(p, end - p, "HTTP_COOKIE=%s",
2505 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COOKIE));
2508 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT)) {
2510 p += lws_snprintf(p, end - p, "USER_AGENT=%s",
2511 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_USER_AGENT));
2514 if (uritok == WSI_TOKEN_POST_URI) {
2515 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE)) {
2517 p += lws_snprintf(p, end - p, "CONTENT_TYPE=%s",
2518 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE));
2521 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
2523 p += lws_snprintf(p, end - p, "CONTENT_LENGTH=%s",
2524 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH));
2529 p += lws_snprintf(p, end - p, "SCRIPT_PATH=%s", exec_array[0]) + 1;
2533 p += lws_snprintf(p, end - p, "%s=%s", mp_cgienv->name,
2535 lwsl_debug(" Applying mount-specific cgi env '%s'\n",
2538 mp_cgienv = mp_cgienv->next;
2541 env_array[n++] = "SERVER_SOFTWARE=libwebsockets";
2542 env_array[n++] = "PATH=/bin:/usr/bin:/usr/local/bin:/var/www/cgi-bin";
2543 env_array[n] = NULL;
2546 for (m = 0; m < n; m++)
2547 lwsl_err(" %s\n", env_array[m]);
2551 * Actually having made the env, as a cgi we don't need the ah
2554 if (lws_header_table_is_in_detachable_state(wsi))
2555 lws_header_table_detach(wsi, 0);
2557 /* we are ready with the redirection pipes... run the thing */
2558 #if !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
2564 lwsl_err("fork failed, errno %d", errno);
2568 #if defined(__linux__)
2569 prctl(PR_SET_PDEATHSIG, SIGTERM);
2571 setpgrp(); /* stops on-daemonized main processess getting SIGINT from TTY */
2574 /* we are the parent process */
2575 wsi->context->count_cgi_spawned++;
2576 lwsl_debug("%s: cgi %p spawned PID %d\n", __func__, cgi, cgi->pid);
2580 /* somewhere we can at least read things and enter it */
2582 lwsl_notice("%s: Failed to chdir\n", __func__);
2584 /* We are the forked process, redirect and kill inherited things.
2586 * Because of vfork(), we cannot do anything that changes pages in
2587 * the parent environment. Stuff that changes kernel state for the
2588 * process is OK. Stuff that happens after the execvpe() is OK.
2591 for (n = 0; n < 3; n++) {
2592 if (dup2(cgi->pipe_fds[n][!(n == 0)], n) < 0) {
2593 lwsl_err("%s: stdin dup2 failed\n", __func__);
2596 close(cgi->pipe_fds[n][!(n == 0)]);
2599 #if !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
2600 for (m = 0; m < n; m++) {
2601 p = strchr(env_array[m], '=');
2603 setenv(env_array[m], p, 1);
2605 execvp(exec_array[0], (char * const *)&exec_array[0]);
2607 execvpe(exec_array[0], (char * const *)&exec_array[0], &env_array[0]);
2613 /* drop us from the pt cgi list */
2614 pt->cgi_list = cgi->cgi_list;
2617 remove_wsi_socket_from_fds(wsi->cgi->stdwsi[n]);
2619 for (n = 0; n < 3; n++)
2620 if (wsi->cgi->stdwsi[n])
2621 lws_free_wsi(cgi->stdwsi[n]);
2624 for (n = 0; n < 3; n++) {
2625 if (cgi->pipe_fds[n][0])
2626 close(cgi->pipe_fds[n][0]);
2627 if (cgi->pipe_fds[n][1])
2628 close(cgi->pipe_fds[n][1]);
2631 lws_free_set_NULL(wsi->cgi);
2633 lwsl_err("%s: failed\n", __func__);
2638 /* we have to parse out these headers in the CGI output */
2640 static const char * const significant_hdr[SIGNIFICANT_HDR_COUNT] = {
2644 "transfer-encoding: chunked",
2647 LWS_VISIBLE LWS_EXTERN int
2648 lws_cgi_write_split_stdout_headers(struct lws *wsi)
2651 unsigned char buf[LWS_PRE + 1024], *start = &buf[LWS_PRE], *p = start,
2652 *end = &buf[sizeof(buf) - 1 - LWS_PRE];
2658 while (wsi->hdr_state != LHCS_PAYLOAD) {
2659 /* we have to separate header / finalize and
2660 * payload chunks, since they need to be
2661 * handled separately
2664 switch (wsi->hdr_state) {
2667 lwsl_info("LHCS_RESPONSE: issuing response %d\n",
2668 wsi->cgi->response_code);
2669 if (lws_add_http_header_status(wsi, wsi->cgi->response_code, &p, end))
2671 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
2672 (unsigned char *)"close", 5, &p, end))
2674 n = lws_write(wsi, start, p - start,
2675 LWS_WRITE_HTTP_HEADERS);
2677 /* finalize cached headers before dumping them */
2678 if (lws_finalize_http_header(wsi,
2679 (unsigned char **)&wsi->cgi->headers_pos,
2680 (unsigned char *)wsi->cgi->headers_end)) {
2682 lwsl_notice("finalize failed\n");
2686 wsi->hdr_state = LHCS_DUMP_HEADERS;
2687 wsi->reason_bf |= 8;
2688 lws_callback_on_writable(wsi);
2689 /* back to the loop for writeability again */
2692 case LHCS_DUMP_HEADERS:
2694 n = wsi->cgi->headers_pos - wsi->cgi->headers_dumped;
2698 lwsl_debug("LHCS_DUMP_HEADERS: %d\n", n);
2700 m = lws_write(wsi, (unsigned char *)wsi->cgi->headers_dumped,
2701 n, LWS_WRITE_HTTP_HEADERS);
2703 lwsl_debug("%s: write says %d\n", __func__, m);
2706 wsi->cgi->headers_dumped += n;
2707 if (wsi->cgi->headers_dumped == wsi->cgi->headers_pos) {
2708 wsi->hdr_state = LHCS_PAYLOAD;
2709 lws_free_set_NULL(wsi->cgi->headers_buf);
2710 lwsl_debug("freed cgi headers\n");
2712 wsi->reason_bf |= 8;
2713 lws_callback_on_writable(wsi);
2716 /* writeability becomes uncertain now we wrote
2717 * something, we must return to the event loop
2722 if (!wsi->cgi->headers_buf) {
2723 /* if we don't already have a headers buf, cook one up */
2725 wsi->cgi->headers_buf = malloc(n);
2726 if (!wsi->cgi->headers_buf) {
2731 lwsl_debug("allocated cgi hdrs\n");
2732 wsi->cgi->headers_pos = wsi->cgi->headers_buf;
2733 wsi->cgi->headers_dumped = wsi->cgi->headers_pos;
2734 wsi->cgi->headers_end = wsi->cgi->headers_buf + n - 1;
2736 for (n = 0; n < SIGNIFICANT_HDR_COUNT; n++) {
2737 wsi->cgi->match[n] = 0;
2742 n = read(lws_get_socket_fd(wsi->cgi->stdwsi[LWS_STDOUT]), &c, 1);
2744 if (errno != EAGAIN) {
2745 lwsl_debug("%s: read says %d\n", __func__, n);
2751 if (wsi->cgi->headers_pos >= wsi->cgi->headers_end - 4) {
2752 lwsl_notice("CGI headers larger than buffer size\n");
2758 lwsl_debug("-- 0x%02X %c %d %d\n", (unsigned char)c, c, wsi->cgi->match[1], wsi->hdr_state);
2761 switch (wsi->hdr_state) {
2764 for (n = 0; n < SIGNIFICANT_HDR_COUNT; n++) {
2765 /* significant headers with numeric decimal payloads */
2766 if (!significant_hdr[n][wsi->cgi->match[n]] &&
2767 (c >= '0' && c <= '9') &&
2768 wsi->cgi->lp < sizeof(wsi->cgi->l) - 1) {
2769 wsi->cgi->l[wsi->cgi->lp++] = c;
2770 wsi->cgi->l[wsi->cgi->lp] = '\0';
2772 case SIGNIFICANT_HDR_CONTENT_LENGTH:
2773 wsi->cgi->content_length = atoll(wsi->cgi->l);
2775 case SIGNIFICANT_HDR_STATUS:
2776 wsi->cgi->response_code = atol(wsi->cgi->l);
2777 lwsl_debug("Status set to %d\n", wsi->cgi->response_code);
2783 /* hits up to the NUL are sticky until next hdr */
2784 if (significant_hdr[n][wsi->cgi->match[n]]) {
2785 if (tolower(c) == significant_hdr[n][wsi->cgi->match[n]])
2786 wsi->cgi->match[n]++;
2788 wsi->cgi->match[n] = 0;
2792 /* some cgi only send us \x0a for EOL */
2794 wsi->hdr_state = LCHS_SINGLE_0A;
2795 *wsi->cgi->headers_pos++ = '\x0d';
2797 *wsi->cgi->headers_pos++ = c;
2799 wsi->hdr_state = LCHS_LF1;
2801 if (wsi->hdr_state != LCHS_HEADER &&
2802 !significant_hdr[SIGNIFICANT_HDR_TRANSFER_ENCODING][wsi->cgi->match[SIGNIFICANT_HDR_TRANSFER_ENCODING]]) {
2803 lwsl_debug("cgi produced chunked\n");
2804 wsi->cgi->explicitly_chunked = 1;
2807 /* presence of Location: mandates 302 retcode */
2808 if (wsi->hdr_state != LCHS_HEADER &&
2809 !significant_hdr[SIGNIFICANT_HDR_LOCATION][wsi->cgi->match[SIGNIFICANT_HDR_LOCATION]]) {
2810 lwsl_debug("CGI: Location hdr seen\n");
2811 wsi->cgi->response_code = 302;
2816 *wsi->cgi->headers_pos++ = c;
2818 wsi->hdr_state = LCHS_CR2;
2821 /* we got \r[^\n]... it's unreasonable */
2822 lwsl_debug("%s: funny CRLF 0x%02X\n", __func__, (unsigned char)c);
2828 wsi->hdr_state = LCHS_LF2;
2831 wsi->hdr_state = LCHS_HEADER;
2832 for (n = 0; n < SIGNIFICANT_HDR_COUNT; n++)
2833 wsi->cgi->match[n] = 0;
2838 case LCHS_SINGLE_0A:
2841 lwsl_debug("Content-Length: %lld\n", (unsigned long long)wsi->cgi->content_length);
2842 wsi->hdr_state = LHCS_RESPONSE;
2843 /* drop the \0xa ... finalize will add it if needed */
2847 /* we got \r\n\r[^\n]... it's unreasonable */
2849 /* we got \x0anext header, it's reasonable */
2850 *wsi->cgi->headers_pos++ = c;
2851 wsi->hdr_state = LCHS_HEADER;
2852 for (n = 0; n < SIGNIFICANT_HDR_COUNT; n++)
2853 wsi->cgi->match[n] = 0;
2861 /* ran out of input, ended the headers, or filled up the headers buf */
2862 if (!n || wsi->hdr_state == LHCS_PAYLOAD)
2866 /* payload processing */
2868 n = read(lws_get_socket_fd(wsi->cgi->stdwsi[LWS_STDOUT]),
2869 start, sizeof(buf) - LWS_PRE);
2871 if (n < 0 && errno != EAGAIN) {
2872 lwsl_debug("%s: stdout read says %d\n", __func__, n);
2876 m = lws_write(wsi, (unsigned char *)start, n, LWS_WRITE_HTTP);
2877 //lwsl_notice("write %d\n", m);
2879 lwsl_debug("%s: stdout write says %d\n", __func__, m);
2882 wsi->cgi->content_length_seen += m;
2888 LWS_VISIBLE LWS_EXTERN int
2889 lws_cgi_kill(struct lws *wsi)
2891 struct lws_cgi_args args;
2894 lwsl_debug("%s: %p\n", __func__, wsi);
2899 if (wsi->cgi->pid > 0) {
2900 n = waitpid(wsi->cgi->pid, &status, WNOHANG);
2902 lwsl_debug("%s: PID %d reaped\n", __func__,
2906 /* kill the process group */
2907 n = kill(-wsi->cgi->pid, SIGTERM);
2908 lwsl_debug("%s: SIGTERM child PID %d says %d (errno %d)\n", __func__,
2909 wsi->cgi->pid, n, errno);
2912 * hum seen errno=3 when process is listed in ps,
2913 * it seems we don't always retain process grouping
2915 * Direct these fallback attempt to the exact child
2917 n = kill(wsi->cgi->pid, SIGTERM);
2919 n = kill(wsi->cgi->pid, SIGPIPE);
2921 n = kill(wsi->cgi->pid, SIGKILL);
2923 lwsl_err("%s: SIGKILL PID %d failed errno %d (maybe zombie)\n",
2924 __func__, wsi->cgi->pid, errno);
2928 /* He could be unkillable because he's a zombie */
2931 n = waitpid(-wsi->cgi->pid, &status, WNOHANG);
2933 lwsl_debug("%s: reaped PID %d\n", __func__, n);
2935 n = waitpid(wsi->cgi->pid, &status, WNOHANG);
2937 lwsl_debug("%s: reaped PID %d\n", __func__, n);
2943 args.stdwsi = &wsi->cgi->stdwsi[0];
2945 if (wsi->cgi->pid != -1 && user_callback_handle_rxflow(
2946 wsi->protocol->callback,
2947 wsi, LWS_CALLBACK_CGI_TERMINATED,
2949 (void *)&args, 0)) {
2951 if (!wsi->cgi->being_closed)
2952 lws_close_free_wsi(wsi, 0);
2959 lws_cgi_kill_terminated(struct lws_context_per_thread *pt)
2961 struct lws_cgi **pcgi, *cgi = NULL;
2965 /* find finished guys but don't reap yet */
2966 n = waitpid(-1, &status, WNOHANG);
2969 lwsl_debug("%s: observed PID %d terminated\n", __func__, n);
2971 pcgi = &pt->cgi_list;
2973 /* check all the subprocesses on the cgi list */
2975 /* get the next one first as list may change */
2977 pcgi = &(*pcgi)->cgi_list;
2982 /* finish sending cached headers */
2983 if (cgi->headers_buf)
2986 /* wait for stdout to be drained */
2987 if (cgi->content_length > cgi->content_length_seen)
2990 if (cgi->content_length) {
2991 lwsl_debug("%s: wsi %p: expected content length seen: %lld\n",
2992 __func__, cgi->wsi, (unsigned long long)cgi->content_length_seen);
2996 waitpid(n, &status, WNOHANG);
2998 * he's already terminated so no need for kill()
2999 * but we should do the terminated cgi callback
3000 * and close him if he's not already closing
3002 if (n == cgi->pid) {
3003 lwsl_debug("%s: found PID %d on cgi list\n",
3006 if (!cgi->content_length && cgi->explicitly_chunked) {
3008 * well, if he sends chunked... give him 5s after the
3009 * cgi terminated to send buffered
3011 cgi->chunked_grace++;
3017 lws_cgi_kill(cgi->wsi);
3023 /* if not found on the cgi list, as he's one of ours, reap */
3025 lwsl_debug("%s: reading PID %d although no cgi match\n",
3027 waitpid(n, &status, WNOHANG);
3031 /* disable this to confirm timeout cgi cleanup flow */
3033 pcgi = &pt->cgi_list;
3035 /* check all the subprocesses on the cgi list */
3037 /* get the next one first as list may change */
3039 pcgi = &(*pcgi)->cgi_list;
3044 /* we deferred killing him after reaping his PID */
3045 if (cgi->chunked_grace) {
3046 cgi->chunked_grace++;
3047 if (cgi->chunked_grace < 5)
3052 /* finish sending cached headers */
3053 if (cgi->headers_buf)
3056 /* wait for stdout to be drained */
3057 if (cgi->content_length > cgi->content_length_seen)
3060 if (cgi->content_length)
3061 lwsl_debug("%s: wsi %p: expected content length seen: %lld\n",
3062 __func__, cgi->wsi, (unsigned long long)cgi->content_length_seen);
3065 if (waitpid(cgi->pid, &status, WNOHANG) > 0) {
3067 if (!cgi->content_length) {
3069 * well, if he sends chunked... give him 5s after the
3070 * cgi terminated to send buffered
3072 cgi->chunked_grace++;
3076 lwsl_debug("%s: found PID %d on cgi list\n",
3077 __func__, cgi->pid);
3080 lws_cgi_kill(cgi->wsi);
3087 /* general anti zombie defence */
3088 // n = waitpid(-1, &status, WNOHANG);
3090 // lwsl_notice("%s: anti-zombie wait says %d\n", __func__, n);
3096 #ifdef LWS_NO_EXTENSIONS
3098 lws_set_extension_option(struct lws *wsi, const char *ext_name,
3099 const char *opt_name, const char *opt_val)
3105 #ifdef LWS_WITH_ACCESS_LOG
3107 lws_access_log(struct lws *wsi)
3109 char *p = wsi->access_log.user_agent, ass[512];
3112 if (!wsi->access_log_pending)
3115 if (!wsi->access_log.header_log)
3121 l = lws_snprintf(ass, sizeof(ass) - 1, "%s %d %lu %s\n",
3122 wsi->access_log.header_log,
3123 wsi->access_log.response, wsi->access_log.sent, p);
3125 if (wsi->vhost->log_fd != (int)LWS_INVALID_FILE) {
3126 if (write(wsi->vhost->log_fd, ass, l) != l)
3127 lwsl_err("Failed to write log\n");
3129 lwsl_err("%s", ass);
3131 if (wsi->access_log.header_log) {
3132 lws_free(wsi->access_log.header_log);
3133 wsi->access_log.header_log = NULL;
3135 if (wsi->access_log.user_agent) {
3136 lws_free(wsi->access_log.user_agent);
3137 wsi->access_log.user_agent = NULL;
3139 wsi->access_log_pending = 0;
3146 lws_sum_stats(const struct lws_context *ctx, struct lws_conn_stats *cs)
3148 const struct lws_vhost *vh = ctx->vhost_list;
3152 cs->rx += vh->conn_stats.rx;
3153 cs->tx += vh->conn_stats.tx;
3154 cs->conn += vh->conn_stats.conn;
3155 cs->trans += vh->conn_stats.trans;
3156 cs->ws_upg += vh->conn_stats.ws_upg;
3157 cs->http2_upg += vh->conn_stats.http2_upg;
3158 cs->rejected += vh->conn_stats.rejected;
3160 vh = vh->vhost_next;
3164 #ifdef LWS_WITH_SERVER_STATUS
3167 lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len)
3169 static const char * const prots[] = {
3178 char *orig = buf, *end = buf + len - 1, first = 1;
3184 buf += lws_snprintf(buf, end - buf,
3185 "{\n \"name\":\"%s\",\n"
3186 " \"port\":\"%d\",\n"
3187 " \"use_ssl\":\"%d\",\n"
3188 " \"sts\":\"%d\",\n"
3189 " \"rx\":\"%llu\",\n"
3190 " \"tx\":\"%llu\",\n"
3191 " \"conn\":\"%lu\",\n"
3192 " \"trans\":\"%lu\",\n"
3193 " \"ws_upg\":\"%lu\",\n"
3194 " \"rejected\":\"%lu\",\n"
3195 " \"http2_upg\":\"%lu\""
3197 vh->name, vh->listen_port,
3198 #ifdef LWS_OPENSSL_SUPPORT
3203 !!(vh->options & LWS_SERVER_OPTION_STS),
3204 vh->conn_stats.rx, vh->conn_stats.tx,
3205 vh->conn_stats.conn, vh->conn_stats.trans,
3206 vh->conn_stats.ws_upg,
3207 vh->conn_stats.rejected,
3208 vh->conn_stats.http2_upg
3211 if (vh->mount_list) {
3212 const struct lws_http_mount *m = vh->mount_list;
3214 buf += lws_snprintf(buf, end - buf, ",\n \"mounts\":[");
3217 buf += lws_snprintf(buf, end - buf, ",");
3218 buf += lws_snprintf(buf, end - buf,
3219 "\n {\n \"mountpoint\":\"%s\",\n"
3220 " \"origin\":\"%s%s\",\n"
3221 " \"cache_max_age\":\"%d\",\n"
3222 " \"cache_reuse\":\"%d\",\n"
3223 " \"cache_revalidate\":\"%d\",\n"
3224 " \"cache_intermediaries\":\"%d\"\n"
3227 prots[m->origin_protocol],
3231 m->cache_revalidate,
3232 m->cache_intermediaries);
3234 buf += lws_snprintf(buf, end - buf,
3235 ",\n \"default\":\"%s\"",
3237 buf += lws_snprintf(buf, end - buf, "\n }");
3241 buf += lws_snprintf(buf, end - buf, "\n ]");
3244 if (vh->protocols) {
3248 buf += lws_snprintf(buf, end - buf, ",\n \"ws-protocols\":[");
3249 while (n < vh->count_protocols) {
3251 buf += lws_snprintf(buf, end - buf, ",");
3252 buf += lws_snprintf(buf, end - buf,
3254 " \"status\":\"ok\"\n }\n }"
3256 vh->protocols[n].name);
3260 buf += lws_snprintf(buf, end - buf, "\n ]");
3263 buf += lws_snprintf(buf, end - buf, "\n}");
3269 LWS_EXTERN LWS_VISIBLE int
3270 lws_json_dump_context(const struct lws_context *context, char *buf, int len,
3273 char *orig = buf, *end = buf + len - 1, first = 1;
3274 const struct lws_vhost *vh = context->vhost_list;
3275 const struct lws_context_per_thread *pt;
3276 time_t t = time(NULL);
3277 int n, listening = 0, cgi_count = 0;
3278 struct lws_conn_stats cs;
3281 struct lws_cgi * const *pcgi;
3284 #ifdef LWS_USE_LIBUV
3288 buf += lws_snprintf(buf, end - buf, "{ "
3289 "\"version\":\"%s\",\n"
3290 "\"uptime\":\"%ld\",\n",
3291 lws_get_library_version(),
3294 #ifdef LWS_HAVE_GETLOADAVG
3299 m = getloadavg(d, 3);
3300 for (n = 0; n < m; n++) {
3301 buf += lws_snprintf(buf, end - buf,
3302 "\"l%d\":\"%.2f\",\n",
3308 buf += lws_snprintf(buf, end - buf, "\"contexts\":[\n");
3310 buf += lws_snprintf(buf, end - buf, "{ "
3311 "\"context_uptime\":\"%ld\",\n"
3312 "\"cgi_spawned\":\"%d\",\n"
3313 "\"pt_fd_max\":\"%d\",\n"
3314 "\"ah_pool_max\":\"%d\",\n"
3315 "\"deprecated\":\"%d\",\n"
3316 "\"wsi_alive\":\"%d\",\n",
3317 (unsigned long)(t - context->time_up),
3318 context->count_cgi_spawned,
3319 context->fd_limit_per_thread,
3320 context->max_http_header_pool,
3321 context->deprecated,
3322 context->count_wsi_allocated);
3324 buf += lws_snprintf(buf, end - buf, "\"pt\":[\n ");
3325 for (n = 0; n < context->count_threads; n++) {
3326 pt = &context->pt[n];
3328 buf += lws_snprintf(buf, end - buf, ",");
3329 buf += lws_snprintf(buf, end - buf,
3331 " \"fds_count\":\"%d\",\n"
3332 " \"ah_pool_inuse\":\"%d\",\n"
3333 " \"ah_wait_list\":\"%d\"\n"
3336 pt->ah_count_in_use,
3337 pt->ah_wait_list_length);
3340 buf += lws_snprintf(buf, end - buf, "]");
3342 buf += lws_snprintf(buf, end - buf, ", \"vhosts\":[\n ");
3345 vh = context->vhost_list;
3347 cs = context->conn_stats;
3348 lws_sum_stats(context, &cs);
3355 buf += lws_json_dump_vhost(vh, buf, end - buf);
3360 vh = vh->vhost_next;
3363 buf += lws_snprintf(buf, end - buf,
3364 "],\n\"listen_wsi\":\"%d\",\n"
3365 " \"rx\":\"%llu\",\n"
3366 " \"tx\":\"%llu\",\n"
3367 " \"conn\":\"%lu\",\n"
3368 " \"trans\":\"%lu\",\n"
3369 " \"ws_upg\":\"%lu\",\n"
3370 " \"rejected\":\"%lu\",\n"
3371 " \"http2_upg\":\"%lu\"",
3373 cs.rx, cs.tx, cs.conn, cs.trans,
3374 cs.ws_upg, cs.rejected, cs.http2_upg);
3377 for (n = 0; n < context->count_threads; n++) {
3378 pt = &context->pt[n];
3379 pcgi = &pt->cgi_list;
3382 pcgi = &(*pcgi)->cgi_list;
3388 buf += lws_snprintf(buf, end - buf, ",\n \"cgi_alive\":\"%d\"\n ",
3391 buf += lws_snprintf(buf, end - buf, "}");
3394 buf += lws_snprintf(buf, end - buf, "]}\n ");
3401 #if defined(LWS_WITH_STATS)
3403 LWS_VISIBLE LWS_EXTERN uint64_t
3404 lws_stats_get(struct lws_context *context, int index)
3406 if (index >= LWSSTATS_SIZE)
3409 return context->lws_stats[index];
3412 LWS_VISIBLE LWS_EXTERN void
3413 lws_stats_log_dump(struct lws_context *context)
3415 struct lws_vhost *v = context->vhost_list;
3418 if (!context->updated)
3421 context->updated = 0;
3424 lwsl_notice("LWS internal statistics dump ----->\n");
3425 lwsl_notice("LWSSTATS_C_CONNECTIONS: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_CONNECTIONS));
3426 lwsl_notice("LWSSTATS_C_API_CLOSE: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_API_CLOSE));
3427 lwsl_notice("LWSSTATS_C_API_READ: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_API_READ));
3428 lwsl_notice("LWSSTATS_C_API_LWS_WRITE: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_API_LWS_WRITE));
3429 lwsl_notice("LWSSTATS_C_API_WRITE: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_API_WRITE));
3430 lwsl_notice("LWSSTATS_C_WRITE_PARTIALS: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_WRITE_PARTIALS));
3431 lwsl_notice("LWSSTATS_C_WRITEABLE_CB_REQ: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB_REQ));
3432 lwsl_notice("LWSSTATS_C_WRITEABLE_CB_EFF_REQ: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB_EFF_REQ));
3433 lwsl_notice("LWSSTATS_C_WRITEABLE_CB: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB));
3434 lwsl_notice("LWSSTATS_C_SSL_CONNECTIONS_FAILED: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_SSL_CONNECTIONS_FAILED));
3435 lwsl_notice("LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED));
3436 lwsl_notice("LWSSTATS_C_SSL_CONNS_HAD_RX: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_SSL_CONNS_HAD_RX));
3438 lwsl_notice("LWSSTATS_C_TIMEOUTS: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_TIMEOUTS));
3439 lwsl_notice("LWSSTATS_C_SERVICE_ENTRY: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_C_SERVICE_ENTRY));
3440 lwsl_notice("LWSSTATS_B_READ: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_B_READ));
3441 lwsl_notice("LWSSTATS_B_WRITE: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_B_WRITE));
3442 lwsl_notice("LWSSTATS_B_PARTIALS_ACCEPTED_PARTS: %8llu\n", (unsigned long long)lws_stats_get(context, LWSSTATS_B_PARTIALS_ACCEPTED_PARTS));
3443 lwsl_notice("LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY: %8llums\n", (unsigned long long)lws_stats_get(context, LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY) / 1000);
3444 if (lws_stats_get(context, LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED))
3445 lwsl_notice(" Avg accept delay: %8llums\n",
3446 (unsigned long long)(lws_stats_get(context, LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY) /
3447 lws_stats_get(context, LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED)) / 1000);
3448 lwsl_notice("LWSSTATS_MS_SSL_RX_DELAY: %8llums\n", (unsigned long long)lws_stats_get(context, LWSSTATS_MS_SSL_RX_DELAY) / 1000);
3449 if (lws_stats_get(context, LWSSTATS_C_SSL_CONNS_HAD_RX))
3450 lwsl_notice(" Avg accept-rx delay: %8llums\n",
3451 (unsigned long long)(lws_stats_get(context, LWSSTATS_MS_SSL_RX_DELAY) /
3452 lws_stats_get(context, LWSSTATS_C_SSL_CONNS_HAD_RX)) / 1000);
3454 lwsl_notice("LWSSTATS_MS_WRITABLE_DELAY: %8lluus\n",
3455 (unsigned long long)lws_stats_get(context, LWSSTATS_MS_WRITABLE_DELAY));
3456 lwsl_notice("LWSSTATS_MS_WORST_WRITABLE_DELAY: %8lluus\n",
3457 (unsigned long long)lws_stats_get(context, LWSSTATS_MS_WORST_WRITABLE_DELAY));
3458 if (lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB))
3459 lwsl_notice(" Avg writable delay: %8lluus\n",
3460 (unsigned long long)(lws_stats_get(context, LWSSTATS_MS_WRITABLE_DELAY) /
3461 lws_stats_get(context, LWSSTATS_C_WRITEABLE_CB)));
3462 lwsl_notice("Simultaneous SSL restriction: %8d/%d/%d\n", context->simultaneous_ssl,
3463 context->simultaneous_ssl_restriction, context->ssl_gate_accepts);
3465 lwsl_notice("Live wsi: %8d\n", context->count_wsi_allocated);
3467 #if defined(LWS_WITH_STATS)
3468 context->updated = 1;
3474 struct lws_context_per_thread *pt = &context->pt[(int)v->lserv_wsi->tsi];
3475 struct lws_pollfd *pfd;
3477 pfd = &pt->fds[v->lserv_wsi->position_in_fds_table];
3479 lwsl_notice(" Listen port %d actual POLLIN: %d\n",
3480 v->listen_port, (int)pfd->events & LWS_POLLIN);
3486 for (n = 0; n < context->count_threads; n++) {
3487 struct lws_context_per_thread *pt = &context->pt[n];
3491 lwsl_notice("PT %d\n", n + 1);
3495 lwsl_notice(" AH in use / max: %d / %d\n",
3496 pt->ah_count_in_use,
3497 context->max_http_header_pool);
3499 wl = pt->ah_wait_list;
3502 wl = wl->u.hdr.ah_wait_list;
3505 lwsl_notice(" AH wait list count / actual: %d / %d\n",
3506 pt->ah_wait_list_length, m);
3515 lws_stats_atomic_bump(struct lws_context * context,
3516 struct lws_context_per_thread *pt, int index, uint64_t bump)
3519 context->lws_stats[index] += bump;
3520 if (index != LWSSTATS_C_SERVICE_ENTRY)
3521 context->updated = 1;
3526 lws_stats_atomic_max(struct lws_context * context,
3527 struct lws_context_per_thread *pt, int index, uint64_t val)
3530 if (val > context->lws_stats[index]) {
3531 context->lws_stats[index] = val;
3532 context->updated = 1;