output.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #include "private-libwebsockets.h"
  22. static int
  23. lws_0405_frame_mask_generate(struct lws *wsi)
  24. {
  25. #if 0
  26. wsi->u.ws.mask[0] = 0;
  27. wsi->u.ws.mask[1] = 0;
  28. wsi->u.ws.mask[2] = 0;
  29. wsi->u.ws.mask[3] = 0;
  30. #else
  31. int n;
  32. /* fetch the per-frame nonce */
  33. n = lws_get_random(lws_get_context(wsi), wsi->u.ws.mask, 4);
  34. if (n != 4) {
  35. lwsl_parser("Unable to read from random device %s %d\n",
  36. SYSTEM_RANDOM_FILEPATH, n);
  37. return 1;
  38. }
  39. #endif
  40. /* start masking from first byte of masking key buffer */
  41. wsi->u.ws.mask_idx = 0;
  42. return 0;
  43. }
  44. #ifdef _DEBUG
  45. LWS_VISIBLE void lwsl_hexdump(void *vbuf, size_t len)
  46. {
  47. unsigned char *buf = (unsigned char *)vbuf;
  48. unsigned int n, m, start;
  49. char line[80];
  50. char *p;
  51. lwsl_parser("\n");
  52. for (n = 0; n < len;) {
  53. start = n;
  54. p = line;
  55. p += sprintf(p, "%04X: ", start);
  56. for (m = 0; m < 16 && n < len; m++)
  57. p += sprintf(p, "%02X ", buf[n++]);
  58. while (m++ < 16)
  59. p += sprintf(p, " ");
  60. p += sprintf(p, " ");
  61. for (m = 0; m < 16 && (start + m) < len; m++) {
  62. if (buf[start + m] >= ' ' && buf[start + m] < 127)
  63. *p++ = buf[start + m];
  64. else
  65. *p++ = '.';
  66. }
  67. while (m++ < 16)
  68. *p++ = ' ';
  69. *p++ = '\n';
  70. *p = '\0';
  71. lwsl_debug("%s", line);
  72. }
  73. lwsl_debug("\n");
  74. }
  75. #endif
  76. /*
  77. * notice this returns number of bytes consumed, or -1
  78. */
  79. int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
  80. {
  81. struct lws_context *context = lws_get_context(wsi);
  82. size_t real_len = len;
  83. unsigned int n;
  84. int m;
  85. if (!len)
  86. return 0;
  87. /* just ignore sends after we cleared the truncation buffer */
  88. if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
  89. !wsi->trunc_len)
  90. return len;
  91. if (wsi->trunc_len && (buf < wsi->trunc_alloc ||
  92. buf > (wsi->trunc_alloc + wsi->trunc_len + wsi->trunc_offset))) {
  93. char dump[20];
  94. strncpy(dump, (char *)buf, sizeof(dump) - 1);
  95. dump[sizeof(dump) - 1] = '\0';
  96. #if defined(LWS_WITH_ESP8266)
  97. lwsl_err("****** %p: Sending new %d (%s), pending truncated ...\n",
  98. wsi, len, dump);
  99. #else
  100. lwsl_err("****** %p: Sending new %d (%s), pending truncated ...\n"
  101. " It's illegal to do an lws_write outside of\n"
  102. " the writable callback: fix your code",
  103. wsi, len, dump);
  104. #endif
  105. assert(0);
  106. return -1;
  107. }
  108. m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_TX_DO_SEND, &buf, len);
  109. if (m < 0)
  110. return -1;
  111. if (m) /* handled */ {
  112. n = m;
  113. goto handle_truncated_send;
  114. }
  115. if (!lws_socket_is_valid(wsi->sock))
  116. lwsl_warn("** error invalid sock but expected to send\n");
  117. /* limit sending */
  118. n = wsi->protocol->rx_buffer_size;
  119. if (!n)
  120. n = context->pt_serv_buf_size;
  121. n += LWS_PRE + 4;
  122. if (n > len)
  123. n = len;
  124. #if defined(LWS_WITH_ESP8266)
  125. if (wsi->pending_send_completion) {
  126. n = 0;
  127. goto handle_truncated_send;
  128. }
  129. #endif
  130. /* nope, send it on the socket directly */
  131. lws_latency_pre(context, wsi);
  132. n = lws_ssl_capable_write(wsi, buf, n);
  133. lws_latency(context, wsi, "send lws_issue_raw", n, n == len);
  134. switch (n) {
  135. case LWS_SSL_CAPABLE_ERROR:
  136. /* we're going to close, let close know sends aren't possible */
  137. wsi->socket_is_permanently_unusable = 1;
  138. return -1;
  139. case LWS_SSL_CAPABLE_MORE_SERVICE:
  140. /* nothing got sent, not fatal, retry the whole thing later */
  141. n = 0;
  142. break;
  143. }
  144. handle_truncated_send:
  145. /*
  146. * we were already handling a truncated send?
  147. */
  148. if (wsi->trunc_len) {
  149. lwsl_info("%p partial adv %d (vs %d)\n", wsi, n, real_len);
  150. wsi->trunc_offset += n;
  151. wsi->trunc_len -= n;
  152. if (!wsi->trunc_len) {
  153. lwsl_info("***** %x partial send completed\n", wsi);
  154. /* done with it, but don't free it */
  155. n = real_len;
  156. if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
  157. lwsl_info("***** %x signalling to close now\n", wsi);
  158. return -1; /* retry closing now */
  159. }
  160. }
  161. /* always callback on writeable */
  162. lws_callback_on_writable(wsi);
  163. return n;
  164. }
  165. if ((unsigned int)n == real_len)
  166. /* what we just sent went out cleanly */
  167. return n;
  168. /*
  169. * Newly truncated send. Buffer the remainder (it will get
  170. * first priority next time the socket is writable)
  171. */
  172. lwsl_notice("%p new partial sent %d from %d total\n", wsi, n, real_len);
  173. /*
  174. * - if we still have a suitable malloc lying around, use it
  175. * - or, if too small, reallocate it
  176. * - or, if no buffer, create it
  177. */
  178. if (!wsi->trunc_alloc || real_len - n > wsi->trunc_alloc_len) {
  179. lws_free(wsi->trunc_alloc);
  180. wsi->trunc_alloc_len = real_len - n;
  181. wsi->trunc_alloc = lws_malloc(real_len - n);
  182. if (!wsi->trunc_alloc) {
  183. lwsl_err("truncated send: unable to malloc %d\n",
  184. real_len - n);
  185. return -1;
  186. }
  187. }
  188. wsi->trunc_offset = 0;
  189. wsi->trunc_len = real_len - n;
  190. memcpy(wsi->trunc_alloc, buf + n, real_len - n);
  191. /* since something buffered, force it to get another chance to send */
  192. lws_callback_on_writable(wsi);
  193. return real_len;
  194. }
  195. LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
  196. enum lws_write_protocol wp)
  197. {
  198. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  199. int masked7 = (wsi->mode == LWSCM_WS_CLIENT);
  200. unsigned char is_masked_bit = 0;
  201. unsigned char *dropmask = NULL;
  202. struct lws_tokens eff_buf;
  203. int pre = 0, n;
  204. size_t orig_len = len;
  205. #ifdef LWS_WITH_ACCESS_LOG
  206. wsi->access_log.sent += len;
  207. #endif
  208. if (wsi->vhost)
  209. wsi->vhost->tx += len;
  210. if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
  211. /* remove us from the list */
  212. struct lws **w = &pt->tx_draining_ext_list;
  213. lwsl_debug("%s: TX EXT DRAINING: Remove from list\n", __func__);
  214. wsi->u.ws.tx_draining_ext = 0;
  215. /* remove us from context draining ext list */
  216. while (*w) {
  217. if (*w == wsi) {
  218. *w = wsi->u.ws.tx_draining_ext_list;
  219. break;
  220. }
  221. w = &((*w)->u.ws.tx_draining_ext_list);
  222. }
  223. wsi->u.ws.tx_draining_ext_list = NULL;
  224. wp = (wsi->u.ws.tx_draining_stashed_wp & 0xc0) |
  225. LWS_WRITE_CONTINUATION;
  226. lwsl_ext("FORCED draining wp to 0x%02X\n", wp);
  227. }
  228. lws_restart_ws_ping_pong_timer(wsi);
  229. if (wp == LWS_WRITE_HTTP ||
  230. wp == LWS_WRITE_HTTP_FINAL ||
  231. wp == LWS_WRITE_HTTP_HEADERS)
  232. goto send_raw;
  233. /* if not in a state to send stuff, then just send nothing */
  234. if (wsi->state != LWSS_ESTABLISHED &&
  235. ((wsi->state != LWSS_RETURNED_CLOSE_ALREADY &&
  236. wsi->state != LWSS_AWAITING_CLOSE_ACK) ||
  237. wp != LWS_WRITE_CLOSE))
  238. return 0;
  239. /* if we are continuing a frame that already had its header done */
  240. if (wsi->u.ws.inside_frame) {
  241. lwsl_debug("INSIDE FRAME\n");
  242. goto do_more_inside_frame;
  243. }
  244. wsi->u.ws.clean_buffer = 1;
  245. /*
  246. * give a chance to the extensions to modify payload
  247. * the extension may decide to produce unlimited payload erratically
  248. * (eg, compression extension), so we require only that if he produces
  249. * something, it will be a complete fragment of the length known at
  250. * the time (just the fragment length known), and if he has
  251. * more we will come back next time he is writeable and allow him to
  252. * produce more fragments until he's drained.
  253. *
  254. * This allows what is sent each time it is writeable to be limited to
  255. * a size that can be sent without partial sends or blocking, allows
  256. * interleaving of control frames and other connection service.
  257. */
  258. eff_buf.token = (char *)buf;
  259. eff_buf.token_len = len;
  260. switch ((int)wp) {
  261. case LWS_WRITE_PING:
  262. case LWS_WRITE_PONG:
  263. case LWS_WRITE_CLOSE:
  264. break;
  265. default:
  266. n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_TX, &eff_buf, wp);
  267. if (n < 0)
  268. return -1;
  269. if (n && eff_buf.token_len) {
  270. /* extension requires further draining */
  271. wsi->u.ws.tx_draining_ext = 1;
  272. wsi->u.ws.tx_draining_ext_list = pt->tx_draining_ext_list;
  273. pt->tx_draining_ext_list = wsi;
  274. /* we must come back to do more */
  275. lws_callback_on_writable(wsi);
  276. /*
  277. * keep a copy of the write type for the overall
  278. * action that has provoked generation of these
  279. * fragments, so the last guy can use its FIN state.
  280. */
  281. wsi->u.ws.tx_draining_stashed_wp = wp;
  282. /* this is definitely not actually the last fragment
  283. * because the extension asserted he has more coming
  284. * So make sure this intermediate one doesn't go out
  285. * with a FIN.
  286. */
  287. wp |= LWS_WRITE_NO_FIN;
  288. }
  289. if (eff_buf.token_len && wsi->u.ws.stashed_write_pending) {
  290. wsi->u.ws.stashed_write_pending = 0;
  291. wp = (wp &0xc0) | (int)wsi->u.ws.stashed_write_type;
  292. }
  293. }
  294. /*
  295. * an extension did something we need to keep... for example, if
  296. * compression extension, it has already updated its state according
  297. * to this being issued
  298. */
  299. if ((char *)buf != eff_buf.token) {
  300. /*
  301. * ext might eat it, but no have anything to issue yet
  302. * in that case we have to follow his lead, but stash and
  303. * replace the write type that was lost here the first time.
  304. */
  305. if (len && !eff_buf.token_len) {
  306. if (!wsi->u.ws.stashed_write_pending)
  307. wsi->u.ws.stashed_write_type = (char)wp & 0x3f;
  308. wsi->u.ws.stashed_write_pending = 1;
  309. return len;
  310. }
  311. /*
  312. * extension recreated it:
  313. * need to buffer this if not all sent
  314. */
  315. wsi->u.ws.clean_buffer = 0;
  316. }
  317. buf = (unsigned char *)eff_buf.token;
  318. len = eff_buf.token_len;
  319. switch (wsi->ietf_spec_revision) {
  320. case 13:
  321. if (masked7) {
  322. pre += 4;
  323. dropmask = &buf[0 - pre];
  324. is_masked_bit = 0x80;
  325. }
  326. switch (wp & 0xf) {
  327. case LWS_WRITE_TEXT:
  328. n = LWSWSOPC_TEXT_FRAME;
  329. break;
  330. case LWS_WRITE_BINARY:
  331. n = LWSWSOPC_BINARY_FRAME;
  332. break;
  333. case LWS_WRITE_CONTINUATION:
  334. n = LWSWSOPC_CONTINUATION;
  335. break;
  336. case LWS_WRITE_CLOSE:
  337. n = LWSWSOPC_CLOSE;
  338. break;
  339. case LWS_WRITE_PING:
  340. n = LWSWSOPC_PING;
  341. break;
  342. case LWS_WRITE_PONG:
  343. n = LWSWSOPC_PONG;
  344. break;
  345. default:
  346. lwsl_warn("lws_write: unknown write opc / wp\n");
  347. return -1;
  348. }
  349. if (!(wp & LWS_WRITE_NO_FIN))
  350. n |= 1 << 7;
  351. if (len < 126) {
  352. pre += 2;
  353. buf[-pre] = n;
  354. buf[-pre + 1] = (unsigned char)(len | is_masked_bit);
  355. } else {
  356. if (len < 65536) {
  357. pre += 4;
  358. buf[-pre] = n;
  359. buf[-pre + 1] = 126 | is_masked_bit;
  360. buf[-pre + 2] = (unsigned char)(len >> 8);
  361. buf[-pre + 3] = (unsigned char)len;
  362. } else {
  363. pre += 10;
  364. buf[-pre] = n;
  365. buf[-pre + 1] = 127 | is_masked_bit;
  366. #if defined __LP64__
  367. buf[-pre + 2] = (len >> 56) & 0x7f;
  368. buf[-pre + 3] = len >> 48;
  369. buf[-pre + 4] = len >> 40;
  370. buf[-pre + 5] = len >> 32;
  371. #else
  372. buf[-pre + 2] = 0;
  373. buf[-pre + 3] = 0;
  374. buf[-pre + 4] = 0;
  375. buf[-pre + 5] = 0;
  376. #endif
  377. buf[-pre + 6] = (unsigned char)(len >> 24);
  378. buf[-pre + 7] = (unsigned char)(len >> 16);
  379. buf[-pre + 8] = (unsigned char)(len >> 8);
  380. buf[-pre + 9] = (unsigned char)len;
  381. }
  382. }
  383. break;
  384. }
  385. do_more_inside_frame:
  386. /*
  387. * Deal with masking if we are in client -> server direction and
  388. * the wp demands it
  389. */
  390. if (masked7) {
  391. if (!wsi->u.ws.inside_frame)
  392. if (lws_0405_frame_mask_generate(wsi)) {
  393. lwsl_err("frame mask generation failed\n");
  394. return -1;
  395. }
  396. /*
  397. * in v7, just mask the payload
  398. */
  399. if (dropmask) { /* never set if already inside frame */
  400. for (n = 4; n < (int)len + 4; n++)
  401. dropmask[n] = dropmask[n] ^ wsi->u.ws.mask[
  402. (wsi->u.ws.mask_idx++) & 3];
  403. /* copy the frame nonce into place */
  404. memcpy(dropmask, wsi->u.ws.mask, 4);
  405. }
  406. }
  407. send_raw:
  408. switch ((int)wp) {
  409. case LWS_WRITE_CLOSE:
  410. /* lwsl_hexdump(&buf[-pre], len); */
  411. case LWS_WRITE_HTTP:
  412. case LWS_WRITE_HTTP_FINAL:
  413. case LWS_WRITE_HTTP_HEADERS:
  414. case LWS_WRITE_PONG:
  415. case LWS_WRITE_PING:
  416. #ifdef LWS_USE_HTTP2
  417. if (wsi->mode == LWSCM_HTTP2_SERVING) {
  418. unsigned char flags = 0;
  419. n = LWS_HTTP2_FRAME_TYPE_DATA;
  420. if (wp == LWS_WRITE_HTTP_HEADERS) {
  421. n = LWS_HTTP2_FRAME_TYPE_HEADERS;
  422. flags = LWS_HTTP2_FLAG_END_HEADERS;
  423. if (wsi->u.http2.send_END_STREAM)
  424. flags |= LWS_HTTP2_FLAG_END_STREAM;
  425. }
  426. if ((wp == LWS_WRITE_HTTP ||
  427. wp == LWS_WRITE_HTTP_FINAL) &&
  428. wsi->u.http.content_length) {
  429. wsi->u.http.content_remain -= len;
  430. lwsl_info("%s: content_remain = %lu\n", __func__,
  431. wsi->u.http.content_remain);
  432. if (!wsi->u.http.content_remain) {
  433. lwsl_info("%s: selecting final write mode\n", __func__);
  434. wp = LWS_WRITE_HTTP_FINAL;
  435. }
  436. }
  437. if (wp == LWS_WRITE_HTTP_FINAL && wsi->u.http2.END_STREAM) {
  438. lwsl_info("%s: setting END_STREAM\n", __func__);
  439. flags |= LWS_HTTP2_FLAG_END_STREAM;
  440. }
  441. return lws_http2_frame_write(wsi, n, flags,
  442. wsi->u.http2.my_stream_id, len, buf);
  443. }
  444. #endif
  445. return lws_issue_raw(wsi, (unsigned char *)buf - pre, len + pre);
  446. default:
  447. break;
  448. }
  449. /*
  450. * give any active extensions a chance to munge the buffer
  451. * before send. We pass in a pointer to an lws_tokens struct
  452. * prepared with the default buffer and content length that's in
  453. * there. Rather than rewrite the default buffer, extensions
  454. * that expect to grow the buffer can adapt .token to
  455. * point to their own per-connection buffer in the extension
  456. * user allocation. By default with no extensions or no
  457. * extension callback handling, just the normal input buffer is
  458. * used then so it is efficient.
  459. *
  460. * callback returns 1 in case it wants to spill more buffers
  461. *
  462. * This takes care of holding the buffer if send is incomplete, ie,
  463. * if wsi->u.ws.clean_buffer is 0 (meaning an extension meddled with
  464. * the buffer). If wsi->u.ws.clean_buffer is 1, it will instead
  465. * return to the user code how much OF THE USER BUFFER was consumed.
  466. */
  467. n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre);
  468. wsi->u.ws.inside_frame = 1;
  469. if (n <= 0)
  470. return n;
  471. if (n == (int)len + pre) {
  472. /* everything in the buffer was handled (or rebuffered...) */
  473. wsi->u.ws.inside_frame = 0;
  474. return orig_len;
  475. }
  476. /*
  477. * it is how many bytes of user buffer got sent... may be < orig_len
  478. * in which case callback when writable has already been arranged
  479. * and user code can call lws_write() again with the rest
  480. * later.
  481. */
  482. return n - pre;
  483. }
  484. LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
  485. {
  486. struct lws_context *context = wsi->context;
  487. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  488. struct lws_process_html_args args;
  489. unsigned long amount, poss;
  490. unsigned char *p = pt->serv_buf;
  491. int n, m;
  492. // lwsl_notice("%s (trunc len %d)\n", __func__, wsi->trunc_len);
  493. while (wsi->http2_substream || !lws_send_pipe_choked(wsi)) {
  494. if (wsi->trunc_len) {
  495. if (lws_issue_raw(wsi, wsi->trunc_alloc +
  496. wsi->trunc_offset,
  497. wsi->trunc_len) < 0) {
  498. lwsl_info("%s: closing\n", __func__);
  499. return -1;
  500. }
  501. continue;
  502. }
  503. if (wsi->u.http.filepos == wsi->u.http.filelen)
  504. goto all_sent;
  505. poss = context->pt_serv_buf_size;
  506. if (wsi->sending_chunked) {
  507. /* we need to drop the chunk size in here */
  508. p += 10;
  509. /* allow for the chunk to grow by 128 in translation */
  510. poss -= 10 + 128;
  511. }
  512. if (lws_plat_file_read(wsi, wsi->u.http.fd, &amount, p, poss) < 0)
  513. return -1; /* caller will close */
  514. //lwsl_notice("amount %ld\n", amount);
  515. n = (int)amount;
  516. if (n) {
  517. lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
  518. context->timeout_secs);
  519. if (wsi->sending_chunked) {
  520. args.p = (char *)p;
  521. args.len = n;
  522. args.max_len = poss + 128;
  523. args.final = wsi->u.http.filepos + n ==
  524. wsi->u.http.filelen;
  525. if (user_callback_handle_rxflow(
  526. wsi->vhost->protocols[(int)wsi->protocol_interpret_idx].callback, wsi,
  527. LWS_CALLBACK_PROCESS_HTML,
  528. wsi->user_space, &args, 0) < 0)
  529. return -1;
  530. n = args.len;
  531. p = (unsigned char *)args.p;
  532. }
  533. m = lws_write(wsi, p, n,
  534. wsi->u.http.filepos == wsi->u.http.filelen ?
  535. LWS_WRITE_HTTP_FINAL :
  536. LWS_WRITE_HTTP
  537. );
  538. if (m < 0)
  539. return -1;
  540. wsi->u.http.filepos += amount;
  541. if (m != n) {
  542. /* adjust for what was not sent */
  543. if (lws_plat_file_seek_cur(wsi, wsi->u.http.fd,
  544. m - n) ==
  545. (unsigned long)-1)
  546. return -1;
  547. }
  548. }
  549. all_sent:
  550. if (!wsi->trunc_len &&
  551. wsi->u.http.filepos == wsi->u.http.filelen) {
  552. wsi->state = LWSS_HTTP;
  553. /* we might be in keepalive, so close it off here */
  554. lws_plat_file_close(wsi, wsi->u.http.fd);
  555. wsi->u.http.fd = LWS_INVALID_FILE;
  556. lwsl_debug("file completed\n");
  557. if (wsi->protocol->callback)
  558. /* ignore callback returned value */
  559. if (user_callback_handle_rxflow(
  560. wsi->protocol->callback, wsi,
  561. LWS_CALLBACK_HTTP_FILE_COMPLETION,
  562. wsi->user_space, NULL, 0) < 0)
  563. return -1;
  564. return 1; /* >0 indicates completed */
  565. }
  566. }
  567. lws_callback_on_writable(wsi);
  568. return 0; /* indicates further processing must be done */
  569. }
  570. #if LWS_POSIX
  571. LWS_VISIBLE int
  572. lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
  573. {
  574. int n;
  575. n = recv(wsi->sock, (char *)buf, len, 0);
  576. if (n >= 0) {
  577. if (wsi->vhost)
  578. wsi->vhost->rx += n;
  579. lws_restart_ws_ping_pong_timer(wsi);
  580. return n;
  581. }
  582. #if LWS_POSIX
  583. if (LWS_ERRNO == LWS_EAGAIN ||
  584. LWS_ERRNO == LWS_EWOULDBLOCK ||
  585. LWS_ERRNO == LWS_EINTR)
  586. return LWS_SSL_CAPABLE_MORE_SERVICE;
  587. #endif
  588. lwsl_notice("error on reading from skt : %d\n", LWS_ERRNO);
  589. return LWS_SSL_CAPABLE_ERROR;
  590. }
  591. LWS_VISIBLE int
  592. lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
  593. {
  594. int n = 0;
  595. #if LWS_POSIX
  596. n = send(wsi->sock, (char *)buf, len, MSG_NOSIGNAL);
  597. // lwsl_info("%s: sent len %d result %d", __func__, len, n);
  598. if (n >= 0)
  599. return n;
  600. if (LWS_ERRNO == LWS_EAGAIN ||
  601. LWS_ERRNO == LWS_EWOULDBLOCK ||
  602. LWS_ERRNO == LWS_EINTR) {
  603. if (LWS_ERRNO == LWS_EWOULDBLOCK)
  604. lws_set_blocking_send(wsi);
  605. return LWS_SSL_CAPABLE_MORE_SERVICE;
  606. }
  607. #else
  608. (void)n;
  609. (void)wsi;
  610. (void)buf;
  611. (void)len;
  612. // !!!
  613. #endif
  614. lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n", len, wsi->sock, n, LWS_ERRNO);
  615. return LWS_SSL_CAPABLE_ERROR;
  616. }
  617. #endif
  618. LWS_VISIBLE int
  619. lws_ssl_pending_no_ssl(struct lws *wsi)
  620. {
  621. (void)wsi;
  622. return 0;
  623. }