lws-vhost-destroy
[platform/upstream/libwebsockets.git] / lib / service.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
5  *
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.
10  *
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.
15  *
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,
19  *  MA  02110-1301  USA
20  */
21
22 #include "private-libwebsockets.h"
23
24 static int
25 lws_calllback_as_writeable(struct lws *wsi)
26 {
27         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
28         int n;
29
30         lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB, 1);
31 #if defined(LWS_WITH_STATS)
32         {
33                 uint64_t ul = time_in_microseconds() - wsi->active_writable_req_us;
34
35                 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_MS_WRITABLE_DELAY, ul);
36                 lws_stats_atomic_max(wsi->context, pt, LWSSTATS_MS_WORST_WRITABLE_DELAY, ul);
37                 wsi->active_writable_req_us = 0;
38         }
39 #endif
40
41         switch (wsi->mode) {
42         case LWSCM_RAW:
43                 n = LWS_CALLBACK_RAW_WRITEABLE;
44                 break;
45         case LWSCM_RAW_FILEDESC:
46                 n = LWS_CALLBACK_RAW_WRITEABLE_FILE;
47                 break;
48         case LWSCM_WS_CLIENT:
49                 n = LWS_CALLBACK_CLIENT_WRITEABLE;
50                 break;
51         case LWSCM_WSCL_ISSUE_HTTP_BODY:
52                 n = LWS_CALLBACK_CLIENT_HTTP_WRITEABLE;
53                 break;
54         case LWSCM_WS_SERVING:
55                 n = LWS_CALLBACK_SERVER_WRITEABLE;
56                 break;
57         default:
58                 n = LWS_CALLBACK_HTTP_WRITEABLE;
59                 break;
60         }
61         lwsl_debug("%s: %p (user=%p)\n", __func__, wsi, wsi->user_space);
62         return user_callback_handle_rxflow(wsi->protocol->callback,
63                                            wsi, (enum lws_callback_reasons) n,
64                                            wsi->user_space, NULL, 0);
65 }
66
67 int
68 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd)
69 {
70         int write_type = LWS_WRITE_PONG;
71         struct lws_tokens eff_buf;
72 #ifdef LWS_USE_HTTP2
73         struct lws *wsi2;
74 #endif
75         int ret, m, n;
76
77         //lwsl_err("%s: %p\n", __func__, wsi);
78
79         wsi->leave_pollout_active = 0;
80         wsi->handling_pollout = 1;
81         /*
82          * if another thread wants POLLOUT on us, from here on while
83          * handling_pollout is set, he will only set leave_pollout_active.
84          * If we are going to disable POLLOUT, we will check that first.
85          */
86
87         /*
88          * user callback is lowest priority to get these notifications
89          * actually, since other pending things cannot be disordered
90          */
91
92         /* Priority 1: pending truncated sends are incomplete ws fragments
93          *             If anything else sent first the protocol would be
94          *             corrupted.
95          */
96         if (wsi->trunc_len) {
97                 if (lws_issue_raw(wsi, wsi->trunc_alloc + wsi->trunc_offset,
98                                   wsi->trunc_len) < 0) {
99                         lwsl_info("%s signalling to close\n", __func__);
100                         goto bail_die;
101                 }
102                 /* leave POLLOUT active either way */
103                 goto bail_ok;
104         } else
105                 if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
106                         wsi->socket_is_permanently_unusable = 1;
107                         goto bail_die; /* retry closing now */
108                 }
109
110         if (wsi->mode == LWSCM_WSCL_ISSUE_HTTP_BODY)
111                 goto user_service;
112
113
114 #ifdef LWS_USE_HTTP2
115         /* Priority 2: protocol packets
116          */
117         if (wsi->pps) {
118                 lwsl_info("servicing pps %d\n", wsi->pps);
119                 switch (wsi->pps) {
120                 case LWS_PPS_HTTP2_MY_SETTINGS:
121                 case LWS_PPS_HTTP2_ACK_SETTINGS:
122                         lws_http2_do_pps_send(lws_get_context(wsi), wsi);
123                         break;
124                 default:
125                         break;
126                 }
127                 wsi->pps = LWS_PPS_NONE;
128                 lws_rx_flow_control(wsi, 1);
129
130                 goto bail_ok; /* leave POLLOUT active */
131         }
132 #endif
133
134 #ifdef LWS_WITH_CGI
135         if (wsi->cgi) {
136                 /* also one shot */
137                 if (pollfd)
138                         if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
139                                 lwsl_info("failed at set pollfd\n");
140                                 return 1;
141                         }
142                 goto user_service_go_again;
143         }
144 #endif
145
146         /* Priority 3: pending control packets (pong or close)
147          */
148         if ((wsi->state == LWSS_ESTABLISHED &&
149              wsi->u.ws.ping_pending_flag) ||
150             (wsi->state == LWSS_RETURNED_CLOSE_ALREADY &&
151              wsi->u.ws.payload_is_close)) {
152
153                 if (wsi->u.ws.payload_is_close)
154                         write_type = LWS_WRITE_CLOSE;
155
156                 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
157                               wsi->u.ws.ping_payload_len, write_type);
158                 if (n < 0)
159                         goto bail_die;
160
161                 /* well he is sent, mark him done */
162                 wsi->u.ws.ping_pending_flag = 0;
163                 if (wsi->u.ws.payload_is_close)
164                         /* oh... a close frame was it... then we are done */
165                         goto bail_die;
166
167                 /* otherwise for PING, leave POLLOUT active either way */
168                 goto bail_ok;
169         }
170
171         if (wsi->state == LWSS_ESTABLISHED &&
172             !wsi->socket_is_permanently_unusable &&
173             wsi->u.ws.send_check_ping) {
174
175                 lwsl_info("issuing ping on wsi %p\n", wsi);
176                 wsi->u.ws.send_check_ping = 0;
177                 n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
178                               0, LWS_WRITE_PING);
179                 if (n < 0)
180                         goto bail_die;
181
182                 /*
183                  * we apparently were able to send the PING in a reasonable time
184                  * now reset the clock on our peer to be able to send the
185                  * PONG in a reasonable time.
186                  */
187
188                 lws_set_timeout(wsi, PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG,
189                                 wsi->context->timeout_secs);
190
191                 goto bail_ok;
192         }
193
194         /* Priority 4: if we are closing, not allowed to send more data frags
195          *             which means user callback or tx ext flush banned now
196          */
197         if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
198                 goto user_service;
199
200         /* Priority 5: Tx path extension with more to send
201          *
202          *             These are handled as new fragments each time around
203          *             So while we must block new writeable callback to enforce
204          *             payload ordering, but since they are always complete
205          *             fragments control packets can interleave OK.
206          */
207         if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
208                 lwsl_ext("SERVICING TX EXT DRAINING\n");
209                 if (lws_write(wsi, NULL, 0, LWS_WRITE_CONTINUATION) < 0)
210                         goto bail_die;
211                 /* leave POLLOUT active */
212                 goto bail_ok;
213         }
214
215         /* Priority 6: user can get the callback
216          */
217         m = lws_ext_cb_active(wsi, LWS_EXT_CB_IS_WRITEABLE, NULL, 0);
218         if (m)
219                 goto bail_die;
220 #ifndef LWS_NO_EXTENSIONS
221         if (!wsi->extension_data_pending)
222                 goto user_service;
223 #endif
224         /*
225          * check in on the active extensions, see if they
226          * had pending stuff to spill... they need to get the
227          * first look-in otherwise sequence will be disordered
228          *
229          * NULL, zero-length eff_buf means just spill pending
230          */
231
232         ret = 1;
233         if (wsi->mode == LWSCM_RAW || wsi->mode == LWSCM_RAW_FILEDESC)
234                 ret = 0;
235         while (ret == 1) {
236
237                 /* default to nobody has more to spill */
238
239                 ret = 0;
240                 eff_buf.token = NULL;
241                 eff_buf.token_len = 0;
242
243                 /* give every extension a chance to spill */
244
245                 m = lws_ext_cb_active(wsi,
246                                         LWS_EXT_CB_PACKET_TX_PRESEND,
247                                                &eff_buf, 0);
248                 if (m < 0) {
249                         lwsl_err("ext reports fatal error\n");
250                         goto bail_die;
251                 }
252                 if (m)
253                         /*
254                          * at least one extension told us he has more
255                          * to spill, so we will go around again after
256                          */
257                         ret = 1;
258
259                 /* assuming they gave us something to send, send it */
260
261                 if (eff_buf.token_len) {
262                         n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
263                                           eff_buf.token_len);
264                         if (n < 0) {
265                                 lwsl_info("closing from POLLOUT spill\n");
266                                 goto bail_die;
267                         }
268                         /*
269                          * Keep amount spilled small to minimize chance of this
270                          */
271                         if (n != eff_buf.token_len) {
272                                 lwsl_err("Unable to spill ext %d vs %d\n",
273                                                           eff_buf.token_len, n);
274                                 goto bail_die;
275                         }
276                 } else
277                         continue;
278
279                 /* no extension has more to spill */
280
281                 if (!ret)
282                         continue;
283
284                 /*
285                  * There's more to spill from an extension, but we just sent
286                  * something... did that leave the pipe choked?
287                  */
288
289                 if (!lws_send_pipe_choked(wsi))
290                         /* no we could add more */
291                         continue;
292
293                 lwsl_info("choked in POLLOUT service\n");
294
295                 /*
296                  * Yes, he's choked.  Leave the POLLOUT masked on so we will
297                  * come back here when he is unchoked.  Don't call the user
298                  * callback to enforce ordering of spilling, he'll get called
299                  * when we come back here and there's nothing more to spill.
300                  */
301
302                 goto bail_ok;
303         }
304 #ifndef LWS_NO_EXTENSIONS
305         wsi->extension_data_pending = 0;
306 #endif
307 user_service:
308         /* one shot */
309
310         if (pollfd) {
311                 int eff = wsi->leave_pollout_active;
312
313                 if (!eff)
314                         if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
315                                 lwsl_info("failed at set pollfd\n");
316                                 goto bail_die;
317                         }
318
319                 wsi->handling_pollout = 0;
320
321                 /* cannot get leave_pollout_active set after the above */
322
323                 if (!eff && wsi->leave_pollout_active)
324                         /* got set inbetween sampling eff and clearing
325                          * handling_pollout, force POLLOUT on */
326                         lws_calllback_as_writeable(wsi);
327
328                 wsi->leave_pollout_active = 0;
329         }
330
331         if (wsi->mode != LWSCM_WSCL_ISSUE_HTTP_BODY &&
332             !wsi->hdr_parsing_completed)
333                 goto bail_ok;
334
335
336 #ifdef LWS_WITH_CGI
337 user_service_go_again:
338 #endif
339
340 #ifdef LWS_USE_HTTP2
341         /*
342          * we are the 'network wsi' for potentially many muxed child wsi with
343          * no network connection of their own, who have to use us for all their
344          * network actions.  So we use a round-robin scheme to share out the
345          * POLLOUT notifications to our children.
346          *
347          * But because any child could exhaust the socket's ability to take
348          * writes, we can only let one child get notified each time.
349          *
350          * In addition children may be closed / deleted / added between POLLOUT
351          * notifications, so we can't hold pointers
352          */
353
354         if (wsi->mode != LWSCM_HTTP2_SERVING) {
355                 lwsl_info("%s: non http2\n", __func__);
356                 goto notify;
357         }
358
359         wsi->u.http2.requested_POLLOUT = 0;
360         if (!wsi->u.http2.initialized) {
361                 lwsl_info("pollout on uninitialized http2 conn\n");
362                 goto bail_ok;
363         }
364
365         lwsl_info("%s: doing children\n", __func__);
366
367         wsi2 = wsi;
368         do {
369                 wsi2 = wsi2->u.http2.next_child_wsi;
370                 lwsl_info("%s: child %p\n", __func__, wsi2);
371                 if (!wsi2)
372                         continue;
373                 if (!wsi2->u.http2.requested_POLLOUT)
374                         continue;
375                 wsi2->u.http2.requested_POLLOUT = 0;
376                 if (lws_calllback_as_writeable(wsi2)) {
377                         lwsl_debug("Closing POLLOUT child\n");
378                         lws_close_free_wsi(wsi2, LWS_CLOSE_STATUS_NOSTATUS);
379                 }
380                 wsi2 = wsi;
381         } while (wsi2 != NULL && !lws_send_pipe_choked(wsi));
382
383         lwsl_info("%s: completed\n", __func__);
384
385         goto bail_ok;
386 notify:
387 #endif
388         wsi->handling_pollout = 0;
389         wsi->leave_pollout_active = 0;
390
391         return lws_calllback_as_writeable(wsi);
392
393         /*
394          * since these don't disable the POLLOUT, they are always doing the
395          * right thing for leave_pollout_active whether it was set or not.
396          */
397
398 bail_ok:
399         wsi->handling_pollout = 0;
400         wsi->leave_pollout_active = 0;
401
402         return 0;
403
404 bail_die:
405         wsi->handling_pollout = 0;
406         wsi->leave_pollout_active = 0;
407
408         return -1;
409 }
410
411 int
412 lws_service_timeout_check(struct lws *wsi, unsigned int sec)
413 {
414 //#if LWS_POSIX
415         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
416         int n = 0;
417 //#endif
418
419         (void)n;
420
421         /*
422          * if extensions want in on it (eg, we are a mux parent)
423          * give them a chance to service child timeouts
424          */
425         if (lws_ext_cb_active(wsi, LWS_EXT_CB_1HZ, NULL, sec) < 0)
426                 return 0;
427
428         if (!wsi->pending_timeout)
429                 return 0;
430
431         /*
432          * if we went beyond the allowed time, kill the
433          * connection
434          */
435         if ((time_t)sec > wsi->pending_timeout_limit) {
436 //#if LWS_POSIX
437                 if (wsi->desc.sockfd != LWS_SOCK_INVALID && wsi->position_in_fds_table >= 0)
438                         n = pt->fds[wsi->position_in_fds_table].events;
439
440                 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_TIMEOUTS, 1);
441
442                 /* no need to log normal idle keepalive timeout */
443                 if (wsi->pending_timeout != PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE)
444                         lwsl_notice("wsi %p: TIMEDOUT WAITING on %d (did hdr %d, ah %p, wl %d, pfd events %d) %llu vs %llu\n",
445                             (void *)wsi, wsi->pending_timeout,
446                             wsi->hdr_parsing_completed, wsi->u.hdr.ah,
447                             pt->ah_wait_list_length, n, (unsigned long long)sec, (unsigned long long)wsi->pending_timeout_limit);
448 //#endif
449                 /*
450                  * Since he failed a timeout, he already had a chance to do
451                  * something and was unable to... that includes situations like
452                  * half closed connections.  So process this "failed timeout"
453                  * close as a violent death and don't try to do protocol
454                  * cleanup like flush partials.
455                  */
456                 wsi->socket_is_permanently_unusable = 1;
457                 if (wsi->mode == LWSCM_WSCL_WAITING_SSL)
458                         wsi->vhost->protocols[0].callback(wsi,
459                                 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
460                                 wsi->user_space, (void *)"Timed out waiting SSL", 21);
461
462                 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
463
464                 return 1;
465         }
466
467         return 0;
468 }
469
470 int lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len)
471 {
472         /* his RX is flowcontrolled, don't send remaining now */
473         if (wsi->rxflow_buffer) {
474                 /* rxflow while we were spilling prev rxflow */
475                 lwsl_info("stalling in existing rxflow buf\n");
476                 return 1;
477         }
478
479         /* a new rxflow, buffer it and warn caller */
480         lwsl_info("new rxflow input buffer len %d\n", len - n);
481         wsi->rxflow_buffer = lws_malloc(len - n);
482         if (!wsi->rxflow_buffer)
483                 return -1;
484         wsi->rxflow_len = len - n;
485         wsi->rxflow_pos = 0;
486         memcpy(wsi->rxflow_buffer, buf + n, len - n);
487
488         return 0;
489 }
490
491 /* this is used by the platform service code to stop us waiting for network
492  * activity in poll() when we have something that already needs service
493  */
494
495 LWS_VISIBLE LWS_EXTERN int
496 lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi)
497 {
498         struct lws_context_per_thread *pt = &context->pt[tsi];
499         int n;
500
501         /* Figure out if we really want to wait in poll()
502          * We only need to wait if really nothing already to do and we have
503          * to wait for something from network
504          */
505
506         /* 1) if we know we are draining rx ext, do not wait in poll */
507         if (pt->rx_draining_ext_list)
508                 return 0;
509
510 #ifdef LWS_OPENSSL_SUPPORT
511         /* 2) if we know we have non-network pending data, do not wait in poll */
512         if (lws_ssl_anybody_has_buffered_read_tsi(context, tsi)) {
513                 lwsl_info("ssl buffered read\n");
514                 return 0;
515         }
516 #endif
517
518         /* 3) if any ah has pending rx, do not wait in poll */
519         for (n = 0; n < context->max_http_header_pool; n++)
520                 if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen) {
521                         /* any ah with pending rx must be attached to someone */
522                         if (!pt->ah_pool[n].wsi) {
523                                 lwsl_err("%s: assert: no wsi attached to ah\n", __func__);
524                                 assert(0);
525                         }
526                         return 0;
527                 }
528
529         return timeout_ms;
530 }
531
532 /*
533  * guys that need POLLIN service again without waiting for network action
534  * can force POLLIN here if not flowcontrolled, so they will get service.
535  *
536  * Return nonzero if anybody got their POLLIN faked
537  */
538 int
539 lws_service_flag_pending(struct lws_context *context, int tsi)
540 {
541         struct lws_context_per_thread *pt = &context->pt[tsi];
542 #ifdef LWS_OPENSSL_SUPPORT
543         struct lws *wsi_next;
544 #endif
545         struct lws *wsi;
546         int forced = 0;
547         int n;
548
549         /* POLLIN faking */
550
551         /*
552          * 1) For all guys with already-available ext data to drain, if they are
553          * not flowcontrolled, fake their POLLIN status
554          */
555         wsi = pt->rx_draining_ext_list;
556         while (wsi) {
557                 pt->fds[wsi->position_in_fds_table].revents |=
558                         pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
559                 if (pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN) {
560                         forced = 1;
561                         break;
562                 }
563                 wsi = wsi->u.ws.rx_draining_ext_list;
564         }
565
566 #ifdef LWS_OPENSSL_SUPPORT
567         /*
568          * 2) For all guys with buffered SSL read data already saved up, if they
569          * are not flowcontrolled, fake their POLLIN status so they'll get
570          * service to use up the buffered incoming data, even though their
571          * network socket may have nothing
572          */
573         wsi = pt->pending_read_list;
574         while (wsi) {
575                 wsi_next = wsi->pending_read_list_next;
576                 pt->fds[wsi->position_in_fds_table].revents |=
577                         pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
578                 if (pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN) {
579                         forced = 1;
580                         /*
581                          * he's going to get serviced now, take him off the
582                          * list of guys with buffered SSL.  If he still has some
583                          * at the end of the service, he'll get put back on the
584                          * list then.
585                          */
586                         lws_ssl_remove_wsi_from_buffered_list(wsi);
587                 }
588
589                 wsi = wsi_next;
590         }
591 #endif
592         /*
593          * 3) For any wsi who have an ah with pending RX who did not
594          * complete their current headers, and are not flowcontrolled,
595          * fake their POLLIN status so they will be able to drain the
596          * rx buffered in the ah
597          */
598         for (n = 0; n < context->max_http_header_pool; n++)
599                 if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen &&
600                     !pt->ah_pool[n].wsi->hdr_parsing_completed) {
601                         pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents |=
602                                 pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].events &
603                                         LWS_POLLIN;
604                         if (pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents &
605                             LWS_POLLIN)
606                                 forced = 1;
607                 }
608
609         return forced;
610 }
611
612 #ifndef LWS_NO_CLIENT
613
614 LWS_VISIBLE int
615 lws_http_client_read(struct lws *wsi, char **buf, int *len)
616 {
617         int rlen, n;
618
619         rlen = lws_ssl_capable_read(wsi, (unsigned char *)*buf, *len);
620         *len = 0;
621
622         /* allow the source to signal he has data again next time */
623         lws_change_pollfd(wsi, 0, LWS_POLLIN);
624
625         if (rlen == LWS_SSL_CAPABLE_ERROR) {
626                 lwsl_notice("%s: SSL capable error\n", __func__);
627                 return -1;
628         }
629
630         if (rlen == 0)
631                 return -1;
632
633         if (rlen < 0)
634                 return 0;
635
636         *len = rlen;
637         wsi->client_rx_avail = 0;
638
639         /*
640          * server may insist on transfer-encoding: chunked,
641          * so http client must deal with it
642          */
643 spin_chunks:
644         while (wsi->chunked && (wsi->chunk_parser != ELCP_CONTENT) && *len) {
645                 switch (wsi->chunk_parser) {
646                 case ELCP_HEX:
647                         if ((*buf)[0] == '\x0d') {
648                                 wsi->chunk_parser = ELCP_CR;
649                                 break;
650                         }
651                         n = char_to_hex((*buf)[0]);
652                         if (n < 0) {
653                                 lwsl_debug("chunking failure\n");
654                                 return -1;
655                         }
656                         wsi->chunk_remaining <<= 4;
657                         wsi->chunk_remaining |= n;
658                         break;
659                 case ELCP_CR:
660                         if ((*buf)[0] != '\x0a') {
661                                 lwsl_debug("chunking failure\n");
662                                 return -1;
663                         }
664                         wsi->chunk_parser = ELCP_CONTENT;
665                         lwsl_info("chunk %d\n", wsi->chunk_remaining);
666                         if (wsi->chunk_remaining)
667                                 break;
668                         lwsl_info("final chunk\n");
669                         goto completed;
670
671                 case ELCP_CONTENT:
672                         break;
673
674                 case ELCP_POST_CR:
675                         if ((*buf)[0] != '\x0d') {
676                                 lwsl_debug("chunking failure\n");
677
678                                 return -1;
679                         }
680
681                         wsi->chunk_parser = ELCP_POST_LF;
682                         break;
683
684                 case ELCP_POST_LF:
685                         if ((*buf)[0] != '\x0a')
686                                 return -1;
687
688                         wsi->chunk_parser = ELCP_HEX;
689                         wsi->chunk_remaining = 0;
690                         break;
691                 }
692                 (*buf)++;
693                 (*len)--;
694         }
695
696         if (wsi->chunked && !wsi->chunk_remaining)
697                 return 0;
698
699         if (wsi->u.http.content_remain &&
700             wsi->u.http.content_remain < *len)
701                 n = (int)wsi->u.http.content_remain;
702         else
703                 n = *len;
704
705         if (wsi->chunked && wsi->chunk_remaining &&
706             wsi->chunk_remaining < n)
707                 n = wsi->chunk_remaining;
708
709 #ifdef LWS_WITH_HTTP_PROXY
710         /* hubbub */
711         if (wsi->perform_rewrite)
712                 lws_rewrite_parse(wsi->rw, (unsigned char *)*buf, n);
713         else
714 #endif
715                 if (user_callback_handle_rxflow(wsi->protocol->callback,
716                                 wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
717                                 wsi->user_space, *buf, n)) {
718                         lwsl_debug("%s: LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ returned -1\n", __func__);
719
720                         return -1;
721                 }
722
723         if (wsi->chunked && wsi->chunk_remaining) {
724                 (*buf) += n;
725                 wsi->chunk_remaining -= n;
726                 *len -= n;
727         }
728
729         if (wsi->chunked && !wsi->chunk_remaining)
730                 wsi->chunk_parser = ELCP_POST_CR;
731
732         if (wsi->chunked && *len)
733                 goto spin_chunks;
734
735         if (wsi->chunked)
736                 return 0;
737
738         /* if we know the content length, decrement the content remaining */
739         if (wsi->u.http.content_length > 0)
740                 wsi->u.http.content_remain -= n;
741
742         if (wsi->u.http.content_remain || !wsi->u.http.content_length)
743                 return 0;
744
745 completed:
746         if (user_callback_handle_rxflow(wsi->protocol->callback,
747                         wsi, LWS_CALLBACK_COMPLETED_CLIENT_HTTP,
748                         wsi->user_space, NULL, 0)) {
749                 lwsl_debug("Completed call returned -1\n");
750                 return -1;
751         }
752
753         if (lws_http_transaction_completed_client(wsi)) {
754                 lwsl_notice("%s: transaction completed says -1\n", __func__);
755                 return -1;
756         }
757
758         return 0;
759 }
760 #endif
761
762 static int
763 lws_is_ws_with_ext(struct lws *wsi)
764 {
765 #if defined(LWS_NO_EXTENSIONS)
766         return 0;
767 #else
768         return wsi->state == LWSS_ESTABLISHED &&
769                !!wsi->count_act_ext;
770 #endif
771 }
772
773 LWS_VISIBLE int
774 lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd, int tsi)
775 {
776         struct lws_context_per_thread *pt = &context->pt[tsi];
777         lws_sockfd_type our_fd = 0, tmp_fd;
778         struct lws_tokens eff_buf;
779         unsigned int pending = 0;
780         struct lws *wsi, *wsi1;
781         char draining_flow = 0;
782         int timed_out = 0;
783         time_t now;
784         int n = 0, m;
785         int more;
786
787         if (!context->protocol_init_done)
788                 lws_protocol_init(context);
789
790         time(&now);
791
792         /*
793          * handle case that system time was uninitialized when lws started
794          * at boot, and got initialized a little later
795          */
796         if (context->time_up < 1464083026 && now > 1464083026)
797                 context->time_up = now;
798
799         /* TODO: if using libev, we should probably use timeout watchers... */
800         if (context->last_timeout_check_s != now) {
801                 context->last_timeout_check_s = now;
802
803 #if defined(LWS_WITH_STATS)
804                 if (!tsi && now - context->last_dump > 10) {
805                         lws_stats_log_dump(context);
806                         context->last_dump = now;
807                 }
808 #endif
809
810                 lws_plat_service_periodic(context);
811
812                 lws_check_deferred_free(context, 0);
813
814                 /* retire unused deprecated context */
815 #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_WITH_ESP32)
816 #if LWS_POSIX && !defined(_WIN32)
817                 if (context->deprecated && !context->count_wsi_allocated) {
818                         lwsl_notice("%s: ending deprecated context\n", __func__);
819                         kill(getpid(), SIGINT);
820                         return 0;
821                 }
822 #endif
823 #endif
824                 /* global timeout check once per second */
825
826                 if (pollfd)
827                         our_fd = pollfd->fd;
828
829                 wsi = context->pt[tsi].timeout_list;
830                 while (wsi) {
831                         /* we have to take copies, because he may be deleted */
832                         wsi1 = wsi->timeout_list;
833                         tmp_fd = wsi->desc.sockfd;
834                         if (lws_service_timeout_check(wsi, (unsigned int)now)) {
835                                 /* he did time out... */
836                                 if (tmp_fd == our_fd)
837                                         /* it was the guy we came to service! */
838                                         timed_out = 1;
839                                         /* he's gone, no need to mark as handled */
840                         }
841                         wsi = wsi1;
842                 }
843 #ifdef LWS_WITH_CGI
844                 lws_cgi_kill_terminated(pt);
845 #endif
846 #if 0
847                 {
848                         char s[300], *p = s;
849
850                         for (n = 0; n < context->count_threads; n++)
851                                 p += sprintf(p, " %7lu (%5d), ",
852                                              context->pt[n].count_conns,
853                                              context->pt[n].fds_count);
854
855                         lwsl_notice("load: %s\n", s);
856                 }
857 #endif
858         }
859
860         /*
861          * at intervals, check for ws connections needing ping-pong checks
862          */
863
864         if (context->ws_ping_pong_interval &&
865             context->last_ws_ping_pong_check_s < now + 10) {
866                 struct lws_vhost *vh = context->vhost_list;
867                 context->last_ws_ping_pong_check_s = now;
868
869                 while (vh) {
870                         for (n = 0; n < vh->count_protocols; n++) {
871                                 wsi = vh->same_vh_protocol_list[n];
872
873                                 while (wsi) {
874                                         if (wsi->state == LWSS_ESTABLISHED &&
875                                             !wsi->socket_is_permanently_unusable &&
876                                             !wsi->u.ws.send_check_ping &&
877                                             wsi->u.ws.time_next_ping_check &&
878                                             wsi->u.ws.time_next_ping_check < now) {
879
880                                                 lwsl_info("requesting ping-pong on wsi %p\n", wsi);
881                                                 wsi->u.ws.send_check_ping = 1;
882                                                 lws_set_timeout(wsi, PENDING_TIMEOUT_WS_PONG_CHECK_SEND_PING,
883                                                                 context->timeout_secs);
884                                                 lws_callback_on_writable(wsi);
885                                                 wsi->u.ws.time_next_ping_check = now +
886                                                                 wsi->context->ws_ping_pong_interval;
887                                         }
888                                         wsi = wsi->same_vh_protocol_next;
889                                 }
890                         }
891                         vh = vh->vhost_next;
892                 }
893         }
894
895         /* the socket we came to service timed out, nothing to do */
896         if (timed_out)
897                 return 0;
898
899         /* just here for timeout management? */
900         if (!pollfd)
901                 return 0;
902
903         /* no, here to service a socket descriptor */
904         wsi = wsi_from_fd(context, pollfd->fd);
905         if (!wsi)
906                 /* not lws connection ... leave revents alone and return */
907                 return 0;
908
909         /*
910          * so that caller can tell we handled, past here we need to
911          * zero down pollfd->revents after handling
912          */
913
914 #if LWS_POSIX
915         /* handle session socket closed */
916
917         if ((!(pollfd->revents & pollfd->events & LWS_POLLIN)) &&
918             (pollfd->revents & LWS_POLLHUP)) {
919                 wsi->socket_is_permanently_unusable = 1;
920                 lwsl_debug("Session Socket %p (fd=%d) dead\n",
921                                                        (void *)wsi, pollfd->fd);
922
923                 goto close_and_handled;
924         }
925
926 #ifdef _WIN32
927         if (pollfd->revents & LWS_POLLOUT)
928                 wsi->sock_send_blocking = FALSE;
929 #endif
930
931 #endif
932
933 //       lwsl_debug("fd=%d, revents=%d, mode=%d, state=%d\n", pollfd->fd, pollfd->revents, (int)wsi->mode, (int)wsi->state);
934         if (pollfd->revents & LWS_POLLHUP) {
935                 lwsl_debug("pollhup\n");
936                 wsi->socket_is_permanently_unusable = 1;
937                 goto close_and_handled;
938         }
939
940
941 #ifdef LWS_OPENSSL_SUPPORT
942         if ((wsi->state == LWSS_SHUTDOWN) && lws_is_ssl(wsi) && wsi->ssl)
943         {
944                 n = SSL_shutdown(wsi->ssl);
945                 lwsl_debug("SSL_shutdown=%d for fd %d\n", n, wsi->desc.sockfd);
946                 if (n == 1)
947                 {
948                         n = shutdown(wsi->desc.sockfd, SHUT_WR);
949                         goto close_and_handled;
950                 }
951                 else if (n == 0)
952                 {
953                         lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
954                         n = 0;
955                         goto handled;
956                 }
957                 else /* n < 0 */
958                 {
959                         int shutdown_error = SSL_get_error(wsi->ssl, n);
960                         lwsl_debug("SSL_shutdown returned %d, SSL_get_error: %d\n", n, shutdown_error);
961                         if (shutdown_error == SSL_ERROR_WANT_READ) {
962                                 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
963                                 n = 0;
964                                 goto handled;
965                         } else if (shutdown_error == SSL_ERROR_WANT_WRITE) {
966                                 lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLOUT);
967                                 n = 0;
968                                 goto handled;
969                         }
970
971                         // actual error occurred, just close the connection
972                         n = shutdown(wsi->desc.sockfd, SHUT_WR);
973                         goto close_and_handled;
974                 }
975         }
976 #endif
977
978         /* okay, what we came here to do... */
979
980         switch (wsi->mode) {
981         case LWSCM_HTTP_SERVING:
982         case LWSCM_HTTP_CLIENT:
983         case LWSCM_HTTP_SERVING_ACCEPTED:
984         case LWSCM_SERVER_LISTENER:
985         case LWSCM_SSL_ACK_PENDING:
986         case LWSCM_SSL_ACK_PENDING_RAW:
987                 if (wsi->state == LWSS_CLIENT_HTTP_ESTABLISHED)
988                         goto handled;
989
990 #ifdef LWS_WITH_CGI
991                 if (wsi->cgi && (pollfd->revents & LWS_POLLOUT)) {
992                         n = lws_handle_POLLOUT_event(wsi, pollfd);
993                         if (n)
994                                 goto close_and_handled;
995                         goto handled;
996                 }
997 #endif
998                 /* fallthru */
999         case LWSCM_RAW:
1000                 n = lws_server_socket_service(context, wsi, pollfd);
1001                 if (n) /* closed by above */
1002                         return 1;
1003                 goto handled;
1004
1005         case LWSCM_RAW_FILEDESC:
1006
1007                 if (pollfd->revents & LWS_POLLOUT) {
1008                         n = lws_calllback_as_writeable(wsi);
1009                         if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
1010                                 lwsl_info("failed at set pollfd\n");
1011                                 return 1;
1012                         }
1013                         if (n)
1014                                 goto close_and_handled;
1015                 }
1016                 n = LWS_CALLBACK_RAW_RX;
1017                 if (wsi->mode == LWSCM_RAW_FILEDESC)
1018                         n = LWS_CALLBACK_RAW_RX_FILE;
1019
1020                 if (pollfd->revents & LWS_POLLIN) {
1021                         if (user_callback_handle_rxflow(
1022                                         wsi->protocol->callback,
1023                                         wsi, n,
1024                                         wsi->user_space, NULL, 0)) {
1025                                 lwsl_debug("raw rx callback closed it\n");
1026                                 goto close_and_handled;
1027                         }
1028                 }
1029
1030                 if (pollfd->revents & LWS_POLLHUP)
1031                         goto close_and_handled;
1032                 n = 0;
1033                 goto handled;
1034
1035         case LWSCM_WS_SERVING:
1036         case LWSCM_WS_CLIENT:
1037         case LWSCM_HTTP2_SERVING:
1038         case LWSCM_HTTP_CLIENT_ACCEPTED:
1039
1040                 /* 1: something requested a callback when it was OK to write */
1041
1042                 if ((pollfd->revents & LWS_POLLOUT) &&
1043                     ((wsi->state == LWSS_ESTABLISHED ||
1044                      wsi->state == LWSS_HTTP2_ESTABLISHED ||
1045                      wsi->state == LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS ||
1046                      wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
1047                      wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE)) &&
1048                     lws_handle_POLLOUT_event(wsi, pollfd)) {
1049                         if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
1050                                 wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
1051                         lwsl_info("lws_service_fd: closing\n");
1052                         goto close_and_handled;
1053                 }
1054
1055                 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
1056                     wsi->state == LWSS_AWAITING_CLOSE_ACK) {
1057                         /*
1058                          * we stopped caring about anything except control
1059                          * packets.  Force flow control off, defeat tx
1060                          * draining.
1061                          */
1062                         lws_rx_flow_control(wsi, 1);
1063                         wsi->u.ws.tx_draining_ext = 0;
1064                 }
1065
1066                 if (wsi->u.ws.tx_draining_ext)
1067                         /* we cannot deal with new RX until the TX ext
1068                          * path has been drained.  It's because new
1069                          * rx will, eg, crap on the wsi rx buf that
1070                          * may be needed to retain state.
1071                          *
1072                          * TX ext drain path MUST go through event loop
1073                          * to avoid blocking.
1074                          */
1075                         break;
1076
1077                 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
1078                         /* We cannot deal with any kind of new RX
1079                          * because we are RX-flowcontrolled.
1080                          */
1081                         break;
1082
1083                 /* 2: RX Extension needs to be drained
1084                  */
1085
1086                 if (wsi->state == LWSS_ESTABLISHED &&
1087                     wsi->u.ws.rx_draining_ext) {
1088
1089                         lwsl_ext("%s: RX EXT DRAINING: Service\n", __func__);
1090 #ifndef LWS_NO_CLIENT
1091                         if (wsi->mode == LWSCM_WS_CLIENT) {
1092                                 n = lws_client_rx_sm(wsi, 0);
1093                                 if (n < 0)
1094                                         /* we closed wsi */
1095                                         n = 0;
1096                         } else
1097 #endif
1098                                 n = lws_rx_sm(wsi, 0);
1099
1100                         goto handled;
1101                 }
1102
1103                 if (wsi->u.ws.rx_draining_ext)
1104                         /*
1105                          * We have RX EXT content to drain, but can't do it
1106                          * right now.  That means we cannot do anything lower
1107                          * priority either.
1108                          */
1109                         break;
1110
1111                 /* 3: RX Flowcontrol buffer needs to be drained
1112                  */
1113
1114                 if (wsi->rxflow_buffer) {
1115                         lwsl_info("draining rxflow (len %d)\n",
1116                                 wsi->rxflow_len - wsi->rxflow_pos
1117                         );
1118                         /* well, drain it */
1119                         eff_buf.token = (char *)wsi->rxflow_buffer +
1120                                                 wsi->rxflow_pos;
1121                         eff_buf.token_len = wsi->rxflow_len - wsi->rxflow_pos;
1122                         draining_flow = 1;
1123                         goto drain;
1124                 }
1125
1126                 /* 4: any incoming (or ah-stashed incoming rx) data ready?
1127                  * notice if rx flow going off raced poll(), rx flow wins
1128                  */
1129
1130                 if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
1131                         break;
1132
1133 read:
1134                 /* all the union members start with hdr, so even in ws mode
1135                  * we can deal with the ah via u.hdr
1136                  */
1137                 if (wsi->u.hdr.ah) {
1138                         lwsl_info("%s: %p: inherited ah rx\n", __func__, wsi);
1139                         eff_buf.token_len = wsi->u.hdr.ah->rxlen -
1140                                             wsi->u.hdr.ah->rxpos;
1141                         eff_buf.token = (char *)wsi->u.hdr.ah->rx +
1142                                         wsi->u.hdr.ah->rxpos;
1143                 } else {
1144                         if (wsi->mode != LWSCM_HTTP_CLIENT_ACCEPTED) {
1145                                 /*
1146                                  * extension may not consume everything (eg, pmd may be constrained
1147                                  * as to what it can output...) has to go in per-wsi rx buf area.
1148                                  * Otherwise in large temp serv_buf area.
1149                                  */
1150                                 eff_buf.token = (char *)pt->serv_buf;
1151                                 if (lws_is_ws_with_ext(wsi)) {
1152                                         eff_buf.token_len = wsi->u.ws.rx_ubuf_alloc;
1153                                 } else {
1154                                         eff_buf.token_len = context->pt_serv_buf_size;
1155                                 }
1156
1157                                 if ((unsigned int)eff_buf.token_len > context->pt_serv_buf_size)
1158                                         eff_buf.token_len = context->pt_serv_buf_size;
1159
1160                                 eff_buf.token_len = lws_ssl_capable_read(wsi,
1161                                         (unsigned char *)eff_buf.token, pending ? pending :
1162                                         eff_buf.token_len);
1163                                 switch (eff_buf.token_len) {
1164                                 case 0:
1165                                         lwsl_info("%s: zero length read\n", __func__);
1166                                         goto close_and_handled;
1167                                 case LWS_SSL_CAPABLE_MORE_SERVICE:
1168                                         lwsl_info("SSL Capable more service\n");
1169                                         n = 0;
1170                                         goto handled;
1171                                 case LWS_SSL_CAPABLE_ERROR:
1172                                         lwsl_info("Closing when error\n");
1173                                         goto close_and_handled;
1174                                 }
1175                                 // lwsl_notice("Actual RX %d\n", eff_buf.token_len);
1176                         }
1177                 }
1178
1179 drain:
1180 #ifndef LWS_NO_CLIENT
1181                 if (wsi->mode == LWSCM_HTTP_CLIENT_ACCEPTED &&
1182                     !wsi->told_user_closed) {
1183
1184                         /*
1185                          * In SSL mode we get POLLIN notification about
1186                          * encrypted data in.
1187                          *
1188                          * But that is not necessarily related to decrypted
1189                          * data out becoming available; in may need to perform
1190                          * other in or out before that happens.
1191                          *
1192                          * simply mark ourselves as having readable data
1193                          * and turn off our POLLIN
1194                          */
1195                         wsi->client_rx_avail = 1;
1196                         lws_change_pollfd(wsi, LWS_POLLIN, 0);
1197
1198                         /* let user code know, he'll usually ask for writeable
1199                          * callback and drain / re-enable it there
1200                          */
1201                         if (user_callback_handle_rxflow(
1202                                         wsi->protocol->callback,
1203                                         wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP,
1204                                         wsi->user_space, NULL, 0)) {
1205                                 lwsl_notice("LWS_CALLBACK_RECEIVE_CLIENT_HTTP closed it\n");
1206                                 goto close_and_handled;
1207                         }
1208
1209                         n = 0;
1210                         goto handled;
1211                 }
1212 #endif
1213                 /*
1214                  * give any active extensions a chance to munge the buffer
1215                  * before parse.  We pass in a pointer to an lws_tokens struct
1216                  * prepared with the default buffer and content length that's in
1217                  * there.  Rather than rewrite the default buffer, extensions
1218                  * that expect to grow the buffer can adapt .token to
1219                  * point to their own per-connection buffer in the extension
1220                  * user allocation.  By default with no extensions or no
1221                  * extension callback handling, just the normal input buffer is
1222                  * used then so it is efficient.
1223                  */
1224                 do {
1225                         more = 0;
1226
1227                         m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_RX_PREPARSE,
1228                                               &eff_buf, 0);
1229                         if (m < 0)
1230                                 goto close_and_handled;
1231                         if (m)
1232                                 more = 1;
1233
1234                         /* service incoming data */
1235
1236                         if (eff_buf.token_len) {
1237                                 /*
1238                                  * if draining from rxflow buffer, not
1239                                  * critical to track what was used since at the
1240                                  * use it bumps wsi->rxflow_pos.  If we come
1241                                  * around again it will pick up from where it
1242                                  * left off.
1243                                  */
1244                                 // lwsl_notice("doing lws_read from pt->serv_buf %p %p for len %d\n", pt->serv_buf, eff_buf.token, (int)eff_buf.token_len);
1245
1246                                 n = lws_read(wsi, (unsigned char *)eff_buf.token,
1247                                              eff_buf.token_len);
1248                                 if (n < 0) {
1249                                         /* we closed wsi */
1250                                         n = 0;
1251                                         goto handled;
1252                                 }
1253                         }
1254
1255                         eff_buf.token = NULL;
1256                         eff_buf.token_len = 0;
1257                 } while (more);
1258
1259                 if (wsi->u.hdr.ah) {
1260                         lwsl_notice("%s: %p: detaching\n",
1261                                  __func__, wsi);
1262                         lws_header_table_force_to_detachable_state(wsi);
1263                         /* we can run the normal ah detach flow despite
1264                          * being in ws union mode, since all union members
1265                          * start with hdr */
1266                         lws_header_table_detach(wsi, 0);
1267                 }
1268
1269                 pending = lws_ssl_pending(wsi);
1270                 if (pending) {
1271                         if (lws_is_ws_with_ext(wsi))
1272                                 pending = pending > wsi->u.ws.rx_ubuf_alloc ?
1273                                         wsi->u.ws.rx_ubuf_alloc : pending;
1274                         else
1275                                 pending = pending > context->pt_serv_buf_size ?
1276                                         context->pt_serv_buf_size : pending;
1277                         goto read;
1278                 }
1279
1280                 if (draining_flow && wsi->rxflow_buffer &&
1281                     wsi->rxflow_pos == wsi->rxflow_len) {
1282                         lwsl_info("flow buffer: drained\n");
1283                         lws_free_set_NULL(wsi->rxflow_buffer);
1284                         /* having drained the rxflow buffer, can rearm POLLIN */
1285 #ifdef LWS_NO_SERVER
1286                         n =
1287 #endif
1288                         _lws_rx_flow_control(wsi);
1289                         /* n ignored, needed for NO_SERVER case */
1290                 }
1291
1292                 break;
1293 #ifdef LWS_WITH_CGI
1294         case LWSCM_CGI: /* we exist to handle a cgi's stdin/out/err data...
1295                          * do the callback on our master wsi
1296                          */
1297                 {
1298                         struct lws_cgi_args args;
1299
1300                         if (wsi->cgi_channel >= LWS_STDOUT &&
1301                             !(pollfd->revents & pollfd->events & LWS_POLLIN))
1302                                 break;
1303                         if (wsi->cgi_channel == LWS_STDIN &&
1304                             !(pollfd->revents & pollfd->events & LWS_POLLOUT))
1305                                 break;
1306
1307                         if (wsi->cgi_channel == LWS_STDIN)
1308                                 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
1309                                         lwsl_info("failed at set pollfd\n");
1310                                         return 1;
1311                                 }
1312
1313                         args.ch = wsi->cgi_channel;
1314                         args.stdwsi = &wsi->parent->cgi->stdwsi[0];
1315                         args.hdr_state = wsi->hdr_state;
1316
1317                         //lwsl_err("CGI LWS_STDOUT waiting wsi %p mode %d state %d\n",
1318                         //       wsi->parent, wsi->parent->mode, wsi->parent->state);
1319
1320                         if (user_callback_handle_rxflow(
1321                                         wsi->parent->protocol->callback,
1322                                         wsi->parent, LWS_CALLBACK_CGI,
1323                                         wsi->parent->user_space,
1324                                         (void *)&args, 0))
1325                                 return 1;
1326
1327                         break;
1328                 }
1329 #endif
1330         default:
1331 #ifdef LWS_NO_CLIENT
1332                 break;
1333 #else
1334                 if ((pollfd->revents & LWS_POLLOUT) &&
1335                     lws_handle_POLLOUT_event(wsi, pollfd)) {
1336                         lwsl_debug("POLLOUT event closed it\n");
1337                         goto close_and_handled;
1338                 }
1339
1340                 n = lws_client_socket_service(context, wsi, pollfd);
1341                 if (n)
1342                         return 1;
1343                 goto handled;
1344 #endif
1345         }
1346
1347         n = 0;
1348         goto handled;
1349
1350 close_and_handled:
1351         lwsl_debug("%p: Close and handled\n", wsi);
1352         lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
1353         /*
1354          * pollfd may point to something else after the close
1355          * due to pollfd swapping scheme on delete on some platforms
1356          * we can't clear revents now because it'd be the wrong guy's revents
1357          */
1358         return 1;
1359
1360 handled:
1361         pollfd->revents = 0;
1362         return n;
1363 }
1364
1365 LWS_VISIBLE int
1366 lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd)
1367 {
1368         return lws_service_fd_tsi(context, pollfd, 0);
1369 }
1370
1371 LWS_VISIBLE int
1372 lws_service(struct lws_context *context, int timeout_ms)
1373 {
1374         return lws_plat_service(context, timeout_ms);
1375 }
1376
1377 LWS_VISIBLE int
1378 lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
1379 {
1380         return _lws_plat_service_tsi(context, timeout_ms, tsi);
1381 }
1382