blob: 0cd044dc5bac18fd50fb39309966db33a0404d06 [file] [log] [blame]
Andy Greenb429d482013-01-16 12:21:29 +08001/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
Andy Green27e770b2014-03-23 11:21:51 +08004 * Copyright (C) 2010-2014 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 Green62304762015-12-04 08:43:54 +080031 n = lws_get_random(wsi->protocol->owning_server,
Andy Greenb5b23192013-02-11 17:13:32 +080032 wsi->u.ws.frame_masking_nonce_04, 4);
Andy Greenb429d482013-01-16 12:21:29 +080033 if (n != 4) {
34 lwsl_parser("Unable to read from random device %s %d\n",
35 SYSTEM_RANDOM_FILEPATH, n);
36 return 1;
37 }
38
39 /* start masking from first byte of masking key buffer */
Andy Green623a98d2013-01-21 11:04:23 +080040 wsi->u.ws.frame_mask_index = 0;
Andy Greenb429d482013-01-16 12:21:29 +080041
Andy Greenb429d482013-01-16 12:21:29 +080042 return 0;
43}
44
Andy Greencf3590e2013-01-16 14:35:27 +080045#ifdef _DEBUG
Andy Greenb429d482013-01-16 12:21:29 +080046
Peter Pentchev9a4fef72013-03-30 09:52:21 +080047LWS_VISIBLE void lwsl_hexdump(void *vbuf, size_t len)
Andy Greenb429d482013-01-16 12:21:29 +080048{
Andy Green11f27342015-11-08 12:10:26 +080049 unsigned int n;
50 unsigned int m;
51 unsigned int start;
Andy Greencf3590e2013-01-16 14:35:27 +080052 unsigned char *buf = (unsigned char *)vbuf;
53 char line[80];
54 char *p;
Andy Greenb429d482013-01-16 12:21:29 +080055
56 lwsl_parser("\n");
57
58 for (n = 0; n < len;) {
59 start = n;
Andy Greencf3590e2013-01-16 14:35:27 +080060 p = line;
Andy Greenb429d482013-01-16 12:21:29 +080061
Andy Greencf3590e2013-01-16 14:35:27 +080062 p += sprintf(p, "%04X: ", start);
Andy Greenb429d482013-01-16 12:21:29 +080063
64 for (m = 0; m < 16 && n < len; m++)
Andy Greencf3590e2013-01-16 14:35:27 +080065 p += sprintf(p, "%02X ", buf[n++]);
Andy Greenb429d482013-01-16 12:21:29 +080066 while (m++ < 16)
Andy Greencf3590e2013-01-16 14:35:27 +080067 p += sprintf(p, " ");
Andy Greenb429d482013-01-16 12:21:29 +080068
Andy Greencf3590e2013-01-16 14:35:27 +080069 p += sprintf(p, " ");
Andy Greenb429d482013-01-16 12:21:29 +080070
71 for (m = 0; m < 16 && (start + m) < len; m++) {
Andy Green3182ece2013-01-20 17:08:31 +080072 if (buf[start + m] >= ' ' && buf[start + m] < 127)
Andy Greencf3590e2013-01-16 14:35:27 +080073 *p++ = buf[start + m];
Andy Greenb429d482013-01-16 12:21:29 +080074 else
Andy Greencf3590e2013-01-16 14:35:27 +080075 *p++ = '.';
Andy Greenb429d482013-01-16 12:21:29 +080076 }
77 while (m++ < 16)
Andy Greencf3590e2013-01-16 14:35:27 +080078 *p++ = ' ';
Andy Greenb429d482013-01-16 12:21:29 +080079
Andy Greencf3590e2013-01-16 14:35:27 +080080 *p++ = '\n';
81 *p = '\0';
Andy Green3182ece2013-01-20 17:08:31 +080082 lwsl_debug("%s", line);
Andy Greenb429d482013-01-16 12:21:29 +080083 }
84 lwsl_debug("\n");
85}
86
Andy Greencf3590e2013-01-16 14:35:27 +080087#endif
88
Andy Greenfc7c5e42013-02-23 10:50:10 +080089/*
Andy Green1f4267b2013-10-17 08:09:19 +080090 * notice this returns number of bytes consumed, or -1
Andy Greenfc7c5e42013-02-23 10:50:10 +080091 */
92
Andy Green4b85c1d2015-12-04 11:08:32 +080093int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
Andy Greenb429d482013-01-16 12:21:29 +080094{
Andy Green4b85c1d2015-12-04 11:08:32 +080095 struct lws_context *context = wsi->protocol->owning_server;
Andy Greenb429d482013-01-16 12:21:29 +080096 int n;
mroszko793e7c02013-12-10 21:15:00 +080097 size_t real_len = len;
Andy Greenb429d482013-01-16 12:21:29 +080098 int m;
Andy Green03cf1dd2014-04-01 14:20:44 +080099
100 if (!len)
101 return 0;
Andy Greena1a24d22014-04-10 14:25:24 +0800102 /* just ignore sends after we cleared the truncation buffer */
103 if (wsi->state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
Andy Green11f27342015-11-08 12:10:26 +0800104 !wsi->truncated_send_len)
Andy Greena1a24d22014-04-10 14:25:24 +0800105 return len;
Andy Green2764eba2013-12-09 14:16:17 +0800106
Andy Greene254d952014-03-23 11:41:15 +0800107 if (wsi->truncated_send_len && (buf < wsi->truncated_send_malloc ||
Andy Green11f27342015-11-08 12:10:26 +0800108 buf > (wsi->truncated_send_malloc + wsi->truncated_send_len +
109 wsi->truncated_send_offset))) {
Andy Green27e770b2014-03-23 11:21:51 +0800110 lwsl_err("****** %x Sending new, pending truncated ...\n", wsi);
Andy Green2764eba2013-12-09 14:16:17 +0800111 assert(0);
112 }
Andy Greenb429d482013-01-16 12:21:29 +0800113
Andy Green2c24ec02014-04-02 19:45:42 +0800114 m = lws_ext_callback_for_each_active(wsi,
115 LWS_EXT_CALLBACK_PACKET_TX_DO_SEND, &buf, len);
116 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
126 /*
127 * nope, send it on the socket directly
128 */
Andy Greene000a702013-01-29 12:37:35 +0800129 lws_latency_pre(context, wsi);
Andy Green02138122014-04-06 06:26:35 +0100130 n = lws_ssl_capable_write(wsi, buf, len);
Andy Green2cd30742015-11-02 13:10:33 +0800131 lws_latency(context, wsi, "send lws_issue_raw", n, (unsigned int)n == len);
Andy Greend7340c12014-04-10 14:08:10 +0800132
Andy Green02138122014-04-06 06:26:35 +0100133 switch (n) {
134 case LWS_SSL_CAPABLE_ERROR:
Andy Green11f27342015-11-08 12:10:26 +0800135 lwsl_err("%s: wsi %p: LWS_SSL_CAPABLE_ERROR\n", __func__, (void *)wsi);
Andy Green1e499182014-10-16 08:23:46 +0800136 /* we're going to close, let close know sends aren't possible */
137 wsi->socket_is_permanently_unusable = 1;
Andy Green02138122014-04-06 06:26:35 +0100138 return -1;
139 case LWS_SSL_CAPABLE_MORE_SERVICE:
Andy Greend7340c12014-04-10 14:08:10 +0800140 /* nothing got sent, not fatal, retry the whole thing later */
Andy Green02138122014-04-06 06:26:35 +0100141 n = 0;
Andy Greend7340c12014-04-10 14:08:10 +0800142 break;
Andy Greenb429d482013-01-16 12:21:29 +0800143 }
Andy Green1f4267b2013-10-17 08:09:19 +0800144
145handle_truncated_send:
Andy Green1f4267b2013-10-17 08:09:19 +0800146 /*
Andy Greend7340c12014-04-10 14:08:10 +0800147 * we were already handling a truncated send?
Andy Green1f4267b2013-10-17 08:09:19 +0800148 */
Andy Greene254d952014-03-23 11:41:15 +0800149 if (wsi->truncated_send_len) {
Andy Green27e770b2014-03-23 11:21:51 +0800150 lwsl_info("***** %x partial send moved on by %d (vs %d)\n",
151 wsi, n, real_len);
Andy Green2764eba2013-12-09 14:16:17 +0800152 wsi->truncated_send_offset += n;
153 wsi->truncated_send_len -= n;
Andy Green1f4267b2013-10-17 08:09:19 +0800154
Andy Green2764eba2013-12-09 14:16:17 +0800155 if (!wsi->truncated_send_len) {
156 lwsl_info("***** %x partial send completed\n", wsi);
Andy Greene254d952014-03-23 11:41:15 +0800157 /* done with it, but don't free it */
Andy Green2764eba2013-12-09 14:16:17 +0800158 n = real_len;
Andy Greenf4ffc1e2014-04-10 17:06:59 +0800159 if (wsi->state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
Andy Greena1a24d22014-04-10 14:25:24 +0800160 lwsl_info("***** %x signalling to close now\n", wsi);
161 return -1; /* retry closing now */
Andy Greenf4ffc1e2014-04-10 17:06:59 +0800162 }
Andy Green15d56dd2014-04-10 11:23:18 +0800163 }
164 /* always callback on writeable */
Andy Green62304762015-12-04 08:43:54 +0800165 lws_callback_on_writable(
Andy Green1f4267b2013-10-17 08:09:19 +0800166 wsi->protocol->owning_server, wsi);
167
168 return n;
169 }
170
Andy Green2cd30742015-11-02 13:10:33 +0800171 if ((unsigned int)n == real_len)
Andy Greend7340c12014-04-10 14:08:10 +0800172 /* what we just sent went out cleanly */
173 return n;
Andy Green1f4267b2013-10-17 08:09:19 +0800174
Andy Greend7340c12014-04-10 14:08:10 +0800175 if (n && wsi->u.ws.clean_buffer)
Andy Green1f4267b2013-10-17 08:09:19 +0800176 /*
Andy Greend7340c12014-04-10 14:08:10 +0800177 * This buffer unaffected by extension rewriting.
178 * It means the user code is expected to deal with
179 * partial sends. (lws knows the header was already
180 * sent, so on next send will just resume sending
181 * payload)
Andy Green1f4267b2013-10-17 08:09:19 +0800182 */
Andy Greend7340c12014-04-10 14:08:10 +0800183 return n;
Andy Green1f4267b2013-10-17 08:09:19 +0800184
Andy Greend7340c12014-04-10 14:08:10 +0800185 /*
186 * Newly truncated send. Buffer the remainder (it will get
187 * first priority next time the socket is writable)
188 */
189 lwsl_info("***** %x new partial sent %d from %d total\n",
190 wsi, n, real_len);
Andy Green1f4267b2013-10-17 08:09:19 +0800191
Andy Greend7340c12014-04-10 14:08:10 +0800192 /*
193 * - if we still have a suitable malloc lying around, use it
194 * - or, if too small, reallocate it
195 * - or, if no buffer, create it
196 */
197 if (!wsi->truncated_send_malloc ||
198 real_len - n > wsi->truncated_send_allocation) {
Alejandro Mery6ff28242014-12-04 23:59:35 +0100199 lws_free(wsi->truncated_send_malloc);
Andy Greend7340c12014-04-10 14:08:10 +0800200
201 wsi->truncated_send_allocation = real_len - n;
Alejandro Mery6ff28242014-12-04 23:59:35 +0100202 wsi->truncated_send_malloc = lws_malloc(real_len - n);
Andy Greend7340c12014-04-10 14:08:10 +0800203 if (!wsi->truncated_send_malloc) {
204 lwsl_err("truncated send: unable to malloc %d\n",
205 real_len - n);
206 return -1;
Andy Greene254d952014-03-23 11:41:15 +0800207 }
Andy Green1f4267b2013-10-17 08:09:19 +0800208 }
Andy Greend7340c12014-04-10 14:08:10 +0800209 wsi->truncated_send_offset = 0;
210 wsi->truncated_send_len = real_len - n;
211 memcpy(wsi->truncated_send_malloc, buf + n, real_len - n);
Andy Green1f4267b2013-10-17 08:09:19 +0800212
Andy Greend7340c12014-04-10 14:08:10 +0800213 /* since something buffered, force it to get another chance to send */
Andy Green62304762015-12-04 08:43:54 +0800214 lws_callback_on_writable(wsi->protocol->owning_server, wsi);
Andy Greend7340c12014-04-10 14:08:10 +0800215
216 return real_len;
Andy Greenb429d482013-01-16 12:21:29 +0800217}
218
Andy Greenb429d482013-01-16 12:21:29 +0800219/**
Andy Green62304762015-12-04 08:43:54 +0800220 * lws_write() - Apply protocol then write data to client
Andy Greenb429d482013-01-16 12:21:29 +0800221 * @wsi: Websocket instance (available from user callback)
222 * @buf: The data to send. For data being sent on a websocket
223 * connection (ie, not default http), this buffer MUST have
224 * LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
225 * and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
226 * in the buffer after (buf + len). This is so the protocol
227 * header and trailer data can be added in-situ.
228 * @len: Count of the data bytes in the payload starting from buf
229 * @protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one
230 * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
231 * data on a websockets connection. Remember to allow the extra
232 * bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
233 * are used.
234 *
235 * This function provides the way to issue data back to the client
236 * for both http and websocket protocols.
237 *
238 * In the case of sending using websocket protocol, be sure to allocate
239 * valid storage before and after buf as explained above. This scheme
240 * allows maximum efficiency of sending data and protocol in a single
241 * packet while not burdening the user code with any protocol knowledge.
Andy Greenfc7c5e42013-02-23 10:50:10 +0800242 *
243 * Return may be -1 for a fatal error needing connection close, or a
244 * positive number reflecting the amount of bytes actually sent. This
245 * can be less than the requested number of bytes due to OS memory
246 * pressure at any given time.
Andy Greenb429d482013-01-16 12:21:29 +0800247 */
248
Andy Green4b85c1d2015-12-04 11:08:32 +0800249LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf,
Andy Green62304762015-12-04 08:43:54 +0800250 size_t len, enum lws_write_protocol protocol)
Andy Greenb429d482013-01-16 12:21:29 +0800251{
252 int n;
253 int pre = 0;
254 int post = 0;
Andy Green5738c0e2013-01-21 09:53:35 +0800255 int masked7 = wsi->mode == LWS_CONNMODE_WS_CLIENT;
Andy Greenb429d482013-01-16 12:21:29 +0800256 unsigned char *dropmask = NULL;
257 unsigned char is_masked_bit = 0;
Andy Greenfc7c5e42013-02-23 10:50:10 +0800258 size_t orig_len = len;
Andy Greenb429d482013-01-16 12:21:29 +0800259 struct lws_tokens eff_buf;
Andy Greenb429d482013-01-16 12:21:29 +0800260
Andy Green1f4267b2013-10-17 08:09:19 +0800261 if (len == 0 && protocol != LWS_WRITE_CLOSE &&
262 protocol != LWS_WRITE_PING && protocol != LWS_WRITE_PONG) {
Andy Green62304762015-12-04 08:43:54 +0800263 lwsl_warn("zero length lws_write attempt\n");
Andy Greenb429d482013-01-16 12:21:29 +0800264 return 0;
265 }
266
Andy Green200f3852014-10-18 12:23:05 +0800267 if (protocol == LWS_WRITE_HTTP ||
268 protocol == LWS_WRITE_HTTP_FINAL ||
269 protocol == LWS_WRITE_HTTP_HEADERS)
Andy Greenb429d482013-01-16 12:21:29 +0800270 goto send_raw;
271
272 /* websocket protocol, either binary or text */
273
Andrejs Hanins140ac6e2015-11-06 18:18:32 +0200274 if (wsi->state != WSI_STATE_ESTABLISHED &&
275 !(wsi->state == WSI_STATE_RETURNED_CLOSE_ALREADY &&
276 protocol == LWS_WRITE_CLOSE))
Andy Greenb429d482013-01-16 12:21:29 +0800277 return -1;
278
Andy Green1f4267b2013-10-17 08:09:19 +0800279 /* if we are continuing a frame that already had its header done */
280
281 if (wsi->u.ws.inside_frame)
282 goto do_more_inside_frame;
283
Andy Green822241c2014-08-18 22:21:51 +0800284 wsi->u.ws.clean_buffer = 1;
Andy Green1f4267b2013-10-17 08:09:19 +0800285
Andy Green1f4267b2013-10-17 08:09:19 +0800286 /*
287 * give a chance to the extensions to modify payload
288 * pre-TX mangling is not allowed to truncate
289 */
Andy Greenb429d482013-01-16 12:21:29 +0800290 eff_buf.token = (char *)buf;
291 eff_buf.token_len = len;
292
Andy Green0303db42013-01-17 14:46:43 +0800293 switch (protocol) {
294 case LWS_WRITE_PING:
295 case LWS_WRITE_PONG:
296 case LWS_WRITE_CLOSE:
297 break;
298 default:
Andy Green2c24ec02014-04-02 19:45:42 +0800299 if (lws_ext_callback_for_each_active(wsi,
300 LWS_EXT_CALLBACK_PAYLOAD_TX, &eff_buf, 0) < 0)
301 return -1;
Andy Greenb429d482013-01-16 12:21:29 +0800302 }
303
Andy Green1f4267b2013-10-17 08:09:19 +0800304 /*
305 * an extension did something we need to keep... for example, if
306 * compression extension, it has already updated its state according
307 * to this being issued
308 */
309 if ((char *)buf != eff_buf.token)
Andy Green27e770b2014-03-23 11:21:51 +0800310 /*
311 * extension recreated it:
312 * need to buffer this if not all sent
313 */
314 wsi->u.ws.clean_buffer = 0;
Andy Green1f4267b2013-10-17 08:09:19 +0800315
Andy Greenb429d482013-01-16 12:21:29 +0800316 buf = (unsigned char *)eff_buf.token;
317 len = eff_buf.token_len;
318
319 switch (wsi->ietf_spec_revision) {
Andy Greenb429d482013-01-16 12:21:29 +0800320 case 13:
Andy Green1f4267b2013-10-17 08:09:19 +0800321
Andy Greenb429d482013-01-16 12:21:29 +0800322 if (masked7) {
323 pre += 4;
324 dropmask = &buf[0 - pre];
325 is_masked_bit = 0x80;
326 }
Andy Green5738c0e2013-01-21 09:53:35 +0800327
Andy Greenb429d482013-01-16 12:21:29 +0800328 switch (protocol & 0xf) {
329 case LWS_WRITE_TEXT:
Andy Green5738c0e2013-01-21 09:53:35 +0800330 n = LWS_WS_OPCODE_07__TEXT_FRAME;
Andy Greenb429d482013-01-16 12:21:29 +0800331 break;
332 case LWS_WRITE_BINARY:
Andy Green5738c0e2013-01-21 09:53:35 +0800333 n = LWS_WS_OPCODE_07__BINARY_FRAME;
Andy Greenb429d482013-01-16 12:21:29 +0800334 break;
335 case LWS_WRITE_CONTINUATION:
Andy Green5738c0e2013-01-21 09:53:35 +0800336 n = LWS_WS_OPCODE_07__CONTINUATION;
Andy Greenb429d482013-01-16 12:21:29 +0800337 break;
338
339 case LWS_WRITE_CLOSE:
Andy Green5738c0e2013-01-21 09:53:35 +0800340 n = LWS_WS_OPCODE_07__CLOSE;
Andy Greenb429d482013-01-16 12:21:29 +0800341
342 /*
Andy Green5738c0e2013-01-21 09:53:35 +0800343 * 06+ has a 2-byte status code in network order
344 * we can do this because we demand post-buf
Andy Greenb429d482013-01-16 12:21:29 +0800345 */
346
Andy Green623a98d2013-01-21 11:04:23 +0800347 if (wsi->u.ws.close_reason) {
Andy Green5738c0e2013-01-21 09:53:35 +0800348 /* reason codes count as data bytes */
349 buf -= 2;
Andy Green623a98d2013-01-21 11:04:23 +0800350 buf[0] = wsi->u.ws.close_reason >> 8;
351 buf[1] = wsi->u.ws.close_reason;
Andy Green5738c0e2013-01-21 09:53:35 +0800352 len += 2;
Andy Greenb429d482013-01-16 12:21:29 +0800353 }
354 break;
355 case LWS_WRITE_PING:
Andy Green5738c0e2013-01-21 09:53:35 +0800356 n = LWS_WS_OPCODE_07__PING;
Andy Greenb429d482013-01-16 12:21:29 +0800357 break;
358 case LWS_WRITE_PONG:
Andy Green5738c0e2013-01-21 09:53:35 +0800359 n = LWS_WS_OPCODE_07__PONG;
Andy Greenb429d482013-01-16 12:21:29 +0800360 break;
361 default:
Andy Greenb5b23192013-02-11 17:13:32 +0800362 lwsl_warn("lws_write: unknown write opc / protocol\n");
Andy Greenb429d482013-01-16 12:21:29 +0800363 return -1;
364 }
365
366 if (!(protocol & LWS_WRITE_NO_FIN))
367 n |= 1 << 7;
368
369 if (len < 126) {
370 pre += 2;
371 buf[-pre] = n;
372 buf[-pre + 1] = len | is_masked_bit;
373 } else {
374 if (len < 65536) {
375 pre += 4;
376 buf[-pre] = n;
377 buf[-pre + 1] = 126 | is_masked_bit;
378 buf[-pre + 2] = len >> 8;
379 buf[-pre + 3] = len;
380 } else {
381 pre += 10;
382 buf[-pre] = n;
383 buf[-pre + 1] = 127 | is_masked_bit;
384#if defined __LP64__
385 buf[-pre + 2] = (len >> 56) & 0x7f;
386 buf[-pre + 3] = len >> 48;
387 buf[-pre + 4] = len >> 40;
388 buf[-pre + 5] = len >> 32;
389#else
390 buf[-pre + 2] = 0;
391 buf[-pre + 3] = 0;
392 buf[-pre + 4] = 0;
393 buf[-pre + 5] = 0;
394#endif
395 buf[-pre + 6] = len >> 24;
396 buf[-pre + 7] = len >> 16;
397 buf[-pre + 8] = len >> 8;
398 buf[-pre + 9] = len;
399 }
400 }
401 break;
402 }
403
Andy Green1f4267b2013-10-17 08:09:19 +0800404do_more_inside_frame:
405
Andy Greenb429d482013-01-16 12:21:29 +0800406 /*
407 * Deal with masking if we are in client -> server direction and
408 * the protocol demands it
409 */
410
Andy Green5738c0e2013-01-21 09:53:35 +0800411 if (wsi->mode == LWS_CONNMODE_WS_CLIENT) {
Andy Greenb429d482013-01-16 12:21:29 +0800412
Andy Green1f4267b2013-10-17 08:09:19 +0800413 if (!wsi->u.ws.inside_frame)
Andy Green3ef579b2015-12-04 09:23:56 +0800414 if (lws_0405_frame_mask_generate(wsi)) {
Andy Green27e770b2014-03-23 11:21:51 +0800415 lwsl_err("frame mask generation failed\n");
Andy Green1f4267b2013-10-17 08:09:19 +0800416 return -1;
417 }
Andy Greenb429d482013-01-16 12:21:29 +0800418
Andy Green5738c0e2013-01-21 09:53:35 +0800419 /*
420 * in v7, just mask the payload
421 */
Nikolay Dimitrova8268e72013-12-21 10:22:17 +0800422 if (dropmask) { /* never set if already inside frame */
423 for (n = 4; n < (int)len + 4; n++)
424 dropmask[n] = dropmask[n] ^
Andy Greenb5b23192013-02-11 17:13:32 +0800425 wsi->u.ws.frame_masking_nonce_04[
426 (wsi->u.ws.frame_mask_index++) & 3];
Andy Green5738c0e2013-01-21 09:53:35 +0800427
Andy Green5738c0e2013-01-21 09:53:35 +0800428 /* copy the frame nonce into place */
Andy Green1f4267b2013-10-17 08:09:19 +0800429 memcpy(dropmask, wsi->u.ws.frame_masking_nonce_04, 4);
Nikolay Dimitrova8268e72013-12-21 10:22:17 +0800430 }
Andy Greenb429d482013-01-16 12:21:29 +0800431 }
432
433send_raw:
Andy Green0303db42013-01-17 14:46:43 +0800434 switch (protocol) {
435 case LWS_WRITE_CLOSE:
Andy Greenb5b23192013-02-11 17:13:32 +0800436/* lwsl_hexdump(&buf[-pre], len + post); */
Andy Green0303db42013-01-17 14:46:43 +0800437 case LWS_WRITE_HTTP:
Andy Green200f3852014-10-18 12:23:05 +0800438 case LWS_WRITE_HTTP_FINAL:
Andy Green024eb6c2014-10-08 12:00:53 +0800439 case LWS_WRITE_HTTP_HEADERS:
Andy Green0303db42013-01-17 14:46:43 +0800440 case LWS_WRITE_PONG:
441 case LWS_WRITE_PING:
Andy Green095d3032014-10-08 12:15:15 +0800442#ifdef LWS_USE_HTTP2
Andy Green024eb6c2014-10-08 12:00:53 +0800443 if (wsi->mode == LWS_CONNMODE_HTTP2_SERVING) {
Andy Green2add6342014-10-12 08:38:16 +0800444 unsigned char flags = 0;
445
Andy Green024eb6c2014-10-08 12:00:53 +0800446 n = LWS_HTTP2_FRAME_TYPE_DATA;
Andy Green2add6342014-10-12 08:38:16 +0800447 if (protocol == LWS_WRITE_HTTP_HEADERS) {
Andy Green024eb6c2014-10-08 12:00:53 +0800448 n = LWS_HTTP2_FRAME_TYPE_HEADERS;
Andy Green91b05892014-10-17 08:38:44 +0800449 flags = LWS_HTTP2_FLAG_END_HEADERS;
Andy Green1cea5812014-10-19 07:36:20 +0800450 if (wsi->u.http2.send_END_STREAM)
451 flags |= LWS_HTTP2_FLAG_END_STREAM;
Andy Green2add6342014-10-12 08:38:16 +0800452 }
Andy Green200f3852014-10-18 12:23:05 +0800453
454 if ((protocol == LWS_WRITE_HTTP || protocol == LWS_WRITE_HTTP_FINAL) && wsi->u.http.content_length) {
455 wsi->u.http.content_remain -= len;
456 lwsl_info("%s: content_remain = %lu\n", __func__, wsi->u.http.content_remain);
457 if (!wsi->u.http.content_remain) {
458 lwsl_info("%s: selecting final write mode\n", __func__);
459 protocol = LWS_WRITE_HTTP_FINAL;
460 }
461 }
462
Andy Green1cea5812014-10-19 07:36:20 +0800463 if (protocol == LWS_WRITE_HTTP_FINAL && wsi->u.http2.END_STREAM) {
Andy Green200f3852014-10-18 12:23:05 +0800464 lwsl_info("%s: setting END_STREAM\n", __func__);
465 flags |= LWS_HTTP2_FLAG_END_STREAM;
466 }
467
Andy Green2add6342014-10-12 08:38:16 +0800468 return lws_http2_frame_write(wsi, n, flags, wsi->u.http2.my_stream_id, len, buf);
Andy Green024eb6c2014-10-08 12:00:53 +0800469 }
Andy Green095d3032014-10-08 12:15:15 +0800470#endif
Andy Greenfc7c5e42013-02-23 10:50:10 +0800471 return lws_issue_raw(wsi, (unsigned char *)buf - pre,
472 len + pre + post);
Andy Green0303db42013-01-17 14:46:43 +0800473 default:
474 break;
Andy Greenb429d482013-01-16 12:21:29 +0800475 }
476
Andy Green1f4267b2013-10-17 08:09:19 +0800477 wsi->u.ws.inside_frame = 1;
478
Andy Greenb429d482013-01-16 12:21:29 +0800479 /*
480 * give any active extensions a chance to munge the buffer
481 * before send. We pass in a pointer to an lws_tokens struct
482 * prepared with the default buffer and content length that's in
483 * there. Rather than rewrite the default buffer, extensions
484 * that expect to grow the buffer can adapt .token to
485 * point to their own per-connection buffer in the extension
486 * user allocation. By default with no extensions or no
487 * extension callback handling, just the normal input buffer is
488 * used then so it is efficient.
489 *
490 * callback returns 1 in case it wants to spill more buffers
Andy Green1f4267b2013-10-17 08:09:19 +0800491 *
492 * This takes care of holding the buffer if send is incomplete, ie,
493 * if wsi->u.ws.clean_buffer is 0 (meaning an extension meddled with
494 * the buffer). If wsi->u.ws.clean_buffer is 1, it will instead
495 * return to the user code how much OF THE USER BUFFER was consumed.
Andy Greenb429d482013-01-16 12:21:29 +0800496 */
497
Andy Greenfc7c5e42013-02-23 10:50:10 +0800498 n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre + post);
Andy Green03cf1dd2014-04-01 14:20:44 +0800499 if (n <= 0)
Andy Greenfc7c5e42013-02-23 10:50:10 +0800500 return n;
501
Andy Green2cd30742015-11-02 13:10:33 +0800502 if (n == (int)len + pre + post) {
Andy Green1f4267b2013-10-17 08:09:19 +0800503 /* everything in the buffer was handled (or rebuffered...) */
504 wsi->u.ws.inside_frame = 0;
505 return orig_len;
506 }
507
508 /*
509 * it is how many bytes of user buffer got sent... may be < orig_len
510 * in which case callback when writable has already been arranged
Andy Green62304762015-12-04 08:43:54 +0800511 * and user code can call lws_write() again with the rest
Andy Green1f4267b2013-10-17 08:09:19 +0800512 * later.
513 */
514
515 return n - (pre + post);
Andy Greenb429d482013-01-16 12:21:29 +0800516}
517
Andy Green62304762015-12-04 08:43:54 +0800518LWS_VISIBLE int lws_serve_http_file_fragment(
Andy Green4b85c1d2015-12-04 11:08:32 +0800519 struct lws_context *context, struct lws *wsi)
Andy Greenb8b247d2013-01-22 07:20:08 +0800520{
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100521 int n;
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100522 int m;
Andy Greenb8b247d2013-01-22 07:20:08 +0800523
524 while (!lws_send_pipe_choked(wsi)) {
Andy Green2764eba2013-12-09 14:16:17 +0800525
Andy Greene254d952014-03-23 11:41:15 +0800526 if (wsi->truncated_send_len) {
Andy Greena1a24d22014-04-10 14:25:24 +0800527 if (lws_issue_raw(wsi, wsi->truncated_send_malloc +
Andy Green2764eba2013-12-09 14:16:17 +0800528 wsi->truncated_send_offset,
Andy Greena1a24d22014-04-10 14:25:24 +0800529 wsi->truncated_send_len) < 0) {
Andy Green62304762015-12-04 08:43:54 +0800530 lwsl_info("closing from lws_serve_http_file_fragment\n");
Andy Greena1a24d22014-04-10 14:25:24 +0800531 return -1;
532 }
Andy Green2764eba2013-12-09 14:16:17 +0800533 continue;
534 }
535
536 if (wsi->u.http.filepos == wsi->u.http.filelen)
537 goto all_sent;
538
Andy Green158e8042014-04-02 14:25:10 +0800539 compatible_file_read(n, wsi->u.http.fd, context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +0800540 sizeof(context->service_buffer));
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100541 if (n < 0)
542 return -1; /* caller will close */
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100543 if (n) {
Andy Green62304762015-12-04 08:43:54 +0800544 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, AWAITING_TIMEOUT);
Andy Green200f3852014-10-18 12:23:05 +0800545 wsi->u.http.filepos += n;
Andy Green62304762015-12-04 08:43:54 +0800546 m = lws_write(wsi, context->service_buffer, n,
Andy Green200f3852014-10-18 12:23:05 +0800547 wsi->u.http.filepos == wsi->u.http.filelen ? LWS_WRITE_HTTP_FINAL : LWS_WRITE_HTTP);
Andy Greenfc7c5e42013-02-23 10:50:10 +0800548 if (m < 0)
549 return -1;
550
Andy Green158e8042014-04-02 14:25:10 +0800551 if (m != n)
Andy Greenfc7c5e42013-02-23 10:50:10 +0800552 /* adjust for what was not sent */
Andy Green7ef4b2e2014-11-30 13:00:47 +0800553 if (compatible_file_seek_cur(wsi->u.http.fd, m - n) < 0)
554 return -1;
Andy Greenb8b247d2013-01-22 07:20:08 +0800555 }
Andy Green2764eba2013-12-09 14:16:17 +0800556all_sent:
Andy Greene254d952014-03-23 11:41:15 +0800557 if (!wsi->truncated_send_len &&
Andy Green2764eba2013-12-09 14:16:17 +0800558 wsi->u.http.filepos == wsi->u.http.filelen) {
Andy Greenb8b247d2013-01-22 07:20:08 +0800559 wsi->state = WSI_STATE_HTTP;
560
Andy Greenc6f95d32015-10-27 07:07:14 +0800561 /* we might be in keepalive, so close it off here */
562 compatible_file_close(wsi->u.http.fd);
563 wsi->u.http.fd = LWS_INVALID_FILE;
564
Andy Greenb8b247d2013-01-22 07:20:08 +0800565 if (wsi->protocol->callback)
David Gauchard6c582282013-06-29 10:24:16 +0800566 /* ignore callback returned value */
567 user_callback_handle_rxflow(
Andy Greenb5b23192013-02-11 17:13:32 +0800568 wsi->protocol->callback, context, wsi,
569 LWS_CALLBACK_HTTP_FILE_COMPLETION,
570 wsi->user_space, NULL, 0);
David Gauchard6c582282013-06-29 10:24:16 +0800571 return 1; /* >0 indicates completed */
Andy Greenb8b247d2013-01-22 07:20:08 +0800572 }
573 }
574
Andy Green2764eba2013-12-09 14:16:17 +0800575 lwsl_info("choked before able to send whole file (post)\n");
Andy Green62304762015-12-04 08:43:54 +0800576 lws_callback_on_writable(context, wsi);
Andy Greenb8b247d2013-01-22 07:20:08 +0800577
David Gauchard6c582282013-06-29 10:24:16 +0800578 return 0; /* indicates further processing must be done */
Andy Greenb8b247d2013-01-22 07:20:08 +0800579}
Andy Green02138122014-04-06 06:26:35 +0100580
Andy Green11f27342015-11-08 12:10:26 +0800581#if LWS_POSIX
Andy Green02138122014-04-06 06:26:35 +0100582LWS_VISIBLE int
Andy Green4b85c1d2015-12-04 11:08:32 +0800583lws_ssl_capable_read_no_ssl(struct lws_context *context,
584 struct lws *wsi, unsigned char *buf, int len)
Andy Green02138122014-04-06 06:26:35 +0100585{
586 int n;
587
Andy Green2cd30742015-11-02 13:10:33 +0800588 (void)context;
Andy Green11f27342015-11-08 12:10:26 +0800589
Stephan Eberleb820e2c2015-10-23 08:10:55 +0200590 n = recv(wsi->sock, (char *)buf, len, 0);
Andy Greend7340c12014-04-10 14:08:10 +0800591 if (n >= 0)
592 return n;
Andy Green11f27342015-11-08 12:10:26 +0800593#if LWS_POSIX
Mark Liknessdbe624d2015-10-15 21:21:06 +0800594 if (LWS_ERRNO == LWS_EAGAIN ||
595 LWS_ERRNO == LWS_EWOULDBLOCK ||
596 LWS_ERRNO == LWS_EINTR)
597 return LWS_SSL_CAPABLE_MORE_SERVICE;
Andy Green8c0d3c02015-11-02 20:34:12 +0800598#endif
Andy Greend7340c12014-04-10 14:08:10 +0800599 lwsl_warn("error on reading from skt\n");
600 return LWS_SSL_CAPABLE_ERROR;
Andy Green02138122014-04-06 06:26:35 +0100601}
602
603LWS_VISIBLE int
Andy Green4b85c1d2015-12-04 11:08:32 +0800604lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
Andy Green02138122014-04-06 06:26:35 +0100605{
Andy Green11f27342015-11-08 12:10:26 +0800606 int n = 0;
Andy Green8c0d3c02015-11-02 20:34:12 +0800607
608#if LWS_POSIX
Stephan Eberleb820e2c2015-10-23 08:10:55 +0200609 n = send(wsi->sock, (char *)buf, len, MSG_NOSIGNAL);
Andy Greend7340c12014-04-10 14:08:10 +0800610 if (n >= 0)
611 return n;
Andy Green02138122014-04-06 06:26:35 +0100612
Andy Greend7340c12014-04-10 14:08:10 +0800613 if (LWS_ERRNO == LWS_EAGAIN ||
614 LWS_ERRNO == LWS_EWOULDBLOCK ||
615 LWS_ERRNO == LWS_EINTR) {
616 if (LWS_ERRNO == LWS_EWOULDBLOCK)
617 lws_set_blocking_send(wsi);
618
619 return LWS_SSL_CAPABLE_MORE_SERVICE;
620 }
Andy Green8c0d3c02015-11-02 20:34:12 +0800621#else
622 (void)n;
623 (void)wsi;
624 (void)buf;
625 (void)len;
626 // !!!
627#endif
628
Andy Greend7340c12014-04-10 14:08:10 +0800629 lwsl_debug("ERROR writing len %d to skt %d\n", len, n);
630 return LWS_SSL_CAPABLE_ERROR;
Andy Green02138122014-04-06 06:26:35 +0100631}
Andy Green11f27342015-11-08 12:10:26 +0800632#endif
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +0200633LWS_VISIBLE int
Andy Green4b85c1d2015-12-04 11:08:32 +0800634lws_ssl_pending_no_ssl(struct lws *wsi)
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +0200635{
Andy Green2cd30742015-11-02 13:10:33 +0800636 (void)wsi;
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?=4c0ba022015-08-19 16:23:33 +0200637 return 0;
638}