service.c 34 KB

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