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 | 27e770b | 2014-03-23 11:21:51 +0800 | [diff] [blame] | 4 | * Copyright (C) 2010-2014 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 | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 27 | int n; |
| 28 | |
| 29 | /* fetch the per-frame nonce */ |
| 30 | |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 31 | n = lws_get_random(wsi->protocol->owning_server, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 32 | wsi->u.ws.frame_masking_nonce_04, 4); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 33 | 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 Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 40 | wsi->u.ws.frame_mask_index = 0; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 41 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 45 | #ifdef _DEBUG |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 46 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 47 | LWS_VISIBLE void lwsl_hexdump(void *vbuf, size_t len) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 48 | { |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 49 | unsigned char *buf = (unsigned char *)vbuf; |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 50 | unsigned int n, m, start; |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 51 | char line[80]; |
| 52 | char *p; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 53 | |
| 54 | lwsl_parser("\n"); |
| 55 | |
| 56 | for (n = 0; n < len;) { |
| 57 | start = n; |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 58 | p = line; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 59 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 60 | p += sprintf(p, "%04X: ", start); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 61 | |
| 62 | for (m = 0; m < 16 && n < len; m++) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 63 | p += sprintf(p, "%02X ", buf[n++]); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 64 | while (m++ < 16) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 65 | p += sprintf(p, " "); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 66 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 67 | p += sprintf(p, " "); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 68 | |
| 69 | for (m = 0; m < 16 && (start + m) < len; m++) { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 70 | if (buf[start + m] >= ' ' && buf[start + m] < 127) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 71 | *p++ = buf[start + m]; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 72 | else |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 73 | *p++ = '.'; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 74 | } |
| 75 | while (m++ < 16) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 76 | *p++ = ' '; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 77 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 78 | *p++ = '\n'; |
| 79 | *p = '\0'; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 80 | lwsl_debug("%s", line); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 81 | } |
| 82 | lwsl_debug("\n"); |
| 83 | } |
| 84 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 85 | #endif |
| 86 | |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 87 | /* |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 88 | * notice this returns number of bytes consumed, or -1 |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 89 | */ |
| 90 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 91 | 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] | 92 | { |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 93 | struct lws_context *context = wsi->protocol->owning_server; |
mroszko | 793e7c0 | 2013-12-10 21:15:00 +0800 | [diff] [blame] | 94 | size_t real_len = len; |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 95 | int n, m; |
Andy Green | 03cf1dd | 2014-04-01 14:20:44 +0800 | [diff] [blame] | 96 | |
| 97 | if (!len) |
| 98 | return 0; |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 99 | /* just ignore sends after we cleared the truncation buffer */ |
| 100 | if (wsi->state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE && |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 101 | !wsi->truncated_send_len) |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 102 | return len; |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 103 | |
Andy Green | e254d95 | 2014-03-23 11:41:15 +0800 | [diff] [blame] | 104 | if (wsi->truncated_send_len && (buf < wsi->truncated_send_malloc || |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 105 | buf > (wsi->truncated_send_malloc + wsi->truncated_send_len + |
| 106 | wsi->truncated_send_offset))) { |
Andy Green | 27e770b | 2014-03-23 11:21:51 +0800 | [diff] [blame] | 107 | lwsl_err("****** %x Sending new, pending truncated ...\n", wsi); |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 108 | assert(0); |
| 109 | } |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 110 | |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 111 | m = lws_ext_callback_for_each_active(wsi, |
| 112 | LWS_EXT_CALLBACK_PACKET_TX_DO_SEND, &buf, len); |
| 113 | if (m < 0) |
| 114 | return -1; |
| 115 | if (m) /* handled */ { |
| 116 | n = m; |
| 117 | goto handle_truncated_send; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 118 | } |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 119 | |
Andy Green | fc772cc | 2015-11-14 13:48:58 +0800 | [diff] [blame] | 120 | if (!lws_socket_is_valid(wsi->sock)) |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 121 | lwsl_warn("** error invalid sock but expected to send\n"); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 122 | |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 123 | /* nope, send it on the socket directly */ |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 124 | lws_latency_pre(context, wsi); |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 125 | n = lws_ssl_capable_write(wsi, buf, len); |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 126 | lws_latency(context, wsi, "send lws_issue_raw", n, (unsigned int)n == len); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 127 | |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 128 | switch (n) { |
| 129 | case LWS_SSL_CAPABLE_ERROR: |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 130 | lwsl_err("%s: wsi %p: LWS_SSL_CAPABLE_ERROR\n", __func__, (void *)wsi); |
Andy Green | 1e49918 | 2014-10-16 08:23:46 +0800 | [diff] [blame] | 131 | /* we're going to close, let close know sends aren't possible */ |
| 132 | wsi->socket_is_permanently_unusable = 1; |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 133 | return -1; |
| 134 | case LWS_SSL_CAPABLE_MORE_SERVICE: |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 135 | /* nothing got sent, not fatal, retry the whole thing later */ |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 136 | n = 0; |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 137 | break; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 138 | } |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 139 | |
| 140 | handle_truncated_send: |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 141 | /* |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 142 | * we were already handling a truncated send? |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 143 | */ |
Andy Green | e254d95 | 2014-03-23 11:41:15 +0800 | [diff] [blame] | 144 | if (wsi->truncated_send_len) { |
Andy Green | 27e770b | 2014-03-23 11:21:51 +0800 | [diff] [blame] | 145 | lwsl_info("***** %x partial send moved on by %d (vs %d)\n", |
| 146 | wsi, n, real_len); |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 147 | wsi->truncated_send_offset += n; |
| 148 | wsi->truncated_send_len -= n; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 149 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 150 | if (!wsi->truncated_send_len) { |
| 151 | lwsl_info("***** %x partial send completed\n", wsi); |
Andy Green | e254d95 | 2014-03-23 11:41:15 +0800 | [diff] [blame] | 152 | /* done with it, but don't free it */ |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 153 | n = real_len; |
Andy Green | f4ffc1e | 2014-04-10 17:06:59 +0800 | [diff] [blame] | 154 | if (wsi->state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE) { |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 155 | lwsl_info("***** %x signalling to close now\n", wsi); |
| 156 | return -1; /* retry closing now */ |
Andy Green | f4ffc1e | 2014-04-10 17:06:59 +0800 | [diff] [blame] | 157 | } |
Andy Green | 15d56dd | 2014-04-10 11:23:18 +0800 | [diff] [blame] | 158 | } |
| 159 | /* always callback on writeable */ |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 160 | lws_callback_on_writable(wsi->protocol->owning_server, wsi); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 161 | |
| 162 | return n; |
| 163 | } |
| 164 | |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 165 | if ((unsigned int)n == real_len) |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 166 | /* what we just sent went out cleanly */ |
| 167 | return n; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 168 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 169 | if (n && wsi->u.ws.clean_buffer) |
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 | * 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 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 | return n; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 178 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 179 | /* |
| 180 | * Newly truncated send. Buffer the remainder (it will get |
| 181 | * first priority next time the socket is writable) |
| 182 | */ |
| 183 | lwsl_info("***** %x new partial sent %d from %d total\n", |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 184 | wsi, n, real_len); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 185 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 186 | /* |
| 187 | * - if we still have a suitable malloc lying around, use it |
| 188 | * - or, if too small, reallocate it |
| 189 | * - or, if no buffer, create it |
| 190 | */ |
| 191 | if (!wsi->truncated_send_malloc || |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 192 | real_len - n > wsi->truncated_send_allocation) { |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 193 | lws_free(wsi->truncated_send_malloc); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 194 | |
| 195 | wsi->truncated_send_allocation = real_len - n; |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 196 | wsi->truncated_send_malloc = lws_malloc(real_len - n); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 197 | if (!wsi->truncated_send_malloc) { |
| 198 | lwsl_err("truncated send: unable to malloc %d\n", |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 199 | real_len - n); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 200 | return -1; |
Andy Green | e254d95 | 2014-03-23 11:41:15 +0800 | [diff] [blame] | 201 | } |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 202 | } |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 203 | wsi->truncated_send_offset = 0; |
| 204 | wsi->truncated_send_len = real_len - n; |
| 205 | memcpy(wsi->truncated_send_malloc, buf + n, real_len - n); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 206 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 207 | /* since something buffered, force it to get another chance to send */ |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 208 | lws_callback_on_writable(wsi->protocol->owning_server, wsi); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 209 | |
| 210 | return real_len; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 211 | } |
| 212 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 213 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 214 | * lws_write() - Apply protocol then write data to client |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 215 | * @wsi: Websocket instance (available from user callback) |
| 216 | * @buf: The data to send. For data being sent on a websocket |
| 217 | * connection (ie, not default http), this buffer MUST have |
| 218 | * LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer |
| 219 | * and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid |
| 220 | * in the buffer after (buf + len). This is so the protocol |
| 221 | * header and trailer data can be added in-situ. |
| 222 | * @len: Count of the data bytes in the payload starting from buf |
| 223 | * @protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one |
| 224 | * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate |
| 225 | * data on a websockets connection. Remember to allow the extra |
| 226 | * bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT |
| 227 | * are used. |
| 228 | * |
| 229 | * This function provides the way to issue data back to the client |
| 230 | * for both http and websocket protocols. |
| 231 | * |
| 232 | * In the case of sending using websocket protocol, be sure to allocate |
| 233 | * valid storage before and after buf as explained above. This scheme |
| 234 | * allows maximum efficiency of sending data and protocol in a single |
| 235 | * packet while not burdening the user code with any protocol knowledge. |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 236 | * |
| 237 | * Return may be -1 for a fatal error needing connection close, or a |
| 238 | * positive number reflecting the amount of bytes actually sent. This |
| 239 | * can be less than the requested number of bytes due to OS memory |
| 240 | * pressure at any given time. |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 241 | */ |
| 242 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 243 | LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 244 | size_t len, enum lws_write_protocol protocol) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 245 | { |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 246 | int masked7 = wsi->mode == LWS_CONNMODE_WS_CLIENT; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 247 | unsigned char is_masked_bit = 0; |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 248 | unsigned char *dropmask = NULL; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 249 | struct lws_tokens eff_buf; |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 250 | int post = 0, pre = 0, n; |
| 251 | size_t orig_len = len; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 252 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 253 | if (len == 0 && protocol != LWS_WRITE_CLOSE && |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 254 | protocol != LWS_WRITE_PING && protocol != LWS_WRITE_PONG) { |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 255 | lwsl_warn("zero length lws_write attempt\n"); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 259 | if (protocol == LWS_WRITE_HTTP || |
| 260 | protocol == LWS_WRITE_HTTP_FINAL || |
| 261 | protocol == LWS_WRITE_HTTP_HEADERS) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 262 | goto send_raw; |
| 263 | |
| 264 | /* websocket protocol, either binary or text */ |
| 265 | |
Andrejs Hanins | 140ac6e | 2015-11-06 18:18:32 +0200 | [diff] [blame] | 266 | if (wsi->state != WSI_STATE_ESTABLISHED && |
| 267 | !(wsi->state == WSI_STATE_RETURNED_CLOSE_ALREADY && |
| 268 | protocol == LWS_WRITE_CLOSE)) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 269 | return -1; |
| 270 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 271 | /* if we are continuing a frame that already had its header done */ |
| 272 | |
| 273 | if (wsi->u.ws.inside_frame) |
| 274 | goto do_more_inside_frame; |
| 275 | |
Andy Green | 822241c | 2014-08-18 22:21:51 +0800 | [diff] [blame] | 276 | wsi->u.ws.clean_buffer = 1; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 277 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 278 | /* |
| 279 | * give a chance to the extensions to modify payload |
| 280 | * pre-TX mangling is not allowed to truncate |
| 281 | */ |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 282 | eff_buf.token = (char *)buf; |
| 283 | eff_buf.token_len = len; |
| 284 | |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 285 | switch (protocol) { |
| 286 | case LWS_WRITE_PING: |
| 287 | case LWS_WRITE_PONG: |
| 288 | case LWS_WRITE_CLOSE: |
| 289 | break; |
| 290 | default: |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 291 | if (lws_ext_callback_for_each_active(wsi, |
| 292 | LWS_EXT_CALLBACK_PAYLOAD_TX, &eff_buf, 0) < 0) |
| 293 | return -1; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 294 | } |
| 295 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 296 | /* |
| 297 | * an extension did something we need to keep... for example, if |
| 298 | * compression extension, it has already updated its state according |
| 299 | * to this being issued |
| 300 | */ |
| 301 | if ((char *)buf != eff_buf.token) |
Andy Green | 27e770b | 2014-03-23 11:21:51 +0800 | [diff] [blame] | 302 | /* |
| 303 | * extension recreated it: |
| 304 | * need to buffer this if not all sent |
| 305 | */ |
| 306 | wsi->u.ws.clean_buffer = 0; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 307 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 308 | buf = (unsigned char *)eff_buf.token; |
| 309 | len = eff_buf.token_len; |
| 310 | |
| 311 | switch (wsi->ietf_spec_revision) { |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 312 | case 13: |
| 313 | if (masked7) { |
| 314 | pre += 4; |
| 315 | dropmask = &buf[0 - pre]; |
| 316 | is_masked_bit = 0x80; |
| 317 | } |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 318 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 319 | switch (protocol & 0xf) { |
| 320 | case LWS_WRITE_TEXT: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 321 | n = LWS_WS_OPCODE_07__TEXT_FRAME; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 322 | break; |
| 323 | case LWS_WRITE_BINARY: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 324 | n = LWS_WS_OPCODE_07__BINARY_FRAME; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 325 | break; |
| 326 | case LWS_WRITE_CONTINUATION: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 327 | n = LWS_WS_OPCODE_07__CONTINUATION; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 328 | break; |
| 329 | |
| 330 | case LWS_WRITE_CLOSE: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 331 | n = LWS_WS_OPCODE_07__CLOSE; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 332 | |
| 333 | /* |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 334 | * 06+ has a 2-byte status code in network order |
| 335 | * we can do this because we demand post-buf |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 336 | */ |
| 337 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 338 | if (wsi->u.ws.close_reason) { |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 339 | /* reason codes count as data bytes */ |
| 340 | buf -= 2; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 341 | buf[0] = wsi->u.ws.close_reason >> 8; |
| 342 | buf[1] = wsi->u.ws.close_reason; |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 343 | len += 2; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 344 | } |
| 345 | break; |
| 346 | case LWS_WRITE_PING: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 347 | n = LWS_WS_OPCODE_07__PING; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 348 | break; |
| 349 | case LWS_WRITE_PONG: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 350 | n = LWS_WS_OPCODE_07__PONG; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 351 | break; |
| 352 | default: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 353 | lwsl_warn("lws_write: unknown write opc / protocol\n"); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 354 | return -1; |
| 355 | } |
| 356 | |
| 357 | if (!(protocol & LWS_WRITE_NO_FIN)) |
| 358 | n |= 1 << 7; |
| 359 | |
| 360 | if (len < 126) { |
| 361 | pre += 2; |
| 362 | buf[-pre] = n; |
| 363 | buf[-pre + 1] = len | is_masked_bit; |
| 364 | } else { |
| 365 | if (len < 65536) { |
| 366 | pre += 4; |
| 367 | buf[-pre] = n; |
| 368 | buf[-pre + 1] = 126 | is_masked_bit; |
| 369 | buf[-pre + 2] = len >> 8; |
| 370 | buf[-pre + 3] = len; |
| 371 | } else { |
| 372 | pre += 10; |
| 373 | buf[-pre] = n; |
| 374 | buf[-pre + 1] = 127 | is_masked_bit; |
| 375 | #if defined __LP64__ |
| 376 | buf[-pre + 2] = (len >> 56) & 0x7f; |
| 377 | buf[-pre + 3] = len >> 48; |
| 378 | buf[-pre + 4] = len >> 40; |
| 379 | buf[-pre + 5] = len >> 32; |
| 380 | #else |
| 381 | buf[-pre + 2] = 0; |
| 382 | buf[-pre + 3] = 0; |
| 383 | buf[-pre + 4] = 0; |
| 384 | buf[-pre + 5] = 0; |
| 385 | #endif |
| 386 | buf[-pre + 6] = len >> 24; |
| 387 | buf[-pre + 7] = len >> 16; |
| 388 | buf[-pre + 8] = len >> 8; |
| 389 | buf[-pre + 9] = len; |
| 390 | } |
| 391 | } |
| 392 | break; |
| 393 | } |
| 394 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 395 | do_more_inside_frame: |
| 396 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 397 | /* |
| 398 | * Deal with masking if we are in client -> server direction and |
| 399 | * the protocol demands it |
| 400 | */ |
| 401 | |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 402 | if (wsi->mode == LWS_CONNMODE_WS_CLIENT) { |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 403 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 404 | if (!wsi->u.ws.inside_frame) |
Andy Green | 3ef579b | 2015-12-04 09:23:56 +0800 | [diff] [blame] | 405 | if (lws_0405_frame_mask_generate(wsi)) { |
Andy Green | 27e770b | 2014-03-23 11:21:51 +0800 | [diff] [blame] | 406 | lwsl_err("frame mask generation failed\n"); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 407 | return -1; |
| 408 | } |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 409 | |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 410 | /* |
| 411 | * in v7, just mask the payload |
| 412 | */ |
Nikolay Dimitrov | a8268e7 | 2013-12-21 10:22:17 +0800 | [diff] [blame] | 413 | if (dropmask) { /* never set if already inside frame */ |
| 414 | for (n = 4; n < (int)len + 4; n++) |
| 415 | dropmask[n] = dropmask[n] ^ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 416 | wsi->u.ws.frame_masking_nonce_04[ |
| 417 | (wsi->u.ws.frame_mask_index++) & 3]; |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 418 | |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 419 | /* copy the frame nonce into place */ |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 420 | memcpy(dropmask, wsi->u.ws.frame_masking_nonce_04, 4); |
Nikolay Dimitrov | a8268e7 | 2013-12-21 10:22:17 +0800 | [diff] [blame] | 421 | } |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | send_raw: |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 425 | switch (protocol) { |
| 426 | case LWS_WRITE_CLOSE: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 427 | /* lwsl_hexdump(&buf[-pre], len + post); */ |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 428 | case LWS_WRITE_HTTP: |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 429 | case LWS_WRITE_HTTP_FINAL: |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 430 | case LWS_WRITE_HTTP_HEADERS: |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 431 | case LWS_WRITE_PONG: |
| 432 | case LWS_WRITE_PING: |
Andy Green | 095d303 | 2014-10-08 12:15:15 +0800 | [diff] [blame] | 433 | #ifdef LWS_USE_HTTP2 |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 434 | if (wsi->mode == LWS_CONNMODE_HTTP2_SERVING) { |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 435 | unsigned char flags = 0; |
| 436 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 437 | n = LWS_HTTP2_FRAME_TYPE_DATA; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 438 | if (protocol == LWS_WRITE_HTTP_HEADERS) { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 439 | n = LWS_HTTP2_FRAME_TYPE_HEADERS; |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 440 | flags = LWS_HTTP2_FLAG_END_HEADERS; |
Andy Green | 1cea581 | 2014-10-19 07:36:20 +0800 | [diff] [blame] | 441 | if (wsi->u.http2.send_END_STREAM) |
| 442 | flags |= LWS_HTTP2_FLAG_END_STREAM; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 443 | } |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 444 | |
| 445 | if ((protocol == LWS_WRITE_HTTP || protocol == LWS_WRITE_HTTP_FINAL) && wsi->u.http.content_length) { |
| 446 | wsi->u.http.content_remain -= len; |
| 447 | lwsl_info("%s: content_remain = %lu\n", __func__, wsi->u.http.content_remain); |
| 448 | if (!wsi->u.http.content_remain) { |
| 449 | lwsl_info("%s: selecting final write mode\n", __func__); |
| 450 | protocol = LWS_WRITE_HTTP_FINAL; |
| 451 | } |
| 452 | } |
| 453 | |
Andy Green | 1cea581 | 2014-10-19 07:36:20 +0800 | [diff] [blame] | 454 | if (protocol == LWS_WRITE_HTTP_FINAL && wsi->u.http2.END_STREAM) { |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 455 | lwsl_info("%s: setting END_STREAM\n", __func__); |
| 456 | flags |= LWS_HTTP2_FLAG_END_STREAM; |
| 457 | } |
| 458 | |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 459 | return lws_http2_frame_write(wsi, n, flags, wsi->u.http2.my_stream_id, len, buf); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 460 | } |
Andy Green | 095d303 | 2014-10-08 12:15:15 +0800 | [diff] [blame] | 461 | #endif |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 462 | return lws_issue_raw(wsi, (unsigned char *)buf - pre, |
| 463 | len + pre + post); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 464 | default: |
| 465 | break; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 466 | } |
| 467 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 468 | wsi->u.ws.inside_frame = 1; |
| 469 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 470 | /* |
| 471 | * give any active extensions a chance to munge the buffer |
| 472 | * before send. We pass in a pointer to an lws_tokens struct |
| 473 | * prepared with the default buffer and content length that's in |
| 474 | * there. Rather than rewrite the default buffer, extensions |
| 475 | * that expect to grow the buffer can adapt .token to |
| 476 | * point to their own per-connection buffer in the extension |
| 477 | * user allocation. By default with no extensions or no |
| 478 | * extension callback handling, just the normal input buffer is |
| 479 | * used then so it is efficient. |
| 480 | * |
| 481 | * callback returns 1 in case it wants to spill more buffers |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 482 | * |
| 483 | * This takes care of holding the buffer if send is incomplete, ie, |
| 484 | * if wsi->u.ws.clean_buffer is 0 (meaning an extension meddled with |
| 485 | * the buffer). If wsi->u.ws.clean_buffer is 1, it will instead |
| 486 | * 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] | 487 | */ |
| 488 | |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 489 | n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre + post); |
Andy Green | 03cf1dd | 2014-04-01 14:20:44 +0800 | [diff] [blame] | 490 | if (n <= 0) |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 491 | return n; |
| 492 | |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 493 | if (n == (int)len + pre + post) { |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 494 | /* everything in the buffer was handled (or rebuffered...) */ |
| 495 | wsi->u.ws.inside_frame = 0; |
| 496 | return orig_len; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * it is how many bytes of user buffer got sent... may be < orig_len |
| 501 | * in which case callback when writable has already been arranged |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 502 | * and user code can call lws_write() again with the rest |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 503 | * later. |
| 504 | */ |
| 505 | |
| 506 | return n - (pre + post); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 507 | } |
| 508 | |
Andy Green | 5c9660d | 2015-12-04 11:30:53 +0800 | [diff] [blame] | 509 | LWS_VISIBLE int lws_serve_http_file_fragment(struct lws_context *context, |
| 510 | struct lws *wsi) |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 511 | { |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 512 | int n; |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 513 | int m; |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 514 | |
| 515 | while (!lws_send_pipe_choked(wsi)) { |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 516 | |
Andy Green | e254d95 | 2014-03-23 11:41:15 +0800 | [diff] [blame] | 517 | if (wsi->truncated_send_len) { |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 518 | if (lws_issue_raw(wsi, wsi->truncated_send_malloc + |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 519 | wsi->truncated_send_offset, |
| 520 | wsi->truncated_send_len) < 0) { |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 521 | lwsl_info("closing from lws_serve_http_file_fragment\n"); |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 522 | return -1; |
| 523 | } |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 524 | continue; |
| 525 | } |
| 526 | |
| 527 | if (wsi->u.http.filepos == wsi->u.http.filelen) |
| 528 | goto all_sent; |
| 529 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 530 | compatible_file_read(n, wsi->u.http.fd, context->service_buffer, |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 531 | sizeof(context->service_buffer)); |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 532 | if (n < 0) |
| 533 | return -1; /* caller will close */ |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 534 | if (n) { |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 535 | lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, |
| 536 | AWAITING_TIMEOUT); |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 537 | wsi->u.http.filepos += n; |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 538 | m = lws_write(wsi, context->service_buffer, n, |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 539 | wsi->u.http.filepos == wsi->u.http.filelen ? |
| 540 | LWS_WRITE_HTTP_FINAL : LWS_WRITE_HTTP); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 541 | if (m < 0) |
| 542 | return -1; |
| 543 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 544 | if (m != n) |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 545 | /* adjust for what was not sent */ |
Andy Green | 7ef4b2e | 2014-11-30 13:00:47 +0800 | [diff] [blame] | 546 | if (compatible_file_seek_cur(wsi->u.http.fd, m - n) < 0) |
| 547 | return -1; |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 548 | } |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 549 | all_sent: |
Andy Green | e254d95 | 2014-03-23 11:41:15 +0800 | [diff] [blame] | 550 | if (!wsi->truncated_send_len && |
Andy Green | d607bb9 | 2015-12-06 05:52:09 +0800 | [diff] [blame^] | 551 | wsi->u.http.filepos == wsi->u.http.filelen) { |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 552 | wsi->state = WSI_STATE_HTTP; |
| 553 | |
Andy Green | c6f95d3 | 2015-10-27 07:07:14 +0800 | [diff] [blame] | 554 | /* we might be in keepalive, so close it off here */ |
| 555 | compatible_file_close(wsi->u.http.fd); |
| 556 | wsi->u.http.fd = LWS_INVALID_FILE; |
| 557 | |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 558 | if (wsi->protocol->callback) |
David Gauchard | 6c58228 | 2013-06-29 10:24:16 +0800 | [diff] [blame] | 559 | /* ignore callback returned value */ |
| 560 | user_callback_handle_rxflow( |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 561 | wsi->protocol->callback, context, wsi, |
| 562 | LWS_CALLBACK_HTTP_FILE_COMPLETION, |
| 563 | wsi->user_space, NULL, 0); |
David Gauchard | 6c58228 | 2013-06-29 10:24:16 +0800 | [diff] [blame] | 564 | return 1; /* >0 indicates completed */ |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 568 | lwsl_info("choked before able to send whole file (post)\n"); |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 569 | lws_callback_on_writable(context, wsi); |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 570 | |
David Gauchard | 6c58228 | 2013-06-29 10:24:16 +0800 | [diff] [blame] | 571 | return 0; /* indicates further processing must be done */ |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 572 | } |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 573 | |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 574 | #if LWS_POSIX |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 575 | LWS_VISIBLE int |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 576 | lws_ssl_capable_read_no_ssl(struct lws_context *context, |
| 577 | struct lws *wsi, unsigned char *buf, int len) |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 578 | { |
| 579 | int n; |
| 580 | |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 581 | (void)context; |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 582 | |
Stephan Eberle | b820e2c | 2015-10-23 08:10:55 +0200 | [diff] [blame] | 583 | n = recv(wsi->sock, (char *)buf, len, 0); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 584 | if (n >= 0) |
| 585 | return n; |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 586 | #if LWS_POSIX |
Mark Likness | dbe624d | 2015-10-15 21:21:06 +0800 | [diff] [blame] | 587 | if (LWS_ERRNO == LWS_EAGAIN || |
| 588 | LWS_ERRNO == LWS_EWOULDBLOCK || |
| 589 | LWS_ERRNO == LWS_EINTR) |
| 590 | return LWS_SSL_CAPABLE_MORE_SERVICE; |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 591 | #endif |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 592 | lwsl_warn("error on reading from skt\n"); |
| 593 | return LWS_SSL_CAPABLE_ERROR; |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | LWS_VISIBLE int |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 597 | 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] | 598 | { |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 599 | int n = 0; |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 600 | |
| 601 | #if LWS_POSIX |
Stephan Eberle | b820e2c | 2015-10-23 08:10:55 +0200 | [diff] [blame] | 602 | n = send(wsi->sock, (char *)buf, len, MSG_NOSIGNAL); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 603 | if (n >= 0) |
| 604 | return n; |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 605 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 606 | if (LWS_ERRNO == LWS_EAGAIN || |
| 607 | LWS_ERRNO == LWS_EWOULDBLOCK || |
| 608 | LWS_ERRNO == LWS_EINTR) { |
| 609 | if (LWS_ERRNO == LWS_EWOULDBLOCK) |
| 610 | lws_set_blocking_send(wsi); |
| 611 | |
| 612 | return LWS_SSL_CAPABLE_MORE_SERVICE; |
| 613 | } |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 614 | #else |
| 615 | (void)n; |
| 616 | (void)wsi; |
| 617 | (void)buf; |
| 618 | (void)len; |
| 619 | // !!! |
| 620 | #endif |
| 621 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 622 | lwsl_debug("ERROR writing len %d to skt %d\n", len, n); |
| 623 | return LWS_SSL_CAPABLE_ERROR; |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 624 | } |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 625 | #endif |
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= | 4c0ba02 | 2015-08-19 16:23:33 +0200 | [diff] [blame] | 626 | LWS_VISIBLE int |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 627 | 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] | 628 | { |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 629 | (void)wsi; |
=?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= | 4c0ba02 | 2015-08-19 16:23:33 +0200 | [diff] [blame] | 630 | return 0; |
| 631 | } |