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_close_free_wsi(struct lws *wsi, enum lws_close_status reason)
148 struct lws_context_per_thread *pt;
149 struct lws **pwsi, *wsi1, *wsi2;
150 struct lws_context *context;
151 struct lws_tokens eff_buf;
158 #if defined(LWS_WITH_ESP8266)
159 if (wsi->premature_rx)
160 lws_free(wsi->premature_rx);
162 if (wsi->pending_send_completion && !wsi->close_is_pending_send_completion) {
163 lwsl_notice("delaying close\n");
164 wsi->close_is_pending_send_completion = 1;
170 /* we're closing, losing some rx is OK */
171 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
173 context = wsi->context;
174 pt = &context->pt[(int)wsi->tsi];
176 /* if we have children, close them first */
177 if (wsi->child_list) {
178 wsi2 = wsi->child_list;
180 //lwsl_notice("%s: closing %p: close child %p\n",
181 // __func__, wsi, wsi2);
182 wsi1 = wsi2->sibling_list;
183 //lwsl_notice("%s: closing %p: next sibling %p\n",
184 // __func__, wsi2, wsi1);
186 /* stop it doing shutdown processing */
187 wsi2->socket_is_permanently_unusable = 1;
188 lws_close_free_wsi(wsi2, reason);
191 wsi->child_list = NULL;
195 if (wsi->mode == LWSCM_CGI) {
196 /* we are not a network connection, but a handler for CGI io */
197 if (wsi->parent && wsi->parent->cgi)
198 /* end the binding between us and master */
199 wsi->parent->cgi->stdwsi[(int)wsi->cgi_channel] = NULL;
200 wsi->socket_is_permanently_unusable = 1;
202 lwsl_debug("------ %s: detected cgi fdhandler wsi %p\n", __func__, wsi);
203 goto just_kill_connection;
207 struct lws_cgi **pcgi = &pt->cgi_list;
208 /* remove us from the cgi list */
209 lwsl_debug("%s: remove cgi %p from list\n", __func__, wsi->cgi);
211 if (*pcgi == wsi->cgi) {
212 /* drop us from the pt cgi list */
213 *pcgi = (*pcgi)->cgi_list;
216 pcgi = &(*pcgi)->cgi_list;
218 /* we have a cgi going, we must kill it */
219 wsi->cgi->being_closed = 1;
224 if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED &&
225 wsi->u.http.fd != LWS_INVALID_FILE) {
226 lws_plat_file_close(wsi, wsi->u.http.fd);
227 wsi->u.http.fd = LWS_INVALID_FILE;
228 wsi->vhost->protocols->callback(wsi,
229 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
230 wsi->told_user_closed = 1;
232 if (wsi->socket_is_permanently_unusable ||
233 reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY ||
234 wsi->state == LWSS_SHUTDOWN)
235 goto just_kill_connection;
237 wsi->state_pre_close = wsi->state;
239 switch (wsi->state_pre_close) {
240 case LWSS_DEAD_SOCKET:
243 /* we tried the polite way... */
244 case LWSS_AWAITING_CLOSE_ACK:
245 goto just_kill_connection;
247 case LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE:
248 if (wsi->trunc_len) {
249 lws_callback_on_writable(wsi);
252 lwsl_info("wsi %p completed LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
253 goto just_kill_connection;
255 if (wsi->trunc_len) {
256 lwsl_info("wsi %p entering LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
257 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
258 lws_set_timeout(wsi, PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5);
264 if (wsi->mode == LWSCM_WSCL_WAITING_CONNECT ||
265 wsi->mode == LWSCM_WSCL_ISSUE_HANDSHAKE)
266 goto just_kill_connection;
268 if (wsi->mode == LWSCM_HTTP_SERVING) {
270 wsi->vhost->protocols->callback(wsi,
271 LWS_CALLBACK_HTTP_DROP_PROTOCOL,
272 wsi->user_space, NULL, 0);
273 wsi->vhost->protocols->callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
274 wsi->user_space, NULL, 0);
275 wsi->told_user_closed = 1;
277 if (wsi->mode & LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP) {
278 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_CLIENT_HTTP,
279 wsi->user_space, NULL, 0);
280 wsi->told_user_closed = 1;
284 * are his extensions okay with him closing? Eg he might be a mux
285 * parent and just his ch1 aspect is closing?
288 if (lws_ext_cb_active(wsi,
289 LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE, NULL, 0) > 0) {
290 lwsl_ext("extension vetoed close\n");
295 * flush any tx pending from extensions, since we may send close packet
296 * if there are problems with send, just nuke the connection
301 eff_buf.token = NULL;
302 eff_buf.token_len = 0;
304 /* show every extension the new incoming data */
306 m = lws_ext_cb_active(wsi,
307 LWS_EXT_CB_FLUSH_PENDING_TX, &eff_buf, 0);
309 lwsl_ext("Extension reports fatal error\n");
310 goto just_kill_connection;
314 * at least one extension told us he has more
315 * to spill, so we will go around again after
319 /* assuming they left us something to send, send it */
321 if (eff_buf.token_len)
322 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
323 eff_buf.token_len) !=
325 lwsl_debug("close: ext spill failed\n");
326 goto just_kill_connection;
331 * signal we are closing, lws_write will
332 * add any necessary version-specific stuff. If the write fails,
333 * no worries we are closing anyway. If we didn't initiate this
334 * close, then our state has been changed to
335 * LWSS_RETURNED_CLOSE_ALREADY and we will skip this.
337 * Likewise if it's a second call to close this connection after we
338 * sent the close indication to the peer already, we are in state
339 * LWSS_AWAITING_CLOSE_ACK and will skip doing this a second time.
342 if (wsi->state_pre_close == LWSS_ESTABLISHED &&
343 (wsi->u.ws.close_in_ping_buffer_len || /* already a reason */
344 (reason != LWS_CLOSE_STATUS_NOSTATUS &&
345 (reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)))) {
346 lwsl_debug("sending close indication...\n");
348 /* if no prepared close reason, use 1000 and no aux data */
349 if (!wsi->u.ws.close_in_ping_buffer_len) {
350 wsi->u.ws.close_in_ping_buffer_len = 2;
351 wsi->u.ws.ping_payload_buf[LWS_PRE] =
352 (reason >> 16) & 0xff;
353 wsi->u.ws.ping_payload_buf[LWS_PRE + 1] =
357 #if defined (LWS_WITH_ESP8266)
358 wsi->close_is_pending_send_completion = 1;
360 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
361 wsi->u.ws.close_in_ping_buffer_len,
365 * we have sent a nice protocol level indication we
366 * now wish to close, we should not send anything more
368 wsi->state = LWSS_AWAITING_CLOSE_ACK;
371 * ...and we should wait for a reply for a bit
374 lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_ACK, 1);
375 lwsl_debug("sent close indication, awaiting ack\n");
380 lwsl_info("close: sending close packet failed, hanging up\n");
382 /* else, the send failed and we should just hang up */
385 just_kill_connection:
388 /* detach ourselves from parent's child list */
389 pwsi = &wsi->parent->child_list;
392 //lwsl_notice("%s: detach %p from parent %p\n",
393 // __func__, wsi, wsi->parent);
394 *pwsi = wsi->sibling_list;
397 pwsi = &(*pwsi)->sibling_list;
400 lwsl_err("%s: failed to detach from parent\n",
404 /* manage the vhost same protocol list entry */
406 if (wsi->same_vh_protocol_prev) { // we are on the vh list
408 // make guy who pointed to us, point to what our next was pointing to
409 *wsi->same_vh_protocol_prev = wsi->same_vh_protocol_next;
411 // if we had a next guy...
412 if (wsi->same_vh_protocol_next)
413 // have him point back to our prev
414 wsi->same_vh_protocol_next->same_vh_protocol_prev =
415 wsi->same_vh_protocol_prev;
421 * Testing with ab shows that we have to stage the socket close when
422 * the system is under stress... shutdown any further TX, change the
423 * state to one that won't emit anything more, and wait with a timeout
424 * for the POLLIN to show a zero-size rx before coming back and doing
427 if (wsi->state != LWSS_SHUTDOWN &&
428 wsi->state != LWSS_CLIENT_UNCONNECTED &&
429 reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY &&
430 !wsi->socket_is_permanently_unusable) {
431 lwsl_info("%s: shutting down connection: %p (sock %d, state %d)\n", __func__, wsi, wsi->sock, wsi->state);
432 n = shutdown(wsi->sock, SHUT_WR);
434 lwsl_debug("closing: shutdown (state %d) ret %d\n", wsi->state, LWS_ERRNO);
436 // This causes problems with disconnection when the events are half closing connection
437 // FD_READ | FD_CLOSE (33)
439 /* libuv: no event available to guarantee completion */
440 if (!LWS_LIBUV_ENABLED(context)) {
442 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
443 wsi->state = LWSS_SHUTDOWN;
444 lws_set_timeout(wsi, PENDING_TIMEOUT_SHUTDOWN_FLUSH,
445 context->timeout_secs);
452 lwsl_info("%s: real just_kill_connection: %p (sockfd %d)\n", __func__,
455 #ifdef LWS_WITH_HTTP_PROXY
457 lws_rewrite_destroy(wsi->rw);
462 * we won't be servicing or receiving anything further from this guy
463 * delete socket from the internal poll list if still present
465 lws_ssl_remove_wsi_from_buffered_list(wsi);
467 lws_remove_from_timeout_list(wsi);
469 /* checking return redundant since we anyway close */
470 if (wsi->sock != LWS_SOCK_INVALID)
471 remove_wsi_socket_from_fds(wsi);
473 #if defined(LWS_WITH_ESP8266)
474 espconn_disconnect(wsi->sock);
477 wsi->state = LWSS_DEAD_SOCKET;
479 lws_free_set_NULL(wsi->rxflow_buffer);
481 if (wsi->state_pre_close == LWSS_ESTABLISHED ||
482 wsi->mode == LWSCM_WS_SERVING ||
483 wsi->mode == LWSCM_WS_CLIENT) {
485 if (wsi->u.ws.rx_draining_ext) {
486 struct lws **w = &pt->rx_draining_ext_list;
488 wsi->u.ws.rx_draining_ext = 0;
489 /* remove us from context draining ext list */
492 *w = wsi->u.ws.rx_draining_ext_list;
495 w = &((*w)->u.ws.rx_draining_ext_list);
497 wsi->u.ws.rx_draining_ext_list = NULL;
500 if (wsi->u.ws.tx_draining_ext) {
501 struct lws **w = &pt->tx_draining_ext_list;
503 wsi->u.ws.tx_draining_ext = 0;
504 /* remove us from context draining ext list */
507 *w = wsi->u.ws.tx_draining_ext_list;
510 w = &((*w)->u.ws.tx_draining_ext_list);
512 wsi->u.ws.tx_draining_ext_list = NULL;
514 lws_free_set_NULL(wsi->u.ws.rx_ubuf);
516 if (wsi->trunc_alloc)
517 /* not going to be completed... nuke it */
518 lws_free_set_NULL(wsi->trunc_alloc);
520 wsi->u.ws.ping_payload_len = 0;
521 wsi->u.ws.ping_pending_flag = 0;
524 /* tell the user it's all over for this guy */
526 if (wsi->protocol && wsi->protocol->callback &&
527 ((wsi->state_pre_close == LWSS_ESTABLISHED) ||
528 (wsi->state_pre_close == LWSS_RETURNED_CLOSE_ALREADY) ||
529 (wsi->state_pre_close == LWSS_AWAITING_CLOSE_ACK) ||
530 (wsi->state_pre_close == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) ||
531 (wsi->mode == LWSCM_WS_CLIENT && wsi->state_pre_close == LWSS_HTTP) ||
532 (wsi->mode == LWSCM_WS_SERVING && wsi->state_pre_close == LWSS_HTTP))) {
534 if (wsi->user_space) {
535 lwsl_debug("%s: doing LWS_CALLBACK_HTTP_DROP_PROTOCOL for %p prot %s", __func__, wsi, wsi->protocol->name);
536 wsi->protocol->callback(wsi,
537 LWS_CALLBACK_HTTP_DROP_PROTOCOL,
538 wsi->user_space, NULL, 0);
540 lwsl_debug("calling back CLOSED\n");
541 wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
542 wsi->user_space, NULL, 0);
543 } else if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED) {
544 lwsl_debug("calling back CLOSED_HTTP\n");
545 wsi->vhost->protocols->callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
546 wsi->user_space, NULL, 0 );
547 } else if ((wsi->mode == LWSCM_WSCL_WAITING_SERVER_REPLY ||
548 wsi->mode == LWSCM_WSCL_WAITING_CONNECT) &&
549 !wsi->already_did_cce) {
550 wsi->vhost->protocols[0].callback(wsi,
551 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
552 wsi->user_space, NULL, 0);
554 lwsl_debug("not calling back closed mode=%d state=%d\n",
555 wsi->mode, wsi->state_pre_close);
557 /* deallocate any active extension contexts */
559 if (lws_ext_cb_active(wsi, LWS_EXT_CB_DESTROY, NULL, 0) < 0)
560 lwsl_warn("extension destruction failed\n");
562 * inform all extensions in case they tracked this guy out of band
563 * even though not active on him specifically
565 if (lws_ext_cb_all_exts(context, wsi,
566 LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING, NULL, 0) < 0)
567 lwsl_warn("ext destroy wsi failed\n");
569 wsi->socket_is_permanently_unusable = 1;
572 if (LWS_LIBUV_ENABLED(context)) {
573 lwsl_debug("%s: lws_libuv_closehandle: wsi %p\n", __func__, wsi);
574 /* libuv has to do his own close handle processing asynchronously */
575 lws_libuv_closehandle(wsi);
581 lws_close_free_wsi_final(wsi);
585 lws_close_free_wsi_final(struct lws *wsi)
589 if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->sock)) {
591 //lwsl_err("*** closing sockfd %d\n", wsi->sock);
592 n = compatible_close(wsi->sock);
594 lwsl_debug("closing: close ret %d\n", LWS_ERRNO);
597 compatible_close(wsi->sock);
600 wsi->sock = LWS_SOCK_INVALID;
603 /* outermost destroy notification for wsi (user_space still intact) */
604 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
605 wsi->user_space, NULL, 0);
609 for (n = 0; n < 6; n++) {
610 if (wsi->cgi->pipe_fds[n / 2][n & 1] == 0)
611 lwsl_err("ZERO FD IN CGI CLOSE");
613 if (wsi->cgi->pipe_fds[n / 2][n & 1] >= 0)
614 close(wsi->cgi->pipe_fds[n / 2][n & 1]);
624 LWS_VISIBLE LWS_EXTERN const char *
625 lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len)
627 int n = 0, sl = strlen(name);
629 while (lws_hdr_copy_fragment(wsi, buf, len,
630 WSI_TOKEN_HTTP_URI_ARGS, n) >= 0) {
632 if (!strncmp(buf, name, sl))
643 interface_to_sa(struct lws_vhost *vh, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
647 ipv6 = LWS_IPV6_ENABLED(vh);
651 return lws_interface_to_sa(ipv6, ifname, addr, addrlen);
655 #ifndef LWS_PLAT_OPTEE
658 lws_get_addresses(struct lws_vhost *vh, void *ads, char *name,
659 int name_len, char *rip, int rip_len)
662 struct addrinfo ai, *res;
663 struct sockaddr_in addr4;
668 addr4.sin_family = AF_UNSPEC;
671 if (LWS_IPV6_ENABLED(vh)) {
672 if (!lws_plat_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ads)->sin6_addr, rip, rip_len)) {
673 lwsl_err("inet_ntop", strerror(LWS_ERRNO));
677 // Strip off the IPv4 to IPv6 header if one exists
678 if (strncmp(rip, "::ffff:", 7) == 0)
679 memmove(rip, rip + 7, strlen(rip) - 6);
681 getnameinfo((struct sockaddr *)ads,
682 sizeof(struct sockaddr_in6), name,
683 name_len, NULL, 0, 0);
689 struct addrinfo *result;
691 memset(&ai, 0, sizeof ai);
692 ai.ai_family = PF_UNSPEC;
693 ai.ai_socktype = SOCK_STREAM;
694 ai.ai_flags = AI_CANONNAME;
696 if (getnameinfo((struct sockaddr *)ads,
697 sizeof(struct sockaddr_in),
698 name, name_len, NULL, 0, 0))
704 if (getaddrinfo(name, NULL, &ai, &result))
708 while (addr4.sin_family == AF_UNSPEC && res) {
709 switch (res->ai_family) {
711 addr4.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
712 addr4.sin_family = AF_INET;
718 freeaddrinfo(result);
721 if (addr4.sin_family == AF_UNSPEC)
724 if (lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len) == NULL)
742 LWS_VISIBLE const char *
743 lws_get_peer_simple(struct lws *wsi, char *name, int namelen)
748 struct sockaddr_in6 sin6;
750 struct sockaddr_in sin4;
755 if (LWS_IPV6_ENABLED(wsi->vhost)) {
769 if (getpeername(wsi->sock, p, &len) < 0 || len > olen) {
770 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
774 return lws_plat_inet_ntop(af, q, name, namelen);
776 #if defined(LWS_WITH_ESP8266)
777 return lws_plat_get_peer_simple(wsi, name, namelen);
786 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
787 int name_len, char *rip, int rip_len)
789 #ifndef LWS_PLAT_OPTEE
793 struct sockaddr_in6 sin6;
795 struct sockaddr_in sin4;
796 struct lws_context *context = wsi->context;
803 lws_latency_pre(context, wsi);
806 if (LWS_IPV6_ENABLED(wsi->vhost)) {
816 if (getpeername(fd, p, &len) < 0) {
817 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
821 ret = lws_get_addresses(wsi->vhost, p, name, name_len, rip, rip_len);
824 lws_latency(context, wsi, "lws_get_peer_addresses", ret, 1);
837 lws_context_user(struct lws_context *context)
839 return context->user_space;
842 LWS_VISIBLE struct lws_vhost *
843 lws_vhost_get(struct lws *wsi)
848 LWS_VISIBLE struct lws_vhost *
849 lws_get_vhost(struct lws *wsi)
854 LWS_VISIBLE const struct lws_protocols *
855 lws_protocol_get(struct lws *wsi)
857 return wsi->protocol;
861 lws_callback_all_protocol(struct lws_context *context,
862 const struct lws_protocols *protocol, int reason)
864 struct lws_context_per_thread *pt = &context->pt[0];
865 unsigned int n, m = context->count_threads;
869 for (n = 0; n < pt->fds_count; n++) {
870 wsi = wsi_from_fd(context, pt->fds[n].fd);
873 if (wsi->protocol == protocol)
874 protocol->callback(wsi, reason, wsi->user_space,
884 lws_callback_all_protocol_vhost(struct lws_vhost *vh,
885 const struct lws_protocols *protocol, int reason)
887 struct lws_context *context = vh->context;
888 struct lws_context_per_thread *pt = &context->pt[0];
889 unsigned int n, m = context->count_threads;
893 for (n = 0; n < pt->fds_count; n++) {
894 wsi = wsi_from_fd(context, pt->fds[n].fd);
897 if (wsi->vhost == vh && wsi->protocol == protocol)
898 protocol->callback(wsi, reason, wsi->user_space,
907 LWS_VISIBLE LWS_EXTERN int
908 lws_callback_vhost_protocols(struct lws *wsi, int reason, void *in, int len)
912 for (n = 0; n < wsi->vhost->count_protocols; n++)
913 if (wsi->vhost->protocols[n].callback(wsi, reason, NULL, in, len))
920 * lws_now_secs() - seconds since 1970-1-1
923 LWS_VISIBLE LWS_EXTERN unsigned long
928 gettimeofday(&tv, NULL);
937 lws_get_socket_fd(struct lws *wsi)
946 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
947 int ret, int completed)
949 unsigned long long u;
952 u = time_in_microseconds();
955 wsi->latency_start = u;
956 if (!wsi->action_start)
957 wsi->action_start = u;
961 if (wsi->action_start == wsi->latency_start)
963 "Completion first try lat %lluus: %p: ret %d: %s\n",
964 u - wsi->latency_start,
965 (void *)wsi, ret, action);
968 "Completion %lluus: lat %lluus: %p: ret %d: %s\n",
969 u - wsi->action_start,
970 u - wsi->latency_start,
971 (void *)wsi, ret, action);
972 wsi->action_start = 0;
974 sprintf(buf, "lat %lluus: %p: ret %d: %s\n",
975 u - wsi->latency_start, (void *)wsi, ret, action);
977 if (u - wsi->latency_start > context->worst_latency) {
978 context->worst_latency = u - wsi->latency_start;
979 strcpy(context->worst_latency_info, buf);
981 lwsl_latency("%s", buf);
986 lws_rx_flow_control(struct lws *wsi, int enable)
988 if (enable == (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
991 lwsl_info("%s: (0x%p, %d)\n", __func__, wsi, enable);
992 wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
998 lws_rx_flow_allow_all_protocol(const struct lws_context *context,
999 const struct lws_protocols *protocol)
1001 const struct lws_context_per_thread *pt = &context->pt[0];
1003 unsigned int n, m = context->count_threads;
1006 for (n = 0; n < pt->fds_count; n++) {
1007 wsi = wsi_from_fd(context, pt->fds[n].fd);
1010 if (wsi->protocol == protocol)
1011 lws_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1017 LWS_VISIBLE extern const char *
1018 lws_canonical_hostname(struct lws_context *context)
1020 return (const char *)context->canonical_hostname;
1023 int user_callback_handle_rxflow(lws_callback_function callback_function,
1025 enum lws_callback_reasons reason, void *user,
1026 void *in, size_t len)
1030 n = callback_function(wsi, reason, user, in, len);
1032 n = _lws_rx_flow_control(wsi);
1037 #if defined(LWS_WITH_ESP8266)
1039 #define strchr ets_strchr
1043 lws_set_proxy(struct lws_vhost *vhost, const char *proxy)
1045 #if !defined(LWS_WITH_ESP8266)
1047 char authstring[96];
1052 /* we have to deal with a possible redundant leading http:// */
1053 if (!strncmp(proxy, "http://", 7))
1056 p = strchr(proxy, '@');
1057 if (p) { /* auth is around */
1059 if ((unsigned int)(p - proxy) > sizeof(authstring) - 1)
1062 strncpy(authstring, proxy, p - proxy);
1063 // null termination not needed on input
1064 if (lws_b64_encode_string(authstring, (p - proxy),
1065 vhost->proxy_basic_auth_token,
1066 sizeof vhost->proxy_basic_auth_token) < 0)
1069 lwsl_info(" Proxy auth in use\n");
1073 vhost->proxy_basic_auth_token[0] = '\0';
1075 strncpy(vhost->http_proxy_address, proxy,
1076 sizeof(vhost->http_proxy_address) - 1);
1077 vhost->http_proxy_address[
1078 sizeof(vhost->http_proxy_address) - 1] = '\0';
1080 p = strchr(vhost->http_proxy_address, ':');
1081 if (!p && !vhost->http_proxy_port) {
1082 lwsl_err("http_proxy needs to be ads:port\n");
1088 vhost->http_proxy_port = atoi(p + 1);
1092 lwsl_info(" Proxy %s:%u\n", vhost->http_proxy_address,
1093 vhost->http_proxy_port);
1098 lwsl_err("proxy auth too long\n");
1103 LWS_VISIBLE const struct lws_protocols *
1104 lws_get_protocol(struct lws *wsi)
1106 return wsi->protocol;
1110 lws_is_final_fragment(struct lws *wsi)
1112 lwsl_info("%s: final %d, rx pk length %ld, draining %ld", __func__,
1113 wsi->u.ws.final, (long)wsi->u.ws.rx_packet_length,
1114 (long)wsi->u.ws.rx_draining_ext);
1115 return wsi->u.ws.final && !wsi->u.ws.rx_packet_length && !wsi->u.ws.rx_draining_ext;
1118 LWS_VISIBLE unsigned char
1119 lws_get_reserved_bits(struct lws *wsi)
1121 return wsi->u.ws.rsv;
1125 lws_ensure_user_space(struct lws *wsi)
1127 lwsl_info("%s: %p protocol %p\n", __func__, wsi, wsi->protocol);
1131 /* allocate the per-connection user memory (if any) */
1133 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
1134 wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size);
1135 if (wsi->user_space == NULL) {
1136 lwsl_err("Out of memory for conn user space\n");
1140 lwsl_info("%s: %p protocol pss %lu, user_space=%p\n",
1141 __func__, wsi, (long)wsi->protocol->per_session_data_size,
1147 lwsl_timestamp(int level, char *p, int len)
1149 #ifndef LWS_PLAT_OPTEE
1150 time_t o_now = time(NULL);
1151 unsigned long long now;
1152 struct tm *ptm = NULL;
1160 ptm = localtime(&o_now);
1162 if (localtime_r(&o_now, &tm))
1167 for (n = 0; n < LLL_COUNT; n++) {
1168 if (level != (1 << n))
1170 now = time_in_microseconds() / 100;
1172 n = lws_snprintf(p, len,
1173 "[%04d/%02d/%02d %02d:%02d:%02d:%04d] %s: ",
1174 ptm->tm_year + 1900,
1180 (int)(now % 10000), log_level_names[n]);
1182 n = lws_snprintf(p, len, "[%llu:%04d] %s: ",
1183 (unsigned long long) now / 10000,
1184 (int)(now % 10000), log_level_names[n]);
1191 #ifndef LWS_PLAT_OPTEE
1192 LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line)
1194 #if !defined(LWS_WITH_ESP8266)
1197 lwsl_timestamp(level, buf, sizeof(buf));
1198 fprintf(stderr, "%s%s", buf, line);
1203 LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl)
1205 #if defined(LWS_WITH_ESP8266)
1212 if (!(log_level & filter))
1215 n = vsnprintf(buf, sizeof(buf) - 1, format, vl);
1217 #if defined(LWS_WITH_ESP8266)
1218 buf[sizeof(buf) - 1] = '\0';
1220 /* vnsprintf returns what it would have written, even if truncated */
1221 if (n > sizeof(buf) - 1)
1222 n = sizeof(buf) - 1;
1227 lwsl_emit(filter, buf);
1230 LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
1234 va_start(ap, format);
1235 _lws_logv(filter, format, ap);
1239 LWS_VISIBLE void lws_set_log_level(int level,
1240 void (*func)(int level, const char *line))
1248 lws_is_ssl(struct lws *wsi)
1250 #ifdef LWS_OPENSSL_SUPPORT
1251 return wsi->use_ssl;
1258 #ifdef LWS_OPENSSL_SUPPORT
1260 lws_get_ssl(struct lws *wsi)
1267 lws_partial_buffered(struct lws *wsi)
1269 return !!wsi->trunc_len;
1272 void lws_set_protocol_write_pending(struct lws *wsi,
1273 enum lws_pending_protocol_send pend)
1275 lwsl_info("setting pps %d\n", pend);
1278 lwsl_err("pps overwrite\n");
1280 lws_rx_flow_control(wsi, 0);
1281 lws_callback_on_writable(wsi);
1285 lws_get_peer_write_allowance(struct lws *wsi)
1287 #ifdef LWS_USE_HTTP2
1288 /* only if we are using HTTP2 on this connection */
1289 if (wsi->mode != LWSCM_HTTP2_SERVING)
1291 /* user is only interested in how much he can send, or that he can't */
1292 if (wsi->u.http2.tx_credit <= 0)
1295 return wsi->u.http2.tx_credit;
1303 lws_union_transition(struct lws *wsi, enum connection_mode mode)
1305 lwsl_debug("%s: %p: mode %d\n", __func__, wsi, mode);
1306 memset(&wsi->u, 0, sizeof(wsi->u));
1310 LWS_VISIBLE struct lws_plat_file_ops *
1311 lws_get_fops(struct lws_context *context)
1313 return &context->fops;
1316 LWS_VISIBLE LWS_EXTERN struct lws_context *
1317 lws_get_context(const struct lws *wsi)
1319 return wsi->context;
1322 LWS_VISIBLE LWS_EXTERN int
1323 lws_get_count_threads(struct lws_context *context)
1325 return context->count_threads;
1328 LWS_VISIBLE LWS_EXTERN void *
1329 lws_wsi_user(struct lws *wsi)
1331 return wsi->user_space;
1334 LWS_VISIBLE LWS_EXTERN struct lws *
1335 lws_get_parent(const struct lws *wsi)
1340 LWS_VISIBLE LWS_EXTERN struct lws *
1341 lws_get_child(const struct lws *wsi)
1343 return wsi->child_list;
1346 LWS_VISIBLE LWS_EXTERN void
1347 lws_close_reason(struct lws *wsi, enum lws_close_status status,
1348 unsigned char *buf, size_t len)
1350 unsigned char *p, *start;
1351 int budget = sizeof(wsi->u.ws.ping_payload_buf) - LWS_PRE;
1353 assert(wsi->mode == LWSCM_WS_SERVING || wsi->mode == LWSCM_WS_CLIENT);
1355 start = p = &wsi->u.ws.ping_payload_buf[LWS_PRE];
1357 *p++ = (((int)status) >> 8) & 0xff;
1358 *p++ = ((int)status) & 0xff;
1361 while (len-- && p < start + budget)
1364 wsi->u.ws.close_in_ping_buffer_len = p - start;
1368 _lws_rx_flow_control(struct lws *wsi)
1370 /* there is no pending change */
1371 if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) {
1372 lwsl_debug("%s: no pending change\n", __func__);
1376 /* stuff is still buffered, not ready to really accept new input */
1377 if (wsi->rxflow_buffer) {
1378 /* get ourselves called back to deal with stashed buffer */
1379 lws_callback_on_writable(wsi);
1383 /* pending is cleared, we can change rxflow state */
1385 wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1387 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1388 wsi->rxflow_change_to & LWS_RXFLOW_ALLOW);
1390 /* adjust the pollfd for this wsi */
1392 if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) {
1393 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
1394 lwsl_info("%s: fail\n", __func__);
1398 if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
1405 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len)
1407 static const unsigned char e0f4[] = {
1408 0xa0 | ((2 - 1) << 2) | 1, /* e0 */
1409 0x80 | ((4 - 1) << 2) | 1, /* e1 */
1410 0x80 | ((4 - 1) << 2) | 1, /* e2 */
1411 0x80 | ((4 - 1) << 2) | 1, /* e3 */
1412 0x80 | ((4 - 1) << 2) | 1, /* e4 */
1413 0x80 | ((4 - 1) << 2) | 1, /* e5 */
1414 0x80 | ((4 - 1) << 2) | 1, /* e6 */
1415 0x80 | ((4 - 1) << 2) | 1, /* e7 */
1416 0x80 | ((4 - 1) << 2) | 1, /* e8 */
1417 0x80 | ((4 - 1) << 2) | 1, /* e9 */
1418 0x80 | ((4 - 1) << 2) | 1, /* ea */
1419 0x80 | ((4 - 1) << 2) | 1, /* eb */
1420 0x80 | ((4 - 1) << 2) | 1, /* ec */
1421 0x80 | ((2 - 1) << 2) | 1, /* ed */
1422 0x80 | ((4 - 1) << 2) | 1, /* ee */
1423 0x80 | ((4 - 1) << 2) | 1, /* ef */
1424 0x90 | ((3 - 1) << 2) | 2, /* f0 */
1425 0x80 | ((4 - 1) << 2) | 2, /* f1 */
1426 0x80 | ((4 - 1) << 2) | 2, /* f2 */
1427 0x80 | ((4 - 1) << 2) | 2, /* f3 */
1428 0x80 | ((1 - 1) << 2) | 2, /* f4 */
1431 0x80 | ((4 - 1) << 2) | 0, /* s2 */
1432 0x80 | ((4 - 1) << 2) | 1, /* s3 */
1434 unsigned char s = *state;
1437 unsigned char c = *buf++;
1441 if (c < 0xc2 || c > 0xf4)
1444 s = 0x80 | ((4 - 1) << 2);
1449 if (c < (s & 0xf0) ||
1450 c >= (s & 0xf0) + 0x10 + ((s << 2) & 0x30))
1452 s = e0f4[21 + (s & 3)];
1461 LWS_VISIBLE LWS_EXTERN int
1462 lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
1466 static const char *slash = "/";
1468 /* cut up the location into address, port and path */
1470 while (*p && (*p != ':' || p[1] != '/' || p[2] != '/'))
1481 if (!strcmp(*prot, "http") || !strcmp(*prot, "ws"))
1483 else if (!strcmp(*prot, "https") || !strcmp(*prot, "wss"))
1486 while (*p && *p != ':' && *p != '/')
1491 while (*p && *p != '/')
1504 #ifdef LWS_NO_EXTENSIONS
1506 /* we need to provide dummy callbacks for internal exts
1507 * so user code runs when faced with a lib compiled with
1508 * extensions disabled.
1512 lws_extension_callback_pm_deflate(struct lws_context *context,
1513 const struct lws_extension *ext,
1515 enum lws_extension_callback_reasons reason,
1516 void *user, void *in, size_t len)
1531 lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
1535 #ifdef LWS_USE_UNIX_SOCK
1536 struct sockaddr_un serv_unix;
1539 struct sockaddr_in6 serv_addr6;
1541 struct sockaddr_in serv_addr4;
1542 #ifndef LWS_PLAT_OPTEE
1543 socklen_t len = sizeof(struct sockaddr);
1546 struct sockaddr_in sin;
1549 #ifdef LWS_USE_UNIX_SOCK
1550 if (LWS_UNIX_SOCK_ENABLED(vhost)) {
1551 v = (struct sockaddr *)&serv_unix;
1552 n = sizeof(struct sockaddr_un);
1553 bzero((char *) &serv_unix, sizeof(serv_unix));
1554 serv_unix.sun_family = AF_UNIX;
1555 if (sizeof(serv_unix.sun_path) <= strlen(iface)) {
1556 lwsl_err("\"%s\" too long for UNIX domain socket\n",
1560 strcpy(serv_unix.sun_path, iface);
1561 if (serv_unix.sun_path[0] == '@')
1562 serv_unix.sun_path[0] = '\0';
1567 if (LWS_IPV6_ENABLED(vhost)) {
1568 v = (struct sockaddr *)&serv_addr6;
1569 n = sizeof(struct sockaddr_in6);
1570 bzero((char *) &serv_addr6, sizeof(serv_addr6));
1572 interface_to_sa(vhost, iface,
1573 (struct sockaddr_in *)v, n) < 0) {
1574 lwsl_err("Unable to find interface %s\n", iface);
1579 struct ifaddrs *addrs, *addr;
1580 char ip[NI_MAXHOST];
1584 for (addr = addrs; addr; addr = addr->ifa_next) {
1585 if (!addr->ifa_addr ||
1586 addr->ifa_addr->sa_family != AF_INET6)
1589 getnameinfo(addr->ifa_addr,
1590 sizeof(struct sockaddr_in6),
1592 NULL, 0, NI_NUMERICHOST);
1596 if (ip[i++] == '%') {
1601 if (!strcmp(ip, iface)) {
1602 serv_addr6.sin6_scope_id =
1603 if_nametoindex(addr->ifa_name);
1610 serv_addr6.sin6_family = AF_INET6;
1611 serv_addr6.sin6_port = htons(port);
1615 v = (struct sockaddr *)&serv_addr4;
1616 n = sizeof(serv_addr4);
1617 bzero((char *) &serv_addr4, sizeof(serv_addr4));
1618 serv_addr4.sin_addr.s_addr = INADDR_ANY;
1619 serv_addr4.sin_family = AF_INET;
1622 interface_to_sa(vhost, iface,
1623 (struct sockaddr_in *)v, n) < 0) {
1624 lwsl_err("Unable to find interface %s\n", iface);
1628 serv_addr4.sin_port = htons(port);
1631 n = bind(sockfd, v, n);
1632 #ifdef LWS_USE_UNIX_SOCK
1633 if (n < 0 && LWS_UNIX_SOCK_ENABLED(vhost)) {
1634 lwsl_err("ERROR on binding fd %d to \"%s\" (%d %d)\n",
1635 sockfd, iface, n, LWS_ERRNO);
1640 lwsl_err("ERROR on binding fd %d to port %d (%d %d)\n",
1641 sockfd, port, n, LWS_ERRNO);
1645 #ifndef LWS_PLAT_OPTEE
1646 if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
1647 lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
1650 port = ntohs(sin.sin_port);
1657 lws_restart_ws_ping_pong_timer(struct lws *wsi)
1659 if (!wsi->context->ws_ping_pong_interval)
1661 if (wsi->state != LWSS_ESTABLISHED)
1664 wsi->u.ws.time_next_ping_check = (time_t)lws_now_secs() +
1665 wsi->context->ws_ping_pong_interval;
1668 static const char *hex = "0123456789ABCDEF";
1670 LWS_VISIBLE LWS_EXTERN const char *
1671 lws_sql_purify(char *escaped, const char *string, int len)
1673 const char *p = string;
1676 while (*p && len-- > 2) {
1690 LWS_VISIBLE LWS_EXTERN const char *
1691 lws_json_purify(char *escaped, const char *string, int len)
1693 const char *p = string;
1701 while (*p && len-- > 6) {
1702 if (*p == '\"' || *p == '\\' || *p < 0x20) {
1707 *q++ = hex[((*p) >> 4) & 15];
1708 *q++ = hex[(*p) & 15];
1719 LWS_VISIBLE LWS_EXTERN const char *
1720 lws_urlencode(char *escaped, const char *string, int len)
1722 const char *p = string;
1725 while (*p && len-- > 3) {
1731 if ((*p >= '0' && *p <= '9') ||
1732 (*p >= 'A' && *p <= 'Z') ||
1733 (*p >= 'a' && *p <= 'z')) {
1738 *q++ = hex[(*p >> 4) & 0xf];
1739 *q++ = hex[*p & 0xf];
1749 LWS_VISIBLE LWS_EXTERN int
1750 lws_urldecode(char *string, const char *escaped, int len)
1755 while (*escaped && len) {
1758 if (*escaped == '%') {
1763 if (*escaped == '+') {
1769 *string++ = *escaped++;
1773 n = char_to_hex(*escaped);
1782 n = char_to_hex(*escaped);
1786 *string++ = sum | n;
1798 LWS_VISIBLE LWS_EXTERN int
1799 lws_finalize_startup(struct lws_context *context)
1801 struct lws_context_creation_info info;
1803 info.uid = context->uid;
1804 info.gid = context->gid;
1806 if (lws_check_opt(context->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
1807 lws_plat_drop_app_privileges(&info);
1813 lws_snprintf(char *str, size_t size, const char *format, ...)
1821 va_start(ap, format);
1822 n = vsnprintf(str, size, format, ap);
1832 LWS_VISIBLE LWS_EXTERN int
1833 lws_is_cgi(struct lws *wsi) {
1844 urlencode(const char *in, int inlen, char *out, int outlen)
1846 char *start = out, *end = out + outlen;
1848 while (inlen-- && out < end - 4) {
1849 if ((*in >= 'A' && *in <= 'Z') ||
1850 (*in >= 'a' && *in <= 'z') ||
1851 (*in >= '0' && *in <= '9') ||
1865 *out++ = hex[(*in) >> 4];
1866 *out++ = hex[(*in++) & 15];
1877 lws_create_basic_wsi(struct lws_context *context, int tsi)
1879 struct lws *new_wsi;
1881 if ((unsigned int)context->pt[tsi].fds_count ==
1882 context->fd_limit_per_thread - 1) {
1883 lwsl_err("no space for new conn\n");
1887 new_wsi = lws_zalloc(sizeof(struct lws));
1888 if (new_wsi == NULL) {
1889 lwsl_err("Out of memory for new connection\n");
1894 new_wsi->context = context;
1895 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
1896 new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
1898 /* initialize the instance struct */
1900 new_wsi->state = LWSS_CGI;
1901 new_wsi->mode = LWSCM_CGI;
1902 new_wsi->hdr_parsing_completed = 0;
1903 new_wsi->position_in_fds_table = -1;
1906 * these can only be set once the protocol is known
1907 * we set an unestablished connection's protocol pointer
1908 * to the start of the defauly vhost supported list, so it can look
1909 * for matching ones during the handshake
1911 new_wsi->protocol = context->vhost_list->protocols;
1912 new_wsi->user_space = NULL;
1913 new_wsi->ietf_spec_revision = 0;
1914 new_wsi->sock = LWS_SOCK_INVALID;
1915 context->count_wsi_allocated++;
1920 LWS_VISIBLE LWS_EXTERN int
1921 lws_cgi(struct lws *wsi, const char * const *exec_array, int script_uri_path_len,
1922 int timeout_secs, const struct lws_protocol_vhost_options *mp_cgienv)
1924 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
1925 char *env_array[30], cgi_path[400], e[1024], *p = e,
1926 *end = p + sizeof(e) - 1, tok[256], *t;
1927 struct lws_cgi *cgi;
1928 int n, m, i, uritok = -1;
1931 * give the master wsi a cgi struct
1934 wsi->cgi = lws_zalloc(sizeof(*wsi->cgi));
1936 lwsl_err("%s: OOM\n", __func__);
1941 cgi->wsi = wsi; /* set cgi's owning wsi */
1943 /* create pipes for [stdin|stdout] and [stderr] */
1945 for (n = 0; n < 3; n++)
1946 if (pipe(cgi->pipe_fds[n]) == -1)
1949 /* create cgi wsis for each stdin/out/err fd */
1951 for (n = 0; n < 3; n++) {
1952 cgi->stdwsi[n] = lws_create_basic_wsi(wsi->context, wsi->tsi);
1953 if (!cgi->stdwsi[n])
1955 cgi->stdwsi[n]->cgi_channel = n;
1956 cgi->stdwsi[n]->vhost = wsi->vhost;
1958 // lwsl_err("%s: cgi %p: pipe fd %d -> fd %d / %d\n", __func__, wsi, n,
1959 // cgi->pipe_fds[n][!!(n == 0)], cgi->pipe_fds[n][!(n == 0)]);
1961 /* read side is 0, stdin we want the write side, others read */
1962 cgi->stdwsi[n]->sock = cgi->pipe_fds[n][!!(n == 0)];
1963 if (fcntl(cgi->pipe_fds[n][!!(n == 0)], F_SETFL, O_NONBLOCK) < 0) {
1964 lwsl_err("%s: setting NONBLOCK failed\n", __func__);
1969 for (n = 0; n < 3; n++) {
1970 lws_libuv_accept(cgi->stdwsi[n], cgi->stdwsi[n]->sock);
1971 if (insert_wsi_socket_into_fds(wsi->context, cgi->stdwsi[n]))
1973 cgi->stdwsi[n]->parent = wsi;
1974 cgi->stdwsi[n]->sibling_list = wsi->child_list;
1975 wsi->child_list = cgi->stdwsi[n];
1978 lws_change_pollfd(cgi->stdwsi[LWS_STDIN], LWS_POLLIN, LWS_POLLOUT);
1979 lws_change_pollfd(cgi->stdwsi[LWS_STDOUT], LWS_POLLOUT, LWS_POLLIN);
1980 lws_change_pollfd(cgi->stdwsi[LWS_STDERR], LWS_POLLOUT, LWS_POLLIN);
1982 lwsl_debug("%s: fds in %d, out %d, err %d\n", __func__,
1983 cgi->stdwsi[LWS_STDIN]->sock, cgi->stdwsi[LWS_STDOUT]->sock,
1984 cgi->stdwsi[LWS_STDERR]->sock);
1986 lws_set_timeout(wsi, PENDING_TIMEOUT_CGI, timeout_secs);
1988 /* the cgi stdout is always sending us http1.x header data first */
1989 wsi->hdr_state = LCHS_HEADER;
1991 /* add us to the pt list of active cgis */
1992 lwsl_debug("%s: adding cgi %p to list\n", __func__, wsi->cgi);
1993 cgi->cgi_list = pt->cgi_list;
1996 /* prepare his CGI env */
2000 if (lws_is_ssl(wsi))
2001 env_array[n++] = "HTTPS=ON";
2002 if (wsi->u.hdr.ah) {
2003 static const unsigned char meths[] = {
2006 WSI_TOKEN_OPTIONS_URI,
2008 WSI_TOKEN_PATCH_URI,
2009 WSI_TOKEN_DELETE_URI,
2011 static const char * const meth_names[] = {
2012 "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE",
2015 for (m = 0; m < ARRAY_SIZE(meths); m++)
2016 if (lws_hdr_total_length(wsi, meths[m]) >=
2017 script_uri_path_len) {
2025 lws_snprintf(cgi_path, sizeof(cgi_path) - 1, "REQUEST_URI=%s",
2026 lws_hdr_simple_ptr(wsi, uritok));
2027 cgi_path[sizeof(cgi_path) - 1] = '\0';
2028 env_array[n++] = cgi_path;
2031 p += lws_snprintf(p, end - p, "REQUEST_METHOD=%s",
2036 p += lws_snprintf(p, end - p, "QUERY_STRING=");
2037 /* dump the individual URI Arg parameters */
2040 i = lws_hdr_copy_fragment(wsi, tok, sizeof(tok),
2041 WSI_TOKEN_HTTP_URI_ARGS, m);
2045 while (*t && *t != '=' && p < end - 4)
2049 i = urlencode(t, i- (t - tok), p, end - p);
2061 p += lws_snprintf(p, end - p, "PATH_INFO=%s",
2062 lws_hdr_simple_ptr(wsi, uritok) +
2063 script_uri_path_len);
2066 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_REFERER)) {
2068 p += lws_snprintf(p, end - p, "HTTP_REFERER=%s",
2069 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_REFERER));
2072 if (lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
2074 p += lws_snprintf(p, end - p, "HTTP_HOST=%s",
2075 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
2078 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) {
2080 p += lws_snprintf(p, end - p, "HTTP_COOKIE=%s",
2081 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COOKIE));
2084 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT)) {
2086 p += lws_snprintf(p, end - p, "USER_AGENT=%s",
2087 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_USER_AGENT));
2090 if (uritok == WSI_TOKEN_POST_URI) {
2091 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE)) {
2093 p += lws_snprintf(p, end - p, "CONTENT_TYPE=%s",
2094 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE));
2097 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
2099 p += lws_snprintf(p, end - p, "CONTENT_LENGTH=%s",
2100 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH));
2105 p += lws_snprintf(p, end - p, "SCRIPT_PATH=%s", exec_array[0]) + 1;
2109 p += lws_snprintf(p, end - p, "%s=%s", mp_cgienv->name,
2111 lwsl_debug(" Applying mount-specific cgi env '%s'\n",
2114 mp_cgienv = mp_cgienv->next;
2117 env_array[n++] = "SERVER_SOFTWARE=libwebsockets";
2118 env_array[n++] = "PATH=/bin:/usr/bin:/usr/local/bin:/var/www/cgi-bin";
2119 env_array[n] = NULL;
2122 for (m = 0; m < n; m++)
2123 lwsl_err(" %s\n", env_array[m]);
2127 * Actually having made the env, as a cgi we don't need the ah
2130 if (wsi->u.hdr.ah->rxpos == wsi->u.hdr.ah->rxlen)
2131 lws_header_table_detach(wsi, 0);
2133 /* we are ready with the redirection pipes... run the thing */
2134 #if !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
2140 lwsl_err("fork failed, errno %d", errno);
2144 #if defined(__linux__)
2145 prctl(PR_SET_PDEATHSIG, SIGTERM);
2147 setpgrp(); /* stops on-daemonized main processess getting SIGINT from TTY */
2150 /* we are the parent process */
2151 wsi->context->count_cgi_spawned++;
2152 lwsl_debug("%s: cgi %p spawned PID %d\n", __func__, cgi, cgi->pid);
2156 /* somewhere we can at least read things and enter it */
2158 lwsl_notice("%s: Failed to chdir\n", __func__);
2160 /* We are the forked process, redirect and kill inherited things.
2162 * Because of vfork(), we cannot do anything that changes pages in
2163 * the parent environment. Stuff that changes kernel state for the
2164 * process is OK. Stuff that happens after the execvpe() is OK.
2167 for (n = 0; n < 3; n++)
2168 if (dup2(cgi->pipe_fds[n][!(n == 0)], n) < 0) {
2169 lwsl_err("%s: stdin dup2 failed\n", __func__);
2173 #if !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
2174 for (m = 0; m < n; m++) {
2175 p = strchr(env_array[m], '=');
2177 setenv(env_array[m], p, 1);
2179 execvp(exec_array[0], (char * const *)&exec_array[0]);
2181 execvpe(exec_array[0], (char * const *)&exec_array[0], &env_array[0]);
2187 /* drop us from the pt cgi list */
2188 pt->cgi_list = cgi->cgi_list;
2191 remove_wsi_socket_from_fds(wsi->cgi->stdwsi[n]);
2193 for (n = 0; n < 3; n++)
2194 if (wsi->cgi->stdwsi[n])
2195 lws_free_wsi(cgi->stdwsi[n]);
2198 for (n = 0; n < 3; n++) {
2199 if (cgi->pipe_fds[n][0])
2200 close(cgi->pipe_fds[n][0]);
2201 if (cgi->pipe_fds[n][1])
2202 close(cgi->pipe_fds[n][1]);
2205 lws_free_set_NULL(wsi->cgi);
2207 lwsl_err("%s: failed\n", __func__);
2212 LWS_VISIBLE LWS_EXTERN int
2213 lws_cgi_write_split_stdout_headers(struct lws *wsi)
2215 int n, m, match = 0, lp = 0;
2216 static const char * const content_length = "content-length: ";
2217 char buf[LWS_PRE + 1024], *start = &buf[LWS_PRE], *p = start,
2218 *end = &buf[sizeof(buf) - 1 - LWS_PRE], c, l[12];
2223 while (wsi->hdr_state != LHCS_PAYLOAD) {
2224 /* we have to separate header / finalize and
2225 * payload chunks, since they need to be
2226 * handled separately
2228 n = read(lws_get_socket_fd(wsi->cgi->stdwsi[LWS_STDOUT]), &c, 1);
2230 if (errno != EAGAIN) {
2231 lwsl_debug("%s: read says %d\n", __func__, n);
2238 lwsl_debug("-- 0x%02X %c\n", (unsigned char)c, c);
2239 switch (wsi->hdr_state) {
2241 if (!content_length[match] &&
2242 (c >= '0' && c <= '9') &&
2243 lp < sizeof(l) - 1) {
2246 wsi->cgi->content_length = atol(l);
2248 if (tolower(c) == content_length[match])
2253 /* some cgi only send us \x0a for EOL */
2255 wsi->hdr_state = LCHS_SINGLE_0A;
2260 wsi->hdr_state = LCHS_LF1;
2268 wsi->hdr_state = LCHS_CR2;
2271 /* we got \r[^\n]... it's unreasonable */
2272 lwsl_debug("%s: funny CRLF 0x%02X\n", __func__, (unsigned char)c);
2278 wsi->hdr_state = LCHS_LF2;
2281 wsi->hdr_state = LCHS_HEADER;
2286 case LCHS_SINGLE_0A:
2289 lwsl_debug("Content-Length: %ld\n", wsi->cgi->content_length);
2290 wsi->hdr_state = LHCS_PAYLOAD;
2291 /* drop the \0xa ... finalize will add it if needed */
2292 if (lws_finalize_http_header(wsi,
2293 (unsigned char **)&p,
2294 (unsigned char *)end))
2299 /* we got \r\n\r[^\n]... it's unreasonable */
2301 /* we got \x0anext header, it's reasonable */
2303 wsi->hdr_state = LCHS_HEADER;
2310 /* ran out of input, ended the headers, or filled up the headers buf */
2311 if (!n || wsi->hdr_state == LHCS_PAYLOAD || (p + 4) == end) {
2313 m = lws_write(wsi, (unsigned char *)start,
2314 p - start, LWS_WRITE_HTTP_HEADERS);
2316 lwsl_debug("%s: write says %d\n", __func__, m);
2319 /* writeability becomes uncertain now we wrote
2320 * something, we must return to the event loop
2327 n = read(lws_get_socket_fd(wsi->cgi->stdwsi[LWS_STDOUT]),
2328 start, sizeof(buf) - LWS_PRE);
2330 if (n < 0 && errno != EAGAIN) {
2331 lwsl_debug("%s: stdout read says %d\n", __func__, n);
2335 m = lws_write(wsi, (unsigned char *)start, n, LWS_WRITE_HTTP);
2336 //lwsl_notice("write %d\n", m);
2338 lwsl_debug("%s: stdout write says %d\n", __func__, m);
2341 wsi->cgi->content_length_seen += m;
2347 LWS_VISIBLE LWS_EXTERN int
2348 lws_cgi_kill(struct lws *wsi)
2350 struct lws_cgi_args args;
2353 lwsl_debug("%s: %p\n", __func__, wsi);
2358 if (wsi->cgi->pid > 0) {
2359 n = waitpid(wsi->cgi->pid, &status, WNOHANG);
2361 lwsl_debug("%s: PID %d reaped\n", __func__,
2365 /* kill the process group */
2366 n = kill(-wsi->cgi->pid, SIGTERM);
2367 lwsl_debug("%s: SIGTERM child PID %d says %d (errno %d)\n", __func__,
2368 wsi->cgi->pid, n, errno);
2371 * hum seen errno=3 when process is listed in ps,
2372 * it seems we don't always retain process grouping
2374 * Direct these fallback attempt to the exact child
2376 n = kill(wsi->cgi->pid, SIGTERM);
2378 n = kill(wsi->cgi->pid, SIGPIPE);
2380 n = kill(wsi->cgi->pid, SIGKILL);
2382 lwsl_err("%s: SIGKILL PID %d failed errno %d (maybe zombie)\n",
2383 __func__, wsi->cgi->pid, errno);
2387 /* He could be unkillable because he's a zombie */
2390 n = waitpid(-wsi->cgi->pid, &status, WNOHANG);
2392 lwsl_debug("%s: reaped PID %d\n", __func__, n);
2394 n = waitpid(wsi->cgi->pid, &status, WNOHANG);
2396 lwsl_debug("%s: reaped PID %d\n", __func__, n);
2402 args.stdwsi = &wsi->cgi->stdwsi[0];
2404 if (wsi->cgi->pid != -1 && user_callback_handle_rxflow(
2405 wsi->protocol->callback,
2406 wsi, LWS_CALLBACK_CGI_TERMINATED,
2408 (void *)&args, 0)) {
2410 if (!wsi->cgi->being_closed)
2411 lws_close_free_wsi(wsi, 0);
2418 lws_cgi_kill_terminated(struct lws_context_per_thread *pt)
2420 struct lws_cgi **pcgi, *cgi = NULL;
2424 /* find finished guys but don't reap yet */
2425 n = waitpid(-1, &status, WNOHANG);
2428 lwsl_debug("%s: observed PID %d terminated\n", __func__, n);
2430 pcgi = &pt->cgi_list;
2432 /* check all the subprocesses on the cgi list */
2434 /* get the next one first as list may change */
2436 pcgi = &(*pcgi)->cgi_list;
2441 /* wait for stdout to be drained */
2442 if (cgi->content_length > cgi->content_length_seen)
2445 if (cgi->content_length) {
2446 lwsl_debug("%s: wsi %p: expected content length seen: %ld\n",
2447 __func__, cgi->wsi, cgi->content_length_seen);
2451 waitpid(n, &status, WNOHANG);
2453 * he's already terminated so no need for kill()
2454 * but we should do the terminated cgi callback
2455 * and close him if he's not already closing
2457 if (n == cgi->pid) {
2458 lwsl_debug("%s: found PID %d on cgi list\n",
2461 if (!cgi->content_length) {
2463 * well, if he sends chunked... give him 5s after the
2464 * cgi terminated to send buffered
2466 cgi->chunked_grace++;
2472 lws_cgi_kill(cgi->wsi);
2478 /* if not found on the cgi list, as he's one of ours, reap */
2480 lwsl_debug("%s: reading PID %d although no cgi match\n",
2482 waitpid(n, &status, WNOHANG);
2486 /* disable this to confirm timeout cgi cleanup flow */
2488 pcgi = &pt->cgi_list;
2490 /* check all the subprocesses on the cgi list */
2492 /* get the next one first as list may change */
2494 pcgi = &(*pcgi)->cgi_list;
2499 /* we deferred killing him after reaping his PID */
2500 if (cgi->chunked_grace) {
2501 cgi->chunked_grace++;
2502 if (cgi->chunked_grace < 5)
2507 /* wait for stdout to be drained */
2508 if (cgi->content_length > cgi->content_length_seen)
2511 if (cgi->content_length)
2512 lwsl_debug("%s: wsi %p: expected content length seen: %ld\n",
2513 __func__, cgi->wsi, cgi->content_length_seen);
2516 if (waitpid(cgi->pid, &status, WNOHANG) > 0) {
2518 if (!cgi->content_length) {
2520 * well, if he sends chunked... give him 5s after the
2521 * cgi terminated to send buffered
2523 cgi->chunked_grace++;
2527 lwsl_debug("%s: found PID %d on cgi list\n",
2528 __func__, cgi->pid);
2531 lws_cgi_kill(cgi->wsi);
2538 /* general anti zombie defence */
2539 // n = waitpid(-1, &status, WNOHANG);
2541 // lwsl_notice("%s: anti-zombie wait says %d\n", __func__, n);
2547 #ifdef LWS_NO_EXTENSIONS
2549 lws_set_extension_option(struct lws *wsi, const char *ext_name,
2550 const char *opt_name, const char *opt_val)
2556 #ifdef LWS_WITH_ACCESS_LOG
2558 lws_access_log(struct lws *wsi)
2560 char *p = wsi->access_log.user_agent, ass[512];
2563 if (!wsi->access_log_pending)
2566 if (!wsi->access_log.header_log)
2572 l = lws_snprintf(ass, sizeof(ass) - 1, "%s %d %lu %s\n",
2573 wsi->access_log.header_log,
2574 wsi->access_log.response, wsi->access_log.sent, p);
2576 if (wsi->vhost->log_fd != (int)LWS_INVALID_FILE) {
2577 if (write(wsi->vhost->log_fd, ass, l) != l)
2578 lwsl_err("Failed to write log\n");
2580 lwsl_err("%s", ass);
2582 if (wsi->access_log.header_log) {
2583 lws_free(wsi->access_log.header_log);
2584 wsi->access_log.header_log = NULL;
2586 if (wsi->access_log.user_agent) {
2587 lws_free(wsi->access_log.user_agent);
2588 wsi->access_log.user_agent = NULL;
2590 wsi->access_log_pending = 0;
2597 lws_sum_stats(const struct lws_context *ctx, struct lws_conn_stats *cs)
2599 const struct lws_vhost *vh = ctx->vhost_list;
2603 cs->rx += vh->conn_stats.rx;
2604 cs->tx += vh->conn_stats.tx;
2605 cs->conn += vh->conn_stats.conn;
2606 cs->trans += vh->conn_stats.trans;
2607 cs->ws_upg += vh->conn_stats.ws_upg;
2608 cs->http2_upg += vh->conn_stats.http2_upg;
2609 cs->rejected += vh->conn_stats.rejected;
2611 vh = vh->vhost_next;
2615 #ifdef LWS_WITH_SERVER_STATUS
2618 lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len)
2620 static const char * const prots[] = {
2629 char *orig = buf, *end = buf + len - 1, first = 1;
2635 buf += lws_snprintf(buf, end - buf,
2636 "{\n \"name\":\"%s\",\n"
2637 " \"port\":\"%d\",\n"
2638 " \"use_ssl\":\"%d\",\n"
2639 " \"sts\":\"%d\",\n"
2640 " \"rx\":\"%llu\",\n"
2641 " \"tx\":\"%llu\",\n"
2642 " \"conn\":\"%lu\",\n"
2643 " \"trans\":\"%lu\",\n"
2644 " \"ws_upg\":\"%lu\",\n"
2645 " \"rejected\":\"%lu\",\n"
2646 " \"http2_upg\":\"%lu\""
2648 vh->name, vh->listen_port,
2649 #ifdef LWS_OPENSSL_SUPPORT
2654 !!(vh->options & LWS_SERVER_OPTION_STS),
2655 vh->conn_stats.rx, vh->conn_stats.tx,
2656 vh->conn_stats.conn, vh->conn_stats.trans,
2657 vh->conn_stats.ws_upg,
2658 vh->conn_stats.rejected,
2659 vh->conn_stats.http2_upg
2662 if (vh->mount_list) {
2663 const struct lws_http_mount *m = vh->mount_list;
2665 buf += lws_snprintf(buf, end - buf, ",\n \"mounts\":[");
2668 buf += lws_snprintf(buf, end - buf, ",");
2669 buf += lws_snprintf(buf, end - buf,
2670 "\n {\n \"mountpoint\":\"%s\",\n"
2671 " \"origin\":\"%s%s\",\n"
2672 " \"cache_max_age\":\"%d\",\n"
2673 " \"cache_reuse\":\"%d\",\n"
2674 " \"cache_revalidate\":\"%d\",\n"
2675 " \"cache_intermediaries\":\"%d\"\n"
2678 prots[m->origin_protocol],
2682 m->cache_revalidate,
2683 m->cache_intermediaries);
2685 buf += lws_snprintf(buf, end - buf,
2686 ",\n \"default\":\"%s\"",
2688 buf += lws_snprintf(buf, end - buf, "\n }");
2692 buf += lws_snprintf(buf, end - buf, "\n ]");
2695 if (vh->protocols) {
2699 buf += lws_snprintf(buf, end - buf, ",\n \"ws-protocols\":[");
2700 while (n < vh->count_protocols) {
2702 buf += lws_snprintf(buf, end - buf, ",");
2703 buf += lws_snprintf(buf, end - buf,
2705 " \"status\":\"ok\"\n }\n }"
2707 vh->protocols[n].name);
2711 buf += lws_snprintf(buf, end - buf, "\n ]");
2714 buf += lws_snprintf(buf, end - buf, "\n}");
2720 LWS_EXTERN LWS_VISIBLE int
2721 lws_json_dump_context(const struct lws_context *context, char *buf, int len,
2724 char *orig = buf, *end = buf + len - 1, first = 1;
2725 const struct lws_vhost *vh = context->vhost_list;
2726 const struct lws_context_per_thread *pt;
2727 time_t t = time(NULL);
2728 int n, listening = 0, cgi_count = 0;
2729 struct lws_conn_stats cs;
2732 struct lws_cgi * const *pcgi;
2735 #ifdef LWS_USE_LIBUV
2739 buf += lws_snprintf(buf, end - buf, "{ "
2740 "\"version\":\"%s\",\n"
2741 "\"uptime\":\"%ld\",\n",
2742 lws_get_library_version(),
2745 #ifdef LWS_HAVE_GETLOADAVG
2750 m = getloadavg(d, 3);
2751 for (n = 0; n < m; n++) {
2752 buf += lws_snprintf(buf, end - buf,
2753 "\"l%d\":\"%.2f\",\n",
2759 buf += lws_snprintf(buf, end - buf, "\"contexts\":[\n");
2761 buf += lws_snprintf(buf, end - buf, "{ "
2762 "\"context_uptime\":\"%ld\",\n"
2763 "\"cgi_spawned\":\"%d\",\n"
2764 "\"pt_fd_max\":\"%d\",\n"
2765 "\"ah_pool_max\":\"%d\",\n"
2766 "\"deprecated\":\"%d\",\n"
2767 "\"wsi_alive\":\"%d\",\n",
2768 (unsigned long)(t - context->time_up),
2769 context->count_cgi_spawned,
2770 context->fd_limit_per_thread,
2771 context->max_http_header_pool,
2772 context->deprecated,
2773 context->count_wsi_allocated);
2775 buf += lws_snprintf(buf, end - buf, "\"pt\":[\n ");
2776 for (n = 0; n < context->count_threads; n++) {
2777 pt = &context->pt[n];
2779 buf += lws_snprintf(buf, end - buf, ",");
2780 buf += lws_snprintf(buf, end - buf,
2782 " \"fds_count\":\"%d\",\n"
2783 " \"ah_pool_inuse\":\"%d\",\n"
2784 " \"ah_wait_list\":\"%d\"\n"
2787 pt->ah_count_in_use,
2788 pt->ah_wait_list_length);
2791 buf += lws_snprintf(buf, end - buf, "]");
2793 buf += lws_snprintf(buf, end - buf, ", \"vhosts\":[\n ");
2796 vh = context->vhost_list;
2798 cs = context->conn_stats;
2799 lws_sum_stats(context, &cs);
2806 buf += lws_json_dump_vhost(vh, buf, end - buf);
2811 vh = vh->vhost_next;
2814 buf += lws_snprintf(buf, end - buf,
2815 "],\n\"listen_wsi\":\"%d\",\n"
2816 " \"rx\":\"%llu\",\n"
2817 " \"tx\":\"%llu\",\n"
2818 " \"conn\":\"%lu\",\n"
2819 " \"trans\":\"%lu\",\n"
2820 " \"ws_upg\":\"%lu\",\n"
2821 " \"rejected\":\"%lu\",\n"
2822 " \"http2_upg\":\"%lu\"",
2824 cs.rx, cs.tx, cs.conn, cs.trans,
2825 cs.ws_upg, cs.rejected, cs.http2_upg);
2828 for (n = 0; n < context->count_threads; n++) {
2829 pt = &context->pt[n];
2830 pcgi = &pt->cgi_list;
2833 pcgi = &(*pcgi)->cgi_list;
2839 buf += lws_snprintf(buf, end - buf, ",\n \"cgi_alive\":\"%d\"\n ",
2842 buf += lws_snprintf(buf, end - buf, "}");
2845 buf += lws_snprintf(buf, end - buf, "]}\n ");