parsers.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #include "private-libwebsockets.h"
  22. const unsigned char lextable[] = {
  23. #include "lextable.h"
  24. };
  25. #define FAIL_CHAR 0x08
  26. int LWS_WARN_UNUSED_RESULT
  27. lextable_decode(int pos, char c)
  28. {
  29. if (c >= 'A' && c <= 'Z')
  30. c += 'a' - 'A';
  31. while (1) {
  32. if (lextable[pos] & (1 << 7)) { /* 1-byte, fail on mismatch */
  33. if ((lextable[pos] & 0x7f) != c)
  34. return -1;
  35. /* fall thru */
  36. pos++;
  37. if (lextable[pos] == FAIL_CHAR)
  38. return -1;
  39. return pos;
  40. }
  41. if (lextable[pos] == FAIL_CHAR)
  42. return -1;
  43. /* b7 = 0, end or 3-byte */
  44. if (lextable[pos] < FAIL_CHAR) /* terminal marker */
  45. return pos;
  46. if (lextable[pos] == c) /* goto */
  47. return pos + (lextable[pos + 1]) +
  48. (lextable[pos + 2] << 8);
  49. /* fall thru goto */
  50. pos += 3;
  51. /* continue */
  52. }
  53. }
  54. void
  55. _lws_header_table_reset(struct allocated_headers *ah)
  56. {
  57. /* init the ah to reflect no headers or data have appeared yet */
  58. memset(ah->frag_index, 0, sizeof(ah->frag_index));
  59. ah->nfrag = 0;
  60. ah->pos = 0;
  61. ah->http_response = 0;
  62. }
  63. // doesn't scrub the ah rxbuffer by default, parent must do if needed
  64. void
  65. lws_header_table_reset(struct lws *wsi, int autoservice)
  66. {
  67. struct allocated_headers *ah = wsi->u.hdr.ah;
  68. struct lws_context_per_thread *pt;
  69. struct lws_pollfd *pfd;
  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);
  74. _lws_header_table_reset(ah);
  75. /* since we will restart the ah, our new headers are not completed */
  76. // wsi->hdr_parsing_completed = 0;
  77. /*
  78. * if we inherited pending rx (from socket adoption deferred
  79. * processing), apply and free it.
  80. */
  81. if (wsi->u.hdr.preamble_rx) {
  82. memcpy(ah->rx, wsi->u.hdr.preamble_rx,
  83. wsi->u.hdr.preamble_rx_len);
  84. ah->rxlen = wsi->u.hdr.preamble_rx_len;
  85. lws_free_set_NULL(wsi->u.hdr.preamble_rx);
  86. if (autoservice) {
  87. lwsl_notice("%s: calling service on readbuf ah\n", __func__);
  88. pt = &wsi->context->pt[(int)wsi->tsi];
  89. /* unlike a normal connect, we have the headers already
  90. * (or the first part of them anyway)
  91. */
  92. pfd = &pt->fds[wsi->position_in_fds_table];
  93. pfd->revents |= LWS_POLLIN;
  94. lwsl_err("%s: calling service\n", __func__);
  95. lws_service_fd_tsi(wsi->context, pfd, wsi->tsi);
  96. }
  97. }
  98. }
  99. int LWS_WARN_UNUSED_RESULT
  100. lws_header_table_attach(struct lws *wsi, int autoservice)
  101. {
  102. struct lws_context *context = wsi->context;
  103. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  104. struct lws_pollargs pa;
  105. struct lws **pwsi;
  106. int n;
  107. lwsl_info("%s: wsi %p: ah %p (tsi %d, count = %d) in\n", __func__, (void *)wsi,
  108. (void *)wsi->u.hdr.ah, wsi->tsi, pt->ah_count_in_use);
  109. /* if we are already bound to one, just clear it down */
  110. if (wsi->u.hdr.ah) {
  111. lwsl_info("cleardown\n");
  112. goto reset;
  113. }
  114. lws_pt_lock(pt);
  115. pwsi = &pt->ah_wait_list;
  116. while (*pwsi) {
  117. if (*pwsi == wsi) {
  118. /* if already waiting on list, if no new ah just ret */
  119. if (pt->ah_count_in_use ==
  120. context->max_http_header_pool) {
  121. lwsl_notice("%s: no free ah to attach\n", __func__);
  122. goto bail;
  123. }
  124. /* new ah.... remove ourselves from waiting list */
  125. *pwsi = wsi->u.hdr.ah_wait_list; /* set our prev to our next */
  126. wsi->u.hdr.ah_wait_list = NULL; /* no next any more */
  127. pt->ah_wait_list_length--;
  128. break;
  129. }
  130. pwsi = &(*pwsi)->u.hdr.ah_wait_list;
  131. }
  132. /*
  133. * pool is all busy... add us to waiting list and return that we
  134. * weren't able to deliver it right now
  135. */
  136. if (pt->ah_count_in_use == context->max_http_header_pool) {
  137. lwsl_info("%s: adding %p to ah waiting list\n", __func__, wsi);
  138. wsi->u.hdr.ah_wait_list = pt->ah_wait_list;
  139. pt->ah_wait_list = wsi;
  140. pt->ah_wait_list_length++;
  141. /* we cannot accept input then */
  142. _lws_change_pollfd(wsi, LWS_POLLIN, 0, &pa);
  143. goto bail;
  144. }
  145. for (n = 0; n < context->max_http_header_pool; n++)
  146. if (!pt->ah_pool[n].in_use)
  147. break;
  148. /* if the count of in use said something free... */
  149. assert(n != context->max_http_header_pool);
  150. wsi->u.hdr.ah = &pt->ah_pool[n];
  151. wsi->u.hdr.ah->in_use = 1;
  152. pt->ah_pool[n].wsi = wsi; /* mark our owner */
  153. pt->ah_count_in_use++;
  154. _lws_change_pollfd(wsi, 0, LWS_POLLIN, &pa);
  155. lwsl_info("%s: wsi %p: ah %p: count %d (on exit)\n", __func__,
  156. (void *)wsi, (void *)wsi->u.hdr.ah, pt->ah_count_in_use);
  157. lws_pt_unlock(pt);
  158. reset:
  159. /* and reset the rx state */
  160. wsi->u.hdr.ah->rxpos = 0;
  161. wsi->u.hdr.ah->rxlen = 0;
  162. lws_header_table_reset(wsi, autoservice);
  163. time(&wsi->u.hdr.ah->assigned);
  164. #ifndef LWS_NO_CLIENT
  165. if (wsi->state == LWSS_CLIENT_UNCONNECTED)
  166. if (!lws_client_connect_via_info2(wsi))
  167. /* our client connect has failed, the wsi
  168. * has been closed
  169. */
  170. return -1;
  171. #endif
  172. return 0;
  173. bail:
  174. lws_pt_unlock(pt);
  175. return 1;
  176. }
  177. int lws_header_table_detach(struct lws *wsi, int autoservice)
  178. {
  179. struct lws_context *context = wsi->context;
  180. struct allocated_headers *ah = wsi->u.hdr.ah;
  181. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  182. struct lws_pollargs pa;
  183. struct lws **pwsi;
  184. time_t now;
  185. lwsl_info("%s: wsi %p: ah %p (tsi=%d, count = %d)\n", __func__,
  186. (void *)wsi, (void *)ah, wsi->tsi,
  187. pt->ah_count_in_use);
  188. if (wsi->u.hdr.preamble_rx)
  189. lws_free_set_NULL(wsi->u.hdr.preamble_rx);
  190. /* may not be detached while he still has unprocessed rx */
  191. if (ah && ah->rxpos != ah->rxlen) {
  192. lwsl_err("%s: %p: CANNOT DETACH rxpos:%d, rxlen:%d\n", __func__, wsi,
  193. ah->rxpos, ah->rxlen);
  194. assert(ah->rxpos == ah->rxlen);
  195. return 0;
  196. }
  197. lws_pt_lock(pt);
  198. pwsi = &pt->ah_wait_list;
  199. if (!ah) { /* remove from wait list if none attached */
  200. while (*pwsi) {
  201. if (*pwsi == wsi) {
  202. lwsl_info("%s: wsi %p, remv wait\n",
  203. __func__, wsi);
  204. *pwsi = wsi->u.hdr.ah_wait_list;
  205. wsi->u.hdr.ah_wait_list = NULL;
  206. pt->ah_wait_list_length--;
  207. goto bail;
  208. }
  209. pwsi = &(*pwsi)->u.hdr.ah_wait_list;
  210. }
  211. /* no ah, not on list... no more business here */
  212. goto bail;
  213. }
  214. /* we did have an ah attached */
  215. time(&now);
  216. if (ah->assigned && now - ah->assigned > 3) {
  217. /*
  218. * we're detaching the ah, but it was held an
  219. * unreasonably long time
  220. */
  221. lwsl_notice("%s: wsi %p: ah held %ds, "
  222. "ah.rxpos %d, ah.rxlen %d, mode/state %d %d,"
  223. "wsi->more_rx_waiting %d\n", __func__, wsi,
  224. (int)(now - ah->assigned),
  225. ah->rxpos, ah->rxlen, wsi->mode, wsi->state,
  226. wsi->more_rx_waiting);
  227. }
  228. ah->assigned = 0;
  229. /* if we think we're detaching one, there should be one in use */
  230. assert(pt->ah_count_in_use > 0);
  231. /* and this specific one should have been in use */
  232. assert(ah->in_use);
  233. wsi->u.hdr.ah = NULL;
  234. ah->wsi = NULL; /* no owner */
  235. /* oh there is nobody on the waiting list... leave it at that then */
  236. if (!*pwsi) {
  237. ah->in_use = 0;
  238. pt->ah_count_in_use--;
  239. goto bail;
  240. }
  241. /* somebody else on same tsi is waiting, give it to oldest guy */
  242. lwsl_info("pt wait list %p\n", *pwsi);
  243. while ((*pwsi)->u.hdr.ah_wait_list)
  244. pwsi = &(*pwsi)->u.hdr.ah_wait_list;
  245. wsi = *pwsi;
  246. lwsl_info("last wsi in wait list %p\n", wsi);
  247. wsi->u.hdr.ah = ah;
  248. ah->wsi = wsi; /* new owner */
  249. /* and reset the rx state */
  250. ah->rxpos = 0;
  251. ah->rxlen = 0;
  252. lws_header_table_reset(wsi, autoservice);
  253. time(&wsi->u.hdr.ah->assigned);
  254. /* clients acquire the ah and then insert themselves in fds table... */
  255. if (wsi->position_in_fds_table != -1) {
  256. lwsl_info("%s: Enabling %p POLLIN\n", __func__, wsi);
  257. /* he has been stuck waiting for an ah, but now his wait is over,
  258. * let him progress
  259. */
  260. _lws_change_pollfd(wsi, 0, LWS_POLLIN, &pa);
  261. }
  262. /* point prev guy to next guy in list instead */
  263. *pwsi = wsi->u.hdr.ah_wait_list;
  264. /* the guy who got one is out of the list */
  265. wsi->u.hdr.ah_wait_list = NULL;
  266. pt->ah_wait_list_length--;
  267. #ifndef LWS_NO_CLIENT
  268. if (wsi->state == LWSS_CLIENT_UNCONNECTED)
  269. if (!lws_client_connect_via_info2(wsi)) {
  270. /* our client connect has failed, the wsi
  271. * has been closed
  272. */
  273. lws_pt_unlock(pt);
  274. return -1;
  275. }
  276. #endif
  277. assert(!!pt->ah_wait_list_length == !!(int)(long)pt->ah_wait_list);
  278. bail:
  279. lwsl_info("%s: wsi %p: ah %p (tsi=%d, count = %d)\n", __func__,
  280. (void *)wsi, (void *)ah, wsi->tsi,
  281. pt->ah_count_in_use);
  282. lws_pt_unlock(pt);
  283. return 0;
  284. }
  285. LWS_VISIBLE int
  286. lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx)
  287. {
  288. int n;
  289. n = wsi->u.hdr.ah->frag_index[h];
  290. if (!n)
  291. return 0;
  292. do {
  293. if (!frag_idx)
  294. return wsi->u.hdr.ah->frags[n].len;
  295. n = wsi->u.hdr.ah->frags[n].nfrag;
  296. } while (frag_idx-- && n);
  297. return 0;
  298. }
  299. LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h)
  300. {
  301. int n;
  302. int len = 0;
  303. n = wsi->u.hdr.ah->frag_index[h];
  304. if (!n)
  305. return 0;
  306. do {
  307. len += wsi->u.hdr.ah->frags[n].len;
  308. n = wsi->u.hdr.ah->frags[n].nfrag;
  309. } while (n);
  310. return len;
  311. }
  312. LWS_VISIBLE int lws_hdr_copy_fragment(struct lws *wsi, char *dst, int len,
  313. enum lws_token_indexes h, int frag_idx)
  314. {
  315. int n = 0;
  316. int f = wsi->u.hdr.ah->frag_index[h];
  317. if (!f)
  318. return -1;
  319. while (n < frag_idx) {
  320. f = wsi->u.hdr.ah->frags[f].nfrag;
  321. if (!f)
  322. return -1;
  323. n++;
  324. }
  325. if (wsi->u.hdr.ah->frags[f].len >= len)
  326. return -1;
  327. memcpy(dst, wsi->u.hdr.ah->data + wsi->u.hdr.ah->frags[f].offset,
  328. wsi->u.hdr.ah->frags[f].len);
  329. dst[wsi->u.hdr.ah->frags[f].len] = '\0';
  330. return wsi->u.hdr.ah->frags[f].len;
  331. }
  332. LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dst, int len,
  333. enum lws_token_indexes h)
  334. {
  335. int toklen = lws_hdr_total_length(wsi, h);
  336. int n;
  337. if (toklen >= len)
  338. return -1;
  339. n = wsi->u.hdr.ah->frag_index[h];
  340. if (!n)
  341. return 0;
  342. do {
  343. strcpy(dst, &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]);
  344. dst += wsi->u.hdr.ah->frags[n].len;
  345. n = wsi->u.hdr.ah->frags[n].nfrag;
  346. } while (n);
  347. return toklen;
  348. }
  349. char *lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h)
  350. {
  351. int n;
  352. n = wsi->u.hdr.ah->frag_index[h];
  353. if (!n)
  354. return NULL;
  355. return wsi->u.hdr.ah->data + wsi->u.hdr.ah->frags[n].offset;
  356. }
  357. int LWS_WARN_UNUSED_RESULT
  358. lws_pos_in_bounds(struct lws *wsi)
  359. {
  360. if (wsi->u.hdr.ah->pos < (unsigned int)wsi->context->max_http_header_data)
  361. return 0;
  362. if (wsi->u.hdr.ah->pos == wsi->context->max_http_header_data) {
  363. lwsl_err("Ran out of header data space\n");
  364. return 1;
  365. }
  366. /*
  367. * with these tests everywhere, it should never be able to exceed
  368. * the limit, only meet the limit
  369. */
  370. lwsl_err("%s: pos %d, limit %d\n", __func__, wsi->u.hdr.ah->pos,
  371. wsi->context->max_http_header_data);
  372. assert(0);
  373. return 1;
  374. }
  375. int LWS_WARN_UNUSED_RESULT
  376. lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s)
  377. {
  378. wsi->u.hdr.ah->nfrag++;
  379. if (wsi->u.hdr.ah->nfrag == ARRAY_SIZE(wsi->u.hdr.ah->frags)) {
  380. lwsl_warn("More hdr frags than we can deal with, dropping\n");
  381. return -1;
  382. }
  383. wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->nfrag;
  384. wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].offset = wsi->u.hdr.ah->pos;
  385. wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len = 0;
  386. wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].nfrag = 0;
  387. do {
  388. if (lws_pos_in_bounds(wsi))
  389. return -1;
  390. wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s;
  391. if (*s)
  392. wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++;
  393. } while (*s++);
  394. return 0;
  395. }
  396. signed char char_to_hex(const char c)
  397. {
  398. if (c >= '0' && c <= '9')
  399. return c - '0';
  400. if (c >= 'a' && c <= 'f')
  401. return c - 'a' + 10;
  402. if (c >= 'A' && c <= 'F')
  403. return c - 'A' + 10;
  404. return -1;
  405. }
  406. static int LWS_WARN_UNUSED_RESULT
  407. issue_char(struct lws *wsi, unsigned char c)
  408. {
  409. unsigned short frag_len;
  410. if (lws_pos_in_bounds(wsi))
  411. return -1;
  412. frag_len = wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len;
  413. /*
  414. * If we haven't hit the token limit, just copy the character into
  415. * the header
  416. */
  417. if (frag_len < wsi->u.hdr.current_token_limit) {
  418. wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c;
  419. if (c)
  420. wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++;
  421. return 0;
  422. }
  423. /* Insert a null character when we *hit* the limit: */
  424. if (frag_len == wsi->u.hdr.current_token_limit) {
  425. if (lws_pos_in_bounds(wsi))
  426. return -1;
  427. wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
  428. lwsl_warn("header %i exceeds limit %d\n",
  429. wsi->u.hdr.parser_state,
  430. wsi->u.hdr.current_token_limit);
  431. }
  432. return 1;
  433. }
  434. int LWS_WARN_UNUSED_RESULT
  435. lws_parse(struct lws *wsi, unsigned char c)
  436. {
  437. static const unsigned char methods[] = {
  438. WSI_TOKEN_GET_URI,
  439. WSI_TOKEN_POST_URI,
  440. WSI_TOKEN_OPTIONS_URI,
  441. WSI_TOKEN_PUT_URI,
  442. WSI_TOKEN_PATCH_URI,
  443. WSI_TOKEN_DELETE_URI,
  444. WSI_TOKEN_CONNECT,
  445. };
  446. struct allocated_headers *ah = wsi->u.hdr.ah;
  447. struct lws_context *context = wsi->context;
  448. unsigned int n, m, enc = 0;
  449. assert(wsi->u.hdr.ah);
  450. switch (wsi->u.hdr.parser_state) {
  451. default:
  452. lwsl_parser("WSI_TOK_(%d) '%c'\n", wsi->u.hdr.parser_state, c);
  453. /* collect into malloc'd buffers */
  454. /* optional initial space swallow */
  455. if (!ah->frags[ah->frag_index[wsi->u.hdr.parser_state]].len &&
  456. c == ' ')
  457. break;
  458. for (m = 0; m < ARRAY_SIZE(methods); m++)
  459. if (wsi->u.hdr.parser_state == methods[m])
  460. break;
  461. if (m == ARRAY_SIZE(methods))
  462. /* it was not any of the methods */
  463. goto check_eol;
  464. /* special URI processing... end at space */
  465. if (c == ' ') {
  466. /* enforce starting with / */
  467. if (!ah->frags[ah->nfrag].len)
  468. if (issue_char(wsi, '/') < 0)
  469. return -1;
  470. if (wsi->u.hdr.ups == URIPS_SEEN_SLASH_DOT_DOT) {
  471. /*
  472. * back up one dir level if possible
  473. * safe against header fragmentation because
  474. * the method URI can only be in 1 fragment
  475. */
  476. if (ah->frags[ah->nfrag].len > 2) {
  477. ah->pos--;
  478. ah->frags[ah->nfrag].len--;
  479. do {
  480. ah->pos--;
  481. ah->frags[ah->nfrag].len--;
  482. } while (ah->frags[ah->nfrag].len > 1 &&
  483. ah->data[ah->pos] != '/');
  484. }
  485. }
  486. /* begin parsing HTTP version: */
  487. if (issue_char(wsi, '\0') < 0)
  488. return -1;
  489. wsi->u.hdr.parser_state = WSI_TOKEN_HTTP;
  490. goto start_fragment;
  491. }
  492. /*
  493. * PRIORITY 1
  494. * special URI processing... convert %xx
  495. */
  496. switch (wsi->u.hdr.ues) {
  497. case URIES_IDLE:
  498. if (c == '%') {
  499. wsi->u.hdr.ues = URIES_SEEN_PERCENT;
  500. goto swallow;
  501. }
  502. break;
  503. case URIES_SEEN_PERCENT:
  504. if (char_to_hex(c) < 0)
  505. /* illegal post-% char */
  506. goto forbid;
  507. wsi->u.hdr.esc_stash = c;
  508. wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1;
  509. goto swallow;
  510. case URIES_SEEN_PERCENT_H1:
  511. if (char_to_hex(c) < 0)
  512. /* illegal post-% char */
  513. goto forbid;
  514. c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) |
  515. char_to_hex(c);
  516. enc = 1;
  517. wsi->u.hdr.ues = URIES_IDLE;
  518. break;
  519. }
  520. /*
  521. * PRIORITY 2
  522. * special URI processing...
  523. * convert /.. or /... or /../ etc to /
  524. * convert /./ to /
  525. * convert // or /// etc to /
  526. * leave /.dir or whatever alone
  527. */
  528. switch (wsi->u.hdr.ups) {
  529. case URIPS_IDLE:
  530. if (!c)
  531. return -1;
  532. /* genuine delimiter */
  533. if ((c == '&' || c == ';') && !enc) {
  534. if (issue_char(wsi, c) < 0)
  535. return -1;
  536. /* swallow the terminator */
  537. ah->frags[ah->nfrag].len--;
  538. /* link to next fragment */
  539. ah->frags[ah->nfrag].nfrag = ah->nfrag + 1;
  540. ah->nfrag++;
  541. if (ah->nfrag >= ARRAY_SIZE(ah->frags))
  542. goto excessive;
  543. /* start next fragment after the & */
  544. wsi->u.hdr.post_literal_equal = 0;
  545. ah->frags[ah->nfrag].offset = ah->pos;
  546. ah->frags[ah->nfrag].len = 0;
  547. ah->frags[ah->nfrag].nfrag = 0;
  548. goto swallow;
  549. }
  550. /* uriencoded = in the name part, disallow */
  551. if (c == '=' && enc &&
  552. ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] &&
  553. !wsi->u.hdr.post_literal_equal)
  554. c = '_';
  555. /* after the real =, we don't care how many = */
  556. if (c == '=' && !enc)
  557. wsi->u.hdr.post_literal_equal = 1;
  558. /* + to space */
  559. if (c == '+' && !enc)
  560. c = ' ';
  561. /* issue the first / always */
  562. if (c == '/' && !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS])
  563. wsi->u.hdr.ups = URIPS_SEEN_SLASH;
  564. break;
  565. case URIPS_SEEN_SLASH:
  566. /* swallow subsequent slashes */
  567. if (c == '/')
  568. goto swallow;
  569. /* track and swallow the first . after / */
  570. if (c == '.') {
  571. wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT;
  572. goto swallow;
  573. }
  574. wsi->u.hdr.ups = URIPS_IDLE;
  575. break;
  576. case URIPS_SEEN_SLASH_DOT:
  577. /* swallow second . */
  578. if (c == '.') {
  579. wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
  580. goto swallow;
  581. }
  582. /* change /./ to / */
  583. if (c == '/') {
  584. wsi->u.hdr.ups = URIPS_SEEN_SLASH;
  585. goto swallow;
  586. }
  587. /* it was like /.dir ... regurgitate the . */
  588. wsi->u.hdr.ups = URIPS_IDLE;
  589. if (issue_char(wsi, '.') < 0)
  590. return -1;
  591. break;
  592. case URIPS_SEEN_SLASH_DOT_DOT:
  593. /* /../ or /..[End of URI] --> backup to last / */
  594. if (c == '/' || c == '?') {
  595. /*
  596. * back up one dir level if possible
  597. * safe against header fragmentation because
  598. * the method URI can only be in 1 fragment
  599. */
  600. if (ah->frags[ah->nfrag].len > 2) {
  601. ah->pos--;
  602. ah->frags[ah->nfrag].len--;
  603. do {
  604. ah->pos--;
  605. ah->frags[ah->nfrag].len--;
  606. } while (ah->frags[ah->nfrag].len > 1 &&
  607. ah->data[ah->pos] != '/');
  608. }
  609. wsi->u.hdr.ups = URIPS_SEEN_SLASH;
  610. if (ah->frags[ah->nfrag].len > 1)
  611. break;
  612. goto swallow;
  613. }
  614. /* /..[^/] ... regurgitate and allow */
  615. if (issue_char(wsi, '.') < 0)
  616. return -1;
  617. if (issue_char(wsi, '.') < 0)
  618. return -1;
  619. wsi->u.hdr.ups = URIPS_IDLE;
  620. break;
  621. }
  622. if (c == '?' && !enc &&
  623. !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS]) { /* start of URI arguments */
  624. if (wsi->u.hdr.ues != URIES_IDLE)
  625. goto forbid;
  626. /* seal off uri header */
  627. if (issue_char(wsi, '\0') < 0)
  628. return -1;
  629. /* move to using WSI_TOKEN_HTTP_URI_ARGS */
  630. ah->nfrag++;
  631. if (ah->nfrag >= ARRAY_SIZE(ah->frags))
  632. goto excessive;
  633. ah->frags[ah->nfrag].offset = ah->pos;
  634. ah->frags[ah->nfrag].len = 0;
  635. ah->frags[ah->nfrag].nfrag = 0;
  636. wsi->u.hdr.post_literal_equal = 0;
  637. ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] = ah->nfrag;
  638. wsi->u.hdr.ups = URIPS_IDLE;
  639. goto swallow;
  640. }
  641. check_eol:
  642. /* bail at EOL */
  643. if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE &&
  644. c == '\x0d') {
  645. if (wsi->u.hdr.ues != URIES_IDLE)
  646. goto forbid;
  647. c = '\0';
  648. wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
  649. lwsl_parser("*\n");
  650. }
  651. n = issue_char(wsi, c);
  652. if ((int)n < 0)
  653. return -1;
  654. if (n > 0)
  655. wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
  656. swallow:
  657. /* per-protocol end of headers management */
  658. if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
  659. goto set_parsing_complete;
  660. break;
  661. /* collecting and checking a name part */
  662. case WSI_TOKEN_NAME_PART:
  663. lwsl_parser("WSI_TOKEN_NAME_PART '%c' (mode=%d)\n", c, wsi->mode);
  664. wsi->u.hdr.lextable_pos =
  665. lextable_decode(wsi->u.hdr.lextable_pos, c);
  666. /*
  667. * Server needs to look out for unknown methods...
  668. */
  669. if (wsi->u.hdr.lextable_pos < 0 &&
  670. wsi->mode == LWSCM_HTTP_SERVING) {
  671. /* this is not a header we know about */
  672. for (m = 0; m < ARRAY_SIZE(methods); m++)
  673. if (ah->frag_index[methods[m]]) {
  674. /*
  675. * already had the method, no idea what
  676. * this crap from the client is, ignore
  677. */
  678. wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
  679. break;
  680. }
  681. /*
  682. * hm it's an unknown http method from a client in fact,
  683. * it cannot be valid http
  684. */
  685. if (m == ARRAY_SIZE(methods)) {
  686. /*
  687. * are we set up to accept raw in these cases?
  688. */
  689. if (lws_check_opt(wsi->vhost->options,
  690. LWS_SERVER_OPTION_FALLBACK_TO_RAW))
  691. return 2; /* transition to raw */
  692. lwsl_info("Unknown method - dropping\n");
  693. goto forbid;
  694. }
  695. break;
  696. }
  697. /*
  698. * ...otherwise for a client, let him ignore unknown headers
  699. * coming from the server
  700. */
  701. if (wsi->u.hdr.lextable_pos < 0) {
  702. wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
  703. break;
  704. }
  705. if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) {
  706. /* terminal state */
  707. n = ((unsigned int)lextable[wsi->u.hdr.lextable_pos] << 8) |
  708. lextable[wsi->u.hdr.lextable_pos + 1];
  709. lwsl_parser("known hdr %d\n", n);
  710. for (m = 0; m < ARRAY_SIZE(methods); m++)
  711. if (n == methods[m] &&
  712. ah->frag_index[methods[m]]) {
  713. lwsl_warn("Duplicated method\n");
  714. return -1;
  715. }
  716. /*
  717. * WSORIGIN is protocol equiv to ORIGIN,
  718. * JWebSocket likes to send it, map to ORIGIN
  719. */
  720. if (n == WSI_TOKEN_SWORIGIN)
  721. n = WSI_TOKEN_ORIGIN;
  722. wsi->u.hdr.parser_state = (enum lws_token_indexes)
  723. (WSI_TOKEN_GET_URI + n);
  724. if (context->token_limits)
  725. wsi->u.hdr.current_token_limit =
  726. context->token_limits->token_limit[
  727. wsi->u.hdr.parser_state];
  728. else
  729. wsi->u.hdr.current_token_limit =
  730. wsi->context->max_http_header_data;
  731. if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
  732. goto set_parsing_complete;
  733. goto start_fragment;
  734. }
  735. break;
  736. start_fragment:
  737. ah->nfrag++;
  738. excessive:
  739. if (ah->nfrag == ARRAY_SIZE(ah->frags)) {
  740. lwsl_warn("More hdr frags than we can deal with\n");
  741. return -1;
  742. }
  743. ah->frags[ah->nfrag].offset = ah->pos;
  744. ah->frags[ah->nfrag].len = 0;
  745. ah->frags[ah->nfrag].nfrag = 0;
  746. n = ah->frag_index[wsi->u.hdr.parser_state];
  747. if (!n) { /* first fragment */
  748. ah->frag_index[wsi->u.hdr.parser_state] = ah->nfrag;
  749. break;
  750. }
  751. /* continuation */
  752. while (ah->frags[n].nfrag)
  753. n = ah->frags[n].nfrag;
  754. ah->frags[n].nfrag = ah->nfrag;
  755. if (issue_char(wsi, ' ') < 0)
  756. return -1;
  757. break;
  758. /* skipping arg part of a name we didn't recognize */
  759. case WSI_TOKEN_SKIPPING:
  760. lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
  761. if (c == '\x0d')
  762. wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
  763. break;
  764. case WSI_TOKEN_SKIPPING_SAW_CR:
  765. lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
  766. if (wsi->u.hdr.ues != URIES_IDLE)
  767. goto forbid;
  768. if (c == '\x0a') {
  769. wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
  770. wsi->u.hdr.lextable_pos = 0;
  771. } else
  772. wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
  773. break;
  774. /* we're done, ignore anything else */
  775. case WSI_PARSING_COMPLETE:
  776. lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
  777. break;
  778. }
  779. return 0;
  780. set_parsing_complete:
  781. if (wsi->u.hdr.ues != URIES_IDLE)
  782. goto forbid;
  783. if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
  784. if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
  785. wsi->ietf_spec_revision =
  786. atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
  787. lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision);
  788. }
  789. wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
  790. wsi->hdr_parsing_completed = 1;
  791. return 0;
  792. forbid:
  793. lwsl_notice(" forbidding on uri sanitation\n");
  794. lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
  795. return -1;
  796. }
  797. LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
  798. {
  799. return wsi->u.ws.frame_is_binary;
  800. }
  801. void
  802. lws_add_wsi_to_draining_ext_list(struct lws *wsi)
  803. {
  804. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  805. if (wsi->u.ws.rx_draining_ext)
  806. return;
  807. lwsl_ext("%s: RX EXT DRAINING: Adding to list\n", __func__);
  808. wsi->u.ws.rx_draining_ext = 1;
  809. wsi->u.ws.rx_draining_ext_list = pt->rx_draining_ext_list;
  810. pt->rx_draining_ext_list = wsi;
  811. }
  812. void
  813. lws_remove_wsi_from_draining_ext_list(struct lws *wsi)
  814. {
  815. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  816. struct lws **w = &pt->rx_draining_ext_list;
  817. if (!wsi->u.ws.rx_draining_ext)
  818. return;
  819. lwsl_ext("%s: RX EXT DRAINING: Removing from list\n", __func__);
  820. wsi->u.ws.rx_draining_ext = 0;
  821. /* remove us from context draining ext list */
  822. while (*w) {
  823. if (*w == wsi) {
  824. /* if us, point it instead to who we were pointing to */
  825. *w = wsi->u.ws.rx_draining_ext_list;
  826. break;
  827. }
  828. w = &((*w)->u.ws.rx_draining_ext_list);
  829. }
  830. wsi->u.ws.rx_draining_ext_list = NULL;
  831. }
  832. /*
  833. * client-parser.c: lws_client_rx_sm() needs to be roughly kept in
  834. * sync with changes here, esp related to ext draining
  835. */
  836. int
  837. lws_rx_sm(struct lws *wsi, unsigned char c)
  838. {
  839. int callback_action = LWS_CALLBACK_RECEIVE;
  840. int ret = 0, n, rx_draining_ext = 0;
  841. struct lws_tokens eff_buf;
  842. eff_buf.token = NULL;
  843. eff_buf.token_len = 0;
  844. if (wsi->socket_is_permanently_unusable)
  845. return -1;
  846. switch (wsi->lws_rx_parse_state) {
  847. case LWS_RXPS_NEW:
  848. if (wsi->u.ws.rx_draining_ext) {
  849. eff_buf.token = NULL;
  850. eff_buf.token_len = 0;
  851. lws_remove_wsi_from_draining_ext_list(wsi);
  852. rx_draining_ext = 1;
  853. lwsl_debug("%s: doing draining flow\n", __func__);
  854. goto drain_extension;
  855. }
  856. switch (wsi->ietf_spec_revision) {
  857. case 13:
  858. /*
  859. * no prepended frame key any more
  860. */
  861. wsi->u.ws.all_zero_nonce = 1;
  862. goto handle_first;
  863. default:
  864. lwsl_warn("lws_rx_sm: unknown spec version %d\n",
  865. wsi->ietf_spec_revision);
  866. break;
  867. }
  868. break;
  869. case LWS_RXPS_04_mask_1:
  870. wsi->u.ws.mask[1] = c;
  871. if (c)
  872. wsi->u.ws.all_zero_nonce = 0;
  873. wsi->lws_rx_parse_state = LWS_RXPS_04_mask_2;
  874. break;
  875. case LWS_RXPS_04_mask_2:
  876. wsi->u.ws.mask[2] = c;
  877. if (c)
  878. wsi->u.ws.all_zero_nonce = 0;
  879. wsi->lws_rx_parse_state = LWS_RXPS_04_mask_3;
  880. break;
  881. case LWS_RXPS_04_mask_3:
  882. wsi->u.ws.mask[3] = c;
  883. if (c)
  884. wsi->u.ws.all_zero_nonce = 0;
  885. /*
  886. * start from the zero'th byte in the XOR key buffer since
  887. * this is the start of a frame with a new key
  888. */
  889. wsi->u.ws.mask_idx = 0;
  890. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
  891. break;
  892. /*
  893. * 04 logical framing from the spec (all this is masked when incoming
  894. * and has to be unmasked)
  895. *
  896. * We ignore the possibility of extension data because we don't
  897. * negotiate any extensions at the moment.
  898. *
  899. * 0 1 2 3
  900. * 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
  901. * +-+-+-+-+-------+-+-------------+-------------------------------+
  902. * |F|R|R|R| opcode|R| Payload len | Extended payload length |
  903. * |I|S|S|S| (4) |S| (7) | (16/63) |
  904. * |N|V|V|V| |V| | (if payload len==126/127) |
  905. * | |1|2|3| |4| | |
  906. * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
  907. * | Extended payload length continued, if payload len == 127 |
  908. * + - - - - - - - - - - - - - - - +-------------------------------+
  909. * | | Extension data |
  910. * +-------------------------------+ - - - - - - - - - - - - - - - +
  911. * : :
  912. * +---------------------------------------------------------------+
  913. * : Application data :
  914. * +---------------------------------------------------------------+
  915. *
  916. * We pass payload through to userland as soon as we get it, ignoring
  917. * FIN. It's up to userland to buffer it up if it wants to see a
  918. * whole unfragmented block of the original size (which may be up to
  919. * 2^63 long!)
  920. */
  921. case LWS_RXPS_04_FRAME_HDR_1:
  922. handle_first:
  923. wsi->u.ws.opcode = c & 0xf;
  924. wsi->u.ws.rsv = c & 0x70;
  925. wsi->u.ws.final = !!((c >> 7) & 1);
  926. switch (wsi->u.ws.opcode) {
  927. case LWSWSOPC_TEXT_FRAME:
  928. case LWSWSOPC_BINARY_FRAME:
  929. wsi->u.ws.rsv_first_msg = (c & 0x70);
  930. wsi->u.ws.frame_is_binary =
  931. wsi->u.ws.opcode == LWSWSOPC_BINARY_FRAME;
  932. break;
  933. case 3:
  934. case 4:
  935. case 5:
  936. case 6:
  937. case 7:
  938. case 0xb:
  939. case 0xc:
  940. case 0xd:
  941. case 0xe:
  942. case 0xf:
  943. lwsl_info("illegal opcode\n");
  944. return -1;
  945. }
  946. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
  947. break;
  948. case LWS_RXPS_04_FRAME_HDR_LEN:
  949. wsi->u.ws.this_frame_masked = !!(c & 0x80);
  950. switch (c & 0x7f) {
  951. case 126:
  952. /* control frames are not allowed to have big lengths */
  953. if (wsi->u.ws.opcode & 8)
  954. goto illegal_ctl_length;
  955. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
  956. break;
  957. case 127:
  958. /* control frames are not allowed to have big lengths */
  959. if (wsi->u.ws.opcode & 8)
  960. goto illegal_ctl_length;
  961. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
  962. break;
  963. default:
  964. wsi->u.ws.rx_packet_length = c & 0x7f;
  965. if (wsi->u.ws.this_frame_masked)
  966. wsi->lws_rx_parse_state =
  967. LWS_RXPS_07_COLLECT_FRAME_KEY_1;
  968. else
  969. if (wsi->u.ws.rx_packet_length)
  970. wsi->lws_rx_parse_state =
  971. LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
  972. else {
  973. wsi->lws_rx_parse_state = LWS_RXPS_NEW;
  974. goto spill;
  975. }
  976. break;
  977. }
  978. break;
  979. case LWS_RXPS_04_FRAME_HDR_LEN16_2:
  980. wsi->u.ws.rx_packet_length = c << 8;
  981. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
  982. break;
  983. case LWS_RXPS_04_FRAME_HDR_LEN16_1:
  984. wsi->u.ws.rx_packet_length |= c;
  985. if (wsi->u.ws.this_frame_masked)
  986. wsi->lws_rx_parse_state =
  987. LWS_RXPS_07_COLLECT_FRAME_KEY_1;
  988. else
  989. wsi->lws_rx_parse_state =
  990. LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
  991. break;
  992. case LWS_RXPS_04_FRAME_HDR_LEN64_8:
  993. if (c & 0x80) {
  994. lwsl_warn("b63 of length must be zero\n");
  995. /* kill the connection */
  996. return -1;
  997. }
  998. #if defined __LP64__
  999. wsi->u.ws.rx_packet_length = ((size_t)c) << 56;
  1000. #else
  1001. wsi->u.ws.rx_packet_length = 0;
  1002. #endif
  1003. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
  1004. break;
  1005. case LWS_RXPS_04_FRAME_HDR_LEN64_7:
  1006. #if defined __LP64__
  1007. wsi->u.ws.rx_packet_length |= ((size_t)c) << 48;
  1008. #endif
  1009. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
  1010. break;
  1011. case LWS_RXPS_04_FRAME_HDR_LEN64_6:
  1012. #if defined __LP64__
  1013. wsi->u.ws.rx_packet_length |= ((size_t)c) << 40;
  1014. #endif
  1015. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
  1016. break;
  1017. case LWS_RXPS_04_FRAME_HDR_LEN64_5:
  1018. #if defined __LP64__
  1019. wsi->u.ws.rx_packet_length |= ((size_t)c) << 32;
  1020. #endif
  1021. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
  1022. break;
  1023. case LWS_RXPS_04_FRAME_HDR_LEN64_4:
  1024. wsi->u.ws.rx_packet_length |= ((size_t)c) << 24;
  1025. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
  1026. break;
  1027. case LWS_RXPS_04_FRAME_HDR_LEN64_3:
  1028. wsi->u.ws.rx_packet_length |= ((size_t)c) << 16;
  1029. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
  1030. break;
  1031. case LWS_RXPS_04_FRAME_HDR_LEN64_2:
  1032. wsi->u.ws.rx_packet_length |= ((size_t)c) << 8;
  1033. wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
  1034. break;
  1035. case LWS_RXPS_04_FRAME_HDR_LEN64_1:
  1036. wsi->u.ws.rx_packet_length |= ((size_t)c);
  1037. if (wsi->u.ws.this_frame_masked)
  1038. wsi->lws_rx_parse_state =
  1039. LWS_RXPS_07_COLLECT_FRAME_KEY_1;
  1040. else
  1041. wsi->lws_rx_parse_state =
  1042. LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
  1043. break;
  1044. case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
  1045. wsi->u.ws.mask[0] = c;
  1046. if (c)
  1047. wsi->u.ws.all_zero_nonce = 0;
  1048. wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
  1049. break;
  1050. case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
  1051. wsi->u.ws.mask[1] = c;
  1052. if (c)
  1053. wsi->u.ws.all_zero_nonce = 0;
  1054. wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
  1055. break;
  1056. case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
  1057. wsi->u.ws.mask[2] = c;
  1058. if (c)
  1059. wsi->u.ws.all_zero_nonce = 0;
  1060. wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
  1061. break;
  1062. case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
  1063. wsi->u.ws.mask[3] = c;
  1064. if (c)
  1065. wsi->u.ws.all_zero_nonce = 0;
  1066. wsi->lws_rx_parse_state =
  1067. LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
  1068. wsi->u.ws.mask_idx = 0;
  1069. if (wsi->u.ws.rx_packet_length == 0) {
  1070. wsi->lws_rx_parse_state = LWS_RXPS_NEW;
  1071. goto spill;
  1072. }
  1073. break;
  1074. case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
  1075. assert(wsi->u.ws.rx_ubuf);
  1076. if (wsi->u.ws.rx_draining_ext)
  1077. goto drain_extension;
  1078. if (wsi->u.ws.rx_ubuf_head + LWS_PRE >=
  1079. wsi->u.ws.rx_ubuf_alloc) {
  1080. lwsl_err("Attempted overflow \n");
  1081. return -1;
  1082. }
  1083. if (wsi->u.ws.all_zero_nonce)
  1084. wsi->u.ws.rx_ubuf[LWS_PRE +
  1085. (wsi->u.ws.rx_ubuf_head++)] = c;
  1086. else
  1087. wsi->u.ws.rx_ubuf[LWS_PRE +
  1088. (wsi->u.ws.rx_ubuf_head++)] =
  1089. c ^ wsi->u.ws.mask[
  1090. (wsi->u.ws.mask_idx++) & 3];
  1091. if (--wsi->u.ws.rx_packet_length == 0) {
  1092. /* spill because we have the whole frame */
  1093. wsi->lws_rx_parse_state = LWS_RXPS_NEW;
  1094. goto spill;
  1095. }
  1096. /*
  1097. * if there's no protocol max frame size given, we are
  1098. * supposed to default to context->pt_serv_buf_size
  1099. */
  1100. if (!wsi->protocol->rx_buffer_size &&
  1101. wsi->u.ws.rx_ubuf_head != wsi->context->pt_serv_buf_size)
  1102. break;
  1103. else
  1104. if (wsi->protocol->rx_buffer_size &&
  1105. wsi->u.ws.rx_ubuf_head !=
  1106. wsi->protocol->rx_buffer_size)
  1107. break;
  1108. /* spill because we filled our rx buffer */
  1109. spill:
  1110. /*
  1111. * is this frame a control packet we should take care of at this
  1112. * layer? If so service it and hide it from the user callback
  1113. */
  1114. lwsl_parser("spill on %s\n", wsi->protocol->name);
  1115. switch (wsi->u.ws.opcode) {
  1116. case LWSWSOPC_CLOSE:
  1117. /* is this an acknowledgement of our close? */
  1118. if (wsi->state == LWSS_AWAITING_CLOSE_ACK) {
  1119. /*
  1120. * fine he has told us he is closing too, let's
  1121. * finish our close
  1122. */
  1123. lwsl_parser("seen client close ack\n");
  1124. return -1;
  1125. }
  1126. if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
  1127. /* if he sends us 2 CLOSE, kill him */
  1128. return -1;
  1129. if (lws_partial_buffered(wsi)) {
  1130. /*
  1131. * if we're in the middle of something,
  1132. * we can't do a normal close response and
  1133. * have to just close our end.
  1134. */
  1135. wsi->socket_is_permanently_unusable = 1;
  1136. lwsl_parser("Closing on peer close due to Pending tx\n");
  1137. return -1;
  1138. }
  1139. if (user_callback_handle_rxflow(
  1140. wsi->protocol->callback, wsi,
  1141. LWS_CALLBACK_WS_PEER_INITIATED_CLOSE,
  1142. wsi->user_space,
  1143. &wsi->u.ws.rx_ubuf[LWS_PRE],
  1144. wsi->u.ws.rx_ubuf_head))
  1145. return -1;
  1146. lwsl_parser("server sees client close packet\n");
  1147. wsi->state = LWSS_RETURNED_CLOSE_ALREADY;
  1148. /* deal with the close packet contents as a PONG */
  1149. wsi->u.ws.payload_is_close = 1;
  1150. goto process_as_ping;
  1151. case LWSWSOPC_PING:
  1152. lwsl_info("received %d byte ping, sending pong\n",
  1153. wsi->u.ws.rx_ubuf_head);
  1154. if (wsi->u.ws.ping_pending_flag) {
  1155. /*
  1156. * there is already a pending ping payload
  1157. * we should just log and drop
  1158. */
  1159. lwsl_parser("DROP PING since one pending\n");
  1160. goto ping_drop;
  1161. }
  1162. process_as_ping:
  1163. /* control packets can only be < 128 bytes long */
  1164. if (wsi->u.ws.rx_ubuf_head > 128 - 3) {
  1165. lwsl_parser("DROP PING payload too large\n");
  1166. goto ping_drop;
  1167. }
  1168. /* stash the pong payload */
  1169. memcpy(wsi->u.ws.ping_payload_buf + LWS_PRE,
  1170. &wsi->u.ws.rx_ubuf[LWS_PRE],
  1171. wsi->u.ws.rx_ubuf_head);
  1172. wsi->u.ws.ping_payload_len = wsi->u.ws.rx_ubuf_head;
  1173. wsi->u.ws.ping_pending_flag = 1;
  1174. /* get it sent as soon as possible */
  1175. lws_callback_on_writable(wsi);
  1176. ping_drop:
  1177. wsi->u.ws.rx_ubuf_head = 0;
  1178. return 0;
  1179. case LWSWSOPC_PONG:
  1180. lwsl_info("received pong\n");
  1181. lwsl_hexdump(&wsi->u.ws.rx_ubuf[LWS_PRE],
  1182. wsi->u.ws.rx_ubuf_head);
  1183. if (wsi->pending_timeout == PENDING_TIMEOUT_WS_PONG_CHECK_GET_PONG) {
  1184. lwsl_info("received expected PONG on wsi %p\n", wsi);
  1185. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  1186. }
  1187. /* issue it */
  1188. callback_action = LWS_CALLBACK_RECEIVE_PONG;
  1189. break;
  1190. case LWSWSOPC_TEXT_FRAME:
  1191. case LWSWSOPC_BINARY_FRAME:
  1192. case LWSWSOPC_CONTINUATION:
  1193. break;
  1194. default:
  1195. lwsl_parser("passing opc %x up to exts\n",
  1196. wsi->u.ws.opcode);
  1197. /*
  1198. * It's something special we can't understand here.
  1199. * Pass the payload up to the extension's parsing
  1200. * state machine.
  1201. */
  1202. eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
  1203. eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
  1204. if (lws_ext_cb_active(wsi, LWS_EXT_CB_EXTENDED_PAYLOAD_RX,
  1205. &eff_buf, 0) <= 0)
  1206. /* not handle or fail */
  1207. lwsl_ext("ext opc opcode 0x%x unknown\n",
  1208. wsi->u.ws.opcode);
  1209. wsi->u.ws.rx_ubuf_head = 0;
  1210. return 0;
  1211. }
  1212. /*
  1213. * No it's real payload, pass it up to the user callback.
  1214. * It's nicely buffered with the pre-padding taken care of
  1215. * so it can be sent straight out again using lws_write
  1216. */
  1217. eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
  1218. eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
  1219. drain_extension:
  1220. lwsl_ext("%s: passing %d to ext\n", __func__, eff_buf.token_len);
  1221. if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
  1222. wsi->state == LWSS_AWAITING_CLOSE_ACK)
  1223. goto already_done;
  1224. n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_RX, &eff_buf, 0);
  1225. /* eff_buf may be pointing somewhere completely different now,
  1226. * it's the output
  1227. */
  1228. if (n < 0) {
  1229. /*
  1230. * we may rely on this to get RX, just drop connection
  1231. */
  1232. wsi->socket_is_permanently_unusable = 1;
  1233. return -1;
  1234. }
  1235. if (rx_draining_ext && eff_buf.token_len == 0)
  1236. goto already_done;
  1237. if (n && eff_buf.token_len) {
  1238. /* extension had more... main loop will come back */
  1239. lws_add_wsi_to_draining_ext_list(wsi);
  1240. } else
  1241. lws_remove_wsi_from_draining_ext_list(wsi);
  1242. if (eff_buf.token_len > 0 ||
  1243. callback_action == LWS_CALLBACK_RECEIVE_PONG) {
  1244. eff_buf.token[eff_buf.token_len] = '\0';
  1245. if (wsi->protocol->callback) {
  1246. if (callback_action == LWS_CALLBACK_RECEIVE_PONG)
  1247. lwsl_info("Doing pong callback\n");
  1248. ret = user_callback_handle_rxflow(
  1249. wsi->protocol->callback,
  1250. wsi,
  1251. (enum lws_callback_reasons)callback_action,
  1252. wsi->user_space,
  1253. eff_buf.token,
  1254. eff_buf.token_len);
  1255. }
  1256. else
  1257. lwsl_err("No callback on payload spill!\n");
  1258. }
  1259. already_done:
  1260. wsi->u.ws.rx_ubuf_head = 0;
  1261. break;
  1262. }
  1263. return ret;
  1264. illegal_ctl_length:
  1265. lwsl_warn("Control frame with xtended length is illegal\n");
  1266. /* kill the connection */
  1267. return -1;
  1268. }
  1269. LWS_VISIBLE size_t
  1270. lws_remaining_packet_payload(struct lws *wsi)
  1271. {
  1272. return wsi->u.ws.rx_packet_length;
  1273. }
  1274. /* Once we reach LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED, we know how much
  1275. * to expect in that state and can deal with it in bulk more efficiently.
  1276. */
  1277. int
  1278. lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf,
  1279. size_t *len)
  1280. {
  1281. unsigned char *buffer = *buf, mask[4];
  1282. int buffer_size, n;
  1283. unsigned int avail;
  1284. char *rx_ubuf;
  1285. if (wsi->protocol->rx_buffer_size)
  1286. buffer_size = wsi->protocol->rx_buffer_size;
  1287. else
  1288. buffer_size = wsi->context->pt_serv_buf_size;
  1289. avail = buffer_size - wsi->u.ws.rx_ubuf_head;
  1290. /* do not consume more than we should */
  1291. if (avail > wsi->u.ws.rx_packet_length)
  1292. avail = wsi->u.ws.rx_packet_length;
  1293. /* do not consume more than what is in the buffer */
  1294. if (avail > *len)
  1295. avail = *len;
  1296. /* we want to leave 1 byte for the parser to handle properly */
  1297. if (avail <= 1)
  1298. return 0;
  1299. avail--;
  1300. rx_ubuf = wsi->u.ws.rx_ubuf + LWS_PRE + wsi->u.ws.rx_ubuf_head;
  1301. if (wsi->u.ws.all_zero_nonce)
  1302. memcpy(rx_ubuf, buffer, avail);
  1303. else {
  1304. for (n = 0; n < 4; n++)
  1305. mask[n] = wsi->u.ws.mask[(wsi->u.ws.mask_idx + n) & 3];
  1306. /* deal with 4-byte chunks using unwrapped loop */
  1307. n = avail >> 2;
  1308. while (n--) {
  1309. *(rx_ubuf++) = *(buffer++) ^ mask[0];
  1310. *(rx_ubuf++) = *(buffer++) ^ mask[1];
  1311. *(rx_ubuf++) = *(buffer++) ^ mask[2];
  1312. *(rx_ubuf++) = *(buffer++) ^ mask[3];
  1313. }
  1314. /* and the remaining bytes bytewise */
  1315. for (n = 0; n < (int)(avail & 3); n++)
  1316. *(rx_ubuf++) = *(buffer++) ^ mask[n];
  1317. wsi->u.ws.mask_idx = (wsi->u.ws.mask_idx + avail) & 3;
  1318. }
  1319. (*buf) += avail;
  1320. wsi->u.ws.rx_ubuf_head += avail;
  1321. wsi->u.ws.rx_packet_length -= avail;
  1322. *len -= avail;
  1323. return avail;
  1324. }