1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342 |
- #include "private-libwebsockets.h"
- static int
- lws_calllback_as_writeable(struct lws *wsi)
- {
- int n;
- switch (wsi->mode) {
- case LWSCM_RAW:
- n = LWS_CALLBACK_RAW_WRITEABLE;
- break;
- case LWSCM_RAW_FILEDESC:
- n = LWS_CALLBACK_RAW_WRITEABLE_FILE;
- break;
- case LWSCM_WS_CLIENT:
- n = LWS_CALLBACK_CLIENT_WRITEABLE;
- break;
- case LWSCM_WSCL_ISSUE_HTTP_BODY:
- n = LWS_CALLBACK_CLIENT_HTTP_WRITEABLE;
- break;
- case LWSCM_WS_SERVING:
- n = LWS_CALLBACK_SERVER_WRITEABLE;
- break;
- default:
- n = LWS_CALLBACK_HTTP_WRITEABLE;
- break;
- }
- lwsl_debug("%s: %p (user=%p)\n", __func__, wsi, wsi->user_space);
- return user_callback_handle_rxflow(wsi->protocol->callback,
- wsi, (enum lws_callback_reasons) n,
- wsi->user_space, NULL, 0);
- }
- int
- lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd)
- {
- int write_type = LWS_WRITE_PONG;
- struct lws_tokens eff_buf;
- #ifdef LWS_USE_HTTP2
- struct lws *wsi2;
- #endif
- int ret, m, n;
-
- wsi->leave_pollout_active = 0;
- wsi->handling_pollout = 1;
-
-
-
- if (wsi->trunc_len) {
- if (lws_issue_raw(wsi, wsi->trunc_alloc + wsi->trunc_offset,
- wsi->trunc_len) < 0) {
- lwsl_info("%s signalling to close\n", __func__);
- goto bail_die;
- }
-
- goto bail_ok;
- } else
- if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE)
- goto bail_die;
- if (wsi->mode == LWSCM_WSCL_ISSUE_HTTP_BODY)
- goto user_service;
- #ifdef LWS_USE_HTTP2
-
- if (wsi->pps) {
- lwsl_info("servicing pps %d\n", wsi->pps);
- switch (wsi->pps) {
- case LWS_PPS_HTTP2_MY_SETTINGS:
- case LWS_PPS_HTTP2_ACK_SETTINGS:
- lws_http2_do_pps_send(lws_get_context(wsi), wsi);
- break;
- default:
- break;
- }
- wsi->pps = LWS_PPS_NONE;
- lws_rx_flow_control(wsi, 1);
- goto bail_ok;
- }
- #endif
- #ifdef LWS_WITH_CGI
- if (wsi->cgi)
- goto user_service_go_again;
- #endif
-
- if ((wsi->state == LWSS_ESTABLISHED &&
- wsi->u.ws.ping_pending_flag) ||
- (wsi->state == LWSS_RETURNED_CLOSE_ALREADY &&
- wsi->u.ws.payload_is_close)) {
- if (wsi->u.ws.payload_is_close)
- write_type = LWS_WRITE_CLOSE;
- n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
- wsi->u.ws.ping_payload_len, write_type);
- if (n < 0)
- goto bail_die;
-
- wsi->u.ws.ping_pending_flag = 0;
- if (wsi->u.ws.payload_is_close)
-
- goto bail_die;
-
- goto bail_ok;
- }
- if (wsi->state == LWSS_ESTABLISHED &&
- !wsi->socket_is_permanently_unusable &&
- wsi->u.ws.send_check_ping) {
- lwsl_info("issuing ping on wsi %p\n", wsi);
- wsi->u.ws.send_check_ping = 0;
- n = lws_write(wsi, &wsi->u.ws.ping_payload_buf[LWS_PRE],
- 0, LWS_WRITE_PING);
- if (n < 0)
- goto bail_die;
-
- lws_set_timeout(wsi, PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG,
- wsi->context->timeout_secs);
- goto bail_ok;
- }
-
- if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
- goto user_service;
-
- if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
- lwsl_ext("SERVICING TX EXT DRAINING\n");
- if (lws_write(wsi, NULL, 0, LWS_WRITE_CONTINUATION) < 0)
- goto bail_die;
-
- goto bail_ok;
- }
-
- m = lws_ext_cb_active(wsi, LWS_EXT_CB_IS_WRITEABLE, NULL, 0);
- if (m)
- goto bail_die;
- #ifndef LWS_NO_EXTENSIONS
- if (!wsi->extension_data_pending)
- goto user_service;
- #endif
-
- ret = 1;
- if (wsi->mode == LWSCM_RAW || wsi->mode == LWSCM_RAW_FILEDESC)
- ret = 0;
- while (ret == 1) {
-
- ret = 0;
- eff_buf.token = NULL;
- eff_buf.token_len = 0;
-
- m = lws_ext_cb_active(wsi,
- LWS_EXT_CB_PACKET_TX_PRESEND,
- &eff_buf, 0);
- if (m < 0) {
- lwsl_err("ext reports fatal error\n");
- goto bail_die;
- }
- if (m)
-
- ret = 1;
-
- if (eff_buf.token_len) {
- n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
- eff_buf.token_len);
- if (n < 0) {
- lwsl_info("closing from POLLOUT spill\n");
- goto bail_die;
- }
-
- if (n != eff_buf.token_len) {
- lwsl_err("Unable to spill ext %d vs %d\n",
- eff_buf.token_len, n);
- goto bail_die;
- }
- } else
- continue;
-
- if (!ret)
- continue;
-
- if (!lws_send_pipe_choked(wsi))
-
- continue;
- lwsl_info("choked in POLLOUT service\n");
-
- goto bail_ok;
- }
- #ifndef LWS_NO_EXTENSIONS
- wsi->extension_data_pending = 0;
- #endif
- user_service:
-
- if (pollfd) {
- int eff = wsi->leave_pollout_active;
- if (!eff)
- if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
- lwsl_info("failed at set pollfd\n");
- goto bail_die;
- }
- wsi->handling_pollout = 0;
-
- if (!eff && wsi->leave_pollout_active)
-
- lws_calllback_as_writeable(wsi);
- wsi->leave_pollout_active = 0;
- }
- if (wsi->mode != LWSCM_WSCL_ISSUE_HTTP_BODY &&
- !wsi->hdr_parsing_completed)
- goto bail_ok;
- #ifdef LWS_WITH_CGI
- user_service_go_again:
- #endif
- #ifdef LWS_USE_HTTP2
-
- if (wsi->mode != LWSCM_HTTP2_SERVING) {
- lwsl_info("%s: non http2\n", __func__);
- goto notify;
- }
- wsi->u.http2.requested_POLLOUT = 0;
- if (!wsi->u.http2.initialized) {
- lwsl_info("pollout on uninitialized http2 conn\n");
- goto bail_ok;
- }
- lwsl_info("%s: doing children\n", __func__);
- wsi2 = wsi;
- do {
- wsi2 = wsi2->u.http2.next_child_wsi;
- lwsl_info("%s: child %p\n", __func__, wsi2);
- if (!wsi2)
- continue;
- if (!wsi2->u.http2.requested_POLLOUT)
- continue;
- wsi2->u.http2.requested_POLLOUT = 0;
- if (lws_calllback_as_writeable(wsi2)) {
- lwsl_debug("Closing POLLOUT child\n");
- lws_close_free_wsi(wsi2, LWS_CLOSE_STATUS_NOSTATUS);
- }
- wsi2 = wsi;
- } while (wsi2 != NULL && !lws_send_pipe_choked(wsi));
- lwsl_info("%s: completed\n", __func__);
- goto bail_ok;
- notify:
- #endif
- wsi->handling_pollout = 0;
- wsi->leave_pollout_active = 0;
- return lws_calllback_as_writeable(wsi);
-
- bail_ok:
- wsi->handling_pollout = 0;
- wsi->leave_pollout_active = 0;
- return 0;
- bail_die:
- wsi->handling_pollout = 0;
- wsi->leave_pollout_active = 0;
- return -1;
- }
- int
- lws_service_timeout_check(struct lws *wsi, unsigned int sec)
- {
- struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
- int n = 0;
- (void)n;
-
- if (lws_ext_cb_active(wsi, LWS_EXT_CB_1HZ, NULL, sec) < 0)
- return 0;
- if (!wsi->pending_timeout)
- return 0;
-
- if ((time_t)sec > wsi->pending_timeout_limit) {
- if (wsi->desc.sockfd != LWS_SOCK_INVALID && wsi->position_in_fds_table >= 0)
- n = pt->fds[wsi->position_in_fds_table].events;
-
- if (wsi->pending_timeout != PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE)
- lwsl_notice("wsi %p: TIMEDOUT WAITING on %d (did hdr %d, ah %p, wl %d, pfd events %d) %llu vs %llu\n",
- (void *)wsi, wsi->pending_timeout,
- wsi->hdr_parsing_completed, wsi->u.hdr.ah,
- pt->ah_wait_list_length, n, (unsigned long long)sec, (unsigned long long)wsi->pending_timeout_limit);
-
- wsi->socket_is_permanently_unusable = 1;
- if (wsi->mode == LWSCM_WSCL_WAITING_SSL)
- wsi->vhost->protocols[0].callback(wsi,
- LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
- wsi->user_space, (void *)"Timed out waiting SSL", 21);
- lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
- return 1;
- }
- return 0;
- }
- int lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len)
- {
-
- if (wsi->rxflow_buffer) {
-
- lwsl_info("stalling in existing rxflow buf\n");
- return 1;
- }
-
- lwsl_info("new rxflow input buffer len %d\n", len - n);
- wsi->rxflow_buffer = lws_malloc(len - n);
- if (!wsi->rxflow_buffer)
- return -1;
- wsi->rxflow_len = len - n;
- wsi->rxflow_pos = 0;
- memcpy(wsi->rxflow_buffer, buf + n, len - n);
- return 0;
- }
- LWS_VISIBLE LWS_EXTERN int
- lws_service_adjust_timeout(struct lws_context *context, int timeout_ms, int tsi)
- {
- struct lws_context_per_thread *pt = &context->pt[tsi];
- int n;
-
-
- if (pt->rx_draining_ext_list)
- return 0;
- #ifdef LWS_OPENSSL_SUPPORT
-
- if (lws_ssl_anybody_has_buffered_read_tsi(context, tsi)) {
- lwsl_info("ssl buffered read\n");
- return 0;
- }
- #endif
-
- for (n = 0; n < context->max_http_header_pool; n++)
- if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen) {
-
- if (!pt->ah_pool[n].wsi) {
- lwsl_err("%s: assert: no wsi attached to ah\n", __func__);
- assert(0);
- }
- return 0;
- }
- return timeout_ms;
- }
- int
- lws_service_flag_pending(struct lws_context *context, int tsi)
- {
- struct lws_context_per_thread *pt = &context->pt[tsi];
- #ifdef LWS_OPENSSL_SUPPORT
- struct lws *wsi_next;
- #endif
- struct lws *wsi;
- int forced = 0;
- int n;
-
-
- wsi = pt->rx_draining_ext_list;
- while (wsi) {
- pt->fds[wsi->position_in_fds_table].revents |=
- pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
- if (pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN) {
- forced = 1;
- break;
- }
- wsi = wsi->u.ws.rx_draining_ext_list;
- }
- #ifdef LWS_OPENSSL_SUPPORT
-
- wsi = pt->pending_read_list;
- while (wsi) {
- wsi_next = wsi->pending_read_list_next;
- pt->fds[wsi->position_in_fds_table].revents |=
- pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN;
- if (pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN) {
- forced = 1;
-
- lws_ssl_remove_wsi_from_buffered_list(wsi);
- }
- wsi = wsi_next;
- }
- #endif
-
- for (n = 0; n < context->max_http_header_pool; n++)
- if (pt->ah_pool[n].rxpos != pt->ah_pool[n].rxlen &&
- !pt->ah_pool[n].wsi->hdr_parsing_completed) {
- pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents |=
- pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].events &
- LWS_POLLIN;
- if (pt->fds[pt->ah_pool[n].wsi->position_in_fds_table].revents &
- LWS_POLLIN)
- forced = 1;
- }
- return forced;
- }
- #ifndef LWS_NO_CLIENT
- LWS_VISIBLE int
- lws_http_client_read(struct lws *wsi, char **buf, int *len)
- {
- int rlen, n;
- rlen = lws_ssl_capable_read(wsi, (unsigned char *)*buf, *len);
- *len = 0;
-
- lws_change_pollfd(wsi, 0, LWS_POLLIN);
- if (rlen == LWS_SSL_CAPABLE_ERROR) {
- lwsl_notice("%s: SSL capable error\n", __func__);
- return -1;
- }
- if (rlen <= 0)
- return 0;
- *len = rlen;
- wsi->client_rx_avail = 0;
-
- spin_chunks:
- while (wsi->chunked && (wsi->chunk_parser != ELCP_CONTENT) && *len) {
- switch (wsi->chunk_parser) {
- case ELCP_HEX:
- if ((*buf)[0] == '\x0d') {
- wsi->chunk_parser = ELCP_CR;
- break;
- }
- n = char_to_hex((*buf)[0]);
- if (n < 0) {
- lwsl_debug("chunking failure\n");
- return -1;
- }
- wsi->chunk_remaining <<= 4;
- wsi->chunk_remaining |= n;
- break;
- case ELCP_CR:
- if ((*buf)[0] != '\x0a') {
- lwsl_debug("chunking failure\n");
- return -1;
- }
- wsi->chunk_parser = ELCP_CONTENT;
- lwsl_info("chunk %d\n", wsi->chunk_remaining);
- if (wsi->chunk_remaining)
- break;
- lwsl_info("final chunk\n");
- goto completed;
- case ELCP_CONTENT:
- break;
- case ELCP_POST_CR:
- if ((*buf)[0] != '\x0d') {
- lwsl_debug("chunking failure\n");
- return -1;
- }
- wsi->chunk_parser = ELCP_POST_LF;
- break;
- case ELCP_POST_LF:
- if ((*buf)[0] != '\x0a')
- return -1;
- wsi->chunk_parser = ELCP_HEX;
- wsi->chunk_remaining = 0;
- break;
- }
- (*buf)++;
- (*len)--;
- }
- if (wsi->chunked && !wsi->chunk_remaining)
- return 0;
- if (wsi->u.http.content_remain &&
- (int)wsi->u.http.content_remain < *len)
- n = wsi->u.http.content_remain;
- else
- n = *len;
- if (wsi->chunked && wsi->chunk_remaining &&
- wsi->chunk_remaining < n)
- n = wsi->chunk_remaining;
- #ifdef LWS_WITH_HTTP_PROXY
-
- if (wsi->perform_rewrite)
- lws_rewrite_parse(wsi->rw, (unsigned char *)*buf, n);
- else
- #endif
- if (user_callback_handle_rxflow(wsi->protocol->callback,
- wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
- wsi->user_space, *buf, n)) {
- lwsl_debug("%s: LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ returned -1\n", __func__);
- return -1;
- }
- if (wsi->chunked && wsi->chunk_remaining) {
- (*buf) += n;
- wsi->chunk_remaining -= n;
- *len -= n;
- }
- if (wsi->chunked && !wsi->chunk_remaining)
- wsi->chunk_parser = ELCP_POST_CR;
- if (wsi->chunked && *len)
- goto spin_chunks;
- if (wsi->chunked)
- return 0;
-
- if (wsi->u.http.content_length > 0)
- wsi->u.http.content_remain -= n;
- if (wsi->u.http.content_remain || !wsi->u.http.content_length)
- return 0;
- completed:
- if (user_callback_handle_rxflow(wsi->protocol->callback,
- wsi, LWS_CALLBACK_COMPLETED_CLIENT_HTTP,
- wsi->user_space, NULL, 0)) {
- lwsl_debug("Completed call returned -1\n");
- return -1;
- }
- if (lws_http_transaction_completed_client(wsi)) {
- lwsl_notice("%s: transaction completed says -1\n", __func__);
- return -1;
- }
- return 0;
- }
- #endif
- static int
- lws_is_ws_with_ext(struct lws *wsi)
- {
- #if defined(LWS_NO_EXTENSIONS)
- return 0;
- #else
- return wsi->state == LWSS_ESTABLISHED &&
- !!wsi->count_act_ext;
- #endif
- }
- LWS_VISIBLE int
- lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd, int tsi)
- {
- struct lws_context_per_thread *pt = &context->pt[tsi];
- lws_sockfd_type our_fd = 0, tmp_fd;
- struct lws_tokens eff_buf;
- unsigned int pending = 0;
- struct lws *wsi, *wsi1;
- char draining_flow = 0;
- int timed_out = 0;
- time_t now;
- int n = 0, m;
- int more;
- if (!context->protocol_init_done)
- lws_protocol_init(context);
- time(&now);
-
- if (context->time_up < 1464083026 && now > 1464083026)
- context->time_up = now;
-
- if (context->last_timeout_check_s != now) {
- context->last_timeout_check_s = now;
- lws_plat_service_periodic(context);
-
- #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_WITH_ESP32)
- #if LWS_POSIX && !defined(_WIN32)
- if (context->deprecated && !context->count_wsi_allocated) {
- lwsl_notice("%s: ending deprecated context\n", __func__);
- kill(getpid(), SIGINT);
- return 0;
- }
- #endif
- #endif
-
- if (pollfd)
- our_fd = pollfd->fd;
- wsi = context->pt[tsi].timeout_list;
- while (wsi) {
-
- wsi1 = wsi->timeout_list;
- tmp_fd = wsi->desc.sockfd;
- if (lws_service_timeout_check(wsi, (unsigned int)now)) {
-
- if (tmp_fd == our_fd)
-
- timed_out = 1;
-
- }
- wsi = wsi1;
- }
- #ifdef LWS_WITH_CGI
- lws_cgi_kill_terminated(pt);
- #endif
- #if 0
- {
- char s[300], *p = s;
- for (n = 0; n < context->count_threads; n++)
- p += sprintf(p, " %7lu (%5d), ",
- context->pt[n].count_conns,
- context->pt[n].fds_count);
- lwsl_notice("load: %s\n", s);
- }
- #endif
- }
-
- if (context->ws_ping_pong_interval &&
- context->last_ws_ping_pong_check_s < now + 10) {
- struct lws_vhost *vh = context->vhost_list;
- context->last_ws_ping_pong_check_s = now;
- while (vh) {
- for (n = 0; n < vh->count_protocols; n++) {
- wsi = vh->same_vh_protocol_list[n];
- while (wsi) {
- if (wsi->state == LWSS_ESTABLISHED &&
- !wsi->socket_is_permanently_unusable &&
- !wsi->u.ws.send_check_ping &&
- wsi->u.ws.time_next_ping_check &&
- wsi->u.ws.time_next_ping_check < now) {
- lwsl_info("requesting ping-pong on wsi %p\n", wsi);
- wsi->u.ws.send_check_ping = 1;
- lws_set_timeout(wsi, PENDING_TIMEOUT_WS_PONG_CHECK_SEND_PING,
- context->timeout_secs);
- lws_callback_on_writable(wsi);
- wsi->u.ws.time_next_ping_check = now +
- wsi->context->ws_ping_pong_interval;
- }
- wsi = wsi->same_vh_protocol_next;
- }
- }
- vh = vh->vhost_next;
- }
- }
-
- if (timed_out)
- return 0;
-
- if (!pollfd)
- return 0;
-
- wsi = wsi_from_fd(context, pollfd->fd);
- if (!wsi)
-
- return 0;
-
- #if LWS_POSIX
-
- if ((!(pollfd->revents & pollfd->events & LWS_POLLIN)) &&
- (pollfd->revents & LWS_POLLHUP)) {
- wsi->socket_is_permanently_unusable = 1;
- lwsl_debug("Session Socket %p (fd=%d) dead\n",
- (void *)wsi, pollfd->fd);
- goto close_and_handled;
- }
- #ifdef _WIN32
- if (pollfd->revents & LWS_POLLOUT)
- wsi->sock_send_blocking = FALSE;
- #endif
- #endif
- if (pollfd->revents & LWS_POLLHUP)
- goto close_and_handled;
- #ifdef LWS_OPENSSL_SUPPORT
- if ((wsi->state == LWSS_SHUTDOWN) && lws_is_ssl(wsi) && wsi->ssl)
- {
- n = SSL_shutdown(wsi->ssl);
- lwsl_debug("SSL_shutdown=%d for fd %d\n", n, wsi->desc.sockfd);
- if (n == 1)
- {
- n = shutdown(wsi->desc.sockfd, SHUT_WR);
- goto close_and_handled;
- }
- else if (n == 0)
- {
- lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
- n = 0;
- goto handled;
- }
- else
- {
- int shutdown_error = SSL_get_error(wsi->ssl, n);
- lwsl_debug("SSL_shutdown returned %d, SSL_get_error: %d\n", n, shutdown_error);
- if (shutdown_error == SSL_ERROR_WANT_READ) {
- lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
- n = 0;
- goto handled;
- } else if (shutdown_error == SSL_ERROR_WANT_WRITE) {
- lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLOUT);
- n = 0;
- goto handled;
- }
-
- n = shutdown(wsi->desc.sockfd, SHUT_WR);
- goto close_and_handled;
- }
- }
- #endif
-
- switch (wsi->mode) {
- case LWSCM_HTTP_SERVING:
- case LWSCM_HTTP_CLIENT:
- case LWSCM_HTTP_SERVING_ACCEPTED:
- case LWSCM_SERVER_LISTENER:
- case LWSCM_SSL_ACK_PENDING:
- case LWSCM_SSL_ACK_PENDING_RAW:
- if (wsi->state == LWSS_CLIENT_HTTP_ESTABLISHED)
- goto handled;
- #ifdef LWS_WITH_CGI
- if (wsi->cgi && (pollfd->revents & LWS_POLLOUT)) {
- n = lws_handle_POLLOUT_event(wsi, pollfd);
- if (n)
- goto close_and_handled;
- goto handled;
- }
- #endif
-
- case LWSCM_RAW:
- n = lws_server_socket_service(context, wsi, pollfd);
- if (n)
- return 1;
- goto handled;
- case LWSCM_RAW_FILEDESC:
- if (pollfd->revents & LWS_POLLOUT) {
- n = lws_calllback_as_writeable(wsi);
- if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
- lwsl_info("failed at set pollfd\n");
- return 1;
- }
- if (n)
- goto close_and_handled;
- }
- n = LWS_CALLBACK_RAW_RX;
- if (wsi->mode == LWSCM_RAW_FILEDESC)
- n = LWS_CALLBACK_RAW_RX_FILE;
- if (pollfd->revents & LWS_POLLIN) {
- if (user_callback_handle_rxflow(
- wsi->protocol->callback,
- wsi, n,
- wsi->user_space, NULL, 0)) {
- lwsl_debug("raw rx callback closed it\n");
- goto close_and_handled;
- }
- }
- if (pollfd->revents & LWS_POLLHUP)
- goto close_and_handled;
- n = 0;
- goto handled;
- case LWSCM_WS_SERVING:
- case LWSCM_WS_CLIENT:
- case LWSCM_HTTP2_SERVING:
- case LWSCM_HTTP_CLIENT_ACCEPTED:
-
- if ((pollfd->revents & LWS_POLLOUT) &&
- ((wsi->state == LWSS_ESTABLISHED ||
- wsi->state == LWSS_HTTP2_ESTABLISHED ||
- wsi->state == LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS ||
- wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
- wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE)) &&
- lws_handle_POLLOUT_event(wsi, pollfd)) {
- if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
- wsi->state = LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE;
- lwsl_info("lws_service_fd: closing\n");
- goto close_and_handled;
- }
- if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
- wsi->state == LWSS_AWAITING_CLOSE_ACK) {
-
- lws_rx_flow_control(wsi, 1);
- wsi->u.ws.tx_draining_ext = 0;
- }
- if (wsi->u.ws.tx_draining_ext)
-
- break;
- if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW))
-
- break;
-
- if (wsi->state == LWSS_ESTABLISHED &&
- wsi->u.ws.rx_draining_ext) {
- lwsl_ext("%s: RX EXT DRAINING: Service\n", __func__);
- #ifndef LWS_NO_CLIENT
- if (wsi->mode == LWSCM_WS_CLIENT) {
- n = lws_client_rx_sm(wsi, 0);
- if (n < 0)
-
- n = 0;
- } else
- #endif
- n = lws_rx_sm(wsi, 0);
- goto handled;
- }
- if (wsi->u.ws.rx_draining_ext)
-
- break;
-
- if (wsi->rxflow_buffer) {
- lwsl_info("draining rxflow (len %d)\n",
- wsi->rxflow_len - wsi->rxflow_pos
- );
-
- eff_buf.token = (char *)wsi->rxflow_buffer +
- wsi->rxflow_pos;
- eff_buf.token_len = wsi->rxflow_len - wsi->rxflow_pos;
- draining_flow = 1;
- goto drain;
- }
-
- if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
- break;
- read:
-
- if (wsi->u.hdr.ah) {
- lwsl_info("%s: %p: inherited ah rx\n", __func__, wsi);
- eff_buf.token_len = wsi->u.hdr.ah->rxlen -
- wsi->u.hdr.ah->rxpos;
- eff_buf.token = (char *)wsi->u.hdr.ah->rx +
- wsi->u.hdr.ah->rxpos;
- } else {
- if (wsi->mode != LWSCM_HTTP_CLIENT_ACCEPTED) {
-
- eff_buf.token = (char *)pt->serv_buf;
- if (lws_is_ws_with_ext(wsi)) {
- eff_buf.token_len = wsi->u.ws.rx_ubuf_alloc;
- } else {
- eff_buf.token_len = context->pt_serv_buf_size;
- }
- if (eff_buf.token_len > context->pt_serv_buf_size)
- eff_buf.token_len = context->pt_serv_buf_size;
- eff_buf.token_len = lws_ssl_capable_read(wsi,
- (unsigned char *)eff_buf.token, pending ? pending :
- eff_buf.token_len);
- switch (eff_buf.token_len) {
- case 0:
- lwsl_info("%s: zero length read\n", __func__);
- goto close_and_handled;
- case LWS_SSL_CAPABLE_MORE_SERVICE:
- lwsl_info("SSL Capable more service\n");
- n = 0;
- goto handled;
- case LWS_SSL_CAPABLE_ERROR:
- lwsl_info("Closing when error\n");
- goto close_and_handled;
- }
-
- }
- }
- drain:
- #ifndef LWS_NO_CLIENT
- if (wsi->mode == LWSCM_HTTP_CLIENT_ACCEPTED &&
- !wsi->told_user_closed) {
-
- wsi->client_rx_avail = 1;
- lws_change_pollfd(wsi, LWS_POLLIN, 0);
-
- if (user_callback_handle_rxflow(
- wsi->protocol->callback,
- wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP,
- wsi->user_space, NULL, 0)) {
- lwsl_debug("LWS_CALLBACK_RECEIVE_CLIENT_HTTP closed it\n");
- goto close_and_handled;
- }
- }
- #endif
-
- do {
- more = 0;
- m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_RX_PREPARSE,
- &eff_buf, 0);
- if (m < 0)
- goto close_and_handled;
- if (m)
- more = 1;
-
- if (eff_buf.token_len) {
-
-
- n = lws_read(wsi, (unsigned char *)eff_buf.token,
- eff_buf.token_len);
- if (n < 0) {
-
- n = 0;
- goto handled;
- }
- }
- eff_buf.token = NULL;
- eff_buf.token_len = 0;
- } while (more);
- if (wsi->u.hdr.ah) {
- lwsl_notice("%s: %p: detaching\n",
- __func__, wsi);
-
- wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
-
- lws_header_table_detach(wsi, 0);
- }
- pending = lws_ssl_pending(wsi);
- if (pending) {
- if (lws_is_ws_with_ext(wsi))
- pending = pending > wsi->u.ws.rx_ubuf_alloc ?
- wsi->u.ws.rx_ubuf_alloc : pending;
- else
- pending = pending > context->pt_serv_buf_size ?
- context->pt_serv_buf_size : pending;
- goto read;
- }
- if (draining_flow && wsi->rxflow_buffer &&
- wsi->rxflow_pos == wsi->rxflow_len) {
- lwsl_info("flow buffer: drained\n");
- lws_free_set_NULL(wsi->rxflow_buffer);
-
- #ifdef LWS_NO_SERVER
- n =
- #endif
- _lws_rx_flow_control(wsi);
-
- }
- break;
- #ifdef LWS_WITH_CGI
- case LWSCM_CGI:
- {
- struct lws_cgi_args args;
- if (wsi->cgi_channel >= LWS_STDOUT &&
- !(pollfd->revents & pollfd->events & LWS_POLLIN))
- break;
- if (wsi->cgi_channel == LWS_STDIN &&
- !(pollfd->revents & pollfd->events & LWS_POLLOUT))
- break;
- if (wsi->cgi_channel == LWS_STDIN)
- if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
- lwsl_info("failed at set pollfd\n");
- return 1;
- }
- args.ch = wsi->cgi_channel;
- args.stdwsi = &wsi->parent->cgi->stdwsi[0];
- args.hdr_state = wsi->hdr_state;
-
-
- if (user_callback_handle_rxflow(
- wsi->parent->protocol->callback,
- wsi->parent, LWS_CALLBACK_CGI,
- wsi->parent->user_space,
- (void *)&args, 0))
- return 1;
- break;
- }
- #endif
- default:
- #ifdef LWS_NO_CLIENT
- break;
- #else
- if ((pollfd->revents & LWS_POLLOUT) &&
- lws_handle_POLLOUT_event(wsi, pollfd)) {
- lwsl_debug("POLLOUT event closed it\n");
- goto close_and_handled;
- }
- n = lws_client_socket_service(context, wsi, pollfd);
- if (n)
- return 1;
- goto handled;
- #endif
- }
- n = 0;
- goto handled;
- close_and_handled:
- lwsl_debug("Close and handled\n");
- lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
-
- return 1;
- handled:
- pollfd->revents = 0;
- return n;
- }
- LWS_VISIBLE int
- lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd)
- {
- return lws_service_fd_tsi(context, pollfd, 0);
- }
- LWS_VISIBLE int
- lws_service(struct lws_context *context, int timeout_ms)
- {
- return lws_plat_service(context, timeout_ms);
- }
- LWS_VISIBLE int
- lws_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
- {
- return _lws_plat_service_tsi(context, timeout_ms, tsi);
- }
|