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