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 | |
David Galeano | aeb5748 | 2013-01-09 15:25:05 +0800 | [diff] [blame] | 24 | #ifdef WIN32 |
| 25 | #include <io.h> |
| 26 | #endif |
| 27 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 28 | |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 29 | unsigned char lextable[] = { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 30 | /* pos 0: state 0 */ |
| 31 | 0x47 /* 'G' */, 0x07 /* to pos 14 state 1 */, |
| 32 | 0x48 /* 'H' */, 0x0A /* to pos 22 state 5 */, |
| 33 | 0x43 /* 'C' */, 0x0F /* to pos 34 state 10 */, |
| 34 | 0x53 /* 'S' */, 0x19 /* to pos 56 state 21 */, |
| 35 | 0x55 /* 'U' */, 0x3F /* to pos 134 state 51 */, |
| 36 | 0x4F /* 'O' */, 0x46 /* to pos 150 state 59 */, |
| 37 | 0x8D /* '.' */, 0x52 /* to pos 176 state 72 */, |
| 38 | /* pos 14: state 1 */ |
| 39 | 0xC5 /* 'E' */, 0x01 /* to pos 16 state 2 */, |
| 40 | /* pos 16: state 2 */ |
| 41 | 0xD4 /* 'T' */, 0x01 /* to pos 18 state 3 */, |
| 42 | /* pos 18: state 3 */ |
| 43 | 0xA0 /* ' ' */, 0x01 /* to pos 20 state 4 */, |
| 44 | /* pos 20: state 4 */ |
| 45 | 0x80, 0x00 /* terminal marker */, |
| 46 | /* pos 22: state 5 */ |
| 47 | 0x6F /* 'o' */, 0x02 /* to pos 26 state 6 */, |
| 48 | 0xD4 /* 'T' */, 0x76 /* to pos 260 state 114 */, |
| 49 | /* pos 26: state 6 */ |
| 50 | 0xF3 /* 's' */, 0x01 /* to pos 28 state 7 */, |
| 51 | /* pos 28: state 7 */ |
| 52 | 0xF4 /* 't' */, 0x01 /* to pos 30 state 8 */, |
| 53 | /* pos 30: state 8 */ |
| 54 | 0xBA /* ':' */, 0x01 /* to pos 32 state 9 */, |
| 55 | /* pos 32: state 9 */ |
| 56 | 0x81, 0x00 /* terminal marker */, |
| 57 | /* pos 34: state 10 */ |
| 58 | 0xEF /* 'o' */, 0x01 /* to pos 36 state 11 */, |
| 59 | /* pos 36: state 11 */ |
| 60 | 0xEE /* 'n' */, 0x01 /* to pos 38 state 12 */, |
| 61 | /* pos 38: state 12 */ |
| 62 | 0xEE /* 'n' */, 0x01 /* to pos 40 state 13 */, |
| 63 | /* pos 40: state 13 */ |
| 64 | 0xE5 /* 'e' */, 0x01 /* to pos 42 state 14 */, |
| 65 | /* pos 42: state 14 */ |
| 66 | 0xE3 /* 'c' */, 0x01 /* to pos 44 state 15 */, |
| 67 | /* pos 44: state 15 */ |
| 68 | 0xF4 /* 't' */, 0x01 /* to pos 46 state 16 */, |
| 69 | /* pos 46: state 16 */ |
| 70 | 0xE9 /* 'i' */, 0x01 /* to pos 48 state 17 */, |
| 71 | /* pos 48: state 17 */ |
| 72 | 0xEF /* 'o' */, 0x01 /* to pos 50 state 18 */, |
| 73 | /* pos 50: state 18 */ |
| 74 | 0xEE /* 'n' */, 0x01 /* to pos 52 state 19 */, |
| 75 | /* pos 52: state 19 */ |
| 76 | 0xBA /* ':' */, 0x01 /* to pos 54 state 20 */, |
| 77 | /* pos 54: state 20 */ |
| 78 | 0x82, 0x00 /* terminal marker */, |
| 79 | /* pos 56: state 21 */ |
| 80 | 0xE5 /* 'e' */, 0x01 /* to pos 58 state 22 */, |
| 81 | /* pos 58: state 22 */ |
| 82 | 0xE3 /* 'c' */, 0x01 /* to pos 60 state 23 */, |
| 83 | /* pos 60: state 23 */ |
| 84 | 0xAD /* '-' */, 0x01 /* to pos 62 state 24 */, |
| 85 | /* pos 62: state 24 */ |
| 86 | 0xD7 /* 'W' */, 0x01 /* to pos 64 state 25 */, |
| 87 | /* pos 64: state 25 */ |
| 88 | 0xE5 /* 'e' */, 0x01 /* to pos 66 state 26 */, |
| 89 | /* pos 66: state 26 */ |
| 90 | 0xE2 /* 'b' */, 0x01 /* to pos 68 state 27 */, |
| 91 | /* pos 68: state 27 */ |
| 92 | 0xD3 /* 'S' */, 0x01 /* to pos 70 state 28 */, |
| 93 | /* pos 70: state 28 */ |
| 94 | 0xEF /* 'o' */, 0x01 /* to pos 72 state 29 */, |
| 95 | /* pos 72: state 29 */ |
| 96 | 0xE3 /* 'c' */, 0x01 /* to pos 74 state 30 */, |
| 97 | /* pos 74: state 30 */ |
| 98 | 0xEB /* 'k' */, 0x01 /* to pos 76 state 31 */, |
| 99 | /* pos 76: state 31 */ |
| 100 | 0xE5 /* 'e' */, 0x01 /* to pos 78 state 32 */, |
| 101 | /* pos 78: state 32 */ |
| 102 | 0xF4 /* 't' */, 0x01 /* to pos 80 state 33 */, |
| 103 | /* pos 80: state 33 */ |
| 104 | 0xAD /* '-' */, 0x01 /* to pos 82 state 34 */, |
| 105 | /* pos 82: state 34 */ |
| 106 | 0x4B /* 'K' */, 0x08 /* to pos 98 state 35 */, |
| 107 | 0x50 /* 'P' */, 0x10 /* to pos 116 state 42 */, |
| 108 | 0x44 /* 'D' */, 0x27 /* to pos 164 state 66 */, |
| 109 | 0x56 /* 'V' */, 0x2F /* to pos 182 state 75 */, |
| 110 | 0x4F /* 'O' */, 0x36 /* to pos 198 state 83 */, |
| 111 | 0x45 /* 'E' */, 0x3C /* to pos 212 state 90 */, |
| 112 | 0x41 /* 'A' */, 0x46 /* to pos 234 state 101 */, |
| 113 | 0xCE /* 'N' */, 0x4C /* to pos 248 state 108 */, |
| 114 | /* pos 98: state 35 */ |
| 115 | 0xE5 /* 'e' */, 0x01 /* to pos 100 state 36 */, |
| 116 | /* pos 100: state 36 */ |
| 117 | 0xF9 /* 'y' */, 0x01 /* to pos 102 state 37 */, |
| 118 | /* pos 102: state 37 */ |
| 119 | 0x31 /* '1' */, 0x03 /* to pos 108 state 38 */, |
| 120 | 0x32 /* '2' */, 0x04 /* to pos 112 state 40 */, |
| 121 | 0xBA /* ':' */, 0x25 /* to pos 180 state 74 */, |
| 122 | /* pos 108: state 38 */ |
| 123 | 0xBA /* ':' */, 0x01 /* to pos 110 state 39 */, |
| 124 | /* pos 110: state 39 */ |
| 125 | 0x83, 0x00 /* terminal marker */, |
| 126 | /* pos 112: state 40 */ |
| 127 | 0xBA /* ':' */, 0x01 /* to pos 114 state 41 */, |
| 128 | /* pos 114: state 41 */ |
| 129 | 0x84, 0x00 /* terminal marker */, |
| 130 | /* pos 116: state 42 */ |
| 131 | 0xF2 /* 'r' */, 0x01 /* to pos 118 state 43 */, |
| 132 | /* pos 118: state 43 */ |
| 133 | 0xEF /* 'o' */, 0x01 /* to pos 120 state 44 */, |
| 134 | /* pos 120: state 44 */ |
| 135 | 0xF4 /* 't' */, 0x01 /* to pos 122 state 45 */, |
| 136 | /* pos 122: state 45 */ |
| 137 | 0xEF /* 'o' */, 0x01 /* to pos 124 state 46 */, |
| 138 | /* pos 124: state 46 */ |
| 139 | 0xE3 /* 'c' */, 0x01 /* to pos 126 state 47 */, |
| 140 | /* pos 126: state 47 */ |
| 141 | 0xEF /* 'o' */, 0x01 /* to pos 128 state 48 */, |
| 142 | /* pos 128: state 48 */ |
| 143 | 0xEC /* 'l' */, 0x01 /* to pos 130 state 49 */, |
| 144 | /* pos 130: state 49 */ |
| 145 | 0xBA /* ':' */, 0x01 /* to pos 132 state 50 */, |
| 146 | /* pos 132: state 50 */ |
| 147 | 0x85, 0x00 /* terminal marker */, |
| 148 | /* pos 134: state 51 */ |
| 149 | 0xF0 /* 'p' */, 0x01 /* to pos 136 state 52 */, |
| 150 | /* pos 136: state 52 */ |
| 151 | 0xE7 /* 'g' */, 0x01 /* to pos 138 state 53 */, |
| 152 | /* pos 138: state 53 */ |
| 153 | 0xF2 /* 'r' */, 0x01 /* to pos 140 state 54 */, |
| 154 | /* pos 140: state 54 */ |
| 155 | 0xE1 /* 'a' */, 0x01 /* to pos 142 state 55 */, |
| 156 | /* pos 142: state 55 */ |
| 157 | 0xE4 /* 'd' */, 0x01 /* to pos 144 state 56 */, |
| 158 | /* pos 144: state 56 */ |
| 159 | 0xE5 /* 'e' */, 0x01 /* to pos 146 state 57 */, |
| 160 | /* pos 146: state 57 */ |
| 161 | 0xBA /* ':' */, 0x01 /* to pos 148 state 58 */, |
| 162 | /* pos 148: state 58 */ |
| 163 | 0x86, 0x00 /* terminal marker */, |
| 164 | /* pos 150: state 59 */ |
| 165 | 0xF2 /* 'r' */, 0x01 /* to pos 152 state 60 */, |
| 166 | /* pos 152: state 60 */ |
| 167 | 0xE9 /* 'i' */, 0x01 /* to pos 154 state 61 */, |
| 168 | /* pos 154: state 61 */ |
| 169 | 0xE7 /* 'g' */, 0x01 /* to pos 156 state 62 */, |
| 170 | /* pos 156: state 62 */ |
| 171 | 0xE9 /* 'i' */, 0x01 /* to pos 158 state 63 */, |
| 172 | /* pos 158: state 63 */ |
| 173 | 0xEE /* 'n' */, 0x01 /* to pos 160 state 64 */, |
| 174 | /* pos 160: state 64 */ |
| 175 | 0xBA /* ':' */, 0x01 /* to pos 162 state 65 */, |
| 176 | /* pos 162: state 65 */ |
| 177 | 0x87, 0x00 /* terminal marker */, |
| 178 | /* pos 164: state 66 */ |
| 179 | 0xF2 /* 'r' */, 0x01 /* to pos 166 state 67 */, |
| 180 | /* pos 166: state 67 */ |
| 181 | 0xE1 /* 'a' */, 0x01 /* to pos 168 state 68 */, |
| 182 | /* pos 168: state 68 */ |
| 183 | 0xE6 /* 'f' */, 0x01 /* to pos 170 state 69 */, |
| 184 | /* pos 170: state 69 */ |
| 185 | 0xF4 /* 't' */, 0x01 /* to pos 172 state 70 */, |
| 186 | /* pos 172: state 70 */ |
| 187 | 0xBA /* ':' */, 0x01 /* to pos 174 state 71 */, |
| 188 | /* pos 174: state 71 */ |
| 189 | 0x88, 0x00 /* terminal marker */, |
| 190 | /* pos 176: state 72 */ |
| 191 | 0x8A /* '.' */, 0x01 /* to pos 178 state 73 */, |
| 192 | /* pos 178: state 73 */ |
| 193 | 0x89, 0x00 /* terminal marker */, |
| 194 | /* pos 180: state 74 */ |
| 195 | 0x8A, 0x00 /* terminal marker */, |
| 196 | /* pos 182: state 75 */ |
| 197 | 0xE5 /* 'e' */, 0x01 /* to pos 184 state 76 */, |
| 198 | /* pos 184: state 76 */ |
| 199 | 0xF2 /* 'r' */, 0x01 /* to pos 186 state 77 */, |
| 200 | /* pos 186: state 77 */ |
| 201 | 0xF3 /* 's' */, 0x01 /* to pos 188 state 78 */, |
| 202 | /* pos 188: state 78 */ |
| 203 | 0xE9 /* 'i' */, 0x01 /* to pos 190 state 79 */, |
| 204 | /* pos 190: state 79 */ |
| 205 | 0xEF /* 'o' */, 0x01 /* to pos 192 state 80 */, |
| 206 | /* pos 192: state 80 */ |
| 207 | 0xEE /* 'n' */, 0x01 /* to pos 194 state 81 */, |
| 208 | /* pos 194: state 81 */ |
| 209 | 0xBA /* ':' */, 0x01 /* to pos 196 state 82 */, |
| 210 | /* pos 196: state 82 */ |
| 211 | 0x8B, 0x00 /* terminal marker */, |
| 212 | /* pos 198: state 83 */ |
| 213 | 0xF2 /* 'r' */, 0x01 /* to pos 200 state 84 */, |
| 214 | /* pos 200: state 84 */ |
| 215 | 0xE9 /* 'i' */, 0x01 /* to pos 202 state 85 */, |
| 216 | /* pos 202: state 85 */ |
| 217 | 0xE7 /* 'g' */, 0x01 /* to pos 204 state 86 */, |
| 218 | /* pos 204: state 86 */ |
| 219 | 0xE9 /* 'i' */, 0x01 /* to pos 206 state 87 */, |
| 220 | /* pos 206: state 87 */ |
| 221 | 0xEE /* 'n' */, 0x01 /* to pos 208 state 88 */, |
| 222 | /* pos 208: state 88 */ |
| 223 | 0xBA /* ':' */, 0x01 /* to pos 210 state 89 */, |
| 224 | /* pos 210: state 89 */ |
| 225 | 0x8C, 0x00 /* terminal marker */, |
| 226 | /* pos 212: state 90 */ |
| 227 | 0xF8 /* 'x' */, 0x01 /* to pos 214 state 91 */, |
| 228 | /* pos 214: state 91 */ |
| 229 | 0xF4 /* 't' */, 0x01 /* to pos 216 state 92 */, |
| 230 | /* pos 216: state 92 */ |
| 231 | 0xE5 /* 'e' */, 0x01 /* to pos 218 state 93 */, |
| 232 | /* pos 218: state 93 */ |
| 233 | 0xEE /* 'n' */, 0x01 /* to pos 220 state 94 */, |
| 234 | /* pos 220: state 94 */ |
| 235 | 0xF3 /* 's' */, 0x01 /* to pos 222 state 95 */, |
| 236 | /* pos 222: state 95 */ |
| 237 | 0xE9 /* 'i' */, 0x01 /* to pos 224 state 96 */, |
| 238 | /* pos 224: state 96 */ |
| 239 | 0xEF /* 'o' */, 0x01 /* to pos 226 state 97 */, |
| 240 | /* pos 226: state 97 */ |
| 241 | 0xEE /* 'n' */, 0x01 /* to pos 228 state 98 */, |
| 242 | /* pos 228: state 98 */ |
| 243 | 0xF3 /* 's' */, 0x01 /* to pos 230 state 99 */, |
| 244 | /* pos 230: state 99 */ |
| 245 | 0xBA /* ':' */, 0x01 /* to pos 232 state 100 */, |
| 246 | /* pos 232: state 100 */ |
| 247 | 0x8D, 0x00 /* terminal marker */, |
| 248 | /* pos 234: state 101 */ |
| 249 | 0xE3 /* 'c' */, 0x01 /* to pos 236 state 102 */, |
| 250 | /* pos 236: state 102 */ |
| 251 | 0xE3 /* 'c' */, 0x01 /* to pos 238 state 103 */, |
| 252 | /* pos 238: state 103 */ |
| 253 | 0xE5 /* 'e' */, 0x01 /* to pos 240 state 104 */, |
| 254 | /* pos 240: state 104 */ |
| 255 | 0xF0 /* 'p' */, 0x01 /* to pos 242 state 105 */, |
| 256 | /* pos 242: state 105 */ |
| 257 | 0xF4 /* 't' */, 0x01 /* to pos 244 state 106 */, |
| 258 | /* pos 244: state 106 */ |
| 259 | 0xBA /* ':' */, 0x01 /* to pos 246 state 107 */, |
| 260 | /* pos 246: state 107 */ |
| 261 | 0x8E, 0x00 /* terminal marker */, |
| 262 | /* pos 248: state 108 */ |
| 263 | 0xEF /* 'o' */, 0x01 /* to pos 250 state 109 */, |
| 264 | /* pos 250: state 109 */ |
| 265 | 0xEE /* 'n' */, 0x01 /* to pos 252 state 110 */, |
| 266 | /* pos 252: state 110 */ |
| 267 | 0xE3 /* 'c' */, 0x01 /* to pos 254 state 111 */, |
| 268 | /* pos 254: state 111 */ |
| 269 | 0xE5 /* 'e' */, 0x01 /* to pos 256 state 112 */, |
| 270 | /* pos 256: state 112 */ |
| 271 | 0xBA /* ':' */, 0x01 /* to pos 258 state 113 */, |
| 272 | /* pos 258: state 113 */ |
| 273 | 0x8F, 0x00 /* terminal marker */, |
| 274 | /* pos 260: state 114 */ |
| 275 | 0xD4 /* 'T' */, 0x01 /* to pos 262 state 115 */, |
| 276 | /* pos 262: state 115 */ |
| 277 | 0xD0 /* 'P' */, 0x01 /* to pos 264 state 116 */, |
| 278 | /* pos 264: state 116 */ |
| 279 | 0xAF /* '/' */, 0x01 /* to pos 266 state 117 */, |
| 280 | /* pos 266: state 117 */ |
| 281 | 0xB1 /* '1' */, 0x01 /* to pos 268 state 118 */, |
| 282 | /* pos 268: state 118 */ |
| 283 | 0xAE /* '.' */, 0x01 /* to pos 270 state 119 */, |
| 284 | /* pos 270: state 119 */ |
| 285 | 0xB1 /* '1' */, 0x01 /* to pos 272 state 120 */, |
| 286 | /* pos 272: state 120 */ |
| 287 | 0xA0 /* ' ' */, 0x01 /* to pos 274 state 121 */, |
| 288 | /* pos 274: state 121 */ |
| 289 | 0x90, 0x00 /* terminal marker */, |
| 290 | /* total size 276 bytes */ |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | int lextable_decode(int pos, char c) |
| 294 | { |
| 295 | while (pos >= 0) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 296 | if (lextable[pos + 1] == 0) /* terminal marker */ |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 297 | return pos; |
| 298 | |
| 299 | if ((lextable[pos] & 0x7f) == c) |
| 300 | return pos + (lextable[pos + 1] << 1); |
| 301 | |
| 302 | if (lextable[pos] & 0x80) |
| 303 | return -1; |
| 304 | |
| 305 | pos += 2; |
| 306 | } |
| 307 | return pos; |
| 308 | } |
| 309 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 310 | int lws_allocate_header_table(struct libwebsocket *wsi) |
| 311 | { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 312 | wsi->u.hdr.ah = malloc(sizeof(*wsi->u.hdr.ah)); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 313 | if (wsi->u.hdr.ah == NULL) { |
| 314 | lwsl_err("Out of memory\n"); |
| 315 | return -1; |
| 316 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 317 | 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] | 318 | wsi->u.hdr.ah->next_frag_index = 0; |
| 319 | wsi->u.hdr.ah->pos = 0; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 320 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | int lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h) |
| 325 | { |
| 326 | int n; |
| 327 | int len = 0; |
| 328 | |
| 329 | n = wsi->u.hdr.ah->frag_index[h]; |
| 330 | if (n == 0) |
| 331 | return 0; |
| 332 | |
| 333 | do { |
| 334 | len += wsi->u.hdr.ah->frags[n].len; |
| 335 | n = wsi->u.hdr.ah->frags[n].next_frag_index; |
| 336 | } while (n); |
| 337 | |
| 338 | return len; |
| 339 | } |
| 340 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 341 | int lws_hdr_copy(struct libwebsocket *wsi, char *dest, int len, |
| 342 | enum lws_token_indexes h) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 343 | { |
| 344 | int toklen = lws_hdr_total_length(wsi, h); |
| 345 | int n; |
| 346 | |
| 347 | if (toklen >= len) |
| 348 | return -1; |
| 349 | |
| 350 | n = wsi->u.hdr.ah->frag_index[h]; |
| 351 | if (n == 0) |
| 352 | return 0; |
| 353 | |
| 354 | do { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 355 | strcpy(dest, |
| 356 | &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 357 | dest += wsi->u.hdr.ah->frags[n].len; |
| 358 | n = wsi->u.hdr.ah->frags[n].next_frag_index; |
| 359 | } while (n); |
| 360 | |
| 361 | return toklen; |
| 362 | } |
| 363 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 364 | 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] | 365 | { |
| 366 | int n; |
| 367 | |
| 368 | n = wsi->u.hdr.ah->frag_index[h]; |
| 369 | if (!n) |
| 370 | return NULL; |
| 371 | |
| 372 | return &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]; |
| 373 | } |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 374 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 375 | int lws_hdr_simple_create(struct libwebsocket *wsi, |
| 376 | enum lws_token_indexes h, const char *s) |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 377 | { |
| 378 | wsi->u.hdr.ah->next_frag_index++; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 379 | if (wsi->u.hdr.ah->next_frag_index == |
| 380 | sizeof(wsi->u.hdr.ah->frags) / sizeof(wsi->u.hdr.ah->frags[0])) { |
| 381 | lwsl_warn("More hdr frags than we can deal with, dropping\n"); |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->next_frag_index; |
| 386 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 387 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset = |
| 388 | wsi->u.hdr.ah->pos; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 389 | 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] | 390 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].next_frag_index = |
| 391 | 0; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 392 | |
| 393 | do { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 394 | if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) { |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 395 | lwsl_err("Ran out of header data space\n"); |
| 396 | return -1; |
| 397 | } |
| 398 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s; |
| 399 | if (*s) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 400 | wsi->u.hdr.ah->frags[ |
| 401 | wsi->u.hdr.ah->next_frag_index].len++; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 402 | } while (*s++); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 407 | int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c) |
| 408 | { |
| 409 | int n; |
| 410 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 411 | switch (wsi->u.hdr.parser_state) { |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 412 | case WSI_TOKEN_GET_URI: |
| 413 | case WSI_TOKEN_HOST: |
| 414 | case WSI_TOKEN_CONNECTION: |
| 415 | case WSI_TOKEN_KEY1: |
| 416 | case WSI_TOKEN_KEY2: |
| 417 | case WSI_TOKEN_PROTOCOL: |
| 418 | case WSI_TOKEN_UPGRADE: |
| 419 | case WSI_TOKEN_ORIGIN: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 420 | case WSI_TOKEN_SWORIGIN: |
Andy Green | ce510c6 | 2010-11-11 12:48:13 +0000 | [diff] [blame] | 421 | case WSI_TOKEN_DRAFT: |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 422 | case WSI_TOKEN_CHALLENGE: |
Andy Green | d1b11e3 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 423 | case WSI_TOKEN_KEY: |
| 424 | case WSI_TOKEN_VERSION: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 425 | case WSI_TOKEN_ACCEPT: |
| 426 | case WSI_TOKEN_NONCE: |
Andy Green | 18910c5 | 2011-02-09 08:58:42 +0000 | [diff] [blame] | 427 | case WSI_TOKEN_EXTENSIONS: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 428 | case WSI_TOKEN_HTTP: |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 429 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 430 | 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] | 431 | |
| 432 | /* collect into malloc'd buffers */ |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 433 | /* optional initial space swallow */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 434 | if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->frag_index[ |
| 435 | wsi->u.hdr.parser_state]].len && c == ' ') |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 436 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 437 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 438 | /* special case space terminator for get-uri */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 439 | if (wsi->u.hdr.parser_state == WSI_TOKEN_GET_URI && c == ' ') { |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 440 | c = '\0'; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 441 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* bail at EOL */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 445 | if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE && |
| 446 | c == '\x0d') { |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 447 | c = '\0'; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 448 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 449 | lwsl_parser("*\n"); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 452 | if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) { |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 453 | lwsl_warn("excessive header content\n"); |
| 454 | return -1; |
| 455 | } |
| 456 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c; |
| 457 | if (c) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 458 | wsi->u.hdr.ah->frags[ |
| 459 | wsi->u.hdr.ah->next_frag_index].len++; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 460 | |
Andy Green | d1b11e3 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 461 | /* per-protocol end of headers management */ |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 462 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 463 | if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE) |
| 464 | goto set_parsing_complete; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 465 | break; |
| 466 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 467 | /* collecting and checking a name part */ |
| 468 | case WSI_TOKEN_NAME_PART: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 469 | lwsl_parser("WSI_TOKEN_NAME_PART '%c'\n", c); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 470 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 471 | wsi->u.hdr.lextable_pos = |
| 472 | lextable_decode(wsi->u.hdr.lextable_pos, c); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 473 | |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 474 | if (wsi->u.hdr.lextable_pos < 0) { |
| 475 | /* this is not a header we know about */ |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 476 | if (wsi->u.hdr.ah->frag_index[WSI_TOKEN_GET_URI]) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 477 | /* |
| 478 | * altready had the method, no idea what |
| 479 | * this crap is, ignore |
| 480 | */ |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 481 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
| 482 | break; |
| 483 | } |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 484 | /* |
| 485 | * hm it's an unknown http method in fact, |
| 486 | * treat as dangerous |
| 487 | */ |
| 488 | |
| 489 | lwsl_info("Unknown method - dropping\n"); |
| 490 | return -1; |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 491 | } |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 492 | if (lextable[wsi->u.hdr.lextable_pos + 1] == 0) { |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 493 | |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 494 | /* terminal state */ |
| 495 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 496 | n = lextable[wsi->u.hdr.lextable_pos] & 0x7f; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 497 | |
Andy Green | cc7cb68 | 2013-02-18 10:22:42 +0800 | [diff] [blame^] | 498 | lwsl_parser("known hdr %d\n", n); |
Andy Green | fd96330 | 2012-04-03 17:02:20 +0200 | [diff] [blame] | 499 | |
Andy Green | 94f9465 | 2013-02-12 13:10:19 +0800 | [diff] [blame] | 500 | if (n == WSI_TOKEN_GET_URI && |
| 501 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_GET_URI]) { |
| 502 | lwsl_warn("Duplicated GET\n"); |
| 503 | return -1; |
| 504 | } |
| 505 | |
Andy Green | fd96330 | 2012-04-03 17:02:20 +0200 | [diff] [blame] | 506 | /* |
| 507 | * WSORIGIN is protocol equiv to ORIGIN, |
| 508 | * JWebSocket likes to send it, map to ORIGIN |
| 509 | */ |
| 510 | if (n == WSI_TOKEN_SWORIGIN) |
| 511 | n = WSI_TOKEN_ORIGIN; |
| 512 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 513 | wsi->u.hdr.parser_state = (enum lws_token_indexes) |
| 514 | (WSI_TOKEN_GET_URI + n); |
| 515 | if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE) |
| 516 | goto set_parsing_complete; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 517 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 518 | goto start_fragment; |
| 519 | } |
| 520 | break; |
Nick Dowell | 3059263 | 2012-04-05 10:31:48 +0800 | [diff] [blame] | 521 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 522 | start_fragment: |
| 523 | wsi->u.hdr.ah->next_frag_index++; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 524 | if (wsi->u.hdr.ah->next_frag_index == |
| 525 | sizeof(wsi->u.hdr.ah->frags) / |
| 526 | sizeof(wsi->u.hdr.ah->frags[0])) { |
| 527 | lwsl_warn("More hdr frags than we can deal with\n"); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 528 | return -1; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 531 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset = |
| 532 | wsi->u.hdr.ah->pos; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 533 | 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] | 534 | wsi->u.hdr.ah->frags[ |
| 535 | wsi->u.hdr.ah->next_frag_index].next_frag_index = 0; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 536 | |
| 537 | n = wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state]; |
| 538 | if (!n) { /* first fragment */ |
| 539 | wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state] = |
| 540 | wsi->u.hdr.ah->next_frag_index; |
| 541 | } else { /* continuation */ |
| 542 | while (wsi->u.hdr.ah->frags[n].next_frag_index) |
| 543 | n = wsi->u.hdr.ah->frags[n].next_frag_index; |
| 544 | wsi->u.hdr.ah->frags[n].next_frag_index = |
| 545 | wsi->u.hdr.ah->next_frag_index; |
| 546 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 547 | if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) { |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 548 | lwsl_warn("excessive header content\n"); |
| 549 | return -1; |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 550 | } |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 551 | |
| 552 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = ' '; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 553 | wsi->u.hdr.ah->frags[ |
| 554 | wsi->u.hdr.ah->next_frag_index].len++; |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 555 | } |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 556 | |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 557 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 558 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 559 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 560 | /* skipping arg part of a name we didn't recognize */ |
| 561 | case WSI_TOKEN_SKIPPING: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 562 | lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 563 | if (c == '\x0d') |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 564 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 565 | break; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 566 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 567 | case WSI_TOKEN_SKIPPING_SAW_CR: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 568 | lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c); |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 569 | if (c == '\x0a') { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 570 | wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART; |
| 571 | wsi->u.hdr.lextable_pos = 0; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 572 | } else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 573 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 574 | break; |
| 575 | /* we're done, ignore anything else */ |
| 576 | case WSI_PARSING_COMPLETE: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 577 | lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 578 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 579 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 580 | default: /* keep gcc happy */ |
| 581 | break; |
| 582 | } |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 583 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 584 | return 0; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 585 | |
| 586 | set_parsing_complete: |
| 587 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 588 | if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 589 | if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION)) |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 590 | wsi->ietf_spec_revision = |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 591 | atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION)); |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 592 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 593 | lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision); |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 594 | } |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 595 | wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE; |
Andy Green | 224149a | 2013-02-11 21:43:41 +0800 | [diff] [blame] | 596 | wsi->hdr_parsing_completed = 1; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 597 | |
| 598 | return 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 601 | |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 602 | /** |
| 603 | * lws_frame_is_binary: true if the current frame was sent in binary mode |
| 604 | * |
| 605 | * @wsi: the connection we are inquiring about |
| 606 | * |
| 607 | * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if |
| 608 | * it's interested to see if the frame it's dealing with was sent in binary |
| 609 | * mode. |
| 610 | */ |
| 611 | |
| 612 | int lws_frame_is_binary(struct libwebsocket *wsi) |
| 613 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 614 | return wsi->u.ws.frame_is_binary; |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 615 | } |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 616 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 617 | int |
| 618 | libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c) |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 619 | { |
| 620 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 621 | struct lws_tokens eff_buf; |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 622 | int ret = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 623 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 624 | int handled; |
| 625 | int m; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 626 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 627 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 628 | #if 0 |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 629 | lwsl_debug("RX: %02X ", c); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 630 | #endif |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 631 | |
| 632 | switch (wsi->lws_rx_parse_state) { |
| 633 | case LWS_RXPS_NEW: |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 634 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 635 | switch (wsi->ietf_spec_revision) { |
Andy Green | d85cb20 | 2011-09-25 09:32:54 +0100 | [diff] [blame] | 636 | case 13: |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 637 | /* |
| 638 | * no prepended frame key any more |
| 639 | */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 640 | wsi->u.ws.all_zero_nonce = 1; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 641 | goto handle_first; |
| 642 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 643 | default: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 644 | lwsl_warn("lws_rx_sm: unknown spec version %d\n", |
| 645 | wsi->ietf_spec_revision); |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 646 | break; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 647 | } |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 648 | break; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 649 | case LWS_RXPS_04_MASK_NONCE_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 650 | wsi->u.ws.frame_masking_nonce_04[1] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 651 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 652 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 653 | wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_2; |
| 654 | break; |
| 655 | case LWS_RXPS_04_MASK_NONCE_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 656 | wsi->u.ws.frame_masking_nonce_04[2] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 657 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 658 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 659 | wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_3; |
| 660 | break; |
| 661 | case LWS_RXPS_04_MASK_NONCE_3: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 662 | wsi->u.ws.frame_masking_nonce_04[3] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 663 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 664 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 665 | |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 666 | /* |
| 667 | * start from the zero'th byte in the XOR key buffer since |
| 668 | * this is the start of a frame with a new key |
| 669 | */ |
| 670 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 671 | wsi->u.ws.frame_mask_index = 0; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 672 | |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 673 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1; |
| 674 | break; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 675 | |
| 676 | /* |
| 677 | * 04 logical framing from the spec (all this is masked when incoming |
| 678 | * and has to be unmasked) |
| 679 | * |
| 680 | * We ignore the possibility of extension data because we don't |
| 681 | * negotiate any extensions at the moment. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 682 | * |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 683 | * 0 1 2 3 |
| 684 | * 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 |
| 685 | * +-+-+-+-+-------+-+-------------+-------------------------------+ |
| 686 | * |F|R|R|R| opcode|R| Payload len | Extended payload length | |
| 687 | * |I|S|S|S| (4) |S| (7) | (16/63) | |
| 688 | * |N|V|V|V| |V| | (if payload len==126/127) | |
| 689 | * | |1|2|3| |4| | | |
| 690 | * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + |
| 691 | * | Extended payload length continued, if payload len == 127 | |
| 692 | * + - - - - - - - - - - - - - - - +-------------------------------+ |
| 693 | * | | Extension data | |
| 694 | * +-------------------------------+ - - - - - - - - - - - - - - - + |
| 695 | * : : |
| 696 | * +---------------------------------------------------------------+ |
| 697 | * : Application data : |
| 698 | * +---------------------------------------------------------------+ |
| 699 | * |
| 700 | * We pass payload through to userland as soon as we get it, ignoring |
| 701 | * FIN. It's up to userland to buffer it up if it wants to see a |
| 702 | * whole unfragmented block of the original size (which may be up to |
| 703 | * 2^63 long!) |
| 704 | */ |
| 705 | |
| 706 | case LWS_RXPS_04_FRAME_HDR_1: |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 707 | handle_first: |
| 708 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 709 | /* |
| 710 | * 04 spec defines the opcode like this: (1, 2, and 3 are |
| 711 | * "control frame" opcodes which may not be fragmented or |
| 712 | * have size larger than 126) |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 713 | * |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 714 | * frame-opcode = |
| 715 | * %x0 ; continuation frame |
| 716 | * / %x1 ; connection close |
| 717 | * / %x2 ; ping |
| 718 | * / %x3 ; pong |
| 719 | * / %x4 ; text frame |
| 720 | * / %x5 ; binary frame |
| 721 | * / %x6-F ; reserved |
| 722 | * |
| 723 | * FIN (b7) |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 724 | */ |
| 725 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 726 | wsi->u.ws.opcode = c & 0xf; |
| 727 | wsi->u.ws.rsv = c & 0x70; |
| 728 | wsi->u.ws.final = !!((c >> 7) & 1); |
| 729 | switch (wsi->u.ws.opcode) { |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 730 | case LWS_WS_OPCODE_07__TEXT_FRAME: |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 731 | case LWS_WS_OPCODE_07__BINARY_FRAME: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 732 | wsi->u.ws.frame_is_binary = |
| 733 | wsi->u.ws.opcode == LWS_WS_OPCODE_07__BINARY_FRAME; |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 734 | break; |
| 735 | } |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 736 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN; |
| 737 | break; |
| 738 | |
| 739 | case LWS_RXPS_04_FRAME_HDR_LEN: |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 740 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 741 | wsi->u.ws.this_frame_masked = !!(c & 0x80); |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 742 | |
Andy Green | 5bf6578 | 2011-09-25 10:46:31 +0100 | [diff] [blame] | 743 | switch (c & 0x7f) { |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 744 | case 126: |
| 745 | /* control frames are not allowed to have big lengths */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 746 | if (wsi->u.ws.opcode & 8) |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 747 | goto illegal_ctl_length; |
| 748 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 749 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2; |
| 750 | break; |
| 751 | case 127: |
| 752 | /* control frames are not allowed to have big lengths */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 753 | if (wsi->u.ws.opcode & 8) |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 754 | goto illegal_ctl_length; |
| 755 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 756 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8; |
| 757 | break; |
| 758 | default: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 759 | wsi->u.ws.rx_packet_length = c & 0x7f; |
| 760 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 761 | wsi->lws_rx_parse_state = |
| 762 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 763 | else |
| 764 | wsi->lws_rx_parse_state = |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 765 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
| 766 | break; |
| 767 | } |
| 768 | break; |
| 769 | |
| 770 | case LWS_RXPS_04_FRAME_HDR_LEN16_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 771 | wsi->u.ws.rx_packet_length = c << 8; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 772 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1; |
| 773 | break; |
| 774 | |
| 775 | case LWS_RXPS_04_FRAME_HDR_LEN16_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 776 | wsi->u.ws.rx_packet_length |= c; |
| 777 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 778 | wsi->lws_rx_parse_state = |
| 779 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 780 | else |
| 781 | wsi->lws_rx_parse_state = |
| 782 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 783 | break; |
| 784 | |
| 785 | case LWS_RXPS_04_FRAME_HDR_LEN64_8: |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 786 | if (c & 0x80) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 787 | lwsl_warn("b63 of length must be zero\n"); |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 788 | /* kill the connection */ |
| 789 | return -1; |
| 790 | } |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 791 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 792 | wsi->u.ws.rx_packet_length = ((size_t)c) << 56; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 793 | #else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 794 | wsi->u.ws.rx_packet_length = 0; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 795 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 796 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7; |
| 797 | break; |
| 798 | |
| 799 | case LWS_RXPS_04_FRAME_HDR_LEN64_7: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 800 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 801 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 48; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 802 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 803 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6; |
| 804 | break; |
| 805 | |
| 806 | case LWS_RXPS_04_FRAME_HDR_LEN64_6: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 807 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 808 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 40; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 809 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 810 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5; |
| 811 | break; |
| 812 | |
| 813 | case LWS_RXPS_04_FRAME_HDR_LEN64_5: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 814 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 815 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 32; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 816 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 817 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4; |
| 818 | break; |
| 819 | |
| 820 | case LWS_RXPS_04_FRAME_HDR_LEN64_4: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 821 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 24; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 822 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3; |
| 823 | break; |
| 824 | |
| 825 | case LWS_RXPS_04_FRAME_HDR_LEN64_3: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 826 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 16; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 827 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2; |
| 828 | break; |
| 829 | |
| 830 | case LWS_RXPS_04_FRAME_HDR_LEN64_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 831 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 8; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 832 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1; |
| 833 | break; |
| 834 | |
| 835 | case LWS_RXPS_04_FRAME_HDR_LEN64_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 836 | wsi->u.ws.rx_packet_length |= ((size_t)c); |
| 837 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 838 | wsi->lws_rx_parse_state = |
| 839 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 840 | else |
| 841 | wsi->lws_rx_parse_state = |
| 842 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 843 | break; |
| 844 | |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 845 | case LWS_RXPS_07_COLLECT_FRAME_KEY_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 846 | wsi->u.ws.frame_masking_nonce_04[0] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 847 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 848 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 849 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2; |
| 850 | break; |
| 851 | |
| 852 | case LWS_RXPS_07_COLLECT_FRAME_KEY_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 853 | wsi->u.ws.frame_masking_nonce_04[1] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 854 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 855 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 856 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3; |
| 857 | break; |
| 858 | |
| 859 | case LWS_RXPS_07_COLLECT_FRAME_KEY_3: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 860 | wsi->u.ws.frame_masking_nonce_04[2] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 861 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 862 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 863 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4; |
| 864 | break; |
| 865 | |
| 866 | case LWS_RXPS_07_COLLECT_FRAME_KEY_4: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 867 | wsi->u.ws.frame_masking_nonce_04[3] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 868 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 869 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 870 | wsi->lws_rx_parse_state = |
| 871 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 872 | wsi->u.ws.frame_mask_index = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 873 | break; |
| 874 | |
| 875 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 876 | case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 877 | |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 878 | if (!wsi->u.ws.rx_user_buffer) |
| 879 | lwsl_err("NULL user buffer...\n"); |
| 880 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 881 | if (wsi->u.ws.all_zero_nonce) |
| 882 | wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + |
| 883 | (wsi->u.ws.rx_user_buffer_head++)] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 884 | else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 885 | wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + |
| 886 | (wsi->u.ws.rx_user_buffer_head++)] = |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 887 | c ^ wsi->u.ws.frame_masking_nonce_04[ |
| 888 | (wsi->u.ws.frame_mask_index++) & 3]; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 889 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 890 | if (--wsi->u.ws.rx_packet_length == 0) { |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 891 | /* spill because we have the whole frame */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 892 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
| 893 | goto spill; |
| 894 | } |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 895 | |
| 896 | /* |
| 897 | * if there's no protocol max frame size given, we are |
| 898 | * supposed to default to LWS_MAX_SOCKET_IO_BUF |
| 899 | */ |
| 900 | |
| 901 | if (!wsi->protocol->rx_buffer_size && |
| 902 | wsi->u.ws.rx_user_buffer_head != |
| 903 | LWS_MAX_SOCKET_IO_BUF) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 904 | break; |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 905 | else |
| 906 | if (wsi->protocol->rx_buffer_size && |
| 907 | wsi->u.ws.rx_user_buffer_head != |
| 908 | wsi->protocol->rx_buffer_size) |
| 909 | break; |
| 910 | |
| 911 | /* spill because we filled our rx buffer */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 912 | spill: |
| 913 | /* |
| 914 | * is this frame a control packet we should take care of at this |
| 915 | * layer? If so service it and hide it from the user callback |
| 916 | */ |
| 917 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 918 | lwsl_parser("spill on %s\n", wsi->protocol->name); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 919 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 920 | switch (wsi->u.ws.opcode) { |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 921 | case LWS_WS_OPCODE_07__CLOSE: |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 922 | /* is this an acknowledgement of our close? */ |
| 923 | if (wsi->state == WSI_STATE_AWAITING_CLOSE_ACK) { |
| 924 | /* |
| 925 | * fine he has told us he is closing too, let's |
| 926 | * finish our close |
| 927 | */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 928 | lwsl_parser("seen client close ack\n"); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 929 | return -1; |
| 930 | } |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 931 | lwsl_parser("server sees client close packet\n"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 932 | /* parrot the close packet payload back */ |
| 933 | n = libwebsocket_write(wsi, (unsigned char *) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 934 | &wsi->u.ws.rx_user_buffer[ |
| 935 | LWS_SEND_BUFFER_PRE_PADDING], |
| 936 | wsi->u.ws.rx_user_buffer_head, |
| 937 | LWS_WRITE_CLOSE); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 938 | if (n) |
| 939 | lwsl_info("write of close ack failed %d\n", n); |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 940 | wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 941 | /* close the connection */ |
| 942 | return -1; |
| 943 | |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 944 | case LWS_WS_OPCODE_07__PING: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 945 | lwsl_info("received %d byte ping, sending pong\n", |
| 946 | wsi->u.ws.rx_user_buffer_head); |
| 947 | lwsl_hexdump(&wsi->u.ws.rx_user_buffer[ |
| 948 | LWS_SEND_BUFFER_PRE_PADDING], |
| 949 | wsi->u.ws.rx_user_buffer_head); |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 950 | /* parrot the ping packet payload back as a pong */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 951 | n = libwebsocket_write(wsi, (unsigned char *) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 952 | &wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], |
| 953 | wsi->u.ws.rx_user_buffer_head, LWS_WRITE_PONG); |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 954 | /* ... then just drop it */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 955 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 956 | return 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 957 | |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 958 | case LWS_WS_OPCODE_07__PONG: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 959 | /* ... then just drop it */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 960 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 961 | return 0; |
| 962 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 963 | case LWS_WS_OPCODE_07__TEXT_FRAME: |
| 964 | case LWS_WS_OPCODE_07__BINARY_FRAME: |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 965 | case LWS_WS_OPCODE_07__CONTINUATION: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 966 | break; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 967 | |
| 968 | default: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 969 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 970 | lwsl_parser("passing opc %x up to exts\n", |
| 971 | wsi->u.ws.opcode); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 972 | |
| 973 | /* |
| 974 | * It's something special we can't understand here. |
| 975 | * Pass the payload up to the extension's parsing |
| 976 | * state machine. |
| 977 | */ |
| 978 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 979 | eff_buf.token = &wsi->u.ws.rx_user_buffer[ |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 980 | LWS_SEND_BUFFER_PRE_PADDING]; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 981 | eff_buf.token_len = wsi->u.ws.rx_user_buffer_head; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 982 | |
| 983 | handled = 0; |
| 984 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 985 | m = wsi->active_extensions[n]->callback( |
| 986 | wsi->protocol->owning_server, |
| 987 | wsi->active_extensions[n], wsi, |
| 988 | LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX, |
| 989 | wsi->active_extensions_user[n], |
| 990 | &eff_buf, 0); |
| 991 | if (m) |
| 992 | handled = 1; |
| 993 | } |
| 994 | |
Andy Green | 25a56b0 | 2011-09-25 08:47:57 +0100 | [diff] [blame] | 995 | if (!handled) |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 996 | #endif |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 997 | lwsl_ext("ext opc opcode 0x%x unknown\n", |
| 998 | wsi->u.ws.opcode); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 999 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1000 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1001 | return 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * No it's real payload, pass it up to the user callback. |
| 1006 | * It's nicely buffered with the pre-padding taken care of |
| 1007 | * so it can be sent straight out again using libwebsocket_write |
| 1008 | */ |
| 1009 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1010 | eff_buf.token = &wsi->u.ws.rx_user_buffer[ |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1011 | LWS_SEND_BUFFER_PRE_PADDING]; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1012 | eff_buf.token_len = wsi->u.ws.rx_user_buffer_head; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1013 | #ifndef LWS_NO_EXTENSIONS |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1014 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 1015 | m = wsi->active_extensions[n]->callback( |
| 1016 | wsi->protocol->owning_server, |
| 1017 | wsi->active_extensions[n], wsi, |
| 1018 | LWS_EXT_CALLBACK_PAYLOAD_RX, |
| 1019 | wsi->active_extensions_user[n], |
| 1020 | &eff_buf, 0); |
| 1021 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1022 | lwsl_ext( |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1023 | "Extension '%s' failed to handle payload!\n", |
| 1024 | wsi->active_extensions[n]->name); |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1025 | return -1; |
| 1026 | } |
| 1027 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1028 | #endif |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1029 | if (eff_buf.token_len > 0) { |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 1030 | eff_buf.token[eff_buf.token_len] = '\0'; |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1031 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 1032 | if (wsi->protocol->callback) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1033 | ret = user_callback_handle_rxflow( |
| 1034 | wsi->protocol->callback, |
| 1035 | wsi->protocol->owning_server, |
| 1036 | wsi, LWS_CALLBACK_RECEIVE, |
| 1037 | wsi->user_space, |
| 1038 | eff_buf.token, |
| 1039 | eff_buf.token_len); |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1040 | else |
Andy Green | f7609e9 | 2013-01-14 13:10:55 +0800 | [diff] [blame] | 1041 | lwsl_err("No callback on payload spill!\n"); |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1042 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1043 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1044 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1045 | break; |
| 1046 | } |
| 1047 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 1048 | return ret; |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 1049 | |
| 1050 | illegal_ctl_length: |
| 1051 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1052 | lwsl_warn("Control frame with xtended length is illegal\n"); |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 1053 | /* kill the connection */ |
| 1054 | return -1; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1058 | int libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi, |
| 1059 | unsigned char *buf, size_t len) |
| 1060 | { |
David Galeano | aeb5748 | 2013-01-09 15:25:05 +0800 | [diff] [blame] | 1061 | size_t n; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1062 | int m; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1063 | int clear_rxflow = !!wsi->u.ws.rxflow_buffer; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1064 | struct libwebsocket_context *context = wsi->protocol->owning_server; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1065 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1066 | #if 0 |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1067 | lwsl_parser("received %d byte packet\n", (int)len); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 1068 | lwsl_hexdump(buf, len); |
Andy Green | ab7d933 | 2010-11-11 13:19:19 +0000 | [diff] [blame] | 1069 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1070 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1071 | if (buf && wsi->u.ws.rxflow_buffer) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1072 | lwsl_err("!!!! pending rxflow data loss\n"); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1073 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1074 | /* let the rx protocol state machine have as much as it needs */ |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1075 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1076 | n = 0; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1077 | if (!buf) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1078 | lwsl_info("dumping stored rxflow buffer len %d pos=%d\n", |
| 1079 | wsi->u.ws.rxflow_len, wsi->u.ws.rxflow_pos); |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1080 | buf = wsi->u.ws.rxflow_buffer; |
| 1081 | n = wsi->u.ws.rxflow_pos; |
| 1082 | len = wsi->u.ws.rxflow_len; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1083 | /* let's pretend he's already allowing input */ |
| 1084 | context->fds[wsi->position_in_fds_table].events |= POLLIN; |
| 1085 | } |
| 1086 | |
| 1087 | while (n < len) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1088 | if (!(context->fds[wsi->position_in_fds_table].events & |
| 1089 | POLLIN)) { |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1090 | /* his RX is flowcontrolled */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1091 | if (!wsi->u.ws.rxflow_buffer) { |
| 1092 | /* a new rxflow, buffer it and warn caller */ |
| 1093 | lwsl_info("new rxflow input buffer len %d\n", |
| 1094 | len - n); |
| 1095 | wsi->u.ws.rxflow_buffer = |
| 1096 | (unsigned char *)malloc(len - n); |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1097 | wsi->u.ws.rxflow_len = len - n; |
| 1098 | wsi->u.ws.rxflow_pos = 0; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1099 | memcpy(wsi->u.ws.rxflow_buffer, |
| 1100 | buf + n, len - n); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1101 | } else { |
| 1102 | lwsl_info("re-using rxflow input buffer\n"); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1103 | /* rxflow while we were spilling prev rxflow */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1104 | wsi->u.ws.rxflow_pos = n; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1105 | } |
| 1106 | return 1; |
| 1107 | } |
| 1108 | m = libwebsocket_rx_sm(wsi, buf[n]); |
| 1109 | if (m < 0) |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1110 | return -1; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1111 | n++; |
| 1112 | } |
| 1113 | |
| 1114 | if (clear_rxflow) { |
| 1115 | lwsl_info("flow: clearing it\n"); |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1116 | free(wsi->u.ws.rxflow_buffer); |
| 1117 | wsi->u.ws.rxflow_buffer = NULL; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1118 | context->fds[wsi->position_in_fds_table].events &= ~POLLIN; |
| 1119 | } |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1120 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1121 | return 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1125 | /** |
| 1126 | * libwebsockets_remaining_packet_payload() - Bytes to come before "overall" |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1127 | * rx packet is complete |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1128 | * @wsi: Websocket instance (available from user callback) |
| 1129 | * |
| 1130 | * This function is intended to be called from the callback if the |
| 1131 | * user code is interested in "complete packets" from the client. |
| 1132 | * libwebsockets just passes through payload as it comes and issues a buffer |
| 1133 | * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE |
| 1134 | * callback handler can use this API to find out if the buffer it has just |
| 1135 | * been given is the last piece of a "complete packet" from the client -- |
| 1136 | * when that is the case libwebsockets_remaining_packet_payload() will return |
| 1137 | * 0. |
| 1138 | * |
| 1139 | * Many protocols won't care becuse their packets are always small. |
| 1140 | */ |
| 1141 | |
| 1142 | size_t |
| 1143 | libwebsockets_remaining_packet_payload(struct libwebsocket *wsi) |
| 1144 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1145 | return wsi->u.ws.rx_packet_length; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1146 | } |