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 | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 30 | int LWS_WARN_UNUSED_RESULT |
| 31 | lextable_decode(int pos, char c) |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 32 | { |
Andy Green | f38ad33 | 2016-01-20 07:55:41 +0800 | [diff] [blame] | 33 | if (c >= 'A' && c <= 'Z') |
| 34 | c += 'a' - 'A'; |
Andy Green | bbc5c07 | 2014-03-09 11:49:21 +0800 | [diff] [blame] | 35 | |
| 36 | while (1) { |
| 37 | if (lextable[pos] & (1 << 7)) { /* 1-byte, fail on mismatch */ |
| 38 | if ((lextable[pos] & 0x7f) != c) |
| 39 | return -1; |
| 40 | /* fall thru */ |
| 41 | pos++; |
| 42 | if (lextable[pos] == FAIL_CHAR) |
| 43 | return -1; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 44 | return pos; |
Andy Green | bbc5c07 | 2014-03-09 11:49:21 +0800 | [diff] [blame] | 45 | } |
Andy Green | f012f75 | 2014-08-22 19:38:17 +0800 | [diff] [blame] | 46 | |
| 47 | if (lextable[pos] == FAIL_CHAR) |
| 48 | return -1; |
| 49 | |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 50 | /* b7 = 0, end or 3-byte */ |
| 51 | if (lextable[pos] < FAIL_CHAR) /* terminal marker */ |
| 52 | return pos; |
| 53 | |
| 54 | if (lextable[pos] == c) /* goto */ |
| 55 | return pos + (lextable[pos + 1]) + |
| 56 | (lextable[pos + 2] << 8); |
| 57 | /* fall thru goto */ |
| 58 | pos += 3; |
| 59 | /* continue */ |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 60 | } |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 61 | } |
| 62 | |
Andy Green | 4019aab | 2016-01-30 11:43:10 +0800 | [diff] [blame] | 63 | void |
Andy Green | e3d141d | 2016-02-27 11:42:22 +0800 | [diff] [blame] | 64 | lws_header_table_reset(struct lws *wsi, int autoservice) |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 65 | { |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 66 | struct allocated_headers *ah = wsi->u.hdr.ah; |
Andy Green | e3d141d | 2016-02-27 11:42:22 +0800 | [diff] [blame] | 67 | struct lws_context_per_thread *pt; |
| 68 | struct lws_pollfd *pfd; |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 69 | |
| 70 | /* if we have the idea we're resetting 'our' ah, must be bound to one */ |
| 71 | assert(ah); |
| 72 | /* ah also concurs with ownership */ |
| 73 | assert(ah->wsi == wsi); |
Andy Green | 892f03a | 2016-02-09 09:15:02 +0800 | [diff] [blame] | 74 | |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 75 | /* init the ah to reflect no headers or data have appeared yet */ |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 76 | memset(ah->frag_index, 0, sizeof(ah->frag_index)); |
| 77 | ah->nfrag = 0; |
| 78 | ah->pos = 0; |
| 79 | |
| 80 | /* and reset the rx state */ |
| 81 | ah->rxpos = 0; |
| 82 | ah->rxlen = 0; |
| 83 | |
| 84 | /* since we will restart the ah, our new headers are not completed */ |
| 85 | wsi->hdr_parsing_completed = 0; |
Andy Green | 51d9afa | 2016-02-24 11:05:56 +0800 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * if we inherited pending rx (from socket adoption deferred |
| 89 | * processing), apply and free it. |
| 90 | */ |
| 91 | if (wsi->u.hdr.preamble_rx) { |
Andy Green | e3d141d | 2016-02-27 11:42:22 +0800 | [diff] [blame] | 92 | memcpy(ah->rx, wsi->u.hdr.preamble_rx, |
| 93 | wsi->u.hdr.preamble_rx_len); |
Andy Green | 51d9afa | 2016-02-24 11:05:56 +0800 | [diff] [blame] | 94 | ah->rxlen = wsi->u.hdr.preamble_rx_len; |
| 95 | lws_free_set_NULL(wsi->u.hdr.preamble_rx); |
Andy Green | e3d141d | 2016-02-27 11:42:22 +0800 | [diff] [blame] | 96 | |
| 97 | if (autoservice) { |
| 98 | lwsl_notice("%s: calling service on readbuf ah\n", __func__); |
| 99 | |
| 100 | pt = &wsi->context->pt[(int)wsi->tsi]; |
| 101 | |
| 102 | /* unlike a normal connect, we have the headers already |
| 103 | * (or the first part of them anyway) |
| 104 | */ |
| 105 | pfd = &pt->fds[wsi->position_in_fds_table]; |
| 106 | pfd->revents |= LWS_POLLIN; |
| 107 | lwsl_err("%s: calling service\n", __func__); |
| 108 | lws_service_fd_tsi(wsi->context, pfd, wsi->tsi); |
| 109 | } |
Andy Green | 51d9afa | 2016-02-24 11:05:56 +0800 | [diff] [blame] | 110 | } |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 111 | } |
| 112 | |
Andy Green | 5a0e786 | 2016-01-21 10:57:39 +0800 | [diff] [blame] | 113 | int LWS_WARN_UNUSED_RESULT |
Andy Green | e3d141d | 2016-02-27 11:42:22 +0800 | [diff] [blame] | 114 | lws_header_table_attach(struct lws *wsi, int autoservice) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 115 | { |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 116 | struct lws_context *context = wsi->context; |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 117 | struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi]; |
| 118 | struct lws_pollargs pa; |
| 119 | struct lws **pwsi; |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 120 | int n; |
| 121 | |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 122 | lwsl_info("%s: wsi %p: ah %p (tsi %d)\n", __func__, (void *)wsi, |
| 123 | (void *)wsi->u.hdr.ah, wsi->tsi); |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 124 | |
| 125 | /* if we are already bound to one, just clear it down */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 126 | if (wsi->u.hdr.ah) { |
Andy Green | 8971971 | 2016-02-25 21:39:01 +0800 | [diff] [blame] | 127 | lwsl_info("cleardown\n"); |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 128 | goto reset; |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | lws_pt_lock(pt); |
| 132 | pwsi = &pt->ah_wait_list; |
| 133 | while (*pwsi) { |
| 134 | if (*pwsi == wsi) { |
| 135 | /* if already waiting on list, if no new ah just ret */ |
| 136 | if (pt->ah_count_in_use == |
| 137 | context->max_http_header_pool) { |
| 138 | lwsl_err("ah wl denied\n"); |
| 139 | goto bail; |
| 140 | } |
| 141 | /* new ah.... remove ourselves from waiting list */ |
Andy Green | 8971971 | 2016-02-25 21:39:01 +0800 | [diff] [blame] | 142 | *pwsi = wsi->u.hdr.ah_wait_list; /* set our prev to our next */ |
| 143 | wsi->u.hdr.ah_wait_list = NULL; /* no next any more */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 144 | pt->ah_wait_list_length--; |
| 145 | break; |
| 146 | } |
| 147 | pwsi = &(*pwsi)->u.hdr.ah_wait_list; |
| 148 | } |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 149 | /* |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 150 | * pool is all busy... add us to waiting list and return that we |
| 151 | * weren't able to deliver it right now |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 152 | */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 153 | if (pt->ah_count_in_use == context->max_http_header_pool) { |
Andy Green | 4019aab | 2016-01-30 11:43:10 +0800 | [diff] [blame] | 154 | lwsl_info("%s: adding %p to ah waiting list\n", __func__, wsi); |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 155 | wsi->u.hdr.ah_wait_list = pt->ah_wait_list; |
| 156 | pt->ah_wait_list = wsi; |
| 157 | pt->ah_wait_list_length++; |
| 158 | |
| 159 | /* we cannot accept input then */ |
| 160 | |
| 161 | _lws_change_pollfd(wsi, LWS_POLLIN, 0, &pa); |
| 162 | goto bail; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 163 | } |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 164 | |
| 165 | for (n = 0; n < context->max_http_header_pool; n++) |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 166 | if (!pt->ah_pool[n].in_use) |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 167 | break; |
| 168 | |
| 169 | /* if the count of in use said something free... */ |
| 170 | assert(n != context->max_http_header_pool); |
| 171 | |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 172 | wsi->u.hdr.ah = &pt->ah_pool[n]; |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 173 | wsi->u.hdr.ah->in_use = 1; |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 174 | pt->ah_pool[n].wsi = wsi; /* mark our owner */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 175 | pt->ah_count_in_use++; |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 176 | |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 177 | _lws_change_pollfd(wsi, 0, LWS_POLLIN, &pa); |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 178 | |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 179 | lwsl_info("%s: wsi %p: ah %p: count %d (on exit)\n", __func__, |
| 180 | (void *)wsi, (void *)wsi->u.hdr.ah, pt->ah_count_in_use); |
| 181 | |
| 182 | lws_pt_unlock(pt); |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 183 | |
| 184 | reset: |
Andy Green | e3d141d | 2016-02-27 11:42:22 +0800 | [diff] [blame] | 185 | lws_header_table_reset(wsi, autoservice); |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 186 | time(&wsi->u.hdr.ah->assigned); |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 187 | |
Andy Green | 2d8d35a | 2016-02-29 14:19:16 +0800 | [diff] [blame] | 188 | #ifndef LWS_NO_CLIENT |
| 189 | if (wsi->state == LWSS_CLIENT_UNCONNECTED) |
| 190 | lws_client_connect_via_info2(wsi); |
| 191 | #endif |
| 192 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 193 | return 0; |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 194 | |
| 195 | bail: |
| 196 | lws_pt_unlock(pt); |
| 197 | |
| 198 | return 1; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 199 | } |
| 200 | |
Andy Green | e3d141d | 2016-02-27 11:42:22 +0800 | [diff] [blame] | 201 | int lws_header_table_detach(struct lws *wsi, int autoservice) |
Andrew Canaday | 3771881 | 2014-11-07 11:20:59 +0800 | [diff] [blame] | 202 | { |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 203 | struct lws_context *context = wsi->context; |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 204 | struct allocated_headers *ah = wsi->u.hdr.ah; |
| 205 | struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi]; |
| 206 | struct lws_pollargs pa; |
| 207 | struct lws **pwsi; |
| 208 | time_t now; |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 209 | |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 210 | lwsl_info("%s: wsi %p: ah %p (tsi=%d, count = %d)\n", __func__, |
| 211 | (void *)wsi, (void *)wsi->u.hdr.ah, wsi->tsi, |
| 212 | pt->ah_count_in_use); |
| 213 | |
Andy Green | 51d9afa | 2016-02-24 11:05:56 +0800 | [diff] [blame] | 214 | if (wsi->u.hdr.preamble_rx) |
| 215 | lws_free_set_NULL(wsi->u.hdr.preamble_rx); |
| 216 | |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 217 | /* may not be detached while he still has unprocessed rx */ |
Andy Green | 27966c2 | 2016-02-15 21:00:39 +0800 | [diff] [blame] | 218 | if (ah && ah->rxpos != ah->rxlen) { |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 219 | lwsl_err("%s: %p: rxpos:%d, rxlen:%d\n", __func__, wsi, |
| 220 | ah->rxpos, ah->rxlen); |
| 221 | assert(ah->rxpos == ah->rxlen); |
| 222 | } |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 223 | |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 224 | lws_pt_lock(pt); |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 225 | |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 226 | pwsi = &pt->ah_wait_list; |
Andy Green | 8971971 | 2016-02-25 21:39:01 +0800 | [diff] [blame] | 227 | if (!ah) { /* remove from wait list if none attached */ |
Andy Green | 4ba798d | 2016-02-25 21:50:49 +0800 | [diff] [blame] | 228 | while (*pwsi) { |
| 229 | if (*pwsi == wsi) { |
| 230 | lwsl_info("%s: wsi %p, remv wait\n", |
| 231 | __func__, wsi); |
| 232 | *pwsi = wsi->u.hdr.ah_wait_list; |
| 233 | wsi->u.hdr.ah_wait_list = NULL; |
| 234 | pt->ah_wait_list_length--; |
| 235 | goto bail; |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 236 | } |
Andy Green | 4ba798d | 2016-02-25 21:50:49 +0800 | [diff] [blame] | 237 | pwsi = &(*pwsi)->u.hdr.ah_wait_list; |
| 238 | } |
| 239 | /* no ah, not on list... no more business here */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 240 | goto bail; |
| 241 | } |
Andy Green | 8971971 | 2016-02-25 21:39:01 +0800 | [diff] [blame] | 242 | /* we did have an ah attached */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 243 | time(&now); |
Andy Green | c1e6e38 | 2016-02-19 11:47:52 +0800 | [diff] [blame] | 244 | if (now - wsi->u.hdr.ah->assigned > 3) { |
| 245 | /* |
| 246 | * we're detaching the ah, but it was held an |
| 247 | * unreasonably long time |
| 248 | */ |
| 249 | lwsl_notice("%s: wsi %p: ah held %ds, " |
| 250 | "ah.rxpos %d, ah.rxlen %d, mode/state %d %d," |
| 251 | "wsi->more_rx_waiting %d\n", __func__, wsi, |
| 252 | (int)(now - wsi->u.hdr.ah->assigned), |
| 253 | ah->rxpos, ah->rxlen, wsi->mode, wsi->state, |
Andy Green | 83af28a | 2016-02-28 10:55:31 +0800 | [diff] [blame] | 254 | wsi->more_rx_waiting); |
Andy Green | c1e6e38 | 2016-02-19 11:47:52 +0800 | [diff] [blame] | 255 | } |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 256 | |
| 257 | /* if we think we're detaching one, there should be one in use */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 258 | assert(pt->ah_count_in_use > 0); |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 259 | /* and this specific one should have been in use */ |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 260 | assert(wsi->u.hdr.ah->in_use); |
Alejandro Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 261 | wsi->u.hdr.ah = NULL; |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 262 | ah->wsi = NULL; /* no owner */ |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 263 | |
Andy Green | 8971971 | 2016-02-25 21:39:01 +0800 | [diff] [blame] | 264 | /* oh there is nobody on the waiting list... leave it at that then */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 265 | if (!*pwsi) { |
| 266 | ah->in_use = 0; |
| 267 | pt->ah_count_in_use--; |
| 268 | |
| 269 | goto bail; |
| 270 | } |
| 271 | |
Andy Green | 8971971 | 2016-02-25 21:39:01 +0800 | [diff] [blame] | 272 | /* somebody else on same tsi is waiting, give it to oldest guy */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 273 | |
| 274 | lwsl_info("pt wait list %p\n", *pwsi); |
| 275 | while ((*pwsi)->u.hdr.ah_wait_list) |
| 276 | pwsi = &(*pwsi)->u.hdr.ah_wait_list; |
| 277 | |
| 278 | wsi = *pwsi; |
| 279 | lwsl_info("last wsi in wait list %p\n", wsi); |
| 280 | |
| 281 | wsi->u.hdr.ah = ah; |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 282 | ah->wsi = wsi; /* new owner */ |
Andy Green | e3d141d | 2016-02-27 11:42:22 +0800 | [diff] [blame] | 283 | lws_header_table_reset(wsi, autoservice); |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 284 | time(&wsi->u.hdr.ah->assigned); |
| 285 | |
| 286 | assert(wsi->position_in_fds_table != -1); |
| 287 | |
| 288 | lwsl_info("%s: Enabling %p POLLIN\n", __func__, wsi); |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 289 | |
| 290 | /* he has been stuck waiting for an ah, but now his wait is over, |
| 291 | * let him progress |
| 292 | */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 293 | _lws_change_pollfd(wsi, 0, LWS_POLLIN, &pa); |
| 294 | |
| 295 | /* point prev guy to next guy in list instead */ |
| 296 | *pwsi = wsi->u.hdr.ah_wait_list; |
Andy Green | 8971971 | 2016-02-25 21:39:01 +0800 | [diff] [blame] | 297 | /* the guy who got one is out of the list */ |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 298 | wsi->u.hdr.ah_wait_list = NULL; |
| 299 | pt->ah_wait_list_length--; |
| 300 | |
Andy Green | 2d8d35a | 2016-02-29 14:19:16 +0800 | [diff] [blame] | 301 | #ifndef LWS_NO_CLIENT |
| 302 | if (wsi->state == LWSS_CLIENT_UNCONNECTED) |
| 303 | lws_client_connect_via_info2(wsi); |
| 304 | #endif |
| 305 | |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 306 | assert(!!pt->ah_wait_list_length == !!(int)(long)pt->ah_wait_list); |
| 307 | bail: |
| 308 | lws_pt_unlock(pt); |
| 309 | |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 310 | return 0; |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 311 | } |
Andrew Canaday | 3771881 | 2014-11-07 11:20:59 +0800 | [diff] [blame] | 312 | |
Andy Green | 0c7e5a9 | 2015-12-30 14:53:50 +0800 | [diff] [blame] | 313 | /** |
| 314 | * lws_hdr_fragment_length: report length of a single fragment of a header |
| 315 | * The returned length does not include the space for a |
| 316 | * terminating '\0' |
| 317 | * |
| 318 | * @wsi: websocket connection |
| 319 | * @h: which header index we are interested in |
| 320 | * @frag_idx: which fragment of @h we want to get the length of |
| 321 | */ |
| 322 | |
Andy Green | e974f0c | 2015-12-19 07:35:23 +0800 | [diff] [blame] | 323 | LWS_VISIBLE int |
| 324 | lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx) |
| 325 | { |
| 326 | int n; |
| 327 | |
| 328 | n = wsi->u.hdr.ah->frag_index[h]; |
| 329 | if (!n) |
| 330 | return 0; |
| 331 | do { |
| 332 | if (!frag_idx) |
| 333 | return wsi->u.hdr.ah->frags[n].len; |
| 334 | n = wsi->u.hdr.ah->frags[n].nfrag; |
| 335 | } while (frag_idx-- && n); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Andy Green | 0c7e5a9 | 2015-12-30 14:53:50 +0800 | [diff] [blame] | 340 | /** |
| 341 | * lws_hdr_total_length: report length of all fragments of a header totalled up |
| 342 | * The returned length does not include the space for a |
| 343 | * terminating '\0' |
| 344 | * |
| 345 | * @wsi: websocket connection |
| 346 | * @h: which header index we are interested in |
| 347 | */ |
| 348 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 349 | LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 350 | { |
| 351 | int n; |
| 352 | int len = 0; |
| 353 | |
| 354 | n = wsi->u.hdr.ah->frag_index[h]; |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 355 | if (!n) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 356 | return 0; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 357 | do { |
| 358 | len += wsi->u.hdr.ah->frags[n].len; |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 359 | n = wsi->u.hdr.ah->frags[n].nfrag; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 360 | } while (n); |
| 361 | |
| 362 | return len; |
| 363 | } |
| 364 | |
Andy Green | 0c7e5a9 | 2015-12-30 14:53:50 +0800 | [diff] [blame] | 365 | /** |
| 366 | * lws_hdr_copy_fragment: copy a single fragment of the given header to a buffer |
| 367 | * The buffer length @len must include space for an additional |
| 368 | * terminating '\0', or it will fail returning -1. |
| 369 | * If the requested fragment index is not present, it fails |
| 370 | * returning -1. |
| 371 | * |
| 372 | * @wsi: websocket connection |
| 373 | * @dst: destination buffer |
| 374 | * @len: length of destination buffer |
| 375 | * @h: which header index we are interested in |
| 376 | * @frag_index: which fragment of @h we want to copy |
| 377 | */ |
| 378 | |
Andy Green | 566eb43 | 2015-12-15 22:59:23 +0800 | [diff] [blame] | 379 | LWS_VISIBLE int lws_hdr_copy_fragment(struct lws *wsi, char *dst, int len, |
| 380 | enum lws_token_indexes h, int frag_idx) |
| 381 | { |
| 382 | int n = 0; |
| 383 | int f = wsi->u.hdr.ah->frag_index[h]; |
| 384 | |
Andy Green | af607da | 2016-01-20 09:44:04 +0800 | [diff] [blame] | 385 | if (!f) |
| 386 | return -1; |
| 387 | |
Andy Green | 566eb43 | 2015-12-15 22:59:23 +0800 | [diff] [blame] | 388 | while (n < frag_idx) { |
| 389 | f = wsi->u.hdr.ah->frags[f].nfrag; |
| 390 | if (!f) |
| 391 | return -1; |
| 392 | n++; |
| 393 | } |
| 394 | |
Andy Green | 9f54c1f | 2015-12-18 16:40:02 +0800 | [diff] [blame] | 395 | if (wsi->u.hdr.ah->frags[f].len >= len) |
Andy Green | 566eb43 | 2015-12-15 22:59:23 +0800 | [diff] [blame] | 396 | return -1; |
| 397 | |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 398 | memcpy(dst, wsi->u.hdr.ah->data + wsi->u.hdr.ah->frags[f].offset, |
Andy Green | 566eb43 | 2015-12-15 22:59:23 +0800 | [diff] [blame] | 399 | wsi->u.hdr.ah->frags[f].len); |
| 400 | dst[wsi->u.hdr.ah->frags[f].len] = '\0'; |
| 401 | |
| 402 | return wsi->u.hdr.ah->frags[f].len; |
| 403 | } |
| 404 | |
Andy Green | 0c7e5a9 | 2015-12-30 14:53:50 +0800 | [diff] [blame] | 405 | /** |
| 406 | * lws_hdr_copy: copy a single fragment of the given header to a buffer |
| 407 | * The buffer length @len must include space for an additional |
| 408 | * terminating '\0', or it will fail returning -1. |
| 409 | * |
| 410 | * @wsi: websocket connection |
| 411 | * @dst: destination buffer |
| 412 | * @len: length of destination buffer |
| 413 | * @h: which header index we are interested in |
| 414 | */ |
| 415 | |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 416 | LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dst, int len, |
Andy Green | 5c9660d | 2015-12-04 11:30:53 +0800 | [diff] [blame] | 417 | enum lws_token_indexes h) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 418 | { |
| 419 | int toklen = lws_hdr_total_length(wsi, h); |
| 420 | int n; |
| 421 | |
| 422 | if (toklen >= len) |
| 423 | return -1; |
| 424 | |
| 425 | n = wsi->u.hdr.ah->frag_index[h]; |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 426 | if (!n) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 427 | return 0; |
| 428 | |
| 429 | do { |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 430 | strcpy(dst, &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]); |
| 431 | dst += wsi->u.hdr.ah->frags[n].len; |
| 432 | n = wsi->u.hdr.ah->frags[n].nfrag; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 433 | } while (n); |
| 434 | |
| 435 | return toklen; |
| 436 | } |
| 437 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 438 | char *lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 439 | { |
| 440 | int n; |
| 441 | |
| 442 | n = wsi->u.hdr.ah->frag_index[h]; |
| 443 | if (!n) |
| 444 | return NULL; |
| 445 | |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 446 | return wsi->u.hdr.ah->data + wsi->u.hdr.ah->frags[n].offset; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 447 | } |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 448 | |
Andy Green | 5a0e786 | 2016-01-21 10:57:39 +0800 | [diff] [blame] | 449 | int LWS_WARN_UNUSED_RESULT |
| 450 | lws_pos_in_bounds(struct lws *wsi) |
| 451 | { |
| 452 | if (wsi->u.hdr.ah->pos < wsi->context->max_http_header_data) |
| 453 | return 0; |
| 454 | |
| 455 | if (wsi->u.hdr.ah->pos == wsi->context->max_http_header_data) { |
| 456 | lwsl_err("Ran out of header data space\n"); |
| 457 | return 1; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * with these tests everywhere, it should never be able to exceed |
| 462 | * the limit, only meet the limit |
| 463 | */ |
| 464 | |
| 465 | lwsl_err("%s: pos %d, limit %d\n", __func__, wsi->u.hdr.ah->pos, |
| 466 | wsi->context->max_http_header_data); |
| 467 | assert(0); |
| 468 | |
| 469 | return 1; |
| 470 | } |
| 471 | |
| 472 | int LWS_WARN_UNUSED_RESULT |
| 473 | lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s) |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 474 | { |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 475 | wsi->u.hdr.ah->nfrag++; |
| 476 | if (wsi->u.hdr.ah->nfrag == ARRAY_SIZE(wsi->u.hdr.ah->frags)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 477 | lwsl_warn("More hdr frags than we can deal with, dropping\n"); |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 478 | return -1; |
| 479 | } |
| 480 | |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 481 | wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->nfrag; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 482 | |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 483 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].offset = wsi->u.hdr.ah->pos; |
| 484 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len = 0; |
| 485 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].nfrag = 0; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 486 | |
| 487 | do { |
Andy Green | 5a0e786 | 2016-01-21 10:57:39 +0800 | [diff] [blame] | 488 | if (lws_pos_in_bounds(wsi)) |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 489 | return -1; |
Andy Green | 5a0e786 | 2016-01-21 10:57:39 +0800 | [diff] [blame] | 490 | |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 491 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s; |
| 492 | if (*s) |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 493 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 494 | } while (*s++); |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
Andy Green | 2a5774e | 2015-03-30 18:56:52 +0800 | [diff] [blame] | 499 | static signed char char_to_hex(const char c) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 500 | { |
| 501 | if (c >= '0' && c <= '9') |
| 502 | return c - '0'; |
| 503 | |
| 504 | if (c >= 'a' && c <= 'f') |
| 505 | return c - 'a' + 10; |
| 506 | |
| 507 | if (c >= 'A' && c <= 'F') |
| 508 | return c - 'A' + 10; |
| 509 | |
| 510 | return -1; |
| 511 | } |
| 512 | |
Andy Green | e99a83c | 2016-01-20 16:56:06 +0800 | [diff] [blame] | 513 | static int LWS_WARN_UNUSED_RESULT |
| 514 | issue_char(struct lws *wsi, unsigned char c) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 515 | { |
Andrew Canaday | ffe6456 | 2015-11-29 19:26:01 +0800 | [diff] [blame] | 516 | unsigned short frag_len; |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 517 | |
Andy Green | 5a0e786 | 2016-01-21 10:57:39 +0800 | [diff] [blame] | 518 | if (lws_pos_in_bounds(wsi)) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 519 | return -1; |
Andrew Canaday | 74b4a65 | 2014-06-29 00:25:19 -0400 | [diff] [blame] | 520 | |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 521 | frag_len = wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len; |
| 522 | /* |
| 523 | * If we haven't hit the token limit, just copy the character into |
| 524 | * the header |
| 525 | */ |
| 526 | if (frag_len < wsi->u.hdr.current_token_limit) { |
Andrew Canaday | 4cfc42c | 2015-11-29 19:24:04 +0800 | [diff] [blame] | 527 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c; |
| 528 | if (c) |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 529 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++; |
Andrew Canaday | 4cfc42c | 2015-11-29 19:24:04 +0800 | [diff] [blame] | 530 | return 0; |
| 531 | } |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 532 | |
| 533 | /* Insert a null character when we *hit* the limit: */ |
| 534 | if (frag_len == wsi->u.hdr.current_token_limit) { |
Andy Green | 5a0e786 | 2016-01-21 10:57:39 +0800 | [diff] [blame] | 535 | if (lws_pos_in_bounds(wsi)) |
Andy Green | bc40129 | 2016-01-20 07:40:13 +0800 | [diff] [blame] | 536 | return -1; |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 537 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0'; |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 538 | lwsl_warn("header %i exceeds limit %d\n", |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 539 | wsi->u.hdr.parser_state, |
| 540 | wsi->u.hdr.current_token_limit); |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 541 | } |
| 542 | |
Andrew Canaday | 4cfc42c | 2015-11-29 19:24:04 +0800 | [diff] [blame] | 543 | return 1; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 544 | } |
| 545 | |
Andy Green | e99a83c | 2016-01-20 16:56:06 +0800 | [diff] [blame] | 546 | int LWS_WARN_UNUSED_RESULT |
| 547 | lws_parse(struct lws *wsi, unsigned char c) |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 548 | { |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 549 | static const unsigned char methods[] = { |
| 550 | WSI_TOKEN_GET_URI, |
| 551 | WSI_TOKEN_POST_URI, |
| 552 | WSI_TOKEN_OPTIONS_URI, |
| 553 | WSI_TOKEN_PUT_URI, |
| 554 | WSI_TOKEN_PATCH_URI, |
| 555 | WSI_TOKEN_DELETE_URI, |
| 556 | }; |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 557 | struct allocated_headers *ah = wsi->u.hdr.ah; |
| 558 | struct lws_context *context = wsi->context; |
Andy Green | 38f3225 | 2015-12-14 19:42:26 +0800 | [diff] [blame] | 559 | unsigned int n, m, enc = 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 560 | |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 561 | assert(wsi->u.hdr.ah); |
| 562 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 563 | switch (wsi->u.hdr.parser_state) { |
Andy Green | b08cb50 | 2014-09-30 16:33:56 +0800 | [diff] [blame] | 564 | default: |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 565 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 566 | 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] | 567 | |
| 568 | /* collect into malloc'd buffers */ |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 569 | /* optional initial space swallow */ |
Andy Green | 5a0e786 | 2016-01-21 10:57:39 +0800 | [diff] [blame] | 570 | if (!ah->frags[ah->frag_index[wsi->u.hdr.parser_state]].len && |
| 571 | c == ' ') |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 572 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 573 | |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 574 | for (m = 0; m < ARRAY_SIZE(methods); m++) |
| 575 | if (wsi->u.hdr.parser_state == methods[m]) |
| 576 | break; |
| 577 | if (m == ARRAY_SIZE(methods)) |
| 578 | /* it was not any of the methods */ |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 579 | goto check_eol; |
| 580 | |
| 581 | /* special URI processing... end at space */ |
| 582 | |
| 583 | if (c == ' ') { |
| 584 | /* enforce starting with / */ |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 585 | if (!ah->frags[ah->nfrag].len) |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 586 | if (issue_char(wsi, '/') < 0) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 587 | return -1; |
Andrew Canaday | afe26cf | 2014-07-13 01:07:36 -0400 | [diff] [blame] | 588 | |
| 589 | /* begin parsing HTTP version: */ |
| 590 | if (issue_char(wsi, '\0') < 0) |
| 591 | return -1; |
| 592 | wsi->u.hdr.parser_state = WSI_TOKEN_HTTP; |
| 593 | goto start_fragment; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 596 | /* special URI processing... convert %xx */ |
| 597 | |
| 598 | switch (wsi->u.hdr.ues) { |
| 599 | case URIES_IDLE: |
| 600 | if (c == '%') { |
| 601 | wsi->u.hdr.ues = URIES_SEEN_PERCENT; |
| 602 | goto swallow; |
| 603 | } |
| 604 | break; |
| 605 | case URIES_SEEN_PERCENT: |
| 606 | if (char_to_hex(c) < 0) { |
| 607 | /* regurgitate */ |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 608 | if (issue_char(wsi, '%') < 0) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 609 | return -1; |
| 610 | wsi->u.hdr.ues = URIES_IDLE; |
| 611 | /* continue on to assess c */ |
| 612 | break; |
| 613 | } |
| 614 | wsi->u.hdr.esc_stash = c; |
| 615 | wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1; |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 616 | goto swallow; |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 617 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 618 | case URIES_SEEN_PERCENT_H1: |
| 619 | if (char_to_hex(c) < 0) { |
| 620 | /* regurgitate */ |
Andy Green | e99a83c | 2016-01-20 16:56:06 +0800 | [diff] [blame] | 621 | if (issue_char(wsi, '%') < 0) |
| 622 | return -1; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 623 | wsi->u.hdr.ues = URIES_IDLE; |
| 624 | /* regurgitate + assess */ |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 625 | if (lws_parse(wsi, wsi->u.hdr.esc_stash) < 0) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 626 | return -1; |
| 627 | /* continue on to assess c */ |
| 628 | break; |
| 629 | } |
| 630 | c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) | |
| 631 | char_to_hex(c); |
Andy Green | 38f3225 | 2015-12-14 19:42:26 +0800 | [diff] [blame] | 632 | enc = 1; |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 633 | wsi->u.hdr.ues = URIES_IDLE; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | |
| 637 | /* |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 638 | * special URI processing... |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 639 | * convert /.. or /... or /../ etc to / |
| 640 | * convert /./ to / |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 641 | * convert // or /// etc to / |
| 642 | * leave /.dir or whatever alone |
| 643 | */ |
| 644 | |
| 645 | switch (wsi->u.hdr.ups) { |
| 646 | case URIPS_IDLE: |
Andy Green | bc40129 | 2016-01-20 07:40:13 +0800 | [diff] [blame] | 647 | if (!c) |
| 648 | return -1; |
Andy Green | 4eb3637 | 2015-12-15 22:57:19 +0800 | [diff] [blame] | 649 | /* genuine delimiter */ |
Andy Green | 03d7e9d | 2015-12-18 15:20:09 +0800 | [diff] [blame] | 650 | if ((c == '&' || c == ';') && !enc) { |
Andy Green | bc40129 | 2016-01-20 07:40:13 +0800 | [diff] [blame] | 651 | if (issue_char(wsi, c) < 0) |
| 652 | return -1; |
Andy Green | 4eb3637 | 2015-12-15 22:57:19 +0800 | [diff] [blame] | 653 | /* swallow the terminator */ |
| 654 | ah->frags[ah->nfrag].len--; |
| 655 | /* link to next fragment */ |
| 656 | ah->frags[ah->nfrag].nfrag = ah->nfrag + 1; |
| 657 | ah->nfrag++; |
| 658 | if (ah->nfrag >= ARRAY_SIZE(ah->frags)) |
| 659 | goto excessive; |
| 660 | /* start next fragment after the & */ |
Andy Green | 3ba035d | 2015-12-18 15:40:03 +0800 | [diff] [blame] | 661 | wsi->u.hdr.post_literal_equal = 0; |
Andy Green | 4eb3637 | 2015-12-15 22:57:19 +0800 | [diff] [blame] | 662 | ah->frags[ah->nfrag].offset = ah->pos; |
| 663 | ah->frags[ah->nfrag].len = 0; |
| 664 | ah->frags[ah->nfrag].nfrag = 0; |
| 665 | goto swallow; |
| 666 | } |
Andy Green | 3ba035d | 2015-12-18 15:40:03 +0800 | [diff] [blame] | 667 | /* uriencoded = in the name part, disallow */ |
| 668 | if (c == '=' && enc && !wsi->u.hdr.post_literal_equal) |
| 669 | c = '_'; |
| 670 | |
| 671 | /* after the real =, we don't care how many = */ |
| 672 | if (c == '=' && !enc) |
| 673 | wsi->u.hdr.post_literal_equal = 1; |
| 674 | |
Andy Green | 8b9fe99 | 2015-12-18 15:23:31 +0800 | [diff] [blame] | 675 | /* + to space */ |
| 676 | if (c == '+' && !enc) |
| 677 | c = ' '; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 678 | /* issue the first / always */ |
Andy Green | 4eb3637 | 2015-12-15 22:57:19 +0800 | [diff] [blame] | 679 | if (c == '/' && !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS]) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 680 | wsi->u.hdr.ups = URIPS_SEEN_SLASH; |
| 681 | break; |
| 682 | case URIPS_SEEN_SLASH: |
| 683 | /* swallow subsequent slashes */ |
| 684 | if (c == '/') |
| 685 | goto swallow; |
| 686 | /* track and swallow the first . after / */ |
| 687 | if (c == '.') { |
| 688 | wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT; |
| 689 | goto swallow; |
Andy Green | 4b812fe | 2014-08-19 18:34:31 +0800 | [diff] [blame] | 690 | } |
| 691 | wsi->u.hdr.ups = URIPS_IDLE; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 692 | break; |
| 693 | case URIPS_SEEN_SLASH_DOT: |
| 694 | /* swallow second . */ |
| 695 | if (c == '.') { |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 696 | /* |
Andy Green | d3f6873 | 2013-11-13 06:53:21 +0800 | [diff] [blame] | 697 | * back up one dir level if possible |
| 698 | * safe against header fragmentation because |
| 699 | * the method URI can only be in 1 fragment |
| 700 | */ |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 701 | if (ah->frags[ah->nfrag].len > 2) { |
| 702 | ah->pos--; |
| 703 | ah->frags[ah->nfrag].len--; |
Andy Green | d3f6873 | 2013-11-13 06:53:21 +0800 | [diff] [blame] | 704 | do { |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 705 | ah->pos--; |
| 706 | ah->frags[ah->nfrag].len--; |
| 707 | } while (ah->frags[ah->nfrag].len > 1 && |
| 708 | ah->data[ah->pos] != '/'); |
Andy Green | d3f6873 | 2013-11-13 06:53:21 +0800 | [diff] [blame] | 709 | } |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 710 | wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT; |
| 711 | goto swallow; |
| 712 | } |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 713 | /* change /./ to / */ |
| 714 | if (c == '/') { |
| 715 | wsi->u.hdr.ups = URIPS_SEEN_SLASH; |
| 716 | goto swallow; |
| 717 | } |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 718 | /* it was like /.dir ... regurgitate the . */ |
| 719 | wsi->u.hdr.ups = URIPS_IDLE; |
Andy Green | 4eb3637 | 2015-12-15 22:57:19 +0800 | [diff] [blame] | 720 | if (issue_char(wsi, '.') < 0) |
| 721 | return -1; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 722 | break; |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 723 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 724 | case URIPS_SEEN_SLASH_DOT_DOT: |
| 725 | /* swallow prior .. chars and any subsequent . */ |
| 726 | if (c == '.') |
| 727 | goto swallow; |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 728 | /* last issued was /, so another / == // */ |
| 729 | if (c == '/') |
| 730 | goto swallow; |
Andy Green | 4b812fe | 2014-08-19 18:34:31 +0800 | [diff] [blame] | 731 | /* last we issued was / so SEEN_SLASH */ |
| 732 | wsi->u.hdr.ups = URIPS_SEEN_SLASH; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 733 | break; |
| 734 | } |
| 735 | |
Andy Green | 4eb3637 | 2015-12-15 22:57:19 +0800 | [diff] [blame] | 736 | if (c == '?' && !enc && |
| 737 | !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS]) { /* start of URI arguments */ |
Andy Green | 1e3f7b8 | 2013-11-13 07:45:17 +0800 | [diff] [blame] | 738 | /* seal off uri header */ |
Andy Green | bc40129 | 2016-01-20 07:40:13 +0800 | [diff] [blame] | 739 | if (issue_char(wsi, '\0') < 0) |
| 740 | return -1; |
Andy Green | 1e3f7b8 | 2013-11-13 07:45:17 +0800 | [diff] [blame] | 741 | |
| 742 | /* move to using WSI_TOKEN_HTTP_URI_ARGS */ |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 743 | ah->nfrag++; |
Andy Green | 4eb3637 | 2015-12-15 22:57:19 +0800 | [diff] [blame] | 744 | if (ah->nfrag >= ARRAY_SIZE(ah->frags)) |
| 745 | goto excessive; |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 746 | ah->frags[ah->nfrag].offset = ah->pos; |
| 747 | ah->frags[ah->nfrag].len = 0; |
| 748 | ah->frags[ah->nfrag].nfrag = 0; |
Andy Green | 1e3f7b8 | 2013-11-13 07:45:17 +0800 | [diff] [blame] | 749 | |
Andy Green | 3ba035d | 2015-12-18 15:40:03 +0800 | [diff] [blame] | 750 | wsi->u.hdr.post_literal_equal = 0; |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 751 | ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] = ah->nfrag; |
Andy Green | f35801b | 2015-12-15 23:05:23 +0800 | [diff] [blame] | 752 | wsi->u.hdr.ups = URIPS_IDLE; |
Andy Green | 1e3f7b8 | 2013-11-13 07:45:17 +0800 | [diff] [blame] | 753 | goto swallow; |
| 754 | } |
| 755 | |
Andrew Canaday | 991f1cd | 2014-07-19 06:58:53 +0800 | [diff] [blame] | 756 | check_eol: |
| 757 | |
| 758 | /* bail at EOL */ |
| 759 | if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE && |
Andy Green | 2c218e7 | 2016-02-15 12:37:04 +0800 | [diff] [blame] | 760 | c == '\x0d') { |
Andrew Canaday | 991f1cd | 2014-07-19 06:58:53 +0800 | [diff] [blame] | 761 | c = '\0'; |
| 762 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR; |
| 763 | lwsl_parser("*\n"); |
| 764 | } |
| 765 | |
Andy Green | 4b812fe | 2014-08-19 18:34:31 +0800 | [diff] [blame] | 766 | n = issue_char(wsi, c); |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 767 | if ((int)n < 0) |
Andy Green | 4b812fe | 2014-08-19 18:34:31 +0800 | [diff] [blame] | 768 | return -1; |
| 769 | if (n > 0) |
| 770 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
| 771 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 772 | swallow: |
Andy Green | d1b11e3 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 773 | /* per-protocol end of headers management */ |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 774 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 775 | if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE) |
| 776 | goto set_parsing_complete; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 777 | break; |
| 778 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 779 | /* collecting and checking a name part */ |
| 780 | case WSI_TOKEN_NAME_PART: |
Andy Green | 8fb338f | 2015-04-07 08:19:30 +0800 | [diff] [blame] | 781 | lwsl_parser("WSI_TOKEN_NAME_PART '%c' (mode=%d)\n", c, wsi->mode); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 782 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 783 | wsi->u.hdr.lextable_pos = |
| 784 | lextable_decode(wsi->u.hdr.lextable_pos, c); |
Andy Green | 8fb338f | 2015-04-07 08:19:30 +0800 | [diff] [blame] | 785 | /* |
| 786 | * Server needs to look out for unknown methods... |
| 787 | */ |
| 788 | if (wsi->u.hdr.lextable_pos < 0 && |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 789 | wsi->mode == LWSCM_HTTP_SERVING) { |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 790 | /* this is not a header we know about */ |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 791 | for (m = 0; m < ARRAY_SIZE(methods); m++) |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 792 | if (ah->frag_index[methods[m]]) { |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 793 | /* |
| 794 | * already had the method, no idea what |
Andy Green | 8fb338f | 2015-04-07 08:19:30 +0800 | [diff] [blame] | 795 | * this crap from the client is, ignore |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 796 | */ |
| 797 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
| 798 | break; |
| 799 | } |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 800 | /* |
Andy Green | 8fb338f | 2015-04-07 08:19:30 +0800 | [diff] [blame] | 801 | * hm it's an unknown http method from a client in fact, |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 802 | * treat as dangerous |
| 803 | */ |
Drew Noakes | 2121e8a | 2015-01-30 12:04:43 +0000 | [diff] [blame] | 804 | if (m == ARRAY_SIZE(methods)) { |
| 805 | lwsl_info("Unknown method - dropping\n"); |
| 806 | return -1; |
| 807 | } |
| 808 | break; |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 809 | } |
Andy Green | 8fb338f | 2015-04-07 08:19:30 +0800 | [diff] [blame] | 810 | /* |
| 811 | * ...otherwise for a client, let him ignore unknown headers |
| 812 | * coming from the server |
| 813 | */ |
| 814 | if (wsi->u.hdr.lextable_pos < 0) { |
| 815 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
| 816 | break; |
| 817 | } |
| 818 | |
Andy Green | bbc5c07 | 2014-03-09 11:49:21 +0800 | [diff] [blame] | 819 | if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) { |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 820 | /* terminal state */ |
| 821 | |
Andy Green | 59800b7 | 2014-11-30 12:36:09 +0800 | [diff] [blame] | 822 | n = ((unsigned int)lextable[wsi->u.hdr.lextable_pos] << 8) | |
| 823 | lextable[wsi->u.hdr.lextable_pos + 1]; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 824 | |
Andy Green | cc7cb68 | 2013-02-18 10:22:42 +0800 | [diff] [blame] | 825 | lwsl_parser("known hdr %d\n", n); |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 826 | for (m = 0; m < ARRAY_SIZE(methods); m++) |
| 827 | if (n == methods[m] && |
Andy Green | 8c1f602 | 2016-01-26 20:56:56 +0800 | [diff] [blame] | 828 | ah->frag_index[methods[m]]) { |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 829 | lwsl_warn("Duplicated method\n"); |
| 830 | return -1; |
| 831 | } |
Andy Green | 94f9465 | 2013-02-12 13:10:19 +0800 | [diff] [blame] | 832 | |
Andy Green | fd96330 | 2012-04-03 17:02:20 +0200 | [diff] [blame] | 833 | /* |
| 834 | * WSORIGIN is protocol equiv to ORIGIN, |
| 835 | * JWebSocket likes to send it, map to ORIGIN |
| 836 | */ |
| 837 | if (n == WSI_TOKEN_SWORIGIN) |
| 838 | n = WSI_TOKEN_ORIGIN; |
| 839 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 840 | wsi->u.hdr.parser_state = (enum lws_token_indexes) |
| 841 | (WSI_TOKEN_GET_URI + n); |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 842 | |
Andy Green | f012f75 | 2014-08-22 19:38:17 +0800 | [diff] [blame] | 843 | if (context->token_limits) |
| 844 | wsi->u.hdr.current_token_limit = |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 845 | context->token_limits->token_limit[ |
| 846 | wsi->u.hdr.parser_state]; |
Andy Green | f012f75 | 2014-08-22 19:38:17 +0800 | [diff] [blame] | 847 | else |
Andy Green | 3df5800 | 2015-12-25 12:44:12 +0800 | [diff] [blame] | 848 | wsi->u.hdr.current_token_limit = |
| 849 | wsi->context->max_http_header_data; |
Andrew Canaday | 18fd4f6 | 2014-06-29 01:34:24 -0400 | [diff] [blame] | 850 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 851 | if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE) |
| 852 | goto set_parsing_complete; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 853 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 854 | goto start_fragment; |
| 855 | } |
| 856 | break; |
Nick Dowell | 3059263 | 2012-04-05 10:31:48 +0800 | [diff] [blame] | 857 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 858 | start_fragment: |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 859 | ah->nfrag++; |
Andy Green | 4eb3637 | 2015-12-15 22:57:19 +0800 | [diff] [blame] | 860 | excessive: |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 861 | if (ah->nfrag == ARRAY_SIZE(ah->frags)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 862 | lwsl_warn("More hdr frags than we can deal with\n"); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 863 | return -1; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 866 | ah->frags[ah->nfrag].offset = ah->pos; |
| 867 | ah->frags[ah->nfrag].len = 0; |
Andy Green | 5a0e786 | 2016-01-21 10:57:39 +0800 | [diff] [blame] | 868 | ah->frags[ah->nfrag].nfrag = 0; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 869 | |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 870 | n = ah->frag_index[wsi->u.hdr.parser_state]; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 871 | if (!n) { /* first fragment */ |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 872 | ah->frag_index[wsi->u.hdr.parser_state] = ah->nfrag; |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 873 | break; |
| 874 | } |
| 875 | /* continuation */ |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 876 | while (ah->frags[n].nfrag) |
Andy Green | 5a0e786 | 2016-01-21 10:57:39 +0800 | [diff] [blame] | 877 | n = ah->frags[n].nfrag; |
Andy Green | 6b5de70 | 2015-12-15 21:15:58 +0800 | [diff] [blame] | 878 | ah->frags[n].nfrag = ah->nfrag; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 879 | |
Andy Green | bc40129 | 2016-01-20 07:40:13 +0800 | [diff] [blame] | 880 | if (issue_char(wsi, ' ') < 0) |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 881 | return -1; |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 882 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 883 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 884 | /* skipping arg part of a name we didn't recognize */ |
| 885 | case WSI_TOKEN_SKIPPING: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 886 | lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c); |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 887 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 888 | if (c == '\x0d') |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 889 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 890 | break; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 891 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 892 | case WSI_TOKEN_SKIPPING_SAW_CR: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 893 | lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c); |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 894 | if (c == '\x0a') { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 895 | wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART; |
| 896 | wsi->u.hdr.lextable_pos = 0; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 897 | } else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 898 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 899 | break; |
| 900 | /* we're done, ignore anything else */ |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 901 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 902 | case WSI_PARSING_COMPLETE: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 903 | lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 904 | break; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 905 | } |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 906 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 907 | return 0; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 908 | |
| 909 | set_parsing_complete: |
| 910 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 911 | if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 912 | if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION)) |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 913 | wsi->ietf_spec_revision = |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 914 | atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION)); |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 915 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 916 | lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision); |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 917 | } |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 918 | wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE; |
Andy Green | 224149a | 2013-02-11 21:43:41 +0800 | [diff] [blame] | 919 | wsi->hdr_parsing_completed = 1; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 920 | |
| 921 | return 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 924 | |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 925 | /** |
| 926 | * lws_frame_is_binary: true if the current frame was sent in binary mode |
| 927 | * |
| 928 | * @wsi: the connection we are inquiring about |
| 929 | * |
| 930 | * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if |
| 931 | * it's interested to see if the frame it's dealing with was sent in binary |
| 932 | * mode. |
| 933 | */ |
| 934 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 935 | LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi) |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 936 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 937 | return wsi->u.ws.frame_is_binary; |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 938 | } |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 939 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 940 | int |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 941 | lws_rx_sm(struct lws *wsi, unsigned char c) |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 942 | { |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 943 | struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi]; |
John Tarlton | 05fc6ba | 2015-10-05 11:35:52 +0100 | [diff] [blame] | 944 | int callback_action = LWS_CALLBACK_RECEIVE; |
Andy Green | 1b2c9a2 | 2016-01-29 21:18:54 +0800 | [diff] [blame] | 945 | int ret = 0, n, rx_draining_ext = 0; |
| 946 | struct lws_tokens eff_buf; |
| 947 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 948 | if (wsi->socket_is_permanently_unusable) |
| 949 | return -1; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 950 | |
| 951 | switch (wsi->lws_rx_parse_state) { |
| 952 | case LWS_RXPS_NEW: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 953 | if (wsi->u.ws.rx_draining_ext) { |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 954 | struct lws **w = &pt->rx_draining_ext_list; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 955 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 956 | eff_buf.token = NULL; |
| 957 | eff_buf.token_len = 0; |
| 958 | wsi->u.ws.rx_draining_ext = 0; |
| 959 | /* remove us from context draining ext list */ |
| 960 | while (*w) { |
| 961 | if (*w == wsi) { |
| 962 | *w = wsi->u.ws.rx_draining_ext_list; |
| 963 | break; |
| 964 | } |
| 965 | w = &((*w)->u.ws.rx_draining_ext_list); |
| 966 | } |
| 967 | wsi->u.ws.rx_draining_ext_list = NULL; |
| 968 | rx_draining_ext = 1; |
| 969 | lwsl_err("%s: doing draining flow\n", __func__); |
| 970 | |
| 971 | goto drain_extension; |
| 972 | } |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 973 | switch (wsi->ietf_spec_revision) { |
Andy Green | d85cb20 | 2011-09-25 09:32:54 +0100 | [diff] [blame] | 974 | case 13: |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 975 | /* |
| 976 | * no prepended frame key any more |
| 977 | */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 978 | wsi->u.ws.all_zero_nonce = 1; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 979 | goto handle_first; |
| 980 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 981 | default: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 982 | lwsl_warn("lws_rx_sm: unknown spec version %d\n", |
| 983 | wsi->ietf_spec_revision); |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 984 | break; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 985 | } |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 986 | break; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 987 | case LWS_RXPS_04_mask_1: |
| 988 | wsi->u.ws.mask[1] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 989 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 990 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 991 | wsi->lws_rx_parse_state = LWS_RXPS_04_mask_2; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 992 | break; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 993 | case LWS_RXPS_04_mask_2: |
| 994 | wsi->u.ws.mask[2] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 995 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 996 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 997 | wsi->lws_rx_parse_state = LWS_RXPS_04_mask_3; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 998 | break; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 999 | case LWS_RXPS_04_mask_3: |
| 1000 | wsi->u.ws.mask[3] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 1001 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1002 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 1003 | |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 1004 | /* |
| 1005 | * start from the zero'th byte in the XOR key buffer since |
| 1006 | * this is the start of a frame with a new key |
| 1007 | */ |
| 1008 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1009 | wsi->u.ws.mask_idx = 0; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1010 | |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 1011 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1; |
| 1012 | break; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1013 | |
| 1014 | /* |
| 1015 | * 04 logical framing from the spec (all this is masked when incoming |
| 1016 | * and has to be unmasked) |
| 1017 | * |
| 1018 | * We ignore the possibility of extension data because we don't |
| 1019 | * negotiate any extensions at the moment. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1020 | * |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1021 | * 0 1 2 3 |
| 1022 | * 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 |
| 1023 | * +-+-+-+-+-------+-+-------------+-------------------------------+ |
| 1024 | * |F|R|R|R| opcode|R| Payload len | Extended payload length | |
| 1025 | * |I|S|S|S| (4) |S| (7) | (16/63) | |
| 1026 | * |N|V|V|V| |V| | (if payload len==126/127) | |
| 1027 | * | |1|2|3| |4| | | |
| 1028 | * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + |
| 1029 | * | Extended payload length continued, if payload len == 127 | |
| 1030 | * + - - - - - - - - - - - - - - - +-------------------------------+ |
| 1031 | * | | Extension data | |
| 1032 | * +-------------------------------+ - - - - - - - - - - - - - - - + |
| 1033 | * : : |
| 1034 | * +---------------------------------------------------------------+ |
| 1035 | * : Application data : |
| 1036 | * +---------------------------------------------------------------+ |
| 1037 | * |
| 1038 | * We pass payload through to userland as soon as we get it, ignoring |
| 1039 | * FIN. It's up to userland to buffer it up if it wants to see a |
| 1040 | * whole unfragmented block of the original size (which may be up to |
| 1041 | * 2^63 long!) |
| 1042 | */ |
| 1043 | |
| 1044 | case LWS_RXPS_04_FRAME_HDR_1: |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1045 | handle_first: |
| 1046 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1047 | wsi->u.ws.opcode = c & 0xf; |
| 1048 | wsi->u.ws.rsv = c & 0x70; |
| 1049 | wsi->u.ws.final = !!((c >> 7) & 1); |
Andy Green | 1bc12f9 | 2013-02-28 17:11:29 +0800 | [diff] [blame] | 1050 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1051 | switch (wsi->u.ws.opcode) { |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 1052 | case LWSWSOPC_TEXT_FRAME: |
| 1053 | case LWSWSOPC_BINARY_FRAME: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1054 | wsi->u.ws.rsv_first_msg = (c & 0x70); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1055 | wsi->u.ws.frame_is_binary = |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 1056 | wsi->u.ws.opcode == LWSWSOPC_BINARY_FRAME; |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 1057 | break; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1058 | case 3: |
| 1059 | case 4: |
| 1060 | case 5: |
| 1061 | case 6: |
| 1062 | case 7: |
| 1063 | case 0xb: |
| 1064 | case 0xc: |
| 1065 | case 0xd: |
| 1066 | case 0xe: |
| 1067 | case 0xf: |
| 1068 | lwsl_info("illegal opcode\n"); |
| 1069 | return -1; |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 1070 | } |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1071 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN; |
| 1072 | break; |
| 1073 | |
| 1074 | case LWS_RXPS_04_FRAME_HDR_LEN: |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1075 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1076 | wsi->u.ws.this_frame_masked = !!(c & 0x80); |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1077 | |
Andy Green | 5bf6578 | 2011-09-25 10:46:31 +0100 | [diff] [blame] | 1078 | switch (c & 0x7f) { |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1079 | case 126: |
| 1080 | /* control frames are not allowed to have big lengths */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1081 | if (wsi->u.ws.opcode & 8) |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 1082 | goto illegal_ctl_length; |
| 1083 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1084 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2; |
| 1085 | break; |
| 1086 | case 127: |
| 1087 | /* control frames are not allowed to have big lengths */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1088 | if (wsi->u.ws.opcode & 8) |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 1089 | goto illegal_ctl_length; |
| 1090 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1091 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8; |
| 1092 | break; |
| 1093 | default: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1094 | wsi->u.ws.rx_packet_length = c & 0x7f; |
| 1095 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1096 | wsi->lws_rx_parse_state = |
| 1097 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 1098 | else |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 1099 | if (wsi->u.ws.rx_packet_length) |
| 1100 | wsi->lws_rx_parse_state = |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1101 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 1102 | else { |
| 1103 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
| 1104 | goto spill; |
| 1105 | } |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1106 | break; |
| 1107 | } |
| 1108 | break; |
| 1109 | |
| 1110 | case LWS_RXPS_04_FRAME_HDR_LEN16_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1111 | wsi->u.ws.rx_packet_length = c << 8; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1112 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1; |
| 1113 | break; |
| 1114 | |
| 1115 | case LWS_RXPS_04_FRAME_HDR_LEN16_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1116 | wsi->u.ws.rx_packet_length |= c; |
| 1117 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1118 | wsi->lws_rx_parse_state = |
| 1119 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 1120 | else |
| 1121 | wsi->lws_rx_parse_state = |
| 1122 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1123 | break; |
| 1124 | |
| 1125 | case LWS_RXPS_04_FRAME_HDR_LEN64_8: |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1126 | if (c & 0x80) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1127 | lwsl_warn("b63 of length must be zero\n"); |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1128 | /* kill the connection */ |
| 1129 | return -1; |
| 1130 | } |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 1131 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1132 | wsi->u.ws.rx_packet_length = ((size_t)c) << 56; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 1133 | #else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1134 | wsi->u.ws.rx_packet_length = 0; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 1135 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1136 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7; |
| 1137 | break; |
| 1138 | |
| 1139 | case LWS_RXPS_04_FRAME_HDR_LEN64_7: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 1140 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1141 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 48; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 1142 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1143 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6; |
| 1144 | break; |
| 1145 | |
| 1146 | case LWS_RXPS_04_FRAME_HDR_LEN64_6: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 1147 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1148 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 40; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 1149 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1150 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5; |
| 1151 | break; |
| 1152 | |
| 1153 | case LWS_RXPS_04_FRAME_HDR_LEN64_5: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 1154 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1155 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 32; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 1156 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1157 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4; |
| 1158 | break; |
| 1159 | |
| 1160 | case LWS_RXPS_04_FRAME_HDR_LEN64_4: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1161 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 24; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1162 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3; |
| 1163 | break; |
| 1164 | |
| 1165 | case LWS_RXPS_04_FRAME_HDR_LEN64_3: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1166 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 16; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1167 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2; |
| 1168 | break; |
| 1169 | |
| 1170 | case LWS_RXPS_04_FRAME_HDR_LEN64_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1171 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 8; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1172 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1; |
| 1173 | break; |
| 1174 | |
| 1175 | case LWS_RXPS_04_FRAME_HDR_LEN64_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1176 | wsi->u.ws.rx_packet_length |= ((size_t)c); |
| 1177 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1178 | wsi->lws_rx_parse_state = |
| 1179 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 1180 | else |
| 1181 | wsi->lws_rx_parse_state = |
| 1182 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1183 | break; |
| 1184 | |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1185 | case LWS_RXPS_07_COLLECT_FRAME_KEY_1: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1186 | wsi->u.ws.mask[0] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1187 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1188 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1189 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2; |
| 1190 | break; |
| 1191 | |
| 1192 | case LWS_RXPS_07_COLLECT_FRAME_KEY_2: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1193 | wsi->u.ws.mask[1] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1194 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1195 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1196 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3; |
| 1197 | break; |
| 1198 | |
| 1199 | case LWS_RXPS_07_COLLECT_FRAME_KEY_3: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1200 | wsi->u.ws.mask[2] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1201 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1202 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1203 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4; |
| 1204 | break; |
| 1205 | |
| 1206 | case LWS_RXPS_07_COLLECT_FRAME_KEY_4: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1207 | wsi->u.ws.mask[3] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1208 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1209 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1210 | wsi->lws_rx_parse_state = |
| 1211 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1212 | wsi->u.ws.mask_idx = 0; |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 1213 | if (wsi->u.ws.rx_packet_length == 0) { |
| 1214 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
Andy Green | 1bc12f9 | 2013-02-28 17:11:29 +0800 | [diff] [blame] | 1215 | goto spill; |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 1216 | } |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 1217 | break; |
| 1218 | |
| 1219 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1220 | case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1221 | assert(wsi->u.ws.rx_ubuf); |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 1222 | |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 1223 | if (wsi->u.ws.rx_ubuf_head + LWS_PRE >= |
| 1224 | wsi->u.ws.rx_ubuf_alloc) { |
| 1225 | lwsl_err("Attempted overflow \n"); |
| 1226 | return -1; |
| 1227 | } |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1228 | if (wsi->u.ws.all_zero_nonce) |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1229 | wsi->u.ws.rx_ubuf[LWS_PRE + |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 1230 | (wsi->u.ws.rx_ubuf_head++)] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 1231 | else |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1232 | wsi->u.ws.rx_ubuf[LWS_PRE + |
| 1233 | (wsi->u.ws.rx_ubuf_head++)] = |
| 1234 | c ^ wsi->u.ws.mask[ |
| 1235 | (wsi->u.ws.mask_idx++) & 3]; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1236 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1237 | if (--wsi->u.ws.rx_packet_length == 0) { |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 1238 | /* spill because we have the whole frame */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1239 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
| 1240 | goto spill; |
| 1241 | } |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 1242 | |
| 1243 | /* |
| 1244 | * if there's no protocol max frame size given, we are |
| 1245 | * supposed to default to LWS_MAX_SOCKET_IO_BUF |
| 1246 | */ |
| 1247 | |
| 1248 | if (!wsi->protocol->rx_buffer_size && |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1249 | wsi->u.ws.rx_ubuf_head != |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 1250 | LWS_MAX_SOCKET_IO_BUF) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1251 | break; |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 1252 | else |
| 1253 | if (wsi->protocol->rx_buffer_size && |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1254 | wsi->u.ws.rx_ubuf_head != |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 1255 | wsi->protocol->rx_buffer_size) |
| 1256 | break; |
| 1257 | |
| 1258 | /* spill because we filled our rx buffer */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1259 | spill: |
| 1260 | /* |
| 1261 | * is this frame a control packet we should take care of at this |
| 1262 | * layer? If so service it and hide it from the user callback |
| 1263 | */ |
| 1264 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1265 | lwsl_parser("spill on %s\n", wsi->protocol->name); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1266 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1267 | switch (wsi->u.ws.opcode) { |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 1268 | case LWSWSOPC_CLOSE: |
Andy Green | 066a7a1 | 2015-12-26 15:47:06 +0800 | [diff] [blame] | 1269 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 1270 | /* is this an acknowledgement of our close? */ |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 1271 | if (wsi->state == LWSS_AWAITING_CLOSE_ACK) { |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 1272 | /* |
| 1273 | * fine he has told us he is closing too, let's |
| 1274 | * finish our close |
| 1275 | */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1276 | lwsl_parser("seen client close ack\n"); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 1277 | return -1; |
| 1278 | } |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 1279 | if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY) |
Andy Green | 40d5abc | 2015-04-17 20:29:58 +0800 | [diff] [blame] | 1280 | /* if he sends us 2 CLOSE, kill him */ |
| 1281 | return -1; |
| 1282 | |
Andy Green | 066a7a1 | 2015-12-26 15:47:06 +0800 | [diff] [blame] | 1283 | if (user_callback_handle_rxflow( |
| 1284 | wsi->protocol->callback, wsi, |
| 1285 | LWS_CALLBACK_WS_PEER_INITIATED_CLOSE, |
| 1286 | wsi->user_space, |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 1287 | &wsi->u.ws.rx_ubuf[LWS_PRE], |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1288 | wsi->u.ws.rx_ubuf_head)) |
Andy Green | 066a7a1 | 2015-12-26 15:47:06 +0800 | [diff] [blame] | 1289 | return -1; |
| 1290 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1291 | lwsl_parser("server sees client close packet\n"); |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 1292 | wsi->state = LWSS_RETURNED_CLOSE_ALREADY; |
Andy Green | 40d5abc | 2015-04-17 20:29:58 +0800 | [diff] [blame] | 1293 | /* deal with the close packet contents as a PONG */ |
| 1294 | wsi->u.ws.payload_is_close = 1; |
| 1295 | goto process_as_ping; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1296 | |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 1297 | case LWSWSOPC_PING: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1298 | lwsl_info("received %d byte ping, sending pong\n", |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1299 | wsi->u.ws.rx_ubuf_head); |
Andy Green | 82bac6b | 2014-08-24 14:39:19 +0800 | [diff] [blame] | 1300 | |
Andy Green | 2fd6e6f | 2015-03-24 21:07:01 +0800 | [diff] [blame] | 1301 | if (wsi->u.ws.ping_pending_flag) { |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 1302 | /* |
Andy Green | 82bac6b | 2014-08-24 14:39:19 +0800 | [diff] [blame] | 1303 | * there is already a pending ping payload |
| 1304 | * we should just log and drop |
| 1305 | */ |
| 1306 | lwsl_parser("DROP PING since one pending\n"); |
| 1307 | goto ping_drop; |
| 1308 | } |
Andy Green | 40d5abc | 2015-04-17 20:29:58 +0800 | [diff] [blame] | 1309 | process_as_ping: |
Andy Green | 82bac6b | 2014-08-24 14:39:19 +0800 | [diff] [blame] | 1310 | /* control packets can only be < 128 bytes long */ |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1311 | if (wsi->u.ws.rx_ubuf_head > 128 - 3) { |
Andy Green | 82bac6b | 2014-08-24 14:39:19 +0800 | [diff] [blame] | 1312 | lwsl_parser("DROP PING payload too large\n"); |
| 1313 | goto ping_drop; |
| 1314 | } |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 1315 | |
Andy Green | 82bac6b | 2014-08-24 14:39:19 +0800 | [diff] [blame] | 1316 | /* stash the pong payload */ |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1317 | memcpy(wsi->u.ws.ping_payload_buf + LWS_PRE, |
| 1318 | &wsi->u.ws.rx_ubuf[LWS_PRE], |
| 1319 | wsi->u.ws.rx_ubuf_head); |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 1320 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1321 | wsi->u.ws.ping_payload_len = wsi->u.ws.rx_ubuf_head; |
Andy Green | 2fd6e6f | 2015-03-24 21:07:01 +0800 | [diff] [blame] | 1322 | wsi->u.ws.ping_pending_flag = 1; |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 1323 | |
Andy Green | 82bac6b | 2014-08-24 14:39:19 +0800 | [diff] [blame] | 1324 | /* get it sent as soon as possible */ |
Andy Green | 11c05bf | 2015-12-16 18:19:08 +0800 | [diff] [blame] | 1325 | lws_callback_on_writable(wsi); |
Andy Green | 82bac6b | 2014-08-24 14:39:19 +0800 | [diff] [blame] | 1326 | ping_drop: |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1327 | wsi->u.ws.rx_ubuf_head = 0; |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 1328 | return 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1329 | |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 1330 | case LWSWSOPC_PONG: |
John Tarlton | 05fc6ba | 2015-10-05 11:35:52 +0100 | [diff] [blame] | 1331 | lwsl_info("received pong\n"); |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1332 | lwsl_hexdump(&wsi->u.ws.rx_ubuf[LWS_PRE], |
| 1333 | wsi->u.ws.rx_ubuf_head); |
John Tarlton | 05fc6ba | 2015-10-05 11:35:52 +0100 | [diff] [blame] | 1334 | |
| 1335 | /* issue it */ |
| 1336 | callback_action = LWS_CALLBACK_RECEIVE_PONG; |
| 1337 | break; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1338 | |
Andy Green | 54806b1 | 2015-12-17 17:03:59 +0800 | [diff] [blame] | 1339 | case LWSWSOPC_TEXT_FRAME: |
| 1340 | case LWSWSOPC_BINARY_FRAME: |
| 1341 | case LWSWSOPC_CONTINUATION: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1342 | break; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1343 | |
| 1344 | default: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1345 | lwsl_parser("passing opc %x up to exts\n", |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 1346 | wsi->u.ws.opcode); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1347 | /* |
| 1348 | * It's something special we can't understand here. |
| 1349 | * Pass the payload up to the extension's parsing |
| 1350 | * state machine. |
| 1351 | */ |
| 1352 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1353 | eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE]; |
| 1354 | eff_buf.token_len = wsi->u.ws.rx_ubuf_head; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1355 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1356 | if (lws_ext_cb_active(wsi, LWS_EXT_CB_EXTENDED_PAYLOAD_RX, |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 1357 | &eff_buf, 0) <= 0) /* not handle or fail */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1358 | lwsl_ext("ext opc opcode 0x%x unknown\n", |
| 1359 | wsi->u.ws.opcode); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1360 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1361 | wsi->u.ws.rx_ubuf_head = 0; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1362 | return 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | /* |
| 1366 | * No it's real payload, pass it up to the user callback. |
| 1367 | * It's nicely buffered with the pre-padding taken care of |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 1368 | * so it can be sent straight out again using lws_write |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1369 | */ |
| 1370 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1371 | eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE]; |
| 1372 | eff_buf.token_len = wsi->u.ws.rx_ubuf_head; |
Andy Green | 40110e8 | 2015-12-14 08:52:03 +0800 | [diff] [blame] | 1373 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1374 | drain_extension: |
| 1375 | lwsl_ext("%s: passing %d to ext\n", __func__, eff_buf.token_len); |
| 1376 | |
| 1377 | if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY || |
| 1378 | wsi->state == LWSS_AWAITING_CLOSE_ACK) |
| 1379 | goto already_done; |
| 1380 | |
| 1381 | n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_RX, &eff_buf, 0); |
| 1382 | if (n < 0) { |
| 1383 | /* |
| 1384 | * we may rely on this to get RX, just drop connection |
| 1385 | */ |
| 1386 | wsi->socket_is_permanently_unusable = 1; |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 1387 | return -1; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | if (rx_draining_ext && eff_buf.token_len == 0) |
| 1391 | goto already_done; |
| 1392 | |
| 1393 | if (n && eff_buf.token_len) { |
| 1394 | /* extension had more... main loop will come back */ |
| 1395 | wsi->u.ws.rx_draining_ext = 1; |
Andy Green | d3a5505 | 2016-01-19 03:34:24 +0800 | [diff] [blame] | 1396 | wsi->u.ws.rx_draining_ext_list = pt->rx_draining_ext_list; |
| 1397 | pt->rx_draining_ext_list = wsi; |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1398 | } |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 1399 | |
Andrejs Hanins | 765914c | 2015-12-01 14:44:33 +0200 | [diff] [blame] | 1400 | if (eff_buf.token_len > 0 || |
| 1401 | callback_action == LWS_CALLBACK_RECEIVE_PONG) { |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 1402 | eff_buf.token[eff_buf.token_len] = '\0'; |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1403 | |
John Tarlton | 05fc6ba | 2015-10-05 11:35:52 +0100 | [diff] [blame] | 1404 | if (wsi->protocol->callback) { |
| 1405 | |
| 1406 | if (callback_action == LWS_CALLBACK_RECEIVE_PONG) |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1407 | lwsl_info("Doing pong callback\n"); |
John Tarlton | 05fc6ba | 2015-10-05 11:35:52 +0100 | [diff] [blame] | 1408 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1409 | ret = user_callback_handle_rxflow( |
| 1410 | wsi->protocol->callback, |
John Tarlton | 05fc6ba | 2015-10-05 11:35:52 +0100 | [diff] [blame] | 1411 | wsi, |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 1412 | (enum lws_callback_reasons)callback_action, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1413 | wsi->user_space, |
| 1414 | eff_buf.token, |
| 1415 | eff_buf.token_len); |
John Tarlton | 05fc6ba | 2015-10-05 11:35:52 +0100 | [diff] [blame] | 1416 | } |
| 1417 | else |
| 1418 | lwsl_err("No callback on payload spill!\n"); |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1419 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1420 | |
Andy Green | 6711266 | 2016-01-11 11:34:01 +0800 | [diff] [blame] | 1421 | already_done: |
| 1422 | wsi->u.ws.rx_ubuf_head = 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1423 | break; |
| 1424 | } |
| 1425 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 1426 | return ret; |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 1427 | |
| 1428 | illegal_ctl_length: |
| 1429 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1430 | lwsl_warn("Control frame with xtended length is illegal\n"); |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 1431 | /* kill the connection */ |
| 1432 | return -1; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1436 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 1437 | * lws_remaining_packet_payload() - Bytes to come before "overall" |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1438 | * rx packet is complete |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1439 | * @wsi: Websocket instance (available from user callback) |
| 1440 | * |
| 1441 | * This function is intended to be called from the callback if the |
| 1442 | * user code is interested in "complete packets" from the client. |
| 1443 | * libwebsockets just passes through payload as it comes and issues a buffer |
| 1444 | * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE |
| 1445 | * callback handler can use this API to find out if the buffer it has just |
| 1446 | * been given is the last piece of a "complete packet" from the client -- |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 1447 | * when that is the case lws_remaining_packet_payload() will return |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1448 | * 0. |
| 1449 | * |
| 1450 | * Many protocols won't care becuse their packets are always small. |
| 1451 | */ |
| 1452 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1453 | LWS_VISIBLE size_t |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame] | 1454 | lws_remaining_packet_payload(struct lws *wsi) |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1455 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1456 | return wsi->u.ws.rx_packet_length; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1457 | } |