ops-h1.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include <private-lib-core.h>
  25. #ifndef min
  26. #define min(a, b) ((a) < (b) ? (a) : (b))
  27. #endif
  28. /*
  29. * We have to take care about parsing because the headers may be split
  30. * into multiple fragments. They may contain unknown headers with arbitrary
  31. * argument lengths. So, we parse using a single-character at a time state
  32. * machine that is completely independent of packet size.
  33. *
  34. * Returns <0 for error or length of chars consumed from buf (up to len)
  35. */
  36. int
  37. lws_read_h1(struct lws *wsi, unsigned char *buf, lws_filepos_t len)
  38. {
  39. unsigned char *last_char, *oldbuf = buf;
  40. lws_filepos_t body_chunk_len;
  41. size_t n;
  42. lwsl_debug("%s: h1 path: wsi state 0x%x\n", __func__, lwsi_state(wsi));
  43. switch (lwsi_state(wsi)) {
  44. case LRS_ISSUING_FILE:
  45. return 0;
  46. case LRS_ESTABLISHED:
  47. if (lwsi_role_ws(wsi))
  48. goto ws_mode;
  49. if (lwsi_role_client(wsi))
  50. break;
  51. wsi->hdr_parsing_completed = 0;
  52. /* fallthru */
  53. case LRS_HEADERS:
  54. if (!wsi->http.ah) {
  55. lwsl_err("%s: LRS_HEADERS: NULL ah\n", __func__);
  56. assert(0);
  57. }
  58. lwsl_parser("issuing %d bytes to parser\n", (int)len);
  59. #if defined(LWS_ROLE_WS) && defined(LWS_WITH_CLIENT)
  60. if (lws_ws_handshake_client(wsi, &buf, (size_t)len))
  61. goto bail;
  62. #endif
  63. last_char = buf;
  64. if (lws_handshake_server(wsi, &buf, (size_t)len))
  65. /* Handshake indicates this session is done. */
  66. goto bail;
  67. /* we might have transitioned to RAW */
  68. if (wsi->role_ops == &role_ops_raw_skt
  69. #if defined(LWS_ROLE_RAW_FILE)
  70. ||
  71. wsi->role_ops == &role_ops_raw_file
  72. #endif
  73. )
  74. /* we gave the read buffer to RAW handler already */
  75. goto read_ok;
  76. /*
  77. * It's possible that we've exhausted our data already, or
  78. * rx flow control has stopped us dealing with this early,
  79. * but lws_handshake_server doesn't update len for us.
  80. * Figure out how much was read, so that we can proceed
  81. * appropriately:
  82. */
  83. len -= (buf - last_char);
  84. if (!wsi->hdr_parsing_completed)
  85. /* More header content on the way */
  86. goto read_ok;
  87. switch (lwsi_state(wsi)) {
  88. case LRS_ESTABLISHED:
  89. case LRS_HEADERS:
  90. goto read_ok;
  91. case LRS_ISSUING_FILE:
  92. goto read_ok;
  93. case LRS_DISCARD_BODY:
  94. case LRS_BODY:
  95. wsi->http.rx_content_remain =
  96. wsi->http.rx_content_length;
  97. if (wsi->http.rx_content_remain)
  98. goto http_postbody;
  99. /* there is no POST content */
  100. goto postbody_completion;
  101. default:
  102. break;
  103. }
  104. break;
  105. case LRS_DISCARD_BODY:
  106. case LRS_BODY:
  107. http_postbody:
  108. lwsl_debug("%s: http post body: remain %d\n", __func__,
  109. (int)wsi->http.rx_content_remain);
  110. if (!wsi->http.rx_content_remain)
  111. goto postbody_completion;
  112. while (len && wsi->http.rx_content_remain) {
  113. /* Copy as much as possible, up to the limit of:
  114. * what we have in the read buffer (len)
  115. * remaining portion of the POST body (content_remain)
  116. */
  117. body_chunk_len = min(wsi->http.rx_content_remain, len);
  118. wsi->http.rx_content_remain -= body_chunk_len;
  119. // len -= body_chunk_len;
  120. #ifdef LWS_WITH_CGI
  121. if (wsi->http.cgi) {
  122. struct lws_cgi_args args;
  123. args.ch = LWS_STDIN;
  124. args.stdwsi = &wsi->http.cgi->lsp->stdwsi[0];
  125. args.data = buf;
  126. args.len = body_chunk_len;
  127. /* returns how much used */
  128. n = user_callback_handle_rxflow(
  129. wsi->a.protocol->callback,
  130. wsi, LWS_CALLBACK_CGI_STDIN_DATA,
  131. wsi->user_space,
  132. (void *)&args, 0);
  133. if ((int)n < 0)
  134. goto bail;
  135. } else {
  136. #endif
  137. if (lwsi_state(wsi) != LRS_DISCARD_BODY) {
  138. n = wsi->a.protocol->callback(wsi,
  139. LWS_CALLBACK_HTTP_BODY, wsi->user_space,
  140. buf, (size_t)body_chunk_len);
  141. if (n)
  142. goto bail;
  143. }
  144. n = (size_t)body_chunk_len;
  145. #ifdef LWS_WITH_CGI
  146. }
  147. #endif
  148. buf += n;
  149. if (wsi->http.rx_content_remain) {
  150. lws_set_timeout(wsi,
  151. PENDING_TIMEOUT_HTTP_CONTENT,
  152. wsi->a.context->timeout_secs);
  153. break;
  154. }
  155. /* he sent all the content in time */
  156. postbody_completion:
  157. #ifdef LWS_WITH_CGI
  158. /*
  159. * If we're running a cgi, we can't let him off the
  160. * hook just because he sent his POST data
  161. */
  162. if (wsi->http.cgi)
  163. lws_set_timeout(wsi, PENDING_TIMEOUT_CGI,
  164. wsi->a.context->timeout_secs);
  165. else
  166. #endif
  167. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  168. #ifdef LWS_WITH_CGI
  169. if (!wsi->http.cgi)
  170. #endif
  171. {
  172. #if defined(LWS_WITH_SERVER)
  173. if (lwsi_state(wsi) == LRS_DISCARD_BODY) {
  174. /*
  175. * repeat the transaction completed
  176. * that got us into this state, having
  177. * consumed the pending body now
  178. */
  179. if (lws_http_transaction_completed(wsi))
  180. goto bail;
  181. break;
  182. }
  183. #endif
  184. lwsl_info("HTTP_BODY_COMPLETION: %p (%s)\n",
  185. wsi, wsi->a.protocol->name);
  186. n = wsi->a.protocol->callback(wsi,
  187. LWS_CALLBACK_HTTP_BODY_COMPLETION,
  188. wsi->user_space, NULL, 0);
  189. if (n)
  190. goto bail;
  191. if (wsi->mux_substream)
  192. lwsi_set_state(wsi, LRS_ESTABLISHED);
  193. }
  194. break;
  195. }
  196. break;
  197. case LRS_RETURNED_CLOSE:
  198. case LRS_AWAITING_CLOSE_ACK:
  199. case LRS_WAITING_TO_SEND_CLOSE:
  200. case LRS_SHUTDOWN:
  201. ws_mode:
  202. #if defined(LWS_WITH_CLIENT) && defined(LWS_ROLE_WS)
  203. // lwsl_notice("%s: ws_mode\n", __func__);
  204. if (lws_ws_handshake_client(wsi, &buf, (size_t)len))
  205. goto bail;
  206. #endif
  207. #if defined(LWS_ROLE_WS)
  208. if (lwsi_role_ws(wsi) && lwsi_role_server(wsi) &&
  209. /*
  210. * for h2 we are on the swsi
  211. */
  212. lws_parse_ws(wsi, &buf, (size_t)len) < 0) {
  213. lwsl_info("%s: lws_parse_ws bailed\n", __func__);
  214. goto bail;
  215. }
  216. #endif
  217. // lwsl_notice("%s: ws_mode: buf moved on by %d\n", __func__,
  218. // lws_ptr_diff(buf, oldbuf));
  219. break;
  220. case LRS_DEFERRING_ACTION:
  221. lwsl_notice("%s: LRS_DEFERRING_ACTION\n", __func__);
  222. break;
  223. case LRS_SSL_ACK_PENDING:
  224. break;
  225. case LRS_FLUSHING_BEFORE_CLOSE:
  226. break;
  227. case LRS_DEAD_SOCKET:
  228. lwsl_err("%s: Unhandled state LRS_DEAD_SOCKET\n", __func__);
  229. goto bail;
  230. // assert(0);
  231. /* fallthru */
  232. default:
  233. lwsl_err("%s: Unhandled state %d\n", __func__, lwsi_state(wsi));
  234. assert(0);
  235. goto bail;
  236. }
  237. read_ok:
  238. /* Nothing more to do for now */
  239. // lwsl_info("%s: %p: read_ok, used %ld (len %d, state %d)\n", __func__,
  240. // wsi, (long)(buf - oldbuf), (int)len, wsi->state);
  241. return lws_ptr_diff(buf, oldbuf);
  242. bail:
  243. /*
  244. * h2 / h2-ws calls us recursively in
  245. *
  246. * lws_read_h1()->
  247. * lws_h2_parser()->
  248. * lws_read_h1()
  249. *
  250. * pattern, having stripped the h2 framing in the middle.
  251. *
  252. * When taking down the whole connection, make sure that only the
  253. * outer lws_read() does the wsi close.
  254. */
  255. if (!wsi->outer_will_close)
  256. lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
  257. "lws_read_h1 bail");
  258. return -1;
  259. }
  260. #if defined(LWS_WITH_SERVER)
  261. static int
  262. lws_h1_server_socket_service(struct lws *wsi, struct lws_pollfd *pollfd)
  263. {
  264. struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
  265. struct lws_tokens ebuf;
  266. int n, buffered;
  267. if (lwsi_state(wsi) == LRS_DEFERRING_ACTION)
  268. goto try_pollout;
  269. /* any incoming data ready? */
  270. if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
  271. goto try_pollout;
  272. /*
  273. * If we previously just did POLLIN when IN and OUT were signaled
  274. * (because POLLIN processing may have used up the POLLOUT), don't let
  275. * that happen twice in a row... next time we see the situation favour
  276. * POLLOUT
  277. */
  278. if (wsi->favoured_pollin &&
  279. (pollfd->revents & pollfd->events & LWS_POLLOUT)) {
  280. // lwsl_notice("favouring pollout\n");
  281. wsi->favoured_pollin = 0;
  282. goto try_pollout;
  283. }
  284. /*
  285. * We haven't processed that the tunnel is set up yet, so
  286. * defer reading
  287. */
  288. if (lwsi_state(wsi) == LRS_SSL_ACK_PENDING)
  289. return LWS_HPI_RET_HANDLED;
  290. /* these states imply we MUST have an ah attached */
  291. if ((lwsi_state(wsi) == LRS_ESTABLISHED ||
  292. lwsi_state(wsi) == LRS_ISSUING_FILE ||
  293. lwsi_state(wsi) == LRS_HEADERS ||
  294. lwsi_state(wsi) == LRS_DISCARD_BODY ||
  295. lwsi_state(wsi) == LRS_BODY)) {
  296. if (!wsi->http.ah && lws_header_table_attach(wsi, 0)) {
  297. lwsl_info("%s: wsi %p: ah not available\n", __func__,
  298. wsi);
  299. goto try_pollout;
  300. }
  301. /*
  302. * We got here because there was specifically POLLIN...
  303. * regardless of our buflist state, we need to get it,
  304. * and either use it, or append to the buflist and use
  305. * buflist head material.
  306. *
  307. * We will not notice a connection close until the buflist is
  308. * exhausted and we tried to do a read of some kind.
  309. */
  310. ebuf.token = NULL;
  311. ebuf.len = 0;
  312. buffered = lws_buflist_aware_read(pt, wsi, &ebuf, 0, __func__);
  313. switch (ebuf.len) {
  314. case 0:
  315. lwsl_info("%s: read 0 len a\n", __func__);
  316. wsi->seen_zero_length_recv = 1;
  317. if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
  318. goto fail;
  319. #if !defined(LWS_WITHOUT_EXTENSIONS)
  320. /*
  321. * autobahn requires us to win the race between close
  322. * and draining the extensions
  323. */
  324. if (wsi->ws &&
  325. (wsi->ws->rx_draining_ext ||
  326. wsi->ws->tx_draining_ext))
  327. goto try_pollout;
  328. #endif
  329. /*
  330. * normally, we respond to close with logically closing
  331. * our side immediately
  332. */
  333. goto fail;
  334. case LWS_SSL_CAPABLE_ERROR:
  335. goto fail;
  336. case LWS_SSL_CAPABLE_MORE_SERVICE:
  337. goto try_pollout;
  338. }
  339. /* just ignore incoming if waiting for close */
  340. if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE) {
  341. lwsl_notice("%s: just ignoring\n", __func__);
  342. goto try_pollout;
  343. }
  344. if (lwsi_state(wsi) == LRS_ISSUING_FILE) {
  345. // lwsl_notice("stashing: wsi %p: bd %d\n", wsi, buffered);
  346. if (lws_buflist_aware_finished_consuming(wsi, &ebuf, 0,
  347. buffered, __func__))
  348. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  349. goto try_pollout;
  350. }
  351. /*
  352. * Otherwise give it to whoever wants it according to the
  353. * connection state
  354. */
  355. #if defined(LWS_ROLE_H2)
  356. if (lwsi_role_h2(wsi) && lwsi_state(wsi) != LRS_BODY)
  357. n = lws_read_h2(wsi, ebuf.token, ebuf.len);
  358. else
  359. #endif
  360. n = lws_read_h1(wsi, ebuf.token, ebuf.len);
  361. if (n < 0) /* we closed wsi */
  362. return LWS_HPI_RET_WSI_ALREADY_DIED;
  363. // lwsl_notice("%s: consumed %d\n", __func__, n);
  364. if (lws_buflist_aware_finished_consuming(wsi, &ebuf, n,
  365. buffered, __func__))
  366. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  367. /*
  368. * during the parsing our role changed to something non-http,
  369. * so the ah has no further meaning
  370. */
  371. if (wsi->http.ah &&
  372. !lwsi_role_h1(wsi) &&
  373. !lwsi_role_h2(wsi) &&
  374. !lwsi_role_cgi(wsi))
  375. lws_header_table_detach(wsi, 0);
  376. /*
  377. * He may have used up the writability above, if we will defer
  378. * POLLOUT processing in favour of POLLIN, note it
  379. */
  380. if (pollfd->revents & LWS_POLLOUT)
  381. wsi->favoured_pollin = 1;
  382. return LWS_HPI_RET_HANDLED;
  383. }
  384. /*
  385. * He may have used up the writability above, if we will defer POLLOUT
  386. * processing in favour of POLLIN, note it
  387. */
  388. if (pollfd->revents & LWS_POLLOUT)
  389. wsi->favoured_pollin = 1;
  390. try_pollout:
  391. /* this handles POLLOUT for http serving fragments */
  392. if (!(pollfd->revents & LWS_POLLOUT))
  393. return LWS_HPI_RET_HANDLED;
  394. /* one shot */
  395. if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
  396. lwsl_notice("%s a\n", __func__);
  397. goto fail;
  398. }
  399. /* clear back-to-back write detection */
  400. wsi->could_have_pending = 0;
  401. if (lwsi_state(wsi) == LRS_DEFERRING_ACTION) {
  402. lwsl_debug("%s: LRS_DEFERRING_ACTION now writable\n", __func__);
  403. lwsi_set_state(wsi, LRS_ESTABLISHED);
  404. if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
  405. lwsl_info("failed at set pollfd\n");
  406. goto fail;
  407. }
  408. }
  409. if (!wsi->hdr_parsing_completed)
  410. return LWS_HPI_RET_HANDLED;
  411. if (lwsi_state(wsi) != LRS_ISSUING_FILE) {
  412. if (lws_has_buffered_out(wsi)) {
  413. //lwsl_notice("%s: completing partial\n", __func__);
  414. if (lws_issue_raw(wsi, NULL, 0) < 0) {
  415. lwsl_info("%s signalling to close\n", __func__);
  416. goto fail;
  417. }
  418. return LWS_HPI_RET_HANDLED;
  419. }
  420. lws_stats_bump(pt, LWSSTATS_C_WRITEABLE_CB, 1);
  421. #if defined(LWS_WITH_STATS)
  422. if (wsi->active_writable_req_us) {
  423. uint64_t ul = lws_now_usecs() -
  424. wsi->active_writable_req_us;
  425. lws_stats_bump(pt, LWSSTATS_US_WRITABLE_DELAY_AVG, ul);
  426. lws_stats_max(pt,
  427. LWSSTATS_US_WORST_WRITABLE_DELAY, ul);
  428. wsi->active_writable_req_us = 0;
  429. }
  430. #endif
  431. n = user_callback_handle_rxflow(wsi->a.protocol->callback, wsi,
  432. LWS_CALLBACK_HTTP_WRITEABLE,
  433. wsi->user_space, NULL, 0);
  434. if (n < 0) {
  435. lwsl_info("writeable_fail\n");
  436. goto fail;
  437. }
  438. return LWS_HPI_RET_HANDLED;
  439. }
  440. #if defined(LWS_WITH_FILE_OPS)
  441. /* >0 == completion, <0 == error
  442. *
  443. * We'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
  444. * it's done. That's the case even if we just completed the
  445. * send, so wait for that.
  446. */
  447. n = lws_serve_http_file_fragment(wsi);
  448. if (n < 0)
  449. goto fail;
  450. #endif
  451. return LWS_HPI_RET_HANDLED;
  452. fail:
  453. lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
  454. "server socket svc fail");
  455. return LWS_HPI_RET_WSI_ALREADY_DIED;
  456. }
  457. #endif
  458. static int
  459. rops_handle_POLLIN_h1(struct lws_context_per_thread *pt, struct lws *wsi,
  460. struct lws_pollfd *pollfd)
  461. {
  462. if (lwsi_state(wsi) == LRS_IDLING) {
  463. uint8_t buf[1];
  464. int rlen;
  465. /*
  466. * h1 staggered spins here in IDLING if we don't close it.
  467. * It shows POLLIN but the tls connection returns ERROR if
  468. * you try to read it.
  469. */
  470. // lwsl_notice("%s: %p: wsistate 0x%x %s, revents 0x%x\n",
  471. // __func__, wsi, wsi->wsistate, wsi->role_ops->name,
  472. // pollfd->revents);
  473. rlen = lws_ssl_capable_read(wsi, buf, sizeof(buf));
  474. if (rlen == LWS_SSL_CAPABLE_ERROR)
  475. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  476. }
  477. #ifdef LWS_WITH_CGI
  478. if (wsi->http.cgi && (pollfd->revents & LWS_POLLOUT)) {
  479. if (lws_handle_POLLOUT_event(wsi, pollfd))
  480. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  481. return LWS_HPI_RET_HANDLED;
  482. }
  483. #endif
  484. #if 0
  485. /*
  486. * !!! lws_serve_http_file_fragment() seems to duplicate most of
  487. * lws_handle_POLLOUT_event() in its own loop...
  488. */
  489. lwsl_debug("%s: %d %d\n", __func__, (pollfd->revents & LWS_POLLOUT),
  490. lwsi_state_can_handle_POLLOUT(wsi));
  491. if ((pollfd->revents & LWS_POLLOUT) &&
  492. lwsi_state_can_handle_POLLOUT(wsi) &&
  493. lws_handle_POLLOUT_event(wsi, pollfd)) {
  494. if (lwsi_state(wsi) == LRS_RETURNED_CLOSE)
  495. lwsi_set_state(wsi, LRS_FLUSHING_BEFORE_CLOSE);
  496. /* the write failed... it's had it */
  497. wsi->socket_is_permanently_unusable = 1;
  498. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  499. }
  500. #endif
  501. /* Priority 2: pre- compression transform */
  502. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  503. if (wsi->http.comp_ctx.buflist_comp ||
  504. wsi->http.comp_ctx.may_have_more) {
  505. enum lws_write_protocol wp = LWS_WRITE_HTTP;
  506. lwsl_info("%s: completing comp partial (buflist_comp %p, may %d)\n",
  507. __func__, wsi->http.comp_ctx.buflist_comp,
  508. wsi->http.comp_ctx.may_have_more
  509. );
  510. if (wsi->role_ops->write_role_protocol(wsi, NULL, 0, &wp) < 0) {
  511. lwsl_info("%s signalling to close\n", __func__);
  512. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  513. }
  514. lws_callback_on_writable(wsi);
  515. if (!wsi->http.comp_ctx.buflist_comp &&
  516. !wsi->http.comp_ctx.may_have_more &&
  517. wsi->http.deferred_transaction_completed) {
  518. wsi->http.deferred_transaction_completed = 0;
  519. if (lws_http_transaction_completed(wsi))
  520. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  521. }
  522. return LWS_HPI_RET_HANDLED;
  523. }
  524. #endif
  525. if (lws_is_flowcontrolled(wsi))
  526. /* We cannot deal with any kind of new RX because we are
  527. * RX-flowcontrolled.
  528. */
  529. return LWS_HPI_RET_HANDLED;
  530. #if defined(LWS_WITH_SERVER)
  531. if (!lwsi_role_client(wsi)) {
  532. int n;
  533. lwsl_debug("%s: %p: wsistate 0x%x\n", __func__, wsi,
  534. (int)wsi->wsistate);
  535. n = lws_h1_server_socket_service(wsi, pollfd);
  536. if (n != LWS_HPI_RET_HANDLED)
  537. return n;
  538. if (lwsi_state(wsi) != LRS_SSL_INIT)
  539. if (lws_server_socket_service_ssl(wsi,
  540. LWS_SOCK_INVALID,
  541. !!(pollfd->revents & LWS_POLLIN)))
  542. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  543. return LWS_HPI_RET_HANDLED;
  544. }
  545. #endif
  546. #if defined(LWS_WITH_CLIENT)
  547. if ((pollfd->revents & LWS_POLLIN) &&
  548. wsi->hdr_parsing_completed && !wsi->told_user_closed) {
  549. /*
  550. * In SSL mode we get POLLIN notification about
  551. * encrypted data in.
  552. *
  553. * But that is not necessarily related to decrypted
  554. * data out becoming available; in may need to perform
  555. * other in or out before that happens.
  556. *
  557. * simply mark ourselves as having readable data
  558. * and turn off our POLLIN
  559. */
  560. wsi->client_rx_avail = 1;
  561. if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
  562. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  563. //lwsl_notice("calling back %s\n", wsi->a.protocol->name);
  564. /* let user code know, he'll usually ask for writeable
  565. * callback and drain / re-enable it there
  566. */
  567. if (user_callback_handle_rxflow(wsi->a.protocol->callback, wsi,
  568. LWS_CALLBACK_RECEIVE_CLIENT_HTTP,
  569. wsi->user_space, NULL, 0)) {
  570. lwsl_info("RECEIVE_CLIENT_HTTP closed it\n");
  571. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  572. }
  573. return LWS_HPI_RET_HANDLED;
  574. }
  575. #endif
  576. // if (lwsi_state(wsi) == LRS_ESTABLISHED)
  577. // return LWS_HPI_RET_HANDLED;
  578. #if defined(LWS_WITH_CLIENT)
  579. if ((pollfd->revents & LWS_POLLOUT) &&
  580. lws_handle_POLLOUT_event(wsi, pollfd)) {
  581. lwsl_debug("POLLOUT event closed it\n");
  582. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  583. }
  584. if (lws_client_socket_service(wsi, pollfd))
  585. return LWS_HPI_RET_WSI_ALREADY_DIED;
  586. #endif
  587. return LWS_HPI_RET_HANDLED;
  588. }
  589. static int
  590. rops_handle_POLLOUT_h1(struct lws *wsi)
  591. {
  592. if (lwsi_state(wsi) == LRS_ISSUE_HTTP_BODY) {
  593. #if defined(LWS_WITH_HTTP_PROXY)
  594. if (wsi->http.proxy_clientside) {
  595. unsigned char *buf, prebuf[LWS_PRE + 1024];
  596. size_t len = lws_buflist_next_segment_len(
  597. &wsi->parent->http.buflist_post_body, &buf);
  598. int n;
  599. if (len > sizeof(prebuf) - LWS_PRE)
  600. len = sizeof(prebuf) - LWS_PRE;
  601. if (len) {
  602. memcpy(prebuf + LWS_PRE, buf, len);
  603. lwsl_debug("%s: %p: proxying body %d %d %d %d %d\n",
  604. __func__, wsi, (int)len,
  605. (int)wsi->http.tx_content_length,
  606. (int)wsi->http.tx_content_remain,
  607. (int)wsi->http.rx_content_length,
  608. (int)wsi->http.rx_content_remain
  609. );
  610. n = lws_write(wsi, prebuf + LWS_PRE, len, LWS_WRITE_HTTP);
  611. if (n < 0) {
  612. lwsl_err("%s: PROXY_BODY: write %d failed\n",
  613. __func__, (int)len);
  614. return LWS_HP_RET_BAIL_DIE;
  615. }
  616. lws_buflist_use_segment(&wsi->parent->http.buflist_post_body, len);
  617. }
  618. if (wsi->parent->http.buflist_post_body)
  619. lws_callback_on_writable(wsi);
  620. else {
  621. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  622. /* prepare ourselves to do the parsing */
  623. wsi->http.ah->parser_state = WSI_TOKEN_NAME_PART;
  624. wsi->http.ah->lextable_pos = 0;
  625. #if defined(LWS_WITH_CUSTOM_HEADERS)
  626. wsi->http.ah->unk_pos = 0;
  627. #endif
  628. #endif
  629. lwsi_set_state(wsi, LRS_WAITING_SERVER_REPLY);
  630. lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
  631. wsi->a.context->timeout_secs);
  632. }
  633. }
  634. #endif
  635. return LWS_HP_RET_USER_SERVICE;
  636. }
  637. if (lwsi_role_client(wsi))
  638. return LWS_HP_RET_USER_SERVICE;
  639. return LWS_HP_RET_BAIL_OK;
  640. }
  641. static int
  642. rops_write_role_protocol_h1(struct lws *wsi, unsigned char *buf, size_t len,
  643. enum lws_write_protocol *wp)
  644. {
  645. size_t olen = len;
  646. int n;
  647. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  648. if (wsi->http.lcs && (((*wp) & 0x1f) == LWS_WRITE_HTTP_FINAL ||
  649. ((*wp) & 0x1f) == LWS_WRITE_HTTP)) {
  650. unsigned char mtubuf[1500 + LWS_PRE +
  651. LWS_HTTP_CHUNK_HDR_MAX_SIZE +
  652. LWS_HTTP_CHUNK_TRL_MAX_SIZE],
  653. *out = mtubuf + LWS_PRE +
  654. LWS_HTTP_CHUNK_HDR_MAX_SIZE;
  655. size_t o = sizeof(mtubuf) - LWS_PRE -
  656. LWS_HTTP_CHUNK_HDR_MAX_SIZE -
  657. LWS_HTTP_CHUNK_TRL_MAX_SIZE;
  658. n = lws_http_compression_transform(wsi, buf, len, wp, &out, &o);
  659. if (n)
  660. return n;
  661. lwsl_info("%s: %p: transformed %d bytes to %d "
  662. "(wp 0x%x, more %d)\n", __func__, wsi, (int)len,
  663. (int)o, (int)*wp, wsi->http.comp_ctx.may_have_more);
  664. if (!o)
  665. return olen;
  666. if (wsi->http.comp_ctx.chunking) {
  667. char c[LWS_HTTP_CHUNK_HDR_MAX_SIZE + 2];
  668. /*
  669. * this only needs dealing with on http/1.1 to allow
  670. * pipelining
  671. */
  672. n = lws_snprintf(c, sizeof(c), "%X\x0d\x0a", (int)o);
  673. lwsl_info("%s: chunk (%d) %s", __func__, (int)o, c);
  674. out -= n;
  675. o += n;
  676. memcpy(out, c, n);
  677. out[o++] = '\x0d';
  678. out[o++] = '\x0a';
  679. if (((*wp) & 0x1f) == LWS_WRITE_HTTP_FINAL) {
  680. lwsl_info("%s: final chunk\n", __func__);
  681. out[o++] = '0';
  682. out[o++] = '\x0d';
  683. out[o++] = '\x0a';
  684. out[o++] = '\x0d';
  685. out[o++] = '\x0a';
  686. }
  687. }
  688. buf = out;
  689. len = o;
  690. }
  691. #endif
  692. n = lws_issue_raw(wsi, (unsigned char *)buf, len);
  693. if (n < 0)
  694. return n;
  695. /* hide there may have been compression */
  696. return (int)olen;
  697. }
  698. static int
  699. rops_alpn_negotiated_h1(struct lws *wsi, const char *alpn)
  700. {
  701. lwsl_debug("%s: client %d\n", __func__, lwsi_role_client(wsi));
  702. #if defined(LWS_WITH_CLIENT)
  703. if (lwsi_role_client(wsi)) {
  704. /*
  705. * If alpn asserts it is http/1.1, server support for KA is
  706. * mandatory.
  707. *
  708. * Knowing this lets us proceed with sending pipelined headers
  709. * before we received the first response headers.
  710. */
  711. wsi->keepalive_active = 1;
  712. }
  713. #endif
  714. return 0;
  715. }
  716. static int
  717. rops_destroy_role_h1(struct lws *wsi)
  718. {
  719. struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
  720. struct allocated_headers *ah;
  721. /* we may not have an ah, but may be on the waiting list... */
  722. lwsl_info("%s: ah det due to close\n", __func__);
  723. __lws_header_table_detach(wsi, 0);
  724. ah = pt->http.ah_list;
  725. while (ah) {
  726. if (ah->in_use && ah->wsi == wsi) {
  727. lwsl_err("%s: ah leak: wsi %p\n", __func__, wsi);
  728. ah->in_use = 0;
  729. ah->wsi = NULL;
  730. pt->http.ah_count_in_use--;
  731. break;
  732. }
  733. ah = ah->next;
  734. }
  735. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  736. lws_http_compression_destroy(wsi);
  737. #endif
  738. #ifdef LWS_ROLE_WS
  739. lws_free_set_NULL(wsi->ws);
  740. #endif
  741. return 0;
  742. }
  743. #if defined(LWS_WITH_SERVER)
  744. static int
  745. rops_adoption_bind_h1(struct lws *wsi, int type, const char *vh_prot_name)
  746. {
  747. if (!(type & LWS_ADOPT_HTTP))
  748. return 0; /* no match */
  749. if (type & _LWS_ADOPT_FINISH && !lwsi_role_http(wsi))
  750. return 0;
  751. if (type & _LWS_ADOPT_FINISH) {
  752. if (!lws_header_table_attach(wsi, 0))
  753. lwsl_debug("Attached ah immediately\n");
  754. else
  755. lwsl_info("%s: waiting for ah\n", __func__);
  756. return 1;
  757. }
  758. #if defined(LWS_WITH_SERVER) && defined(LWS_WITH_SECURE_STREAMS)
  759. if (wsi->a.vhost->ss_handle &&
  760. wsi->a.vhost->ss_handle->policy->protocol == LWSSSP_RAW) {
  761. lws_role_transition(wsi, LWSIFR_SERVER, (type & LWS_ADOPT_ALLOW_SSL) ?
  762. LRS_SSL_INIT : LRS_ESTABLISHED, &role_ops_raw_skt);
  763. return 1;
  764. }
  765. #endif
  766. /* If Non-TLS and HTTP2 prior knowledge is enabled, skip to clear text HTTP2 */
  767. #if defined(LWS_WITH_HTTP2)
  768. if ((!(type & LWS_ADOPT_ALLOW_SSL)) && (wsi->a.vhost->options & LWS_SERVER_OPTION_H2_PRIOR_KNOWLEDGE)) {
  769. lwsl_info("http/2 prior knowledge\n");
  770. lws_role_call_alpn_negotiated(wsi, "h2");
  771. }
  772. else
  773. #endif
  774. lws_role_transition(wsi, LWSIFR_SERVER, (type & LWS_ADOPT_ALLOW_SSL) ?
  775. LRS_SSL_INIT : LRS_HEADERS, &role_ops_h1);
  776. /*
  777. * Otherwise, we have to bind to h1 as a default even when we're actually going to
  778. * replace it as an h2 bind later. So don't take this seriously if the
  779. * default is disabled (ws upgrade caees properly about it)
  780. */
  781. if (!vh_prot_name && wsi->a.vhost->default_protocol_index <
  782. wsi->a.vhost->count_protocols)
  783. wsi->a.protocol = &wsi->a.vhost->protocols[
  784. wsi->a.vhost->default_protocol_index];
  785. else
  786. wsi->a.protocol = &wsi->a.vhost->protocols[0];
  787. /* the transport is accepted... give him time to negotiate */
  788. lws_set_timeout(wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
  789. wsi->a.context->timeout_secs);
  790. return 1; /* bound */
  791. }
  792. #endif
  793. #if defined(LWS_WITH_CLIENT)
  794. static const char * const http_methods[] = {
  795. "GET", "POST", "OPTIONS", "HEAD", "PUT", "PATCH", "DELETE", "CONNECT"
  796. };
  797. static int
  798. rops_client_bind_h1(struct lws *wsi, const struct lws_client_connect_info *i)
  799. {
  800. int n;
  801. if (!i) {
  802. /* we are finalizing an already-selected role */
  803. /*
  804. * If we stay in http, assuming there wasn't already-set
  805. * external user_space, since we know our initial protocol
  806. * we can assign the user space now, otherwise do it after the
  807. * ws subprotocol negotiated
  808. */
  809. if (!wsi->user_space && wsi->stash->cis[CIS_METHOD])
  810. if (lws_ensure_user_space(wsi))
  811. return 1;
  812. /*
  813. * For ws, default to http/1.1 only. If i->alpn had been set
  814. * though, defer to whatever he has set in there (eg, "h2").
  815. *
  816. * The problem is he has to commit to h2 before he can find
  817. * out if the server has the SETTINGS for ws-over-h2 enabled;
  818. * if not then ws is not possible on that connection. So we
  819. * only try h2 if he assertively said to use h2 alpn, otherwise
  820. * ws implies alpn restriction to h1.
  821. */
  822. if (!wsi->stash->cis[CIS_METHOD] && !wsi->stash->cis[CIS_ALPN])
  823. wsi->stash->cis[CIS_ALPN] = "http/1.1";
  824. /* if we went on the ah waiting list, it's ok, we can wait.
  825. *
  826. * When we do get the ah, now or later, he will end up at
  827. * lws_http_client_connect_via_info2().
  828. */
  829. if (lws_header_table_attach(wsi, 0)
  830. #if defined(LWS_WITH_CLIENT)
  831. < 0)
  832. /*
  833. * if we failed here, the connection is already closed
  834. * and freed.
  835. */
  836. return -1;
  837. #else
  838. )
  839. return 0;
  840. #endif
  841. return 0;
  842. }
  843. /*
  844. * Clients that want to be h1, h2, or ws all start out as h1
  845. * (we don't yet know if the server supports h2 or ws), unless their
  846. * alpn is only "h2"
  847. */
  848. // if (i->alpn && !strcmp(i->alpn, "h2"))
  849. // return 0; /* we are h1, he only wants h2 */
  850. if (!i->method) { /* websockets */
  851. #if defined(LWS_ROLE_WS)
  852. if (lws_create_client_ws_object(i, wsi))
  853. goto fail_wsi;
  854. goto bind_h1;
  855. #else
  856. lwsl_err("%s: ws role not configured\n", __func__);
  857. goto fail_wsi;
  858. #endif
  859. }
  860. /* if a recognized http method, bind to it */
  861. for (n = 0; n < (int)LWS_ARRAY_SIZE(http_methods); n++)
  862. if (!strcmp(i->method, http_methods[n]))
  863. goto bind_h1;
  864. /* other roles may bind to it */
  865. return 0; /* no match */
  866. bind_h1:
  867. /* assert the mode and union status (hdr) clearly */
  868. lws_role_transition(wsi, LWSIFR_CLIENT, LRS_UNCONNECTED, &role_ops_h1);
  869. return 1; /* matched */
  870. fail_wsi:
  871. return -1;
  872. }
  873. #endif
  874. #if 0
  875. static int
  876. rops_perform_user_POLLOUT_h1(struct lws *wsi)
  877. {
  878. volatile struct lws *vwsi = (volatile struct lws *)wsi;
  879. int n;
  880. /* priority 1: post compression-transform buffered output */
  881. if (lws_has_buffered_out(wsi)) {
  882. lwsl_debug("%s: completing partial\n", __func__);
  883. if (lws_issue_raw(wsi, NULL, 0) < 0) {
  884. lwsl_info("%s signalling to close\n", __func__);
  885. return -1;
  886. }
  887. n = 0;
  888. vwsi->leave_pollout_active = 1;
  889. goto cleanup;
  890. }
  891. /* priority 2: pre compression-transform buffered output */
  892. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  893. if (wsi->http.comp_ctx.buflist_comp ||
  894. wsi->http.comp_ctx.may_have_more) {
  895. enum lws_write_protocol wp = LWS_WRITE_HTTP;
  896. lwsl_info("%s: completing comp partial"
  897. "(buflist_comp %p, may %d)\n",
  898. __func__, wsi->http.comp_ctx.buflist_comp,
  899. wsi->http.comp_ctx.may_have_more);
  900. if (rops_write_role_protocol_h1(wsi, NULL, 0, &wp) < 0) {
  901. lwsl_info("%s signalling to close\n", __func__);
  902. lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
  903. "comp write fail");
  904. }
  905. n = 0;
  906. vwsi->leave_pollout_active = 1;
  907. goto cleanup;
  908. }
  909. #endif
  910. /* priority 3: if no buffered out and waiting for that... */
  911. if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE) {
  912. wsi->socket_is_permanently_unusable = 1;
  913. return -1;
  914. }
  915. /* priority 4: user writeable callback */
  916. vwsi = (volatile struct lws *)wsi;
  917. vwsi->leave_pollout_active = 0;
  918. n = lws_callback_as_writeable(wsi);
  919. cleanup:
  920. vwsi->handling_pollout = 0;
  921. if (vwsi->leave_pollout_active)
  922. lws_change_pollfd(wsi, 0, LWS_POLLOUT);
  923. return n;
  924. }
  925. #endif
  926. static int
  927. rops_close_kill_connection_h1(struct lws *wsi, enum lws_close_status reason)
  928. {
  929. #if defined(LWS_WITH_HTTP_PROXY)
  930. if (!wsi->http.proxy_clientside)
  931. return 0;
  932. wsi->http.proxy_clientside = 0;
  933. if (user_callback_handle_rxflow(wsi->a.protocol->callback, wsi,
  934. LWS_CALLBACK_COMPLETED_CLIENT_HTTP,
  935. wsi->user_space, NULL, 0))
  936. return 0;
  937. #endif
  938. return 0;
  939. }
  940. int
  941. rops_pt_init_destroy_h1(struct lws_context *context,
  942. const struct lws_context_creation_info *info,
  943. struct lws_context_per_thread *pt, int destroy)
  944. {
  945. /*
  946. * We only want to do this once... we will do it if no h2 support
  947. * otherwise let h2 ops do it.
  948. */
  949. #if !defined(LWS_ROLE_H2) && defined(LWS_WITH_SERVER)
  950. if (!destroy) {
  951. pt->sul_ah_lifecheck.cb = lws_sul_http_ah_lifecheck;
  952. __lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
  953. &pt->sul_ah_lifecheck, 30 * LWS_US_PER_SEC);
  954. } else
  955. lws_dll2_remove(&pt->sul_ah_lifecheck.list);
  956. #endif
  957. return 0;
  958. }
  959. const struct lws_role_ops role_ops_h1 = {
  960. /* role name */ "h1",
  961. /* alpn id */ "http/1.1",
  962. /* check_upgrades */ NULL,
  963. /* pt_init_destroy */ rops_pt_init_destroy_h1,
  964. /* init_vhost */ NULL,
  965. /* destroy_vhost */ NULL,
  966. /* service_flag_pending */ NULL,
  967. /* handle_POLLIN */ rops_handle_POLLIN_h1,
  968. /* handle_POLLOUT */ rops_handle_POLLOUT_h1,
  969. /* perform_user_POLLOUT */ NULL,
  970. /* callback_on_writable */ NULL,
  971. /* tx_credit */ NULL,
  972. /* write_role_protocol */ rops_write_role_protocol_h1,
  973. /* encapsulation_parent */ NULL,
  974. /* alpn_negotiated */ rops_alpn_negotiated_h1,
  975. /* close_via_role_protocol */ NULL,
  976. /* close_role */ NULL,
  977. /* close_kill_connection */ rops_close_kill_connection_h1,
  978. /* destroy_role */ rops_destroy_role_h1,
  979. #if defined(LWS_WITH_SERVER)
  980. /* adoption_bind */ rops_adoption_bind_h1,
  981. #else
  982. NULL,
  983. #endif
  984. #if defined(LWS_WITH_CLIENT)
  985. /* client_bind */ rops_client_bind_h1,
  986. #else
  987. NULL,
  988. #endif
  989. /* issue_keepalive */ NULL,
  990. /* adoption_cb clnt, srv */ { LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED,
  991. LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED },
  992. /* rx_cb clnt, srv */ { LWS_CALLBACK_RECEIVE_CLIENT_HTTP,
  993. 0 /* may be POST, etc */ },
  994. /* writeable cb clnt, srv */ { LWS_CALLBACK_CLIENT_HTTP_WRITEABLE,
  995. LWS_CALLBACK_HTTP_WRITEABLE },
  996. /* close cb clnt, srv */ { LWS_CALLBACK_CLOSED_CLIENT_HTTP,
  997. LWS_CALLBACK_CLOSED_HTTP },
  998. /* protocol_bind cb c, srv */ { LWS_CALLBACK_CLIENT_HTTP_BIND_PROTOCOL,
  999. LWS_CALLBACK_HTTP_BIND_PROTOCOL },
  1000. /* protocol_unbind cb c, srv */ { LWS_CALLBACK_CLIENT_HTTP_DROP_PROTOCOL,
  1001. LWS_CALLBACK_HTTP_DROP_PROTOCOL },
  1002. /* file_handle */ 0,
  1003. };