Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets - small server side websockets and web server implementation |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 3 | * |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 4 | * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [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 | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 24 | unsigned char lextable[] = { |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 25 | #include "lextable.h" |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 26 | }; |
| 27 | |
Andy Green | bbc5c07 | 2014-03-09 11:49:21 +0800 | [diff] [blame] | 28 | #define FAIL_CHAR 0x08 |
| 29 | |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 30 | int lextable_decode(int pos, char c) |
| 31 | { |
Andy Green | bbc5c07 | 2014-03-09 11:49:21 +0800 | [diff] [blame] | 32 | |
| 33 | c = tolower(c); |
| 34 | |
| 35 | while (1) { |
| 36 | if (lextable[pos] & (1 << 7)) { /* 1-byte, fail on mismatch */ |
| 37 | if ((lextable[pos] & 0x7f) != c) |
| 38 | return -1; |
| 39 | /* fall thru */ |
| 40 | pos++; |
| 41 | if (lextable[pos] == FAIL_CHAR) |
| 42 | return -1; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 43 | return pos; |
Andy Green | bbc5c07 | 2014-03-09 11:49:21 +0800 | [diff] [blame] | 44 | } |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 45 | /* b7 = 0, end or 3-byte */ |
| 46 | if (lextable[pos] < FAIL_CHAR) /* terminal marker */ |
| 47 | return pos; |
| 48 | |
| 49 | if (lextable[pos] == c) /* goto */ |
| 50 | return pos + (lextable[pos + 1]) + |
| 51 | (lextable[pos + 2] << 8); |
| 52 | /* fall thru goto */ |
| 53 | pos += 3; |
| 54 | /* continue */ |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 55 | } |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 56 | } |
| 57 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 58 | int lws_allocate_header_table(struct libwebsocket *wsi) |
| 59 | { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 60 | wsi->u.hdr.ah = malloc(sizeof(*wsi->u.hdr.ah)); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 61 | if (wsi->u.hdr.ah == NULL) { |
| 62 | lwsl_err("Out of memory\n"); |
| 63 | return -1; |
| 64 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 65 | memset(wsi->u.hdr.ah->frag_index, 0, sizeof(wsi->u.hdr.ah->frag_index)); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 66 | wsi->u.hdr.ah->next_frag_index = 0; |
| 67 | wsi->u.hdr.ah->pos = 0; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 68 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 72 | LWS_VISIBLE int lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 73 | { |
| 74 | int n; |
| 75 | int len = 0; |
| 76 | |
| 77 | n = wsi->u.hdr.ah->frag_index[h]; |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 78 | if (!n) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 79 | return 0; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 80 | do { |
| 81 | len += wsi->u.hdr.ah->frags[n].len; |
| 82 | n = wsi->u.hdr.ah->frags[n].next_frag_index; |
| 83 | } while (n); |
| 84 | |
| 85 | return len; |
| 86 | } |
| 87 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 88 | LWS_VISIBLE int lws_hdr_copy(struct libwebsocket *wsi, char *dest, int len, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 89 | enum lws_token_indexes h) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 90 | { |
| 91 | int toklen = lws_hdr_total_length(wsi, h); |
| 92 | int n; |
| 93 | |
| 94 | if (toklen >= len) |
| 95 | return -1; |
| 96 | |
| 97 | n = wsi->u.hdr.ah->frag_index[h]; |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 98 | if (!n) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 99 | return 0; |
| 100 | |
| 101 | do { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 102 | strcpy(dest, |
| 103 | &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 104 | dest += wsi->u.hdr.ah->frags[n].len; |
| 105 | n = wsi->u.hdr.ah->frags[n].next_frag_index; |
| 106 | } while (n); |
| 107 | |
| 108 | return toklen; |
| 109 | } |
| 110 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 111 | char *lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 112 | { |
| 113 | int n; |
| 114 | |
| 115 | n = wsi->u.hdr.ah->frag_index[h]; |
| 116 | if (!n) |
| 117 | return NULL; |
| 118 | |
| 119 | return &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]; |
| 120 | } |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 121 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 122 | int lws_hdr_simple_create(struct libwebsocket *wsi, |
| 123 | enum lws_token_indexes h, const char *s) |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 124 | { |
| 125 | wsi->u.hdr.ah->next_frag_index++; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 126 | if (wsi->u.hdr.ah->next_frag_index == |
| 127 | sizeof(wsi->u.hdr.ah->frags) / sizeof(wsi->u.hdr.ah->frags[0])) { |
| 128 | lwsl_warn("More hdr frags than we can deal with, dropping\n"); |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->next_frag_index; |
| 133 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 134 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset = |
| 135 | wsi->u.hdr.ah->pos; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 136 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 137 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].next_frag_index = |
| 138 | 0; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 139 | |
| 140 | do { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 141 | if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) { |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 142 | lwsl_err("Ran out of header data space\n"); |
| 143 | return -1; |
| 144 | } |
| 145 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s; |
| 146 | if (*s) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 147 | wsi->u.hdr.ah->frags[ |
| 148 | wsi->u.hdr.ah->next_frag_index].len++; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 149 | } while (*s++); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 154 | static char char_to_hex(const char c) |
| 155 | { |
| 156 | if (c >= '0' && c <= '9') |
| 157 | return c - '0'; |
| 158 | |
| 159 | if (c >= 'a' && c <= 'f') |
| 160 | return c - 'a' + 10; |
| 161 | |
| 162 | if (c >= 'A' && c <= 'F') |
| 163 | return c - 'A' + 10; |
| 164 | |
| 165 | return -1; |
| 166 | } |
| 167 | |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 168 | static int issue_char(struct libwebsocket *wsi, unsigned char c) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 169 | { |
| 170 | if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) { |
| 171 | lwsl_warn("excessive header content\n"); |
| 172 | return -1; |
| 173 | } |
Andrew Canaday | 74b4a65 | 2014-06-29 00:25:19 -0400 | [diff] [blame] | 174 | |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 175 | if( wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len >= |
| 176 | wsi->u.hdr.current_token_limit) { |
Andrew Canaday | 74b4a65 | 2014-06-29 00:25:19 -0400 | [diff] [blame] | 177 | lwsl_warn("header %i exceeds limit\n", wsi->u.hdr.parser_state); |
| 178 | return 1; |
| 179 | }; |
| 180 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 181 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c; |
| 182 | if (c) |
| 183 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++; |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Andrew Canaday | 74b4a65 | 2014-06-29 00:25:19 -0400 | [diff] [blame] | 188 | int libwebsocket_parse( |
| 189 | struct libwebsocket_context *context, |
| 190 | struct libwebsocket *wsi, unsigned char c) |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 191 | { |
| 192 | int n; |
| 193 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 194 | switch (wsi->u.hdr.parser_state) { |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 195 | case WSI_TOKEN_GET_URI: |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 196 | case WSI_TOKEN_POST_URI: |
Andrew Canaday | da55fb5 | 2014-07-05 10:04:19 +0800 | [diff] [blame] | 197 | case WSI_TOKEN_OPTIONS_URI: |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 198 | case WSI_TOKEN_HOST: |
| 199 | case WSI_TOKEN_CONNECTION: |
| 200 | case WSI_TOKEN_KEY1: |
| 201 | case WSI_TOKEN_KEY2: |
| 202 | case WSI_TOKEN_PROTOCOL: |
| 203 | case WSI_TOKEN_UPGRADE: |
| 204 | case WSI_TOKEN_ORIGIN: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 205 | case WSI_TOKEN_SWORIGIN: |
Andy Green | ce510c6 | 2010-11-11 12:48:13 +0000 | [diff] [blame] | 206 | case WSI_TOKEN_DRAFT: |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 207 | case WSI_TOKEN_CHALLENGE: |
Andy Green | d1b11e3 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 208 | case WSI_TOKEN_KEY: |
| 209 | case WSI_TOKEN_VERSION: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 210 | case WSI_TOKEN_ACCEPT: |
| 211 | case WSI_TOKEN_NONCE: |
Andy Green | 18910c5 | 2011-02-09 08:58:42 +0000 | [diff] [blame] | 212 | case WSI_TOKEN_EXTENSIONS: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 213 | case WSI_TOKEN_HTTP: |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 214 | case WSI_TOKEN_HTTP_ACCEPT: |
Andrew Canaday | 4220327 | 2014-06-28 20:19:57 -0400 | [diff] [blame] | 215 | case WSI_TOKEN_HTTP_AC_REQUEST_HEADERS: |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 216 | case WSI_TOKEN_HTTP_IF_MODIFIED_SINCE: |
Andrew Canaday | 4220327 | 2014-06-28 20:19:57 -0400 | [diff] [blame] | 217 | case WSI_TOKEN_HTTP_IF_NONE_MATCH: |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 218 | case WSI_TOKEN_HTTP_ACCEPT_ENCODING: |
| 219 | case WSI_TOKEN_HTTP_ACCEPT_LANGUAGE: |
| 220 | case WSI_TOKEN_HTTP_PRAGMA: |
| 221 | case WSI_TOKEN_HTTP_CACHE_CONTROL: |
| 222 | case WSI_TOKEN_HTTP_AUTHORIZATION: |
| 223 | case WSI_TOKEN_HTTP_COOKIE: |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 224 | case WSI_TOKEN_HTTP_CONTENT_LENGTH: |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 225 | case WSI_TOKEN_HTTP_CONTENT_TYPE: |
| 226 | case WSI_TOKEN_HTTP_DATE: |
| 227 | case WSI_TOKEN_HTTP_RANGE: |
| 228 | case WSI_TOKEN_HTTP_REFERER: |
| 229 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 230 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 231 | lwsl_parser("WSI_TOK_(%d) '%c'\n", wsi->u.hdr.parser_state, c); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 232 | |
| 233 | /* collect into malloc'd buffers */ |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 234 | /* optional initial space swallow */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 235 | if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->frag_index[ |
| 236 | wsi->u.hdr.parser_state]].len && c == ' ') |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 237 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 238 | |
Andrew Canaday | da55fb5 | 2014-07-05 10:04:19 +0800 | [diff] [blame] | 239 | if ((wsi->u.hdr.parser_state != WSI_TOKEN_GET_URI) && |
| 240 | (wsi->u.hdr.parser_state != WSI_TOKEN_POST_URI) && |
| 241 | (wsi->u.hdr.parser_state != WSI_TOKEN_OPTIONS_URI)) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 242 | goto check_eol; |
| 243 | |
| 244 | /* special URI processing... end at space */ |
| 245 | |
| 246 | if (c == ' ') { |
| 247 | /* enforce starting with / */ |
| 248 | if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len) |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 249 | if (issue_char(wsi, '/') < 0) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 250 | return -1; |
Andrew Canaday | afe26cf | 2014-07-13 01:07:36 -0400 | [diff] [blame] | 251 | |
| 252 | /* begin parsing HTTP version: */ |
| 253 | if (issue_char(wsi, '\0') < 0) |
| 254 | return -1; |
| 255 | wsi->u.hdr.parser_state = WSI_TOKEN_HTTP; |
| 256 | goto start_fragment; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 259 | /* special URI processing... convert %xx */ |
| 260 | |
| 261 | switch (wsi->u.hdr.ues) { |
| 262 | case URIES_IDLE: |
| 263 | if (c == '%') { |
| 264 | wsi->u.hdr.ues = URIES_SEEN_PERCENT; |
| 265 | goto swallow; |
| 266 | } |
| 267 | break; |
| 268 | case URIES_SEEN_PERCENT: |
| 269 | if (char_to_hex(c) < 0) { |
| 270 | /* regurgitate */ |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 271 | if (issue_char(wsi, '%') < 0) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 272 | return -1; |
| 273 | wsi->u.hdr.ues = URIES_IDLE; |
| 274 | /* continue on to assess c */ |
| 275 | break; |
| 276 | } |
| 277 | wsi->u.hdr.esc_stash = c; |
| 278 | wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1; |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 279 | goto swallow; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 280 | |
| 281 | case URIES_SEEN_PERCENT_H1: |
| 282 | if (char_to_hex(c) < 0) { |
| 283 | /* regurgitate */ |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 284 | issue_char(wsi, '%'); |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 285 | wsi->u.hdr.ues = URIES_IDLE; |
| 286 | /* regurgitate + assess */ |
Andrew Canaday | 74b4a65 | 2014-06-29 00:25:19 -0400 | [diff] [blame] | 287 | if (libwebsocket_parse(context, wsi, wsi->u.hdr.esc_stash) < 0) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 288 | return -1; |
| 289 | /* continue on to assess c */ |
| 290 | break; |
| 291 | } |
| 292 | c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) | |
| 293 | char_to_hex(c); |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 294 | wsi->u.hdr.ues = URIES_IDLE; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * special URI processing... |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 300 | * convert /.. or /... or /../ etc to / |
| 301 | * convert /./ to / |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 302 | * convert // or /// etc to / |
| 303 | * leave /.dir or whatever alone |
| 304 | */ |
| 305 | |
| 306 | switch (wsi->u.hdr.ups) { |
| 307 | case URIPS_IDLE: |
| 308 | /* issue the first / always */ |
| 309 | if (c == '/') |
| 310 | wsi->u.hdr.ups = URIPS_SEEN_SLASH; |
| 311 | break; |
| 312 | case URIPS_SEEN_SLASH: |
| 313 | /* swallow subsequent slashes */ |
| 314 | if (c == '/') |
| 315 | goto swallow; |
| 316 | /* track and swallow the first . after / */ |
| 317 | if (c == '.') { |
| 318 | wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT; |
| 319 | goto swallow; |
Andy Green | 4b812fe | 2014-08-19 18:34:31 +0800 | [diff] [blame^] | 320 | } |
| 321 | wsi->u.hdr.ups = URIPS_IDLE; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 322 | break; |
| 323 | case URIPS_SEEN_SLASH_DOT: |
| 324 | /* swallow second . */ |
| 325 | if (c == '.') { |
Andy Green | d3f6873 | 2013-11-13 06:53:21 +0800 | [diff] [blame] | 326 | /* |
| 327 | * back up one dir level if possible |
| 328 | * safe against header fragmentation because |
| 329 | * the method URI can only be in 1 fragment |
| 330 | */ |
| 331 | if (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 2) { |
| 332 | wsi->u.hdr.ah->pos--; |
| 333 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--; |
| 334 | do { |
| 335 | wsi->u.hdr.ah->pos--; |
| 336 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--; |
| 337 | } while (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 1 && |
| 338 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos] != '/'); |
| 339 | } |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 340 | wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT; |
| 341 | goto swallow; |
| 342 | } |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 343 | /* change /./ to / */ |
| 344 | if (c == '/') { |
| 345 | wsi->u.hdr.ups = URIPS_SEEN_SLASH; |
| 346 | goto swallow; |
| 347 | } |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 348 | /* it was like /.dir ... regurgitate the . */ |
| 349 | wsi->u.hdr.ups = URIPS_IDLE; |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 350 | issue_char(wsi, '.'); |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 351 | break; |
| 352 | |
| 353 | case URIPS_SEEN_SLASH_DOT_DOT: |
| 354 | /* swallow prior .. chars and any subsequent . */ |
| 355 | if (c == '.') |
| 356 | goto swallow; |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 357 | /* last issued was /, so another / == // */ |
| 358 | if (c == '/') |
| 359 | goto swallow; |
Andy Green | 4b812fe | 2014-08-19 18:34:31 +0800 | [diff] [blame^] | 360 | /* last we issued was / so SEEN_SLASH */ |
| 361 | wsi->u.hdr.ups = URIPS_SEEN_SLASH; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 362 | break; |
Andy Green | 1e3f7b8 | 2013-11-13 07:45:17 +0800 | [diff] [blame] | 363 | case URIPS_ARGUMENTS: |
| 364 | /* leave them alone */ |
| 365 | break; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 366 | } |
| 367 | |
Andy Green | 1e3f7b8 | 2013-11-13 07:45:17 +0800 | [diff] [blame] | 368 | if (c == '?') { /* start of URI arguments */ |
| 369 | /* seal off uri header */ |
| 370 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0'; |
| 371 | |
| 372 | /* move to using WSI_TOKEN_HTTP_URI_ARGS */ |
| 373 | wsi->u.hdr.ah->next_frag_index++; |
| 374 | wsi->u.hdr.ah->frags[ |
| 375 | wsi->u.hdr.ah->next_frag_index].offset = |
| 376 | wsi->u.hdr.ah->pos; |
| 377 | wsi->u.hdr.ah->frags[ |
| 378 | wsi->u.hdr.ah->next_frag_index].len = 0; |
| 379 | wsi->u.hdr.ah->frags[ |
| 380 | wsi->u.hdr.ah->next_frag_index].next_frag_index = 0; |
| 381 | |
| 382 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] = |
| 383 | wsi->u.hdr.ah->next_frag_index; |
| 384 | |
| 385 | /* defeat normal uri path processing */ |
| 386 | wsi->u.hdr.ups = URIPS_ARGUMENTS; |
| 387 | goto swallow; |
| 388 | } |
| 389 | |
Andrew Canaday | 991f1cd | 2014-07-19 06:58:53 +0800 | [diff] [blame] | 390 | check_eol: |
| 391 | |
| 392 | /* bail at EOL */ |
| 393 | if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE && |
| 394 | c == '\x0d') { |
| 395 | c = '\0'; |
| 396 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR; |
| 397 | lwsl_parser("*\n"); |
| 398 | } |
| 399 | |
Andy Green | 4b812fe | 2014-08-19 18:34:31 +0800 | [diff] [blame^] | 400 | n = issue_char(wsi, c); |
| 401 | if (n < 0) |
| 402 | return -1; |
| 403 | if (n > 0) |
| 404 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
| 405 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 406 | swallow: |
Andy Green | d1b11e3 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 407 | /* per-protocol end of headers management */ |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 408 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 409 | if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE) |
| 410 | goto set_parsing_complete; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 411 | break; |
| 412 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 413 | /* collecting and checking a name part */ |
| 414 | case WSI_TOKEN_NAME_PART: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 415 | lwsl_parser("WSI_TOKEN_NAME_PART '%c'\n", c); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 416 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 417 | wsi->u.hdr.lextable_pos = |
| 418 | lextable_decode(wsi->u.hdr.lextable_pos, c); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 419 | |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 420 | if (wsi->u.hdr.lextable_pos < 0) { |
| 421 | /* this is not a header we know about */ |
Andrew Canaday | da55fb5 | 2014-07-05 10:04:19 +0800 | [diff] [blame] | 422 | if (wsi->u.hdr.ah->frag_index[WSI_TOKEN_GET_URI] || |
| 423 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_POST_URI] || |
| 424 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_OPTIONS_URI] || |
| 425 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_HTTP]) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 426 | /* |
| 427 | * altready had the method, no idea what |
| 428 | * this crap is, ignore |
| 429 | */ |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 430 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
| 431 | break; |
| 432 | } |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 433 | /* |
| 434 | * hm it's an unknown http method in fact, |
| 435 | * treat as dangerous |
| 436 | */ |
| 437 | |
| 438 | lwsl_info("Unknown method - dropping\n"); |
| 439 | return -1; |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 440 | } |
Andy Green | bbc5c07 | 2014-03-09 11:49:21 +0800 | [diff] [blame] | 441 | if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) { |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 442 | |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 443 | /* terminal state */ |
| 444 | |
Andy Green | bbc5c07 | 2014-03-09 11:49:21 +0800 | [diff] [blame] | 445 | n = (lextable[wsi->u.hdr.lextable_pos] << 8) | lextable[wsi->u.hdr.lextable_pos + 1]; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 446 | |
Andy Green | cc7cb68 | 2013-02-18 10:22:42 +0800 | [diff] [blame] | 447 | lwsl_parser("known hdr %d\n", n); |
Andy Green | 94f9465 | 2013-02-12 13:10:19 +0800 | [diff] [blame] | 448 | if (n == WSI_TOKEN_GET_URI && |
| 449 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_GET_URI]) { |
| 450 | lwsl_warn("Duplicated GET\n"); |
| 451 | return -1; |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 452 | } else if (n == WSI_TOKEN_POST_URI && |
| 453 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_POST_URI]) { |
| 454 | lwsl_warn("Duplicated POST\n"); |
| 455 | return -1; |
Andrew Canaday | da55fb5 | 2014-07-05 10:04:19 +0800 | [diff] [blame] | 456 | } else if (n == WSI_TOKEN_OPTIONS_URI && |
| 457 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_OPTIONS_URI]) { |
| 458 | lwsl_warn("Duplicated OPTIONS\n"); |
| 459 | return -1; |
Andy Green | 94f9465 | 2013-02-12 13:10:19 +0800 | [diff] [blame] | 460 | } |
| 461 | |
Andy Green | fd96330 | 2012-04-03 17:02:20 +0200 | [diff] [blame] | 462 | /* |
| 463 | * WSORIGIN is protocol equiv to ORIGIN, |
| 464 | * JWebSocket likes to send it, map to ORIGIN |
| 465 | */ |
| 466 | if (n == WSI_TOKEN_SWORIGIN) |
| 467 | n = WSI_TOKEN_ORIGIN; |
| 468 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 469 | wsi->u.hdr.parser_state = (enum lws_token_indexes) |
| 470 | (WSI_TOKEN_GET_URI + n); |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 471 | |
| 472 | if( context->token_limits ) { |
| 473 | wsi->u.hdr.current_token_limit = \ |
| 474 | context->token_limits->token_limit[wsi->u.hdr.parser_state]; |
| 475 | } |
| 476 | else { |
| 477 | wsi->u.hdr.current_token_limit = sizeof(wsi->u.hdr.ah->data); |
| 478 | }; |
| 479 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 480 | if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE) |
| 481 | goto set_parsing_complete; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 482 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 483 | goto start_fragment; |
| 484 | } |
| 485 | break; |
Nick Dowell | 3059263 | 2012-04-05 10:31:48 +0800 | [diff] [blame] | 486 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 487 | start_fragment: |
| 488 | wsi->u.hdr.ah->next_frag_index++; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 489 | if (wsi->u.hdr.ah->next_frag_index == |
| 490 | sizeof(wsi->u.hdr.ah->frags) / |
| 491 | sizeof(wsi->u.hdr.ah->frags[0])) { |
| 492 | lwsl_warn("More hdr frags than we can deal with\n"); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 493 | return -1; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 496 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset = |
| 497 | wsi->u.hdr.ah->pos; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 498 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 499 | wsi->u.hdr.ah->frags[ |
| 500 | wsi->u.hdr.ah->next_frag_index].next_frag_index = 0; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 501 | |
| 502 | n = wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state]; |
| 503 | if (!n) { /* first fragment */ |
| 504 | wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state] = |
| 505 | wsi->u.hdr.ah->next_frag_index; |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 506 | break; |
| 507 | } |
| 508 | /* continuation */ |
| 509 | while (wsi->u.hdr.ah->frags[n].next_frag_index) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 510 | n = wsi->u.hdr.ah->frags[n].next_frag_index; |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 511 | wsi->u.hdr.ah->frags[n].next_frag_index = |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 512 | wsi->u.hdr.ah->next_frag_index; |
| 513 | |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 514 | if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) { |
| 515 | lwsl_warn("excessive header content\n"); |
| 516 | return -1; |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 517 | } |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 518 | |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 519 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = ' '; |
| 520 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++; |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 521 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 522 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 523 | /* skipping arg part of a name we didn't recognize */ |
| 524 | case WSI_TOKEN_SKIPPING: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 525 | lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c); |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 526 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 527 | if (c == '\x0d') |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 528 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 529 | break; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 530 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 531 | case WSI_TOKEN_SKIPPING_SAW_CR: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 532 | lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c); |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 533 | if (c == '\x0a') { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 534 | wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART; |
| 535 | wsi->u.hdr.lextable_pos = 0; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 536 | } else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 537 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 538 | break; |
| 539 | /* we're done, ignore anything else */ |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 540 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 541 | case WSI_PARSING_COMPLETE: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 542 | lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 543 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 544 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 545 | default: /* keep gcc happy */ |
| 546 | break; |
| 547 | } |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 548 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 549 | return 0; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 550 | |
| 551 | set_parsing_complete: |
| 552 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 553 | if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 554 | if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION)) |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 555 | wsi->ietf_spec_revision = |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 556 | atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION)); |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 557 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 558 | lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision); |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 559 | } |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 560 | wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE; |
Andy Green | 224149a | 2013-02-11 21:43:41 +0800 | [diff] [blame] | 561 | wsi->hdr_parsing_completed = 1; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 562 | |
| 563 | return 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 566 | |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 567 | /** |
| 568 | * lws_frame_is_binary: true if the current frame was sent in binary mode |
| 569 | * |
| 570 | * @wsi: the connection we are inquiring about |
| 571 | * |
| 572 | * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if |
| 573 | * it's interested to see if the frame it's dealing with was sent in binary |
| 574 | * mode. |
| 575 | */ |
| 576 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 577 | LWS_VISIBLE int lws_frame_is_binary(struct libwebsocket *wsi) |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 578 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 579 | return wsi->u.ws.frame_is_binary; |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 580 | } |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 581 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 582 | int |
| 583 | libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c) |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 584 | { |
| 585 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 586 | struct lws_tokens eff_buf; |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 587 | int ret = 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 588 | |
| 589 | switch (wsi->lws_rx_parse_state) { |
| 590 | case LWS_RXPS_NEW: |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 591 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 592 | switch (wsi->ietf_spec_revision) { |
Andy Green | d85cb20 | 2011-09-25 09:32:54 +0100 | [diff] [blame] | 593 | case 13: |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 594 | /* |
| 595 | * no prepended frame key any more |
| 596 | */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 597 | wsi->u.ws.all_zero_nonce = 1; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 598 | goto handle_first; |
| 599 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 600 | default: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 601 | lwsl_warn("lws_rx_sm: unknown spec version %d\n", |
| 602 | wsi->ietf_spec_revision); |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 603 | break; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 604 | } |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 605 | break; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 606 | case LWS_RXPS_04_MASK_NONCE_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 607 | wsi->u.ws.frame_masking_nonce_04[1] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 608 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 609 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 610 | wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_2; |
| 611 | break; |
| 612 | case LWS_RXPS_04_MASK_NONCE_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 613 | wsi->u.ws.frame_masking_nonce_04[2] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 614 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 615 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 616 | wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_3; |
| 617 | break; |
| 618 | case LWS_RXPS_04_MASK_NONCE_3: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 619 | wsi->u.ws.frame_masking_nonce_04[3] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 620 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 621 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 622 | |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 623 | /* |
| 624 | * start from the zero'th byte in the XOR key buffer since |
| 625 | * this is the start of a frame with a new key |
| 626 | */ |
| 627 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 628 | wsi->u.ws.frame_mask_index = 0; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 629 | |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 630 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1; |
| 631 | break; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 632 | |
| 633 | /* |
| 634 | * 04 logical framing from the spec (all this is masked when incoming |
| 635 | * and has to be unmasked) |
| 636 | * |
| 637 | * We ignore the possibility of extension data because we don't |
| 638 | * negotiate any extensions at the moment. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 639 | * |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 640 | * 0 1 2 3 |
| 641 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 642 | * +-+-+-+-+-------+-+-------------+-------------------------------+ |
| 643 | * |F|R|R|R| opcode|R| Payload len | Extended payload length | |
| 644 | * |I|S|S|S| (4) |S| (7) | (16/63) | |
| 645 | * |N|V|V|V| |V| | (if payload len==126/127) | |
| 646 | * | |1|2|3| |4| | | |
| 647 | * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + |
| 648 | * | Extended payload length continued, if payload len == 127 | |
| 649 | * + - - - - - - - - - - - - - - - +-------------------------------+ |
| 650 | * | | Extension data | |
| 651 | * +-------------------------------+ - - - - - - - - - - - - - - - + |
| 652 | * : : |
| 653 | * +---------------------------------------------------------------+ |
| 654 | * : Application data : |
| 655 | * +---------------------------------------------------------------+ |
| 656 | * |
| 657 | * We pass payload through to userland as soon as we get it, ignoring |
| 658 | * FIN. It's up to userland to buffer it up if it wants to see a |
| 659 | * whole unfragmented block of the original size (which may be up to |
| 660 | * 2^63 long!) |
| 661 | */ |
| 662 | |
| 663 | case LWS_RXPS_04_FRAME_HDR_1: |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 664 | handle_first: |
| 665 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 666 | wsi->u.ws.opcode = c & 0xf; |
| 667 | wsi->u.ws.rsv = c & 0x70; |
| 668 | wsi->u.ws.final = !!((c >> 7) & 1); |
Andy Green | 1bc12f9 | 2013-02-28 17:11:29 +0800 | [diff] [blame] | 669 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 670 | switch (wsi->u.ws.opcode) { |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 671 | case LWS_WS_OPCODE_07__TEXT_FRAME: |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 672 | case LWS_WS_OPCODE_07__BINARY_FRAME: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 673 | wsi->u.ws.frame_is_binary = |
| 674 | wsi->u.ws.opcode == LWS_WS_OPCODE_07__BINARY_FRAME; |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 675 | break; |
| 676 | } |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 677 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN; |
| 678 | break; |
| 679 | |
| 680 | case LWS_RXPS_04_FRAME_HDR_LEN: |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 681 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 682 | wsi->u.ws.this_frame_masked = !!(c & 0x80); |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 683 | |
Andy Green | 5bf6578 | 2011-09-25 10:46:31 +0100 | [diff] [blame] | 684 | switch (c & 0x7f) { |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 685 | case 126: |
| 686 | /* control frames are not allowed to have big lengths */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 687 | if (wsi->u.ws.opcode & 8) |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 688 | goto illegal_ctl_length; |
| 689 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 690 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2; |
| 691 | break; |
| 692 | case 127: |
| 693 | /* control frames are not allowed to have big lengths */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 694 | if (wsi->u.ws.opcode & 8) |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 695 | goto illegal_ctl_length; |
| 696 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 697 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8; |
| 698 | break; |
| 699 | default: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 700 | wsi->u.ws.rx_packet_length = c & 0x7f; |
| 701 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 702 | wsi->lws_rx_parse_state = |
| 703 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 704 | else |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 705 | if (wsi->u.ws.rx_packet_length) |
| 706 | wsi->lws_rx_parse_state = |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 707 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 708 | else { |
| 709 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
| 710 | goto spill; |
| 711 | } |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 712 | break; |
| 713 | } |
| 714 | break; |
| 715 | |
| 716 | case LWS_RXPS_04_FRAME_HDR_LEN16_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 717 | wsi->u.ws.rx_packet_length = c << 8; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 718 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1; |
| 719 | break; |
| 720 | |
| 721 | case LWS_RXPS_04_FRAME_HDR_LEN16_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 722 | wsi->u.ws.rx_packet_length |= c; |
| 723 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 724 | wsi->lws_rx_parse_state = |
| 725 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 726 | else |
| 727 | wsi->lws_rx_parse_state = |
| 728 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 729 | break; |
| 730 | |
| 731 | case LWS_RXPS_04_FRAME_HDR_LEN64_8: |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 732 | if (c & 0x80) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 733 | lwsl_warn("b63 of length must be zero\n"); |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 734 | /* kill the connection */ |
| 735 | return -1; |
| 736 | } |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 737 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 738 | wsi->u.ws.rx_packet_length = ((size_t)c) << 56; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 739 | #else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 740 | wsi->u.ws.rx_packet_length = 0; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 741 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 742 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7; |
| 743 | break; |
| 744 | |
| 745 | case LWS_RXPS_04_FRAME_HDR_LEN64_7: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 746 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 747 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 48; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 748 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 749 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6; |
| 750 | break; |
| 751 | |
| 752 | case LWS_RXPS_04_FRAME_HDR_LEN64_6: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 753 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 754 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 40; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 755 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 756 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5; |
| 757 | break; |
| 758 | |
| 759 | case LWS_RXPS_04_FRAME_HDR_LEN64_5: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 760 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 761 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 32; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 762 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 763 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4; |
| 764 | break; |
| 765 | |
| 766 | case LWS_RXPS_04_FRAME_HDR_LEN64_4: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 767 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 24; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 768 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3; |
| 769 | break; |
| 770 | |
| 771 | case LWS_RXPS_04_FRAME_HDR_LEN64_3: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 772 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 16; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 773 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2; |
| 774 | break; |
| 775 | |
| 776 | case LWS_RXPS_04_FRAME_HDR_LEN64_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 777 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 8; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 778 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1; |
| 779 | break; |
| 780 | |
| 781 | case LWS_RXPS_04_FRAME_HDR_LEN64_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 782 | wsi->u.ws.rx_packet_length |= ((size_t)c); |
| 783 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 784 | wsi->lws_rx_parse_state = |
| 785 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 786 | else |
| 787 | wsi->lws_rx_parse_state = |
| 788 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 789 | break; |
| 790 | |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 791 | case LWS_RXPS_07_COLLECT_FRAME_KEY_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 792 | wsi->u.ws.frame_masking_nonce_04[0] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 793 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 794 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 795 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2; |
| 796 | break; |
| 797 | |
| 798 | case LWS_RXPS_07_COLLECT_FRAME_KEY_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 799 | wsi->u.ws.frame_masking_nonce_04[1] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 800 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 801 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 802 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3; |
| 803 | break; |
| 804 | |
| 805 | case LWS_RXPS_07_COLLECT_FRAME_KEY_3: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 806 | wsi->u.ws.frame_masking_nonce_04[2] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 807 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 808 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 809 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4; |
| 810 | break; |
| 811 | |
| 812 | case LWS_RXPS_07_COLLECT_FRAME_KEY_4: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 813 | wsi->u.ws.frame_masking_nonce_04[3] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 814 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 815 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 816 | wsi->lws_rx_parse_state = |
| 817 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 818 | wsi->u.ws.frame_mask_index = 0; |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 819 | if (wsi->u.ws.rx_packet_length == 0) { |
| 820 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
Andy Green | 1bc12f9 | 2013-02-28 17:11:29 +0800 | [diff] [blame] | 821 | goto spill; |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 822 | } |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 823 | break; |
| 824 | |
| 825 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 826 | case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 827 | |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 828 | if (!wsi->u.ws.rx_user_buffer) |
| 829 | lwsl_err("NULL user buffer...\n"); |
| 830 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 831 | if (wsi->u.ws.all_zero_nonce) |
| 832 | wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + |
| 833 | (wsi->u.ws.rx_user_buffer_head++)] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 834 | else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 835 | wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + |
| 836 | (wsi->u.ws.rx_user_buffer_head++)] = |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 837 | c ^ wsi->u.ws.frame_masking_nonce_04[ |
| 838 | (wsi->u.ws.frame_mask_index++) & 3]; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 839 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 840 | if (--wsi->u.ws.rx_packet_length == 0) { |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 841 | /* spill because we have the whole frame */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 842 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
| 843 | goto spill; |
| 844 | } |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 845 | |
| 846 | /* |
| 847 | * if there's no protocol max frame size given, we are |
| 848 | * supposed to default to LWS_MAX_SOCKET_IO_BUF |
| 849 | */ |
| 850 | |
| 851 | if (!wsi->protocol->rx_buffer_size && |
| 852 | wsi->u.ws.rx_user_buffer_head != |
| 853 | LWS_MAX_SOCKET_IO_BUF) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 854 | break; |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 855 | else |
| 856 | if (wsi->protocol->rx_buffer_size && |
| 857 | wsi->u.ws.rx_user_buffer_head != |
| 858 | wsi->protocol->rx_buffer_size) |
| 859 | break; |
| 860 | |
| 861 | /* spill because we filled our rx buffer */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 862 | spill: |
| 863 | /* |
| 864 | * is this frame a control packet we should take care of at this |
| 865 | * layer? If so service it and hide it from the user callback |
| 866 | */ |
| 867 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 868 | lwsl_parser("spill on %s\n", wsi->protocol->name); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 869 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 870 | switch (wsi->u.ws.opcode) { |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 871 | case LWS_WS_OPCODE_07__CLOSE: |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 872 | /* is this an acknowledgement of our close? */ |
| 873 | if (wsi->state == WSI_STATE_AWAITING_CLOSE_ACK) { |
| 874 | /* |
| 875 | * fine he has told us he is closing too, let's |
| 876 | * finish our close |
| 877 | */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 878 | lwsl_parser("seen client close ack\n"); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 879 | return -1; |
| 880 | } |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 881 | lwsl_parser("server sees client close packet\n"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 882 | /* parrot the close packet payload back */ |
| 883 | n = libwebsocket_write(wsi, (unsigned char *) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 884 | &wsi->u.ws.rx_user_buffer[ |
| 885 | LWS_SEND_BUFFER_PRE_PADDING], |
| 886 | wsi->u.ws.rx_user_buffer_head, |
| 887 | LWS_WRITE_CLOSE); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 888 | if (n < 0) |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 889 | lwsl_info("write of close ack failed %d\n", n); |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 890 | wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 891 | /* close the connection */ |
| 892 | return -1; |
| 893 | |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 894 | case LWS_WS_OPCODE_07__PING: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 895 | lwsl_info("received %d byte ping, sending pong\n", |
| 896 | wsi->u.ws.rx_user_buffer_head); |
| 897 | lwsl_hexdump(&wsi->u.ws.rx_user_buffer[ |
| 898 | LWS_SEND_BUFFER_PRE_PADDING], |
| 899 | wsi->u.ws.rx_user_buffer_head); |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 900 | /* parrot the ping packet payload back as a pong */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 901 | n = libwebsocket_write(wsi, (unsigned char *) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 902 | &wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], |
| 903 | wsi->u.ws.rx_user_buffer_head, LWS_WRITE_PONG); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 904 | if (n < 0) |
| 905 | return -1; |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 906 | /* ... then just drop it */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 907 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 908 | return 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 909 | |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 910 | case LWS_WS_OPCODE_07__PONG: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 911 | /* ... then just drop it */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 912 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 913 | return 0; |
| 914 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 915 | case LWS_WS_OPCODE_07__TEXT_FRAME: |
| 916 | case LWS_WS_OPCODE_07__BINARY_FRAME: |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 917 | case LWS_WS_OPCODE_07__CONTINUATION: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 918 | break; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 919 | |
| 920 | default: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 921 | lwsl_parser("passing opc %x up to exts\n", |
| 922 | wsi->u.ws.opcode); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 923 | /* |
| 924 | * It's something special we can't understand here. |
| 925 | * Pass the payload up to the extension's parsing |
| 926 | * state machine. |
| 927 | */ |
| 928 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 929 | eff_buf.token = &wsi->u.ws.rx_user_buffer[ |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 930 | LWS_SEND_BUFFER_PRE_PADDING]; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 931 | eff_buf.token_len = wsi->u.ws.rx_user_buffer_head; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 932 | |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 933 | if (lws_ext_callback_for_each_active(wsi, |
| 934 | LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX, |
| 935 | &eff_buf, 0) <= 0) /* not handle or fail */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 936 | lwsl_ext("ext opc opcode 0x%x unknown\n", |
| 937 | wsi->u.ws.opcode); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 938 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 939 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 940 | return 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /* |
| 944 | * No it's real payload, pass it up to the user callback. |
| 945 | * It's nicely buffered with the pre-padding taken care of |
| 946 | * so it can be sent straight out again using libwebsocket_write |
| 947 | */ |
| 948 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 949 | eff_buf.token = &wsi->u.ws.rx_user_buffer[ |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 950 | LWS_SEND_BUFFER_PRE_PADDING]; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 951 | eff_buf.token_len = wsi->u.ws.rx_user_buffer_head; |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 952 | |
| 953 | if (lws_ext_callback_for_each_active(wsi, |
| 954 | LWS_EXT_CALLBACK_PAYLOAD_RX, &eff_buf, 0) < 0) |
| 955 | return -1; |
| 956 | |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 957 | if (eff_buf.token_len > 0) { |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 958 | eff_buf.token[eff_buf.token_len] = '\0'; |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 959 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 960 | if (wsi->protocol->callback) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 961 | ret = user_callback_handle_rxflow( |
| 962 | wsi->protocol->callback, |
| 963 | wsi->protocol->owning_server, |
| 964 | wsi, LWS_CALLBACK_RECEIVE, |
| 965 | wsi->user_space, |
| 966 | eff_buf.token, |
| 967 | eff_buf.token_len); |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 968 | else |
Andy Green | f7609e9 | 2013-01-14 13:10:55 +0800 | [diff] [blame] | 969 | lwsl_err("No callback on payload spill!\n"); |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 970 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 971 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 972 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 973 | break; |
| 974 | } |
| 975 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 976 | return ret; |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 977 | |
| 978 | illegal_ctl_length: |
| 979 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 980 | lwsl_warn("Control frame with xtended length is illegal\n"); |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 981 | /* kill the connection */ |
| 982 | return -1; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 986 | /** |
| 987 | * libwebsockets_remaining_packet_payload() - Bytes to come before "overall" |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 988 | * rx packet is complete |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 989 | * @wsi: Websocket instance (available from user callback) |
| 990 | * |
| 991 | * This function is intended to be called from the callback if the |
| 992 | * user code is interested in "complete packets" from the client. |
| 993 | * libwebsockets just passes through payload as it comes and issues a buffer |
| 994 | * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE |
| 995 | * callback handler can use this API to find out if the buffer it has just |
| 996 | * been given is the last piece of a "complete packet" from the client -- |
| 997 | * when that is the case libwebsockets_remaining_packet_payload() will return |
| 998 | * 0. |
| 999 | * |
| 1000 | * Many protocols won't care becuse their packets are always small. |
| 1001 | */ |
| 1002 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1003 | LWS_VISIBLE size_t |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1004 | libwebsockets_remaining_packet_payload(struct libwebsocket *wsi) |
| 1005 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1006 | return wsi->u.ws.rx_packet_length; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1007 | } |