Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets - small server side websockets and web server implementation |
| 3 | * |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 4 | * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com> |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation: |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include "private-libwebsockets.h" |
| 23 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 24 | static int |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 25 | lws_0405_frame_mask_generate(struct lws *wsi) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 26 | { |
Andy Green | 44e0b08 | 2015-12-28 14:24:49 +0800 | [diff] [blame] | 27 | #if 0 |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 28 | 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 Green | 44e0b08 | 2015-12-28 14:24:49 +0800 | [diff] [blame] | 32 | #else |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 33 | int n; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 34 | /* fetch the per-frame nonce */ |
| 35 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 36 | n = lws_get_random(lws_get_context(wsi), wsi->u.ws.mask, 4); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 37 | if (n != 4) { |
| 38 | lwsl_parser("Unable to read from random device %s %d\n", |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 39 | SYSTEM_RANDOM_FILEPATH, n); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 40 | return 1; |
| 41 | } |
Andy Green | 44e0b08 | 2015-12-28 14:24:49 +0800 | [diff] [blame] | 42 | #endif |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 43 | /* start masking from first byte of masking key buffer */ |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 44 | wsi->u.ws.mask_idx = 0; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 45 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 49 | #ifdef _DEBUG |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 50 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 51 | LWS_VISIBLE void lwsl_hexdump(void *vbuf, size_t len) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 52 | { |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 53 | unsigned char *buf = (unsigned char *)vbuf; |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame] | 54 | unsigned int n, m, start; |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 55 | char line[80]; |
| 56 | char *p; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 57 | |
| 58 | lwsl_parser("\n"); |
| 59 | |
| 60 | for (n = 0; n < len;) { |
| 61 | start = n; |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 62 | p = line; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 63 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 64 | p += sprintf(p, "%04X: ", start); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 65 | |
| 66 | for (m = 0; m < 16 && n < len; m++) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 67 | p += sprintf(p, "%02X ", buf[n++]); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 68 | while (m++ < 16) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 69 | p += sprintf(p, " "); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 70 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 71 | p += sprintf(p, " "); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 72 | |
| 73 | for (m = 0; m < 16 && (start + m) < len; m++) { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 74 | if (buf[start + m] >= ' ' && buf[start + m] < 127) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 75 | *p++ = buf[start + m]; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 76 | else |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 77 | *p++ = '.'; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 78 | } |
| 79 | while (m++ < 16) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 80 | *p++ = ' '; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 81 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 82 | *p++ = '\n'; |
| 83 | *p = '\0'; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 84 | lwsl_debug("%s", line); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 85 | } |
| 86 | lwsl_debug("\n"); |
| 87 | } |
| 88 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 89 | #endif |
| 90 | |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 91 | /* |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 92 | * notice this returns number of bytes consumed, or -1 |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 93 | */ |
| 94 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 95 | int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 96 | { |
Andy Green | 6d64539 | 2015-12-17 18:25:25 +0800 | [diff] [blame] | 97 | struct lws_context *context = lws_get_context(wsi); |
mroszko | 793e7c0 | 2013-12-10 21:15:00 +0800 | [diff] [blame] | 98 | size_t real_len = len; |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame] | 99 | int n, m; |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 100 | |
Andy Green | 03cf1dd | 2014-04-01 14:20:44 +0800 | [diff] [blame] | 101 | if (!len) |
| 102 | return 0; |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 103 | /* just ignore sends after we cleared the truncation buffer */ |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 104 | if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE && |
| 105 | !wsi->trunc_len) |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 106 | return len; |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 107 | |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 108 | if (wsi->trunc_len && (buf < wsi->trunc_alloc || |
| 109 | buf > (wsi->trunc_alloc + wsi->trunc_len + |
| 110 | wsi->trunc_offset))) { |
Andy Green | 27e770b | 2014-03-23 11:21:51 +0800 | [diff] [blame] | 111 | lwsl_err("****** %x Sending new, pending truncated ...\n", wsi); |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 112 | assert(0); |
| 113 | } |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 114 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 115 | m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_TX_DO_SEND, &buf, len); |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 116 | if (m < 0) |
| 117 | return -1; |
| 118 | if (m) /* handled */ { |
| 119 | n = m; |
| 120 | goto handle_truncated_send; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 121 | } |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 122 | |
Andy Green | fc772cc | 2015-11-14 13:48:58 +0800 | [diff] [blame] | 123 | if (!lws_socket_is_valid(wsi->sock)) |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 124 | lwsl_warn("** error invalid sock but expected to send\n"); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 125 | |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame] | 126 | /* nope, send it on the socket directly */ |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 127 | lws_latency_pre(context, wsi); |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 128 | n = lws_ssl_capable_write(wsi, buf, len); |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 129 | lws_latency(context, wsi, "send lws_issue_raw", n, |
| 130 | (unsigned int)n == len); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 131 | |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 132 | switch (n) { |
| 133 | case LWS_SSL_CAPABLE_ERROR: |
Andy Green | 1e49918 | 2014-10-16 08:23:46 +0800 | [diff] [blame] | 134 | /* we're going to close, let close know sends aren't possible */ |
| 135 | wsi->socket_is_permanently_unusable = 1; |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 136 | return -1; |
| 137 | case LWS_SSL_CAPABLE_MORE_SERVICE: |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 138 | /* nothing got sent, not fatal, retry the whole thing later */ |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 139 | n = 0; |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 140 | break; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 141 | } |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 142 | |
| 143 | handle_truncated_send: |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 144 | /* |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 145 | * we were already handling a truncated send? |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 146 | */ |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 147 | 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 Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 151 | |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 152 | if (!wsi->trunc_len) { |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 153 | lwsl_info("***** %x partial send completed\n", wsi); |
Andy Green | e254d95 | 2014-03-23 11:41:15 +0800 | [diff] [blame] | 154 | /* done with it, but don't free it */ |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 155 | n = real_len; |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 156 | if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) { |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 157 | lwsl_info("***** %x signalling to close now\n", wsi); |
| 158 | return -1; /* retry closing now */ |
Andy Green | f4ffc1e | 2014-04-10 17:06:59 +0800 | [diff] [blame] | 159 | } |
Andy Green | 15d56dd | 2014-04-10 11:23:18 +0800 | [diff] [blame] | 160 | } |
| 161 | /* always callback on writeable */ |
Andy Green | 11c05bf | 2015-12-16 18:19:08 +0800 | [diff] [blame] | 162 | lws_callback_on_writable(wsi); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 163 | |
| 164 | return n; |
| 165 | } |
| 166 | |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 167 | if ((unsigned int)n == real_len) |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 168 | /* what we just sent went out cleanly */ |
| 169 | return n; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 170 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 171 | /* |
| 172 | * Newly truncated send. Buffer the remainder (it will get |
| 173 | * first priority next time the socket is writable) |
| 174 | */ |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 175 | lwsl_info("%p new partial sent %d from %d total\n", wsi, n, real_len); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 176 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 177 | /* |
| 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 Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 182 | if (!wsi->trunc_alloc || real_len - n > wsi->trunc_alloc_len) { |
| 183 | lws_free(wsi->trunc_alloc); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 184 | |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 185 | wsi->trunc_alloc_len = real_len - n; |
| 186 | wsi->trunc_alloc = lws_malloc(real_len - n); |
| 187 | if (!wsi->trunc_alloc) { |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 188 | lwsl_err("truncated send: unable to malloc %d\n", |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame] | 189 | real_len - n); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 190 | return -1; |
Andy Green | e254d95 | 2014-03-23 11:41:15 +0800 | [diff] [blame] | 191 | } |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 192 | } |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 193 | wsi->trunc_offset = 0; |
| 194 | wsi->trunc_len = real_len - n; |
| 195 | memcpy(wsi->trunc_alloc, buf + n, real_len - n); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 196 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 197 | /* since something buffered, force it to get another chance to send */ |
Andy Green | 11c05bf | 2015-12-16 18:19:08 +0800 | [diff] [blame] | 198 | lws_callback_on_writable(wsi); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 199 | |
| 200 | return real_len; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 201 | } |
| 202 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 203 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 204 | * lws_write() - Apply protocol then write data to client |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 205 | * @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 Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 208 | * LWS_PRE bytes valid BEFORE the pointer. |
Andy Green | 3246ebb | 2015-12-26 12:03:06 +0800 | [diff] [blame] | 209 | * This is so the protocol header data can be added in-situ. |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 210 | * @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 Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 224 | * |
| 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 Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 229 | */ |
| 230 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 231 | LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len, |
| 232 | enum lws_write_protocol wp) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 233 | { |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 234 | struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi]; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 235 | int masked7 = (wsi->mode == LWSCM_WS_CLIENT); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 236 | unsigned char is_masked_bit = 0; |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame] | 237 | unsigned char *dropmask = NULL; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 238 | struct lws_tokens eff_buf; |
Andy Green | de1a6a5 | 2015-12-26 10:07:17 +0800 | [diff] [blame] | 239 | int pre = 0, n; |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame] | 240 | size_t orig_len = len; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 241 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 242 | if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) { |
| 243 | /* remove us from the list */ |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 244 | struct lws **w = &pt->tx_draining_ext_list; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 245 | 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 Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 265 | goto send_raw; |
| 266 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 267 | /* if not in a state to send stuff, then just send nothing */ |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 268 | |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 269 | if (wsi->state != LWSS_ESTABLISHED && |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 270 | ((wsi->state != LWSS_RETURNED_CLOSE_ALREADY && |
| 271 | wsi->state != LWSS_AWAITING_CLOSE_ACK) || |
| 272 | wp != LWS_WRITE_CLOSE)) |
| 273 | return 0; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 274 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 275 | /* if we are continuing a frame that already had its header done */ |
| 276 | |
Andy Green | 44e0b08 | 2015-12-28 14:24:49 +0800 | [diff] [blame] | 277 | if (wsi->u.ws.inside_frame) { |
| 278 | lwsl_debug("INSIDE FRAME\n"); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 279 | goto do_more_inside_frame; |
Andy Green | 44e0b08 | 2015-12-28 14:24:49 +0800 | [diff] [blame] | 280 | } |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 281 | |
Andy Green | 822241c | 2014-08-18 22:21:51 +0800 | [diff] [blame] | 282 | wsi->u.ws.clean_buffer = 1; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 283 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 284 | /* |
| 285 | * give a chance to the extensions to modify payload |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 286 | * 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 Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 296 | */ |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 297 | eff_buf.token = (char *)buf; |
| 298 | eff_buf.token_len = len; |
| 299 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 300 | switch ((int)wp) { |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 301 | case LWS_WRITE_PING: |
| 302 | case LWS_WRITE_PONG: |
| 303 | case LWS_WRITE_CLOSE: |
| 304 | break; |
| 305 | default: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 306 | n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_TX, &eff_buf, wp); |
| 307 | if (n < 0) |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 308 | return -1; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 309 | |
| 310 | if (n && eff_buf.token_len) { |
| 311 | /* extension requires further draining */ |
| 312 | wsi->u.ws.tx_draining_ext = 1; |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 313 | wsi->u.ws.tx_draining_ext_list = pt->tx_draining_ext_list; |
| 314 | pt->tx_draining_ext_list = wsi; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 315 | /* 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 Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 335 | } |
| 336 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 337 | /* |
| 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 Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 342 | 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 Green | 27e770b | 2014-03-23 11:21:51 +0800 | [diff] [blame] | 354 | /* |
| 355 | * extension recreated it: |
| 356 | * need to buffer this if not all sent |
| 357 | */ |
| 358 | wsi->u.ws.clean_buffer = 0; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 359 | } |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 360 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 361 | buf = (unsigned char *)eff_buf.token; |
| 362 | len = eff_buf.token_len; |
| 363 | |
| 364 | switch (wsi->ietf_spec_revision) { |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 365 | case 13: |
| 366 | if (masked7) { |
| 367 | pre += 4; |
| 368 | dropmask = &buf[0 - pre]; |
| 369 | is_masked_bit = 0x80; |
| 370 | } |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 371 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 372 | switch (wp & 0xf) { |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 373 | case LWS_WRITE_TEXT: |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 374 | n = LWSWSOPC_TEXT_FRAME; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 375 | break; |
| 376 | case LWS_WRITE_BINARY: |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 377 | n = LWSWSOPC_BINARY_FRAME; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 378 | break; |
| 379 | case LWS_WRITE_CONTINUATION: |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 380 | n = LWSWSOPC_CONTINUATION; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 381 | break; |
| 382 | |
| 383 | case LWS_WRITE_CLOSE: |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 384 | n = LWSWSOPC_CLOSE; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 385 | break; |
| 386 | case LWS_WRITE_PING: |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 387 | n = LWSWSOPC_PING; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 388 | break; |
| 389 | case LWS_WRITE_PONG: |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 390 | n = LWSWSOPC_PONG; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 391 | break; |
| 392 | default: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 393 | lwsl_warn("lws_write: unknown write opc / wp\n"); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 394 | return -1; |
| 395 | } |
| 396 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 397 | if (!(wp & LWS_WRITE_NO_FIN)) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 398 | n |= 1 << 7; |
| 399 | |
| 400 | if (len < 126) { |
| 401 | pre += 2; |
| 402 | buf[-pre] = n; |
Andy Green | 1cc0388 | 2015-12-06 08:00:03 +0800 | [diff] [blame] | 403 | buf[-pre + 1] = (unsigned char)(len | is_masked_bit); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 404 | } else { |
| 405 | if (len < 65536) { |
| 406 | pre += 4; |
| 407 | buf[-pre] = n; |
| 408 | buf[-pre + 1] = 126 | is_masked_bit; |
Andy Green | 1cc0388 | 2015-12-06 08:00:03 +0800 | [diff] [blame] | 409 | buf[-pre + 2] = (unsigned char)(len >> 8); |
| 410 | buf[-pre + 3] = (unsigned char)len; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 411 | } 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 Green | 1cc0388 | 2015-12-06 08:00:03 +0800 | [diff] [blame] | 426 | 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 Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | break; |
| 433 | } |
| 434 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 435 | do_more_inside_frame: |
| 436 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 437 | /* |
| 438 | * Deal with masking if we are in client -> server direction and |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 439 | * the wp demands it |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 440 | */ |
| 441 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 442 | if (masked7) { |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 443 | if (!wsi->u.ws.inside_frame) |
Andy Green | 3ef579b | 2015-12-04 09:23:56 +0800 | [diff] [blame] | 444 | if (lws_0405_frame_mask_generate(wsi)) { |
Andy Green | 27e770b | 2014-03-23 11:21:51 +0800 | [diff] [blame] | 445 | lwsl_err("frame mask generation failed\n"); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 446 | return -1; |
| 447 | } |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 448 | |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 449 | /* |
| 450 | * in v7, just mask the payload |
| 451 | */ |
Nikolay Dimitrov | a8268e7 | 2013-12-21 10:22:17 +0800 | [diff] [blame] | 452 | if (dropmask) { /* never set if already inside frame */ |
| 453 | for (n = 4; n < (int)len + 4; n++) |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 454 | dropmask[n] = dropmask[n] ^ wsi->u.ws.mask[ |
| 455 | (wsi->u.ws.mask_idx++) & 3]; |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 456 | |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 457 | /* copy the frame nonce into place */ |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 458 | memcpy(dropmask, wsi->u.ws.mask, 4); |
Nikolay Dimitrov | a8268e7 | 2013-12-21 10:22:17 +0800 | [diff] [blame] | 459 | } |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | send_raw: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 463 | switch ((int)wp) { |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 464 | case LWS_WRITE_CLOSE: |
Andy Green | de1a6a5 | 2015-12-26 10:07:17 +0800 | [diff] [blame] | 465 | /* lwsl_hexdump(&buf[-pre], len); */ |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 466 | case LWS_WRITE_HTTP: |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 467 | case LWS_WRITE_HTTP_FINAL: |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 468 | case LWS_WRITE_HTTP_HEADERS: |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 469 | case LWS_WRITE_PONG: |
| 470 | case LWS_WRITE_PING: |
Andy Green | 095d303 | 2014-10-08 12:15:15 +0800 | [diff] [blame] | 471 | #ifdef LWS_USE_HTTP2 |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 472 | if (wsi->mode == LWSCM_HTTP2_SERVING) { |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 473 | unsigned char flags = 0; |
| 474 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 475 | n = LWS_HTTP2_FRAME_TYPE_DATA; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 476 | if (wp == LWS_WRITE_HTTP_HEADERS) { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 477 | n = LWS_HTTP2_FRAME_TYPE_HEADERS; |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 478 | flags = LWS_HTTP2_FLAG_END_HEADERS; |
Andy Green | 1cea581 | 2014-10-19 07:36:20 +0800 | [diff] [blame] | 479 | if (wsi->u.http2.send_END_STREAM) |
| 480 | flags |= LWS_HTTP2_FLAG_END_STREAM; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 481 | } |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 482 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 483 | if ((wp == LWS_WRITE_HTTP || |
| 484 | wp == LWS_WRITE_HTTP_FINAL) && |
Andy Green | de1a6a5 | 2015-12-26 10:07:17 +0800 | [diff] [blame] | 485 | wsi->u.http.content_length) { |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 486 | wsi->u.http.content_remain -= len; |
Andy Green | de1a6a5 | 2015-12-26 10:07:17 +0800 | [diff] [blame] | 487 | lwsl_info("%s: content_remain = %lu\n", __func__, |
| 488 | wsi->u.http.content_remain); |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 489 | if (!wsi->u.http.content_remain) { |
| 490 | lwsl_info("%s: selecting final write mode\n", __func__); |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 491 | wp = LWS_WRITE_HTTP_FINAL; |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 492 | } |
| 493 | } |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 494 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 495 | if (wp == LWS_WRITE_HTTP_FINAL && wsi->u.http2.END_STREAM) { |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 496 | lwsl_info("%s: setting END_STREAM\n", __func__); |
| 497 | flags |= LWS_HTTP2_FLAG_END_STREAM; |
| 498 | } |
| 499 | |
Andy Green | de1a6a5 | 2015-12-26 10:07:17 +0800 | [diff] [blame] | 500 | return lws_http2_frame_write(wsi, n, flags, |
| 501 | wsi->u.http2.my_stream_id, len, buf); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 502 | } |
Andy Green | 095d303 | 2014-10-08 12:15:15 +0800 | [diff] [blame] | 503 | #endif |
Andy Green | de1a6a5 | 2015-12-26 10:07:17 +0800 | [diff] [blame] | 504 | return lws_issue_raw(wsi, (unsigned char *)buf - pre, len + pre); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 505 | default: |
| 506 | break; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 507 | } |
| 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 Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 521 | * |
| 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 Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 526 | */ |
| 527 | |
Andy Green | de1a6a5 | 2015-12-26 10:07:17 +0800 | [diff] [blame] | 528 | n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre); |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 529 | wsi->u.ws.inside_frame = 1; |
Andy Green | 03cf1dd | 2014-04-01 14:20:44 +0800 | [diff] [blame] | 530 | if (n <= 0) |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 531 | return n; |
| 532 | |
Andy Green | de1a6a5 | 2015-12-26 10:07:17 +0800 | [diff] [blame] | 533 | if (n == (int)len + pre) { |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 534 | /* 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 Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 542 | * and user code can call lws_write() again with the rest |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 543 | * later. |
| 544 | */ |
| 545 | |
Andy Green | de1a6a5 | 2015-12-26 10:07:17 +0800 | [diff] [blame] | 546 | return n - pre; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 547 | } |
| 548 | |
Andy Green | 11c05bf | 2015-12-16 18:19:08 +0800 | [diff] [blame] | 549 | LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi) |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 550 | { |
Andy Green | 11c05bf | 2015-12-16 18:19:08 +0800 | [diff] [blame] | 551 | struct lws_context *context = wsi->context; |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 552 | struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi]; |
Andy Green | 4e442b7 | 2015-12-10 07:58:58 +0800 | [diff] [blame] | 553 | unsigned long amount; |
| 554 | int n, m; |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 555 | |
| 556 | while (!lws_send_pipe_choked(wsi)) { |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 557 | if (wsi->trunc_len) { |
| 558 | if (lws_issue_raw(wsi, wsi->trunc_alloc + |
| 559 | wsi->trunc_offset, |
| 560 | wsi->trunc_len) < 0) { |
Andy Green | 4e442b7 | 2015-12-10 07:58:58 +0800 | [diff] [blame] | 561 | lwsl_info("%s: closing\n", __func__); |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 562 | return -1; |
| 563 | } |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 564 | continue; |
| 565 | } |
| 566 | |
| 567 | if (wsi->u.http.filepos == wsi->u.http.filelen) |
| 568 | goto all_sent; |
| 569 | |
Andy Green | 891628b | 2015-12-11 13:12:58 +0800 | [diff] [blame] | 570 | if (lws_plat_file_read(wsi, wsi->u.http.fd, &amount, |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 571 | pt->serv_buf, |
| 572 | LWS_MAX_SOCKET_IO_BUF) < 0) |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 573 | return -1; /* caller will close */ |
Andy Green | 4e442b7 | 2015-12-10 07:58:58 +0800 | [diff] [blame] | 574 | |
| 575 | n = (int)amount; |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 576 | if (n) { |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame] | 577 | lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, |
Andy Green | 200a6a2 | 2016-02-15 20:36:02 +0800 | [diff] [blame] | 578 | context->timeout_secs); |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 579 | wsi->u.http.filepos += n; |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 580 | m = lws_write(wsi, pt->serv_buf, n, |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame] | 581 | wsi->u.http.filepos == wsi->u.http.filelen ? |
| 582 | LWS_WRITE_HTTP_FINAL : LWS_WRITE_HTTP); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 583 | if (m < 0) |
| 584 | return -1; |
| 585 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 586 | if (m != n) |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 587 | /* adjust for what was not sent */ |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 588 | if (lws_plat_file_seek_cur(wsi, wsi->u.http.fd, |
Andy Green | 95181d9 | 2015-12-10 12:50:10 +0800 | [diff] [blame] | 589 | m - n) == |
| 590 | (unsigned long)-1) |
Andy Green | 7ef4b2e | 2014-11-30 13:00:47 +0800 | [diff] [blame] | 591 | return -1; |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 592 | } |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 593 | all_sent: |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 594 | if (!wsi->trunc_len && wsi->u.http.filepos == wsi->u.http.filelen) { |
| 595 | wsi->state = LWSS_HTTP; |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 596 | |
Andy Green | c6f95d3 | 2015-10-27 07:07:14 +0800 | [diff] [blame] | 597 | /* we might be in keepalive, so close it off here */ |
Andy Green | 891628b | 2015-12-11 13:12:58 +0800 | [diff] [blame] | 598 | lws_plat_file_close(wsi, wsi->u.http.fd); |
Andy Green | c6f95d3 | 2015-10-27 07:07:14 +0800 | [diff] [blame] | 599 | wsi->u.http.fd = LWS_INVALID_FILE; |
| 600 | |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 601 | if (wsi->protocol->callback) |
David Gauchard | 6c58228 | 2013-06-29 10:24:16 +0800 | [diff] [blame] | 602 | /* ignore callback returned value */ |
Andy Green | e99a83c | 2016-01-20 16:56:06 +0800 | [diff] [blame] | 603 | 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 Gauchard | 6c58228 | 2013-06-29 10:24:16 +0800 | [diff] [blame] | 608 | return 1; /* >0 indicates completed */ |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 612 | lwsl_info("choked before able to send whole file (post)\n"); |
Andy Green | 11c05bf | 2015-12-16 18:19:08 +0800 | [diff] [blame] | 613 | lws_callback_on_writable(wsi); |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 614 | |
David Gauchard | 6c58228 | 2013-06-29 10:24:16 +0800 | [diff] [blame] | 615 | return 0; /* indicates further processing must be done */ |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 616 | } |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 617 | |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 618 | #if LWS_POSIX |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 619 | LWS_VISIBLE int |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 620 | lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len) |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 621 | { |
| 622 | int n; |
| 623 | |
Stephan Eberle | b820e2c | 2015-10-23 08:10:55 +0200 | [diff] [blame] | 624 | n = recv(wsi->sock, (char *)buf, len, 0); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 625 | if (n >= 0) |
| 626 | return n; |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 627 | #if LWS_POSIX |
Mark Likness | dbe624d | 2015-10-15 21:21:06 +0800 | [diff] [blame] | 628 | if (LWS_ERRNO == LWS_EAGAIN || |
| 629 | LWS_ERRNO == LWS_EWOULDBLOCK || |
| 630 | LWS_ERRNO == LWS_EINTR) |
| 631 | return LWS_SSL_CAPABLE_MORE_SERVICE; |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 632 | #endif |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 633 | lwsl_warn("error on reading from skt\n"); |
| 634 | return LWS_SSL_CAPABLE_ERROR; |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | LWS_VISIBLE int |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 638 | lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len) |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 639 | { |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 640 | int n = 0; |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 641 | |
| 642 | #if LWS_POSIX |
Stephan Eberle | b820e2c | 2015-10-23 08:10:55 +0200 | [diff] [blame] | 643 | n = send(wsi->sock, (char *)buf, len, MSG_NOSIGNAL); |
Andy Green | 8d5351a | 2016-01-27 08:50:31 +0800 | [diff] [blame] | 644 | // lwsl_info("%s: sent len %d result %d", __func__, len, n); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 645 | if (n >= 0) |
| 646 | return n; |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 647 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 648 | 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 Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 656 | #else |
| 657 | (void)n; |
| 658 | (void)wsi; |
| 659 | (void)buf; |
| 660 | (void)len; |
| 661 | // !!! |
| 662 | #endif |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 663 | |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 664 | lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n", len, wsi->sock, n, LWS_ERRNO); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 665 | return LWS_SSL_CAPABLE_ERROR; |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 666 | } |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 667 | #endif |
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= | 4c0ba02 | 2015-08-19 16:23:33 +0200 | [diff] [blame] | 668 | LWS_VISIBLE int |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 669 | lws_ssl_pending_no_ssl(struct lws *wsi) |
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= | 4c0ba02 | 2015-08-19 16:23:33 +0200 | [diff] [blame] | 670 | { |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 671 | (void)wsi; |
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= | 4c0ba02 | 2015-08-19 16:23:33 +0200 | [diff] [blame] | 672 | return 0; |
| 673 | } |