ops-h1.c 26 KB

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