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