blob: eb164c94bd16130685335493583b8d723d0c7287 [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 Greenb429d482013-01-16 12:21:29 +080027 int n;
28
29 /* fetch the per-frame nonce */
30
Andy Green6d645392015-12-17 18:25:25 +080031 n = lws_get_random(lws_get_context(wsi), wsi->u.ws.mask_nonce, 4);
Andy Greenb429d482013-01-16 12:21:29 +080032 if (n != 4) {
33 lwsl_parser("Unable to read from random device %s %d\n",
Andy Green54806b12015-12-17 17:03:59 +080034 SYSTEM_RANDOM_FILEPATH, n);
Andy Greenb429d482013-01-16 12:21:29 +080035 return 1;
36 }
37
38 /* start masking from first byte of masking key buffer */
Andy Green623a98d2013-01-21 11:04:23 +080039 wsi->u.ws.frame_mask_index = 0;
Andy Greenb429d482013-01-16 12:21:29 +080040
Andy Greenb429d482013-01-16 12:21:29 +080041 return 0;
42}
43
Andy Greencf3590e2013-01-16 14:35:27 +080044#ifdef _DEBUG
Andy Greenb429d482013-01-16 12:21:29 +080045
Peter Pentchev9a4fef72013-03-30 09:52:21 +080046LWS_VISIBLE void lwsl_hexdump(void *vbuf, size_t len)
Andy Greenb429d482013-01-16 12:21:29 +080047{
Andy Greencf3590e2013-01-16 14:35:27 +080048 unsigned char *buf = (unsigned char *)vbuf;
Andy Greend607bb92015-12-06 05:52:09 +080049 unsigned int n, m, start;
Andy Greencf3590e2013-01-16 14:35:27 +080050 char line[80];
51 char *p;
Andy Greenb429d482013-01-16 12:21:29 +080052
53 lwsl_parser("\n");
54
55 for (n = 0; n < len;) {
56 start = n;
Andy Greencf3590e2013-01-16 14:35:27 +080057 p = line;
Andy Greenb429d482013-01-16 12:21:29 +080058
Andy Greencf3590e2013-01-16 14:35:27 +080059 p += sprintf(p, "%04X: ", start);
Andy Greenb429d482013-01-16 12:21:29 +080060
61 for (m = 0; m < 16 && n < len; m++)
Andy Greencf3590e2013-01-16 14:35:27 +080062 p += sprintf(p, "%02X ", buf[n++]);
Andy Greenb429d482013-01-16 12:21:29 +080063 while (m++ < 16)
Andy Greencf3590e2013-01-16 14:35:27 +080064 p += sprintf(p, " ");
Andy Greenb429d482013-01-16 12:21:29 +080065
Andy Greencf3590e2013-01-16 14:35:27 +080066 p += sprintf(p, " ");
Andy Greenb429d482013-01-16 12:21:29 +080067
68 for (m = 0; m < 16 && (start + m) < len; m++) {
Andy Green3182ece2013-01-20 17:08:31 +080069 if (buf[start + m] >= ' ' && buf[start + m] < 127)
Andy Greencf3590e2013-01-16 14:35:27 +080070 *p++ = buf[start + m];
Andy Greenb429d482013-01-16 12:21:29 +080071 else
Andy Greencf3590e2013-01-16 14:35:27 +080072 *p++ = '.';
Andy Greenb429d482013-01-16 12:21:29 +080073 }
74 while (m++ < 16)
Andy Greencf3590e2013-01-16 14:35:27 +080075 *p++ = ' ';
Andy Greenb429d482013-01-16 12:21:29 +080076
Andy Greencf3590e2013-01-16 14:35:27 +080077 *p++ = '\n';
78 *p = '\0';
Andy Green3182ece2013-01-20 17:08:31 +080079 lwsl_debug("%s", line);
Andy Greenb429d482013-01-16 12:21:29 +080080 }
81 lwsl_debug("\n");
82}
83
Andy Greencf3590e2013-01-16 14:35:27 +080084#endif
85
Andy Greenfc7c5e42013-02-23 10:50:10 +080086/*
Andy Green1f4267b2013-10-17 08:09:19 +080087 * notice this returns number of bytes consumed, or -1
Andy Greenfc7c5e42013-02-23 10:50:10 +080088 */
89
Andy Green4b85c1d2015-12-04 11:08:32 +080090int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
Andy Greenb429d482013-01-16 12:21:29 +080091{
Andy Green6d645392015-12-17 18:25:25 +080092 struct lws_context *context = lws_get_context(wsi);
mroszko793e7c02013-12-10 21:15:00 +080093 size_t real_len = len;
Andy Greend607bb92015-12-06 05:52:09 +080094 int n, m;
Andy Green40110e82015-12-14 08:52:03 +080095
Andy Green03cf1dd2014-04-01 14:20:44 +080096 if (!len)
97 return 0;
Andy Greena1a24d22014-04-10 14:25:24 +080098 /* just ignore sends after we cleared the truncation buffer */
Andy Green54806b12015-12-17 17:03:59 +080099 if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
100 !wsi->trunc_len)
Andy Greena1a24d22014-04-10 14:25:24 +0800101 return len;
Andy Green2764eba2013-12-09 14:16:17 +0800102
Andy Green54806b12015-12-17 17:03:59 +0800103 if (wsi->trunc_len && (buf < wsi->trunc_alloc ||
104 buf > (wsi->trunc_alloc + wsi->trunc_len +
105 wsi->trunc_offset))) {
Andy Green27e770b2014-03-23 11:21:51 +0800106 lwsl_err("****** %x Sending new, pending truncated ...\n", wsi);
Andy Green2764eba2013-12-09 14:16:17 +0800107 assert(0);
108 }
Andy Greenb429d482013-01-16 12:21:29 +0800109
Andy Green54806b12015-12-17 17:03:59 +0800110 m = lws_ext_cb_wsi_active_exts(wsi,
Andy Green2c24ec02014-04-02 19:45:42 +0800111 LWS_EXT_CALLBACK_PACKET_TX_DO_SEND, &buf, len);
112 if (m < 0)
113 return -1;
114 if (m) /* handled */ {
115 n = m;
116 goto handle_truncated_send;
Andy Greenb429d482013-01-16 12:21:29 +0800117 }
Andy Green8c0d3c02015-11-02 20:34:12 +0800118
Andy Greenfc772cc2015-11-14 13:48:58 +0800119 if (!lws_socket_is_valid(wsi->sock))
Andy Green1f4267b2013-10-17 08:09:19 +0800120 lwsl_warn("** error invalid sock but expected to send\n");
Andy Greenb429d482013-01-16 12:21:29 +0800121
Andy Greend607bb92015-12-06 05:52:09 +0800122 /* nope, send it on the socket directly */
Andy Greene000a702013-01-29 12:37:35 +0800123 lws_latency_pre(context, wsi);
Andy Green02138122014-04-06 06:26:35 +0100124 n = lws_ssl_capable_write(wsi, buf, len);
Andy Green54806b12015-12-17 17:03:59 +0800125 lws_latency(context, wsi, "send lws_issue_raw", n,
126 (unsigned int)n == len);
Andy Greend7340c12014-04-10 14:08:10 +0800127
Andy Green02138122014-04-06 06:26:35 +0100128 switch (n) {
129 case LWS_SSL_CAPABLE_ERROR:
Andy Green54806b12015-12-17 17:03:59 +0800130 lwsl_err("%s: wsi %p: LWS_SSL_CAPABLE_ERROR\n", __func__,
131 (void *)wsi);
Andy Green1e499182014-10-16 08:23:46 +0800132 /* we're going to close, let close know sends aren't possible */
133 wsi->socket_is_permanently_unusable = 1;
Andy Green02138122014-04-06 06:26:35 +0100134 return -1;
135 case LWS_SSL_CAPABLE_MORE_SERVICE:
Andy Greend7340c12014-04-10 14:08:10 +0800136 /* nothing got sent, not fatal, retry the whole thing later */
Andy Green02138122014-04-06 06:26:35 +0100137 n = 0;
Andy Greend7340c12014-04-10 14:08:10 +0800138 break;
Andy Greenb429d482013-01-16 12:21:29 +0800139 }
Andy Green1f4267b2013-10-17 08:09:19 +0800140
141handle_truncated_send:
Andy Green1f4267b2013-10-17 08:09:19 +0800142 /*
Andy Greend7340c12014-04-10 14:08:10 +0800143 * we were already handling a truncated send?
Andy Green1f4267b2013-10-17 08:09:19 +0800144 */
Andy Green54806b12015-12-17 17:03:59 +0800145 if (wsi->trunc_len) {
146 lwsl_info("%p partial adv %d (vs %d)\n", wsi, n, real_len);
147 wsi->trunc_offset += n;
148 wsi->trunc_len -= n;
Andy Green1f4267b2013-10-17 08:09:19 +0800149
Andy Green54806b12015-12-17 17:03:59 +0800150 if (!wsi->trunc_len) {
Andy Green2764eba2013-12-09 14:16:17 +0800151 lwsl_info("***** %x partial send completed\n", wsi);
Andy Greene254d952014-03-23 11:41:15 +0800152 /* done with it, but don't free it */
Andy Green2764eba2013-12-09 14:16:17 +0800153 n = real_len;
Andy Green54806b12015-12-17 17:03:59 +0800154 if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
Andy Greena1a24d22014-04-10 14:25:24 +0800155 lwsl_info("***** %x signalling to close now\n", wsi);
156 return -1; /* retry closing now */
Andy Greenf4ffc1e2014-04-10 17:06:59 +0800157 }
Andy Green15d56dd2014-04-10 11:23:18 +0800158 }
159 /* always callback on writeable */
Andy Green11c05bf2015-12-16 18:19:08 +0800160 lws_callback_on_writable(wsi);
Andy Green1f4267b2013-10-17 08:09:19 +0800161
162 return n;
163 }
164
Andy Green2cd30742015-11-02 13:10:33 +0800165 if ((unsigned int)n == real_len)
Andy Greend7340c12014-04-10 14:08:10 +0800166 /* what we just sent went out cleanly */
167 return n;
Andy Green1f4267b2013-10-17 08:09:19 +0800168
Andy Greend7340c12014-04-10 14:08:10 +0800169 if (n && wsi->u.ws.clean_buffer)
Andy Green1f4267b2013-10-17 08:09:19 +0800170 /*
Andy Greend7340c12014-04-10 14:08:10 +0800171 * This buffer unaffected by extension rewriting.
172 * It means the user code is expected to deal with
173 * partial sends. (lws knows the header was already
174 * sent, so on next send will just resume sending
175 * payload)
Andy Green1f4267b2013-10-17 08:09:19 +0800176 */
Andy Greend7340c12014-04-10 14:08:10 +0800177 return n;
Andy Green1f4267b2013-10-17 08:09:19 +0800178
Andy Greend7340c12014-04-10 14:08:10 +0800179 /*
180 * Newly truncated send. Buffer the remainder (it will get
181 * first priority next time the socket is writable)
182 */
Andy Green54806b12015-12-17 17:03:59 +0800183 lwsl_info("%p new partial sent %d from %d total\n", wsi, n, real_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800184
Andy Greend7340c12014-04-10 14:08:10 +0800185 /*
186 * - if we still have a suitable malloc lying around, use it
187 * - or, if too small, reallocate it
188 * - or, if no buffer, create it
189 */
Andy Green54806b12015-12-17 17:03:59 +0800190 if (!wsi->trunc_alloc || real_len - n > wsi->trunc_alloc_len) {
191 lws_free(wsi->trunc_alloc);
Andy Greend7340c12014-04-10 14:08:10 +0800192
Andy Green54806b12015-12-17 17:03:59 +0800193 wsi->trunc_alloc_len = real_len - n;
194 wsi->trunc_alloc = lws_malloc(real_len - n);
195 if (!wsi->trunc_alloc) {
Andy Greend7340c12014-04-10 14:08:10 +0800196 lwsl_err("truncated send: unable to malloc %d\n",
Andy Greend607bb92015-12-06 05:52:09 +0800197 real_len - n);
Andy Greend7340c12014-04-10 14:08:10 +0800198 return -1;
Andy Greene254d952014-03-23 11:41:15 +0800199 }
Andy Green1f4267b2013-10-17 08:09:19 +0800200 }
Andy Green54806b12015-12-17 17:03:59 +0800201 wsi->trunc_offset = 0;
202 wsi->trunc_len = real_len - n;
203 memcpy(wsi->trunc_alloc, buf + n, real_len - n);
Andy Green1f4267b2013-10-17 08:09:19 +0800204
Andy Greend7340c12014-04-10 14:08:10 +0800205 /* since something buffered, force it to get another chance to send */
Andy Green11c05bf2015-12-16 18:19:08 +0800206 lws_callback_on_writable(wsi);
Andy Greend7340c12014-04-10 14:08:10 +0800207
208 return real_len;
Andy Greenb429d482013-01-16 12:21:29 +0800209}
210
Andy Greenb429d482013-01-16 12:21:29 +0800211/**
Andy Green62304762015-12-04 08:43:54 +0800212 * lws_write() - Apply protocol then write data to client
Andy Greenb429d482013-01-16 12:21:29 +0800213 * @wsi: Websocket instance (available from user callback)
214 * @buf: The data to send. For data being sent on a websocket
215 * connection (ie, not default http), this buffer MUST have
216 * LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
217 * and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
218 * in the buffer after (buf + len). This is so the protocol
219 * header and trailer data can be added in-situ.
220 * @len: Count of the data bytes in the payload starting from buf
221 * @protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one
222 * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
223 * data on a websockets connection. Remember to allow the extra
224 * bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
225 * are used.
226 *
227 * This function provides the way to issue data back to the client
228 * for both http and websocket protocols.
229 *
230 * In the case of sending using websocket protocol, be sure to allocate
231 * valid storage before and after buf as explained above. This scheme
232 * allows maximum efficiency of sending data and protocol in a single
233 * packet while not burdening the user code with any protocol knowledge.
Andy Greenfc7c5e42013-02-23 10:50:10 +0800234 *
235 * Return may be -1 for a fatal error needing connection close, or a
236 * positive number reflecting the amount of bytes actually sent. This
237 * can be less than the requested number of bytes due to OS memory
238 * pressure at any given time.
Andy Greenb429d482013-01-16 12:21:29 +0800239 */
240
Andy Green4b85c1d2015-12-04 11:08:32 +0800241LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf,
Andy Green62304762015-12-04 08:43:54 +0800242 size_t len, enum lws_write_protocol protocol)
Andy Greenb429d482013-01-16 12:21:29 +0800243{
Andy Green54806b12015-12-17 17:03:59 +0800244 int masked7 = wsi->mode == LWSCM_WS_CLIENT;
Andy Greenb429d482013-01-16 12:21:29 +0800245 unsigned char is_masked_bit = 0;
Andy Greend607bb92015-12-06 05:52:09 +0800246 unsigned char *dropmask = NULL;
Andy Greenb429d482013-01-16 12:21:29 +0800247 struct lws_tokens eff_buf;
Andy Greende1a6a52015-12-26 10:07:17 +0800248 int pre = 0, n;
Andy Greend607bb92015-12-06 05:52:09 +0800249 size_t orig_len = len;
Andy Greenb429d482013-01-16 12:21:29 +0800250
Andy Green1f4267b2013-10-17 08:09:19 +0800251 if (len == 0 && protocol != LWS_WRITE_CLOSE &&
Andy Greend607bb92015-12-06 05:52:09 +0800252 protocol != LWS_WRITE_PING && protocol != LWS_WRITE_PONG) {
Andy Green62304762015-12-04 08:43:54 +0800253 lwsl_warn("zero length lws_write attempt\n");
Andy Greenb429d482013-01-16 12:21:29 +0800254 return 0;
255 }
256
Andy Green200f3852014-10-18 12:23:05 +0800257 if (protocol == LWS_WRITE_HTTP ||
258 protocol == LWS_WRITE_HTTP_FINAL ||
259 protocol == LWS_WRITE_HTTP_HEADERS)
Andy Greenb429d482013-01-16 12:21:29 +0800260 goto send_raw;
261
262 /* websocket protocol, either binary or text */
263
Andy Green54806b12015-12-17 17:03:59 +0800264 if (wsi->state != LWSS_ESTABLISHED &&
265 !(wsi->state == LWSS_RETURNED_CLOSE_ALREADY &&
Andrejs Hanins140ac6e2015-11-06 18:18:32 +0200266 protocol == LWS_WRITE_CLOSE))
Andy Greenb429d482013-01-16 12:21:29 +0800267 return -1;
268
Andy Green1f4267b2013-10-17 08:09:19 +0800269 /* if we are continuing a frame that already had its header done */
270
271 if (wsi->u.ws.inside_frame)
272 goto do_more_inside_frame;
273
Andy Green822241c2014-08-18 22:21:51 +0800274 wsi->u.ws.clean_buffer = 1;
Andy Green1f4267b2013-10-17 08:09:19 +0800275
Andy Green1f4267b2013-10-17 08:09:19 +0800276 /*
277 * give a chance to the extensions to modify payload
278 * pre-TX mangling is not allowed to truncate
279 */
Andy Greenb429d482013-01-16 12:21:29 +0800280 eff_buf.token = (char *)buf;
281 eff_buf.token_len = len;
282
Andy Green0303db42013-01-17 14:46:43 +0800283 switch (protocol) {
284 case LWS_WRITE_PING:
285 case LWS_WRITE_PONG:
286 case LWS_WRITE_CLOSE:
287 break;
288 default:
Andy Green54806b12015-12-17 17:03:59 +0800289 if (lws_ext_cb_wsi_active_exts(wsi, LWS_EXT_CALLBACK_PAYLOAD_TX,
290 &eff_buf, 0) < 0)
Andy Green2c24ec02014-04-02 19:45:42 +0800291 return -1;
Andy Greenb429d482013-01-16 12:21:29 +0800292 }
293
Andy Green1f4267b2013-10-17 08:09:19 +0800294 /*
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)
Andy Green27e770b2014-03-23 11:21:51 +0800300 /*
301 * extension recreated it:
302 * need to buffer this if not all sent
303 */
304 wsi->u.ws.clean_buffer = 0;
Andy Green1f4267b2013-10-17 08:09:19 +0800305
Andy Greenb429d482013-01-16 12:21:29 +0800306 buf = (unsigned char *)eff_buf.token;
307 len = eff_buf.token_len;
308
309 switch (wsi->ietf_spec_revision) {
Andy Greenb429d482013-01-16 12:21:29 +0800310 case 13:
311 if (masked7) {
312 pre += 4;
313 dropmask = &buf[0 - pre];
314 is_masked_bit = 0x80;
315 }
Andy Green5738c0e2013-01-21 09:53:35 +0800316
Andy Greenb429d482013-01-16 12:21:29 +0800317 switch (protocol & 0xf) {
318 case LWS_WRITE_TEXT:
Andy Green54806b12015-12-17 17:03:59 +0800319 n = LWSWSOPC_TEXT_FRAME;
Andy Greenb429d482013-01-16 12:21:29 +0800320 break;
321 case LWS_WRITE_BINARY:
Andy Green54806b12015-12-17 17:03:59 +0800322 n = LWSWSOPC_BINARY_FRAME;
Andy Greenb429d482013-01-16 12:21:29 +0800323 break;
324 case LWS_WRITE_CONTINUATION:
Andy Green54806b12015-12-17 17:03:59 +0800325 n = LWSWSOPC_CONTINUATION;
Andy Greenb429d482013-01-16 12:21:29 +0800326 break;
327
328 case LWS_WRITE_CLOSE:
Andy Green54806b12015-12-17 17:03:59 +0800329 n = LWSWSOPC_CLOSE;
Andy Greenb429d482013-01-16 12:21:29 +0800330
331 /*
Andy Green5738c0e2013-01-21 09:53:35 +0800332 * 06+ has a 2-byte status code in network order
333 * we can do this because we demand post-buf
Andy Greenb429d482013-01-16 12:21:29 +0800334 */
335
Andy Green623a98d2013-01-21 11:04:23 +0800336 if (wsi->u.ws.close_reason) {
Andy Green5738c0e2013-01-21 09:53:35 +0800337 /* reason codes count as data bytes */
Andy Green1cc03882015-12-06 08:00:03 +0800338 buf[0] = (unsigned char)(wsi->u.ws.close_reason >> 8);
339 buf[1] = (unsigned char)wsi->u.ws.close_reason;
Andy Green5738c0e2013-01-21 09:53:35 +0800340 len += 2;
Andy Greenb429d482013-01-16 12:21:29 +0800341 }
342 break;
343 case LWS_WRITE_PING:
Andy Green54806b12015-12-17 17:03:59 +0800344 n = LWSWSOPC_PING;
Andy Greenb429d482013-01-16 12:21:29 +0800345 break;
346 case LWS_WRITE_PONG:
Andy Green54806b12015-12-17 17:03:59 +0800347 n = LWSWSOPC_PONG;
Andy Greenb429d482013-01-16 12:21:29 +0800348 break;
349 default:
Andy Greenb5b23192013-02-11 17:13:32 +0800350 lwsl_warn("lws_write: unknown write opc / protocol\n");
Andy Greenb429d482013-01-16 12:21:29 +0800351 return -1;
352 }
353
354 if (!(protocol & LWS_WRITE_NO_FIN))
355 n |= 1 << 7;
356
357 if (len < 126) {
358 pre += 2;
359 buf[-pre] = n;
Andy Green1cc03882015-12-06 08:00:03 +0800360 buf[-pre + 1] = (unsigned char)(len | is_masked_bit);
Andy Greenb429d482013-01-16 12:21:29 +0800361 } else {
362 if (len < 65536) {
363 pre += 4;
364 buf[-pre] = n;
365 buf[-pre + 1] = 126 | is_masked_bit;
Andy Green1cc03882015-12-06 08:00:03 +0800366 buf[-pre + 2] = (unsigned char)(len >> 8);
367 buf[-pre + 3] = (unsigned char)len;
Andy Greenb429d482013-01-16 12:21:29 +0800368 } else {
369 pre += 10;
370 buf[-pre] = n;
371 buf[-pre + 1] = 127 | is_masked_bit;
372#if defined __LP64__
373 buf[-pre + 2] = (len >> 56) & 0x7f;
374 buf[-pre + 3] = len >> 48;
375 buf[-pre + 4] = len >> 40;
376 buf[-pre + 5] = len >> 32;
377#else
378 buf[-pre + 2] = 0;
379 buf[-pre + 3] = 0;
380 buf[-pre + 4] = 0;
381 buf[-pre + 5] = 0;
382#endif
Andy Green1cc03882015-12-06 08:00:03 +0800383 buf[-pre + 6] = (unsigned char)(len >> 24);
384 buf[-pre + 7] = (unsigned char)(len >> 16);
385 buf[-pre + 8] = (unsigned char)(len >> 8);
386 buf[-pre + 9] = (unsigned char)len;
Andy Greenb429d482013-01-16 12:21:29 +0800387 }
388 }
389 break;
390 }
391
Andy Green1f4267b2013-10-17 08:09:19 +0800392do_more_inside_frame:
393
Andy Greenb429d482013-01-16 12:21:29 +0800394 /*
395 * Deal with masking if we are in client -> server direction and
396 * the protocol demands it
397 */
398
Andy Green54806b12015-12-17 17:03:59 +0800399 if (wsi->mode == LWSCM_WS_CLIENT) {
Andy Green1f4267b2013-10-17 08:09:19 +0800400 if (!wsi->u.ws.inside_frame)
Andy Green3ef579b2015-12-04 09:23:56 +0800401 if (lws_0405_frame_mask_generate(wsi)) {
Andy Green27e770b2014-03-23 11:21:51 +0800402 lwsl_err("frame mask generation failed\n");
Andy Green1f4267b2013-10-17 08:09:19 +0800403 return -1;
404 }
Andy Greenb429d482013-01-16 12:21:29 +0800405
Andy Green5738c0e2013-01-21 09:53:35 +0800406 /*
407 * in v7, just mask the payload
408 */
Nikolay Dimitrova8268e72013-12-21 10:22:17 +0800409 if (dropmask) { /* never set if already inside frame */
410 for (n = 4; n < (int)len + 4; n++)
411 dropmask[n] = dropmask[n] ^
Andy Green54806b12015-12-17 17:03:59 +0800412 wsi->u.ws.mask_nonce[
Andy Greenb5b23192013-02-11 17:13:32 +0800413 (wsi->u.ws.frame_mask_index++) & 3];
Andy Green5738c0e2013-01-21 09:53:35 +0800414
Andy Green5738c0e2013-01-21 09:53:35 +0800415 /* copy the frame nonce into place */
Andy Green54806b12015-12-17 17:03:59 +0800416 memcpy(dropmask, wsi->u.ws.mask_nonce, 4);
Nikolay Dimitrova8268e72013-12-21 10:22:17 +0800417 }
Andy Greenb429d482013-01-16 12:21:29 +0800418 }
419
420send_raw:
Andy Green0303db42013-01-17 14:46:43 +0800421 switch (protocol) {
422 case LWS_WRITE_CLOSE:
Andy Greende1a6a52015-12-26 10:07:17 +0800423/* lwsl_hexdump(&buf[-pre], len); */
Andy Green0303db42013-01-17 14:46:43 +0800424 case LWS_WRITE_HTTP:
Andy Green200f3852014-10-18 12:23:05 +0800425 case LWS_WRITE_HTTP_FINAL:
Andy Green024eb6c2014-10-08 12:00:53 +0800426 case LWS_WRITE_HTTP_HEADERS:
Andy Green0303db42013-01-17 14:46:43 +0800427 case LWS_WRITE_PONG:
428 case LWS_WRITE_PING:
Andy Green095d3032014-10-08 12:15:15 +0800429#ifdef LWS_USE_HTTP2
Andy Green54806b12015-12-17 17:03:59 +0800430 if (wsi->mode == LWSCM_HTTP2_SERVING) {
Andy Green2add6342014-10-12 08:38:16 +0800431 unsigned char flags = 0;
432
Andy Green024eb6c2014-10-08 12:00:53 +0800433 n = LWS_HTTP2_FRAME_TYPE_DATA;
Andy Green2add6342014-10-12 08:38:16 +0800434 if (protocol == LWS_WRITE_HTTP_HEADERS) {
Andy Green024eb6c2014-10-08 12:00:53 +0800435 n = LWS_HTTP2_FRAME_TYPE_HEADERS;
Andy Green91b05892014-10-17 08:38:44 +0800436 flags = LWS_HTTP2_FLAG_END_HEADERS;
Andy Green1cea5812014-10-19 07:36:20 +0800437 if (wsi->u.http2.send_END_STREAM)
438 flags |= LWS_HTTP2_FLAG_END_STREAM;
Andy Green2add6342014-10-12 08:38:16 +0800439 }
Andy Green40110e82015-12-14 08:52:03 +0800440
Andy Greende1a6a52015-12-26 10:07:17 +0800441 if ((protocol == LWS_WRITE_HTTP ||
442 protocol == LWS_WRITE_HTTP_FINAL) &&
443 wsi->u.http.content_length) {
Andy Green200f3852014-10-18 12:23:05 +0800444 wsi->u.http.content_remain -= len;
Andy Greende1a6a52015-12-26 10:07:17 +0800445 lwsl_info("%s: content_remain = %lu\n", __func__,
446 wsi->u.http.content_remain);
Andy Green200f3852014-10-18 12:23:05 +0800447 if (!wsi->u.http.content_remain) {
448 lwsl_info("%s: selecting final write mode\n", __func__);
449 protocol = LWS_WRITE_HTTP_FINAL;
450 }
451 }
Andy Green40110e82015-12-14 08:52:03 +0800452
Andy Green1cea5812014-10-19 07:36:20 +0800453 if (protocol == LWS_WRITE_HTTP_FINAL && wsi->u.http2.END_STREAM) {
Andy Green200f3852014-10-18 12:23:05 +0800454 lwsl_info("%s: setting END_STREAM\n", __func__);
455 flags |= LWS_HTTP2_FLAG_END_STREAM;
456 }
457
Andy Greende1a6a52015-12-26 10:07:17 +0800458 return lws_http2_frame_write(wsi, n, flags,
459 wsi->u.http2.my_stream_id, len, buf);
Andy Green024eb6c2014-10-08 12:00:53 +0800460 }
Andy Green095d3032014-10-08 12:15:15 +0800461#endif
Andy Greende1a6a52015-12-26 10:07:17 +0800462 return lws_issue_raw(wsi, (unsigned char *)buf - pre, len + pre);
Andy Green0303db42013-01-17 14:46:43 +0800463 default:
464 break;
Andy Greenb429d482013-01-16 12:21:29 +0800465 }
466
Andy Green1f4267b2013-10-17 08:09:19 +0800467 wsi->u.ws.inside_frame = 1;
468
Andy Greenb429d482013-01-16 12:21:29 +0800469 /*
470 * give any active extensions a chance to munge the buffer
471 * before send. We pass in a pointer to an lws_tokens struct
472 * prepared with the default buffer and content length that's in
473 * there. Rather than rewrite the default buffer, extensions
474 * that expect to grow the buffer can adapt .token to
475 * point to their own per-connection buffer in the extension
476 * user allocation. By default with no extensions or no
477 * extension callback handling, just the normal input buffer is
478 * used then so it is efficient.
479 *
480 * callback returns 1 in case it wants to spill more buffers
Andy Green1f4267b2013-10-17 08:09:19 +0800481 *
482 * This takes care of holding the buffer if send is incomplete, ie,
483 * if wsi->u.ws.clean_buffer is 0 (meaning an extension meddled with
484 * the buffer). If wsi->u.ws.clean_buffer is 1, it will instead
485 * return to the user code how much OF THE USER BUFFER was consumed.
Andy Greenb429d482013-01-16 12:21:29 +0800486 */
487
Andy Greende1a6a52015-12-26 10:07:17 +0800488 n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre);
Andy Green03cf1dd2014-04-01 14:20:44 +0800489 if (n <= 0)
Andy Greenfc7c5e42013-02-23 10:50:10 +0800490 return n;
491
Andy Greende1a6a52015-12-26 10:07:17 +0800492 if (n == (int)len + pre) {
Andy Green1f4267b2013-10-17 08:09:19 +0800493 /* everything in the buffer was handled (or rebuffered...) */
494 wsi->u.ws.inside_frame = 0;
495 return orig_len;
496 }
497
498 /*
499 * it is how many bytes of user buffer got sent... may be < orig_len
500 * in which case callback when writable has already been arranged
Andy Green62304762015-12-04 08:43:54 +0800501 * and user code can call lws_write() again with the rest
Andy Green1f4267b2013-10-17 08:09:19 +0800502 * later.
503 */
504
Andy Greende1a6a52015-12-26 10:07:17 +0800505 return n - pre;
Andy Greenb429d482013-01-16 12:21:29 +0800506}
507
Andy Green11c05bf2015-12-16 18:19:08 +0800508LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
Andy Greenb8b247d2013-01-22 07:20:08 +0800509{
Andy Green11c05bf2015-12-16 18:19:08 +0800510 struct lws_context *context = wsi->context;
Andy Green4e442b72015-12-10 07:58:58 +0800511 unsigned long amount;
512 int n, m;
Andy Greenb8b247d2013-01-22 07:20:08 +0800513
514 while (!lws_send_pipe_choked(wsi)) {
Andy Green54806b12015-12-17 17:03:59 +0800515 if (wsi->trunc_len) {
516 if (lws_issue_raw(wsi, wsi->trunc_alloc +
517 wsi->trunc_offset,
518 wsi->trunc_len) < 0) {
Andy Green4e442b72015-12-10 07:58:58 +0800519 lwsl_info("%s: closing\n", __func__);
Andy Greena1a24d22014-04-10 14:25:24 +0800520 return -1;
521 }
Andy Green2764eba2013-12-09 14:16:17 +0800522 continue;
523 }
524
525 if (wsi->u.http.filepos == wsi->u.http.filelen)
526 goto all_sent;
527
Andy Green891628b2015-12-11 13:12:58 +0800528 if (lws_plat_file_read(wsi, wsi->u.http.fd, &amount,
Andy Green54806b12015-12-17 17:03:59 +0800529 context->serv_buf,
530 sizeof(context->serv_buf)) < 0)
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100531 return -1; /* caller will close */
Andy Green4e442b72015-12-10 07:58:58 +0800532
533 n = (int)amount;
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100534 if (n) {
Andy Greend607bb92015-12-06 05:52:09 +0800535 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
536 AWAITING_TIMEOUT);
Andy Green200f3852014-10-18 12:23:05 +0800537 wsi->u.http.filepos += n;
Andy Green54806b12015-12-17 17:03:59 +0800538 m = lws_write(wsi, context->serv_buf, n,
Andy Greend607bb92015-12-06 05:52:09 +0800539 wsi->u.http.filepos == wsi->u.http.filelen ?
540 LWS_WRITE_HTTP_FINAL : LWS_WRITE_HTTP);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800541 if (m < 0)
542 return -1;
543
Andy Green158e8042014-04-02 14:25:10 +0800544 if (m != n)
Andy Greenfc7c5e42013-02-23 10:50:10 +0800545 /* adjust for what was not sent */
Andy Green54806b12015-12-17 17:03:59 +0800546 if (lws_plat_file_seek_cur(wsi, wsi->u.http.fd,
Andy Green95181d92015-12-10 12:50:10 +0800547 m - n) ==
548 (unsigned long)-1)
Andy Green7ef4b2e2014-11-30 13:00:47 +0800549 return -1;
Andy Greenb8b247d2013-01-22 07:20:08 +0800550 }
Andy Green2764eba2013-12-09 14:16:17 +0800551all_sent:
Andy Green54806b12015-12-17 17:03:59 +0800552 if (!wsi->trunc_len && wsi->u.http.filepos == wsi->u.http.filelen) {
553 wsi->state = LWSS_HTTP;
Andy Greenb8b247d2013-01-22 07:20:08 +0800554
Andy Greenc6f95d32015-10-27 07:07:14 +0800555 /* we might be in keepalive, so close it off here */
Andy Green891628b2015-12-11 13:12:58 +0800556 lws_plat_file_close(wsi, wsi->u.http.fd);
Andy Greenc6f95d32015-10-27 07:07:14 +0800557 wsi->u.http.fd = LWS_INVALID_FILE;
558
Andy Greenb8b247d2013-01-22 07:20:08 +0800559 if (wsi->protocol->callback)
David Gauchard6c582282013-06-29 10:24:16 +0800560 /* ignore callback returned value */
561 user_callback_handle_rxflow(
Andy Green00c6d152015-12-17 07:54:44 +0800562 wsi->protocol->callback, wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800563 LWS_CALLBACK_HTTP_FILE_COMPLETION,
564 wsi->user_space, NULL, 0);
David Gauchard6c582282013-06-29 10:24:16 +0800565 return 1; /* >0 indicates completed */
Andy Greenb8b247d2013-01-22 07:20:08 +0800566 }
567 }
568
Andy Green2764eba2013-12-09 14:16:17 +0800569 lwsl_info("choked before able to send whole file (post)\n");
Andy Green11c05bf2015-12-16 18:19:08 +0800570 lws_callback_on_writable(wsi);
Andy Greenb8b247d2013-01-22 07:20:08 +0800571
David Gauchard6c582282013-06-29 10:24:16 +0800572 return 0; /* indicates further processing must be done */
Andy Greenb8b247d2013-01-22 07:20:08 +0800573}
Andy Green02138122014-04-06 06:26:35 +0100574
Andy Green11f27342015-11-08 12:10:26 +0800575#if LWS_POSIX
Andy Green02138122014-04-06 06:26:35 +0100576LWS_VISIBLE int
Andy Green6b5de702015-12-15 21:15:58 +0800577lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
Andy Green02138122014-04-06 06:26:35 +0100578{
579 int n;
580
Stephan Eberleb820e2c2015-10-23 08:10:55 +0200581 n = recv(wsi->sock, (char *)buf, len, 0);
Andy Greend7340c12014-04-10 14:08:10 +0800582 if (n >= 0)
583 return n;
Andy Green11f27342015-11-08 12:10:26 +0800584#if LWS_POSIX
Mark Liknessdbe624d2015-10-15 21:21:06 +0800585 if (LWS_ERRNO == LWS_EAGAIN ||
586 LWS_ERRNO == LWS_EWOULDBLOCK ||
587 LWS_ERRNO == LWS_EINTR)
588 return LWS_SSL_CAPABLE_MORE_SERVICE;
Andy Green8c0d3c02015-11-02 20:34:12 +0800589#endif
Andy Greend7340c12014-04-10 14:08:10 +0800590 lwsl_warn("error on reading from skt\n");
591 return LWS_SSL_CAPABLE_ERROR;
Andy Green02138122014-04-06 06:26:35 +0100592}
593
594LWS_VISIBLE int
Andy Green4b85c1d2015-12-04 11:08:32 +0800595lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
Andy Green02138122014-04-06 06:26:35 +0100596{
Andy Green11f27342015-11-08 12:10:26 +0800597 int n = 0;
Andy Green8c0d3c02015-11-02 20:34:12 +0800598
599#if LWS_POSIX
Stephan Eberleb820e2c2015-10-23 08:10:55 +0200600 n = send(wsi->sock, (char *)buf, len, MSG_NOSIGNAL);
Andy Greend7340c12014-04-10 14:08:10 +0800601 if (n >= 0)
602 return n;
Andy Green02138122014-04-06 06:26:35 +0100603
Andy Greend7340c12014-04-10 14:08:10 +0800604 if (LWS_ERRNO == LWS_EAGAIN ||
605 LWS_ERRNO == LWS_EWOULDBLOCK ||
606 LWS_ERRNO == LWS_EINTR) {
607 if (LWS_ERRNO == LWS_EWOULDBLOCK)
608 lws_set_blocking_send(wsi);
609
610 return LWS_SSL_CAPABLE_MORE_SERVICE;
611 }
Andy Green8c0d3c02015-11-02 20:34:12 +0800612#else
613 (void)n;
614 (void)wsi;
615 (void)buf;
616 (void)len;
617 // !!!
618#endif
Andy Green40110e82015-12-14 08:52:03 +0800619
Andy Greend7340c12014-04-10 14:08:10 +0800620 lwsl_debug("ERROR writing len %d to skt %d\n", len, n);
621 return LWS_SSL_CAPABLE_ERROR;
Andy Green02138122014-04-06 06:26:35 +0100622}
Andy Green11f27342015-11-08 12:10:26 +0800623#endif
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +0200624LWS_VISIBLE int
Andy Green4b85c1d2015-12-04 11:08:32 +0800625lws_ssl_pending_no_ssl(struct lws *wsi)
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +0200626{
Andy Green2cd30742015-11-02 13:10:33 +0800627 (void)wsi;
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +0200628 return 0;
629}