client.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * libwebsockets - lib/client/client.c
  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. LWS_VISIBLE LWS_EXTERN void
  23. lws_client_http_body_pending(struct lws *wsi, int something_left_to_send)
  24. {
  25. wsi->client_http_body_pending = !!something_left_to_send;
  26. }
  27. /*
  28. * return self, or queued client wsi we are acting on behalf of
  29. *
  30. * That is the TAIL of the queue (new queue elements are added at the HEAD)
  31. */
  32. struct lws *
  33. lws_client_wsi_effective(struct lws *wsi)
  34. {
  35. struct lws_dll_lws *tail = NULL;
  36. if (!wsi->transaction_from_pipeline_queue ||
  37. !wsi->dll_client_transaction_queue_head.next)
  38. return wsi;
  39. lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
  40. wsi->dll_client_transaction_queue_head.next) {
  41. tail = d;
  42. } lws_end_foreach_dll_safe(d, d1);
  43. return lws_container_of(tail, struct lws,
  44. dll_client_transaction_queue);
  45. }
  46. /*
  47. * return self or the guy we are queued under
  48. *
  49. * REQUIRES VHOST LOCK HELD
  50. */
  51. static struct lws *
  52. _lws_client_wsi_master(struct lws *wsi)
  53. {
  54. struct lws *wsi_eff = wsi;
  55. struct lws_dll_lws *d;
  56. d = wsi->dll_client_transaction_queue.prev;
  57. while (d) {
  58. wsi_eff = lws_container_of(d, struct lws,
  59. dll_client_transaction_queue_head);
  60. d = d->prev;
  61. }
  62. return wsi_eff;
  63. }
  64. int
  65. lws_client_socket_service(struct lws *wsi, struct lws_pollfd *pollfd,
  66. struct lws *wsi_conn)
  67. {
  68. struct lws_context *context = wsi->context;
  69. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  70. char *p = (char *)&pt->serv_buf[0];
  71. struct lws *w;
  72. #if defined(LWS_WITH_TLS)
  73. char ebuf[128];
  74. #endif
  75. const char *cce = NULL;
  76. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  77. ssize_t len = 0;
  78. unsigned char c;
  79. #endif
  80. char *sb = p;
  81. int n = 0;
  82. #if defined(LWS_WITH_SOCKS5)
  83. int conn_mode = 0, pending_timeout = 0;
  84. #endif
  85. if ((pollfd->revents & LWS_POLLOUT) &&
  86. wsi->keepalive_active &&
  87. wsi->dll_client_transaction_queue_head.next) {
  88. struct lws *wfound = NULL;
  89. lwsl_debug("%s: pollout HANDSHAKE2\n", __func__);
  90. /*
  91. * We have a transaction queued that wants to pipeline.
  92. *
  93. * We have to allow it to send headers strictly in the order
  94. * that it was queued, ie, tail-first.
  95. */
  96. lws_vhost_lock(wsi->vhost);
  97. lws_start_foreach_dll_safe(struct lws_dll_lws *, d, d1,
  98. wsi->dll_client_transaction_queue_head.next) {
  99. struct lws *w = lws_container_of(d, struct lws,
  100. dll_client_transaction_queue);
  101. lwsl_debug("%s: %p states 0x%x\n", __func__, w,
  102. w->wsistate);
  103. if (lwsi_state(w) == LRS_H1C_ISSUE_HANDSHAKE2)
  104. wfound = w;
  105. } lws_end_foreach_dll_safe(d, d1);
  106. if (wfound) {
  107. /*
  108. * pollfd has the master sockfd in it... we
  109. * need to use that in HANDSHAKE2 to understand
  110. * which wsi to actually write on
  111. */
  112. lws_client_socket_service(wfound, pollfd, wsi);
  113. lws_callback_on_writable(wsi);
  114. } else
  115. lwsl_debug("%s: didn't find anything in txn q in HS2\n",
  116. __func__);
  117. lws_vhost_unlock(wsi->vhost);
  118. return 0;
  119. }
  120. switch (lwsi_state(wsi)) {
  121. case LRS_WAITING_CONNECT:
  122. /*
  123. * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
  124. * timeout protection set in client-handshake.c
  125. */
  126. if (!lws_client_connect_2(wsi)) {
  127. /* closed */
  128. lwsl_client("closed\n");
  129. return -1;
  130. }
  131. /* either still pending connection, or changed mode */
  132. return 0;
  133. #if defined(LWS_WITH_SOCKS5)
  134. /* SOCKS Greeting Reply */
  135. case LRS_WAITING_SOCKS_GREETING_REPLY:
  136. case LRS_WAITING_SOCKS_AUTH_REPLY:
  137. case LRS_WAITING_SOCKS_CONNECT_REPLY:
  138. /* handle proxy hung up on us */
  139. if (pollfd->revents & LWS_POLLHUP) {
  140. lwsl_warn("SOCKS connection %p (fd=%d) dead\n",
  141. (void *)wsi, pollfd->fd);
  142. goto bail3;
  143. }
  144. n = recv(wsi->desc.sockfd, sb, context->pt_serv_buf_size, 0);
  145. if (n < 0) {
  146. if (LWS_ERRNO == LWS_EAGAIN) {
  147. lwsl_debug("SOCKS read EAGAIN, retrying\n");
  148. return 0;
  149. }
  150. lwsl_err("ERROR reading from SOCKS socket\n");
  151. goto bail3;
  152. }
  153. switch (lwsi_state(wsi)) {
  154. case LRS_WAITING_SOCKS_GREETING_REPLY:
  155. if (pt->serv_buf[0] != SOCKS_VERSION_5)
  156. goto socks_reply_fail;
  157. if (pt->serv_buf[1] == SOCKS_AUTH_NO_AUTH) {
  158. lwsl_client("SOCKS GR: No Auth Method\n");
  159. socks_generate_msg(wsi, SOCKS_MSG_CONNECT, &len);
  160. conn_mode = LRS_WAITING_SOCKS_CONNECT_REPLY;
  161. pending_timeout =
  162. PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY;
  163. goto socks_send;
  164. }
  165. if (pt->serv_buf[1] == SOCKS_AUTH_USERNAME_PASSWORD) {
  166. lwsl_client("SOCKS GR: User/Pw Method\n");
  167. socks_generate_msg(wsi,
  168. SOCKS_MSG_USERNAME_PASSWORD,
  169. &len);
  170. conn_mode = LRS_WAITING_SOCKS_AUTH_REPLY;
  171. pending_timeout =
  172. PENDING_TIMEOUT_AWAITING_SOCKS_AUTH_REPLY;
  173. goto socks_send;
  174. }
  175. goto socks_reply_fail;
  176. case LRS_WAITING_SOCKS_AUTH_REPLY:
  177. if (pt->serv_buf[0] != SOCKS_SUBNEGOTIATION_VERSION_1 ||
  178. pt->serv_buf[1] !=
  179. SOCKS_SUBNEGOTIATION_STATUS_SUCCESS)
  180. goto socks_reply_fail;
  181. lwsl_client("SOCKS password OK, sending connect\n");
  182. socks_generate_msg(wsi, SOCKS_MSG_CONNECT, &len);
  183. conn_mode = LRS_WAITING_SOCKS_CONNECT_REPLY;
  184. pending_timeout =
  185. PENDING_TIMEOUT_AWAITING_SOCKS_CONNECT_REPLY;
  186. socks_send:
  187. n = send(wsi->desc.sockfd, (char *)pt->serv_buf, len,
  188. MSG_NOSIGNAL);
  189. if (n < 0) {
  190. lwsl_debug("ERROR writing to socks proxy\n");
  191. goto bail3;
  192. }
  193. lws_set_timeout(wsi, pending_timeout, AWAITING_TIMEOUT);
  194. lwsi_set_state(wsi, conn_mode);
  195. break;
  196. socks_reply_fail:
  197. lwsl_notice("socks reply: v%d, err %d\n",
  198. pt->serv_buf[0], pt->serv_buf[1]);
  199. goto bail3;
  200. case LRS_WAITING_SOCKS_CONNECT_REPLY:
  201. if (pt->serv_buf[0] != SOCKS_VERSION_5 ||
  202. pt->serv_buf[1] != SOCKS_REQUEST_REPLY_SUCCESS)
  203. goto socks_reply_fail;
  204. lwsl_client("socks connect OK\n");
  205. /* free stash since we are done with it */
  206. lws_client_stash_destroy(wsi);
  207. if (lws_hdr_simple_create(wsi,
  208. _WSI_TOKEN_CLIENT_PEER_ADDRESS,
  209. wsi->vhost->socks_proxy_address))
  210. goto bail3;
  211. wsi->c_port = wsi->vhost->socks_proxy_port;
  212. /* clear his proxy connection timeout */
  213. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  214. goto start_ws_handshake;
  215. default:
  216. break;
  217. }
  218. break;
  219. #endif
  220. case LRS_WAITING_PROXY_REPLY:
  221. /* handle proxy hung up on us */
  222. if (pollfd->revents & LWS_POLLHUP) {
  223. lwsl_warn("Proxy connection %p (fd=%d) dead\n",
  224. (void *)wsi, pollfd->fd);
  225. goto bail3;
  226. }
  227. n = recv(wsi->desc.sockfd, sb, context->pt_serv_buf_size, 0);
  228. if (n < 0) {
  229. if (LWS_ERRNO == LWS_EAGAIN) {
  230. lwsl_debug("Proxy read EAGAIN... retrying\n");
  231. return 0;
  232. }
  233. lwsl_err("ERROR reading from proxy socket\n");
  234. goto bail3;
  235. }
  236. pt->serv_buf[13] = '\0';
  237. if (strcmp(sb, "HTTP/1.0 200 ") &&
  238. strcmp(sb, "HTTP/1.1 200 ")) {
  239. lwsl_err("ERROR proxy: %s\n", sb);
  240. goto bail3;
  241. }
  242. /* clear his proxy connection timeout */
  243. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  244. /* fallthru */
  245. case LRS_H1C_ISSUE_HANDSHAKE:
  246. /*
  247. * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
  248. * timeout protection set in client-handshake.c
  249. *
  250. * take care of our lws_callback_on_writable
  251. * happening at a time when there's no real connection yet
  252. */
  253. #if defined(LWS_WITH_SOCKS5)
  254. start_ws_handshake:
  255. #endif
  256. if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
  257. return -1;
  258. #if defined(LWS_WITH_TLS)
  259. /* we can retry this... just cook the SSL BIO the first time */
  260. if ((wsi->tls.use_ssl & LCCSCF_USE_SSL) && !wsi->tls.ssl &&
  261. lws_ssl_client_bio_create(wsi) < 0) {
  262. cce = "bio_create failed";
  263. goto bail3;
  264. }
  265. if (wsi->tls.use_ssl & LCCSCF_USE_SSL) {
  266. n = lws_ssl_client_connect1(wsi);
  267. if (!n)
  268. return 0;
  269. if (n < 0) {
  270. cce = "lws_ssl_client_connect1 failed";
  271. goto bail3;
  272. }
  273. } else
  274. wsi->tls.ssl = NULL;
  275. /* fallthru */
  276. case LRS_WAITING_SSL:
  277. if (wsi->tls.use_ssl & LCCSCF_USE_SSL) {
  278. n = lws_ssl_client_connect2(wsi, ebuf, sizeof(ebuf));
  279. if (!n)
  280. return 0;
  281. if (n < 0) {
  282. cce = ebuf;
  283. goto bail3;
  284. }
  285. } else
  286. wsi->tls.ssl = NULL;
  287. #endif
  288. #if defined (LWS_WITH_HTTP2)
  289. if (wsi->client_h2_alpn) {
  290. /*
  291. * We connected to the server and set up tls, and
  292. * negotiated "h2".
  293. *
  294. * So this is it, we are an h2 master client connection
  295. * now, not an h1 client connection.
  296. */
  297. #if defined (LWS_WITH_TLS)
  298. lws_tls_server_conn_alpn(wsi);
  299. #endif
  300. /* send the H2 preface to legitimize the connection */
  301. if (lws_h2_issue_preface(wsi)) {
  302. cce = "error sending h2 preface";
  303. goto bail3;
  304. }
  305. break;
  306. }
  307. #endif
  308. lwsi_set_state(wsi, LRS_H1C_ISSUE_HANDSHAKE2);
  309. lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
  310. context->timeout_secs);
  311. /* fallthru */
  312. case LRS_H1C_ISSUE_HANDSHAKE2:
  313. p = lws_generate_client_handshake(wsi, p);
  314. if (p == NULL) {
  315. if (wsi->role_ops == &role_ops_raw_skt ||
  316. wsi->role_ops == &role_ops_raw_file)
  317. return 0;
  318. lwsl_err("Failed to generate handshake for client\n");
  319. lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
  320. "chs");
  321. return 0;
  322. }
  323. /* send our request to the server */
  324. lws_latency_pre(context, wsi);
  325. w = _lws_client_wsi_master(wsi);
  326. lwsl_info("%s: HANDSHAKE2: %p: sending headers on %p "
  327. "(wsistate 0x%x 0x%x)\n", __func__, wsi, w,
  328. wsi->wsistate, w->wsistate);
  329. n = lws_ssl_capable_write(w, (unsigned char *)sb, (int)(p - sb));
  330. lws_latency(context, wsi, "send lws_issue_raw", n,
  331. n == p - sb);
  332. switch (n) {
  333. case LWS_SSL_CAPABLE_ERROR:
  334. lwsl_debug("ERROR writing to client socket\n");
  335. lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
  336. "cws");
  337. return 0;
  338. case LWS_SSL_CAPABLE_MORE_SERVICE:
  339. lws_callback_on_writable(wsi);
  340. break;
  341. }
  342. if (wsi->client_http_body_pending) {
  343. lwsi_set_state(wsi, LRS_ISSUE_HTTP_BODY);
  344. lws_set_timeout(wsi,
  345. PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD,
  346. context->timeout_secs);
  347. /* user code must ask for writable callback */
  348. break;
  349. }
  350. lwsi_set_state(wsi, LRS_WAITING_SERVER_REPLY);
  351. wsi->hdr_parsing_completed = 0;
  352. if (lwsi_state(w) == LRS_IDLING) {
  353. lwsi_set_state(w, LRS_WAITING_SERVER_REPLY);
  354. w->hdr_parsing_completed = 0;
  355. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  356. w->http.ah->parser_state = WSI_TOKEN_NAME_PART;
  357. w->http.ah->lextable_pos = 0;
  358. /* If we're (re)starting on hdr, need other implied init */
  359. wsi->http.ah->ues = URIES_IDLE;
  360. #endif
  361. }
  362. lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
  363. wsi->context->timeout_secs);
  364. lws_callback_on_writable(w);
  365. goto client_http_body_sent;
  366. case LRS_ISSUE_HTTP_BODY:
  367. if (wsi->client_http_body_pending) {
  368. //lws_set_timeout(wsi,
  369. // PENDING_TIMEOUT_CLIENT_ISSUE_PAYLOAD,
  370. // context->timeout_secs);
  371. /* user code must ask for writable callback */
  372. break;
  373. }
  374. client_http_body_sent:
  375. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  376. /* prepare ourselves to do the parsing */
  377. wsi->http.ah->parser_state = WSI_TOKEN_NAME_PART;
  378. wsi->http.ah->lextable_pos = 0;
  379. #endif
  380. lwsi_set_state(wsi, LRS_WAITING_SERVER_REPLY);
  381. lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
  382. context->timeout_secs);
  383. break;
  384. case LRS_WAITING_SERVER_REPLY:
  385. /*
  386. * handle server hanging up on us...
  387. * but if there is POLLIN waiting, handle that first
  388. */
  389. if ((pollfd->revents & (LWS_POLLIN | LWS_POLLHUP)) ==
  390. LWS_POLLHUP) {
  391. lwsl_debug("Server connection %p (fd=%d) dead\n",
  392. (void *)wsi, pollfd->fd);
  393. cce = "Peer hung up";
  394. goto bail3;
  395. }
  396. if (!(pollfd->revents & LWS_POLLIN))
  397. break;
  398. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  399. /* interpret the server response
  400. *
  401. * HTTP/1.1 101 Switching Protocols
  402. * Upgrade: websocket
  403. * Connection: Upgrade
  404. * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
  405. * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
  406. * Sec-WebSocket-Protocol: chat
  407. *
  408. * we have to take some care here to only take from the
  409. * socket bytewise. The browser may (and has been seen to
  410. * in the case that onopen() performs websocket traffic)
  411. * coalesce both handshake response and websocket traffic
  412. * in one packet, since at that point the connection is
  413. * definitively ready from browser pov.
  414. */
  415. len = 1;
  416. while (wsi->http.ah->parser_state != WSI_PARSING_COMPLETE &&
  417. len > 0) {
  418. int plen = 1;
  419. n = lws_ssl_capable_read(wsi, &c, 1);
  420. lws_latency(context, wsi, "send lws_issue_raw", n,
  421. n == 1);
  422. switch (n) {
  423. case 0:
  424. case LWS_SSL_CAPABLE_ERROR:
  425. cce = "read failed";
  426. goto bail3;
  427. case LWS_SSL_CAPABLE_MORE_SERVICE:
  428. return 0;
  429. }
  430. if (lws_parse(wsi, &c, &plen)) {
  431. lwsl_warn("problems parsing header\n");
  432. goto bail3;
  433. }
  434. }
  435. /*
  436. * hs may also be coming in multiple packets, there is a 5-sec
  437. * libwebsocket timeout still active here too, so if parsing did
  438. * not complete just wait for next packet coming in this state
  439. */
  440. if (wsi->http.ah->parser_state != WSI_PARSING_COMPLETE)
  441. break;
  442. #endif
  443. /*
  444. * otherwise deal with the handshake. If there's any
  445. * packet traffic already arrived we'll trigger poll() again
  446. * right away and deal with it that way
  447. */
  448. return lws_client_interpret_server_handshake(wsi);
  449. bail3:
  450. lwsl_info("closing conn at LWS_CONNMODE...SERVER_REPLY\n");
  451. if (cce)
  452. lwsl_info("reason: %s\n", cce);
  453. wsi->protocol->callback(wsi,
  454. LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
  455. wsi->user_space, (void *)cce, cce ? strlen(cce) : 0);
  456. wsi->already_did_cce = 1;
  457. lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "cbail3");
  458. return -1;
  459. default:
  460. break;
  461. }
  462. return 0;
  463. }
  464. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  465. int LWS_WARN_UNUSED_RESULT
  466. lws_http_transaction_completed_client(struct lws *wsi)
  467. {
  468. struct lws *wsi_eff = lws_client_wsi_effective(wsi);
  469. lwsl_info("%s: wsi: %p, wsi_eff: %p (%s)\n", __func__, wsi, wsi_eff,
  470. wsi_eff->protocol->name);
  471. if (user_callback_handle_rxflow(wsi_eff->protocol->callback, wsi_eff,
  472. LWS_CALLBACK_COMPLETED_CLIENT_HTTP,
  473. wsi_eff->user_space, NULL, 0)) {
  474. lwsl_debug("%s: Completed call returned nonzero (role 0x%x)\n",
  475. __func__, lwsi_role(wsi_eff));
  476. return -1;
  477. }
  478. /*
  479. * Are we constitutionally capable of having a queue, ie, we are on
  480. * the "active client connections" list?
  481. *
  482. * If not, that's it for us.
  483. */
  484. if (lws_dll_is_null(&wsi->dll_active_client_conns))
  485. return -1;
  486. /* if this was a queued guy, close him and remove from queue */
  487. if (wsi->transaction_from_pipeline_queue) {
  488. lwsl_debug("closing queued wsi %p\n", wsi_eff);
  489. /* so the close doesn't trigger a CCE */
  490. wsi_eff->already_did_cce = 1;
  491. __lws_close_free_wsi(wsi_eff,
  492. LWS_CLOSE_STATUS_CLIENT_TRANSACTION_DONE,
  493. "queued client done");
  494. }
  495. _lws_header_table_reset(wsi->http.ah);
  496. /* after the first one, they can only be coming from the queue */
  497. wsi->transaction_from_pipeline_queue = 1;
  498. wsi->http.rx_content_length = 0;
  499. wsi->hdr_parsing_completed = 0;
  500. /* is there a new tail after removing that one? */
  501. wsi_eff = lws_client_wsi_effective(wsi);
  502. /*
  503. * Do we have something pipelined waiting?
  504. * it's OK if he hasn't managed to send his headers yet... he's next
  505. * in line to do that...
  506. */
  507. if (wsi_eff == wsi) {
  508. /*
  509. * Nothing pipelined... we should hang around a bit
  510. * in case something turns up...
  511. */
  512. lwsl_info("%s: nothing pipelined waiting\n", __func__);
  513. lwsi_set_state(wsi, LRS_IDLING);
  514. lws_set_timeout(wsi, PENDING_TIMEOUT_CLIENT_CONN_IDLE, 5);
  515. return 0;
  516. }
  517. /*
  518. * H1: we can serialize the queued guys into the same ah
  519. * H2: everybody needs their own ah until their own STREAM_END
  520. */
  521. /* otherwise set ourselves up ready to go again */
  522. lwsi_set_state(wsi, LRS_WAITING_SERVER_REPLY);
  523. wsi->http.ah->parser_state = WSI_TOKEN_NAME_PART;
  524. wsi->http.ah->lextable_pos = 0;
  525. lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
  526. wsi->context->timeout_secs);
  527. /* If we're (re)starting on headers, need other implied init */
  528. wsi->http.ah->ues = URIES_IDLE;
  529. lwsl_info("%s: %p: new queued transaction as %p\n", __func__, wsi,
  530. wsi_eff);
  531. lws_callback_on_writable(wsi);
  532. return 0;
  533. }
  534. LWS_VISIBLE LWS_EXTERN unsigned int
  535. lws_http_client_http_response(struct lws *_wsi)
  536. {
  537. struct lws *wsi;
  538. unsigned int resp;
  539. if (_wsi->http.ah && _wsi->http.ah->http_response)
  540. return _wsi->http.ah->http_response;
  541. lws_vhost_lock(_wsi->vhost);
  542. wsi = _lws_client_wsi_master(_wsi);
  543. resp = wsi->http.ah->http_response;
  544. lws_vhost_unlock(_wsi->vhost);
  545. return resp;
  546. }
  547. #endif
  548. #if defined(LWS_PLAT_OPTEE)
  549. char *
  550. strrchr(const char *s, int c)
  551. {
  552. char *hit = NULL;
  553. while (*s)
  554. if (*(s++) == (char)c)
  555. hit = (char *)s - 1;
  556. return hit;
  557. }
  558. #define atoll atoi
  559. #endif
  560. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  561. int
  562. lws_client_interpret_server_handshake(struct lws *wsi)
  563. {
  564. int n, port = 0, ssl = 0;
  565. int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
  566. const char *prot, *ads = NULL, *path, *cce = NULL;
  567. struct allocated_headers *ah;
  568. struct lws *w = lws_client_wsi_effective(wsi);
  569. char *p, *q;
  570. char new_path[300];
  571. lws_client_stash_destroy(wsi);
  572. ah = wsi->http.ah;
  573. if (!wsi->do_ws) {
  574. /* we are being an http client...
  575. */
  576. #if defined(LWS_ROLE_H2)
  577. if (wsi->client_h2_alpn || wsi->client_h2_substream) {
  578. lwsl_debug("%s: %p: transitioning to h2 client\n",
  579. __func__, wsi);
  580. lws_role_transition(wsi, LWSIFR_CLIENT,
  581. LRS_ESTABLISHED, &role_ops_h2);
  582. } else
  583. #endif
  584. {
  585. #if defined(LWS_ROLE_H1)
  586. {
  587. lwsl_debug("%s: %p: transitioning to h1 client\n",
  588. __func__, wsi);
  589. lws_role_transition(wsi, LWSIFR_CLIENT,
  590. LRS_ESTABLISHED, &role_ops_h1);
  591. }
  592. #else
  593. return -1;
  594. #endif
  595. }
  596. wsi->http.ah = ah;
  597. ah->http_response = 0;
  598. }
  599. /*
  600. * well, what the server sent looked reasonable for syntax.
  601. * Now let's confirm it sent all the necessary headers
  602. *
  603. * http (non-ws) client will expect something like this
  604. *
  605. * HTTP/1.0.200
  606. * server:.libwebsockets
  607. * content-type:.text/html
  608. * content-length:.17703
  609. * set-cookie:.test=LWS_1456736240_336776_COOKIE;Max-Age=360000
  610. */
  611. wsi->http.conn_type = HTTP_CONNECTION_KEEP_ALIVE;
  612. if (!wsi->client_h2_substream) {
  613. p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
  614. if (wsi->do_ws && !p) {
  615. lwsl_info("no URI\n");
  616. cce = "HS: URI missing";
  617. goto bail3;
  618. }
  619. if (!p) {
  620. p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP1_0);
  621. wsi->http.conn_type = HTTP_CONNECTION_CLOSE;
  622. }
  623. if (!p) {
  624. cce = "HS: URI missing";
  625. lwsl_info("no URI\n");
  626. goto bail3;
  627. }
  628. } else {
  629. p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_STATUS);
  630. if (!p) {
  631. cce = "HS: :status missing";
  632. lwsl_info("no status\n");
  633. goto bail3;
  634. }
  635. }
  636. n = atoi(p);
  637. if (ah)
  638. ah->http_response = n;
  639. if (n == 301 || n == 302 || n == 303 || n == 307 || n == 308) {
  640. p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_LOCATION);
  641. if (!p) {
  642. cce = "HS: Redirect code but no Location";
  643. goto bail3;
  644. }
  645. /* Relative reference absolute path */
  646. if (p[0] == '/') {
  647. #if defined(LWS_WITH_TLS)
  648. ssl = wsi->tls.use_ssl & LCCSCF_USE_SSL;
  649. #endif
  650. ads = lws_hdr_simple_ptr(wsi,
  651. _WSI_TOKEN_CLIENT_PEER_ADDRESS);
  652. port = wsi->c_port;
  653. /* +1 as lws_client_reset expects leading / omitted */
  654. path = p + 1;
  655. }
  656. /* Absolute (Full) URI */
  657. else if (strchr(p, ':')) {
  658. if (lws_parse_uri(p, &prot, &ads, &port, &path)) {
  659. cce = "HS: URI did not parse";
  660. goto bail3;
  661. }
  662. if (!strcmp(prot, "wss") || !strcmp(prot, "https"))
  663. ssl = 1;
  664. }
  665. /* Relative reference relative path */
  666. else {
  667. /* This doesn't try to calculate an absolute path,
  668. * that will be left to the server */
  669. #if defined(LWS_WITH_TLS)
  670. ssl = wsi->tls.use_ssl & LCCSCF_USE_SSL;
  671. #endif
  672. ads = lws_hdr_simple_ptr(wsi,
  673. _WSI_TOKEN_CLIENT_PEER_ADDRESS);
  674. port = wsi->c_port;
  675. /* +1 as lws_client_reset expects leading / omitted */
  676. path = new_path + 1;
  677. lws_strncpy(new_path, lws_hdr_simple_ptr(wsi,
  678. _WSI_TOKEN_CLIENT_URI), sizeof(new_path));
  679. q = strrchr(new_path, '/');
  680. if (q)
  681. lws_strncpy(q + 1, p, sizeof(new_path) -
  682. (q - new_path) - 1);
  683. else
  684. path = p;
  685. }
  686. #if defined(LWS_WITH_TLS)
  687. if ((wsi->tls.use_ssl & LCCSCF_USE_SSL) && !ssl) {
  688. cce = "HS: Redirect attempted SSL downgrade";
  689. goto bail3;
  690. }
  691. #endif
  692. if (!lws_client_reset(&wsi, ssl, ads, port, path, ads)) {
  693. /* there are two ways to fail out with NULL return...
  694. * simple, early problem where the wsi is intact, or
  695. * we went through with the reconnect attempt and the
  696. * wsi is already closed. In the latter case, the wsi
  697. * has beet set to NULL additionally.
  698. */
  699. lwsl_err("Redirect failed\n");
  700. cce = "HS: Redirect failed";
  701. if (wsi)
  702. goto bail3;
  703. return 1;
  704. }
  705. return 0;
  706. }
  707. if (!wsi->do_ws) {
  708. /* if h1 KA is allowed, enable the queued pipeline guys */
  709. if (!wsi->client_h2_alpn && !wsi->client_h2_substream &&
  710. w == wsi) { /* ie, coming to this for the first time */
  711. if (wsi->http.conn_type == HTTP_CONNECTION_KEEP_ALIVE)
  712. wsi->keepalive_active = 1;
  713. else {
  714. /*
  715. * Ugh... now the main http connection has seen
  716. * both sides, we learn the server doesn't
  717. * support keepalive.
  718. *
  719. * That means any guys queued on us are going
  720. * to have to be restarted from connect2 with
  721. * their own connections.
  722. */
  723. /*
  724. * stick around telling any new guys they can't
  725. * pipeline to this server
  726. */
  727. wsi->keepalive_rejected = 1;
  728. lws_vhost_lock(wsi->vhost);
  729. lws_start_foreach_dll_safe(struct lws_dll_lws *,
  730. d, d1,
  731. wsi->dll_client_transaction_queue_head.next) {
  732. struct lws *ww = lws_container_of(d,
  733. struct lws,
  734. dll_client_transaction_queue);
  735. /* remove him from our queue */
  736. lws_dll_lws_remove(
  737. &ww->dll_client_transaction_queue);
  738. /* give up on pipelining */
  739. ww->client_pipeline = 0;
  740. /* go back to "trying to connect" state */
  741. lws_role_transition(ww, LWSIFR_CLIENT,
  742. LRS_UNCONNECTED,
  743. #if defined(LWS_ROLE_H1)
  744. &role_ops_h1);
  745. #else
  746. #if defined (LWS_ROLE_H2)
  747. &role_ops_h2);
  748. #else
  749. &role_ops_raw);
  750. #endif
  751. #endif
  752. ww->user_space = NULL;
  753. } lws_end_foreach_dll_safe(d, d1);
  754. lws_vhost_unlock(wsi->vhost);
  755. }
  756. }
  757. #ifdef LWS_WITH_HTTP_PROXY
  758. wsi->http.perform_rewrite = 0;
  759. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE)) {
  760. if (!strncmp(lws_hdr_simple_ptr(wsi,
  761. WSI_TOKEN_HTTP_CONTENT_TYPE),
  762. "text/html", 9))
  763. wsi->http.perform_rewrite = 0;
  764. }
  765. #endif
  766. /* allocate the per-connection user memory (if any) */
  767. if (lws_ensure_user_space(wsi)) {
  768. lwsl_err("Problem allocating wsi user mem\n");
  769. cce = "HS: OOM";
  770. goto bail2;
  771. }
  772. /* he may choose to send us stuff in chunked transfer-coding */
  773. wsi->chunked = 0;
  774. wsi->chunk_remaining = 0; /* ie, next thing is chunk size */
  775. if (lws_hdr_total_length(wsi,
  776. WSI_TOKEN_HTTP_TRANSFER_ENCODING)) {
  777. wsi->chunked = !strcmp(lws_hdr_simple_ptr(wsi,
  778. WSI_TOKEN_HTTP_TRANSFER_ENCODING),
  779. "chunked");
  780. /* first thing is hex, after payload there is crlf */
  781. wsi->chunk_parser = ELCP_HEX;
  782. }
  783. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
  784. wsi->http.rx_content_length =
  785. atoll(lws_hdr_simple_ptr(wsi,
  786. WSI_TOKEN_HTTP_CONTENT_LENGTH));
  787. lwsl_info("%s: incoming content length %llu\n",
  788. __func__, (unsigned long long)
  789. wsi->http.rx_content_length);
  790. wsi->http.rx_content_remain =
  791. wsi->http.rx_content_length;
  792. } else /* can't do 1.1 without a content length or chunked */
  793. if (!wsi->chunked)
  794. wsi->http.conn_type = HTTP_CONNECTION_CLOSE;
  795. /*
  796. * we seem to be good to go, give client last chance to check
  797. * headers and OK it
  798. */
  799. if (w->protocol->callback(w,
  800. LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
  801. w->user_space, NULL, 0)) {
  802. cce = "HS: disallowed by client filter";
  803. goto bail2;
  804. }
  805. /* clear his proxy connection timeout */
  806. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  807. wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
  808. /* call him back to inform him he is up */
  809. if (w->protocol->callback(w,
  810. LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP,
  811. w->user_space, NULL, 0)) {
  812. cce = "HS: disallowed at ESTABLISHED";
  813. goto bail3;
  814. }
  815. /*
  816. * for pipelining, master needs to keep his ah... guys who
  817. * queued on him can drop it now though.
  818. */
  819. if (w != wsi)
  820. /* free up parsing allocations for queued guy */
  821. lws_header_table_detach(w, 0);
  822. lwsl_info("%s: client connection up\n", __func__);
  823. /*
  824. * Did we get a response from the server with an explicit
  825. * content-length of zero? If so, this transaction is already
  826. * completed at the end of the header processing...
  827. */
  828. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH) &&
  829. !wsi->http.rx_content_length)
  830. return !!lws_http_transaction_completed_client(wsi);
  831. return 0;
  832. }
  833. #if defined(LWS_ROLE_WS)
  834. switch (lws_client_ws_upgrade(wsi, &cce)) {
  835. case 2:
  836. goto bail2;
  837. case 3:
  838. goto bail3;
  839. }
  840. return 0;
  841. #endif
  842. bail3:
  843. close_reason = LWS_CLOSE_STATUS_NOSTATUS;
  844. bail2:
  845. if (wsi->protocol) {
  846. n = 0;
  847. if (cce)
  848. n = (int)strlen(cce);
  849. w->protocol->callback(w,
  850. LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
  851. w->user_space, (void *)cce,
  852. (unsigned int)n);
  853. }
  854. wsi->already_did_cce = 1;
  855. lwsl_info("closing connection (prot %s) "
  856. "due to bail2 connection error: %s\n", wsi->protocol ?
  857. wsi->protocol->name : "unknown", cce);
  858. /* closing will free up his parsing allocations */
  859. lws_close_free_wsi(wsi, close_reason, "c hs interp");
  860. return 1;
  861. }
  862. #endif
  863. char *
  864. lws_generate_client_handshake(struct lws *wsi, char *pkt)
  865. {
  866. char *p = pkt;
  867. const char *meth;
  868. const char *pp = lws_hdr_simple_ptr(wsi,
  869. _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
  870. meth = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_METHOD);
  871. if (!meth) {
  872. meth = "GET";
  873. wsi->do_ws = 1;
  874. } else {
  875. wsi->do_ws = 0;
  876. }
  877. if (!strcmp(meth, "RAW")) {
  878. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  879. lwsl_notice("client transition to raw\n");
  880. if (pp) {
  881. const struct lws_protocols *pr;
  882. pr = lws_vhost_name_to_protocol(wsi->vhost, pp);
  883. if (!pr) {
  884. lwsl_err("protocol %s not enabled on vhost\n",
  885. pp);
  886. return NULL;
  887. }
  888. lws_bind_protocol(wsi, pr, __func__);
  889. }
  890. if ((wsi->protocol->callback)(wsi, LWS_CALLBACK_RAW_ADOPT,
  891. wsi->user_space, NULL, 0))
  892. return NULL;
  893. lws_role_transition(wsi, 0, LRS_ESTABLISHED, &role_ops_raw_skt);
  894. lws_header_table_detach(wsi, 1);
  895. return NULL;
  896. }
  897. /*
  898. * 04 example client handshake
  899. *
  900. * GET /chat HTTP/1.1
  901. * Host: server.example.com
  902. * Upgrade: websocket
  903. * Connection: Upgrade
  904. * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
  905. * Sec-WebSocket-Origin: http://example.com
  906. * Sec-WebSocket-Protocol: chat, superchat
  907. * Sec-WebSocket-Version: 4
  908. */
  909. p += sprintf(p, "%s %s HTTP/1.1\x0d\x0a", meth,
  910. lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
  911. p += sprintf(p, "Pragma: no-cache\x0d\x0a"
  912. "Cache-Control: no-cache\x0d\x0a");
  913. p += sprintf(p, "Host: %s\x0d\x0a",
  914. lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
  915. if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN)) {
  916. if (lws_check_opt(wsi->context->options,
  917. LWS_SERVER_OPTION_JUST_USE_RAW_ORIGIN))
  918. p += sprintf(p, "Origin: %s\x0d\x0a",
  919. lws_hdr_simple_ptr(wsi,
  920. _WSI_TOKEN_CLIENT_ORIGIN));
  921. else
  922. p += sprintf(p, "Origin: http://%s\x0d\x0a",
  923. lws_hdr_simple_ptr(wsi,
  924. _WSI_TOKEN_CLIENT_ORIGIN));
  925. }
  926. #if defined(LWS_ROLE_WS)
  927. if (wsi->do_ws) {
  928. const char *conn1 = "";
  929. if (!wsi->client_pipeline)
  930. conn1 = "close, ";
  931. p = lws_generate_client_ws_handshake(wsi, p, conn1);
  932. } else
  933. #endif
  934. if (!wsi->client_pipeline)
  935. p += sprintf(p, "connection: close\x0d\x0a");
  936. /* give userland a chance to append, eg, cookies */
  937. if (wsi->protocol->callback(wsi,
  938. LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
  939. wsi->user_space, &p,
  940. (pkt + wsi->context->pt_serv_buf_size) - p - 12))
  941. return NULL;
  942. p += sprintf(p, "\x0d\x0a");
  943. return p;
  944. }
  945. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  946. LWS_VISIBLE int
  947. lws_http_client_read(struct lws *wsi, char **buf, int *len)
  948. {
  949. int rlen, n;
  950. rlen = lws_ssl_capable_read(wsi, (unsigned char *)*buf, *len);
  951. *len = 0;
  952. // lwsl_notice("%s: rlen %d\n", __func__, rlen);
  953. /* allow the source to signal he has data again next time */
  954. lws_change_pollfd(wsi, 0, LWS_POLLIN);
  955. if (rlen == LWS_SSL_CAPABLE_ERROR) {
  956. lwsl_debug("%s: SSL capable error\n", __func__);
  957. return -1;
  958. }
  959. if (rlen <= 0)
  960. return 0;
  961. *len = rlen;
  962. wsi->client_rx_avail = 0;
  963. /*
  964. * server may insist on transfer-encoding: chunked,
  965. * so http client must deal with it
  966. */
  967. spin_chunks:
  968. while (wsi->chunked && (wsi->chunk_parser != ELCP_CONTENT) && *len) {
  969. switch (wsi->chunk_parser) {
  970. case ELCP_HEX:
  971. if ((*buf)[0] == '\x0d') {
  972. wsi->chunk_parser = ELCP_CR;
  973. break;
  974. }
  975. n = char_to_hex((*buf)[0]);
  976. if (n < 0) {
  977. lwsl_info("%s: chunking failure\n", __func__);
  978. return -1;
  979. }
  980. wsi->chunk_remaining <<= 4;
  981. wsi->chunk_remaining |= n;
  982. break;
  983. case ELCP_CR:
  984. if ((*buf)[0] != '\x0a') {
  985. lwsl_info("%s: chunking failure\n", __func__);
  986. return -1;
  987. }
  988. wsi->chunk_parser = ELCP_CONTENT;
  989. lwsl_info("chunk %d\n", wsi->chunk_remaining);
  990. if (wsi->chunk_remaining)
  991. break;
  992. lwsl_info("final chunk\n");
  993. goto completed;
  994. case ELCP_CONTENT:
  995. break;
  996. case ELCP_POST_CR:
  997. if ((*buf)[0] != '\x0d') {
  998. lwsl_info("%s: chunking failure\n", __func__);
  999. return -1;
  1000. }
  1001. wsi->chunk_parser = ELCP_POST_LF;
  1002. break;
  1003. case ELCP_POST_LF:
  1004. if ((*buf)[0] != '\x0a') {
  1005. lwsl_info("%s: chunking failure\n", __func__);
  1006. return -1;
  1007. }
  1008. wsi->chunk_parser = ELCP_HEX;
  1009. wsi->chunk_remaining = 0;
  1010. break;
  1011. }
  1012. (*buf)++;
  1013. (*len)--;
  1014. }
  1015. if (wsi->chunked && !wsi->chunk_remaining)
  1016. return 0;
  1017. if (wsi->http.rx_content_remain &&
  1018. wsi->http.rx_content_remain < (unsigned int)*len)
  1019. n = (int)wsi->http.rx_content_remain;
  1020. else
  1021. n = *len;
  1022. if (wsi->chunked && wsi->chunk_remaining &&
  1023. wsi->chunk_remaining < n)
  1024. n = wsi->chunk_remaining;
  1025. #if defined(LWS_WITH_HTTP_PROXY) && defined(LWS_WITH_HUBBUB)
  1026. /* hubbub */
  1027. if (wsi->http.perform_rewrite)
  1028. lws_rewrite_parse(wsi->http.rw, (unsigned char *)*buf, n);
  1029. else
  1030. #endif
  1031. {
  1032. struct lws *wsi_eff = lws_client_wsi_effective(wsi);
  1033. if (user_callback_handle_rxflow(wsi_eff->protocol->callback,
  1034. wsi_eff, LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
  1035. wsi_eff->user_space, *buf, n)) {
  1036. lwsl_info("%s: RECEIVE_CLIENT_HTTP_READ returned -1\n",
  1037. __func__);
  1038. return -1;
  1039. }
  1040. }
  1041. if (wsi->chunked && wsi->chunk_remaining) {
  1042. (*buf) += n;
  1043. wsi->chunk_remaining -= n;
  1044. *len -= n;
  1045. }
  1046. if (wsi->chunked && !wsi->chunk_remaining)
  1047. wsi->chunk_parser = ELCP_POST_CR;
  1048. if (wsi->chunked && *len)
  1049. goto spin_chunks;
  1050. if (wsi->chunked)
  1051. return 0;
  1052. /* if we know the content length, decrement the content remaining */
  1053. if (wsi->http.rx_content_length > 0)
  1054. wsi->http.rx_content_remain -= n;
  1055. // lwsl_notice("rx_content_remain %lld, rx_content_length %lld\n",
  1056. // wsi->http.rx_content_remain, wsi->http.rx_content_length);
  1057. if (wsi->http.rx_content_remain || !wsi->http.rx_content_length)
  1058. return 0;
  1059. completed:
  1060. if (lws_http_transaction_completed_client(wsi)) {
  1061. lwsl_notice("%s: transaction completed says -1\n", __func__);
  1062. return -1;
  1063. }
  1064. return 0;
  1065. }
  1066. #endif