http2.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. /*
  26. * bitmap of control messages that are valid to receive for each http2 state
  27. */
  28. static const uint16_t http2_rx_validity[] = {
  29. /* LWS_H2S_IDLE */
  30. (1 << LWS_H2_FRAME_TYPE_SETTINGS) |
  31. (1 << LWS_H2_FRAME_TYPE_PRIORITY) |
  32. // (1 << LWS_H2_FRAME_TYPE_WINDOW_UPDATE)| /* ignore */
  33. (1 << LWS_H2_FRAME_TYPE_HEADERS) |
  34. (1 << LWS_H2_FRAME_TYPE_CONTINUATION),
  35. /* LWS_H2S_RESERVED_LOCAL */
  36. (1 << LWS_H2_FRAME_TYPE_SETTINGS) |
  37. (1 << LWS_H2_FRAME_TYPE_RST_STREAM) |
  38. (1 << LWS_H2_FRAME_TYPE_PRIORITY) |
  39. (1 << LWS_H2_FRAME_TYPE_WINDOW_UPDATE),
  40. /* LWS_H2S_RESERVED_REMOTE */
  41. (1 << LWS_H2_FRAME_TYPE_SETTINGS) |
  42. (1 << LWS_H2_FRAME_TYPE_HEADERS) |
  43. (1 << LWS_H2_FRAME_TYPE_CONTINUATION) |
  44. (1 << LWS_H2_FRAME_TYPE_RST_STREAM) |
  45. (1 << LWS_H2_FRAME_TYPE_PRIORITY),
  46. /* LWS_H2S_OPEN */
  47. (1 << LWS_H2_FRAME_TYPE_DATA) |
  48. (1 << LWS_H2_FRAME_TYPE_HEADERS) |
  49. (1 << LWS_H2_FRAME_TYPE_PRIORITY) |
  50. (1 << LWS_H2_FRAME_TYPE_RST_STREAM) |
  51. (1 << LWS_H2_FRAME_TYPE_SETTINGS) |
  52. (1 << LWS_H2_FRAME_TYPE_PUSH_PROMISE) |
  53. (1 << LWS_H2_FRAME_TYPE_PING) |
  54. (1 << LWS_H2_FRAME_TYPE_GOAWAY) |
  55. (1 << LWS_H2_FRAME_TYPE_WINDOW_UPDATE) |
  56. (1 << LWS_H2_FRAME_TYPE_CONTINUATION),
  57. /* LWS_H2S_HALF_CLOSED_REMOTE */
  58. (1 << LWS_H2_FRAME_TYPE_SETTINGS) |
  59. (1 << LWS_H2_FRAME_TYPE_WINDOW_UPDATE) |
  60. (1 << LWS_H2_FRAME_TYPE_PRIORITY) |
  61. (1 << LWS_H2_FRAME_TYPE_RST_STREAM),
  62. /* LWS_H2S_HALF_CLOSED_LOCAL */
  63. (1 << LWS_H2_FRAME_TYPE_DATA) |
  64. (1 << LWS_H2_FRAME_TYPE_HEADERS) |
  65. (1 << LWS_H2_FRAME_TYPE_PRIORITY) |
  66. (1 << LWS_H2_FRAME_TYPE_RST_STREAM) |
  67. (1 << LWS_H2_FRAME_TYPE_SETTINGS) |
  68. (1 << LWS_H2_FRAME_TYPE_PUSH_PROMISE) |
  69. (1 << LWS_H2_FRAME_TYPE_PING) |
  70. (1 << LWS_H2_FRAME_TYPE_GOAWAY) |
  71. (1 << LWS_H2_FRAME_TYPE_WINDOW_UPDATE) |
  72. (1 << LWS_H2_FRAME_TYPE_CONTINUATION),
  73. /* LWS_H2S_CLOSED */
  74. (1 << LWS_H2_FRAME_TYPE_SETTINGS) |
  75. (1 << LWS_H2_FRAME_TYPE_PRIORITY) |
  76. (1 << LWS_H2_FRAME_TYPE_WINDOW_UPDATE) |
  77. (1 << LWS_H2_FRAME_TYPE_RST_STREAM),
  78. };
  79. static const char *preface = "PRI * HTTP/2.0\x0d\x0a\x0d\x0aSM\x0d\x0a\x0d\x0a";
  80. static const char * const h2_state_names[] = {
  81. "LWS_H2S_IDLE",
  82. "LWS_H2S_RESERVED_LOCAL",
  83. "LWS_H2S_RESERVED_REMOTE",
  84. "LWS_H2S_OPEN",
  85. "LWS_H2S_HALF_CLOSED_REMOTE",
  86. "LWS_H2S_HALF_CLOSED_LOCAL",
  87. "LWS_H2S_CLOSED",
  88. };
  89. #if 0
  90. static const char * const h2_setting_names[] = {
  91. "",
  92. "H2SET_HEADER_TABLE_SIZE",
  93. "H2SET_ENABLE_PUSH",
  94. "H2SET_MAX_CONCURRENT_STREAMS",
  95. "H2SET_INITIAL_WINDOW_SIZE",
  96. "H2SET_MAX_FRAME_SIZE",
  97. "H2SET_MAX_HEADER_LIST_SIZE",
  98. "reserved",
  99. "H2SET_ENABLE_CONNECT_PROTOCOL"
  100. };
  101. void
  102. lws_h2_dump_settings(struct http2_settings *set)
  103. {
  104. int n;
  105. for (n = 1; n < H2SET_COUNT; n++)
  106. lwsl_notice(" %30s: %10d\n", h2_setting_names[n], set->s[n]);
  107. }
  108. #else
  109. void
  110. lws_h2_dump_settings(struct http2_settings *set)
  111. {
  112. }
  113. #endif
  114. struct lws_h2_protocol_send *
  115. lws_h2_new_pps(enum lws_h2_protocol_send_type type)
  116. {
  117. struct lws_h2_protocol_send *pps = lws_malloc(sizeof(*pps), "pps");
  118. if (pps)
  119. pps->type = type;
  120. return pps;
  121. }
  122. void lws_h2_init(struct lws *wsi)
  123. {
  124. wsi->h2.h2n->our_set = wsi->a.vhost->h2.set;
  125. wsi->h2.h2n->peer_set = lws_h2_defaults;
  126. }
  127. void
  128. lws_h2_state(struct lws *wsi, enum lws_h2_states s)
  129. {
  130. if (!wsi)
  131. return;
  132. lwsl_info("%s: wsi %p: state %s -> %s\n", __func__, wsi,
  133. h2_state_names[wsi->h2.h2_state],
  134. h2_state_names[s]);
  135. (void)h2_state_names;
  136. wsi->h2.h2_state = (uint8_t)s;
  137. }
  138. int
  139. lws_h2_update_peer_txcredit(struct lws *wsi, int sid, int bump)
  140. {
  141. struct lws *nwsi = lws_get_network_wsi(wsi);
  142. struct lws_h2_protocol_send *pps;
  143. assert(wsi);
  144. if (!bump)
  145. return 0;
  146. if (sid == -1)
  147. sid = wsi->mux.my_sid;
  148. lwsl_info("%s: sid %d: bump %d -> %d\n", __func__, sid, bump,
  149. (int)wsi->txc.peer_tx_cr_est + bump);
  150. pps = lws_h2_new_pps(LWS_H2_PPS_UPDATE_WINDOW);
  151. if (!pps)
  152. return 1;
  153. pps->u.update_window.sid = sid;
  154. pps->u.update_window.credit = bump;
  155. wsi->txc.peer_tx_cr_est += bump;
  156. lws_wsi_txc_describe(&wsi->txc, __func__, wsi->mux.my_sid);
  157. lws_pps_schedule(wsi, pps);
  158. pps = lws_h2_new_pps(LWS_H2_PPS_UPDATE_WINDOW);
  159. if (!pps)
  160. return 1;
  161. pps->u.update_window.sid = 0;
  162. pps->u.update_window.credit = bump;
  163. nwsi->txc.peer_tx_cr_est += bump;
  164. lws_wsi_txc_describe(&nwsi->txc, __func__, nwsi->mux.my_sid);
  165. lws_pps_schedule(nwsi, pps);
  166. return 0;
  167. }
  168. int
  169. lws_h2_get_peer_txcredit_estimate(struct lws *wsi)
  170. {
  171. lws_wsi_txc_describe(&wsi->txc, __func__, wsi->mux.my_sid);
  172. return (int)wsi->txc.peer_tx_cr_est;
  173. }
  174. static int
  175. lws_h2_update_peer_txcredit_thresh(struct lws *wsi, int sid, int threshold, int bump)
  176. {
  177. if (wsi->txc.peer_tx_cr_est > threshold)
  178. return 0;
  179. return lws_h2_update_peer_txcredit(wsi, sid, bump);
  180. }
  181. struct lws *
  182. lws_wsi_server_new(struct lws_vhost *vh, struct lws *parent_wsi,
  183. unsigned int sid)
  184. {
  185. struct lws *wsi;
  186. struct lws *nwsi = lws_get_network_wsi(parent_wsi);
  187. struct lws_h2_netconn *h2n = nwsi->h2.h2n;
  188. /*
  189. * The identifier of a newly established stream MUST be numerically
  190. * greater than all streams that the initiating endpoint has opened or
  191. * reserved. This governs streams that are opened using a HEADERS frame
  192. * and streams that are reserved using PUSH_PROMISE. An endpoint that
  193. * receives an unexpected stream identifier MUST respond with a
  194. * connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  195. */
  196. if (sid <= h2n->highest_sid_opened) {
  197. lwsl_info("%s: tried to open lower sid %d (%d)\n", __func__,
  198. sid, (int)h2n->highest_sid_opened);
  199. lws_h2_goaway(nwsi, H2_ERR_PROTOCOL_ERROR, "Bad sid");
  200. return NULL;
  201. }
  202. /* no more children allowed by parent */
  203. if (parent_wsi->mux.child_count + 1 >
  204. parent_wsi->h2.h2n->our_set.s[H2SET_MAX_CONCURRENT_STREAMS]) {
  205. lwsl_notice("reached concurrent stream limit\n");
  206. return NULL;
  207. }
  208. wsi = lws_create_new_server_wsi(vh, parent_wsi->tsi);
  209. if (!wsi) {
  210. lwsl_notice("new server wsi failed (vh %p)\n", vh);
  211. return NULL;
  212. }
  213. h2n->highest_sid_opened = sid;
  214. lws_wsi_mux_insert(wsi, parent_wsi, sid);
  215. if (sid >= h2n->highest_sid)
  216. h2n->highest_sid = sid + 2;
  217. wsi->mux_substream = 1;
  218. wsi->seen_nonpseudoheader = 0;
  219. wsi->txc.tx_cr = nwsi->h2.h2n->peer_set.s[H2SET_INITIAL_WINDOW_SIZE];
  220. wsi->txc.peer_tx_cr_est =
  221. nwsi->h2.h2n->our_set.s[H2SET_INITIAL_WINDOW_SIZE];
  222. lwsi_set_state(wsi, LRS_ESTABLISHED);
  223. lwsi_set_role(wsi, lwsi_role(parent_wsi));
  224. wsi->a.protocol = &vh->protocols[0];
  225. if (lws_ensure_user_space(wsi))
  226. goto bail1;
  227. #if defined(LWS_WITH_SERVER_STATUS)
  228. wsi->a.vhost->conn_stats.h2_subs++;
  229. #endif
  230. #if defined(LWS_WITH_SERVER) && defined(LWS_WITH_SECURE_STREAMS)
  231. if (lws_adopt_ss_server_accept(wsi))
  232. goto bail1;
  233. #endif
  234. /* get the ball rolling */
  235. lws_validity_confirmed(wsi);
  236. lwsl_info("%s: %p new ch %p, sid %d, usersp=%p\n", __func__,
  237. parent_wsi, wsi, sid, wsi->user_space);
  238. lws_wsi_txc_describe(&wsi->txc, __func__, wsi->mux.my_sid);
  239. lws_wsi_txc_describe(&nwsi->txc, __func__, 0);
  240. return wsi;
  241. bail1:
  242. /* undo the insert */
  243. parent_wsi->mux.child_list = wsi->mux.sibling_list;
  244. parent_wsi->mux.child_count--;
  245. vh->context->count_wsi_allocated--;
  246. if (wsi->user_space)
  247. lws_free_set_NULL(wsi->user_space);
  248. vh->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY, NULL, NULL, 0);
  249. lws_vhost_unbind_wsi(wsi);
  250. lws_free(wsi);
  251. return NULL;
  252. }
  253. struct lws *
  254. lws_wsi_h2_adopt(struct lws *parent_wsi, struct lws *wsi)
  255. {
  256. struct lws *nwsi = lws_get_network_wsi(parent_wsi);
  257. /* no more children allowed by parent */
  258. if (parent_wsi->mux.child_count + 1 >
  259. parent_wsi->h2.h2n->our_set.s[H2SET_MAX_CONCURRENT_STREAMS]) {
  260. lwsl_notice("reached concurrent stream limit\n");
  261. return NULL;
  262. }
  263. /* sid is set just before issuing the headers, ensuring monoticity */
  264. wsi->seen_nonpseudoheader = 0;
  265. #if defined(LWS_WITH_CLIENT)
  266. wsi->client_mux_substream = 1;
  267. #endif
  268. wsi->h2.initialized = 1;
  269. #if 0
  270. /* only assign sid at header send time when we know it */
  271. if (!wsi->mux.my_sid) {
  272. wsi->mux.my_sid = nwsi->h2.h2n->highest_sid;
  273. nwsi->h2.h2n->highest_sid += 2;
  274. }
  275. #endif
  276. lwsl_info("%s: binding wsi %p to sid %d (next %d)\n", __func__,
  277. wsi, (int)wsi->mux.my_sid, (int)nwsi->h2.h2n->highest_sid);
  278. lws_wsi_mux_insert(wsi, parent_wsi, wsi->mux.my_sid);
  279. wsi->txc.tx_cr = nwsi->h2.h2n->peer_set.s[H2SET_INITIAL_WINDOW_SIZE];
  280. wsi->txc.peer_tx_cr_est =
  281. nwsi->h2.h2n->our_set.s[H2SET_INITIAL_WINDOW_SIZE];
  282. lws_wsi_txc_describe(&wsi->txc, __func__, wsi->mux.my_sid);
  283. if (lws_ensure_user_space(wsi))
  284. goto bail1;
  285. lws_role_transition(wsi, LWSIFR_CLIENT, LRS_H2_WAITING_TO_SEND_HEADERS,
  286. &role_ops_h2);
  287. lws_callback_on_writable(wsi);
  288. #if defined(LWS_WITH_SERVER_STATUS)
  289. wsi->a.vhost->conn_stats.h2_subs++;
  290. #endif
  291. return wsi;
  292. bail1:
  293. /* undo the insert */
  294. parent_wsi->mux.child_list = wsi->mux.sibling_list;
  295. parent_wsi->mux.child_count--;
  296. if (wsi->user_space)
  297. lws_free_set_NULL(wsi->user_space);
  298. wsi->a.protocol->callback(wsi, LWS_CALLBACK_WSI_DESTROY, NULL, NULL, 0);
  299. lws_free(wsi);
  300. return NULL;
  301. }
  302. int lws_h2_issue_preface(struct lws *wsi)
  303. {
  304. struct lws_h2_netconn *h2n = wsi->h2.h2n;
  305. struct lws_h2_protocol_send *pps;
  306. if (lws_issue_raw(wsi, (uint8_t *)preface, strlen(preface)) !=
  307. (int)strlen(preface))
  308. return 1;
  309. lws_role_transition(wsi, LWSIFR_CLIENT, LRS_H2_WAITING_TO_SEND_HEADERS,
  310. &role_ops_h2);
  311. h2n->count = 0;
  312. wsi->txc.tx_cr = 65535;
  313. /*
  314. * we must send a settings frame
  315. */
  316. pps = lws_h2_new_pps(LWS_H2_PPS_MY_SETTINGS);
  317. if (!pps)
  318. return 1;
  319. lws_pps_schedule(wsi, pps);
  320. lwsl_info("%s: h2 client sending settings\n", __func__);
  321. return 0;
  322. }
  323. void
  324. lws_pps_schedule(struct lws *wsi, struct lws_h2_protocol_send *pps)
  325. {
  326. struct lws *nwsi = lws_get_network_wsi(wsi);
  327. struct lws_h2_netconn *h2n = nwsi->h2.h2n;
  328. pps->next = h2n->pps;
  329. h2n->pps = pps;
  330. lws_rx_flow_control(wsi, LWS_RXFLOW_REASON_APPLIES_DISABLE |
  331. LWS_RXFLOW_REASON_H2_PPS_PENDING);
  332. lws_callback_on_writable(wsi);
  333. }
  334. int
  335. lws_h2_goaway(struct lws *wsi, uint32_t err, const char *reason)
  336. {
  337. struct lws_h2_netconn *h2n = wsi->h2.h2n;
  338. struct lws_h2_protocol_send *pps;
  339. if (h2n->type == LWS_H2_FRAME_TYPE_COUNT)
  340. return 0;
  341. pps = lws_h2_new_pps(LWS_H2_PPS_GOAWAY);
  342. if (!pps)
  343. return 1;
  344. lwsl_info("%s: %p: ERR 0x%x, '%s'\n", __func__, wsi, (int)err, reason);
  345. pps->u.ga.err = err;
  346. pps->u.ga.highest_sid = h2n->highest_sid;
  347. lws_strncpy(pps->u.ga.str, reason, sizeof(pps->u.ga.str));
  348. lws_pps_schedule(wsi, pps);
  349. h2n->type = LWS_H2_FRAME_TYPE_COUNT; /* ie, IGNORE */
  350. return 0;
  351. }
  352. int
  353. lws_h2_rst_stream(struct lws *wsi, uint32_t err, const char *reason)
  354. {
  355. struct lws *nwsi = lws_get_network_wsi(wsi);
  356. struct lws_h2_netconn *h2n = nwsi->h2.h2n;
  357. struct lws_h2_protocol_send *pps;
  358. if (!h2n)
  359. return 0;
  360. if (!wsi->h2_stream_carries_ws && h2n->type == LWS_H2_FRAME_TYPE_COUNT)
  361. return 0;
  362. pps = lws_h2_new_pps(LWS_H2_PPS_RST_STREAM);
  363. if (!pps)
  364. return 1;
  365. lwsl_info("%s: RST_STREAM 0x%x, sid %d, REASON '%s'\n", __func__,
  366. (int)err, wsi->mux.my_sid, reason);
  367. pps->u.rs.sid = wsi->mux.my_sid;
  368. pps->u.rs.err = err;
  369. lws_pps_schedule(wsi, pps);
  370. h2n->type = LWS_H2_FRAME_TYPE_COUNT; /* ie, IGNORE */
  371. lws_h2_state(wsi, LWS_H2_STATE_CLOSED);
  372. return 0;
  373. }
  374. int
  375. lws_h2_settings(struct lws *wsi, struct http2_settings *settings,
  376. unsigned char *buf, int len)
  377. {
  378. struct lws *nwsi = lws_get_network_wsi(wsi);
  379. unsigned int a, b;
  380. if (!len)
  381. return 0;
  382. if (len < LWS_H2_SETTINGS_LEN)
  383. return 1;
  384. while (len >= LWS_H2_SETTINGS_LEN) {
  385. a = (buf[0] << 8) | buf[1];
  386. if (!a || a >= H2SET_COUNT)
  387. goto skip;
  388. b = buf[2] << 24 | buf[3] << 16 | buf[4] << 8 | buf[5];
  389. switch (a) {
  390. case H2SET_HEADER_TABLE_SIZE:
  391. break;
  392. case H2SET_ENABLE_PUSH:
  393. if (b > 1) {
  394. lws_h2_goaway(nwsi, H2_ERR_PROTOCOL_ERROR,
  395. "ENABLE_PUSH invalid arg");
  396. return 1;
  397. }
  398. break;
  399. case H2SET_MAX_CONCURRENT_STREAMS:
  400. break;
  401. case H2SET_INITIAL_WINDOW_SIZE:
  402. if (b > 0x7fffffff) {
  403. lws_h2_goaway(nwsi, H2_ERR_FLOW_CONTROL_ERROR,
  404. "Inital Window beyond max");
  405. return 1;
  406. }
  407. #if defined(LWS_WITH_CLIENT)
  408. #if defined(LWS_AMAZON_RTOS) || defined(LWS_AMAZON_LINUX)
  409. if (
  410. #else
  411. if (wsi->flags & LCCSCF_H2_QUIRK_OVERFLOWS_TXCR &&
  412. #endif
  413. b == 0x7fffffff) {
  414. b >>= 4;
  415. break;
  416. }
  417. #endif
  418. /*
  419. * In addition to changing the flow-control window for
  420. * streams that are not yet active, a SETTINGS frame
  421. * can alter the initial flow-control window size for
  422. * streams with active flow-control windows (that is,
  423. * streams in the "open" or "half-closed (remote)"
  424. * state). When the value of
  425. * SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver
  426. * MUST adjust the size of all stream flow-control
  427. * windows that it maintains by the difference between
  428. * the new value and the old value.
  429. */
  430. lws_start_foreach_ll(struct lws *, w,
  431. nwsi->mux.child_list) {
  432. lwsl_info("%s: adi child tc cr %d +%d -> %d",
  433. __func__, (int)w->txc.tx_cr,
  434. b - (unsigned int)settings->s[a],
  435. (int)w->txc.tx_cr + b -
  436. (unsigned int)settings->s[a]);
  437. w->txc.tx_cr += b - settings->s[a];
  438. if (w->txc.tx_cr > 0 &&
  439. w->txc.tx_cr <=
  440. (int32_t)(b - settings->s[a]))
  441. lws_callback_on_writable(w);
  442. } lws_end_foreach_ll(w, mux.sibling_list);
  443. break;
  444. case H2SET_MAX_FRAME_SIZE:
  445. if (b < wsi->a.vhost->h2.set.s[H2SET_MAX_FRAME_SIZE]) {
  446. lws_h2_goaway(nwsi, H2_ERR_PROTOCOL_ERROR,
  447. "Frame size < initial");
  448. return 1;
  449. }
  450. if (b > 0x00ffffff) {
  451. lws_h2_goaway(nwsi, H2_ERR_PROTOCOL_ERROR,
  452. "Settings Frame size above max");
  453. return 1;
  454. }
  455. break;
  456. case H2SET_MAX_HEADER_LIST_SIZE:
  457. break;
  458. }
  459. settings->s[a] = b;
  460. lwsl_info("http2 settings %d <- 0x%x\n", a, b);
  461. skip:
  462. len -= LWS_H2_SETTINGS_LEN;
  463. buf += LWS_H2_SETTINGS_LEN;
  464. }
  465. if (len)
  466. return 1;
  467. lws_h2_dump_settings(settings);
  468. return 0;
  469. }
  470. /* RFC7640 Sect 6.9
  471. *
  472. * The WINDOW_UPDATE frame can be specific to a stream or to the entire
  473. * connection. In the former case, the frame's stream identifier
  474. * indicates the affected stream; in the latter, the value "0" indicates
  475. * that the entire connection is the subject of the frame.
  476. *
  477. * ...
  478. *
  479. * Two flow-control windows are applicable: the stream flow-control
  480. * window and the connection flow-control window. The sender MUST NOT
  481. * send a flow-controlled frame with a length that exceeds the space
  482. * available in either of the flow-control windows advertised by the
  483. * receiver. Frames with zero length with the END_STREAM flag set (that
  484. * is, an empty DATA frame) MAY be sent if there is no available space
  485. * in either flow-control window.
  486. */
  487. int
  488. lws_h2_tx_cr_get(struct lws *wsi)
  489. {
  490. int c = wsi->txc.tx_cr;
  491. struct lws *nwsi = lws_get_network_wsi(wsi);
  492. if (!wsi->mux_substream && !nwsi->upgraded_to_http2)
  493. return ~0x80000000;
  494. lwsl_info ("%s: %p: own tx credit %d: nwsi credit %d\n",
  495. __func__, wsi, c, (int)nwsi->txc.tx_cr);
  496. if (nwsi->txc.tx_cr < c)
  497. c = nwsi->txc.tx_cr;
  498. if (c < 0)
  499. return 0;
  500. return c;
  501. }
  502. void
  503. lws_h2_tx_cr_consume(struct lws *wsi, int consumed)
  504. {
  505. struct lws *nwsi = lws_get_network_wsi(wsi);
  506. wsi->txc.tx_cr -= consumed;
  507. if (nwsi != wsi)
  508. nwsi->txc.tx_cr -= consumed;
  509. }
  510. int lws_h2_frame_write(struct lws *wsi, int type, int flags,
  511. unsigned int sid, unsigned int len, unsigned char *buf)
  512. {
  513. struct lws *nwsi = lws_get_network_wsi(wsi);
  514. unsigned char *p = &buf[-LWS_H2_FRAME_HEADER_LENGTH];
  515. int n;
  516. //if (wsi->h2_stream_carries_ws)
  517. // lwsl_hexdump_level(LLL_NOTICE, buf, len);
  518. *p++ = len >> 16;
  519. *p++ = len >> 8;
  520. *p++ = len;
  521. *p++ = type;
  522. *p++ = flags;
  523. *p++ = sid >> 24;
  524. *p++ = sid >> 16;
  525. *p++ = sid >> 8;
  526. *p++ = sid;
  527. lwsl_debug("%s: %p (eff %p). typ %d, fl 0x%x, sid=%d, len=%d, "
  528. "txcr=%d, nwsi->txcr=%d\n", __func__, wsi, nwsi, type, flags,
  529. sid, len, (int)wsi->txc.tx_cr, (int)nwsi->txc.tx_cr);
  530. if (type == LWS_H2_FRAME_TYPE_DATA) {
  531. if (wsi->txc.tx_cr < (int)len)
  532. lwsl_info("%s: %p: sending payload len %d"
  533. " but tx_cr only %d!\n", __func__, wsi,
  534. len, (int)wsi->txc.tx_cr);
  535. lws_h2_tx_cr_consume(wsi, len);
  536. }
  537. n = lws_issue_raw(nwsi, &buf[-LWS_H2_FRAME_HEADER_LENGTH],
  538. len + LWS_H2_FRAME_HEADER_LENGTH);
  539. if (n < 0)
  540. return n;
  541. if (n >= LWS_H2_FRAME_HEADER_LENGTH)
  542. return n - LWS_H2_FRAME_HEADER_LENGTH;
  543. return n;
  544. }
  545. static void lws_h2_set_bin(struct lws *wsi, int n, unsigned char *buf)
  546. {
  547. *buf++ = n >> 8;
  548. *buf++ = n;
  549. *buf++ = wsi->h2.h2n->our_set.s[n] >> 24;
  550. *buf++ = wsi->h2.h2n->our_set.s[n] >> 16;
  551. *buf++ = wsi->h2.h2n->our_set.s[n] >> 8;
  552. *buf = wsi->h2.h2n->our_set.s[n];
  553. }
  554. /* we get called on the network connection */
  555. int lws_h2_do_pps_send(struct lws *wsi)
  556. {
  557. struct lws_h2_netconn *h2n = wsi->h2.h2n;
  558. struct lws_h2_protocol_send *pps = NULL;
  559. struct lws *cwsi;
  560. uint8_t set[LWS_PRE + 64], *p = &set[LWS_PRE], *q;
  561. int n, m = 0, flags = 0;
  562. if (!h2n)
  563. return 1;
  564. /* get the oldest pps */
  565. lws_start_foreach_llp(struct lws_h2_protocol_send **, pps1, h2n->pps) {
  566. if ((*pps1)->next == NULL) { /* we are the oldest in the list */
  567. pps = *pps1; /* remove us from the list */
  568. *pps1 = NULL;
  569. continue;
  570. }
  571. } lws_end_foreach_llp(pps1, next);
  572. if (!pps)
  573. return 1;
  574. lwsl_info("%s: %p: %d\n", __func__, wsi, pps->type);
  575. switch (pps->type) {
  576. case LWS_H2_PPS_MY_SETTINGS:
  577. /*
  578. * if any of our settings varies from h2 "default defaults"
  579. * then we must inform the peer
  580. */
  581. for (n = 1; n < H2SET_COUNT; n++)
  582. if (h2n->our_set.s[n] != lws_h2_defaults.s[n]) {
  583. lwsl_debug("sending SETTING %d 0x%x\n", n,
  584. (unsigned int)
  585. wsi->h2.h2n->our_set.s[n]);
  586. lws_h2_set_bin(wsi, n, &set[LWS_PRE + m]);
  587. m += sizeof(h2n->one_setting);
  588. }
  589. n = lws_h2_frame_write(wsi, LWS_H2_FRAME_TYPE_SETTINGS,
  590. flags, LWS_H2_STREAM_ID_MASTER, m,
  591. &set[LWS_PRE]);
  592. if (n != m) {
  593. lwsl_info("send %d %d\n", n, m);
  594. goto bail;
  595. }
  596. break;
  597. case LWS_H2_PPS_SETTINGS_INITIAL_UPDATE_WINDOW:
  598. q = &set[LWS_PRE];
  599. *q++ = H2SET_INITIAL_WINDOW_SIZE >> 8;
  600. *q++ = H2SET_INITIAL_WINDOW_SIZE;
  601. *q++ = pps->u.update_window.credit >> 24;
  602. *q++ = pps->u.update_window.credit >> 16;
  603. *q++ = pps->u.update_window.credit >> 8;
  604. *q = pps->u.update_window.credit;
  605. lwsl_debug("%s: resetting initial window to %d\n", __func__,
  606. (int)pps->u.update_window.credit);
  607. n = lws_h2_frame_write(wsi, LWS_H2_FRAME_TYPE_SETTINGS,
  608. flags, LWS_H2_STREAM_ID_MASTER, 6,
  609. &set[LWS_PRE]);
  610. if (n != 6) {
  611. lwsl_info("send %d %d\n", n, m);
  612. goto bail;
  613. }
  614. break;
  615. case LWS_H2_PPS_ACK_SETTINGS:
  616. /* send ack ... always empty */
  617. n = lws_h2_frame_write(wsi, LWS_H2_FRAME_TYPE_SETTINGS, 1,
  618. LWS_H2_STREAM_ID_MASTER, 0,
  619. &set[LWS_PRE]);
  620. if (n) {
  621. lwsl_err("ack tells %d\n", n);
  622. goto bail;
  623. }
  624. wsi->h2_acked_settings = 0;
  625. /* this is the end of the preface dance then? */
  626. if (lwsi_state(wsi) == LRS_H2_AWAIT_SETTINGS) {
  627. lwsi_set_state(wsi, LRS_ESTABLISHED);
  628. #if defined(LWS_WITH_FILE_OPS)
  629. wsi->http.fop_fd = NULL;
  630. #endif
  631. if (lws_is_ssl(lws_get_network_wsi(wsi)))
  632. break;
  633. if (wsi->a.vhost->options &
  634. LWS_SERVER_OPTION_H2_PRIOR_KNOWLEDGE)
  635. break;
  636. /*
  637. * we need to treat the headers from the upgrade as the
  638. * first job. So these need to get shifted to sid 1.
  639. */
  640. h2n->swsi = lws_wsi_server_new(wsi->a.vhost, wsi, 1);
  641. if (!h2n->swsi)
  642. goto bail;
  643. /* pass on the initial headers to SID 1 */
  644. h2n->swsi->http.ah = wsi->http.ah;
  645. wsi->http.ah = NULL;
  646. lwsl_info("%s: inherited headers %p\n", __func__,
  647. h2n->swsi->http.ah);
  648. h2n->swsi->txc.tx_cr =
  649. h2n->our_set.s[H2SET_INITIAL_WINDOW_SIZE];
  650. lwsl_info("initial tx credit on conn %p: %d\n",
  651. h2n->swsi, (int)h2n->swsi->txc.tx_cr);
  652. h2n->swsi->h2.initialized = 1;
  653. /* demanded by HTTP2 */
  654. h2n->swsi->h2.END_STREAM = 1;
  655. lwsl_info("servicing initial http request\n");
  656. #if defined(LWS_WITH_SERVER_STATUS)
  657. wsi->a.vhost->conn_stats.h2_trans++;
  658. #endif
  659. #if defined(LWS_WITH_SERVER)
  660. if (lws_http_action(h2n->swsi))
  661. goto bail;
  662. #endif
  663. break;
  664. }
  665. break;
  666. /*
  667. * h2 only has PING... ACK = 0 = ping, ACK = 1 = pong
  668. */
  669. case LWS_H2_PPS_PING:
  670. case LWS_H2_PPS_PONG:
  671. if (pps->type == LWS_H2_PPS_PING)
  672. lwsl_info("sending PING\n");
  673. else {
  674. lwsl_info("sending PONG\n");
  675. flags = LWS_H2_FLAG_SETTINGS_ACK;
  676. }
  677. memcpy(&set[LWS_PRE], pps->u.ping.ping_payload, 8);
  678. n = lws_h2_frame_write(wsi, LWS_H2_FRAME_TYPE_PING, flags,
  679. LWS_H2_STREAM_ID_MASTER, 8,
  680. &set[LWS_PRE]);
  681. if (n != 8)
  682. goto bail;
  683. break;
  684. case LWS_H2_PPS_GOAWAY:
  685. lwsl_info("LWS_H2_PPS_GOAWAY\n");
  686. *p++ = pps->u.ga.highest_sid >> 24;
  687. *p++ = pps->u.ga.highest_sid >> 16;
  688. *p++ = pps->u.ga.highest_sid >> 8;
  689. *p++ = pps->u.ga.highest_sid;
  690. *p++ = pps->u.ga.err >> 24;
  691. *p++ = pps->u.ga.err >> 16;
  692. *p++ = pps->u.ga.err >> 8;
  693. *p++ = pps->u.ga.err;
  694. q = (unsigned char *)pps->u.ga.str;
  695. n = 0;
  696. while (*q && n++ < (int)sizeof(pps->u.ga.str))
  697. *p++ = *q++;
  698. h2n->we_told_goaway = 1;
  699. n = lws_h2_frame_write(wsi, LWS_H2_FRAME_TYPE_GOAWAY, 0,
  700. LWS_H2_STREAM_ID_MASTER,
  701. lws_ptr_diff(p, &set[LWS_PRE]),
  702. &set[LWS_PRE]);
  703. if (n != 4) {
  704. lwsl_info("send %d %d\n", n, m);
  705. goto bail;
  706. }
  707. goto bail;
  708. case LWS_H2_PPS_RST_STREAM:
  709. lwsl_info("LWS_H2_PPS_RST_STREAM\n");
  710. *p++ = pps->u.rs.err >> 24;
  711. *p++ = pps->u.rs.err >> 16;
  712. *p++ = pps->u.rs.err >> 8;
  713. *p++ = pps->u.rs.err;
  714. n = lws_h2_frame_write(wsi, LWS_H2_FRAME_TYPE_RST_STREAM,
  715. 0, pps->u.rs.sid, 4, &set[LWS_PRE]);
  716. if (n != 4) {
  717. lwsl_info("send %d %d\n", n, m);
  718. goto bail;
  719. }
  720. cwsi = lws_wsi_mux_from_id(wsi, pps->u.rs.sid);
  721. if (cwsi) {
  722. lwsl_debug("%s: closing cwsi %p %s %s (wsi %p)\n",
  723. __func__, cwsi, cwsi->role_ops->name,
  724. cwsi->a.protocol->name, wsi);
  725. lws_close_free_wsi(cwsi, 0, "reset stream");
  726. }
  727. break;
  728. case LWS_H2_PPS_UPDATE_WINDOW:
  729. lwsl_info("Issuing LWS_H2_PPS_UPDATE_WINDOW: sid %d: add %d\n",
  730. (int)pps->u.update_window.sid,
  731. (int)pps->u.update_window.credit);
  732. *p++ = (pps->u.update_window.credit >> 24) & 0x7f; /* 31b */
  733. *p++ = pps->u.update_window.credit >> 16;
  734. *p++ = pps->u.update_window.credit >> 8;
  735. *p++ = pps->u.update_window.credit;
  736. n = lws_h2_frame_write(wsi, LWS_H2_FRAME_TYPE_WINDOW_UPDATE,
  737. 0, pps->u.update_window.sid, 4,
  738. &set[LWS_PRE]);
  739. if (n != 4) {
  740. lwsl_info("send %d %d\n", n, m);
  741. goto bail;
  742. }
  743. break;
  744. default:
  745. break;
  746. }
  747. lws_free(pps);
  748. return 0;
  749. bail:
  750. lws_free(pps);
  751. return 1;
  752. }
  753. static int
  754. lws_h2_parse_end_of_frame(struct lws *wsi);
  755. /*
  756. * The frame header part has just completely arrived.
  757. * Perform actions for header completion.
  758. */
  759. static int
  760. lws_h2_parse_frame_header(struct lws *wsi)
  761. {
  762. struct lws_h2_netconn *h2n = wsi->h2.h2n;
  763. struct lws_h2_protocol_send *pps;
  764. int n;
  765. /*
  766. * We just got the frame header
  767. */
  768. h2n->count = 0;
  769. h2n->swsi = wsi;
  770. /* b31 is a reserved bit */
  771. h2n->sid = h2n->sid & 0x7fffffff;
  772. if (h2n->sid && !(h2n->sid & 1)) {
  773. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR, "Even Stream ID");
  774. return 0;
  775. }
  776. /* let the network wsi live a bit longer if subs are active */
  777. if (!wsi->immortal_substream_count)
  778. lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE,
  779. wsi->a.vhost->keepalive_timeout ?
  780. wsi->a.vhost->keepalive_timeout : 31);
  781. if (h2n->sid)
  782. h2n->swsi = lws_wsi_mux_from_id(wsi, h2n->sid);
  783. lwsl_debug("%p (%p): fr hdr: typ 0x%x, fla 0x%x, sid 0x%x, len 0x%x\n",
  784. wsi, h2n->swsi, h2n->type, h2n->flags, (unsigned int)h2n->sid,
  785. (unsigned int)h2n->length);
  786. if (h2n->we_told_goaway && h2n->sid > h2n->highest_sid)
  787. h2n->type = LWS_H2_FRAME_TYPE_COUNT; /* ie, IGNORE */
  788. if (h2n->type == LWS_H2_FRAME_TYPE_COUNT)
  789. return 0;
  790. if (h2n->length > h2n->our_set.s[H2SET_MAX_FRAME_SIZE]) {
  791. /*
  792. * peer sent us something bigger than we told
  793. * it we would allow
  794. */
  795. lwsl_info("%s: received oversize frame %d\n", __func__,
  796. (unsigned int)h2n->length);
  797. lws_h2_goaway(wsi, H2_ERR_FRAME_SIZE_ERROR,
  798. "Peer ignored our frame size setting");
  799. return 1;
  800. }
  801. if (h2n->swsi)
  802. lwsl_info("%s: wsi %p, State: %s, received cmd %d\n",
  803. __func__, h2n->swsi,
  804. h2_state_names[h2n->swsi->h2.h2_state], h2n->type);
  805. else {
  806. /* if it's data, either way no swsi means CLOSED state */
  807. if (h2n->type == LWS_H2_FRAME_TYPE_DATA) {
  808. if (h2n->sid <= h2n->highest_sid_opened
  809. #if defined(LWS_WITH_CLIENT)
  810. && wsi->client_h2_alpn
  811. #endif
  812. ) {
  813. lwsl_notice("ignoring straggling data fl 0x%x\n",
  814. h2n->flags);
  815. /* ie, IGNORE */
  816. h2n->type = LWS_H2_FRAME_TYPE_COUNT;
  817. } else {
  818. lws_h2_goaway(wsi, H2_ERR_STREAM_CLOSED,
  819. "Data for nonexistent sid");
  820. return 0;
  821. }
  822. }
  823. /* if the sid is credible, treat as wsi for it closed */
  824. if (h2n->sid > h2n->highest_sid_opened &&
  825. h2n->type != LWS_H2_FRAME_TYPE_HEADERS &&
  826. h2n->type != LWS_H2_FRAME_TYPE_PRIORITY) {
  827. /* if not credible, reject it */
  828. lwsl_info("%s: wsi %p, No child for sid %d, rxcmd %d\n",
  829. __func__, h2n->swsi, (unsigned int)h2n->sid, h2n->type);
  830. lws_h2_goaway(wsi, H2_ERR_STREAM_CLOSED,
  831. "Data for nonexistent sid");
  832. return 0;
  833. }
  834. }
  835. if (h2n->swsi && h2n->sid &&
  836. !(http2_rx_validity[h2n->swsi->h2.h2_state] & (1 << h2n->type))) {
  837. lwsl_info("%s: wsi %p, State: %s, ILLEGAL cmdrx %d (OK 0x%x)\n",
  838. __func__, h2n->swsi,
  839. h2_state_names[h2n->swsi->h2.h2_state], h2n->type,
  840. http2_rx_validity[h2n->swsi->h2.h2_state]);
  841. if (h2n->swsi->h2.h2_state == LWS_H2_STATE_CLOSED ||
  842. h2n->swsi->h2.h2_state == LWS_H2_STATE_HALF_CLOSED_REMOTE)
  843. n = H2_ERR_STREAM_CLOSED;
  844. else
  845. n = H2_ERR_PROTOCOL_ERROR;
  846. lws_h2_goaway(wsi, n, "invalid rx for state");
  847. return 0;
  848. }
  849. if (h2n->cont_exp && (h2n->cont_exp_sid != h2n->sid ||
  850. h2n->type != LWS_H2_FRAME_TYPE_CONTINUATION)) {
  851. lwsl_info("%s: expected cont on sid %u (got %d on sid %u)\n",
  852. __func__, (unsigned int)h2n->cont_exp_sid, h2n->type,
  853. (unsigned int)h2n->sid);
  854. h2n->cont_exp = 0;
  855. if (h2n->cont_exp_headers)
  856. n = H2_ERR_COMPRESSION_ERROR;
  857. else
  858. n = H2_ERR_PROTOCOL_ERROR;
  859. lws_h2_goaway(wsi, n, "Continuation hdrs State");
  860. return 0;
  861. }
  862. switch (h2n->type) {
  863. case LWS_H2_FRAME_TYPE_DATA:
  864. lwsl_info("seen incoming LWS_H2_FRAME_TYPE_DATA start\n");
  865. if (!h2n->sid) {
  866. lwsl_info("DATA: 0 sid\n");
  867. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR, "DATA 0 sid");
  868. break;
  869. }
  870. lwsl_info("Frame header DATA: sid %u, flags 0x%x, len %u\n",
  871. (unsigned int)h2n->sid, h2n->flags,
  872. (unsigned int)h2n->length);
  873. if (!h2n->swsi) {
  874. lwsl_notice("DATA: NULL swsi\n");
  875. break;
  876. }
  877. lwsl_info("DATA rx on state %d\n", h2n->swsi->h2.h2_state);
  878. if (
  879. h2n->swsi->h2.h2_state == LWS_H2_STATE_HALF_CLOSED_REMOTE ||
  880. h2n->swsi->h2.h2_state == LWS_H2_STATE_CLOSED) {
  881. lws_h2_goaway(wsi, H2_ERR_STREAM_CLOSED, "conn closed");
  882. break;
  883. }
  884. if (h2n->length == 0)
  885. lws_h2_parse_end_of_frame(wsi);
  886. break;
  887. case LWS_H2_FRAME_TYPE_PRIORITY:
  888. lwsl_info("LWS_H2_FRAME_TYPE_PRIORITY complete frame\n");
  889. if (!h2n->sid) {
  890. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  891. "Priority has 0 sid");
  892. break;
  893. }
  894. if (h2n->length != 5) {
  895. lws_h2_goaway(wsi, H2_ERR_FRAME_SIZE_ERROR,
  896. "Priority has length other than 5");
  897. break;
  898. }
  899. break;
  900. case LWS_H2_FRAME_TYPE_PUSH_PROMISE:
  901. lwsl_info("LWS_H2_FRAME_TYPE_PUSH_PROMISE complete frame\n");
  902. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR, "Server only");
  903. break;
  904. case LWS_H2_FRAME_TYPE_GOAWAY:
  905. lwsl_debug("LWS_H2_FRAME_TYPE_GOAWAY received\n");
  906. break;
  907. case LWS_H2_FRAME_TYPE_RST_STREAM:
  908. if (!h2n->sid)
  909. return 1;
  910. if (!h2n->swsi) {
  911. if (h2n->sid <= h2n->highest_sid_opened)
  912. break;
  913. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  914. "crazy sid on RST_STREAM");
  915. return 1;
  916. }
  917. if (h2n->length != 4) {
  918. lws_h2_goaway(wsi, H2_ERR_FRAME_SIZE_ERROR,
  919. "RST_STREAM can only be length 4");
  920. break;
  921. }
  922. lws_h2_state(h2n->swsi, LWS_H2_STATE_CLOSED);
  923. break;
  924. case LWS_H2_FRAME_TYPE_SETTINGS:
  925. lwsl_info("LWS_H2_FRAME_TYPE_SETTINGS complete frame\n");
  926. /* nonzero sid on settings is illegal */
  927. if (h2n->sid) {
  928. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  929. "Settings has nonzero sid");
  930. break;
  931. }
  932. if (!(h2n->flags & LWS_H2_FLAG_SETTINGS_ACK)) {
  933. if (h2n->length % 6) {
  934. lws_h2_goaway(wsi, H2_ERR_FRAME_SIZE_ERROR,
  935. "Settings length error");
  936. break;
  937. }
  938. if (h2n->type == LWS_H2_FRAME_TYPE_COUNT)
  939. return 0;
  940. if (wsi->upgraded_to_http2 &&
  941. #if defined(LWS_WITH_CLIENT)
  942. (!(wsi->flags & LCCSCF_H2_QUIRK_NGHTTP2_END_STREAM) ||
  943. #else
  944. (
  945. #endif
  946. !wsi->h2_acked_settings)) {
  947. pps = lws_h2_new_pps(LWS_H2_PPS_ACK_SETTINGS);
  948. if (!pps)
  949. return 1;
  950. lws_pps_schedule(wsi, pps);
  951. wsi->h2_acked_settings = 1;
  952. }
  953. break;
  954. }
  955. /* came to us with ACK set... not allowed to have payload */
  956. if (h2n->length) {
  957. lws_h2_goaway(wsi, H2_ERR_FRAME_SIZE_ERROR,
  958. "Settings with ACK not allowed payload");
  959. break;
  960. }
  961. break;
  962. case LWS_H2_FRAME_TYPE_PING:
  963. if (h2n->sid) {
  964. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  965. "Ping has nonzero sid");
  966. break;
  967. }
  968. if (h2n->length != 8) {
  969. lws_h2_goaway(wsi, H2_ERR_FRAME_SIZE_ERROR,
  970. "Ping payload can only be 8");
  971. break;
  972. }
  973. break;
  974. case LWS_H2_FRAME_TYPE_CONTINUATION:
  975. lwsl_info("LWS_H2_FRAME_TYPE_CONTINUATION: sid = %u %d %d\n",
  976. (unsigned int)h2n->sid, (int)h2n->cont_exp,
  977. (int)h2n->cont_exp_sid);
  978. if (!h2n->cont_exp ||
  979. h2n->cont_exp_sid != h2n->sid ||
  980. !h2n->sid ||
  981. !h2n->swsi) {
  982. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  983. "unexpected CONTINUATION");
  984. break;
  985. }
  986. if (h2n->swsi->h2.END_HEADERS) {
  987. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  988. "END_HEADERS already seen");
  989. break;
  990. }
  991. /* END_STREAM is in HEADERS, skip resetting it */
  992. goto update_end_headers;
  993. case LWS_H2_FRAME_TYPE_HEADERS:
  994. lwsl_info("HEADERS: frame header: sid = %u\n",
  995. (unsigned int)h2n->sid);
  996. if (!h2n->sid) {
  997. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR, "sid 0");
  998. return 1;
  999. }
  1000. if (h2n->swsi && !h2n->swsi->h2.END_STREAM &&
  1001. h2n->swsi->h2.END_HEADERS &&
  1002. !(h2n->flags & LWS_H2_FLAG_END_STREAM)) {
  1003. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  1004. "extra HEADERS together");
  1005. return 1;
  1006. }
  1007. #if defined(LWS_WITH_CLIENT)
  1008. if (wsi->client_h2_alpn) {
  1009. if (h2n->sid) {
  1010. h2n->swsi = lws_wsi_mux_from_id(wsi, h2n->sid);
  1011. lwsl_info("HEADERS: nwsi %p: sid %u mapped "
  1012. "to wsi %p\n", wsi,
  1013. (unsigned int)h2n->sid, h2n->swsi);
  1014. if (!h2n->swsi)
  1015. break;
  1016. }
  1017. goto update_end_headers;
  1018. }
  1019. #endif
  1020. if (!h2n->swsi) {
  1021. /* no more children allowed by parent */
  1022. if (wsi->mux.child_count + 1 >
  1023. wsi->h2.h2n->our_set.s[H2SET_MAX_CONCURRENT_STREAMS]) {
  1024. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  1025. "Another stream not allowed");
  1026. return 1;
  1027. }
  1028. /*
  1029. * The peer has sent us a HEADERS implying the creation
  1030. * of a new stream
  1031. */
  1032. h2n->swsi = lws_wsi_server_new(wsi->a.vhost, wsi,
  1033. h2n->sid);
  1034. if (!h2n->swsi) {
  1035. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  1036. "OOM");
  1037. return 1;
  1038. }
  1039. if (h2n->sid >= h2n->highest_sid)
  1040. h2n->highest_sid = h2n->sid + 2;
  1041. h2n->swsi->h2.initialized = 1;
  1042. if (lws_h2_update_peer_txcredit(h2n->swsi,
  1043. h2n->swsi->mux.my_sid, 4 * 65536))
  1044. goto cleanup_wsi;
  1045. }
  1046. /*
  1047. * ah needs attaching to child wsi, even though
  1048. * we only fill it from network wsi
  1049. */
  1050. if (!h2n->swsi->http.ah)
  1051. if (lws_header_table_attach(h2n->swsi, 0)) {
  1052. lwsl_err("%s: Failed to get ah\n", __func__);
  1053. return 1;
  1054. }
  1055. /*
  1056. * The first use of a new stream identifier implicitly closes
  1057. * all streams in the "idle" state that might have been
  1058. * initiated by that peer with a lower-valued stream identifier.
  1059. *
  1060. * For example, if a client sends a HEADERS frame on stream 7
  1061. * without ever sending a frame on stream 5, then stream 5
  1062. * transitions to the "closed" state when the first frame for
  1063. * stream 7 is sent or received.
  1064. */
  1065. lws_start_foreach_ll(struct lws *, w, wsi->mux.child_list) {
  1066. if (w->mux.my_sid < h2n->sid &&
  1067. w->h2.h2_state == LWS_H2_STATE_IDLE)
  1068. lws_close_free_wsi(w, 0, "h2 sid close");
  1069. assert(w->mux.sibling_list != w);
  1070. } lws_end_foreach_ll(w, mux.sibling_list);
  1071. if (lws_check_opt(h2n->swsi->a.vhost->options,
  1072. LWS_SERVER_OPTION_VH_H2_HALF_CLOSED_LONG_POLL)) {
  1073. /*
  1074. * We don't directly timeout streams that enter the
  1075. * half-closed remote state, allowing immortal long
  1076. * poll
  1077. */
  1078. lws_mux_mark_immortal(h2n->swsi);
  1079. lwsl_info("%s: %p: h2 stream entering long poll\n",
  1080. __func__, h2n->swsi);
  1081. } else {
  1082. h2n->swsi->h2.END_STREAM =
  1083. !!(h2n->flags & LWS_H2_FLAG_END_STREAM);
  1084. lwsl_debug("%s: hdr END_STREAM = %d\n",__func__,
  1085. h2n->swsi->h2.END_STREAM);
  1086. }
  1087. h2n->cont_exp = !(h2n->flags & LWS_H2_FLAG_END_HEADERS);
  1088. h2n->cont_exp_sid = h2n->sid;
  1089. h2n->cont_exp_headers = 1;
  1090. // lws_header_table_reset(h2n->swsi, 0);
  1091. update_end_headers:
  1092. /* no END_HEADERS means CONTINUATION must come */
  1093. h2n->swsi->h2.END_HEADERS =
  1094. !!(h2n->flags & LWS_H2_FLAG_END_HEADERS);
  1095. lwsl_info("%p: END_HEADERS %d\n", h2n->swsi,
  1096. h2n->swsi->h2.END_HEADERS);
  1097. if (h2n->swsi->h2.END_HEADERS)
  1098. h2n->cont_exp = 0;
  1099. lwsl_debug("END_HEADERS %d\n", h2n->swsi->h2.END_HEADERS);
  1100. break;
  1101. cleanup_wsi:
  1102. return 1;
  1103. case LWS_H2_FRAME_TYPE_WINDOW_UPDATE:
  1104. if (h2n->length != 4) {
  1105. lws_h2_goaway(wsi, H2_ERR_FRAME_SIZE_ERROR,
  1106. "window update frame not 4");
  1107. break;
  1108. }
  1109. lwsl_info("LWS_H2_FRAME_TYPE_WINDOW_UPDATE\n");
  1110. break;
  1111. case LWS_H2_FRAME_TYPE_COUNT:
  1112. break;
  1113. default:
  1114. lwsl_info("%s: ILLEGAL FRAME TYPE %d\n", __func__, h2n->type);
  1115. h2n->type = LWS_H2_FRAME_TYPE_COUNT; /* ie, IGNORE */
  1116. break;
  1117. }
  1118. if (h2n->length == 0)
  1119. h2n->frame_state = 0;
  1120. return 0;
  1121. }
  1122. static const char * const method_names[] = {
  1123. "GET", "POST",
  1124. #if defined(LWS_WITH_HTTP_UNCOMMON_HEADERS)
  1125. "OPTIONS", "PUT", "PATCH", "DELETE",
  1126. #endif
  1127. "CONNECT", "HEAD"
  1128. };
  1129. static unsigned char method_index[] = {
  1130. WSI_TOKEN_GET_URI,
  1131. WSI_TOKEN_POST_URI,
  1132. #if defined(LWS_WITH_HTTP_UNCOMMON_HEADERS)
  1133. WSI_TOKEN_OPTIONS_URI,
  1134. WSI_TOKEN_PUT_URI,
  1135. WSI_TOKEN_PATCH_URI,
  1136. WSI_TOKEN_DELETE_URI,
  1137. #endif
  1138. WSI_TOKEN_CONNECT,
  1139. WSI_TOKEN_HEAD_URI,
  1140. };
  1141. /*
  1142. * The last byte of the whole frame has been handled.
  1143. * Perform actions for frame completion.
  1144. *
  1145. * This is the crunch time for parsing that may have occured on a network
  1146. * wsi with a pending partial send... we may call lws_http_action() to send
  1147. * a response, conflicting with the partial.
  1148. *
  1149. * So in that case we change the wsi state and do the lws_http_action() in the
  1150. * WRITABLE handler as a priority.
  1151. */
  1152. static int
  1153. lws_h2_parse_end_of_frame(struct lws *wsi)
  1154. {
  1155. struct lws_h2_netconn *h2n = wsi->h2.h2n;
  1156. struct lws *eff_wsi = wsi;
  1157. const char *p;
  1158. int n;
  1159. h2n->frame_state = 0;
  1160. h2n->count = 0;
  1161. if (h2n->sid)
  1162. h2n->swsi = lws_wsi_mux_from_id(wsi, h2n->sid);
  1163. if (h2n->sid > h2n->highest_sid)
  1164. h2n->highest_sid = h2n->sid;
  1165. if (h2n->collected_priority && (h2n->dep & ~(1u << 31)) == h2n->sid) {
  1166. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR, "depends on own sid");
  1167. return 0;
  1168. }
  1169. switch (h2n->type) {
  1170. case LWS_H2_FRAME_TYPE_SETTINGS:
  1171. #if defined(LWS_WITH_CLIENT)
  1172. if (wsi->client_h2_alpn && !wsi->client_mux_migrated &&
  1173. !(h2n->flags & LWS_H2_FLAG_SETTINGS_ACK)) {
  1174. struct lws_h2_protocol_send *pps;
  1175. /* migrate original client ask on to substream 1 */
  1176. #if defined(LWS_WITH_FILE_OPS)
  1177. wsi->http.fop_fd = NULL;
  1178. #endif
  1179. lwsl_info("%s: migrating\n", __func__);
  1180. wsi->client_mux_migrated = 1;
  1181. /*
  1182. * we need to treat the headers from the upgrade as the
  1183. * first job. So these need to get shifted to sid 1.
  1184. */
  1185. h2n->swsi = lws_wsi_server_new(wsi->a.vhost, wsi, 1);
  1186. if (!h2n->swsi)
  1187. return 1;
  1188. h2n->sid = 1;
  1189. assert(lws_wsi_mux_from_id(wsi, 1) == h2n->swsi);
  1190. lws_role_transition(wsi, LWSIFR_CLIENT,
  1191. LRS_H2_WAITING_TO_SEND_HEADERS,
  1192. &role_ops_h2);
  1193. lws_role_transition(h2n->swsi, LWSIFR_CLIENT,
  1194. LRS_H2_WAITING_TO_SEND_HEADERS,
  1195. &role_ops_h2);
  1196. /* pass on the initial headers to SID 1 */
  1197. h2n->swsi->http.ah = wsi->http.ah;
  1198. h2n->swsi->client_mux_substream = 1;
  1199. h2n->swsi->client_h2_alpn = 1;
  1200. #if defined(LWS_WITH_CLIENT)
  1201. h2n->swsi->flags = wsi->flags;
  1202. #endif
  1203. h2n->swsi->a.protocol = wsi->a.protocol;
  1204. if (h2n->swsi->user_space &&
  1205. !h2n->swsi->user_space_externally_allocated)
  1206. lws_free(h2n->swsi->user_space);
  1207. h2n->swsi->user_space = wsi->user_space;
  1208. h2n->swsi->user_space_externally_allocated =
  1209. wsi->user_space_externally_allocated;
  1210. h2n->swsi->a.opaque_user_data = wsi->a.opaque_user_data;
  1211. wsi->a.opaque_user_data = NULL;
  1212. h2n->swsi->txc.manual_initial_tx_credit =
  1213. wsi->txc.manual_initial_tx_credit;
  1214. wsi->user_space = NULL;
  1215. if (h2n->swsi->http.ah)
  1216. h2n->swsi->http.ah->wsi = h2n->swsi;
  1217. wsi->http.ah = NULL;
  1218. lwsl_info("%s: MIGRATING nwsi %p: swsi %p\n", __func__,
  1219. wsi, h2n->swsi);
  1220. h2n->swsi->txc.tx_cr =
  1221. h2n->peer_set.s[H2SET_INITIAL_WINDOW_SIZE];
  1222. lwsl_info("%s: initial tx credit on conn %p: %d\n",
  1223. __func__, h2n->swsi, (int)h2n->swsi->txc.tx_cr);
  1224. h2n->swsi->h2.initialized = 1;
  1225. /* set our initial window size */
  1226. if (!wsi->h2.initialized) {
  1227. wsi->txc.tx_cr =
  1228. h2n->peer_set.s[H2SET_INITIAL_WINDOW_SIZE];
  1229. lwsl_info("%s: initial tx credit for us to "
  1230. "write on master %p: %d\n", __func__,
  1231. wsi, (int)wsi->txc.tx_cr);
  1232. wsi->h2.initialized = 1;
  1233. }
  1234. lws_callback_on_writable(h2n->swsi);
  1235. if (!wsi->h2_acked_settings ||
  1236. !(wsi->flags & LCCSCF_H2_QUIRK_NGHTTP2_END_STREAM)
  1237. ) {
  1238. pps = lws_h2_new_pps(LWS_H2_PPS_ACK_SETTINGS);
  1239. if (!pps)
  1240. return 1;
  1241. lws_pps_schedule(wsi, pps);
  1242. lwsl_info("%s: SETTINGS ack PPS\n", __func__);
  1243. wsi->h2_acked_settings = 1;
  1244. }
  1245. /* also attach any queued guys */
  1246. lws_wsi_mux_apply_queue(wsi);
  1247. }
  1248. #endif
  1249. break;
  1250. case LWS_H2_FRAME_TYPE_CONTINUATION:
  1251. case LWS_H2_FRAME_TYPE_HEADERS:
  1252. if (!h2n->swsi)
  1253. break;
  1254. /* service the http request itself */
  1255. if (h2n->last_action_dyntable_resize) {
  1256. lws_h2_goaway(wsi, H2_ERR_COMPRESSION_ERROR,
  1257. "dyntable resize last in headers");
  1258. break;
  1259. }
  1260. if (!h2n->swsi->h2.END_HEADERS) {
  1261. /* we are not finished yet */
  1262. lwsl_info("witholding http action for continuation\n");
  1263. h2n->cont_exp_sid = h2n->sid;
  1264. h2n->cont_exp = 1;
  1265. break;
  1266. }
  1267. /* confirm the hpack stream state is reasonable for finishing */
  1268. if (h2n->hpack != HPKS_TYPE) {
  1269. /* hpack incomplete */
  1270. lwsl_info("hpack incomplete %d (type %d, len %u)\n",
  1271. h2n->hpack, h2n->type,
  1272. (unsigned int)h2n->hpack_len);
  1273. lws_h2_goaway(wsi, H2_ERR_COMPRESSION_ERROR,
  1274. "hpack incomplete");
  1275. break;
  1276. }
  1277. /* this is the last part of HEADERS */
  1278. switch (h2n->swsi->h2.h2_state) {
  1279. case LWS_H2_STATE_IDLE:
  1280. lws_h2_state(h2n->swsi, LWS_H2_STATE_OPEN);
  1281. break;
  1282. case LWS_H2_STATE_RESERVED_REMOTE:
  1283. lws_h2_state(h2n->swsi, LWS_H2_STATE_HALF_CLOSED_LOCAL);
  1284. break;
  1285. }
  1286. lwsl_info("http req, wsi=%p, h2n->swsi=%p\n", wsi, h2n->swsi);
  1287. h2n->swsi->hdr_parsing_completed = 1;
  1288. #if defined(LWS_WITH_CLIENT)
  1289. if (h2n->swsi->client_mux_substream &&
  1290. lws_client_interpret_server_handshake(h2n->swsi)) {
  1291. lwsl_info("%s: cli int serv hs closed it\n", __func__);
  1292. break;
  1293. }
  1294. #endif
  1295. if (lws_hdr_extant(h2n->swsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
  1296. const char *simp = lws_hdr_simple_ptr(h2n->swsi,
  1297. WSI_TOKEN_HTTP_CONTENT_LENGTH);
  1298. if (!simp) /* coverity */
  1299. return 1;
  1300. h2n->swsi->http.rx_content_length = atoll(simp);
  1301. h2n->swsi->http.rx_content_remain =
  1302. h2n->swsi->http.rx_content_length;
  1303. lwsl_info("setting rx_content_length %lld\n",
  1304. (long long)h2n->swsi->http.rx_content_length);
  1305. }
  1306. {
  1307. int n = 0, len;
  1308. char buf[256];
  1309. const unsigned char *c;
  1310. do {
  1311. c = lws_token_to_string(n);
  1312. if (!c) {
  1313. n++;
  1314. continue;
  1315. }
  1316. len = lws_hdr_total_length(h2n->swsi, n);
  1317. if (!len || len > (int)sizeof(buf) - 1) {
  1318. n++;
  1319. continue;
  1320. }
  1321. if (lws_hdr_copy(h2n->swsi, buf, sizeof buf,
  1322. n) < 0) {
  1323. lwsl_info(" %s !oversize!\n",
  1324. (char *)c);
  1325. } else {
  1326. buf[sizeof(buf) - 1] = '\0';
  1327. lwsl_info(" %s = %s\n",
  1328. (char *)c, buf);
  1329. }
  1330. n++;
  1331. } while (c);
  1332. }
  1333. if (h2n->swsi->h2.h2_state == LWS_H2_STATE_HALF_CLOSED_REMOTE ||
  1334. h2n->swsi->h2.h2_state == LWS_H2_STATE_CLOSED) {
  1335. lws_h2_goaway(wsi, H2_ERR_STREAM_CLOSED,
  1336. "Banning service on CLOSED_REMOTE");
  1337. break;
  1338. }
  1339. switch (h2n->swsi->h2.h2_state) {
  1340. case LWS_H2_STATE_IDLE:
  1341. lws_h2_state(h2n->swsi, LWS_H2_STATE_OPEN);
  1342. break;
  1343. case LWS_H2_STATE_OPEN:
  1344. if (h2n->swsi->h2.END_STREAM)
  1345. lws_h2_state(h2n->swsi,
  1346. LWS_H2_STATE_HALF_CLOSED_REMOTE);
  1347. break;
  1348. case LWS_H2_STATE_HALF_CLOSED_LOCAL:
  1349. if (h2n->swsi->h2.END_STREAM)
  1350. lws_h2_state(h2n->swsi, LWS_H2_STATE_CLOSED);
  1351. break;
  1352. }
  1353. #if defined(LWS_WITH_CLIENT)
  1354. if (h2n->swsi->client_mux_substream) {
  1355. lwsl_info("%s: wsi %p: headers: client path (h2 state %s)\n",
  1356. __func__, wsi, h2_state_names[h2n->swsi->h2.h2_state]);
  1357. break;
  1358. }
  1359. #endif
  1360. if (!lws_hdr_total_length(h2n->swsi, WSI_TOKEN_HTTP_COLON_PATH) ||
  1361. !lws_hdr_total_length(h2n->swsi, WSI_TOKEN_HTTP_COLON_METHOD) ||
  1362. !lws_hdr_total_length(h2n->swsi, WSI_TOKEN_HTTP_COLON_SCHEME) ||
  1363. lws_hdr_total_length(h2n->swsi, WSI_TOKEN_HTTP_COLON_STATUS) ||
  1364. lws_hdr_extant(h2n->swsi, WSI_TOKEN_CONNECTION)) {
  1365. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  1366. "Pseudoheader checks");
  1367. break;
  1368. }
  1369. if (lws_hdr_extant(h2n->swsi, WSI_TOKEN_TE)) {
  1370. n = lws_hdr_total_length(h2n->swsi, WSI_TOKEN_TE);
  1371. if (n != 8 ||
  1372. !lws_hdr_simple_ptr(h2n->swsi, WSI_TOKEN_TE) ||
  1373. strncmp(lws_hdr_simple_ptr(h2n->swsi, WSI_TOKEN_TE),
  1374. "trailers", n)) {
  1375. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  1376. "Illegal transfer-encoding");
  1377. break;
  1378. }
  1379. }
  1380. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  1381. lws_http_compression_validate(h2n->swsi);
  1382. #endif
  1383. #if defined(LWS_WITH_SERVER_STATUS)
  1384. wsi->a.vhost->conn_stats.h2_trans++;
  1385. #endif
  1386. p = lws_hdr_simple_ptr(h2n->swsi, WSI_TOKEN_HTTP_COLON_METHOD);
  1387. /*
  1388. * duplicate :path into the individual method uri header
  1389. * index, so that it looks the same as h1 in the ah
  1390. */
  1391. for (n = 0; n < (int)LWS_ARRAY_SIZE(method_names); n++)
  1392. if (p && !strcasecmp(p, method_names[n])) {
  1393. h2n->swsi->http.ah->frag_index[method_index[n]] =
  1394. h2n->swsi->http.ah->frag_index[
  1395. WSI_TOKEN_HTTP_COLON_PATH];
  1396. break;
  1397. }
  1398. lwsl_debug("%s: setting DEF_ACT from 0x%x\n", __func__,
  1399. (unsigned int)h2n->swsi->wsistate);
  1400. lwsi_set_state(h2n->swsi, LRS_DEFERRING_ACTION);
  1401. lws_callback_on_writable(h2n->swsi);
  1402. break;
  1403. case LWS_H2_FRAME_TYPE_DATA:
  1404. lwsl_info("%s: DATA flags 0x%x\n", __func__, h2n->flags);
  1405. if (!h2n->swsi)
  1406. break;
  1407. if (lws_hdr_total_length(h2n->swsi,
  1408. WSI_TOKEN_HTTP_CONTENT_LENGTH) &&
  1409. h2n->swsi->h2.END_STREAM &&
  1410. h2n->swsi->http.rx_content_length &&
  1411. h2n->swsi->http.rx_content_remain) {
  1412. lws_h2_rst_stream(h2n->swsi, H2_ERR_PROTOCOL_ERROR,
  1413. "Not enough rx content");
  1414. break;
  1415. }
  1416. if (h2n->swsi->h2.END_STREAM &&
  1417. h2n->swsi->h2.h2_state == LWS_H2_STATE_OPEN)
  1418. lws_h2_state(h2n->swsi,
  1419. LWS_H2_STATE_HALF_CLOSED_REMOTE);
  1420. if (h2n->swsi->h2.END_STREAM &&
  1421. h2n->swsi->h2.h2_state == LWS_H2_STATE_HALF_CLOSED_LOCAL)
  1422. lws_h2_state(h2n->swsi, LWS_H2_STATE_CLOSED);
  1423. #if defined(LWS_WITH_CLIENT)
  1424. /*
  1425. * client... remote END_STREAM implies we weren't going to
  1426. * send anything else anyway.
  1427. */
  1428. if (h2n->swsi->client_mux_substream &&
  1429. (h2n->flags & LWS_H2_FLAG_END_STREAM)) {
  1430. lwsl_info("%s: %p: DATA: end stream\n",
  1431. __func__, h2n->swsi);
  1432. if (h2n->swsi->h2.h2_state == LWS_H2_STATE_OPEN) {
  1433. lws_h2_state(h2n->swsi,
  1434. LWS_H2_STATE_HALF_CLOSED_REMOTE);
  1435. // lws_h2_rst_stream(h2n->swsi, H2_ERR_NO_ERROR,
  1436. // "client done");
  1437. // if (lws_http_transaction_completed_client(h2n->swsi))
  1438. // lwsl_debug("tx completed returned close\n");
  1439. }
  1440. //if (h2n->swsi->h2.h2_state == LWS_H2_STATE_HALF_CLOSED_LOCAL)
  1441. {
  1442. lws_h2_state(h2n->swsi, LWS_H2_STATE_CLOSED);
  1443. lws_h2_rst_stream(h2n->swsi, H2_ERR_NO_ERROR,
  1444. "client done");
  1445. if (lws_http_transaction_completed_client(h2n->swsi))
  1446. lwsl_debug("tx completed returned close\n");
  1447. }
  1448. }
  1449. #endif
  1450. break;
  1451. case LWS_H2_FRAME_TYPE_PING:
  1452. if (h2n->flags & LWS_H2_FLAG_SETTINGS_ACK)
  1453. lws_validity_confirmed(wsi);
  1454. else {
  1455. /* they're sending us a ping request */
  1456. struct lws_h2_protocol_send *pps =
  1457. lws_h2_new_pps(LWS_H2_PPS_PONG);
  1458. if (!pps)
  1459. return 1;
  1460. lwsl_info("rx ping, preparing pong\n");
  1461. memcpy(pps->u.ping.ping_payload, h2n->ping_payload, 8);
  1462. lws_pps_schedule(wsi, pps);
  1463. }
  1464. break;
  1465. case LWS_H2_FRAME_TYPE_WINDOW_UPDATE:
  1466. /*
  1467. * We only have an unsigned 31-bit (positive) increment possible
  1468. */
  1469. h2n->hpack_e_dep &= ~(1u << 31);
  1470. lwsl_info("WINDOW_UPDATE: sid %u %u (0x%x)\n",
  1471. (unsigned int)h2n->sid,
  1472. (unsigned int)h2n->hpack_e_dep,
  1473. (unsigned int)h2n->hpack_e_dep);
  1474. if (h2n->sid)
  1475. eff_wsi = h2n->swsi;
  1476. if (!eff_wsi) {
  1477. if (h2n->sid > h2n->highest_sid_opened)
  1478. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  1479. "alien sid");
  1480. break; /* ignore */
  1481. }
  1482. if (eff_wsi->a.vhost->options &
  1483. LWS_SERVER_OPTION_H2_JUST_FIX_WINDOW_UPDATE_OVERFLOW &&
  1484. (uint64_t)eff_wsi->txc.tx_cr + (uint64_t)h2n->hpack_e_dep >
  1485. (uint64_t)0x7fffffff)
  1486. h2n->hpack_e_dep = 0x7fffffff - eff_wsi->txc.tx_cr;
  1487. if ((uint64_t)eff_wsi->txc.tx_cr + (uint64_t)h2n->hpack_e_dep >
  1488. (uint64_t)0x7fffffff) {
  1489. if (h2n->sid)
  1490. lws_h2_rst_stream(h2n->swsi,
  1491. H2_ERR_FLOW_CONTROL_ERROR,
  1492. "Flow control exceeded max");
  1493. else
  1494. lws_h2_goaway(wsi, H2_ERR_FLOW_CONTROL_ERROR,
  1495. "Flow control exceeded max");
  1496. break;
  1497. }
  1498. if (!h2n->hpack_e_dep) {
  1499. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  1500. "Zero length window update");
  1501. break;
  1502. }
  1503. n = eff_wsi->txc.tx_cr;
  1504. eff_wsi->txc.tx_cr += h2n->hpack_e_dep;
  1505. lws_wsi_txc_report_manual_txcr_in(eff_wsi,
  1506. (int32_t)h2n->hpack_e_dep);
  1507. lws_wsi_txc_describe(&eff_wsi->txc, "WINDOW_UPDATE in",
  1508. eff_wsi->mux.my_sid);
  1509. if (n <= 0 && eff_wsi->txc.tx_cr <= 0)
  1510. /* it helps, but won't change sendability for anyone */
  1511. break;
  1512. /*
  1513. * It may have changed sendability (depends on SID 0 tx credit
  1514. * too)... for us and any children waiting on us... reassess
  1515. * blockage for all children first
  1516. */
  1517. lws_start_foreach_ll(struct lws *, w, wsi->mux.child_list) {
  1518. lws_callback_on_writable(w);
  1519. } lws_end_foreach_ll(w, mux.sibling_list);
  1520. if (eff_wsi->txc.skint &&
  1521. !lws_wsi_txc_check_skint(&eff_wsi->txc,
  1522. lws_h2_tx_cr_get(eff_wsi)))
  1523. /*
  1524. * This one became un-skint, schedule a writeable
  1525. * callback
  1526. */
  1527. lws_callback_on_writable(eff_wsi);
  1528. break;
  1529. case LWS_H2_FRAME_TYPE_GOAWAY:
  1530. lwsl_notice("GOAWAY: last sid %u, error 0x%08X, string '%s'\n",
  1531. (unsigned int)h2n->goaway_last_sid,
  1532. (unsigned int)h2n->goaway_err, h2n->goaway_str);
  1533. return 1;
  1534. case LWS_H2_FRAME_TYPE_RST_STREAM:
  1535. lwsl_info("LWS_H2_FRAME_TYPE_RST_STREAM: sid %u: reason 0x%x\n",
  1536. (unsigned int)h2n->sid,
  1537. (unsigned int)h2n->hpack_e_dep);
  1538. break;
  1539. case LWS_H2_FRAME_TYPE_COUNT: /* IGNORING FRAME */
  1540. break;
  1541. }
  1542. return 0;
  1543. }
  1544. /*
  1545. * This may want to send something on the network wsi, which may be in the
  1546. * middle of a partial send. PPS sends are OK because they are queued to
  1547. * go through the WRITABLE handler already.
  1548. *
  1549. * The read parser for the network wsi has no choice but to parse its stream
  1550. * anyway, because otherwise it will not be able to get tx credit window
  1551. * messages.
  1552. *
  1553. * Therefore if we will send non-PPS, ie, lws_http_action() for a stream
  1554. * wsi, we must change its state and handle it as a priority in the
  1555. * POLLOUT handler instead of writing it here.
  1556. *
  1557. * About closing... for the main network wsi, it should return nonzero to
  1558. * close it all. If it needs to close an swsi, it can do it here.
  1559. */
  1560. int
  1561. lws_h2_parser(struct lws *wsi, unsigned char *in, lws_filepos_t inlen,
  1562. lws_filepos_t *inused)
  1563. {
  1564. struct lws_h2_netconn *h2n = wsi->h2.h2n;
  1565. struct lws_h2_protocol_send *pps;
  1566. unsigned char c, *oldin = in;
  1567. int n, m;
  1568. if (!h2n)
  1569. goto fail;
  1570. while (inlen--) {
  1571. c = *in++;
  1572. // lwsl_notice("%s: 0x%x\n", __func__, c);
  1573. switch (lwsi_state(wsi)) {
  1574. case LRS_H2_AWAIT_PREFACE:
  1575. if (preface[h2n->count++] != c)
  1576. goto fail;
  1577. if (preface[h2n->count])
  1578. break;
  1579. lwsl_info("http2: %p: established\n", wsi);
  1580. lwsi_set_state(wsi, LRS_H2_AWAIT_SETTINGS);
  1581. lws_validity_confirmed(wsi);
  1582. h2n->count = 0;
  1583. wsi->txc.tx_cr = 65535;
  1584. /*
  1585. * we must send a settings frame -- empty one is OK...
  1586. * that must be the first thing sent by server
  1587. * and the peer must send a SETTINGS with ACK flag...
  1588. */
  1589. pps = lws_h2_new_pps(LWS_H2_PPS_MY_SETTINGS);
  1590. if (!pps)
  1591. goto fail;
  1592. lws_pps_schedule(wsi, pps);
  1593. break;
  1594. case LRS_H2_WAITING_TO_SEND_HEADERS:
  1595. case LRS_ESTABLISHED:
  1596. case LRS_H2_AWAIT_SETTINGS:
  1597. if (h2n->frame_state != LWS_H2_FRAME_HEADER_LENGTH)
  1598. goto try_frame_start;
  1599. /*
  1600. * post-header, preamble / payload / padding part
  1601. */
  1602. h2n->count++;
  1603. if (h2n->flags & LWS_H2_FLAG_PADDED &&
  1604. !h2n->pad_length) {
  1605. /*
  1606. * Get the padding count... actual padding is
  1607. * at the end of the frame.
  1608. */
  1609. h2n->padding = c;
  1610. h2n->pad_length = 1;
  1611. h2n->preamble++;
  1612. if (h2n->padding > h2n->length - 1)
  1613. lws_h2_goaway(wsi,
  1614. H2_ERR_PROTOCOL_ERROR,
  1615. "execssive padding");
  1616. break; /* we consumed this */
  1617. }
  1618. if (h2n->flags & LWS_H2_FLAG_PRIORITY &&
  1619. !h2n->collected_priority) {
  1620. /* going to be 5 preamble bytes */
  1621. lwsl_debug("PRIORITY FLAG: 0x%x\n", c);
  1622. if (h2n->preamble++ - h2n->pad_length < 4) {
  1623. h2n->dep = ((h2n->dep) << 8) | c;
  1624. break; /* we consumed this */
  1625. }
  1626. h2n->weight_temp = c;
  1627. h2n->collected_priority = 1;
  1628. lwsl_debug("PRI FL: dep 0x%x, weight 0x%02X\n",
  1629. (unsigned int)h2n->dep,
  1630. h2n->weight_temp);
  1631. break; /* we consumed this */
  1632. }
  1633. if (h2n->padding && h2n->count >
  1634. (h2n->length - h2n->padding)) {
  1635. if (c) {
  1636. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  1637. "nonzero padding");
  1638. break;
  1639. }
  1640. goto frame_end;
  1641. }
  1642. /* applies to wsi->h2.swsi which may be wsi */
  1643. switch(h2n->type) {
  1644. case LWS_H2_FRAME_TYPE_SETTINGS:
  1645. n = (h2n->count - 1 - h2n->preamble) %
  1646. LWS_H2_SETTINGS_LEN;
  1647. h2n->one_setting[n] = c;
  1648. if (n != LWS_H2_SETTINGS_LEN - 1)
  1649. break;
  1650. lws_h2_settings(wsi, &h2n->peer_set,
  1651. h2n->one_setting,
  1652. LWS_H2_SETTINGS_LEN);
  1653. break;
  1654. case LWS_H2_FRAME_TYPE_CONTINUATION:
  1655. case LWS_H2_FRAME_TYPE_HEADERS:
  1656. if (!h2n->swsi)
  1657. break;
  1658. if (lws_hpack_interpret(h2n->swsi, c)) {
  1659. lwsl_info("%s: hpack failed\n",
  1660. __func__);
  1661. goto fail;
  1662. }
  1663. break;
  1664. case LWS_H2_FRAME_TYPE_GOAWAY:
  1665. switch (h2n->inside++) {
  1666. case 0:
  1667. case 1:
  1668. case 2:
  1669. case 3:
  1670. h2n->goaway_last_sid <<= 8;
  1671. h2n->goaway_last_sid |= c;
  1672. h2n->goaway_str[0] = '\0';
  1673. break;
  1674. case 4:
  1675. case 5:
  1676. case 6:
  1677. case 7:
  1678. h2n->goaway_err <<= 8;
  1679. h2n->goaway_err |= c;
  1680. break;
  1681. default:
  1682. if (h2n->inside - 9 <
  1683. sizeof(h2n->goaway_str) - 1)
  1684. h2n->goaway_str[
  1685. h2n->inside - 9] = c;
  1686. h2n->goaway_str[
  1687. sizeof(h2n->goaway_str) - 1] = '\0';
  1688. break;
  1689. }
  1690. break;
  1691. case LWS_H2_FRAME_TYPE_DATA:
  1692. lwsl_info("%s: LWS_H2_FRAME_TYPE_DATA: fl 0x%x\n",
  1693. __func__, h2n->flags);
  1694. /*
  1695. * let the network wsi live a bit longer if
  1696. * subs are active... our frame may take a long
  1697. * time to chew through
  1698. */
  1699. if (!wsi->immortal_substream_count)
  1700. lws_set_timeout(wsi,
  1701. PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE,
  1702. wsi->a.vhost->keepalive_timeout ?
  1703. wsi->a.vhost->keepalive_timeout : 31);
  1704. if (!h2n->swsi)
  1705. break;
  1706. if (lws_buflist_next_segment_len(
  1707. &h2n->swsi->buflist, NULL))
  1708. lwsl_info("%s: substream has pending\n",
  1709. __func__);
  1710. if (lwsi_role_http(h2n->swsi) &&
  1711. lwsi_state(h2n->swsi) == LRS_ESTABLISHED) {
  1712. lwsi_set_state(h2n->swsi, LRS_BODY);
  1713. lwsl_info("%s: swsi %p to LRS_BODY\n",
  1714. __func__, h2n->swsi);
  1715. }
  1716. if (lws_hdr_total_length(h2n->swsi,
  1717. WSI_TOKEN_HTTP_CONTENT_LENGTH) &&
  1718. h2n->swsi->http.rx_content_length &&
  1719. h2n->swsi->http.rx_content_remain <
  1720. inlen + 1 && /* last */
  1721. h2n->inside < h2n->length) {
  1722. lwsl_warn("%s: rem %d, inlen %d\n",
  1723. __func__,
  1724. (int)h2n->swsi->http.rx_content_remain,
  1725. (int)inlen + 1);
  1726. /* unread data in frame */
  1727. lws_h2_goaway(wsi,
  1728. H2_ERR_PROTOCOL_ERROR,
  1729. "More rx than content_length told");
  1730. break;
  1731. }
  1732. /*
  1733. * We operate on a frame. The RX we have at
  1734. * hand may exceed the current frame.
  1735. */
  1736. n = (int)inlen + 1;
  1737. if (n > (int)(h2n->length - h2n->count + 1)) {
  1738. n = h2n->length - h2n->count + 1;
  1739. lwsl_debug("---- restricting len to %d "
  1740. "vs %ld\n", n, (long)inlen + 1);
  1741. }
  1742. #if defined(LWS_WITH_CLIENT)
  1743. if (h2n->swsi->client_mux_substream) {
  1744. if (!h2n->swsi->a.protocol) {
  1745. lwsl_err("%s: swsi %p doesn't have protocol\n",
  1746. __func__, h2n->swsi);
  1747. m = 1;
  1748. } else {
  1749. h2n->swsi->txc.peer_tx_cr_est -= n;
  1750. wsi->txc.peer_tx_cr_est -= n;
  1751. lws_wsi_txc_describe(&h2n->swsi->txc,
  1752. __func__,
  1753. h2n->swsi->mux.my_sid);
  1754. m = user_callback_handle_rxflow(
  1755. h2n->swsi->a.protocol->callback,
  1756. h2n->swsi,
  1757. LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
  1758. h2n->swsi->user_space,
  1759. in - 1, n);
  1760. }
  1761. in += n - 1;
  1762. h2n->inside += n;
  1763. h2n->count += n - 1;
  1764. inlen -= n - 1;
  1765. if (m) {
  1766. lwsl_info("RECEIVE_CLIENT_HTTP "
  1767. "closed it\n");
  1768. goto close_swsi_and_return;
  1769. }
  1770. break;
  1771. }
  1772. #endif
  1773. if (lwsi_state(h2n->swsi) == LRS_DEFERRING_ACTION) {
  1774. m = lws_buflist_append_segment(
  1775. &h2n->swsi->buflist, in - 1, n);
  1776. if (m < 0)
  1777. return -1;
  1778. if (m) {
  1779. struct lws_context_per_thread *pt;
  1780. pt = &wsi->a.context->pt[(int)wsi->tsi];
  1781. lwsl_debug("%s: added %p to rxflow list\n",
  1782. __func__, wsi);
  1783. lws_dll2_add_head(
  1784. &h2n->swsi->dll_buflist,
  1785. &pt->dll_buflist_owner);
  1786. }
  1787. in += n - 1;
  1788. h2n->inside += n;
  1789. h2n->count += n - 1;
  1790. inlen -= n - 1;
  1791. lwsl_debug("%s: deferred %d\n", __func__, n);
  1792. goto do_windows;
  1793. }
  1794. h2n->swsi->outer_will_close = 1;
  1795. /*
  1796. * choose the length for this go so that we end at
  1797. * the frame boundary, in the case there is already
  1798. * more waiting leave it for next time around
  1799. */
  1800. n = lws_read_h1(h2n->swsi, in - 1, n);
  1801. // lwsl_notice("%s: lws_read_h1 %d\n", __func__, n);
  1802. h2n->swsi->outer_will_close = 0;
  1803. /*
  1804. * can return 0 in POST body with
  1805. * content len exhausted somehow.
  1806. */
  1807. if (n < 0 ||
  1808. (!n && !lws_buflist_next_segment_len(
  1809. &wsi->buflist, NULL))) {
  1810. lwsl_info("%s: lws_read_h1 told %d %u / %u\n",
  1811. __func__, n,
  1812. (unsigned int)h2n->count,
  1813. (unsigned int)h2n->length);
  1814. in += h2n->length - h2n->count;
  1815. h2n->inside = h2n->length;
  1816. h2n->count = h2n->length - 1;
  1817. //if (n < 0)
  1818. // goto already_closed_swsi;
  1819. goto close_swsi_and_return;
  1820. }
  1821. inlen -= n - 1;
  1822. in += n - 1;
  1823. h2n->inside += n;
  1824. h2n->count += n - 1;
  1825. h2n->swsi->txc.peer_tx_cr_est -= n;
  1826. wsi->txc.peer_tx_cr_est -= n;
  1827. do_windows:
  1828. #if defined(LWS_WITH_CLIENT)
  1829. if (!(h2n->swsi->flags & LCCSCF_H2_MANUAL_RXFLOW))
  1830. #endif
  1831. {
  1832. /*
  1833. * The default behaviour is we just keep
  1834. * cranking the other side's tx credit
  1835. * back up, for simple bulk transfer as
  1836. * fast as we can take it
  1837. */
  1838. m = n; //(2 * h2n->length) + 65536;
  1839. /* update both the stream and nwsi */
  1840. lws_h2_update_peer_txcredit_thresh(h2n->swsi,
  1841. h2n->sid, m, m);
  1842. }
  1843. #if defined(LWS_WITH_CLIENT)
  1844. else {
  1845. /*
  1846. * If he's handling it himself, only
  1847. * repair the nwsi credit but allow the
  1848. * stream credit to run down until the
  1849. * user code deals with it
  1850. */
  1851. lws_h2_update_peer_txcredit(wsi, 0, n);
  1852. h2n->swsi->txc.manual = 1;
  1853. }
  1854. #endif
  1855. break;
  1856. case LWS_H2_FRAME_TYPE_PRIORITY:
  1857. if (h2n->count <= 4) {
  1858. h2n->dep <<= 8;
  1859. h2n->dep |= c;
  1860. break;
  1861. }
  1862. h2n->weight_temp = c;
  1863. lwsl_info("PRIORITY: dep 0x%x, weight 0x%02X\n",
  1864. (unsigned int)h2n->dep, h2n->weight_temp);
  1865. if ((h2n->dep & ~(1u << 31)) == h2n->sid) {
  1866. lws_h2_goaway(wsi, H2_ERR_PROTOCOL_ERROR,
  1867. "cant depend on own sid");
  1868. break;
  1869. }
  1870. break;
  1871. case LWS_H2_FRAME_TYPE_RST_STREAM:
  1872. h2n->hpack_e_dep <<= 8;
  1873. h2n->hpack_e_dep |= c;
  1874. break;
  1875. case LWS_H2_FRAME_TYPE_PUSH_PROMISE:
  1876. break;
  1877. case LWS_H2_FRAME_TYPE_PING:
  1878. if (h2n->flags & LWS_H2_FLAG_SETTINGS_ACK) { // ack
  1879. } else { /* they're sending us a ping request */
  1880. if (h2n->count > 8)
  1881. return 1;
  1882. h2n->ping_payload[h2n->count - 1] = c;
  1883. }
  1884. break;
  1885. case LWS_H2_FRAME_TYPE_WINDOW_UPDATE:
  1886. h2n->hpack_e_dep <<= 8;
  1887. h2n->hpack_e_dep |= c;
  1888. break;
  1889. case LWS_H2_FRAME_TYPE_COUNT: /* IGNORING FRAME */
  1890. break;
  1891. default:
  1892. lwsl_notice("%s: unhandled frame type %d\n",
  1893. __func__, h2n->type);
  1894. goto fail;
  1895. }
  1896. frame_end:
  1897. if (h2n->count > h2n->length) {
  1898. lwsl_notice("%s: count > length %u %u\n",
  1899. __func__, (unsigned int)h2n->count,
  1900. (unsigned int)h2n->length);
  1901. goto fail;
  1902. }
  1903. if (h2n->count != h2n->length)
  1904. break;
  1905. /*
  1906. * end of frame just happened
  1907. */
  1908. if (lws_h2_parse_end_of_frame(wsi))
  1909. goto fail;
  1910. break;
  1911. try_frame_start:
  1912. if (h2n->frame_state <= 8) {
  1913. switch (h2n->frame_state++) {
  1914. case 0:
  1915. h2n->pad_length = 0;
  1916. h2n->collected_priority = 0;
  1917. h2n->padding = 0;
  1918. h2n->preamble = 0;
  1919. h2n->length = c;
  1920. h2n->inside = 0;
  1921. break;
  1922. case 1:
  1923. case 2:
  1924. h2n->length <<= 8;
  1925. h2n->length |= c;
  1926. break;
  1927. case 3:
  1928. h2n->type = c;
  1929. break;
  1930. case 4:
  1931. h2n->flags = c;
  1932. break;
  1933. case 5:
  1934. case 6:
  1935. case 7:
  1936. case 8:
  1937. h2n->sid <<= 8;
  1938. h2n->sid |= c;
  1939. break;
  1940. }
  1941. }
  1942. if (h2n->frame_state == LWS_H2_FRAME_HEADER_LENGTH)
  1943. if (lws_h2_parse_frame_header(wsi))
  1944. goto fail;
  1945. break;
  1946. default:
  1947. break;
  1948. }
  1949. }
  1950. *inused = in - oldin;
  1951. return 0;
  1952. close_swsi_and_return:
  1953. lws_close_free_wsi(h2n->swsi, 0, "close_swsi_and_return");
  1954. h2n->swsi = NULL;
  1955. h2n->frame_state = 0;
  1956. h2n->count = 0;
  1957. // already_closed_swsi:
  1958. *inused = in - oldin;
  1959. return 2;
  1960. fail:
  1961. *inused = in - oldin;
  1962. return 1;
  1963. }
  1964. #if defined(LWS_WITH_CLIENT)
  1965. int
  1966. lws_h2_client_handshake(struct lws *wsi)
  1967. {
  1968. struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
  1969. uint8_t *buf, *start, *p, *p1, *end;
  1970. char *meth = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_METHOD),
  1971. *uri = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI), *simp;
  1972. struct lws *nwsi = lws_get_network_wsi(wsi);
  1973. int n, m;
  1974. /*
  1975. * The identifier of a newly established stream MUST be numerically
  1976. * greater than all streams that the initiating endpoint has opened or
  1977. * reserved. This governs streams that are opened using a HEADERS frame
  1978. * and streams that are reserved using PUSH_PROMISE. An endpoint that
  1979. * receives an unexpected stream identifier MUST respond with a
  1980. * connection error (Section 5.4.1) of type PROTOCOL_ERROR.
  1981. */
  1982. int sid = nwsi->h2.h2n->highest_sid_opened + 2;
  1983. lwsl_debug("%s\n", __func__);
  1984. /*
  1985. * We MUST allocate our sid here at the point we're about to send the
  1986. * stream open. It's because we don't know the order in which multiple
  1987. * open streams will send their headers... in h2, sending the headers
  1988. * is the point the stream is opened. The peer requires that we only
  1989. * open streams in ascending sid order
  1990. */
  1991. wsi->mux.my_sid = nwsi->h2.h2n->highest_sid_opened = sid;
  1992. lwsl_info("%s: wsi %p: assigning SID %d at header send\n", __func__, wsi, sid);
  1993. lwsl_info("%s: CLIENT_WAITING_TO_SEND_HEADERS: pollout (sid %d)\n",
  1994. __func__, wsi->mux.my_sid);
  1995. p = start = buf = pt->serv_buf + LWS_PRE;
  1996. end = start + (wsi->a.context->pt_serv_buf_size / 2) - LWS_PRE - 1;
  1997. /* it's time for us to send our client stream headers */
  1998. if (!meth)
  1999. meth = "GET";
  2000. if (lws_add_http_header_by_token(wsi,
  2001. WSI_TOKEN_HTTP_COLON_METHOD,
  2002. (unsigned char *)meth,
  2003. (int)strlen(meth), &p, end))
  2004. goto fail_length;
  2005. if (lws_add_http_header_by_token(wsi,
  2006. WSI_TOKEN_HTTP_COLON_SCHEME,
  2007. (unsigned char *)"https", 5,
  2008. &p, end))
  2009. goto fail_length;
  2010. n = lws_hdr_total_length(wsi, _WSI_TOKEN_CLIENT_URI);
  2011. if (n && lws_add_http_header_by_token(wsi,
  2012. WSI_TOKEN_HTTP_COLON_PATH,
  2013. (unsigned char *)uri, n, &p, end))
  2014. goto fail_length;
  2015. n = lws_hdr_total_length(wsi, _WSI_TOKEN_CLIENT_ORIGIN);
  2016. simp = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN);
  2017. if (n && simp && lws_add_http_header_by_token(wsi,
  2018. WSI_TOKEN_HTTP_COLON_AUTHORITY,
  2019. (unsigned char *)simp, n, &p, end))
  2020. goto fail_length;
  2021. n = lws_hdr_total_length(wsi, _WSI_TOKEN_CLIENT_HOST);
  2022. simp = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST);
  2023. if (!wsi->client_h2_alpn && n && simp &&
  2024. lws_add_http_header_by_token(wsi, WSI_TOKEN_HOST,
  2025. (unsigned char *)simp, n, &p, end))
  2026. goto fail_length;
  2027. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_USER_AGENT,
  2028. (unsigned char *)"lwsss", 5,
  2029. &p, end))
  2030. goto fail_length;
  2031. if (wsi->flags & LCCSCF_HTTP_MULTIPART_MIME) {
  2032. p1 = lws_http_multipart_headers(wsi, p);
  2033. if (!p1)
  2034. goto fail_length;
  2035. p = p1;
  2036. }
  2037. if (wsi->flags & LCCSCF_HTTP_X_WWW_FORM_URLENCODED) {
  2038. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
  2039. (unsigned char *)"application/x-www-form-urlencoded",
  2040. 33, &p, end))
  2041. goto fail_length;
  2042. lws_client_http_body_pending(wsi, 1);
  2043. }
  2044. /* give userland a chance to append, eg, cookies */
  2045. if (wsi->a.protocol->callback(wsi,
  2046. LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
  2047. wsi->user_space, &p, (end - p) - 12))
  2048. goto fail_length;
  2049. if (lws_finalize_http_header(wsi, &p, end))
  2050. goto fail_length;
  2051. #if defined(LWS_WITH_DETAILED_LATENCY)
  2052. wsi->detlat.earliest_write_req_pre_write = lws_now_usecs();
  2053. #endif
  2054. m = LWS_WRITE_HTTP_HEADERS;
  2055. #if defined(LWS_WITH_CLIENT)
  2056. /* below is not needed in spec, indeed it destroys the long poll
  2057. * feature, but required by nghttp2 */
  2058. if ((wsi->flags & LCCSCF_H2_QUIRK_NGHTTP2_END_STREAM) &&
  2059. !(wsi->client_http_body_pending))
  2060. m |= LWS_WRITE_H2_STREAM_END;
  2061. #endif
  2062. // lwsl_hexdump_notice(start, p - start);
  2063. n = lws_write(wsi, start, p - start, m);
  2064. if (n != (p - start)) {
  2065. lwsl_err("_write returned %d from %ld\n", n,
  2066. (long)(p - start));
  2067. return -1;
  2068. }
  2069. /*
  2070. * Normally let's charge up the peer tx credit a bit. But if
  2071. * MANUAL_REFLOW is set, just set it to the initial credit given in
  2072. * the client create info
  2073. */
  2074. n = 4 * 65536;
  2075. if (wsi->flags & LCCSCF_H2_MANUAL_RXFLOW) {
  2076. n = wsi->txc.manual_initial_tx_credit;
  2077. wsi->txc.manual = 1;
  2078. }
  2079. if (lws_h2_update_peer_txcredit(wsi, wsi->mux.my_sid, n))
  2080. return 1;
  2081. lws_h2_state(wsi, LWS_H2_STATE_OPEN);
  2082. lwsi_set_state(wsi, LRS_ESTABLISHED);
  2083. if (wsi->flags & LCCSCF_HTTP_MULTIPART_MIME)
  2084. lws_callback_on_writable(wsi);
  2085. return 0;
  2086. fail_length:
  2087. lwsl_err("Client hdrs too long: incr context info.pt_serv_buf_size\n");
  2088. return -1;
  2089. }
  2090. #endif
  2091. #if defined(LWS_ROLE_WS) && defined(LWS_WITH_SERVER)
  2092. int
  2093. lws_h2_ws_handshake(struct lws *wsi)
  2094. {
  2095. uint8_t buf[LWS_PRE + 2048], *p = buf + LWS_PRE, *start = p,
  2096. *end = &buf[sizeof(buf) - 1];
  2097. const struct lws_http_mount *hit;
  2098. const char * uri_ptr;
  2099. int n, m;
  2100. if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
  2101. return -1;
  2102. if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL) > 64)
  2103. return -1;
  2104. if (wsi->proxied_ws_parent && wsi->child_list) {
  2105. if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL)) {
  2106. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_PROTOCOL,
  2107. (uint8_t *)lws_hdr_simple_ptr(wsi,
  2108. WSI_TOKEN_PROTOCOL),
  2109. (int)strlen(lws_hdr_simple_ptr(wsi,
  2110. WSI_TOKEN_PROTOCOL)),
  2111. &p, end))
  2112. return -1;
  2113. }
  2114. } else {
  2115. /* we can only return the protocol header if:
  2116. * - one came in, and ... */
  2117. if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL) &&
  2118. /* - it is not an empty string */
  2119. wsi->a.protocol->name && wsi->a.protocol->name[0]) {
  2120. #if defined(LWS_WITH_SECURE_STREAMS) && defined(LWS_WITH_SERVER)
  2121. /*
  2122. * This is the h2 version of server-ws.c understanding that it
  2123. * did the ws upgrade on a ss server object, therefore it needs
  2124. * to pass back to the peer the policy ws-protocol name, not
  2125. * the generic ss-ws.c protocol name
  2126. */
  2127. if (wsi->a.vhost && wsi->a.vhost->ss_handle &&
  2128. wsi->a.vhost->ss_handle->policy->u.http.u.ws.subprotocol) {
  2129. lws_ss_handle_t *h =
  2130. (lws_ss_handle_t *)wsi->a.opaque_user_data;
  2131. lwsl_notice("%s: Server SS %p .wsi %p switching to ws protocol\n",
  2132. __func__, h, h->wsi);
  2133. wsi->a.protocol = &protocol_secstream_ws;
  2134. /*
  2135. * inform the SS user code that this has done a one-way
  2136. * upgrade to some other protocol... it will likely
  2137. * want to treat subsequent payloads differently
  2138. */
  2139. lws_ss_event_helper(h, LWSSSCS_SERVER_UPGRADE);
  2140. lws_mux_mark_immortal(wsi);
  2141. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_PROTOCOL,
  2142. (unsigned char *)wsi->a.vhost->ss_handle->policy->
  2143. u.http.u.ws.subprotocol,
  2144. (int)strlen(wsi->a.vhost->ss_handle->policy->
  2145. u.http.u.ws.subprotocol), &p, end))
  2146. return -1;
  2147. } else
  2148. #endif
  2149. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_PROTOCOL,
  2150. (unsigned char *)wsi->a.protocol->name,
  2151. (int)strlen(wsi->a.protocol->name), &p, end))
  2152. return -1;
  2153. }
  2154. }
  2155. if (lws_finalize_http_header(wsi, &p, end))
  2156. return -1;
  2157. m = lws_ptr_diff(p, start);
  2158. // lwsl_hexdump_notice(start, m);
  2159. n = lws_write(wsi, start, m, LWS_WRITE_HTTP_HEADERS);
  2160. if (n != m) {
  2161. lwsl_err("_write returned %d from %d\n", n, m);
  2162. return -1;
  2163. }
  2164. /*
  2165. * alright clean up, set our state to generic ws established, the
  2166. * mode / state of the nwsi will get the h2 processing done.
  2167. */
  2168. lwsi_set_state(wsi, LRS_ESTABLISHED);
  2169. wsi->lws_rx_parse_state = 0; // ==LWS_RXPS_NEW;
  2170. uri_ptr = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_PATH);
  2171. n = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COLON_PATH);
  2172. hit = lws_find_mount(wsi, uri_ptr, n);
  2173. if (hit && hit->cgienv &&
  2174. wsi->a.protocol->callback(wsi, LWS_CALLBACK_HTTP_PMO, wsi->user_space,
  2175. (void *)hit->cgienv, 0))
  2176. return 1;
  2177. lws_validity_confirmed(wsi);
  2178. return 0;
  2179. }
  2180. #endif
  2181. int
  2182. lws_read_h2(struct lws *wsi, unsigned char *buf, lws_filepos_t len)
  2183. {
  2184. unsigned char *oldbuf = buf;
  2185. lws_filepos_t body_chunk_len;
  2186. // lwsl_notice("%s: h2 path: wsistate 0x%x len %d\n", __func__,
  2187. // wsi->wsistate, (int)len);
  2188. /*
  2189. * wsi here is always the network connection wsi, not a stream
  2190. * wsi. Once we unpicked the framing we will find the right
  2191. * swsi and make it the target of the frame.
  2192. *
  2193. * If it's ws over h2, the nwsi will get us here to do the h2
  2194. * processing, and that will call us back with the swsi +
  2195. * ESTABLISHED state for the inner payload, handled in a later
  2196. * case.
  2197. */
  2198. while (len) {
  2199. int m;
  2200. /*
  2201. * we were accepting input but now we stopped doing so
  2202. */
  2203. if (lws_is_flowcontrolled(wsi)) {
  2204. lws_rxflow_cache(wsi, buf, 0, (int)len);
  2205. buf += len;
  2206. break;
  2207. }
  2208. /*
  2209. * lws_h2_parser() may send something; when it gets the
  2210. * whole frame, it will want to perform some action
  2211. * involving a reply. But we may be in a partial send
  2212. * situation on the network wsi...
  2213. *
  2214. * Even though we may be in a partial send and unable to
  2215. * send anything new, we still have to parse the network
  2216. * wsi in order to gain tx credit to send, which is
  2217. * potentially necessary to clear the old partial send.
  2218. *
  2219. * ALL network wsi-specific frames are sent by PPS
  2220. * already, these are sent as a priority on the writable
  2221. * handler, and so respect partial sends. The only
  2222. * problem is when a stream wsi wants to send an, eg,
  2223. * reply headers frame in response to the parsing
  2224. * we will do now... the *stream wsi* must stall in a
  2225. * different state until it is able to do so from a
  2226. * priority on the WRITABLE callback, same way that
  2227. * file transfers operate.
  2228. */
  2229. m = lws_h2_parser(wsi, buf, len, &body_chunk_len);
  2230. if (m && m != 2) {
  2231. lwsl_debug("%s: http2_parser bail: %d\n", __func__, m);
  2232. lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS,
  2233. "lws_read_h2 bail");
  2234. return -1;
  2235. }
  2236. if (m == 2) {
  2237. /* swsi has been closed */
  2238. buf += body_chunk_len;
  2239. break;
  2240. }
  2241. buf += body_chunk_len;
  2242. len -= body_chunk_len;
  2243. }
  2244. return lws_ptr_diff(buf, oldbuf);
  2245. }
  2246. int
  2247. lws_h2_client_stream_long_poll_rxonly(struct lws *wsi)
  2248. {
  2249. if (!wsi->mux_substream)
  2250. return 1;
  2251. /*
  2252. * Elect to send an empty DATA with END_STREAM, to force the stream
  2253. * into HALF_CLOSED LOCAL
  2254. */
  2255. wsi->h2.long_poll = 1;
  2256. wsi->h2.send_END_STREAM = 1;
  2257. // lws_header_table_detach(wsi, 0);
  2258. lws_callback_on_writable(wsi);
  2259. return 0;
  2260. }