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_RAW) {
225 wsi->vhost->protocols->callback(wsi,
226 LWS_CALLBACK_RAW_CLOSE, wsi->user_space, NULL, 0);
227 wsi->socket_is_permanently_unusable = 1;
228 goto just_kill_connection;
231 if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED &&
232 wsi->u.http.fop_fd != NULL) {
233 lws_plat_file_close(wsi->u.http.fop_fd);
234 wsi->u.http.fop_fd = NULL;
235 wsi->vhost->protocols->callback(wsi,
236 LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
237 wsi->told_user_closed = 1;
239 if (wsi->socket_is_permanently_unusable ||
240 reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY ||
241 wsi->state == LWSS_SHUTDOWN)
242 goto just_kill_connection;
244 wsi->state_pre_close = wsi->state;
246 switch (wsi->state_pre_close) {
247 case LWSS_DEAD_SOCKET:
250 /* we tried the polite way... */
251 case LWSS_AWAITING_CLOSE_ACK:
252 goto just_kill_connection;
254 case LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE:
255 if (wsi->trunc_len) {
256 lws_callback_on_writable(wsi);
259 lwsl_info("wsi %p completed LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
260 goto just_kill_connection;
262 if (wsi->trunc_len) {
263 lwsl_info("wsi %p entering LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE\n", wsi);
264 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
265 lws_set_timeout(wsi, PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5);
271 if (wsi->mode == LWSCM_WSCL_WAITING_CONNECT ||
272 wsi->mode == LWSCM_WSCL_ISSUE_HANDSHAKE)
273 goto just_kill_connection;
275 if (wsi->mode == LWSCM_HTTP_SERVING) {
277 wsi->vhost->protocols->callback(wsi,
278 LWS_CALLBACK_HTTP_DROP_PROTOCOL,
279 wsi->user_space, NULL, 0);
280 wsi->vhost->protocols->callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
281 wsi->user_space, NULL, 0);
282 wsi->told_user_closed = 1;
284 if (wsi->mode & LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP) {
285 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_CLOSED_CLIENT_HTTP,
286 wsi->user_space, NULL, 0);
287 wsi->told_user_closed = 1;
291 * are his extensions okay with him closing? Eg he might be a mux
292 * parent and just his ch1 aspect is closing?
295 if (lws_ext_cb_active(wsi,
296 LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE, NULL, 0) > 0) {
297 lwsl_ext("extension vetoed close\n");
302 * flush any tx pending from extensions, since we may send close packet
303 * if there are problems with send, just nuke the connection
308 eff_buf.token = NULL;
309 eff_buf.token_len = 0;
311 /* show every extension the new incoming data */
313 m = lws_ext_cb_active(wsi,
314 LWS_EXT_CB_FLUSH_PENDING_TX, &eff_buf, 0);
316 lwsl_ext("Extension reports fatal error\n");
317 goto just_kill_connection;
321 * at least one extension told us he has more
322 * to spill, so we will go around again after
326 /* assuming they left us something to send, send it */
328 if (eff_buf.token_len)
329 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
330 eff_buf.token_len) !=
332 lwsl_debug("close: ext spill failed\n");
333 goto just_kill_connection;
338 * signal we are closing, lws_write will
339 * add any necessary version-specific stuff. If the write fails,
340 * no worries we are closing anyway. If we didn't initiate this
341 * close, then our state has been changed to
342 * LWSS_RETURNED_CLOSE_ALREADY and we will skip this.
344 * Likewise if it's a second call to close this connection after we
345 * sent the close indication to the peer already, we are in state
346 * LWSS_AWAITING_CLOSE_ACK and will skip doing this a second time.
349 if (wsi->state_pre_close == LWSS_ESTABLISHED &&
350 (wsi->u.ws.close_in_ping_buffer_len || /* already a reason */
351 (reason != LWS_CLOSE_STATUS_NOSTATUS &&
352 (reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)))) {
353 lwsl_debug("sending close indication...\n");
355 /* if no prepared close reason, use 1000 and no aux data */
356 if (!wsi->u.ws.close_in_ping_buffer_len) {
357 wsi->u.ws.close_in_ping_buffer_len = 2;
358 wsi->u.ws.ping_payload_buf[LWS_PRE] =
359 (reason >> 8) & 0xff;
360 wsi->u.ws.ping_payload_buf[LWS_PRE + 1] =
364 #if defined (LWS_WITH_ESP8266)
365 wsi->close_is_pending_send_completion = 1;
367 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
368 wsi->u.ws.close_in_ping_buffer_len,
372 * we have sent a nice protocol level indication we
373 * now wish to close, we should not send anything more
375 wsi->state = LWSS_AWAITING_CLOSE_ACK;
378 * ...and we should wait for a reply for a bit
381 lws_set_timeout(wsi, PENDING_TIMEOUT_CLOSE_ACK, 1);
382 lwsl_debug("sent close indication, awaiting ack\n");
387 lwsl_info("close: sending close packet failed, hanging up\n");
389 /* else, the send failed and we should just hang up */
392 just_kill_connection:
395 /* detach ourselves from parent's child list */
396 pwsi = &wsi->parent->child_list;
399 //lwsl_notice("%s: detach %p from parent %p\n",
400 // __func__, wsi, wsi->parent);
401 *pwsi = wsi->sibling_list;
404 pwsi = &(*pwsi)->sibling_list;
407 lwsl_err("%s: failed to detach from parent\n",
411 /* manage the vhost same protocol list entry */
413 if (wsi->same_vh_protocol_prev) { // we are on the vh list
415 // make guy who pointed to us, point to what our next was pointing to
416 *wsi->same_vh_protocol_prev = wsi->same_vh_protocol_next;
418 // if we had a next guy...
419 if (wsi->same_vh_protocol_next)
420 // have him point back to our prev
421 wsi->same_vh_protocol_next->same_vh_protocol_prev =
422 wsi->same_vh_protocol_prev;
428 * Testing with ab shows that we have to stage the socket close when
429 * the system is under stress... shutdown any further TX, change the
430 * state to one that won't emit anything more, and wait with a timeout
431 * for the POLLIN to show a zero-size rx before coming back and doing
434 if (wsi->mode != LWSCM_RAW && wsi->state != LWSS_SHUTDOWN &&
435 wsi->state != LWSS_CLIENT_UNCONNECTED &&
436 reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY &&
437 !wsi->socket_is_permanently_unusable) {
438 #ifdef LWS_OPENSSL_SUPPORT
439 if (lws_is_ssl(wsi) && wsi->ssl)
441 lwsl_info("%s: shutting down SSL connection: %p (ssl %p, sock %d, state %d)\n", __func__, wsi, wsi->ssl, (int)(long)wsi->sock, wsi->state);
442 n = SSL_shutdown(wsi->ssl);
443 if (n == 0) /* Complete bidirectional SSL shutdown */
444 n = SSL_shutdown(wsi->ssl);
445 n = shutdown(wsi->sock, SHUT_WR);
450 lwsl_info("%s: shutting down connection: %p (sock %d, state %d)\n", __func__, wsi, (int)(long)wsi->sock, wsi->state);
451 n = shutdown(wsi->sock, SHUT_WR);
454 lwsl_debug("closing: shutdown (state %d) ret %d\n", wsi->state, LWS_ERRNO);
456 // This causes problems with disconnection when the events are half closing connection
457 // FD_READ | FD_CLOSE (33)
459 /* libuv: no event available to guarantee completion */
460 if (!LWS_LIBUV_ENABLED(context)) {
462 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
463 wsi->state = LWSS_SHUTDOWN;
464 lws_set_timeout(wsi, PENDING_TIMEOUT_SHUTDOWN_FLUSH,
465 context->timeout_secs);
472 lwsl_info("%s: real just_kill_connection: %p (sockfd %d)\n", __func__,
475 #ifdef LWS_WITH_HTTP_PROXY
477 lws_rewrite_destroy(wsi->rw);
482 * we won't be servicing or receiving anything further from this guy
483 * delete socket from the internal poll list if still present
485 lws_ssl_remove_wsi_from_buffered_list(wsi);
487 lws_remove_from_timeout_list(wsi);
489 /* checking return redundant since we anyway close */
490 if (wsi->sock != LWS_SOCK_INVALID)
491 remove_wsi_socket_from_fds(wsi);
493 #if defined(LWS_WITH_ESP8266)
494 espconn_disconnect(wsi->sock);
497 wsi->state = LWSS_DEAD_SOCKET;
499 lws_free_set_NULL(wsi->rxflow_buffer);
501 if (wsi->state_pre_close == LWSS_ESTABLISHED ||
502 wsi->mode == LWSCM_WS_SERVING ||
503 wsi->mode == LWSCM_WS_CLIENT) {
505 if (wsi->u.ws.rx_draining_ext) {
506 struct lws **w = &pt->rx_draining_ext_list;
508 wsi->u.ws.rx_draining_ext = 0;
509 /* remove us from context draining ext list */
512 *w = wsi->u.ws.rx_draining_ext_list;
515 w = &((*w)->u.ws.rx_draining_ext_list);
517 wsi->u.ws.rx_draining_ext_list = NULL;
520 if (wsi->u.ws.tx_draining_ext) {
521 struct lws **w = &pt->tx_draining_ext_list;
523 wsi->u.ws.tx_draining_ext = 0;
524 /* remove us from context draining ext list */
527 *w = wsi->u.ws.tx_draining_ext_list;
530 w = &((*w)->u.ws.tx_draining_ext_list);
532 wsi->u.ws.tx_draining_ext_list = NULL;
534 lws_free_set_NULL(wsi->u.ws.rx_ubuf);
536 if (wsi->trunc_alloc)
537 /* not going to be completed... nuke it */
538 lws_free_set_NULL(wsi->trunc_alloc);
540 wsi->u.ws.ping_payload_len = 0;
541 wsi->u.ws.ping_pending_flag = 0;
544 /* tell the user it's all over for this guy */
546 if (wsi->mode != LWSCM_RAW && wsi->protocol && wsi->protocol->callback &&
547 ((wsi->state_pre_close == LWSS_ESTABLISHED) ||
548 (wsi->state_pre_close == LWSS_RETURNED_CLOSE_ALREADY) ||
549 (wsi->state_pre_close == LWSS_AWAITING_CLOSE_ACK) ||
550 (wsi->state_pre_close == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) ||
551 (wsi->mode == LWSCM_WS_CLIENT && wsi->state_pre_close == LWSS_HTTP) ||
552 (wsi->mode == LWSCM_WS_SERVING && wsi->state_pre_close == LWSS_HTTP))) {
554 if (wsi->user_space) {
555 lwsl_debug("%s: doing LWS_CALLBACK_HTTP_DROP_PROTOCOL for %p prot %s", __func__, wsi, wsi->protocol->name);
556 wsi->protocol->callback(wsi,
557 LWS_CALLBACK_HTTP_DROP_PROTOCOL,
558 wsi->user_space, NULL, 0);
560 lwsl_debug("calling back CLOSED\n");
561 wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
562 wsi->user_space, NULL, 0);
563 } else if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED) {
564 lwsl_debug("calling back CLOSED_HTTP\n");
565 wsi->vhost->protocols->callback(wsi, LWS_CALLBACK_CLOSED_HTTP,
566 wsi->user_space, NULL, 0 );
567 } else if ((wsi->mode == LWSCM_WSCL_WAITING_SERVER_REPLY ||
568 wsi->mode == LWSCM_WSCL_WAITING_CONNECT) &&
569 !wsi->already_did_cce) {
570 wsi->vhost->protocols[0].callback(wsi,
571 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
572 wsi->user_space, NULL, 0);
574 lwsl_debug("not calling back closed mode=%d state=%d\n",
575 wsi->mode, wsi->state_pre_close);
577 /* deallocate any active extension contexts */
579 if (lws_ext_cb_active(wsi, LWS_EXT_CB_DESTROY, NULL, 0) < 0)
580 lwsl_warn("extension destruction failed\n");
582 * inform all extensions in case they tracked this guy out of band
583 * even though not active on him specifically
585 if (lws_ext_cb_all_exts(context, wsi,
586 LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING, NULL, 0) < 0)
587 lwsl_warn("ext destroy wsi failed\n");
589 wsi->socket_is_permanently_unusable = 1;
592 if (LWS_LIBUV_ENABLED(context)) {
593 lwsl_debug("%s: lws_libuv_closehandle: wsi %p\n", __func__, wsi);
594 /* libuv has to do his own close handle processing asynchronously */
595 lws_libuv_closehandle(wsi);
601 lws_close_free_wsi_final(wsi);
605 lws_close_free_wsi_final(struct lws *wsi)
609 if (!lws_ssl_close(wsi) && lws_socket_is_valid(wsi->sock)) {
611 //lwsl_err("*** closing sockfd %d\n", wsi->sock);
612 n = compatible_close(wsi->sock);
614 lwsl_debug("closing: close ret %d\n", LWS_ERRNO);
617 compatible_close(wsi->sock);
620 wsi->sock = LWS_SOCK_INVALID;
623 /* outermost destroy notification for wsi (user_space still intact) */
624 wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
625 wsi->user_space, NULL, 0);
629 for (n = 0; n < 6; n++) {
630 if (wsi->cgi->pipe_fds[n / 2][n & 1] == 0)
631 lwsl_err("ZERO FD IN CGI CLOSE");
633 if (wsi->cgi->pipe_fds[n / 2][n & 1] >= 0)
634 close(wsi->cgi->pipe_fds[n / 2][n & 1]);
644 LWS_VISIBLE LWS_EXTERN const char *
645 lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len)
647 int n = 0, sl = strlen(name);
649 while (lws_hdr_copy_fragment(wsi, buf, len,
650 WSI_TOKEN_HTTP_URI_ARGS, n) >= 0) {
652 if (!strncmp(buf, name, sl))
661 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
663 interface_to_sa(struct lws_vhost *vh, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
667 ipv6 = LWS_IPV6_ENABLED(vh);
671 return lws_interface_to_sa(ipv6, ifname, addr, addrlen);
675 #ifndef LWS_PLAT_OPTEE
678 lws_get_addresses(struct lws_vhost *vh, void *ads, char *name,
679 int name_len, char *rip, int rip_len)
682 struct addrinfo ai, *res;
683 struct sockaddr_in addr4;
688 addr4.sin_family = AF_UNSPEC;
691 if (LWS_IPV6_ENABLED(vh)) {
692 if (!lws_plat_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ads)->sin6_addr, rip, rip_len)) {
693 lwsl_err("inet_ntop: %s", strerror(LWS_ERRNO));
697 // Strip off the IPv4 to IPv6 header if one exists
698 if (strncmp(rip, "::ffff:", 7) == 0)
699 memmove(rip, rip + 7, strlen(rip) - 6);
701 getnameinfo((struct sockaddr *)ads,
702 sizeof(struct sockaddr_in6), name,
703 name_len, NULL, 0, 0);
709 struct addrinfo *result;
711 memset(&ai, 0, sizeof ai);
712 ai.ai_family = PF_UNSPEC;
713 ai.ai_socktype = SOCK_STREAM;
714 ai.ai_flags = AI_CANONNAME;
715 #if !defined(LWS_WITH_ESP32)
716 if (getnameinfo((struct sockaddr *)ads,
717 sizeof(struct sockaddr_in),
718 name, name_len, NULL, 0, 0))
724 if (getaddrinfo(name, NULL, &ai, &result))
728 while (addr4.sin_family == AF_UNSPEC && res) {
729 switch (res->ai_family) {
731 addr4.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
732 addr4.sin_family = AF_INET;
738 freeaddrinfo(result);
741 if (addr4.sin_family == AF_UNSPEC)
744 if (lws_plat_inet_ntop(AF_INET, &addr4.sin_addr, rip, rip_len) == NULL)
762 LWS_VISIBLE const char *
763 lws_get_peer_simple(struct lws *wsi, char *name, int namelen)
768 struct sockaddr_in6 sin6;
770 struct sockaddr_in sin4;
775 if (LWS_IPV6_ENABLED(wsi->vhost)) {
789 if (getpeername(wsi->sock, p, &len) < 0 || len > olen) {
790 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
794 return lws_plat_inet_ntop(af, q, name, namelen);
796 #if defined(LWS_WITH_ESP8266)
797 return lws_plat_get_peer_simple(wsi, name, namelen);
806 lws_get_peer_addresses(struct lws *wsi, lws_sockfd_type fd, char *name,
807 int name_len, char *rip, int rip_len)
809 #ifndef LWS_PLAT_OPTEE
813 struct sockaddr_in6 sin6;
815 struct sockaddr_in sin4;
816 struct lws_context *context = wsi->context;
823 lws_latency_pre(context, wsi);
826 if (LWS_IPV6_ENABLED(wsi->vhost)) {
836 if (getpeername(fd, p, &len) < 0) {
837 lwsl_warn("getpeername: %s\n", strerror(LWS_ERRNO));
841 ret = lws_get_addresses(wsi->vhost, p, name, name_len, rip, rip_len);
844 lws_latency(context, wsi, "lws_get_peer_addresses", ret, 1);
857 lws_context_user(struct lws_context *context)
859 return context->user_space;
862 LWS_VISIBLE struct lws_vhost *
863 lws_vhost_get(struct lws *wsi)
868 LWS_VISIBLE struct lws_vhost *
869 lws_get_vhost(struct lws *wsi)
874 LWS_VISIBLE const struct lws_protocols *
875 lws_protocol_get(struct lws *wsi)
877 return wsi->protocol;
880 LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
881 lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name)
885 for (n = 0; n < vh->count_protocols; n++)
886 if (!strcmp(name, vh->protocols[n].name))
887 return &vh->protocols[n];
893 lws_callback_all_protocol(struct lws_context *context,
894 const struct lws_protocols *protocol, int reason)
896 struct lws_context_per_thread *pt = &context->pt[0];
897 unsigned int n, m = context->count_threads;
901 for (n = 0; n < pt->fds_count; n++) {
902 wsi = wsi_from_fd(context, pt->fds[n].fd);
905 if (wsi->protocol == protocol)
906 protocol->callback(wsi, reason, wsi->user_space,
916 lws_callback_all_protocol_vhost(struct lws_vhost *vh,
917 const struct lws_protocols *protocol, int reason)
919 struct lws_context *context = vh->context;
920 struct lws_context_per_thread *pt = &context->pt[0];
921 unsigned int n, m = context->count_threads;
925 for (n = 0; n < pt->fds_count; n++) {
926 wsi = wsi_from_fd(context, pt->fds[n].fd);
929 if (wsi->vhost == vh && wsi->protocol == protocol)
930 protocol->callback(wsi, reason, wsi->user_space,
939 LWS_VISIBLE LWS_EXTERN int
940 lws_callback_vhost_protocols(struct lws *wsi, int reason, void *in, int len)
944 for (n = 0; n < wsi->vhost->count_protocols; n++)
945 if (wsi->vhost->protocols[n].callback(wsi, reason, NULL, in, len))
951 LWS_VISIBLE LWS_EXTERN void
952 lws_set_fops(struct lws_context *context, struct lws_plat_file_ops *fops)
954 memcpy(&context->fops, fops, sizeof *fops);
958 * lws_now_secs() - seconds since 1970-1-1
961 LWS_VISIBLE LWS_EXTERN unsigned long
966 gettimeofday(&tv, NULL);
975 lws_get_socket_fd(struct lws *wsi)
984 lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
985 int ret, int completed)
987 unsigned long long u;
990 u = time_in_microseconds();
993 wsi->latency_start = u;
994 if (!wsi->action_start)
995 wsi->action_start = u;
999 if (wsi->action_start == wsi->latency_start)
1001 "Completion first try lat %lluus: %p: ret %d: %s\n",
1002 u - wsi->latency_start,
1003 (void *)wsi, ret, action);
1006 "Completion %lluus: lat %lluus: %p: ret %d: %s\n",
1007 u - wsi->action_start,
1008 u - wsi->latency_start,
1009 (void *)wsi, ret, action);
1010 wsi->action_start = 0;
1012 sprintf(buf, "lat %lluus: %p: ret %d: %s\n",
1013 u - wsi->latency_start, (void *)wsi, ret, action);
1015 if (u - wsi->latency_start > context->worst_latency) {
1016 context->worst_latency = u - wsi->latency_start;
1017 strcpy(context->worst_latency_info, buf);
1019 lwsl_latency("%s", buf);
1024 lws_rx_flow_control(struct lws *wsi, int enable)
1026 if (enable == (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
1029 lwsl_info("%s: (0x%p, %d)\n", __func__, wsi, enable);
1030 wsi->rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable;
1036 lws_rx_flow_allow_all_protocol(const struct lws_context *context,
1037 const struct lws_protocols *protocol)
1039 const struct lws_context_per_thread *pt = &context->pt[0];
1041 unsigned int n, m = context->count_threads;
1044 for (n = 0; n < pt->fds_count; n++) {
1045 wsi = wsi_from_fd(context, pt->fds[n].fd);
1048 if (wsi->protocol == protocol)
1049 lws_rx_flow_control(wsi, LWS_RXFLOW_ALLOW);
1055 LWS_VISIBLE extern const char *
1056 lws_canonical_hostname(struct lws_context *context)
1058 return (const char *)context->canonical_hostname;
1061 int user_callback_handle_rxflow(lws_callback_function callback_function,
1063 enum lws_callback_reasons reason, void *user,
1064 void *in, size_t len)
1068 n = callback_function(wsi, reason, user, in, len);
1070 n = _lws_rx_flow_control(wsi);
1075 #if defined(LWS_WITH_ESP8266)
1077 #define strchr ets_strchr
1081 lws_set_proxy(struct lws_vhost *vhost, const char *proxy)
1083 #if !defined(LWS_WITH_ESP8266)
1085 char authstring[96];
1090 /* we have to deal with a possible redundant leading http:// */
1091 if (!strncmp(proxy, "http://", 7))
1094 p = strchr(proxy, '@');
1095 if (p) { /* auth is around */
1097 if ((unsigned int)(p - proxy) > sizeof(authstring) - 1)
1100 strncpy(authstring, proxy, p - proxy);
1101 // null termination not needed on input
1102 if (lws_b64_encode_string(authstring, (p - proxy),
1103 vhost->proxy_basic_auth_token,
1104 sizeof vhost->proxy_basic_auth_token) < 0)
1107 lwsl_info(" Proxy auth in use\n");
1111 vhost->proxy_basic_auth_token[0] = '\0';
1113 strncpy(vhost->http_proxy_address, proxy,
1114 sizeof(vhost->http_proxy_address) - 1);
1115 vhost->http_proxy_address[
1116 sizeof(vhost->http_proxy_address) - 1] = '\0';
1118 p = strchr(vhost->http_proxy_address, ':');
1119 if (!p && !vhost->http_proxy_port) {
1120 lwsl_err("http_proxy needs to be ads:port\n");
1126 vhost->http_proxy_port = atoi(p + 1);
1130 lwsl_info(" Proxy %s:%u\n", vhost->http_proxy_address,
1131 vhost->http_proxy_port);
1136 lwsl_err("proxy auth too long\n");
1141 LWS_VISIBLE const struct lws_protocols *
1142 lws_get_protocol(struct lws *wsi)
1144 return wsi->protocol;
1148 lws_is_final_fragment(struct lws *wsi)
1150 lwsl_info("%s: final %d, rx pk length %ld, draining %ld", __func__,
1151 wsi->u.ws.final, (long)wsi->u.ws.rx_packet_length,
1152 (long)wsi->u.ws.rx_draining_ext);
1153 return wsi->u.ws.final && !wsi->u.ws.rx_packet_length && !wsi->u.ws.rx_draining_ext;
1156 LWS_VISIBLE unsigned char
1157 lws_get_reserved_bits(struct lws *wsi)
1159 return wsi->u.ws.rsv;
1163 lws_ensure_user_space(struct lws *wsi)
1165 lwsl_info("%s: %p protocol %p\n", __func__, wsi, wsi->protocol);
1169 /* allocate the per-connection user memory (if any) */
1171 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
1172 wsi->user_space = lws_zalloc(wsi->protocol->per_session_data_size);
1173 if (wsi->user_space == NULL) {
1174 lwsl_err("Out of memory for conn user space\n");
1178 lwsl_info("%s: %p protocol pss %lu, user_space=%p\n",
1179 __func__, wsi, (long)wsi->protocol->per_session_data_size,
1185 lwsl_timestamp(int level, char *p, int len)
1187 #ifndef LWS_PLAT_OPTEE
1188 time_t o_now = time(NULL);
1189 unsigned long long now;
1190 struct tm *ptm = NULL;
1198 ptm = localtime(&o_now);
1200 if (localtime_r(&o_now, &tm))
1205 for (n = 0; n < LLL_COUNT; n++) {
1206 if (level != (1 << n))
1208 now = time_in_microseconds() / 100;
1210 n = lws_snprintf(p, len,
1211 "[%04d/%02d/%02d %02d:%02d:%02d:%04d] %s: ",
1212 ptm->tm_year + 1900,
1218 (int)(now % 10000), log_level_names[n]);
1220 n = lws_snprintf(p, len, "[%llu:%04d] %s: ",
1221 (unsigned long long) now / 10000,
1222 (int)(now % 10000), log_level_names[n]);
1229 #ifndef LWS_PLAT_OPTEE
1230 LWS_VISIBLE void lwsl_emit_stderr(int level, const char *line)
1232 #if !defined(LWS_WITH_ESP8266)
1235 lwsl_timestamp(level, buf, sizeof(buf));
1236 fprintf(stderr, "%s%s", buf, line);
1241 LWS_VISIBLE void _lws_logv(int filter, const char *format, va_list vl)
1243 #if defined(LWS_WITH_ESP8266)
1250 if (!(log_level & filter))
1253 n = vsnprintf(buf, sizeof(buf) - 1, format, vl);
1255 #if defined(LWS_WITH_ESP8266)
1256 buf[sizeof(buf) - 1] = '\0';
1258 /* vnsprintf returns what it would have written, even if truncated */
1259 if (n > sizeof(buf) - 1)
1260 n = sizeof(buf) - 1;
1265 lwsl_emit(filter, buf);
1268 LWS_VISIBLE void _lws_log(int filter, const char *format, ...)
1272 va_start(ap, format);
1273 _lws_logv(filter, format, ap);
1277 LWS_VISIBLE void lws_set_log_level(int level,
1278 void (*func)(int level, const char *line))
1285 LWS_VISIBLE int lwsl_visible(int level)
1287 return log_level & level;
1291 lws_is_ssl(struct lws *wsi)
1293 #ifdef LWS_OPENSSL_SUPPORT
1294 return wsi->use_ssl;
1301 #ifdef LWS_OPENSSL_SUPPORT
1303 lws_get_ssl(struct lws *wsi)
1310 lws_partial_buffered(struct lws *wsi)
1312 return !!wsi->trunc_len;
1315 void lws_set_protocol_write_pending(struct lws *wsi,
1316 enum lws_pending_protocol_send pend)
1318 lwsl_info("setting pps %d\n", pend);
1321 lwsl_err("pps overwrite\n");
1323 lws_rx_flow_control(wsi, 0);
1324 lws_callback_on_writable(wsi);
1328 lws_get_peer_write_allowance(struct lws *wsi)
1330 #ifdef LWS_USE_HTTP2
1331 /* only if we are using HTTP2 on this connection */
1332 if (wsi->mode != LWSCM_HTTP2_SERVING)
1334 /* user is only interested in how much he can send, or that he can't */
1335 if (wsi->u.http2.tx_credit <= 0)
1338 return wsi->u.http2.tx_credit;
1346 lws_union_transition(struct lws *wsi, enum connection_mode mode)
1348 lwsl_debug("%s: %p: mode %d\n", __func__, wsi, mode);
1349 memset(&wsi->u, 0, sizeof(wsi->u));
1353 LWS_VISIBLE struct lws_plat_file_ops *
1354 lws_get_fops(struct lws_context *context)
1356 return &context->fops;
1359 LWS_VISIBLE LWS_EXTERN struct lws_context *
1360 lws_get_context(const struct lws *wsi)
1362 return wsi->context;
1365 LWS_VISIBLE LWS_EXTERN int
1366 lws_get_count_threads(struct lws_context *context)
1368 return context->count_threads;
1371 LWS_VISIBLE LWS_EXTERN void *
1372 lws_wsi_user(struct lws *wsi)
1374 return wsi->user_space;
1377 LWS_VISIBLE LWS_EXTERN struct lws *
1378 lws_get_parent(const struct lws *wsi)
1383 LWS_VISIBLE LWS_EXTERN struct lws *
1384 lws_get_child(const struct lws *wsi)
1386 return wsi->child_list;
1389 LWS_VISIBLE LWS_EXTERN void
1390 lws_close_reason(struct lws *wsi, enum lws_close_status status,
1391 unsigned char *buf, size_t len)
1393 unsigned char *p, *start;
1394 int budget = sizeof(wsi->u.ws.ping_payload_buf) - LWS_PRE;
1396 assert(wsi->mode == LWSCM_WS_SERVING || wsi->mode == LWSCM_WS_CLIENT);
1398 start = p = &wsi->u.ws.ping_payload_buf[LWS_PRE];
1400 *p++ = (((int)status) >> 8) & 0xff;
1401 *p++ = ((int)status) & 0xff;
1404 while (len-- && p < start + budget)
1407 wsi->u.ws.close_in_ping_buffer_len = p - start;
1411 _lws_rx_flow_control(struct lws *wsi)
1413 /* there is no pending change */
1414 if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) {
1415 lwsl_debug("%s: no pending change\n", __func__);
1419 /* stuff is still buffered, not ready to really accept new input */
1420 if (wsi->rxflow_buffer) {
1421 /* get ourselves called back to deal with stashed buffer */
1422 lws_callback_on_writable(wsi);
1426 /* pending is cleared, we can change rxflow state */
1428 wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
1430 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
1431 wsi->rxflow_change_to & LWS_RXFLOW_ALLOW);
1433 /* adjust the pollfd for this wsi */
1435 if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) {
1436 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
1437 lwsl_info("%s: fail\n", __func__);
1441 if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
1448 lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len)
1450 static const unsigned char e0f4[] = {
1451 0xa0 | ((2 - 1) << 2) | 1, /* e0 */
1452 0x80 | ((4 - 1) << 2) | 1, /* e1 */
1453 0x80 | ((4 - 1) << 2) | 1, /* e2 */
1454 0x80 | ((4 - 1) << 2) | 1, /* e3 */
1455 0x80 | ((4 - 1) << 2) | 1, /* e4 */
1456 0x80 | ((4 - 1) << 2) | 1, /* e5 */
1457 0x80 | ((4 - 1) << 2) | 1, /* e6 */
1458 0x80 | ((4 - 1) << 2) | 1, /* e7 */
1459 0x80 | ((4 - 1) << 2) | 1, /* e8 */
1460 0x80 | ((4 - 1) << 2) | 1, /* e9 */
1461 0x80 | ((4 - 1) << 2) | 1, /* ea */
1462 0x80 | ((4 - 1) << 2) | 1, /* eb */
1463 0x80 | ((4 - 1) << 2) | 1, /* ec */
1464 0x80 | ((2 - 1) << 2) | 1, /* ed */
1465 0x80 | ((4 - 1) << 2) | 1, /* ee */
1466 0x80 | ((4 - 1) << 2) | 1, /* ef */
1467 0x90 | ((3 - 1) << 2) | 2, /* f0 */
1468 0x80 | ((4 - 1) << 2) | 2, /* f1 */
1469 0x80 | ((4 - 1) << 2) | 2, /* f2 */
1470 0x80 | ((4 - 1) << 2) | 2, /* f3 */
1471 0x80 | ((1 - 1) << 2) | 2, /* f4 */
1474 0x80 | ((4 - 1) << 2) | 0, /* s2 */
1475 0x80 | ((4 - 1) << 2) | 1, /* s3 */
1477 unsigned char s = *state;
1480 unsigned char c = *buf++;
1484 if (c < 0xc2 || c > 0xf4)
1487 s = 0x80 | ((4 - 1) << 2);
1492 if (c < (s & 0xf0) ||
1493 c >= (s & 0xf0) + 0x10 + ((s << 2) & 0x30))
1495 s = e0f4[21 + (s & 3)];
1504 LWS_VISIBLE LWS_EXTERN int
1505 lws_parse_uri(char *p, const char **prot, const char **ads, int *port,
1509 static const char *slash = "/";
1511 /* cut up the location into address, port and path */
1513 while (*p && (*p != ':' || p[1] != '/' || p[2] != '/'))
1524 if (!strcmp(*prot, "http") || !strcmp(*prot, "ws"))
1526 else if (!strcmp(*prot, "https") || !strcmp(*prot, "wss"))
1532 while (*p && *p != ']')
1539 while (*p && *p != ':' && *p != '/')
1545 while (*p && *p != '/')
1558 #ifdef LWS_NO_EXTENSIONS
1560 /* we need to provide dummy callbacks for internal exts
1561 * so user code runs when faced with a lib compiled with
1562 * extensions disabled.
1566 lws_extension_callback_pm_deflate(struct lws_context *context,
1567 const struct lws_extension *ext,
1569 enum lws_extension_callback_reasons reason,
1570 void *user, void *in, size_t len)
1585 lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
1589 #ifdef LWS_USE_UNIX_SOCK
1590 struct sockaddr_un serv_unix;
1593 struct sockaddr_in6 serv_addr6;
1595 struct sockaddr_in serv_addr4;
1596 #ifndef LWS_PLAT_OPTEE
1597 socklen_t len = sizeof(struct sockaddr);
1600 struct sockaddr_in sin;
1603 #ifdef LWS_USE_UNIX_SOCK
1604 if (LWS_UNIX_SOCK_ENABLED(vhost)) {
1605 v = (struct sockaddr *)&serv_unix;
1606 n = sizeof(struct sockaddr_un);
1607 bzero((char *) &serv_unix, sizeof(serv_unix));
1608 serv_unix.sun_family = AF_UNIX;
1609 if (sizeof(serv_unix.sun_path) <= strlen(iface)) {
1610 lwsl_err("\"%s\" too long for UNIX domain socket\n",
1614 strcpy(serv_unix.sun_path, iface);
1615 if (serv_unix.sun_path[0] == '@')
1616 serv_unix.sun_path[0] = '\0';
1620 #if defined(LWS_USE_IPV6) && !defined(LWS_WITH_ESP32)
1621 if (LWS_IPV6_ENABLED(vhost)) {
1622 v = (struct sockaddr *)&serv_addr6;
1623 n = sizeof(struct sockaddr_in6);
1624 bzero((char *) &serv_addr6, sizeof(serv_addr6));
1626 interface_to_sa(vhost, iface,
1627 (struct sockaddr_in *)v, n) < 0) {
1628 lwsl_err("Unable to find interface %s\n", iface);
1633 struct ifaddrs *addrs, *addr;
1634 char ip[NI_MAXHOST];
1638 for (addr = addrs; addr; addr = addr->ifa_next) {
1639 if (!addr->ifa_addr ||
1640 addr->ifa_addr->sa_family != AF_INET6)
1643 getnameinfo(addr->ifa_addr,
1644 sizeof(struct sockaddr_in6),
1646 NULL, 0, NI_NUMERICHOST);
1650 if (ip[i++] == '%') {
1655 if (!strcmp(ip, iface)) {
1656 serv_addr6.sin6_scope_id =
1657 if_nametoindex(addr->ifa_name);
1664 serv_addr6.sin6_family = AF_INET6;
1665 serv_addr6.sin6_port = htons(port);
1669 v = (struct sockaddr *)&serv_addr4;
1670 n = sizeof(serv_addr4);
1671 bzero((char *) &serv_addr4, sizeof(serv_addr4));
1672 serv_addr4.sin_addr.s_addr = INADDR_ANY;
1673 serv_addr4.sin_family = AF_INET;
1674 #if !defined(LWS_WITH_ESP32)
1677 interface_to_sa(vhost, iface,
1678 (struct sockaddr_in *)v, n) < 0) {
1679 lwsl_err("Unable to find interface %s\n", iface);
1683 serv_addr4.sin_port = htons(port);
1686 n = bind(sockfd, v, n);
1687 #ifdef LWS_USE_UNIX_SOCK
1688 if (n < 0 && LWS_UNIX_SOCK_ENABLED(vhost)) {
1689 lwsl_err("ERROR on binding fd %d to \"%s\" (%d %d)\n",
1690 sockfd, iface, n, LWS_ERRNO);
1695 lwsl_err("ERROR on binding fd %d to port %d (%d %d)\n",
1696 sockfd, port, n, LWS_ERRNO);
1700 #ifndef LWS_PLAT_OPTEE
1701 if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
1702 lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
1705 port = ntohs(sin.sin_port);
1712 lws_restart_ws_ping_pong_timer(struct lws *wsi)
1714 if (!wsi->context->ws_ping_pong_interval)
1716 if (wsi->state != LWSS_ESTABLISHED)
1719 wsi->u.ws.time_next_ping_check = (time_t)lws_now_secs() +
1720 wsi->context->ws_ping_pong_interval;
1723 static const char *hex = "0123456789ABCDEF";
1725 LWS_VISIBLE LWS_EXTERN const char *
1726 lws_sql_purify(char *escaped, const char *string, int len)
1728 const char *p = string;
1731 while (*p && len-- > 2) {
1745 LWS_VISIBLE LWS_EXTERN const char *
1746 lws_json_purify(char *escaped, const char *string, int len)
1748 const char *p = string;
1756 while (*p && len-- > 6) {
1757 if (*p == '\"' || *p == '\\' || *p < 0x20) {
1762 *q++ = hex[((*p) >> 4) & 15];
1763 *q++ = hex[(*p) & 15];
1774 LWS_VISIBLE LWS_EXTERN const char *
1775 lws_urlencode(char *escaped, const char *string, int len)
1777 const char *p = string;
1780 while (*p && len-- > 3) {
1786 if ((*p >= '0' && *p <= '9') ||
1787 (*p >= 'A' && *p <= 'Z') ||
1788 (*p >= 'a' && *p <= 'z')) {
1793 *q++ = hex[(*p >> 4) & 0xf];
1794 *q++ = hex[*p & 0xf];
1804 LWS_VISIBLE LWS_EXTERN int
1805 lws_urldecode(char *string, const char *escaped, int len)
1810 while (*escaped && len) {
1813 if (*escaped == '%') {
1818 if (*escaped == '+') {
1824 *string++ = *escaped++;
1828 n = char_to_hex(*escaped);
1837 n = char_to_hex(*escaped);
1841 *string++ = sum | n;
1853 LWS_VISIBLE LWS_EXTERN int
1854 lws_finalize_startup(struct lws_context *context)
1856 struct lws_context_creation_info info;
1858 info.uid = context->uid;
1859 info.gid = context->gid;
1861 if (lws_check_opt(context->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
1862 lws_plat_drop_app_privileges(&info);
1868 lws_snprintf(char *str, size_t size, const char *format, ...)
1876 va_start(ap, format);
1877 n = vsnprintf(str, size, format, ap);
1887 LWS_VISIBLE LWS_EXTERN int
1888 lws_is_cgi(struct lws *wsi) {
1899 urlencode(const char *in, int inlen, char *out, int outlen)
1901 char *start = out, *end = out + outlen;
1903 while (inlen-- && out < end - 4) {
1904 if ((*in >= 'A' && *in <= 'Z') ||
1905 (*in >= 'a' && *in <= 'z') ||
1906 (*in >= '0' && *in <= '9') ||
1920 *out++ = hex[(*in) >> 4];
1921 *out++ = hex[(*in++) & 15];
1932 lws_create_basic_wsi(struct lws_context *context, int tsi)
1934 struct lws *new_wsi;
1936 if ((unsigned int)context->pt[tsi].fds_count ==
1937 context->fd_limit_per_thread - 1) {
1938 lwsl_err("no space for new conn\n");
1942 new_wsi = lws_zalloc(sizeof(struct lws));
1943 if (new_wsi == NULL) {
1944 lwsl_err("Out of memory for new connection\n");
1949 new_wsi->context = context;
1950 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
1951 new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
1953 /* initialize the instance struct */
1955 new_wsi->state = LWSS_CGI;
1956 new_wsi->mode = LWSCM_CGI;
1957 new_wsi->hdr_parsing_completed = 0;
1958 new_wsi->position_in_fds_table = -1;
1961 * these can only be set once the protocol is known
1962 * we set an unestablished connection's protocol pointer
1963 * to the start of the defauly vhost supported list, so it can look
1964 * for matching ones during the handshake
1966 new_wsi->protocol = context->vhost_list->protocols;
1967 new_wsi->user_space = NULL;
1968 new_wsi->ietf_spec_revision = 0;
1969 new_wsi->sock = LWS_SOCK_INVALID;
1970 context->count_wsi_allocated++;
1975 LWS_VISIBLE LWS_EXTERN int
1976 lws_cgi(struct lws *wsi, const char * const *exec_array, int script_uri_path_len,
1977 int timeout_secs, const struct lws_protocol_vhost_options *mp_cgienv)
1979 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
1980 char *env_array[30], cgi_path[400], e[1024], *p = e,
1981 *end = p + sizeof(e) - 1, tok[256], *t;
1982 struct lws_cgi *cgi;
1983 int n, m, i, uritok = -1;
1986 * give the master wsi a cgi struct
1989 wsi->cgi = lws_zalloc(sizeof(*wsi->cgi));
1991 lwsl_err("%s: OOM\n", __func__);
1996 cgi->wsi = wsi; /* set cgi's owning wsi */
1998 /* create pipes for [stdin|stdout] and [stderr] */
2000 for (n = 0; n < 3; n++)
2001 if (pipe(cgi->pipe_fds[n]) == -1)
2004 /* create cgi wsis for each stdin/out/err fd */
2006 for (n = 0; n < 3; n++) {
2007 cgi->stdwsi[n] = lws_create_basic_wsi(wsi->context, wsi->tsi);
2008 if (!cgi->stdwsi[n])
2010 cgi->stdwsi[n]->cgi_channel = n;
2011 cgi->stdwsi[n]->vhost = wsi->vhost;
2013 // lwsl_err("%s: cgi %p: pipe fd %d -> fd %d / %d\n", __func__, wsi, n,
2014 // cgi->pipe_fds[n][!!(n == 0)], cgi->pipe_fds[n][!(n == 0)]);
2016 /* read side is 0, stdin we want the write side, others read */
2017 cgi->stdwsi[n]->sock = cgi->pipe_fds[n][!!(n == 0)];
2018 if (fcntl(cgi->pipe_fds[n][!!(n == 0)], F_SETFL, O_NONBLOCK) < 0) {
2019 lwsl_err("%s: setting NONBLOCK failed\n", __func__);
2024 for (n = 0; n < 3; n++) {
2025 lws_libuv_accept(cgi->stdwsi[n], cgi->stdwsi[n]->sock);
2026 if (insert_wsi_socket_into_fds(wsi->context, cgi->stdwsi[n]))
2028 cgi->stdwsi[n]->parent = wsi;
2029 cgi->stdwsi[n]->sibling_list = wsi->child_list;
2030 wsi->child_list = cgi->stdwsi[n];
2033 lws_change_pollfd(cgi->stdwsi[LWS_STDIN], LWS_POLLIN, LWS_POLLOUT);
2034 lws_change_pollfd(cgi->stdwsi[LWS_STDOUT], LWS_POLLOUT, LWS_POLLIN);
2035 lws_change_pollfd(cgi->stdwsi[LWS_STDERR], LWS_POLLOUT, LWS_POLLIN);
2037 lwsl_debug("%s: fds in %d, out %d, err %d\n", __func__,
2038 cgi->stdwsi[LWS_STDIN]->sock, cgi->stdwsi[LWS_STDOUT]->sock,
2039 cgi->stdwsi[LWS_STDERR]->sock);
2041 lws_set_timeout(wsi, PENDING_TIMEOUT_CGI, timeout_secs);
2043 /* the cgi stdout is always sending us http1.x header data first */
2044 wsi->hdr_state = LCHS_HEADER;
2046 /* add us to the pt list of active cgis */
2047 lwsl_debug("%s: adding cgi %p to list\n", __func__, wsi->cgi);
2048 cgi->cgi_list = pt->cgi_list;
2051 /* prepare his CGI env */
2055 if (lws_is_ssl(wsi))
2056 env_array[n++] = "HTTPS=ON";
2057 if (wsi->u.hdr.ah) {
2058 static const unsigned char meths[] = {
2061 WSI_TOKEN_OPTIONS_URI,
2063 WSI_TOKEN_PATCH_URI,
2064 WSI_TOKEN_DELETE_URI,
2066 static const char * const meth_names[] = {
2067 "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE",
2070 for (m = 0; m < ARRAY_SIZE(meths); m++)
2071 if (lws_hdr_total_length(wsi, meths[m]) >=
2072 script_uri_path_len) {
2080 lws_snprintf(cgi_path, sizeof(cgi_path) - 1, "REQUEST_URI=%s",
2081 lws_hdr_simple_ptr(wsi, uritok));
2082 cgi_path[sizeof(cgi_path) - 1] = '\0';
2083 env_array[n++] = cgi_path;
2086 p += lws_snprintf(p, end - p, "REQUEST_METHOD=%s",
2091 p += lws_snprintf(p, end - p, "QUERY_STRING=");
2092 /* dump the individual URI Arg parameters */
2095 i = lws_hdr_copy_fragment(wsi, tok, sizeof(tok),
2096 WSI_TOKEN_HTTP_URI_ARGS, m);
2100 while (*t && *t != '=' && p < end - 4)
2104 i = urlencode(t, i- (t - tok), p, end - p);
2116 p += lws_snprintf(p, end - p, "PATH_INFO=%s",
2117 lws_hdr_simple_ptr(wsi, uritok) +
2118 script_uri_path_len);
2121 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_REFERER)) {
2123 p += lws_snprintf(p, end - p, "HTTP_REFERER=%s",
2124 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_REFERER));
2127 if (lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
2129 p += lws_snprintf(p, end - p, "HTTP_HOST=%s",
2130 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
2133 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COOKIE)) {
2135 p += lws_snprintf(p, end - p, "HTTP_COOKIE=%s",
2136 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COOKIE));
2139 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT)) {
2141 p += lws_snprintf(p, end - p, "USER_AGENT=%s",
2142 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_USER_AGENT));
2145 if (uritok == WSI_TOKEN_POST_URI) {
2146 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE)) {
2148 p += lws_snprintf(p, end - p, "CONTENT_TYPE=%s",
2149 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE));
2152 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
2154 p += lws_snprintf(p, end - p, "CONTENT_LENGTH=%s",
2155 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH));
2160 p += lws_snprintf(p, end - p, "SCRIPT_PATH=%s", exec_array[0]) + 1;
2164 p += lws_snprintf(p, end - p, "%s=%s", mp_cgienv->name,
2166 lwsl_debug(" Applying mount-specific cgi env '%s'\n",
2169 mp_cgienv = mp_cgienv->next;
2172 env_array[n++] = "SERVER_SOFTWARE=libwebsockets";
2173 env_array[n++] = "PATH=/bin:/usr/bin:/usr/local/bin:/var/www/cgi-bin";
2174 env_array[n] = NULL;
2177 for (m = 0; m < n; m++)
2178 lwsl_err(" %s\n", env_array[m]);
2182 * Actually having made the env, as a cgi we don't need the ah
2185 if (wsi->u.hdr.ah->rxpos == wsi->u.hdr.ah->rxlen)
2186 lws_header_table_detach(wsi, 0);
2188 /* we are ready with the redirection pipes... run the thing */
2189 #if !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
2195 lwsl_err("fork failed, errno %d", errno);
2199 #if defined(__linux__)
2200 prctl(PR_SET_PDEATHSIG, SIGTERM);
2202 setpgrp(); /* stops on-daemonized main processess getting SIGINT from TTY */
2205 /* we are the parent process */
2206 wsi->context->count_cgi_spawned++;
2207 lwsl_debug("%s: cgi %p spawned PID %d\n", __func__, cgi, cgi->pid);
2211 /* somewhere we can at least read things and enter it */
2213 lwsl_notice("%s: Failed to chdir\n", __func__);
2215 /* We are the forked process, redirect and kill inherited things.
2217 * Because of vfork(), we cannot do anything that changes pages in
2218 * the parent environment. Stuff that changes kernel state for the
2219 * process is OK. Stuff that happens after the execvpe() is OK.
2222 for (n = 0; n < 3; n++)
2223 if (dup2(cgi->pipe_fds[n][!(n == 0)], n) < 0) {
2224 lwsl_err("%s: stdin dup2 failed\n", __func__);
2228 #if !defined(LWS_HAVE_VFORK) || !defined(LWS_HAVE_EXECVPE)
2229 for (m = 0; m < n; m++) {
2230 p = strchr(env_array[m], '=');
2232 setenv(env_array[m], p, 1);
2234 execvp(exec_array[0], (char * const *)&exec_array[0]);
2236 execvpe(exec_array[0], (char * const *)&exec_array[0], &env_array[0]);
2242 /* drop us from the pt cgi list */
2243 pt->cgi_list = cgi->cgi_list;
2246 remove_wsi_socket_from_fds(wsi->cgi->stdwsi[n]);
2248 for (n = 0; n < 3; n++)
2249 if (wsi->cgi->stdwsi[n])
2250 lws_free_wsi(cgi->stdwsi[n]);
2253 for (n = 0; n < 3; n++) {
2254 if (cgi->pipe_fds[n][0])
2255 close(cgi->pipe_fds[n][0]);
2256 if (cgi->pipe_fds[n][1])
2257 close(cgi->pipe_fds[n][1]);
2260 lws_free_set_NULL(wsi->cgi);
2262 lwsl_err("%s: failed\n", __func__);
2267 LWS_VISIBLE LWS_EXTERN int
2268 lws_cgi_write_split_stdout_headers(struct lws *wsi)
2270 int n, m, match = 0, lp = 0;
2271 static const char * const content_length = "content-length: ";
2272 char buf[LWS_PRE + 1024], *start = &buf[LWS_PRE], *p = start,
2273 *end = &buf[sizeof(buf) - 1 - LWS_PRE], c, l[12];
2278 while (wsi->hdr_state != LHCS_PAYLOAD) {
2279 /* we have to separate header / finalize and
2280 * payload chunks, since they need to be
2281 * handled separately
2283 n = read(lws_get_socket_fd(wsi->cgi->stdwsi[LWS_STDOUT]), &c, 1);
2285 if (errno != EAGAIN) {
2286 lwsl_debug("%s: read says %d\n", __func__, n);
2293 lwsl_debug("-- 0x%02X %c\n", (unsigned char)c, c);
2294 switch (wsi->hdr_state) {
2296 if (!content_length[match] &&
2297 (c >= '0' && c <= '9') &&
2298 lp < sizeof(l) - 1) {
2301 wsi->cgi->content_length = atol(l);
2303 if (tolower(c) == content_length[match])
2308 /* some cgi only send us \x0a for EOL */
2310 wsi->hdr_state = LCHS_SINGLE_0A;
2315 wsi->hdr_state = LCHS_LF1;
2323 wsi->hdr_state = LCHS_CR2;
2326 /* we got \r[^\n]... it's unreasonable */
2327 lwsl_debug("%s: funny CRLF 0x%02X\n", __func__, (unsigned char)c);
2333 wsi->hdr_state = LCHS_LF2;
2336 wsi->hdr_state = LCHS_HEADER;
2341 case LCHS_SINGLE_0A:
2344 lwsl_debug("Content-Length: %ld\n", wsi->cgi->content_length);
2345 wsi->hdr_state = LHCS_PAYLOAD;
2346 /* drop the \0xa ... finalize will add it if needed */
2347 if (lws_finalize_http_header(wsi,
2348 (unsigned char **)&p,
2349 (unsigned char *)end))
2354 /* we got \r\n\r[^\n]... it's unreasonable */
2356 /* we got \x0anext header, it's reasonable */
2358 wsi->hdr_state = LCHS_HEADER;
2365 /* ran out of input, ended the headers, or filled up the headers buf */
2366 if (!n || wsi->hdr_state == LHCS_PAYLOAD || (p + 4) == end) {
2368 m = lws_write(wsi, (unsigned char *)start,
2369 p - start, LWS_WRITE_HTTP_HEADERS);
2371 lwsl_debug("%s: write says %d\n", __func__, m);
2374 /* writeability becomes uncertain now we wrote
2375 * something, we must return to the event loop
2382 n = read(lws_get_socket_fd(wsi->cgi->stdwsi[LWS_STDOUT]),
2383 start, sizeof(buf) - LWS_PRE);
2385 if (n < 0 && errno != EAGAIN) {
2386 lwsl_debug("%s: stdout read says %d\n", __func__, n);
2390 m = lws_write(wsi, (unsigned char *)start, n, LWS_WRITE_HTTP);
2391 //lwsl_notice("write %d\n", m);
2393 lwsl_debug("%s: stdout write says %d\n", __func__, m);
2396 wsi->cgi->content_length_seen += m;
2402 LWS_VISIBLE LWS_EXTERN int
2403 lws_cgi_kill(struct lws *wsi)
2405 struct lws_cgi_args args;
2408 lwsl_debug("%s: %p\n", __func__, wsi);
2413 if (wsi->cgi->pid > 0) {
2414 n = waitpid(wsi->cgi->pid, &status, WNOHANG);
2416 lwsl_debug("%s: PID %d reaped\n", __func__,
2420 /* kill the process group */
2421 n = kill(-wsi->cgi->pid, SIGTERM);
2422 lwsl_debug("%s: SIGTERM child PID %d says %d (errno %d)\n", __func__,
2423 wsi->cgi->pid, n, errno);
2426 * hum seen errno=3 when process is listed in ps,
2427 * it seems we don't always retain process grouping
2429 * Direct these fallback attempt to the exact child
2431 n = kill(wsi->cgi->pid, SIGTERM);
2433 n = kill(wsi->cgi->pid, SIGPIPE);
2435 n = kill(wsi->cgi->pid, SIGKILL);
2437 lwsl_err("%s: SIGKILL PID %d failed errno %d (maybe zombie)\n",
2438 __func__, wsi->cgi->pid, errno);
2442 /* He could be unkillable because he's a zombie */
2445 n = waitpid(-wsi->cgi->pid, &status, WNOHANG);
2447 lwsl_debug("%s: reaped PID %d\n", __func__, n);
2449 n = waitpid(wsi->cgi->pid, &status, WNOHANG);
2451 lwsl_debug("%s: reaped PID %d\n", __func__, n);
2457 args.stdwsi = &wsi->cgi->stdwsi[0];
2459 if (wsi->cgi->pid != -1 && user_callback_handle_rxflow(
2460 wsi->protocol->callback,
2461 wsi, LWS_CALLBACK_CGI_TERMINATED,
2463 (void *)&args, 0)) {
2465 if (!wsi->cgi->being_closed)
2466 lws_close_free_wsi(wsi, 0);
2473 lws_cgi_kill_terminated(struct lws_context_per_thread *pt)
2475 struct lws_cgi **pcgi, *cgi = NULL;
2479 /* find finished guys but don't reap yet */
2480 n = waitpid(-1, &status, WNOHANG);
2483 lwsl_debug("%s: observed PID %d terminated\n", __func__, n);
2485 pcgi = &pt->cgi_list;
2487 /* check all the subprocesses on the cgi list */
2489 /* get the next one first as list may change */
2491 pcgi = &(*pcgi)->cgi_list;
2496 /* wait for stdout to be drained */
2497 if (cgi->content_length > cgi->content_length_seen)
2500 if (cgi->content_length) {
2501 lwsl_debug("%s: wsi %p: expected content length seen: %ld\n",
2502 __func__, cgi->wsi, cgi->content_length_seen);
2506 waitpid(n, &status, WNOHANG);
2508 * he's already terminated so no need for kill()
2509 * but we should do the terminated cgi callback
2510 * and close him if he's not already closing
2512 if (n == cgi->pid) {
2513 lwsl_debug("%s: found PID %d on cgi list\n",
2516 if (!cgi->content_length) {
2518 * well, if he sends chunked... give him 5s after the
2519 * cgi terminated to send buffered
2521 cgi->chunked_grace++;
2527 lws_cgi_kill(cgi->wsi);
2533 /* if not found on the cgi list, as he's one of ours, reap */
2535 lwsl_debug("%s: reading PID %d although no cgi match\n",
2537 waitpid(n, &status, WNOHANG);
2541 /* disable this to confirm timeout cgi cleanup flow */
2543 pcgi = &pt->cgi_list;
2545 /* check all the subprocesses on the cgi list */
2547 /* get the next one first as list may change */
2549 pcgi = &(*pcgi)->cgi_list;
2554 /* we deferred killing him after reaping his PID */
2555 if (cgi->chunked_grace) {
2556 cgi->chunked_grace++;
2557 if (cgi->chunked_grace < 5)
2562 /* wait for stdout to be drained */
2563 if (cgi->content_length > cgi->content_length_seen)
2566 if (cgi->content_length)
2567 lwsl_debug("%s: wsi %p: expected content length seen: %ld\n",
2568 __func__, cgi->wsi, cgi->content_length_seen);
2571 if (waitpid(cgi->pid, &status, WNOHANG) > 0) {
2573 if (!cgi->content_length) {
2575 * well, if he sends chunked... give him 5s after the
2576 * cgi terminated to send buffered
2578 cgi->chunked_grace++;
2582 lwsl_debug("%s: found PID %d on cgi list\n",
2583 __func__, cgi->pid);
2586 lws_cgi_kill(cgi->wsi);
2593 /* general anti zombie defence */
2594 // n = waitpid(-1, &status, WNOHANG);
2596 // lwsl_notice("%s: anti-zombie wait says %d\n", __func__, n);
2602 #ifdef LWS_NO_EXTENSIONS
2604 lws_set_extension_option(struct lws *wsi, const char *ext_name,
2605 const char *opt_name, const char *opt_val)
2611 #ifdef LWS_WITH_ACCESS_LOG
2613 lws_access_log(struct lws *wsi)
2615 char *p = wsi->access_log.user_agent, ass[512];
2618 if (!wsi->access_log_pending)
2621 if (!wsi->access_log.header_log)
2627 l = lws_snprintf(ass, sizeof(ass) - 1, "%s %d %lu %s\n",
2628 wsi->access_log.header_log,
2629 wsi->access_log.response, wsi->access_log.sent, p);
2631 if (wsi->vhost->log_fd != (int)LWS_INVALID_FILE) {
2632 if (write(wsi->vhost->log_fd, ass, l) != l)
2633 lwsl_err("Failed to write log\n");
2635 lwsl_err("%s", ass);
2637 if (wsi->access_log.header_log) {
2638 lws_free(wsi->access_log.header_log);
2639 wsi->access_log.header_log = NULL;
2641 if (wsi->access_log.user_agent) {
2642 lws_free(wsi->access_log.user_agent);
2643 wsi->access_log.user_agent = NULL;
2645 wsi->access_log_pending = 0;
2652 lws_sum_stats(const struct lws_context *ctx, struct lws_conn_stats *cs)
2654 const struct lws_vhost *vh = ctx->vhost_list;
2658 cs->rx += vh->conn_stats.rx;
2659 cs->tx += vh->conn_stats.tx;
2660 cs->conn += vh->conn_stats.conn;
2661 cs->trans += vh->conn_stats.trans;
2662 cs->ws_upg += vh->conn_stats.ws_upg;
2663 cs->http2_upg += vh->conn_stats.http2_upg;
2664 cs->rejected += vh->conn_stats.rejected;
2666 vh = vh->vhost_next;
2670 #ifdef LWS_WITH_SERVER_STATUS
2673 lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len)
2675 static const char * const prots[] = {
2684 char *orig = buf, *end = buf + len - 1, first = 1;
2690 buf += lws_snprintf(buf, end - buf,
2691 "{\n \"name\":\"%s\",\n"
2692 " \"port\":\"%d\",\n"
2693 " \"use_ssl\":\"%d\",\n"
2694 " \"sts\":\"%d\",\n"
2695 " \"rx\":\"%llu\",\n"
2696 " \"tx\":\"%llu\",\n"
2697 " \"conn\":\"%lu\",\n"
2698 " \"trans\":\"%lu\",\n"
2699 " \"ws_upg\":\"%lu\",\n"
2700 " \"rejected\":\"%lu\",\n"
2701 " \"http2_upg\":\"%lu\""
2703 vh->name, vh->listen_port,
2704 #ifdef LWS_OPENSSL_SUPPORT
2709 !!(vh->options & LWS_SERVER_OPTION_STS),
2710 vh->conn_stats.rx, vh->conn_stats.tx,
2711 vh->conn_stats.conn, vh->conn_stats.trans,
2712 vh->conn_stats.ws_upg,
2713 vh->conn_stats.rejected,
2714 vh->conn_stats.http2_upg
2717 if (vh->mount_list) {
2718 const struct lws_http_mount *m = vh->mount_list;
2720 buf += lws_snprintf(buf, end - buf, ",\n \"mounts\":[");
2723 buf += lws_snprintf(buf, end - buf, ",");
2724 buf += lws_snprintf(buf, end - buf,
2725 "\n {\n \"mountpoint\":\"%s\",\n"
2726 " \"origin\":\"%s%s\",\n"
2727 " \"cache_max_age\":\"%d\",\n"
2728 " \"cache_reuse\":\"%d\",\n"
2729 " \"cache_revalidate\":\"%d\",\n"
2730 " \"cache_intermediaries\":\"%d\"\n"
2733 prots[m->origin_protocol],
2737 m->cache_revalidate,
2738 m->cache_intermediaries);
2740 buf += lws_snprintf(buf, end - buf,
2741 ",\n \"default\":\"%s\"",
2743 buf += lws_snprintf(buf, end - buf, "\n }");
2747 buf += lws_snprintf(buf, end - buf, "\n ]");
2750 if (vh->protocols) {
2754 buf += lws_snprintf(buf, end - buf, ",\n \"ws-protocols\":[");
2755 while (n < vh->count_protocols) {
2757 buf += lws_snprintf(buf, end - buf, ",");
2758 buf += lws_snprintf(buf, end - buf,
2760 " \"status\":\"ok\"\n }\n }"
2762 vh->protocols[n].name);
2766 buf += lws_snprintf(buf, end - buf, "\n ]");
2769 buf += lws_snprintf(buf, end - buf, "\n}");
2775 LWS_EXTERN LWS_VISIBLE int
2776 lws_json_dump_context(const struct lws_context *context, char *buf, int len,
2779 char *orig = buf, *end = buf + len - 1, first = 1;
2780 const struct lws_vhost *vh = context->vhost_list;
2781 const struct lws_context_per_thread *pt;
2782 time_t t = time(NULL);
2783 int n, listening = 0, cgi_count = 0;
2784 struct lws_conn_stats cs;
2787 struct lws_cgi * const *pcgi;
2790 #ifdef LWS_USE_LIBUV
2794 buf += lws_snprintf(buf, end - buf, "{ "
2795 "\"version\":\"%s\",\n"
2796 "\"uptime\":\"%ld\",\n",
2797 lws_get_library_version(),
2800 #ifdef LWS_HAVE_GETLOADAVG
2805 m = getloadavg(d, 3);
2806 for (n = 0; n < m; n++) {
2807 buf += lws_snprintf(buf, end - buf,
2808 "\"l%d\":\"%.2f\",\n",
2814 buf += lws_snprintf(buf, end - buf, "\"contexts\":[\n");
2816 buf += lws_snprintf(buf, end - buf, "{ "
2817 "\"context_uptime\":\"%ld\",\n"
2818 "\"cgi_spawned\":\"%d\",\n"
2819 "\"pt_fd_max\":\"%d\",\n"
2820 "\"ah_pool_max\":\"%d\",\n"
2821 "\"deprecated\":\"%d\",\n"
2822 "\"wsi_alive\":\"%d\",\n",
2823 (unsigned long)(t - context->time_up),
2824 context->count_cgi_spawned,
2825 context->fd_limit_per_thread,
2826 context->max_http_header_pool,
2827 context->deprecated,
2828 context->count_wsi_allocated);
2830 buf += lws_snprintf(buf, end - buf, "\"pt\":[\n ");
2831 for (n = 0; n < context->count_threads; n++) {
2832 pt = &context->pt[n];
2834 buf += lws_snprintf(buf, end - buf, ",");
2835 buf += lws_snprintf(buf, end - buf,
2837 " \"fds_count\":\"%d\",\n"
2838 " \"ah_pool_inuse\":\"%d\",\n"
2839 " \"ah_wait_list\":\"%d\"\n"
2842 pt->ah_count_in_use,
2843 pt->ah_wait_list_length);
2846 buf += lws_snprintf(buf, end - buf, "]");
2848 buf += lws_snprintf(buf, end - buf, ", \"vhosts\":[\n ");
2851 vh = context->vhost_list;
2853 cs = context->conn_stats;
2854 lws_sum_stats(context, &cs);
2861 buf += lws_json_dump_vhost(vh, buf, end - buf);
2866 vh = vh->vhost_next;
2869 buf += lws_snprintf(buf, end - buf,
2870 "],\n\"listen_wsi\":\"%d\",\n"
2871 " \"rx\":\"%llu\",\n"
2872 " \"tx\":\"%llu\",\n"
2873 " \"conn\":\"%lu\",\n"
2874 " \"trans\":\"%lu\",\n"
2875 " \"ws_upg\":\"%lu\",\n"
2876 " \"rejected\":\"%lu\",\n"
2877 " \"http2_upg\":\"%lu\"",
2879 cs.rx, cs.tx, cs.conn, cs.trans,
2880 cs.ws_upg, cs.rejected, cs.http2_upg);
2883 for (n = 0; n < context->count_threads; n++) {
2884 pt = &context->pt[n];
2885 pcgi = &pt->cgi_list;
2888 pcgi = &(*pcgi)->cgi_list;
2894 buf += lws_snprintf(buf, end - buf, ",\n \"cgi_alive\":\"%d\"\n ",
2897 buf += lws_snprintf(buf, end - buf, "}");
2900 buf += lws_snprintf(buf, end - buf, "]}\n ");