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_WS_SERVING:
34 n = LWS_CALLBACK_SERVER_WRITEABLE;
37 n = LWS_CALLBACK_HTTP_WRITEABLE;
40 lwsl_debug("%s: %p (user=%p)\n", __func__, wsi, wsi->user_space);
41 return user_callback_handle_rxflow(wsi->protocol->callback,
42 wsi, (enum lws_callback_reasons) n,
43 wsi->user_space, NULL, 0);
47 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd)
49 int write_type = LWS_WRITE_PONG;
50 struct lws_tokens eff_buf;
56 //lwsl_err("%s: %p\n", __func__, wsi);
59 * user callback is lowest priority to get these notifications
60 * actually, since other pending things cannot be disordered
63 /* Priority 1: pending truncated sends are incomplete ws fragments
64 * If anything else sent first the protocol would be
68 if (lws_issue_raw(wsi, wsi->trunc_alloc + wsi->trunc_offset,
69 wsi->trunc_len) < 0) {
70 lwsl_info("%s signalling to close\n", __func__);
73 /* leave POLLOUT active either way */
76 if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE)
77 return -1; /* retry closing now */
81 /* Priority 2: protocol packets
84 lwsl_info("servicing pps %d\n", wsi->pps);
86 case LWS_PPS_HTTP2_MY_SETTINGS:
87 case LWS_PPS_HTTP2_ACK_SETTINGS:
88 lws_http2_do_pps_send(lws_get_context(wsi), wsi);
93 wsi->pps = LWS_PPS_NONE;
94 lws_rx_flow_control(wsi, 1);
96 return 0; /* leave POLLOUT active */
102 goto user_service_go_again;
105 /* Priority 3: pending control packets (pong or close)
107 if ((wsi->state == LWSS_ESTABLISHED &&
108 wsi->u.ws.ping_pending_flag) ||
109 (wsi->state == LWSS_RETURNED_CLOSE_ALREADY &&
110 wsi->u.ws.payload_is_close)) {
112 if (wsi->u.ws.payload_is_close)
113 write_type = LWS_WRITE_CLOSE;
115 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
116 wsi->u.ws.ping_payload_len, write_type);
120 /* well he is sent, mark him done */
121 wsi->u.ws.ping_pending_flag = 0;
122 if (wsi->u.ws.payload_is_close)
123 /* oh... a close frame was it... then we are done */
126 /* otherwise for PING, leave POLLOUT active either way */
130 /* Priority 4: if we are closing, not allowed to send more data frags
131 * which means user callback or tx ext flush banned now
133 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
136 /* Priority 5: Tx path extension with more to send
138 * These are handled as new fragments each time around
139 * So while we must block new writeable callback to enforce
140 * payload ordering, but since they are always complete
141 * fragments control packets can interleave OK.
143 if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
144 lwsl_ext("SERVICING TX EXT DRAINING\n");
145 if (lws_write(wsi, NULL, 0, LWS_WRITE_CONTINUATION) < 0)
147 /* leave POLLOUT active */
151 /* Priority 6: user can get the callback
153 m = lws_ext_cb_active(wsi, LWS_EXT_CB_IS_WRITEABLE, NULL, 0);
156 #ifndef LWS_NO_EXTENSIONS
157 if (!wsi->extension_data_pending)
161 * check in on the active extensions, see if they
162 * had pending stuff to spill... they need to get the
163 * first look-in otherwise sequence will be disordered
165 * NULL, zero-length eff_buf means just spill pending
171 /* default to nobody has more to spill */
174 eff_buf.token = NULL;
175 eff_buf.token_len = 0;
177 /* give every extension a chance to spill */
179 m = lws_ext_cb_active(wsi,
180 LWS_EXT_CB_PACKET_TX_PRESEND,
183 lwsl_err("ext reports fatal error\n");
188 * at least one extension told us he has more
189 * to spill, so we will go around again after
193 /* assuming they gave us something to send, send it */
195 if (eff_buf.token_len) {
196 n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
199 lwsl_info("closing from POLLOUT spill\n");
203 * Keep amount spilled small to minimize chance of this
205 if (n != eff_buf.token_len) {
206 lwsl_err("Unable to spill ext %d vs %s\n",
207 eff_buf.token_len, n);
213 /* no extension has more to spill */
219 * There's more to spill from an extension, but we just sent
220 * something... did that leave the pipe choked?
223 if (!lws_send_pipe_choked(wsi))
224 /* no we could add more */
227 lwsl_info("choked in POLLOUT service\n");
230 * Yes, he's choked. Leave the POLLOUT masked on so we will
231 * come back here when he is unchoked. Don't call the user
232 * callback to enforce ordering of spilling, he'll get called
233 * when we come back here and there's nothing more to spill.
238 #ifndef LWS_NO_EXTENSIONS
239 wsi->extension_data_pending = 0;
245 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
246 lwsl_info("failed at set pollfd\n");
251 if (!wsi->hdr_parsing_completed)
255 user_service_go_again:
260 * we are the 'network wsi' for potentially many muxed child wsi with
261 * no network connection of their own, who have to use us for all their
262 * network actions. So we use a round-robin scheme to share out the
263 * POLLOUT notifications to our children.
265 * But because any child could exhaust the socket's ability to take
266 * writes, we can only let one child get notified each time.
268 * In addition children may be closed / deleted / added between POLLOUT
269 * notifications, so we can't hold pointers
272 if (wsi->mode != LWSCM_HTTP2_SERVING) {
273 lwsl_info("%s: non http2\n", __func__);
277 wsi->u.http2.requested_POLLOUT = 0;
278 if (!wsi->u.http2.initialized) {
279 lwsl_info("pollout on uninitialized http2 conn\n");
283 lwsl_info("%s: doing children\n", __func__);
287 wsi2 = wsi2->u.http2.next_child_wsi;
288 lwsl_info("%s: child %p\n", __func__, wsi2);
291 if (!wsi2->u.http2.requested_POLLOUT)
293 wsi2->u.http2.requested_POLLOUT = 0;
294 if (lws_calllback_as_writeable(wsi2)) {
295 lwsl_debug("Closing POLLOUT child\n");
296 lws_close_free_wsi(wsi2, LWS_CLOSE_STATUS_NOSTATUS);
299 } while (wsi2 != NULL && !lws_send_pipe_choked(wsi));
301 lwsl_info("%s: completed\n", __func__);
306 return lws_calllback_as_writeable(wsi);
310 lws_service_timeout_check(struct lws *wsi, unsigned int sec)
312 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
315 * if extensions want in on it (eg, we are a mux parent)
316 * give them a chance to service child timeouts
318 if (lws_ext_cb_active(wsi, LWS_EXT_CB_1HZ, NULL, sec) < 0)
321 if (!wsi->pending_timeout)
325 * if we went beyond the allowed time, kill the
328 if ((time_t)sec > wsi->pending_timeout_limit) {
330 /* no need to log normal idle keepalive timeout */
331 if (wsi->pending_timeout != PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE)
332 lwsl_notice("wsi %p: TIMEDOUT WAITING on %d (did hdr %d, ah %p, wl %d, pfd events %d)\n",
333 (void *)wsi, wsi->pending_timeout,
334 wsi->hdr_parsing_completed, wsi->u.hdr.ah,
335 pt->ah_wait_list_length,
336 pt->fds[wsi->sock].events);
339 * Since he failed a timeout, he already had a chance to do
340 * something and was unable to... that includes situations like
341 * half closed connections. So process this "failed timeout"
342 * close as a violent death and don't try to do protocol
343 * cleanup like flush partials.
345 wsi->socket_is_permanently_unusable = 1;
346 if (wsi->mode == LWSCM_WSCL_WAITING_SSL)
347 wsi->vhost->protocols[0].callback(wsi,
348 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
349 wsi->user_space, NULL, 0);
351 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
359 int lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len)
361 /* his RX is flowcontrolled, don't send remaining now */
362 if (wsi->rxflow_buffer) {
363 /* rxflow while we were spilling prev rxflow */
364 lwsl_info("stalling in existing rxflow buf\n");
368 /* a new rxflow, buffer it and warn caller */
369 lwsl_info("new rxflow input buffer len %d\n", len - n);
370 wsi->rxflow_buffer = lws_malloc(len - n);
371 wsi->rxflow_len = len - n;
373 memcpy(wsi->rxflow_buffer, buf + n, len - n);
378 /* this is used by the platform service code to stop us waiting for network
379 * activity in poll() when we have something that already needs service
383 lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi)
385 struct lws_context_per_thread *pt = &context->pt[tsi];
388 /* Figure out if we really want to wait in poll()
389 * We only need to wait if really nothing already to do and we have
390 * to wait for something from network
393 /* 1) if we know we are draining rx ext, do not wait in poll */
394 if (pt->rx_draining_ext_list)
397 #ifdef LWS_OPENSSL_SUPPORT
398 /* 2) if we know we have non-network pending data, do not wait in poll */
399 if (lws_ssl_anybody_has_buffered_read_tsi(context, tsi)) {
400 lwsl_info("ssl buffered read\n");
405 /* 3) if any ah has pending rx, do not wait in poll */
406 for (n = 0; n < context->max_http_header_pool; n++)
407 if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen) {
408 /* any ah with pending rx must be attached to someone */
409 assert(pt->ah_pool[n].wsi);
417 * guys that need POLLIN service again without waiting for network action
418 * can force POLLIN here if not flowcontrolled, so they will get service.
420 * Return nonzero if anybody got their POLLIN faked
423 lws_service_flag_pending(struct lws_context *context, int tsi)
425 struct lws_context_per_thread *pt = &context->pt[tsi];
426 #ifdef LWS_OPENSSL_SUPPORT
427 struct lws *wsi_next;
436 * 1) For all guys with already-available ext data to drain, if they are
437 * not flowcontrolled, fake their POLLIN status
439 wsi = pt->rx_draining_ext_list;
441 pt->fds[wsi->position_in_fds_table].revents |=
442 pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
443 if (pt->fds[wsi->position_in_fds_table].revents &
446 wsi = wsi->u.ws.rx_draining_ext_list;
449 #ifdef LWS_OPENSSL_SUPPORT
451 * 2) For all guys with buffered SSL read data already saved up, if they
452 * are not flowcontrolled, fake their POLLIN status so they'll get
453 * service to use up the buffered incoming data, even though their
454 * network socket may have nothing
456 wsi = pt->pending_read_list;
458 wsi_next = wsi->pending_read_list_next;
459 pt->fds[wsi->position_in_fds_table].revents |=
460 pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
461 if (pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN) {
464 * he's going to get serviced now, take him off the
465 * list of guys with buffered SSL. If he still has some
466 * at the end of the service, he'll get put back on the
469 lws_ssl_remove_wsi_from_buffered_list(wsi);
476 * 3) For any wsi who have an ah with pending RX who did not
477 * complete their current headers, and are not flowcontrolled,
478 * fake their POLLIN status so they will be able to drain the
479 * rx buffered in the ah
481 for (n = 0; n < context->max_http_header_pool; n++)
482 if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen &&
483 !pt->ah_pool[n].wsi->hdr_parsing_completed) {
484 pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents |=
485 pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].events &
487 if (pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents &
495 #ifndef LWS_NO_CLIENT
498 lws_http_client_read(struct lws *wsi, char **buf, int *len)
504 rlen = lws_ssl_capable_read(wsi, (unsigned char *)*buf, *len);
512 // lwsl_err("%s: read %d\n", __func__, rlen);
514 /* allow the source to signal he has data again next time */
515 wsi->client_rx_avail = 0;
516 lws_change_pollfd(wsi, 0, LWS_POLLIN);
519 * server may insist on transfer-encoding: chunked,
520 * so http client must deal with it
523 while (wsi->chunked && (wsi->chunk_parser != ELCP_CONTENT) && *len) {
524 switch (wsi->chunk_parser) {
526 if ((*buf)[0] == '\x0d') {
527 wsi->chunk_parser = ELCP_CR;
530 n = char_to_hex((*buf)[0]);
533 wsi->chunk_remaining <<= 4;
534 wsi->chunk_remaining |= n;
537 if ((*buf)[0] != '\x0a')
539 wsi->chunk_parser = ELCP_CONTENT;
540 lwsl_info("chunk %d\n", wsi->chunk_remaining);
541 if (wsi->chunk_remaining)
543 lwsl_info("final chunk\n");
550 if ((*buf)[0] != '\x0d')
553 wsi->chunk_parser = ELCP_POST_LF;
557 if ((*buf)[0] != '\x0a')
560 wsi->chunk_parser = ELCP_HEX;
561 wsi->chunk_remaining = 0;
568 if (wsi->chunked && !wsi->chunk_remaining)
571 if (wsi->u.http.content_remain &&
572 (int)wsi->u.http.content_remain < *len)
573 n = wsi->u.http.content_remain;
577 if (wsi->chunked && wsi->chunk_remaining &&
578 wsi->chunk_remaining < n)
579 n = wsi->chunk_remaining;
581 #ifdef LWS_WITH_HTTP_PROXY
583 if (wsi->perform_rewrite)
584 lws_rewrite_parse(wsi->rw, (unsigned char *)*buf, n);
587 if (user_callback_handle_rxflow(wsi->protocol->callback,
588 wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
589 wsi->user_space, *buf, n))
592 if (wsi->chunked && wsi->chunk_remaining) {
594 wsi->chunk_remaining -= n;
598 if (wsi->chunked && !wsi->chunk_remaining)
599 wsi->chunk_parser = ELCP_POST_CR;
601 if (wsi->chunked && *len) {
608 wsi->u.http.content_remain -= n;
609 if (wsi->u.http.content_remain || !wsi->u.http.content_length)
613 if (user_callback_handle_rxflow(wsi->protocol->callback,
614 wsi, LWS_CALLBACK_COMPLETED_CLIENT_HTTP,
615 wsi->user_space, NULL, 0))
618 if (lws_http_transaction_completed_client(wsi))
626 * lws_service_fd() - Service polled socket with something waiting
627 * @context: Websocket context
628 * @pollfd: The pollfd entry describing the socket fd and which events
631 * This function takes a pollfd that has POLLIN or POLLOUT activity and
632 * services it according to the state of the associated
635 * The one call deals with all "service" that might happen on a socket
636 * including listen accepts, http files as well as websocket protocol.
638 * If a pollfd says it has something, you can just pass it to
639 * lws_service_fd() whether it is a socket handled by lws or not.
640 * If it sees it is a lws socket, the traffic will be handled and
641 * pollfd->revents will be zeroed now.
643 * If the socket is foreign to lws, it leaves revents alone. So you can
644 * see if you should service yourself by checking the pollfd revents
645 * after letting lws try to service it.
649 lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd, int tsi)
651 struct lws_context_per_thread *pt = &context->pt[tsi];
652 lws_sockfd_type our_fd = 0, tmp_fd;
653 struct lws_tokens eff_buf;
654 unsigned int pending = 0;
655 struct lws *wsi, *wsi1;
656 char draining_flow = 0;
662 if (!context->protocol_init_done)
663 lws_protocol_init(context);
666 * you can call us with pollfd = NULL to just allow the once-per-second
667 * global timeout checks; if less than a second since the last check
668 * it returns immediately then.
673 /* TODO: if using libev, we should probably use timeout watchers... */
674 if (context->last_timeout_check_s != now) {
675 context->last_timeout_check_s = now;
677 lws_plat_service_periodic(context);
679 /* global timeout check once per second */
684 wsi = context->pt[tsi].timeout_list;
686 /* we have to take copies, because he may be deleted */
687 wsi1 = wsi->timeout_list;
689 if (lws_service_timeout_check(wsi, (unsigned int)now)) {
690 /* he did time out... */
691 if (tmp_fd == our_fd)
692 /* it was the guy we came to service! */
694 /* he's gone, no need to mark as handled */
699 lws_cgi_kill_terminated(pt);
705 for (n = 0; n < context->count_threads; n++)
706 p += sprintf(p, " %7lu (%5d), ",
707 context->pt[n].count_conns,
708 context->pt[n].fds_count);
710 lwsl_notice("load: %s\n", s);
715 /* the socket we came to service timed out, nothing to do */
719 /* just here for timeout management? */
723 /* no, here to service a socket descriptor */
724 wsi = wsi_from_fd(context, pollfd->fd);
726 /* not lws connection ... leave revents alone and return */
730 * so that caller can tell we handled, past here we need to
731 * zero down pollfd->revents after handling
735 /* handle session socket closed */
737 if ((!(pollfd->revents & pollfd->events & LWS_POLLIN)) &&
738 (pollfd->revents & LWS_POLLHUP)) {
739 wsi->socket_is_permanently_unusable = 1;
740 lwsl_debug("Session Socket %p (fd=%d) dead\n",
741 (void *)wsi, pollfd->fd);
743 goto close_and_handled;
747 if (pollfd->revents & LWS_POLLOUT)
748 wsi->sock_send_blocking = FALSE;
753 lwsl_debug("fd=%d, revents=%d\n", pollfd->fd, pollfd->revents);
755 /* okay, what we came here to do... */
758 case LWSCM_HTTP_SERVING:
759 case LWSCM_HTTP_CLIENT:
760 case LWSCM_HTTP_SERVING_ACCEPTED:
761 case LWSCM_SERVER_LISTENER:
762 case LWSCM_SSL_ACK_PENDING:
763 if (wsi->state == LWSS_CLIENT_HTTP_ESTABLISHED)
767 if (wsi->cgi && (pollfd->revents & LWS_POLLOUT)) {
768 n = lws_handle_POLLOUT_event(wsi, pollfd);
770 goto close_and_handled;
774 n = lws_server_socket_service(context, wsi, pollfd);
775 if (n) /* closed by above */
779 case LWSCM_WS_SERVING:
780 case LWSCM_WS_CLIENT:
781 case LWSCM_HTTP2_SERVING:
782 case LWSCM_HTTP_CLIENT_ACCEPTED:
784 /* 1: something requested a callback when it was OK to write */
786 if ((pollfd->revents & LWS_POLLOUT) &&
787 (wsi->state == LWSS_ESTABLISHED ||
788 wsi->state == LWSS_HTTP2_ESTABLISHED ||
789 wsi->state == LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS ||
790 wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
791 wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) &&
792 lws_handle_POLLOUT_event(wsi, pollfd)) {
793 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
794 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
795 lwsl_info("lws_service_fd: closing\n");
796 goto close_and_handled;
799 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
800 wsi->state == LWSS_AWAITING_CLOSE_ACK) {
802 * we stopped caring about anything except control
803 * packets. Force flow control off, defeat tx
806 lws_rx_flow_control(wsi, 1);
807 wsi->u.ws.tx_draining_ext = 0;
810 if (wsi->u.ws.tx_draining_ext)
811 /* we cannot deal with new RX until the TX ext
812 * path has been drained. It's because new
813 * rx will, eg, crap on the wsi rx buf that
814 * may be needed to retain state.
816 * TX ext drain path MUST go through event loop
821 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
822 /* We cannot deal with any kind of new RX
823 * because we are RX-flowcontrolled.
827 /* 2: RX Extension needs to be drained
830 if (wsi->state == LWSS_ESTABLISHED &&
831 wsi->u.ws.rx_draining_ext) {
833 lwsl_ext("%s: RX EXT DRAINING: Service\n", __func__);
834 #ifndef LWS_NO_CLIENT
835 if (wsi->mode == LWSCM_WS_CLIENT) {
836 n = lws_client_rx_sm(wsi, 0);
842 n = lws_rx_sm(wsi, 0);
847 if (wsi->u.ws.rx_draining_ext)
849 * We have RX EXT content to drain, but can't do it
850 * right now. That means we cannot do anything lower
855 /* 3: RX Flowcontrol buffer needs to be drained
858 if (wsi->rxflow_buffer) {
859 lwsl_info("draining rxflow (len %d)\n",
860 wsi->rxflow_len - wsi->rxflow_pos
863 eff_buf.token = (char *)wsi->rxflow_buffer +
865 eff_buf.token_len = wsi->rxflow_len - wsi->rxflow_pos;
870 /* 4: any incoming (or ah-stashed incoming rx) data ready?
871 * notice if rx flow going off raced poll(), rx flow wins
874 if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
878 /* all the union members start with hdr, so even in ws mode
879 * we can deal with the ah via u.hdr
882 lwsl_info("%s: %p: inherited ah rx\n", __func__, wsi);
883 eff_buf.token_len = wsi->u.hdr.ah->rxlen -
884 wsi->u.hdr.ah->rxpos;
885 eff_buf.token = (char *)wsi->u.hdr.ah->rx +
886 wsi->u.hdr.ah->rxpos;
888 if (wsi->mode != LWSCM_HTTP_CLIENT_ACCEPTED) {
889 eff_buf.token_len = lws_ssl_capable_read(wsi,
890 pt->serv_buf, pending ? pending :
891 LWS_MAX_SOCKET_IO_BUF);
892 switch (eff_buf.token_len) {
894 lwsl_info("%s: zero length read\n", __func__);
895 goto close_and_handled;
896 case LWS_SSL_CAPABLE_MORE_SERVICE:
897 lwsl_info("SSL Capable more service\n");
900 case LWS_SSL_CAPABLE_ERROR:
901 lwsl_info("Closing when error\n");
902 goto close_and_handled;
905 eff_buf.token = (char *)pt->serv_buf;
910 #ifndef LWS_NO_CLIENT
911 if (wsi->mode == LWSCM_HTTP_CLIENT_ACCEPTED) {
914 * simply mark ourselves as having readable data
915 * and turn off our POLLIN
917 wsi->client_rx_avail = 1;
918 lws_change_pollfd(wsi, LWS_POLLIN, 0);
920 /* let user code know, he'll usually ask for writeable
921 * callback and drain / reenable it there
923 if (user_callback_handle_rxflow(
924 wsi->protocol->callback,
925 wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP,
926 wsi->user_space, NULL, 0))
927 goto close_and_handled;
931 * give any active extensions a chance to munge the buffer
932 * before parse. We pass in a pointer to an lws_tokens struct
933 * prepared with the default buffer and content length that's in
934 * there. Rather than rewrite the default buffer, extensions
935 * that expect to grow the buffer can adapt .token to
936 * point to their own per-connection buffer in the extension
937 * user allocation. By default with no extensions or no
938 * extension callback handling, just the normal input buffer is
939 * used then so it is efficient.
944 m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_RX_PREPARSE,
947 goto close_and_handled;
951 /* service incoming data */
953 if (eff_buf.token_len) {
955 * if draining from rxflow buffer, not
956 * critical to track what was used since at the
957 * use it bumps wsi->rxflow_pos. If we come
958 * around again it will pick up from where it
961 n = lws_read(wsi, (unsigned char *)eff_buf.token,
970 eff_buf.token = NULL;
971 eff_buf.token_len = 0;
975 lwsl_info("%s: %p: detaching inherited used ah\n",
977 /* show we used all the pending rx up */
978 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
979 /* we can run the normal ah detach flow despite
980 * being in ws union mode, since all union members
982 lws_header_table_detach(wsi, 0);
985 pending = lws_ssl_pending(wsi);
987 pending = pending > LWS_MAX_SOCKET_IO_BUF ?
988 LWS_MAX_SOCKET_IO_BUF : pending;
992 if (draining_flow && wsi->rxflow_buffer &&
993 wsi->rxflow_pos == wsi->rxflow_len) {
994 lwsl_info("flow buffer: drained\n");
995 lws_free_set_NULL(wsi->rxflow_buffer);
996 /* having drained the rxflow buffer, can rearm POLLIN */
1000 _lws_rx_flow_control(wsi);
1001 /* n ignored, needed for NO_SERVER case */
1006 case LWSCM_CGI: /* we exist to handle a cgi's stdin/out/err data...
1007 * do the callback on our master wsi
1010 struct lws_cgi_args args;
1012 if (wsi->cgi_channel >= LWS_STDOUT &&
1013 !(pollfd->revents & pollfd->events & LWS_POLLIN))
1015 if (wsi->cgi_channel == LWS_STDIN &&
1016 !(pollfd->revents & pollfd->events & LWS_POLLOUT))
1019 if (wsi->cgi_channel == LWS_STDIN)
1020 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
1021 lwsl_info("failed at set pollfd\n");
1025 args.ch = wsi->cgi_channel;
1026 args.stdwsi = &wsi->parent->cgi->stdwsi[0];
1027 args.hdr_state = wsi->hdr_state;
1029 //lwsl_err("CGI LWS_STDOUT waiting wsi %p mode %d state %d\n",
1030 // wsi->parent, wsi->parent->mode, wsi->parent->state);
1032 if (user_callback_handle_rxflow(
1033 wsi->parent->protocol->callback,
1034 wsi->parent, LWS_CALLBACK_CGI,
1035 wsi->parent->user_space,
1043 #ifdef LWS_NO_CLIENT
1046 n = lws_client_socket_service(context, wsi, pollfd);
1057 lwsl_debug("Close and handled\n");
1058 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
1060 * pollfd may point to something else after the close
1061 * due to pollfd swapping scheme on delete on some platforms
1062 * we can't clear revents now because it'd be the wrong guy's revents
1067 pollfd->revents = 0;
1072 lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd)
1074 return lws_service_fd_tsi(context, pollfd, 0);
1078 * lws_service() - Service any pending websocket activity
1079 * @context: Websocket context
1080 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1081 * service otherwise block and service immediately, returning
1082 * after the timeout if nothing needed service.
1084 * This function deals with any pending websocket traffic, for three
1085 * kinds of event. It handles these events on both server and client
1086 * types of connection the same.
1088 * 1) Accept new connections to our context's server
1090 * 2) Call the receive callback for incoming frame data received by
1091 * server or client connections.
1093 * You need to call this service function periodically to all the above
1094 * functions to happen; if your application is single-threaded you can
1095 * just call it in your main event loop.
1097 * Alternatively you can fork a new process that asynchronously handles
1098 * calling this service in a loop. In that case you are happy if this
1099 * call blocks your thread until it needs to take care of something and
1100 * would call it with a large nonzero timeout. Your loop then takes no
1101 * CPU while there is nothing happening.
1103 * If you are calling it in a single-threaded app, you don't want it to
1104 * wait around blocking other things in your loop from happening, so you
1105 * would call it with a timeout_ms of 0, so it returns immediately if
1106 * nothing is pending, or as soon as it services whatever was pending.
1110 lws_service(struct lws_context *context, int timeout_ms)
1112 return lws_plat_service(context, timeout_ms);
1116 lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
1118 return lws_plat_service_tsi(context, timeout_ms, tsi);