Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets - small server side websockets and web server implementation |
| 3 | * |
| 4 | * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com> |
| 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 | |
| 24 | #ifdef WIN32 |
| 25 | #include <io.h> |
| 26 | #endif |
| 27 | |
| 28 | static int |
| 29 | libwebsocket_0405_frame_mask_generate(struct libwebsocket *wsi) |
| 30 | { |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 31 | int n; |
| 32 | |
| 33 | /* fetch the per-frame nonce */ |
| 34 | |
| 35 | n = libwebsockets_get_random(wsi->protocol->owning_server, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 36 | wsi->u.ws.frame_masking_nonce_04, 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", |
| 39 | SYSTEM_RANDOM_FILEPATH, n); |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | /* start masking from first byte of masking key buffer */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 44 | wsi->u.ws.frame_mask_index = 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 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 51 | void lwsl_hexdump(void *vbuf, size_t len) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 52 | { |
| 53 | int n; |
| 54 | int m; |
| 55 | int start; |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 56 | unsigned char *buf = (unsigned char *)vbuf; |
| 57 | char line[80]; |
| 58 | char *p; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 59 | |
| 60 | lwsl_parser("\n"); |
| 61 | |
| 62 | for (n = 0; n < len;) { |
| 63 | start = n; |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 64 | p = line; |
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, "%04X: ", start); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 67 | |
| 68 | for (m = 0; m < 16 && n < len; m++) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 69 | p += sprintf(p, "%02X ", buf[n++]); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 70 | while (m++ < 16) |
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 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 73 | p += sprintf(p, " "); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 74 | |
| 75 | for (m = 0; m < 16 && (start + m) < len; m++) { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 76 | if (buf[start + m] >= ' ' && buf[start + m] < 127) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 77 | *p++ = buf[start + m]; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 78 | else |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 79 | *p++ = '.'; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 80 | } |
| 81 | while (m++ < 16) |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 82 | *p++ = ' '; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 83 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 84 | *p++ = '\n'; |
| 85 | *p = '\0'; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 86 | lwsl_debug("%s", line); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 87 | } |
| 88 | lwsl_debug("\n"); |
| 89 | } |
| 90 | |
Andy Green | cf3590e | 2013-01-16 14:35:27 +0800 | [diff] [blame] | 91 | #endif |
| 92 | |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 93 | /* |
| 94 | * notice this returns number of bytes sent, or -1 |
| 95 | */ |
| 96 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 97 | int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) |
| 98 | { |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 99 | struct libwebsocket_context *context = wsi->protocol->owning_server; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 100 | int n; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 101 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 102 | int m; |
| 103 | |
| 104 | /* |
| 105 | * one of the extensions is carrying our data itself? Like mux? |
| 106 | */ |
| 107 | |
| 108 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 109 | /* |
| 110 | * there can only be active extensions after handshake completed |
| 111 | * so we can rely on protocol being set already in here |
| 112 | */ |
| 113 | m = wsi->active_extensions[n]->callback( |
| 114 | wsi->protocol->owning_server, |
| 115 | wsi->active_extensions[n], wsi, |
| 116 | LWS_EXT_CALLBACK_PACKET_TX_DO_SEND, |
| 117 | wsi->active_extensions_user[n], &buf, len); |
| 118 | if (m < 0) { |
| 119 | lwsl_ext("Extension reports fatal error\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | if (m) /* handled */ { |
| 123 | /* lwsl_ext("ext sent it\n"); */ |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 124 | return m; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 125 | } |
| 126 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 127 | #endif |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 128 | if (!wsi->sock) |
| 129 | lwsl_warn("** error 0 sock but expected to send\n"); |
| 130 | |
| 131 | /* |
| 132 | * nope, send it on the socket directly |
| 133 | */ |
| 134 | |
| 135 | #if 0 |
| 136 | lwsl_debug(" TX: "); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 137 | lws_hexdump(buf, len); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 138 | #endif |
| 139 | |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 140 | lws_latency_pre(context, wsi); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 141 | #ifdef LWS_OPENSSL_SUPPORT |
| 142 | if (wsi->ssl) { |
| 143 | n = SSL_write(wsi->ssl, buf, len); |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 144 | lws_latency(context, wsi, "SSL_write lws_issue_raw", n, n >= 0); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 145 | if (n < 0) { |
| 146 | lwsl_debug("ERROR writing to socket\n"); |
| 147 | return -1; |
| 148 | } |
| 149 | } else { |
| 150 | #endif |
| 151 | n = send(wsi->sock, buf, len, MSG_NOSIGNAL); |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 152 | lws_latency(context, wsi, "send lws_issue_raw", n, n == len); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 153 | if (n < 0) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 154 | lwsl_debug("ERROR writing len %d to skt %d\n", len, n); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 155 | return -1; |
| 156 | } |
| 157 | #ifdef LWS_OPENSSL_SUPPORT |
| 158 | } |
| 159 | #endif |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 160 | return n; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 163 | #ifdef LWS_NO_EXTENSIONS |
| 164 | int |
| 165 | lws_issue_raw_ext_access(struct libwebsocket *wsi, |
| 166 | unsigned char *buf, size_t len) |
| 167 | { |
| 168 | return lws_issue_raw(wsi, buf, len); |
| 169 | } |
| 170 | #else |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 171 | int |
| 172 | lws_issue_raw_ext_access(struct libwebsocket *wsi, |
| 173 | unsigned char *buf, size_t len) |
| 174 | { |
| 175 | int ret; |
| 176 | struct lws_tokens eff_buf; |
| 177 | int m; |
| 178 | int n; |
| 179 | |
| 180 | eff_buf.token = (char *)buf; |
| 181 | eff_buf.token_len = len; |
| 182 | |
| 183 | /* |
| 184 | * while we have original buf to spill ourselves, or extensions report |
| 185 | * more in their pipeline |
| 186 | */ |
| 187 | |
| 188 | ret = 1; |
| 189 | while (ret == 1) { |
| 190 | |
| 191 | /* default to nobody has more to spill */ |
| 192 | |
| 193 | ret = 0; |
| 194 | |
| 195 | /* show every extension the new incoming data */ |
| 196 | |
| 197 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 198 | m = wsi->active_extensions[n]->callback( |
| 199 | wsi->protocol->owning_server, |
| 200 | wsi->active_extensions[n], wsi, |
| 201 | LWS_EXT_CALLBACK_PACKET_TX_PRESEND, |
| 202 | wsi->active_extensions_user[n], &eff_buf, 0); |
| 203 | if (m < 0) { |
| 204 | lwsl_ext("Extension: fatal error\n"); |
| 205 | return -1; |
| 206 | } |
| 207 | if (m) |
| 208 | /* |
| 209 | * at least one extension told us he has more |
| 210 | * to spill, so we will go around again after |
| 211 | */ |
| 212 | ret = 1; |
| 213 | } |
| 214 | |
| 215 | /* assuming they left us something to send, send it */ |
| 216 | |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 217 | if (eff_buf.token_len) { |
| 218 | n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token, |
| 219 | eff_buf.token_len); |
| 220 | if (n < 0) |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 221 | return -1; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 222 | /* |
| 223 | * Keep amount spilled small to minimize chance of this |
| 224 | */ |
| 225 | if (n != eff_buf.token_len) { |
Henrik Abelsson | 2bdbe7a | 2013-03-07 15:52:53 +0100 | [diff] [blame] | 226 | lwsl_err("Unable to spill ext %d vs %d\n", |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 227 | eff_buf.token_len, n); |
| 228 | return -1; |
| 229 | } |
| 230 | |
| 231 | } |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 232 | |
| 233 | lwsl_parser("written %d bytes to client\n", eff_buf.token_len); |
| 234 | |
| 235 | /* no extension has more to spill */ |
| 236 | |
| 237 | if (!ret) |
| 238 | break; |
| 239 | |
| 240 | /* we used up what we had */ |
| 241 | |
| 242 | eff_buf.token = NULL; |
| 243 | eff_buf.token_len = 0; |
| 244 | |
| 245 | /* |
| 246 | * Did that leave the pipe choked? |
| 247 | */ |
| 248 | |
| 249 | if (!lws_send_pipe_choked(wsi)) |
| 250 | /* no we could add more */ |
| 251 | continue; |
| 252 | |
| 253 | lwsl_debug("choked\n"); |
| 254 | |
| 255 | /* |
| 256 | * Yes, he's choked. Don't spill the rest now get a callback |
| 257 | * when he is ready to send and take care of it there |
| 258 | */ |
| 259 | libwebsocket_callback_on_writable( |
| 260 | wsi->protocol->owning_server, wsi); |
| 261 | wsi->extension_data_pending = 1; |
| 262 | ret = 0; |
| 263 | } |
| 264 | |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 265 | return len; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 266 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 267 | #endif |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * libwebsocket_write() - Apply protocol then write data to client |
| 271 | * @wsi: Websocket instance (available from user callback) |
| 272 | * @buf: The data to send. For data being sent on a websocket |
| 273 | * connection (ie, not default http), this buffer MUST have |
| 274 | * LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer |
| 275 | * and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid |
| 276 | * in the buffer after (buf + len). This is so the protocol |
| 277 | * header and trailer data can be added in-situ. |
| 278 | * @len: Count of the data bytes in the payload starting from buf |
| 279 | * @protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one |
| 280 | * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate |
| 281 | * data on a websockets connection. Remember to allow the extra |
| 282 | * bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT |
| 283 | * are used. |
| 284 | * |
| 285 | * This function provides the way to issue data back to the client |
| 286 | * for both http and websocket protocols. |
| 287 | * |
| 288 | * In the case of sending using websocket protocol, be sure to allocate |
| 289 | * valid storage before and after buf as explained above. This scheme |
| 290 | * allows maximum efficiency of sending data and protocol in a single |
| 291 | * packet while not burdening the user code with any protocol knowledge. |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 292 | * |
| 293 | * Return may be -1 for a fatal error needing connection close, or a |
| 294 | * positive number reflecting the amount of bytes actually sent. This |
| 295 | * can be less than the requested number of bytes due to OS memory |
| 296 | * pressure at any given time. |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 297 | */ |
| 298 | |
| 299 | int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, |
| 300 | size_t len, enum libwebsocket_write_protocol protocol) |
| 301 | { |
| 302 | int n; |
| 303 | int pre = 0; |
| 304 | int post = 0; |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 305 | int masked7 = wsi->mode == LWS_CONNMODE_WS_CLIENT; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 306 | unsigned char *dropmask = NULL; |
| 307 | unsigned char is_masked_bit = 0; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 308 | size_t orig_len = len; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 309 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 310 | struct lws_tokens eff_buf; |
| 311 | int m; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 312 | #endif |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 313 | |
| 314 | if (len == 0 && protocol != LWS_WRITE_CLOSE) { |
| 315 | lwsl_warn("zero length libwebsocket_write attempt\n"); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | if (protocol == LWS_WRITE_HTTP) |
| 320 | goto send_raw; |
| 321 | |
| 322 | /* websocket protocol, either binary or text */ |
| 323 | |
| 324 | if (wsi->state != WSI_STATE_ESTABLISHED) |
| 325 | return -1; |
| 326 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 327 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 328 | /* give a change to the extensions to modify payload */ |
| 329 | eff_buf.token = (char *)buf; |
| 330 | eff_buf.token_len = len; |
| 331 | |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 332 | switch (protocol) { |
| 333 | case LWS_WRITE_PING: |
| 334 | case LWS_WRITE_PONG: |
| 335 | case LWS_WRITE_CLOSE: |
| 336 | break; |
| 337 | default: |
Andy Green | f7248f8 | 2013-01-16 14:35:12 +0800 | [diff] [blame] | 338 | |
| 339 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 340 | m = wsi->active_extensions[n]->callback( |
| 341 | wsi->protocol->owning_server, |
| 342 | wsi->active_extensions[n], wsi, |
| 343 | LWS_EXT_CALLBACK_PAYLOAD_TX, |
| 344 | wsi->active_extensions_user[n], &eff_buf, 0); |
| 345 | if (m < 0) |
| 346 | return -1; |
| 347 | } |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | buf = (unsigned char *)eff_buf.token; |
| 351 | len = eff_buf.token_len; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 352 | #endif |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 353 | |
| 354 | switch (wsi->ietf_spec_revision) { |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 355 | case 13: |
| 356 | if (masked7) { |
| 357 | pre += 4; |
| 358 | dropmask = &buf[0 - pre]; |
| 359 | is_masked_bit = 0x80; |
| 360 | } |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 361 | |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 362 | switch (protocol & 0xf) { |
| 363 | case LWS_WRITE_TEXT: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 364 | n = LWS_WS_OPCODE_07__TEXT_FRAME; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 365 | break; |
| 366 | case LWS_WRITE_BINARY: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 367 | n = LWS_WS_OPCODE_07__BINARY_FRAME; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 368 | break; |
| 369 | case LWS_WRITE_CONTINUATION: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 370 | n = LWS_WS_OPCODE_07__CONTINUATION; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 371 | break; |
| 372 | |
| 373 | case LWS_WRITE_CLOSE: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 374 | n = LWS_WS_OPCODE_07__CLOSE; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 375 | |
| 376 | /* |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 377 | * 06+ has a 2-byte status code in network order |
| 378 | * we can do this because we demand post-buf |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 379 | */ |
| 380 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 381 | if (wsi->u.ws.close_reason) { |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 382 | /* reason codes count as data bytes */ |
| 383 | buf -= 2; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 384 | buf[0] = wsi->u.ws.close_reason >> 8; |
| 385 | buf[1] = wsi->u.ws.close_reason; |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 386 | len += 2; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 387 | } |
| 388 | break; |
| 389 | case LWS_WRITE_PING: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 390 | n = LWS_WS_OPCODE_07__PING; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 391 | break; |
| 392 | case LWS_WRITE_PONG: |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 393 | n = LWS_WS_OPCODE_07__PONG; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 394 | break; |
| 395 | default: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 396 | lwsl_warn("lws_write: unknown write opc / protocol\n"); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | if (!(protocol & LWS_WRITE_NO_FIN)) |
| 401 | n |= 1 << 7; |
| 402 | |
| 403 | if (len < 126) { |
| 404 | pre += 2; |
| 405 | buf[-pre] = n; |
| 406 | buf[-pre + 1] = len | is_masked_bit; |
| 407 | } else { |
| 408 | if (len < 65536) { |
| 409 | pre += 4; |
| 410 | buf[-pre] = n; |
| 411 | buf[-pre + 1] = 126 | is_masked_bit; |
| 412 | buf[-pre + 2] = len >> 8; |
| 413 | buf[-pre + 3] = len; |
| 414 | } else { |
| 415 | pre += 10; |
| 416 | buf[-pre] = n; |
| 417 | buf[-pre + 1] = 127 | is_masked_bit; |
| 418 | #if defined __LP64__ |
| 419 | buf[-pre + 2] = (len >> 56) & 0x7f; |
| 420 | buf[-pre + 3] = len >> 48; |
| 421 | buf[-pre + 4] = len >> 40; |
| 422 | buf[-pre + 5] = len >> 32; |
| 423 | #else |
| 424 | buf[-pre + 2] = 0; |
| 425 | buf[-pre + 3] = 0; |
| 426 | buf[-pre + 4] = 0; |
| 427 | buf[-pre + 5] = 0; |
| 428 | #endif |
| 429 | buf[-pre + 6] = len >> 24; |
| 430 | buf[-pre + 7] = len >> 16; |
| 431 | buf[-pre + 8] = len >> 8; |
| 432 | buf[-pre + 9] = len; |
| 433 | } |
| 434 | } |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Deal with masking if we are in client -> server direction and |
| 440 | * the protocol demands it |
| 441 | */ |
| 442 | |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 443 | if (wsi->mode == LWS_CONNMODE_WS_CLIENT) { |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 444 | |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 445 | if (libwebsocket_0405_frame_mask_generate(wsi)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 446 | lwsl_err("lws_write: frame mask generation failed\n"); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 447 | return -1; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 448 | } |
| 449 | |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 450 | /* |
| 451 | * in v7, just mask the payload |
| 452 | */ |
| 453 | for (n = 4; n < (int)len + 4; n++) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 454 | dropmask[n] = dropmask[n] ^ |
| 455 | wsi->u.ws.frame_masking_nonce_04[ |
| 456 | (wsi->u.ws.frame_mask_index++) & 3]; |
Andy Green | 5738c0e | 2013-01-21 09:53:35 +0800 | [diff] [blame] | 457 | |
| 458 | if (dropmask) |
| 459 | /* copy the frame nonce into place */ |
| 460 | memcpy(dropmask, |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 461 | wsi->u.ws.frame_masking_nonce_04, 4); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | send_raw: |
| 465 | |
| 466 | #if 0 |
| 467 | lwsl_debug("send %ld: ", len + post); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 468 | lwsl_hexdump(&buf[-pre], len + post); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 469 | #endif |
| 470 | |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 471 | switch (protocol) { |
| 472 | case LWS_WRITE_CLOSE: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 473 | /* lwsl_hexdump(&buf[-pre], len + post); */ |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 474 | case LWS_WRITE_HTTP: |
| 475 | case LWS_WRITE_PONG: |
| 476 | case LWS_WRITE_PING: |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 477 | return lws_issue_raw(wsi, (unsigned char *)buf - pre, |
| 478 | len + pre + post); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 479 | default: |
| 480 | break; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /* |
| 484 | * give any active extensions a chance to munge the buffer |
| 485 | * before send. We pass in a pointer to an lws_tokens struct |
| 486 | * prepared with the default buffer and content length that's in |
| 487 | * there. Rather than rewrite the default buffer, extensions |
| 488 | * that expect to grow the buffer can adapt .token to |
| 489 | * point to their own per-connection buffer in the extension |
| 490 | * user allocation. By default with no extensions or no |
| 491 | * extension callback handling, just the normal input buffer is |
| 492 | * used then so it is efficient. |
| 493 | * |
| 494 | * callback returns 1 in case it wants to spill more buffers |
| 495 | */ |
| 496 | |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 497 | n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre + post); |
| 498 | if (n < 0) |
| 499 | return n; |
| 500 | |
| 501 | return orig_len - ((len - pre + post) -n ); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 502 | } |
| 503 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 504 | int libwebsockets_serve_http_file_fragment( |
| 505 | struct libwebsocket_context *context, struct libwebsocket *wsi) |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 506 | { |
| 507 | int ret = 0; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 508 | int n, m; |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 509 | |
| 510 | while (!lws_send_pipe_choked(wsi)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 511 | n = read(wsi->u.http.fd, context->service_buffer, |
| 512 | sizeof(context->service_buffer)); |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 513 | if (n > 0) { |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 514 | m = libwebsocket_write(wsi, context->service_buffer, n, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 515 | LWS_WRITE_HTTP); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 516 | if (m < 0) |
| 517 | return -1; |
| 518 | |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 519 | wsi->u.http.filepos += n; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 520 | if (m != n) |
| 521 | /* adjust for what was not sent */ |
| 522 | lseek(wsi->u.http.fd, m - n, SEEK_CUR); |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 523 | } |
| 524 | |
Andy Green | 5ab9c68 | 2013-02-10 20:58:04 +0800 | [diff] [blame] | 525 | if (n < 0) |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 526 | return -1; /* caller will close */ |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 527 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 528 | if (n < sizeof(context->service_buffer) || |
| 529 | wsi->u.http.filepos == wsi->u.http.filelen) { |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 530 | wsi->state = WSI_STATE_HTTP; |
| 531 | |
| 532 | if (wsi->protocol->callback) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 533 | ret = user_callback_handle_rxflow( |
| 534 | wsi->protocol->callback, context, wsi, |
| 535 | LWS_CALLBACK_HTTP_FILE_COMPLETION, |
| 536 | wsi->user_space, NULL, 0); |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 537 | return ret; |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | lwsl_notice("choked before able to send whole file (post)\n"); |
| 542 | libwebsocket_callback_on_writable(context, wsi); |
| 543 | |
| 544 | return ret; |
| 545 | } |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 546 | |
| 547 | /** |
| 548 | * libwebsockets_serve_http_file() - Send a file back to the client using http |
| 549 | * @context: libwebsockets context |
| 550 | * @wsi: Websocket instance (available from user callback) |
| 551 | * @file: The file to issue over http |
| 552 | * @content_type: The http content type, eg, text/html |
| 553 | * |
| 554 | * This function is intended to be called from the callback in response |
| 555 | * to http requests from the client. It allows the callback to issue |
| 556 | * local files down the http link in a single step. |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 557 | * |
| 558 | * Returning <0 indicates error and the wsi should be closed. Returning |
| 559 | * >0 indicates the file was completely sent and the wsi should be closed. |
| 560 | * ==0 indicates the file transfer is started and needs more service later, |
| 561 | * the wsi should be left alone. |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 562 | */ |
| 563 | |
| 564 | int libwebsockets_serve_http_file(struct libwebsocket_context *context, |
| 565 | struct libwebsocket *wsi, const char *file, |
| 566 | const char *content_type) |
| 567 | { |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 568 | struct stat stat_buf; |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 569 | unsigned char *p = context->service_buffer; |
| 570 | int ret = 0; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 571 | |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 572 | wsi->u.http.fd = open(file, O_RDONLY |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 573 | #ifdef WIN32 |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 574 | | _O_BINARY |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 575 | #endif |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 576 | ); |
| 577 | |
| 578 | if (wsi->u.http.fd < 1) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 579 | p += sprintf((char *)p, |
| 580 | "HTTP/1.0 400 Bad\x0d\x0aServer: libwebsockets\x0d\x0a\x0d\x0a" |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 581 | ); |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 582 | wsi->u.http.fd = 0; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 583 | /* too small to care about partial, closing anyway */ |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 584 | libwebsocket_write(wsi, context->service_buffer, |
| 585 | p - context->service_buffer, LWS_WRITE_HTTP); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 586 | |
| 587 | return -1; |
| 588 | } |
| 589 | |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 590 | fstat(wsi->u.http.fd, &stat_buf); |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 591 | wsi->u.http.filelen = stat_buf.st_size; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 592 | p += sprintf((char *)p, |
Andy Green | d579a7d | 2013-02-12 12:53:36 +0800 | [diff] [blame] | 593 | "HTTP/1.0 200 OK\x0d\x0aServer: libwebsockets\x0d\x0a""Content-Type: %s\x0d\x0a", |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 594 | content_type); |
| 595 | p += sprintf((char *)p, |
| 596 | "Content-Length: %u\x0d\x0a\x0d\x0a", |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 597 | (unsigned int)stat_buf.st_size); |
| 598 | |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 599 | ret = libwebsocket_write(wsi, context->service_buffer, |
| 600 | p - context->service_buffer, LWS_WRITE_HTTP); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 601 | if (ret != (p - context->service_buffer)) { |
| 602 | lwsl_err("_write returned %d from %d\n", ret, (p - context->service_buffer)); |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 603 | return -1; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 604 | } |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 605 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 606 | wsi->u.http.filepos = 0; |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 607 | wsi->state = WSI_STATE_HTTP_ISSUING_FILE; |
| 608 | |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 609 | return libwebsockets_serve_http_file_fragment(context, wsi); |
Andy Green | b429d48 | 2013-01-16 12:21:29 +0800 | [diff] [blame] | 610 | } |
| 611 | |