blob: 0d850ebe8e64c615799ed229f1fc6d6b6bbee8a6 [file] [log] [blame]
Andy Greenb429d482013-01-16 12:21:29 +08001/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
Andy Green54806b12015-12-17 17:03:59 +08004 * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
Andy Greenb429d482013-01-16 12:21:29 +08005 *
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
22#include "private-libwebsockets.h"
23
Andy Greenb429d482013-01-16 12:21:29 +080024static int
Andy Green4b85c1d2015-12-04 11:08:32 +080025lws_0405_frame_mask_generate(struct lws *wsi)
Andy Greenb429d482013-01-16 12:21:29 +080026{
Andy Green44e0b082015-12-28 14:24:49 +080027#if 0
Andy Green67112662016-01-11 11:34:01 +080028 wsi->u.ws.mask[0] = 0;
29 wsi->u.ws.mask[1] = 0;
30 wsi->u.ws.mask[2] = 0;
31 wsi->u.ws.mask[3] = 0;
Andy Green44e0b082015-12-28 14:24:49 +080032#else
Andy Green8c1f6022016-01-26 20:56:56 +080033 int n;
Andy Greenb429d482013-01-16 12:21:29 +080034 /* fetch the per-frame nonce */
35
Andy Green67112662016-01-11 11:34:01 +080036 n = lws_get_random(lws_get_context(wsi), wsi->u.ws.mask, 4);
Andy Greenb429d482013-01-16 12:21:29 +080037 if (n != 4) {
38 lwsl_parser("Unable to read from random device %s %d\n",
Andy Green54806b12015-12-17 17:03:59 +080039 SYSTEM_RANDOM_FILEPATH, n);
Andy Greenb429d482013-01-16 12:21:29 +080040 return 1;
41 }
Andy Green44e0b082015-12-28 14:24:49 +080042#endif
Andy Greenb429d482013-01-16 12:21:29 +080043 /* start masking from first byte of masking key buffer */
Andy Green67112662016-01-11 11:34:01 +080044 wsi->u.ws.mask_idx = 0;
Andy Greenb429d482013-01-16 12:21:29 +080045
Andy Greenb429d482013-01-16 12:21:29 +080046 return 0;
47}
48
Andy Greencf3590e2013-01-16 14:35:27 +080049#ifdef _DEBUG
Andy Greenb429d482013-01-16 12:21:29 +080050
Peter Pentchev9a4fef72013-03-30 09:52:21 +080051LWS_VISIBLE void lwsl_hexdump(void *vbuf, size_t len)
Andy Greenb429d482013-01-16 12:21:29 +080052{
Andy Greencf3590e2013-01-16 14:35:27 +080053 unsigned char *buf = (unsigned char *)vbuf;
Andy Greend607bb92015-12-06 05:52:09 +080054 unsigned int n, m, start;
Andy Greencf3590e2013-01-16 14:35:27 +080055 char line[80];
56 char *p;
Andy Greenb429d482013-01-16 12:21:29 +080057
58 lwsl_parser("\n");
59
60 for (n = 0; n < len;) {
61 start = n;
Andy Greencf3590e2013-01-16 14:35:27 +080062 p = line;
Andy Greenb429d482013-01-16 12:21:29 +080063
Andy Greencf3590e2013-01-16 14:35:27 +080064 p += sprintf(p, "%04X: ", start);
Andy Greenb429d482013-01-16 12:21:29 +080065
66 for (m = 0; m < 16 && n < len; m++)
Andy Greencf3590e2013-01-16 14:35:27 +080067 p += sprintf(p, "%02X ", buf[n++]);
Andy Greenb429d482013-01-16 12:21:29 +080068 while (m++ < 16)
Andy Greencf3590e2013-01-16 14:35:27 +080069 p += sprintf(p, " ");
Andy Greenb429d482013-01-16 12:21:29 +080070
Andy Greencf3590e2013-01-16 14:35:27 +080071 p += sprintf(p, " ");
Andy Greenb429d482013-01-16 12:21:29 +080072
73 for (m = 0; m < 16 && (start + m) < len; m++) {
Andy Green3182ece2013-01-20 17:08:31 +080074 if (buf[start + m] >= ' ' && buf[start + m] < 127)
Andy Greencf3590e2013-01-16 14:35:27 +080075 *p++ = buf[start + m];
Andy Greenb429d482013-01-16 12:21:29 +080076 else
Andy Greencf3590e2013-01-16 14:35:27 +080077 *p++ = '.';
Andy Greenb429d482013-01-16 12:21:29 +080078 }
79 while (m++ < 16)
Andy Greencf3590e2013-01-16 14:35:27 +080080 *p++ = ' ';
Andy Greenb429d482013-01-16 12:21:29 +080081
Andy Greencf3590e2013-01-16 14:35:27 +080082 *p++ = '\n';
83 *p = '\0';
Andy Green3182ece2013-01-20 17:08:31 +080084 lwsl_debug("%s", line);
Andy Greenb429d482013-01-16 12:21:29 +080085 }
86 lwsl_debug("\n");
87}
88
Andy Greencf3590e2013-01-16 14:35:27 +080089#endif
90
Andy Greenfc7c5e42013-02-23 10:50:10 +080091/*
Andy Green1f4267b2013-10-17 08:09:19 +080092 * notice this returns number of bytes consumed, or -1
Andy Greenfc7c5e42013-02-23 10:50:10 +080093 */
94
Andy Green4b85c1d2015-12-04 11:08:32 +080095int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
Andy Greenb429d482013-01-16 12:21:29 +080096{
Andy Green6d645392015-12-17 18:25:25 +080097 struct lws_context *context = lws_get_context(wsi);
mroszko793e7c02013-12-10 21:15:00 +080098 size_t real_len = len;
Andy Greend607bb92015-12-06 05:52:09 +080099 int n, m;
Andy Green40110e82015-12-14 08:52:03 +0800100
Andy Green03cf1dd2014-04-01 14:20:44 +0800101 if (!len)
102 return 0;
Andy Greena1a24d22014-04-10 14:25:24 +0800103 /* just ignore sends after we cleared the truncation buffer */
Andy Green54806b12015-12-17 17:03:59 +0800104 if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
105 !wsi->trunc_len)
Andy Greena1a24d22014-04-10 14:25:24 +0800106 return len;
Andy Green2764eba2013-12-09 14:16:17 +0800107
Andy Green54806b12015-12-17 17:03:59 +0800108 if (wsi->trunc_len && (buf < wsi->trunc_alloc ||
109 buf > (wsi->trunc_alloc + wsi->trunc_len +
110 wsi->trunc_offset))) {
Andy Green27e770b2014-03-23 11:21:51 +0800111 lwsl_err("****** %x Sending new, pending truncated ...\n", wsi);
Andy Green2764eba2013-12-09 14:16:17 +0800112 assert(0);
113 }
Andy Greenb429d482013-01-16 12:21:29 +0800114
Andy Green67112662016-01-11 11:34:01 +0800115 m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_TX_DO_SEND, &buf, len);
Andy Green2c24ec02014-04-02 19:45:42 +0800116 if (m < 0)
117 return -1;
118 if (m) /* handled */ {
119 n = m;
120 goto handle_truncated_send;
Andy Greenb429d482013-01-16 12:21:29 +0800121 }
Andy Green8c0d3c02015-11-02 20:34:12 +0800122
Andy Greenfc772cc2015-11-14 13:48:58 +0800123 if (!lws_socket_is_valid(wsi->sock))
Andy Green1f4267b2013-10-17 08:09:19 +0800124 lwsl_warn("** error invalid sock but expected to send\n");
Andy Greenb429d482013-01-16 12:21:29 +0800125
Andy Greend607bb92015-12-06 05:52:09 +0800126 /* nope, send it on the socket directly */
Andy Greene000a702013-01-29 12:37:35 +0800127 lws_latency_pre(context, wsi);
Andy Green02138122014-04-06 06:26:35 +0100128 n = lws_ssl_capable_write(wsi, buf, len);
Andy Green54806b12015-12-17 17:03:59 +0800129 lws_latency(context, wsi, "send lws_issue_raw", n,
130 (unsigned int)n == len);
Andy Greend7340c12014-04-10 14:08:10 +0800131
Andy Green02138122014-04-06 06:26:35 +0100132 switch (n) {
133 case LWS_SSL_CAPABLE_ERROR:
Andy Green1e499182014-10-16 08:23:46 +0800134 /* we're going to close, let close know sends aren't possible */
135 wsi->socket_is_permanently_unusable = 1;
Andy Green02138122014-04-06 06:26:35 +0100136 return -1;
137 case LWS_SSL_CAPABLE_MORE_SERVICE:
Andy Greend7340c12014-04-10 14:08:10 +0800138 /* nothing got sent, not fatal, retry the whole thing later */
Andy Green02138122014-04-06 06:26:35 +0100139 n = 0;
Andy Greend7340c12014-04-10 14:08:10 +0800140 break;
Andy Greenb429d482013-01-16 12:21:29 +0800141 }
Andy Green1f4267b2013-10-17 08:09:19 +0800142
143handle_truncated_send:
Andy Green1f4267b2013-10-17 08:09:19 +0800144 /*
Andy Greend7340c12014-04-10 14:08:10 +0800145 * we were already handling a truncated send?
Andy Green1f4267b2013-10-17 08:09:19 +0800146 */
Andy Green54806b12015-12-17 17:03:59 +0800147 if (wsi->trunc_len) {
148 lwsl_info("%p partial adv %d (vs %d)\n", wsi, n, real_len);
149 wsi->trunc_offset += n;
150 wsi->trunc_len -= n;
Andy Green1f4267b2013-10-17 08:09:19 +0800151
Andy Green54806b12015-12-17 17:03:59 +0800152 if (!wsi->trunc_len) {
Andy Green2764eba2013-12-09 14:16:17 +0800153 lwsl_info("***** %x partial send completed\n", wsi);
Andy Greene254d952014-03-23 11:41:15 +0800154 /* done with it, but don't free it */
Andy Green2764eba2013-12-09 14:16:17 +0800155 n = real_len;
Andy Green54806b12015-12-17 17:03:59 +0800156 if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
Andy Greena1a24d22014-04-10 14:25:24 +0800157 lwsl_info("***** %x signalling to close now\n", wsi);
158 return -1; /* retry closing now */
Andy Greenf4ffc1e2014-04-10 17:06:59 +0800159 }
Andy Green15d56dd2014-04-10 11:23:18 +0800160 }
161 /* always callback on writeable */
Andy Green11c05bf2015-12-16 18:19:08 +0800162 lws_callback_on_writable(wsi);
Andy Green1f4267b2013-10-17 08:09:19 +0800163
164 return n;
165 }
166
Andy Green2cd30742015-11-02 13:10:33 +0800167 if ((unsigned int)n == real_len)
Andy Greend7340c12014-04-10 14:08:10 +0800168 /* what we just sent went out cleanly */
169 return n;
Andy Green1f4267b2013-10-17 08:09:19 +0800170
Andy Greend7340c12014-04-10 14:08:10 +0800171 /*
172 * Newly truncated send. Buffer the remainder (it will get
173 * first priority next time the socket is writable)
174 */
Andy Green54806b12015-12-17 17:03:59 +0800175 lwsl_info("%p new partial sent %d from %d total\n", wsi, n, real_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800176
Andy Greend7340c12014-04-10 14:08:10 +0800177 /*
178 * - if we still have a suitable malloc lying around, use it
179 * - or, if too small, reallocate it
180 * - or, if no buffer, create it
181 */
Andy Green54806b12015-12-17 17:03:59 +0800182 if (!wsi->trunc_alloc || real_len - n > wsi->trunc_alloc_len) {
183 lws_free(wsi->trunc_alloc);
Andy Greend7340c12014-04-10 14:08:10 +0800184
Andy Green54806b12015-12-17 17:03:59 +0800185 wsi->trunc_alloc_len = real_len - n;
186 wsi->trunc_alloc = lws_malloc(real_len - n);
187 if (!wsi->trunc_alloc) {
Andy Greend7340c12014-04-10 14:08:10 +0800188 lwsl_err("truncated send: unable to malloc %d\n",
Andy Greend607bb92015-12-06 05:52:09 +0800189 real_len - n);
Andy Greend7340c12014-04-10 14:08:10 +0800190 return -1;
Andy Greene254d952014-03-23 11:41:15 +0800191 }
Andy Green1f4267b2013-10-17 08:09:19 +0800192 }
Andy Green54806b12015-12-17 17:03:59 +0800193 wsi->trunc_offset = 0;
194 wsi->trunc_len = real_len - n;
195 memcpy(wsi->trunc_alloc, buf + n, real_len - n);
Andy Green1f4267b2013-10-17 08:09:19 +0800196
Andy Greend7340c12014-04-10 14:08:10 +0800197 /* since something buffered, force it to get another chance to send */
Andy Green11c05bf2015-12-16 18:19:08 +0800198 lws_callback_on_writable(wsi);
Andy Greend7340c12014-04-10 14:08:10 +0800199
200 return real_len;
Andy Greenb429d482013-01-16 12:21:29 +0800201}
202
Andy Greenb429d482013-01-16 12:21:29 +0800203/**
Andy Green62304762015-12-04 08:43:54 +0800204 * lws_write() - Apply protocol then write data to client
Andy Greenb429d482013-01-16 12:21:29 +0800205 * @wsi: Websocket instance (available from user callback)
206 * @buf: The data to send. For data being sent on a websocket
207 * connection (ie, not default http), this buffer MUST have
Andy Green67112662016-01-11 11:34:01 +0800208 * LWS_PRE bytes valid BEFORE the pointer.
Andy Green3246ebb2015-12-26 12:03:06 +0800209 * This is so the protocol header data can be added in-situ.
Andy Greenb429d482013-01-16 12:21:29 +0800210 * @len: Count of the data bytes in the payload starting from buf
211 * @protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one
212 * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
213 * data on a websockets connection. Remember to allow the extra
214 * bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
215 * are used.
216 *
217 * This function provides the way to issue data back to the client
218 * for both http and websocket protocols.
219 *
220 * In the case of sending using websocket protocol, be sure to allocate
221 * valid storage before and after buf as explained above. This scheme
222 * allows maximum efficiency of sending data and protocol in a single
223 * packet while not burdening the user code with any protocol knowledge.
Andy Greenfc7c5e42013-02-23 10:50:10 +0800224 *
225 * Return may be -1 for a fatal error needing connection close, or a
226 * positive number reflecting the amount of bytes actually sent. This
227 * can be less than the requested number of bytes due to OS memory
228 * pressure at any given time.
Andy Greenb429d482013-01-16 12:21:29 +0800229 */
230
Andy Green67112662016-01-11 11:34:01 +0800231LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
232 enum lws_write_protocol wp)
Andy Greenb429d482013-01-16 12:21:29 +0800233{
Andy Greend3a55052016-01-19 03:34:24 +0800234 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
Andy Green67112662016-01-11 11:34:01 +0800235 int masked7 = (wsi->mode == LWSCM_WS_CLIENT);
Andy Greenb429d482013-01-16 12:21:29 +0800236 unsigned char is_masked_bit = 0;
Andy Greend607bb92015-12-06 05:52:09 +0800237 unsigned char *dropmask = NULL;
Andy Greenb429d482013-01-16 12:21:29 +0800238 struct lws_tokens eff_buf;
Andy Greende1a6a52015-12-26 10:07:17 +0800239 int pre = 0, n;
Andy Greend607bb92015-12-06 05:52:09 +0800240 size_t orig_len = len;
Andy Greenb429d482013-01-16 12:21:29 +0800241
Andy Green67112662016-01-11 11:34:01 +0800242 if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
243 /* remove us from the list */
Andy Greend3a55052016-01-19 03:34:24 +0800244 struct lws **w = &pt->tx_draining_ext_list;
Andy Green67112662016-01-11 11:34:01 +0800245 lwsl_debug("%s: TX EXT DRAINING: Remove from list\n", __func__);
246 wsi->u.ws.tx_draining_ext = 0;
247 /* remove us from context draining ext list */
248 while (*w) {
249 if (*w == wsi) {
250 *w = wsi->u.ws.tx_draining_ext_list;
251 break;
252 }
253 w = &((*w)->u.ws.tx_draining_ext_list);
254 }
255 wsi->u.ws.tx_draining_ext_list = NULL;
256 wp = (wsi->u.ws.tx_draining_stashed_wp & 0xc0) |
257 LWS_WRITE_CONTINUATION;
258
259 lwsl_ext("FORCED draining wp to 0x%02X\n", wp);
260 }
261
262 if (wp == LWS_WRITE_HTTP ||
263 wp == LWS_WRITE_HTTP_FINAL ||
264 wp == LWS_WRITE_HTTP_HEADERS)
Andy Greenb429d482013-01-16 12:21:29 +0800265 goto send_raw;
266
Andy Green67112662016-01-11 11:34:01 +0800267 /* if not in a state to send stuff, then just send nothing */
Andy Greenb429d482013-01-16 12:21:29 +0800268
Andy Green54806b12015-12-17 17:03:59 +0800269 if (wsi->state != LWSS_ESTABLISHED &&
Andy Green67112662016-01-11 11:34:01 +0800270 ((wsi->state != LWSS_RETURNED_CLOSE_ALREADY &&
271 wsi->state != LWSS_AWAITING_CLOSE_ACK) ||
272 wp != LWS_WRITE_CLOSE))
273 return 0;
Andy Greenb429d482013-01-16 12:21:29 +0800274
Andy Green1f4267b2013-10-17 08:09:19 +0800275 /* if we are continuing a frame that already had its header done */
276
Andy Green44e0b082015-12-28 14:24:49 +0800277 if (wsi->u.ws.inside_frame) {
278 lwsl_debug("INSIDE FRAME\n");
Andy Green1f4267b2013-10-17 08:09:19 +0800279 goto do_more_inside_frame;
Andy Green44e0b082015-12-28 14:24:49 +0800280 }
Andy Green1f4267b2013-10-17 08:09:19 +0800281
Andy Green822241c2014-08-18 22:21:51 +0800282 wsi->u.ws.clean_buffer = 1;
Andy Green1f4267b2013-10-17 08:09:19 +0800283
Andy Green1f4267b2013-10-17 08:09:19 +0800284 /*
285 * give a chance to the extensions to modify payload
Andy Green67112662016-01-11 11:34:01 +0800286 * the extension may decide to produce unlimited payload erratically
287 * (eg, compression extension), so we require only that if he produces
288 * something, it will be a complete fragment of the length known at
289 * the time (just the fragment length known), and if he has
290 * more we will come back next time he is writeable and allow him to
291 * produce more fragments until he's drained.
292 *
293 * This allows what is sent each time it is writeable to be limited to
294 * a size that can be sent without partial sends or blocking, allows
295 * interleaving of control frames and other connection service.
Andy Green1f4267b2013-10-17 08:09:19 +0800296 */
Andy Greenb429d482013-01-16 12:21:29 +0800297 eff_buf.token = (char *)buf;
298 eff_buf.token_len = len;
299
Andy Green67112662016-01-11 11:34:01 +0800300 switch ((int)wp) {
Andy Green0303db42013-01-17 14:46:43 +0800301 case LWS_WRITE_PING:
302 case LWS_WRITE_PONG:
303 case LWS_WRITE_CLOSE:
304 break;
305 default:
Andy Green67112662016-01-11 11:34:01 +0800306 n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_TX, &eff_buf, wp);
307 if (n < 0)
Andy Green2c24ec02014-04-02 19:45:42 +0800308 return -1;
Andy Green67112662016-01-11 11:34:01 +0800309
310 if (n && eff_buf.token_len) {
311 /* extension requires further draining */
312 wsi->u.ws.tx_draining_ext = 1;
Andy Greend3a55052016-01-19 03:34:24 +0800313 wsi->u.ws.tx_draining_ext_list = pt->tx_draining_ext_list;
314 pt->tx_draining_ext_list = wsi;
Andy Green67112662016-01-11 11:34:01 +0800315 /* we must come back to do more */
316 lws_callback_on_writable(wsi);
317 /*
318 * keep a copy of the write type for the overall
319 * action that has provoked generation of these
320 * fragments, so the last guy can use its FIN state.
321 */
322 wsi->u.ws.tx_draining_stashed_wp = wp;
323 /* this is definitely not actually the last fragment
324 * because the extension asserted he has more coming
325 * So make sure this intermediate one doesn't go out
326 * with a FIN.
327 */
328 wp |= LWS_WRITE_NO_FIN;
329 }
330
331 if (eff_buf.token_len && wsi->u.ws.stashed_write_pending) {
332 wsi->u.ws.stashed_write_pending = 0;
333 wp = (wp &0xc0) | (int)wsi->u.ws.stashed_write_type;
334 }
Andy Greenb429d482013-01-16 12:21:29 +0800335 }
336
Andy Green1f4267b2013-10-17 08:09:19 +0800337 /*
338 * an extension did something we need to keep... for example, if
339 * compression extension, it has already updated its state according
340 * to this being issued
341 */
Andy Green67112662016-01-11 11:34:01 +0800342 if ((char *)buf != eff_buf.token) {
343 /*
344 * ext might eat it, but no have anything to issue yet
345 * in that case we have to follow his lead, but stash and
346 * replace the write type that was lost here the first time.
347 */
348 if (len && !eff_buf.token_len) {
349 if (!wsi->u.ws.stashed_write_pending)
350 wsi->u.ws.stashed_write_type = (char)wp & 0x3f;
351 wsi->u.ws.stashed_write_pending = 1;
352 return len;
353 }
Andy Green27e770b2014-03-23 11:21:51 +0800354 /*
355 * extension recreated it:
356 * need to buffer this if not all sent
357 */
358 wsi->u.ws.clean_buffer = 0;
Andy Green67112662016-01-11 11:34:01 +0800359 }
Andy Green1f4267b2013-10-17 08:09:19 +0800360
Andy Greenb429d482013-01-16 12:21:29 +0800361 buf = (unsigned char *)eff_buf.token;
362 len = eff_buf.token_len;
363
364 switch (wsi->ietf_spec_revision) {
Andy Greenb429d482013-01-16 12:21:29 +0800365 case 13:
366 if (masked7) {
367 pre += 4;
368 dropmask = &buf[0 - pre];
369 is_masked_bit = 0x80;
370 }
Andy Green5738c0e2013-01-21 09:53:35 +0800371
Andy Green67112662016-01-11 11:34:01 +0800372 switch (wp & 0xf) {
Andy Greenb429d482013-01-16 12:21:29 +0800373 case LWS_WRITE_TEXT:
Andy Green54806b12015-12-17 17:03:59 +0800374 n = LWSWSOPC_TEXT_FRAME;
Andy Greenb429d482013-01-16 12:21:29 +0800375 break;
376 case LWS_WRITE_BINARY:
Andy Green54806b12015-12-17 17:03:59 +0800377 n = LWSWSOPC_BINARY_FRAME;
Andy Greenb429d482013-01-16 12:21:29 +0800378 break;
379 case LWS_WRITE_CONTINUATION:
Andy Green54806b12015-12-17 17:03:59 +0800380 n = LWSWSOPC_CONTINUATION;
Andy Greenb429d482013-01-16 12:21:29 +0800381 break;
382
383 case LWS_WRITE_CLOSE:
Andy Green54806b12015-12-17 17:03:59 +0800384 n = LWSWSOPC_CLOSE;
Andy Greenb429d482013-01-16 12:21:29 +0800385 break;
386 case LWS_WRITE_PING:
Andy Green54806b12015-12-17 17:03:59 +0800387 n = LWSWSOPC_PING;
Andy Greenb429d482013-01-16 12:21:29 +0800388 break;
389 case LWS_WRITE_PONG:
Andy Green54806b12015-12-17 17:03:59 +0800390 n = LWSWSOPC_PONG;
Andy Greenb429d482013-01-16 12:21:29 +0800391 break;
392 default:
Andy Green67112662016-01-11 11:34:01 +0800393 lwsl_warn("lws_write: unknown write opc / wp\n");
Andy Greenb429d482013-01-16 12:21:29 +0800394 return -1;
395 }
396
Andy Green67112662016-01-11 11:34:01 +0800397 if (!(wp & LWS_WRITE_NO_FIN))
Andy Greenb429d482013-01-16 12:21:29 +0800398 n |= 1 << 7;
399
400 if (len < 126) {
401 pre += 2;
402 buf[-pre] = n;
Andy Green1cc03882015-12-06 08:00:03 +0800403 buf[-pre + 1] = (unsigned char)(len | is_masked_bit);
Andy Greenb429d482013-01-16 12:21:29 +0800404 } else {
405 if (len < 65536) {
406 pre += 4;
407 buf[-pre] = n;
408 buf[-pre + 1] = 126 | is_masked_bit;
Andy Green1cc03882015-12-06 08:00:03 +0800409 buf[-pre + 2] = (unsigned char)(len >> 8);
410 buf[-pre + 3] = (unsigned char)len;
Andy Greenb429d482013-01-16 12:21:29 +0800411 } else {
412 pre += 10;
413 buf[-pre] = n;
414 buf[-pre + 1] = 127 | is_masked_bit;
415#if defined __LP64__
416 buf[-pre + 2] = (len >> 56) & 0x7f;
417 buf[-pre + 3] = len >> 48;
418 buf[-pre + 4] = len >> 40;
419 buf[-pre + 5] = len >> 32;
420#else
421 buf[-pre + 2] = 0;
422 buf[-pre + 3] = 0;
423 buf[-pre + 4] = 0;
424 buf[-pre + 5] = 0;
425#endif
Andy Green1cc03882015-12-06 08:00:03 +0800426 buf[-pre + 6] = (unsigned char)(len >> 24);
427 buf[-pre + 7] = (unsigned char)(len >> 16);
428 buf[-pre + 8] = (unsigned char)(len >> 8);
429 buf[-pre + 9] = (unsigned char)len;
Andy Greenb429d482013-01-16 12:21:29 +0800430 }
431 }
432 break;
433 }
434
Andy Green1f4267b2013-10-17 08:09:19 +0800435do_more_inside_frame:
436
Andy Greenb429d482013-01-16 12:21:29 +0800437 /*
438 * Deal with masking if we are in client -> server direction and
Andy Green67112662016-01-11 11:34:01 +0800439 * the wp demands it
Andy Greenb429d482013-01-16 12:21:29 +0800440 */
441
Andy Green67112662016-01-11 11:34:01 +0800442 if (masked7) {
Andy Green1f4267b2013-10-17 08:09:19 +0800443 if (!wsi->u.ws.inside_frame)
Andy Green3ef579b2015-12-04 09:23:56 +0800444 if (lws_0405_frame_mask_generate(wsi)) {
Andy Green27e770b2014-03-23 11:21:51 +0800445 lwsl_err("frame mask generation failed\n");
Andy Green1f4267b2013-10-17 08:09:19 +0800446 return -1;
447 }
Andy Greenb429d482013-01-16 12:21:29 +0800448
Andy Green5738c0e2013-01-21 09:53:35 +0800449 /*
450 * in v7, just mask the payload
451 */
Nikolay Dimitrova8268e72013-12-21 10:22:17 +0800452 if (dropmask) { /* never set if already inside frame */
453 for (n = 4; n < (int)len + 4; n++)
Andy Green67112662016-01-11 11:34:01 +0800454 dropmask[n] = dropmask[n] ^ wsi->u.ws.mask[
455 (wsi->u.ws.mask_idx++) & 3];
Andy Green5738c0e2013-01-21 09:53:35 +0800456
Andy Green5738c0e2013-01-21 09:53:35 +0800457 /* copy the frame nonce into place */
Andy Green67112662016-01-11 11:34:01 +0800458 memcpy(dropmask, wsi->u.ws.mask, 4);
Nikolay Dimitrova8268e72013-12-21 10:22:17 +0800459 }
Andy Greenb429d482013-01-16 12:21:29 +0800460 }
461
462send_raw:
Andy Green67112662016-01-11 11:34:01 +0800463 switch ((int)wp) {
Andy Green0303db42013-01-17 14:46:43 +0800464 case LWS_WRITE_CLOSE:
Andy Greende1a6a52015-12-26 10:07:17 +0800465/* lwsl_hexdump(&buf[-pre], len); */
Andy Green0303db42013-01-17 14:46:43 +0800466 case LWS_WRITE_HTTP:
Andy Green200f3852014-10-18 12:23:05 +0800467 case LWS_WRITE_HTTP_FINAL:
Andy Green024eb6c2014-10-08 12:00:53 +0800468 case LWS_WRITE_HTTP_HEADERS:
Andy Green0303db42013-01-17 14:46:43 +0800469 case LWS_WRITE_PONG:
470 case LWS_WRITE_PING:
Andy Green095d3032014-10-08 12:15:15 +0800471#ifdef LWS_USE_HTTP2
Andy Green54806b12015-12-17 17:03:59 +0800472 if (wsi->mode == LWSCM_HTTP2_SERVING) {
Andy Green2add6342014-10-12 08:38:16 +0800473 unsigned char flags = 0;
474
Andy Green024eb6c2014-10-08 12:00:53 +0800475 n = LWS_HTTP2_FRAME_TYPE_DATA;
Andy Green67112662016-01-11 11:34:01 +0800476 if (wp == LWS_WRITE_HTTP_HEADERS) {
Andy Green024eb6c2014-10-08 12:00:53 +0800477 n = LWS_HTTP2_FRAME_TYPE_HEADERS;
Andy Green91b05892014-10-17 08:38:44 +0800478 flags = LWS_HTTP2_FLAG_END_HEADERS;
Andy Green1cea5812014-10-19 07:36:20 +0800479 if (wsi->u.http2.send_END_STREAM)
480 flags |= LWS_HTTP2_FLAG_END_STREAM;
Andy Green2add6342014-10-12 08:38:16 +0800481 }
Andy Green40110e82015-12-14 08:52:03 +0800482
Andy Green67112662016-01-11 11:34:01 +0800483 if ((wp == LWS_WRITE_HTTP ||
484 wp == LWS_WRITE_HTTP_FINAL) &&
Andy Greende1a6a52015-12-26 10:07:17 +0800485 wsi->u.http.content_length) {
Andy Green200f3852014-10-18 12:23:05 +0800486 wsi->u.http.content_remain -= len;
Andy Greende1a6a52015-12-26 10:07:17 +0800487 lwsl_info("%s: content_remain = %lu\n", __func__,
488 wsi->u.http.content_remain);
Andy Green200f3852014-10-18 12:23:05 +0800489 if (!wsi->u.http.content_remain) {
490 lwsl_info("%s: selecting final write mode\n", __func__);
Andy Green67112662016-01-11 11:34:01 +0800491 wp = LWS_WRITE_HTTP_FINAL;
Andy Green200f3852014-10-18 12:23:05 +0800492 }
493 }
Andy Green40110e82015-12-14 08:52:03 +0800494
Andy Green67112662016-01-11 11:34:01 +0800495 if (wp == LWS_WRITE_HTTP_FINAL && wsi->u.http2.END_STREAM) {
Andy Green200f3852014-10-18 12:23:05 +0800496 lwsl_info("%s: setting END_STREAM\n", __func__);
497 flags |= LWS_HTTP2_FLAG_END_STREAM;
498 }
499
Andy Greende1a6a52015-12-26 10:07:17 +0800500 return lws_http2_frame_write(wsi, n, flags,
501 wsi->u.http2.my_stream_id, len, buf);
Andy Green024eb6c2014-10-08 12:00:53 +0800502 }
Andy Green095d3032014-10-08 12:15:15 +0800503#endif
Andy Greende1a6a52015-12-26 10:07:17 +0800504 return lws_issue_raw(wsi, (unsigned char *)buf - pre, len + pre);
Andy Green0303db42013-01-17 14:46:43 +0800505 default:
506 break;
Andy Greenb429d482013-01-16 12:21:29 +0800507 }
508
509 /*
510 * give any active extensions a chance to munge the buffer
511 * before send. We pass in a pointer to an lws_tokens struct
512 * prepared with the default buffer and content length that's in
513 * there. Rather than rewrite the default buffer, extensions
514 * that expect to grow the buffer can adapt .token to
515 * point to their own per-connection buffer in the extension
516 * user allocation. By default with no extensions or no
517 * extension callback handling, just the normal input buffer is
518 * used then so it is efficient.
519 *
520 * callback returns 1 in case it wants to spill more buffers
Andy Green1f4267b2013-10-17 08:09:19 +0800521 *
522 * This takes care of holding the buffer if send is incomplete, ie,
523 * if wsi->u.ws.clean_buffer is 0 (meaning an extension meddled with
524 * the buffer). If wsi->u.ws.clean_buffer is 1, it will instead
525 * return to the user code how much OF THE USER BUFFER was consumed.
Andy Greenb429d482013-01-16 12:21:29 +0800526 */
527
Andy Greende1a6a52015-12-26 10:07:17 +0800528 n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre);
Andy Green67112662016-01-11 11:34:01 +0800529 wsi->u.ws.inside_frame = 1;
Andy Green03cf1dd2014-04-01 14:20:44 +0800530 if (n <= 0)
Andy Greenfc7c5e42013-02-23 10:50:10 +0800531 return n;
532
Andy Greende1a6a52015-12-26 10:07:17 +0800533 if (n == (int)len + pre) {
Andy Green1f4267b2013-10-17 08:09:19 +0800534 /* everything in the buffer was handled (or rebuffered...) */
535 wsi->u.ws.inside_frame = 0;
536 return orig_len;
537 }
538
539 /*
540 * it is how many bytes of user buffer got sent... may be < orig_len
541 * in which case callback when writable has already been arranged
Andy Green62304762015-12-04 08:43:54 +0800542 * and user code can call lws_write() again with the rest
Andy Green1f4267b2013-10-17 08:09:19 +0800543 * later.
544 */
545
Andy Greende1a6a52015-12-26 10:07:17 +0800546 return n - pre;
Andy Greenb429d482013-01-16 12:21:29 +0800547}
548
Andy Green11c05bf2015-12-16 18:19:08 +0800549LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
Andy Greenb8b247d2013-01-22 07:20:08 +0800550{
Andy Green11c05bf2015-12-16 18:19:08 +0800551 struct lws_context *context = wsi->context;
Andy Greend3a55052016-01-19 03:34:24 +0800552 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
Andy Green4e442b72015-12-10 07:58:58 +0800553 unsigned long amount;
554 int n, m;
Andy Greenb8b247d2013-01-22 07:20:08 +0800555
556 while (!lws_send_pipe_choked(wsi)) {
Andy Green54806b12015-12-17 17:03:59 +0800557 if (wsi->trunc_len) {
558 if (lws_issue_raw(wsi, wsi->trunc_alloc +
559 wsi->trunc_offset,
560 wsi->trunc_len) < 0) {
Andy Green4e442b72015-12-10 07:58:58 +0800561 lwsl_info("%s: closing\n", __func__);
Andy Greena1a24d22014-04-10 14:25:24 +0800562 return -1;
563 }
Andy Green2764eba2013-12-09 14:16:17 +0800564 continue;
565 }
566
567 if (wsi->u.http.filepos == wsi->u.http.filelen)
568 goto all_sent;
569
Andy Green891628b2015-12-11 13:12:58 +0800570 if (lws_plat_file_read(wsi, wsi->u.http.fd, &amount,
Andy Greend3a55052016-01-19 03:34:24 +0800571 pt->serv_buf,
572 LWS_MAX_SOCKET_IO_BUF) < 0)
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100573 return -1; /* caller will close */
Andy Green4e442b72015-12-10 07:58:58 +0800574
575 n = (int)amount;
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100576 if (n) {
Andy Greend607bb92015-12-06 05:52:09 +0800577 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
Andy Green200a6a22016-02-15 20:36:02 +0800578 context->timeout_secs);
Andy Green200f3852014-10-18 12:23:05 +0800579 wsi->u.http.filepos += n;
Andy Greend3a55052016-01-19 03:34:24 +0800580 m = lws_write(wsi, pt->serv_buf, n,
Andy Greend607bb92015-12-06 05:52:09 +0800581 wsi->u.http.filepos == wsi->u.http.filelen ?
582 LWS_WRITE_HTTP_FINAL : LWS_WRITE_HTTP);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800583 if (m < 0)
584 return -1;
585
Andy Green158e8042014-04-02 14:25:10 +0800586 if (m != n)
Andy Greenfc7c5e42013-02-23 10:50:10 +0800587 /* adjust for what was not sent */
Andy Green54806b12015-12-17 17:03:59 +0800588 if (lws_plat_file_seek_cur(wsi, wsi->u.http.fd,
Andy Green95181d92015-12-10 12:50:10 +0800589 m - n) ==
590 (unsigned long)-1)
Andy Green7ef4b2e2014-11-30 13:00:47 +0800591 return -1;
Andy Greenb8b247d2013-01-22 07:20:08 +0800592 }
Andy Green2764eba2013-12-09 14:16:17 +0800593all_sent:
Andy Green54806b12015-12-17 17:03:59 +0800594 if (!wsi->trunc_len && wsi->u.http.filepos == wsi->u.http.filelen) {
595 wsi->state = LWSS_HTTP;
Andy Greenb8b247d2013-01-22 07:20:08 +0800596
Andy Greenc6f95d32015-10-27 07:07:14 +0800597 /* we might be in keepalive, so close it off here */
Andy Green891628b2015-12-11 13:12:58 +0800598 lws_plat_file_close(wsi, wsi->u.http.fd);
Andy Greenc6f95d32015-10-27 07:07:14 +0800599 wsi->u.http.fd = LWS_INVALID_FILE;
600
Andy Greenb8b247d2013-01-22 07:20:08 +0800601 if (wsi->protocol->callback)
David Gauchard6c582282013-06-29 10:24:16 +0800602 /* ignore callback returned value */
Andy Greene99a83c2016-01-20 16:56:06 +0800603 if (user_callback_handle_rxflow(
604 wsi->protocol->callback, wsi,
605 LWS_CALLBACK_HTTP_FILE_COMPLETION,
606 wsi->user_space, NULL, 0) < 0)
607 return -1;
David Gauchard6c582282013-06-29 10:24:16 +0800608 return 1; /* >0 indicates completed */
Andy Greenb8b247d2013-01-22 07:20:08 +0800609 }
610 }
611
Andy Green2764eba2013-12-09 14:16:17 +0800612 lwsl_info("choked before able to send whole file (post)\n");
Andy Green11c05bf2015-12-16 18:19:08 +0800613 lws_callback_on_writable(wsi);
Andy Greenb8b247d2013-01-22 07:20:08 +0800614
David Gauchard6c582282013-06-29 10:24:16 +0800615 return 0; /* indicates further processing must be done */
Andy Greenb8b247d2013-01-22 07:20:08 +0800616}
Andy Green02138122014-04-06 06:26:35 +0100617
Andy Green11f27342015-11-08 12:10:26 +0800618#if LWS_POSIX
Andy Green02138122014-04-06 06:26:35 +0100619LWS_VISIBLE int
Andy Green6b5de702015-12-15 21:15:58 +0800620lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
Andy Green02138122014-04-06 06:26:35 +0100621{
622 int n;
623
Stephan Eberleb820e2c2015-10-23 08:10:55 +0200624 n = recv(wsi->sock, (char *)buf, len, 0);
Andy Greend7340c12014-04-10 14:08:10 +0800625 if (n >= 0)
626 return n;
Andy Green11f27342015-11-08 12:10:26 +0800627#if LWS_POSIX
Mark Liknessdbe624d2015-10-15 21:21:06 +0800628 if (LWS_ERRNO == LWS_EAGAIN ||
629 LWS_ERRNO == LWS_EWOULDBLOCK ||
630 LWS_ERRNO == LWS_EINTR)
631 return LWS_SSL_CAPABLE_MORE_SERVICE;
Andy Green8c0d3c02015-11-02 20:34:12 +0800632#endif
Andy Greend7340c12014-04-10 14:08:10 +0800633 lwsl_warn("error on reading from skt\n");
634 return LWS_SSL_CAPABLE_ERROR;
Andy Green02138122014-04-06 06:26:35 +0100635}
636
637LWS_VISIBLE int
Andy Green4b85c1d2015-12-04 11:08:32 +0800638lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
Andy Green02138122014-04-06 06:26:35 +0100639{
Andy Green11f27342015-11-08 12:10:26 +0800640 int n = 0;
Andy Green8c0d3c02015-11-02 20:34:12 +0800641
642#if LWS_POSIX
Stephan Eberleb820e2c2015-10-23 08:10:55 +0200643 n = send(wsi->sock, (char *)buf, len, MSG_NOSIGNAL);
Andy Green8d5351a2016-01-27 08:50:31 +0800644// lwsl_info("%s: sent len %d result %d", __func__, len, n);
Andy Greend7340c12014-04-10 14:08:10 +0800645 if (n >= 0)
646 return n;
Andy Green02138122014-04-06 06:26:35 +0100647
Andy Greend7340c12014-04-10 14:08:10 +0800648 if (LWS_ERRNO == LWS_EAGAIN ||
649 LWS_ERRNO == LWS_EWOULDBLOCK ||
650 LWS_ERRNO == LWS_EINTR) {
651 if (LWS_ERRNO == LWS_EWOULDBLOCK)
652 lws_set_blocking_send(wsi);
653
654 return LWS_SSL_CAPABLE_MORE_SERVICE;
655 }
Andy Green8c0d3c02015-11-02 20:34:12 +0800656#else
657 (void)n;
658 (void)wsi;
659 (void)buf;
660 (void)len;
661 // !!!
662#endif
Andy Green40110e82015-12-14 08:52:03 +0800663
Andy Green8c1f6022016-01-26 20:56:56 +0800664 lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n", len, wsi->sock, n, LWS_ERRNO);
Andy Greend7340c12014-04-10 14:08:10 +0800665 return LWS_SSL_CAPABLE_ERROR;
Andy Green02138122014-04-06 06:26:35 +0100666}
Andy Green11f27342015-11-08 12:10:26 +0800667#endif
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +0200668LWS_VISIBLE int
Andy Green4b85c1d2015-12-04 11:08:32 +0800669lws_ssl_pending_no_ssl(struct lws *wsi)
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +0200670{
Andy Green2cd30742015-11-02 13:10:33 +0800671 (void)wsi;
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +0200672 return 0;
673}