2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2015 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"
25 lws_calllback_as_writeable(struct lws *wsi)
31 n = LWS_CALLBACK_CLIENT_WRITEABLE;
33 case LWSCM_WSCL_ISSUE_HTTP_BODY:
34 n = LWS_CALLBACK_CLIENT_HTTP_WRITEABLE;
36 case LWSCM_WS_SERVING:
37 n = LWS_CALLBACK_SERVER_WRITEABLE;
40 n = LWS_CALLBACK_HTTP_WRITEABLE;
43 lwsl_debug("%s: %p (user=%p)\n", __func__, wsi, wsi->user_space);
44 return user_callback_handle_rxflow(wsi->protocol->callback,
45 wsi, (enum lws_callback_reasons) n,
46 wsi->user_space, NULL, 0);
50 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd)
52 int write_type = LWS_WRITE_PONG;
53 struct lws_tokens eff_buf;
59 //lwsl_err("%s: %p\n", __func__, wsi);
62 * user callback is lowest priority to get these notifications
63 * actually, since other pending things cannot be disordered
66 /* Priority 1: pending truncated sends are incomplete ws fragments
67 * If anything else sent first the protocol would be
71 if (lws_issue_raw(wsi, wsi->trunc_alloc + wsi->trunc_offset,
72 wsi->trunc_len) < 0) {
73 lwsl_info("%s signalling to close\n", __func__);
76 /* leave POLLOUT active either way */
79 if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE)
80 return -1; /* retry closing now */
82 if (wsi->mode == LWSCM_WSCL_ISSUE_HTTP_BODY)
87 /* Priority 2: protocol packets
90 lwsl_info("servicing pps %d\n", wsi->pps);
92 case LWS_PPS_HTTP2_MY_SETTINGS:
93 case LWS_PPS_HTTP2_ACK_SETTINGS:
94 lws_http2_do_pps_send(lws_get_context(wsi), wsi);
99 wsi->pps = LWS_PPS_NONE;
100 lws_rx_flow_control(wsi, 1);
102 return 0; /* leave POLLOUT active */
108 goto user_service_go_again;
111 /* Priority 3: pending control packets (pong or close)
113 if ((wsi->state == LWSS_ESTABLISHED &&
114 wsi->u.ws.ping_pending_flag) ||
115 (wsi->state == LWSS_RETURNED_CLOSE_ALREADY &&
116 wsi->u.ws.payload_is_close)) {
118 if (wsi->u.ws.payload_is_close)
119 write_type = LWS_WRITE_CLOSE;
121 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
122 wsi->u.ws.ping_payload_len, write_type);
126 /* well he is sent, mark him done */
127 wsi->u.ws.ping_pending_flag = 0;
128 if (wsi->u.ws.payload_is_close)
129 /* oh... a close frame was it... then we are done */
132 /* otherwise for PING, leave POLLOUT active either way */
136 if (wsi->state == LWSS_ESTABLISHED &&
137 !wsi->socket_is_permanently_unusable &&
138 wsi->u.ws.send_check_ping) {
140 lwsl_info("issuing ping on wsi %p\n", wsi);
141 wsi->u.ws.send_check_ping = 0;
142 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
148 * we apparently were able to send the PING in a reasonable time
149 * now reset the clock on our peer to be able to send the
150 * PONG in a reasonable time.
153 lws_set_timeout(wsi, PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG,
154 wsi->context->timeout_secs);
159 /* Priority 4: if we are closing, not allowed to send more data frags
160 * which means user callback or tx ext flush banned now
162 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
165 /* Priority 5: Tx path extension with more to send
167 * These are handled as new fragments each time around
168 * So while we must block new writeable callback to enforce
169 * payload ordering, but since they are always complete
170 * fragments control packets can interleave OK.
172 if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
173 lwsl_ext("SERVICING TX EXT DRAINING\n");
174 if (lws_write(wsi, NULL, 0, LWS_WRITE_CONTINUATION) < 0)
176 /* leave POLLOUT active */
180 /* Priority 6: user can get the callback
182 m = lws_ext_cb_active(wsi, LWS_EXT_CB_IS_WRITEABLE, NULL, 0);
185 #ifndef LWS_NO_EXTENSIONS
186 if (!wsi->extension_data_pending)
190 * check in on the active extensions, see if they
191 * had pending stuff to spill... they need to get the
192 * first look-in otherwise sequence will be disordered
194 * NULL, zero-length eff_buf means just spill pending
200 /* default to nobody has more to spill */
203 eff_buf.token = NULL;
204 eff_buf.token_len = 0;
206 /* give every extension a chance to spill */
208 m = lws_ext_cb_active(wsi,
209 LWS_EXT_CB_PACKET_TX_PRESEND,
212 lwsl_err("ext reports fatal error\n");
217 * at least one extension told us he has more
218 * to spill, so we will go around again after
222 /* assuming they gave us something to send, send it */
224 if (eff_buf.token_len) {
225 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
228 lwsl_info("closing from POLLOUT spill\n");
232 * Keep amount spilled small to minimize chance of this
234 if (n != eff_buf.token_len) {
235 lwsl_err("Unable to spill ext %d vs %s\n",
236 eff_buf.token_len, n);
242 /* no extension has more to spill */
248 * There's more to spill from an extension, but we just sent
249 * something... did that leave the pipe choked?
252 if (!lws_send_pipe_choked(wsi))
253 /* no we could add more */
256 lwsl_info("choked in POLLOUT service\n");
259 * Yes, he's choked. Leave the POLLOUT masked on so we will
260 * come back here when he is unchoked. Don't call the user
261 * callback to enforce ordering of spilling, he'll get called
262 * when we come back here and there's nothing more to spill.
267 #ifndef LWS_NO_EXTENSIONS
268 wsi->extension_data_pending = 0;
274 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
275 lwsl_info("failed at set pollfd\n");
279 if (wsi->mode != LWSCM_WSCL_ISSUE_HTTP_BODY &&
280 !wsi->hdr_parsing_completed)
284 user_service_go_again:
289 * we are the 'network wsi' for potentially many muxed child wsi with
290 * no network connection of their own, who have to use us for all their
291 * network actions. So we use a round-robin scheme to share out the
292 * POLLOUT notifications to our children.
294 * But because any child could exhaust the socket's ability to take
295 * writes, we can only let one child get notified each time.
297 * In addition children may be closed / deleted / added between POLLOUT
298 * notifications, so we can't hold pointers
301 if (wsi->mode != LWSCM_HTTP2_SERVING) {
302 lwsl_info("%s: non http2\n", __func__);
306 wsi->u.http2.requested_POLLOUT = 0;
307 if (!wsi->u.http2.initialized) {
308 lwsl_info("pollout on uninitialized http2 conn\n");
312 lwsl_info("%s: doing children\n", __func__);
316 wsi2 = wsi2->u.http2.next_child_wsi;
317 lwsl_info("%s: child %p\n", __func__, wsi2);
320 if (!wsi2->u.http2.requested_POLLOUT)
322 wsi2->u.http2.requested_POLLOUT = 0;
323 if (lws_calllback_as_writeable(wsi2)) {
324 lwsl_debug("Closing POLLOUT child\n");
325 lws_close_free_wsi(wsi2, LWS_CLOSE_STATUS_NOSTATUS);
328 } while (wsi2 != NULL && !lws_send_pipe_choked(wsi));
330 lwsl_info("%s: completed\n", __func__);
335 return lws_calllback_as_writeable(wsi);
339 lws_service_timeout_check(struct lws *wsi, unsigned int sec)
342 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
349 * if extensions want in on it (eg, we are a mux parent)
350 * give them a chance to service child timeouts
352 if (lws_ext_cb_active(wsi, LWS_EXT_CB_1HZ, NULL, sec) < 0)
355 if (!wsi->pending_timeout)
359 * if we went beyond the allowed time, kill the
362 if ((time_t)sec > wsi->pending_timeout_limit) {
364 if (wsi->sock != LWS_SOCK_INVALID && wsi->position_in_fds_table >= 0)
365 n = pt->fds[wsi->position_in_fds_table].events;
367 /* no need to log normal idle keepalive timeout */
368 if (wsi->pending_timeout != PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE)
369 lwsl_notice("wsi %p: TIMEDOUT WAITING on %d (did hdr %d, ah %p, wl %d, pfd events %d) %llu vs %llu\n",
370 (void *)wsi, wsi->pending_timeout,
371 wsi->hdr_parsing_completed, wsi->u.hdr.ah,
372 pt->ah_wait_list_length, n, (unsigned long long)sec, (unsigned long long)wsi->pending_timeout_limit);
375 * Since he failed a timeout, he already had a chance to do
376 * something and was unable to... that includes situations like
377 * half closed connections. So process this "failed timeout"
378 * close as a violent death and don't try to do protocol
379 * cleanup like flush partials.
381 wsi->socket_is_permanently_unusable = 1;
382 if (wsi->mode == LWSCM_WSCL_WAITING_SSL)
383 wsi->vhost->protocols[0].callback(wsi,
384 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
385 wsi->user_space, (void *)"Timed out waiting SSL", 21);
387 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
395 int lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len)
397 /* his RX is flowcontrolled, don't send remaining now */
398 if (wsi->rxflow_buffer) {
399 /* rxflow while we were spilling prev rxflow */
400 lwsl_info("stalling in existing rxflow buf\n");
404 /* a new rxflow, buffer it and warn caller */
405 lwsl_info("new rxflow input buffer len %d\n", len - n);
406 wsi->rxflow_buffer = lws_malloc(len - n);
407 if (!wsi->rxflow_buffer)
409 wsi->rxflow_len = len - n;
411 memcpy(wsi->rxflow_buffer, buf + n, len - n);
416 /* this is used by the platform service code to stop us waiting for network
417 * activity in poll() when we have something that already needs service
420 LWS_VISIBLE LWS_EXTERN int
421 lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi)
423 struct lws_context_per_thread *pt = &context->pt[tsi];
426 /* Figure out if we really want to wait in poll()
427 * We only need to wait if really nothing already to do and we have
428 * to wait for something from network
431 /* 1) if we know we are draining rx ext, do not wait in poll */
432 if (pt->rx_draining_ext_list)
435 #ifdef LWS_OPENSSL_SUPPORT
436 /* 2) if we know we have non-network pending data, do not wait in poll */
437 if (lws_ssl_anybody_has_buffered_read_tsi(context, tsi)) {
438 lwsl_info("ssl buffered read\n");
443 /* 3) if any ah has pending rx, do not wait in poll */
444 for (n = 0; n < context->max_http_header_pool; n++)
445 if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen) {
446 /* any ah with pending rx must be attached to someone */
447 if (!pt->ah_pool[n].wsi) {
448 lwsl_err("%s: assert: no wsi attached to ah\n", __func__);
458 * guys that need POLLIN service again without waiting for network action
459 * can force POLLIN here if not flowcontrolled, so they will get service.
461 * Return nonzero if anybody got their POLLIN faked
464 lws_service_flag_pending(struct lws_context *context, int tsi)
466 struct lws_context_per_thread *pt = &context->pt[tsi];
467 #ifdef LWS_OPENSSL_SUPPORT
468 struct lws *wsi_next;
477 * 1) For all guys with already-available ext data to drain, if they are
478 * not flowcontrolled, fake their POLLIN status
480 wsi = pt->rx_draining_ext_list;
482 pt->fds[wsi->position_in_fds_table].revents |=
483 pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
484 if (pt->fds[wsi->position_in_fds_table].revents &
487 wsi = wsi->u.ws.rx_draining_ext_list;
490 #ifdef LWS_OPENSSL_SUPPORT
492 * 2) For all guys with buffered SSL read data already saved up, if they
493 * are not flowcontrolled, fake their POLLIN status so they'll get
494 * service to use up the buffered incoming data, even though their
495 * network socket may have nothing
497 wsi = pt->pending_read_list;
499 wsi_next = wsi->pending_read_list_next;
500 pt->fds[wsi->position_in_fds_table].revents |=
501 pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
502 if (pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN) {
505 * he's going to get serviced now, take him off the
506 * list of guys with buffered SSL. If he still has some
507 * at the end of the service, he'll get put back on the
510 lws_ssl_remove_wsi_from_buffered_list(wsi);
517 * 3) For any wsi who have an ah with pending RX who did not
518 * complete their current headers, and are not flowcontrolled,
519 * fake their POLLIN status so they will be able to drain the
520 * rx buffered in the ah
522 for (n = 0; n < context->max_http_header_pool; n++)
523 if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen &&
524 !pt->ah_pool[n].wsi->hdr_parsing_completed) {
525 pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents |=
526 pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].events &
528 if (pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents &
536 #ifndef LWS_NO_CLIENT
539 lws_http_client_read(struct lws *wsi, char **buf, int *len)
543 rlen = lws_ssl_capable_read(wsi, (unsigned char *)*buf, *len);
546 /* allow the source to signal he has data again next time */
547 lws_change_pollfd(wsi, 0, LWS_POLLIN);
549 if (rlen == LWS_SSL_CAPABLE_ERROR) {
550 lwsl_notice("%s: SSL capable error\n", __func__);
558 wsi->client_rx_avail = 0;
561 * server may insist on transfer-encoding: chunked,
562 * so http client must deal with it
565 while (wsi->chunked && (wsi->chunk_parser != ELCP_CONTENT) && *len) {
566 switch (wsi->chunk_parser) {
568 if ((*buf)[0] == '\x0d') {
569 wsi->chunk_parser = ELCP_CR;
572 n = char_to_hex((*buf)[0]);
574 lwsl_debug("chunking failure\n");
577 wsi->chunk_remaining <<= 4;
578 wsi->chunk_remaining |= n;
581 if ((*buf)[0] != '\x0a') {
582 lwsl_debug("chunking failure\n");
585 wsi->chunk_parser = ELCP_CONTENT;
586 lwsl_info("chunk %d\n", wsi->chunk_remaining);
587 if (wsi->chunk_remaining)
589 lwsl_info("final chunk\n");
596 if ((*buf)[0] != '\x0d') {
597 lwsl_debug("chunking failure\n");
602 wsi->chunk_parser = ELCP_POST_LF;
606 if ((*buf)[0] != '\x0a')
609 wsi->chunk_parser = ELCP_HEX;
610 wsi->chunk_remaining = 0;
617 if (wsi->chunked && !wsi->chunk_remaining)
620 if (wsi->u.http.content_remain &&
621 (int)wsi->u.http.content_remain < *len)
622 n = wsi->u.http.content_remain;
626 if (wsi->chunked && wsi->chunk_remaining &&
627 wsi->chunk_remaining < n)
628 n = wsi->chunk_remaining;
630 #ifdef LWS_WITH_HTTP_PROXY
632 if (wsi->perform_rewrite)
633 lws_rewrite_parse(wsi->rw, (unsigned char *)*buf, n);
636 if (user_callback_handle_rxflow(wsi->protocol->callback,
637 wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
638 wsi->user_space, *buf, n)) {
639 lwsl_debug("%s: LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ returned -1\n", __func__);
644 if (wsi->chunked && wsi->chunk_remaining) {
646 wsi->chunk_remaining -= n;
650 if (wsi->chunked && !wsi->chunk_remaining)
651 wsi->chunk_parser = ELCP_POST_CR;
653 if (wsi->chunked && *len) {
660 wsi->u.http.content_remain -= n;
661 if (wsi->u.http.content_remain || !wsi->u.http.content_length)
665 if (user_callback_handle_rxflow(wsi->protocol->callback,
666 wsi, LWS_CALLBACK_COMPLETED_CLIENT_HTTP,
667 wsi->user_space, NULL, 0)) {
668 lwsl_debug("Completed call returned -1\n");
672 if (lws_http_transaction_completed_client(wsi)) {
673 lwsl_notice("%s: transaction completed says -1\n", __func__);
682 lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd, int tsi)
684 struct lws_context_per_thread *pt = &context->pt[tsi];
685 lws_sockfd_type our_fd = 0, tmp_fd;
686 struct lws_tokens eff_buf;
687 unsigned int pending = 0;
688 struct lws *wsi, *wsi1;
689 char draining_flow = 0;
695 if (!context->protocol_init_done)
696 lws_protocol_init(context);
701 * handle case that system time was uninitialized when lws started
702 * at boot, and got initialized a little later
704 if (context->time_up < 1464083026 && now > 1464083026)
705 context->time_up = now;
707 /* TODO: if using libev, we should probably use timeout watchers... */
708 if (context->last_timeout_check_s != now) {
709 context->last_timeout_check_s = now;
711 lws_plat_service_periodic(context);
713 /* retire unused deprecated context */
714 #ifndef LWS_PLAT_OPTEE
715 #if LWS_POSIX && !defined(_WIN32)
716 if (context->deprecated && !context->count_wsi_allocated) {
717 lwsl_notice("%s: ending deprecated context\n", __func__);
718 kill(getpid(), SIGINT);
723 /* global timeout check once per second */
728 wsi = context->pt[tsi].timeout_list;
730 /* we have to take copies, because he may be deleted */
731 wsi1 = wsi->timeout_list;
733 if (lws_service_timeout_check(wsi, (unsigned int)now)) {
734 /* he did time out... */
735 if (tmp_fd == our_fd)
736 /* it was the guy we came to service! */
738 /* he's gone, no need to mark as handled */
743 lws_cgi_kill_terminated(pt);
749 for (n = 0; n < context->count_threads; n++)
750 p += sprintf(p, " %7lu (%5d), ",
751 context->pt[n].count_conns,
752 context->pt[n].fds_count);
754 lwsl_notice("load: %s\n", s);
760 * at intervals, check for ws connections needing ping-pong checks
763 if (context->ws_ping_pong_interval &&
764 context->last_ws_ping_pong_check_s < now + 10) {
765 struct lws_vhost *vh = context->vhost_list;
766 context->last_ws_ping_pong_check_s = now;
769 for (n = 0; n < vh->count_protocols; n++) {
770 wsi = vh->same_vh_protocol_list[n];
773 if (wsi->state == LWSS_ESTABLISHED &&
774 !wsi->socket_is_permanently_unusable &&
775 !wsi->u.ws.send_check_ping &&
776 wsi->u.ws.time_next_ping_check &&
777 wsi->u.ws.time_next_ping_check < now) {
779 lwsl_info("requesting ping-pong on wsi %p\n", wsi);
780 wsi->u.ws.send_check_ping = 1;
781 lws_set_timeout(wsi, PENDING_TIMEOUT_WS_PONG_CHECK_SEND_PING,
782 context->timeout_secs);
783 lws_callback_on_writable(wsi);
784 wsi->u.ws.time_next_ping_check = now +
785 wsi->context->ws_ping_pong_interval;
787 wsi = wsi->same_vh_protocol_next;
794 /* the socket we came to service timed out, nothing to do */
798 /* just here for timeout management? */
802 /* no, here to service a socket descriptor */
803 wsi = wsi_from_fd(context, pollfd->fd);
805 /* not lws connection ... leave revents alone and return */
809 * so that caller can tell we handled, past here we need to
810 * zero down pollfd->revents after handling
814 /* handle session socket closed */
816 if ((!(pollfd->revents & pollfd->events & LWS_POLLIN)) &&
817 (pollfd->revents & LWS_POLLHUP)) {
818 wsi->socket_is_permanently_unusable = 1;
819 lwsl_debug("Session Socket %p (fd=%d) dead\n",
820 (void *)wsi, pollfd->fd);
822 goto close_and_handled;
826 if (pollfd->revents & LWS_POLLOUT)
827 wsi->sock_send_blocking = FALSE;
832 lwsl_debug("fd=%d, revents=%d\n", pollfd->fd, pollfd->revents);
834 /* okay, what we came here to do... */
837 case LWSCM_HTTP_SERVING:
838 case LWSCM_HTTP_CLIENT:
839 case LWSCM_HTTP_SERVING_ACCEPTED:
840 case LWSCM_SERVER_LISTENER:
841 case LWSCM_SSL_ACK_PENDING:
842 if (wsi->state == LWSS_CLIENT_HTTP_ESTABLISHED)
846 if (wsi->cgi && (pollfd->revents & LWS_POLLOUT)) {
847 n = lws_handle_POLLOUT_event(wsi, pollfd);
849 goto close_and_handled;
853 n = lws_server_socket_service(context, wsi, pollfd);
854 if (n) /* closed by above */
858 case LWSCM_WS_SERVING:
859 case LWSCM_WS_CLIENT:
860 case LWSCM_HTTP2_SERVING:
861 case LWSCM_HTTP_CLIENT_ACCEPTED:
863 /* 1: something requested a callback when it was OK to write */
865 if ((pollfd->revents & LWS_POLLOUT) &&
866 (wsi->state == LWSS_ESTABLISHED ||
867 wsi->state == LWSS_HTTP2_ESTABLISHED ||
868 wsi->state == LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS ||
869 wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
870 wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) &&
871 lws_handle_POLLOUT_event(wsi, pollfd)) {
872 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
873 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
874 lwsl_info("lws_service_fd: closing\n");
875 goto close_and_handled;
878 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
879 wsi->state == LWSS_AWAITING_CLOSE_ACK) {
881 * we stopped caring about anything except control
882 * packets. Force flow control off, defeat tx
885 lws_rx_flow_control(wsi, 1);
886 wsi->u.ws.tx_draining_ext = 0;
889 if (wsi->u.ws.tx_draining_ext)
890 /* we cannot deal with new RX until the TX ext
891 * path has been drained. It's because new
892 * rx will, eg, crap on the wsi rx buf that
893 * may be needed to retain state.
895 * TX ext drain path MUST go through event loop
900 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
901 /* We cannot deal with any kind of new RX
902 * because we are RX-flowcontrolled.
906 /* 2: RX Extension needs to be drained
909 if (wsi->state == LWSS_ESTABLISHED &&
910 wsi->u.ws.rx_draining_ext) {
912 lwsl_ext("%s: RX EXT DRAINING: Service\n", __func__);
913 #ifndef LWS_NO_CLIENT
914 if (wsi->mode == LWSCM_WS_CLIENT) {
915 n = lws_client_rx_sm(wsi, 0);
921 n = lws_rx_sm(wsi, 0);
926 if (wsi->u.ws.rx_draining_ext)
928 * We have RX EXT content to drain, but can't do it
929 * right now. That means we cannot do anything lower
934 /* 3: RX Flowcontrol buffer needs to be drained
937 if (wsi->rxflow_buffer) {
938 lwsl_info("draining rxflow (len %d)\n",
939 wsi->rxflow_len - wsi->rxflow_pos
942 eff_buf.token = (char *)wsi->rxflow_buffer +
944 eff_buf.token_len = wsi->rxflow_len - wsi->rxflow_pos;
949 /* 4: any incoming (or ah-stashed incoming rx) data ready?
950 * notice if rx flow going off raced poll(), rx flow wins
953 if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
957 /* all the union members start with hdr, so even in ws mode
958 * we can deal with the ah via u.hdr
961 lwsl_info("%s: %p: inherited ah rx\n", __func__, wsi);
962 eff_buf.token_len = wsi->u.hdr.ah->rxlen -
963 wsi->u.hdr.ah->rxpos;
964 eff_buf.token = (char *)wsi->u.hdr.ah->rx +
965 wsi->u.hdr.ah->rxpos;
967 if (wsi->mode != LWSCM_HTTP_CLIENT_ACCEPTED) {
968 eff_buf.token_len = lws_ssl_capable_read(wsi,
969 pt->serv_buf, pending ? pending :
970 context->pt_serv_buf_size);
971 switch (eff_buf.token_len) {
973 lwsl_info("%s: zero length read\n", __func__);
974 goto close_and_handled;
975 case LWS_SSL_CAPABLE_MORE_SERVICE:
976 lwsl_info("SSL Capable more service\n");
979 case LWS_SSL_CAPABLE_ERROR:
980 lwsl_info("Closing when error\n");
981 goto close_and_handled;
984 eff_buf.token = (char *)pt->serv_buf;
989 #ifndef LWS_NO_CLIENT
990 if (wsi->mode == LWSCM_HTTP_CLIENT_ACCEPTED &&
991 !wsi->told_user_closed) {
994 * In SSL mode we get POLLIN notification about
997 * But that is not necessarily related to decrypted
998 * data out becoming available; in may need to perform
999 * other in or out before that happens.
1001 * simply mark ourselves as having readable data
1002 * and turn off our POLLIN
1004 wsi->client_rx_avail = 1;
1005 lws_change_pollfd(wsi, LWS_POLLIN, 0);
1007 /* let user code know, he'll usually ask for writeable
1008 * callback and drain / re-enable it there
1010 if (user_callback_handle_rxflow(
1011 wsi->protocol->callback,
1012 wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP,
1013 wsi->user_space, NULL, 0)) {
1014 lwsl_debug("LWS_CALLBACK_RECEIVE_CLIENT_HTTP closed it\n");
1015 goto close_and_handled;
1020 * give any active extensions a chance to munge the buffer
1021 * before parse. We pass in a pointer to an lws_tokens struct
1022 * prepared with the default buffer and content length that's in
1023 * there. Rather than rewrite the default buffer, extensions
1024 * that expect to grow the buffer can adapt .token to
1025 * point to their own per-connection buffer in the extension
1026 * user allocation. By default with no extensions or no
1027 * extension callback handling, just the normal input buffer is
1028 * used then so it is efficient.
1033 m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_RX_PREPARSE,
1036 goto close_and_handled;
1040 /* service incoming data */
1042 if (eff_buf.token_len) {
1044 * if draining from rxflow buffer, not
1045 * critical to track what was used since at the
1046 * use it bumps wsi->rxflow_pos. If we come
1047 * around again it will pick up from where it
1050 n = lws_read(wsi, (unsigned char *)eff_buf.token,
1059 eff_buf.token = NULL;
1060 eff_buf.token_len = 0;
1063 if (wsi->u.hdr.ah) {
1064 lwsl_notice("%s: %p: detaching\n",
1066 /* show we used all the pending rx up */
1067 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1068 /* we can run the normal ah detach flow despite
1069 * being in ws union mode, since all union members
1071 lws_header_table_detach(wsi, 0);
1074 pending = lws_ssl_pending(wsi);
1076 pending = pending > context->pt_serv_buf_size ?
1077 context->pt_serv_buf_size : pending;
1081 if (draining_flow && wsi->rxflow_buffer &&
1082 wsi->rxflow_pos == wsi->rxflow_len) {
1083 lwsl_info("flow buffer: drained\n");
1084 lws_free_set_NULL(wsi->rxflow_buffer);
1085 /* having drained the rxflow buffer, can rearm POLLIN */
1086 #ifdef LWS_NO_SERVER
1089 _lws_rx_flow_control(wsi);
1090 /* n ignored, needed for NO_SERVER case */
1095 case LWSCM_CGI: /* we exist to handle a cgi's stdin/out/err data...
1096 * do the callback on our master wsi
1099 struct lws_cgi_args args;
1101 if (wsi->cgi_channel >= LWS_STDOUT &&
1102 !(pollfd->revents & pollfd->events & LWS_POLLIN))
1104 if (wsi->cgi_channel == LWS_STDIN &&
1105 !(pollfd->revents & pollfd->events & LWS_POLLOUT))
1108 if (wsi->cgi_channel == LWS_STDIN)
1109 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
1110 lwsl_info("failed at set pollfd\n");
1114 args.ch = wsi->cgi_channel;
1115 args.stdwsi = &wsi->parent->cgi->stdwsi[0];
1116 args.hdr_state = wsi->hdr_state;
1118 //lwsl_err("CGI LWS_STDOUT waiting wsi %p mode %d state %d\n",
1119 // wsi->parent, wsi->parent->mode, wsi->parent->state);
1121 if (user_callback_handle_rxflow(
1122 wsi->parent->protocol->callback,
1123 wsi->parent, LWS_CALLBACK_CGI,
1124 wsi->parent->user_space,
1132 #ifdef LWS_NO_CLIENT
1135 if ((pollfd->revents & LWS_POLLOUT) &&
1136 lws_handle_POLLOUT_event(wsi, pollfd)) {
1137 lwsl_debug("POLLOUT event closed it\n");
1138 goto close_and_handled;
1141 n = lws_client_socket_service(context, wsi, pollfd);
1152 lwsl_debug("Close and handled\n");
1153 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
1155 * pollfd may point to something else after the close
1156 * due to pollfd swapping scheme on delete on some platforms
1157 * we can't clear revents now because it'd be the wrong guy's revents
1162 pollfd->revents = 0;
1167 lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd)
1169 return lws_service_fd_tsi(context, pollfd, 0);
1173 lws_service(struct lws_context *context, int timeout_ms)
1175 return lws_plat_service(context, timeout_ms);
1179 lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
1181 return _lws_plat_service_tsi(context, timeout_ms, tsi);