output.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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 %lu (%s), pending truncated ...\n",
  98. wsi, (unsigned long)len, dump);
  99. #else
  100. lwsl_err("****** %p: Sending new %lu (%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, (unsigned long)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->desc.sockfd))
  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 %ld)\n", wsi, n, (long)real_len);
  150. wsi->trunc_offset += n;
  151. wsi->trunc_len -= n;
  152. if (!wsi->trunc_len) {
  153. lwsl_info("***** %p 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("***** %p 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_info("%p new partial sent %d from %lu total\n", wsi, n,
  173. (unsigned long)real_len);
  174. /*
  175. * - if we still have a suitable malloc lying around, use it
  176. * - or, if too small, reallocate it
  177. * - or, if no buffer, create it
  178. */
  179. if (!wsi->trunc_alloc || real_len - n > wsi->trunc_alloc_len) {
  180. lws_free(wsi->trunc_alloc);
  181. wsi->trunc_alloc_len = real_len - n;
  182. wsi->trunc_alloc = lws_malloc(real_len - n);
  183. if (!wsi->trunc_alloc) {
  184. lwsl_err("truncated send: unable to malloc %lu\n",
  185. (unsigned long)(real_len - n));
  186. return -1;
  187. }
  188. }
  189. wsi->trunc_offset = 0;
  190. wsi->trunc_len = real_len - n;
  191. memcpy(wsi->trunc_alloc, buf + n, real_len - n);
  192. /* since something buffered, force it to get another chance to send */
  193. lws_callback_on_writable(wsi);
  194. return real_len;
  195. }
  196. LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
  197. enum lws_write_protocol wp)
  198. {
  199. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  200. int masked7 = (wsi->mode == LWSCM_WS_CLIENT);
  201. unsigned char is_masked_bit = 0;
  202. unsigned char *dropmask = NULL;
  203. struct lws_tokens eff_buf;
  204. int pre = 0, n;
  205. size_t orig_len = len;
  206. #ifdef LWS_WITH_ACCESS_LOG
  207. wsi->access_log.sent += len;
  208. #endif
  209. if (wsi->vhost)
  210. wsi->vhost->conn_stats.tx += len;
  211. if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
  212. /* remove us from the list */
  213. struct lws **w = &pt->tx_draining_ext_list;
  214. lwsl_debug("%s: TX EXT DRAINING: Remove from list\n", __func__);
  215. wsi->u.ws.tx_draining_ext = 0;
  216. /* remove us from context draining ext list */
  217. while (*w) {
  218. if (*w == wsi) {
  219. *w = wsi->u.ws.tx_draining_ext_list;
  220. break;
  221. }
  222. w = &((*w)->u.ws.tx_draining_ext_list);
  223. }
  224. wsi->u.ws.tx_draining_ext_list = NULL;
  225. wp = (wsi->u.ws.tx_draining_stashed_wp & 0xc0) |
  226. LWS_WRITE_CONTINUATION;
  227. lwsl_ext("FORCED draining wp to 0x%02X\n", wp);
  228. }
  229. lws_restart_ws_ping_pong_timer(wsi);
  230. if (wp == LWS_WRITE_HTTP ||
  231. wp == LWS_WRITE_HTTP_FINAL ||
  232. wp == LWS_WRITE_HTTP_HEADERS)
  233. goto send_raw;
  234. /* if not in a state to send stuff, then just send nothing */
  235. if (wsi->state != LWSS_ESTABLISHED &&
  236. ((wsi->state != LWSS_RETURNED_CLOSE_ALREADY &&
  237. wsi->state != LWSS_AWAITING_CLOSE_ACK) ||
  238. wp != LWS_WRITE_CLOSE))
  239. return 0;
  240. /* if we are continuing a frame that already had its header done */
  241. if (wsi->u.ws.inside_frame) {
  242. lwsl_debug("INSIDE FRAME\n");
  243. goto do_more_inside_frame;
  244. }
  245. wsi->u.ws.clean_buffer = 1;
  246. /*
  247. * give a chance to the extensions to modify payload
  248. * the extension may decide to produce unlimited payload erratically
  249. * (eg, compression extension), so we require only that if he produces
  250. * something, it will be a complete fragment of the length known at
  251. * the time (just the fragment length known), and if he has
  252. * more we will come back next time he is writeable and allow him to
  253. * produce more fragments until he's drained.
  254. *
  255. * This allows what is sent each time it is writeable to be limited to
  256. * a size that can be sent without partial sends or blocking, allows
  257. * interleaving of control frames and other connection service.
  258. */
  259. eff_buf.token = (char *)buf;
  260. eff_buf.token_len = len;
  261. switch ((int)wp) {
  262. case LWS_WRITE_PING:
  263. case LWS_WRITE_PONG:
  264. case LWS_WRITE_CLOSE:
  265. break;
  266. default:
  267. n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_TX, &eff_buf, wp);
  268. if (n < 0)
  269. return -1;
  270. if (n && eff_buf.token_len) {
  271. /* extension requires further draining */
  272. wsi->u.ws.tx_draining_ext = 1;
  273. wsi->u.ws.tx_draining_ext_list = pt->tx_draining_ext_list;
  274. pt->tx_draining_ext_list = wsi;
  275. /* we must come back to do more */
  276. lws_callback_on_writable(wsi);
  277. /*
  278. * keep a copy of the write type for the overall
  279. * action that has provoked generation of these
  280. * fragments, so the last guy can use its FIN state.
  281. */
  282. wsi->u.ws.tx_draining_stashed_wp = wp;
  283. /* this is definitely not actually the last fragment
  284. * because the extension asserted he has more coming
  285. * So make sure this intermediate one doesn't go out
  286. * with a FIN.
  287. */
  288. wp |= LWS_WRITE_NO_FIN;
  289. }
  290. if (eff_buf.token_len && wsi->u.ws.stashed_write_pending) {
  291. wsi->u.ws.stashed_write_pending = 0;
  292. wp = (wp &0xc0) | (int)wsi->u.ws.stashed_write_type;
  293. }
  294. }
  295. /*
  296. * an extension did something we need to keep... for example, if
  297. * compression extension, it has already updated its state according
  298. * to this being issued
  299. */
  300. if ((char *)buf != eff_buf.token) {
  301. /*
  302. * ext might eat it, but no have anything to issue yet
  303. * in that case we have to follow his lead, but stash and
  304. * replace the write type that was lost here the first time.
  305. */
  306. if (len && !eff_buf.token_len) {
  307. if (!wsi->u.ws.stashed_write_pending)
  308. wsi->u.ws.stashed_write_type = (char)wp & 0x3f;
  309. wsi->u.ws.stashed_write_pending = 1;
  310. return len;
  311. }
  312. /*
  313. * extension recreated it:
  314. * need to buffer this if not all sent
  315. */
  316. wsi->u.ws.clean_buffer = 0;
  317. }
  318. buf = (unsigned char *)eff_buf.token;
  319. len = eff_buf.token_len;
  320. switch (wsi->ietf_spec_revision) {
  321. case 13:
  322. if (masked7) {
  323. pre += 4;
  324. dropmask = &buf[0 - pre];
  325. is_masked_bit = 0x80;
  326. }
  327. switch (wp & 0xf) {
  328. case LWS_WRITE_TEXT:
  329. n = LWSWSOPC_TEXT_FRAME;
  330. break;
  331. case LWS_WRITE_BINARY:
  332. n = LWSWSOPC_BINARY_FRAME;
  333. break;
  334. case LWS_WRITE_CONTINUATION:
  335. n = LWSWSOPC_CONTINUATION;
  336. break;
  337. case LWS_WRITE_CLOSE:
  338. n = LWSWSOPC_CLOSE;
  339. break;
  340. case LWS_WRITE_PING:
  341. n = LWSWSOPC_PING;
  342. break;
  343. case LWS_WRITE_PONG:
  344. n = LWSWSOPC_PONG;
  345. break;
  346. default:
  347. lwsl_warn("lws_write: unknown write opc / wp\n");
  348. return -1;
  349. }
  350. if (!(wp & LWS_WRITE_NO_FIN))
  351. n |= 1 << 7;
  352. if (len < 126) {
  353. pre += 2;
  354. buf[-pre] = n;
  355. buf[-pre + 1] = (unsigned char)(len | is_masked_bit);
  356. } else {
  357. if (len < 65536) {
  358. pre += 4;
  359. buf[-pre] = n;
  360. buf[-pre + 1] = 126 | is_masked_bit;
  361. buf[-pre + 2] = (unsigned char)(len >> 8);
  362. buf[-pre + 3] = (unsigned char)len;
  363. } else {
  364. pre += 10;
  365. buf[-pre] = n;
  366. buf[-pre + 1] = 127 | is_masked_bit;
  367. #if defined __LP64__
  368. buf[-pre + 2] = (len >> 56) & 0x7f;
  369. buf[-pre + 3] = len >> 48;
  370. buf[-pre + 4] = len >> 40;
  371. buf[-pre + 5] = len >> 32;
  372. #else
  373. buf[-pre + 2] = 0;
  374. buf[-pre + 3] = 0;
  375. buf[-pre + 4] = 0;
  376. buf[-pre + 5] = 0;
  377. #endif
  378. buf[-pre + 6] = (unsigned char)(len >> 24);
  379. buf[-pre + 7] = (unsigned char)(len >> 16);
  380. buf[-pre + 8] = (unsigned char)(len >> 8);
  381. buf[-pre + 9] = (unsigned char)len;
  382. }
  383. }
  384. break;
  385. }
  386. do_more_inside_frame:
  387. /*
  388. * Deal with masking if we are in client -> server direction and
  389. * the wp demands it
  390. */
  391. if (masked7) {
  392. if (!wsi->u.ws.inside_frame)
  393. if (lws_0405_frame_mask_generate(wsi)) {
  394. lwsl_err("frame mask generation failed\n");
  395. return -1;
  396. }
  397. /*
  398. * in v7, just mask the payload
  399. */
  400. if (dropmask) { /* never set if already inside frame */
  401. for (n = 4; n < (int)len + 4; n++)
  402. dropmask[n] = dropmask[n] ^ wsi->u.ws.mask[
  403. (wsi->u.ws.mask_idx++) & 3];
  404. /* copy the frame nonce into place */
  405. memcpy(dropmask, wsi->u.ws.mask, 4);
  406. }
  407. }
  408. send_raw:
  409. switch ((int)wp) {
  410. case LWS_WRITE_CLOSE:
  411. /* lwsl_hexdump(&buf[-pre], len); */
  412. case LWS_WRITE_HTTP:
  413. case LWS_WRITE_HTTP_FINAL:
  414. case LWS_WRITE_HTTP_HEADERS:
  415. case LWS_WRITE_PONG:
  416. case LWS_WRITE_PING:
  417. #ifdef LWS_USE_HTTP2
  418. if (wsi->mode == LWSCM_HTTP2_SERVING) {
  419. unsigned char flags = 0;
  420. n = LWS_HTTP2_FRAME_TYPE_DATA;
  421. if (wp == LWS_WRITE_HTTP_HEADERS) {
  422. n = LWS_HTTP2_FRAME_TYPE_HEADERS;
  423. flags = LWS_HTTP2_FLAG_END_HEADERS;
  424. if (wsi->u.http2.send_END_STREAM)
  425. flags |= LWS_HTTP2_FLAG_END_STREAM;
  426. }
  427. if ((wp == LWS_WRITE_HTTP ||
  428. wp == LWS_WRITE_HTTP_FINAL) &&
  429. wsi->u.http.content_length) {
  430. wsi->u.http.content_remain -= len;
  431. lwsl_info("%s: content_remain = %lu\n", __func__,
  432. (unsigned long)wsi->u.http.content_remain);
  433. if (!wsi->u.http.content_remain) {
  434. lwsl_info("%s: selecting final write mode\n", __func__);
  435. wp = LWS_WRITE_HTTP_FINAL;
  436. }
  437. }
  438. if (wp == LWS_WRITE_HTTP_FINAL && wsi->u.http2.END_STREAM) {
  439. lwsl_info("%s: setting END_STREAM\n", __func__);
  440. flags |= LWS_HTTP2_FLAG_END_STREAM;
  441. }
  442. return lws_http2_frame_write(wsi, n, flags,
  443. wsi->u.http2.my_stream_id, len, buf);
  444. }
  445. #endif
  446. return lws_issue_raw(wsi, (unsigned char *)buf - pre, len + pre);
  447. default:
  448. break;
  449. }
  450. /*
  451. * give any active extensions a chance to munge the buffer
  452. * before send. We pass in a pointer to an lws_tokens struct
  453. * prepared with the default buffer and content length that's in
  454. * there. Rather than rewrite the default buffer, extensions
  455. * that expect to grow the buffer can adapt .token to
  456. * point to their own per-connection buffer in the extension
  457. * user allocation. By default with no extensions or no
  458. * extension callback handling, just the normal input buffer is
  459. * used then so it is efficient.
  460. *
  461. * callback returns 1 in case it wants to spill more buffers
  462. *
  463. * This takes care of holding the buffer if send is incomplete, ie,
  464. * if wsi->u.ws.clean_buffer is 0 (meaning an extension meddled with
  465. * the buffer). If wsi->u.ws.clean_buffer is 1, it will instead
  466. * return to the user code how much OF THE USER BUFFER was consumed.
  467. */
  468. n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre);
  469. wsi->u.ws.inside_frame = 1;
  470. if (n <= 0)
  471. return n;
  472. if (n == (int)len + pre) {
  473. /* everything in the buffer was handled (or rebuffered...) */
  474. wsi->u.ws.inside_frame = 0;
  475. return orig_len;
  476. }
  477. /*
  478. * it is how many bytes of user buffer got sent... may be < orig_len
  479. * in which case callback when writable has already been arranged
  480. * and user code can call lws_write() again with the rest
  481. * later.
  482. */
  483. return n - pre;
  484. }
  485. LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
  486. {
  487. struct lws_context *context = wsi->context;
  488. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  489. struct lws_process_html_args args;
  490. lws_filepos_t amount, poss;
  491. unsigned char *p;
  492. #if defined(LWS_WITH_RANGES)
  493. unsigned char finished = 0;
  494. #endif
  495. int n, m;
  496. // lwsl_notice("%s (trunc len %d)\n", __func__, wsi->trunc_len);
  497. while (wsi->http2_substream || !lws_send_pipe_choked(wsi)) {
  498. if (wsi->trunc_len) {
  499. if (lws_issue_raw(wsi, wsi->trunc_alloc +
  500. wsi->trunc_offset,
  501. wsi->trunc_len) < 0) {
  502. lwsl_info("%s: closing\n", __func__);
  503. goto file_had_it;
  504. }
  505. continue;
  506. }
  507. if (wsi->u.http.filepos == wsi->u.http.filelen)
  508. goto all_sent;
  509. n = 0;
  510. p = pt->serv_buf;
  511. #if defined(LWS_WITH_RANGES)
  512. if (wsi->u.http.range.count_ranges && !wsi->u.http.range.inside) {
  513. lwsl_notice("%s: doing range start %llu\n", __func__, wsi->u.http.range.start);
  514. if ((long)lws_vfs_file_seek_cur(wsi->u.http.fop_fd,
  515. wsi->u.http.range.start -
  516. wsi->u.http.filepos) < 0)
  517. goto file_had_it;
  518. wsi->u.http.filepos = wsi->u.http.range.start;
  519. if (wsi->u.http.range.count_ranges > 1) {
  520. n = lws_snprintf((char *)p, context->pt_serv_buf_size,
  521. "_lws\x0d\x0a"
  522. "Content-Type: %s\x0d\x0a"
  523. "Content-Range: bytes %llu-%llu/%llu\x0d\x0a"
  524. "\x0d\x0a",
  525. wsi->u.http.multipart_content_type,
  526. wsi->u.http.range.start,
  527. wsi->u.http.range.end,
  528. wsi->u.http.range.extent);
  529. p += n;
  530. }
  531. wsi->u.http.range.budget = wsi->u.http.range.end -
  532. wsi->u.http.range.start + 1;
  533. wsi->u.http.range.inside = 1;
  534. }
  535. #endif
  536. poss = context->pt_serv_buf_size - n;
  537. #if defined(LWS_WITH_RANGES)
  538. if (wsi->u.http.range.count_ranges) {
  539. if (wsi->u.http.range.count_ranges > 1)
  540. poss -= 7; /* allow for final boundary */
  541. if (poss > wsi->u.http.range.budget)
  542. poss = wsi->u.http.range.budget;
  543. }
  544. #endif
  545. if (wsi->sending_chunked) {
  546. /* we need to drop the chunk size in here */
  547. p += 10;
  548. /* allow for the chunk to grow by 128 in translation */
  549. poss -= 10 + 128;
  550. }
  551. if (lws_vfs_file_read(wsi->u.http.fop_fd, &amount, p, poss) < 0)
  552. goto file_had_it; /* caller will close */
  553. //lwsl_notice("amount %ld\n", amount);
  554. if (wsi->sending_chunked)
  555. n = (int)amount;
  556. else
  557. n = (p - pt->serv_buf) + (int)amount;
  558. if (n) {
  559. lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
  560. context->timeout_secs);
  561. if (wsi->sending_chunked) {
  562. args.p = (char *)p;
  563. args.len = n;
  564. args.max_len = poss + 128;
  565. args.final = wsi->u.http.filepos + n ==
  566. wsi->u.http.filelen;
  567. if (user_callback_handle_rxflow(
  568. wsi->vhost->protocols[(int)wsi->protocol_interpret_idx].callback, wsi,
  569. LWS_CALLBACK_PROCESS_HTML,
  570. wsi->user_space, &args, 0) < 0)
  571. goto file_had_it;
  572. n = args.len;
  573. p = (unsigned char *)args.p;
  574. } else
  575. p = pt->serv_buf;
  576. #if defined(LWS_WITH_RANGES)
  577. if (wsi->u.http.range.send_ctr + 1 ==
  578. wsi->u.http.range.count_ranges && // last range
  579. wsi->u.http.range.count_ranges > 1 && // was 2+ ranges (ie, multipart)
  580. wsi->u.http.range.budget - amount == 0) {// final part
  581. n += lws_snprintf((char *)pt->serv_buf + n, 6,
  582. "_lws\x0d\x0a"); // append trailing boundary
  583. lwsl_debug("added trailing boundary\n");
  584. }
  585. #endif
  586. m = lws_write(wsi, p, n,
  587. wsi->u.http.filepos == wsi->u.http.filelen ?
  588. LWS_WRITE_HTTP_FINAL :
  589. LWS_WRITE_HTTP
  590. );
  591. if (m < 0)
  592. goto file_had_it;
  593. wsi->u.http.filepos += amount;
  594. #if defined(LWS_WITH_RANGES)
  595. if (wsi->u.http.range.count_ranges >= 1) {
  596. wsi->u.http.range.budget -= amount;
  597. if (wsi->u.http.range.budget == 0) {
  598. lwsl_notice("range budget exhausted\n");
  599. wsi->u.http.range.inside = 0;
  600. wsi->u.http.range.send_ctr++;
  601. if (lws_ranges_next(&wsi->u.http.range) < 1) {
  602. finished = 1;
  603. goto all_sent;
  604. }
  605. }
  606. }
  607. #endif
  608. if (m != n) {
  609. /* adjust for what was not sent */
  610. if (lws_vfs_file_seek_cur(wsi->u.http.fop_fd,
  611. m - n) ==
  612. (unsigned long)-1)
  613. goto file_had_it;
  614. }
  615. }
  616. all_sent:
  617. if ((!wsi->trunc_len && wsi->u.http.filepos == wsi->u.http.filelen)
  618. #if defined(LWS_WITH_RANGES)
  619. || finished)
  620. #else
  621. )
  622. #endif
  623. {
  624. wsi->state = LWSS_HTTP;
  625. /* we might be in keepalive, so close it off here */
  626. lws_vfs_file_close(&wsi->u.http.fop_fd);
  627. lwsl_debug("file completed\n");
  628. if (wsi->protocol->callback)
  629. /* ignore callback returned value */
  630. if (user_callback_handle_rxflow(
  631. wsi->protocol->callback, wsi,
  632. LWS_CALLBACK_HTTP_FILE_COMPLETION,
  633. wsi->user_space, NULL, 0) < 0)
  634. return -1;
  635. return 1; /* >0 indicates completed */
  636. }
  637. }
  638. lws_callback_on_writable(wsi);
  639. return 0; /* indicates further processing must be done */
  640. file_had_it:
  641. lws_vfs_file_close(&wsi->u.http.fop_fd);
  642. return -1;
  643. }
  644. #if LWS_POSIX
  645. LWS_VISIBLE int
  646. lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
  647. {
  648. int n;
  649. n = recv(wsi->desc.sockfd, (char *)buf, len, 0);
  650. if (n >= 0) {
  651. if (wsi->vhost)
  652. wsi->vhost->conn_stats.rx += n;
  653. lws_restart_ws_ping_pong_timer(wsi);
  654. return n;
  655. }
  656. #if LWS_POSIX
  657. if (LWS_ERRNO == LWS_EAGAIN ||
  658. LWS_ERRNO == LWS_EWOULDBLOCK ||
  659. LWS_ERRNO == LWS_EINTR)
  660. return LWS_SSL_CAPABLE_MORE_SERVICE;
  661. #endif
  662. lwsl_notice("error on reading from skt : %d\n", LWS_ERRNO);
  663. return LWS_SSL_CAPABLE_ERROR;
  664. }
  665. LWS_VISIBLE int
  666. lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
  667. {
  668. int n = 0;
  669. #if LWS_POSIX
  670. n = send(wsi->desc.sockfd, (char *)buf, len, MSG_NOSIGNAL);
  671. // lwsl_info("%s: sent len %d result %d", __func__, len, n);
  672. if (n >= 0)
  673. return n;
  674. if (LWS_ERRNO == LWS_EAGAIN ||
  675. LWS_ERRNO == LWS_EWOULDBLOCK ||
  676. LWS_ERRNO == LWS_EINTR) {
  677. if (LWS_ERRNO == LWS_EWOULDBLOCK) {
  678. lws_set_blocking_send(wsi);
  679. }
  680. return LWS_SSL_CAPABLE_MORE_SERVICE;
  681. }
  682. #else
  683. (void)n;
  684. (void)wsi;
  685. (void)buf;
  686. (void)len;
  687. // !!!
  688. #endif
  689. lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n", len, wsi->desc.sockfd, n, LWS_ERRNO);
  690. return LWS_SSL_CAPABLE_ERROR;
  691. }
  692. #endif
  693. LWS_VISIBLE int
  694. lws_ssl_pending_no_ssl(struct lws *wsi)
  695. {
  696. (void)wsi;
  697. return 0;
  698. }