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