http2.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef USE_NGHTTP2
  24. #include <nghttp2/nghttp2.h>
  25. #include "urldata.h"
  26. #include "http2.h"
  27. #include "http.h"
  28. #include "sendf.h"
  29. #include "select.h"
  30. #include "curl_base64.h"
  31. #include "strcase.h"
  32. #include "multiif.h"
  33. #include "url.h"
  34. #include "connect.h"
  35. #include "strtoofft.h"
  36. #include "strdup.h"
  37. #include "dynbuf.h"
  38. /* The last 3 #include files should be in this order */
  39. #include "curl_printf.h"
  40. #include "curl_memory.h"
  41. #include "memdebug.h"
  42. #define H2_BUFSIZE 32768
  43. #if (NGHTTP2_VERSION_NUM < 0x010c00)
  44. #error too old nghttp2 version, upgrade!
  45. #endif
  46. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  47. #define nghttp2_session_callbacks_set_error_callback(x,y)
  48. #endif
  49. #if (NGHTTP2_VERSION_NUM >= 0x010c00)
  50. #define NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE 1
  51. #endif
  52. #define HTTP2_HUGE_WINDOW_SIZE (32 * 1024 * 1024) /* 32 MB */
  53. #ifdef DEBUG_HTTP2
  54. #define H2BUGF(x) x
  55. #else
  56. #define H2BUGF(x) do { } while(0)
  57. #endif
  58. static ssize_t http2_recv(struct connectdata *conn, int sockindex,
  59. char *mem, size_t len, CURLcode *err);
  60. static bool http2_connisdead(struct connectdata *conn);
  61. static int h2_session_send(struct Curl_easy *data,
  62. nghttp2_session *h2);
  63. static int h2_process_pending_input(struct connectdata *conn,
  64. struct http_conn *httpc,
  65. CURLcode *err);
  66. /*
  67. * Curl_http2_init_state() is called when the easy handle is created and
  68. * allows for HTTP/2 specific init of state.
  69. */
  70. void Curl_http2_init_state(struct UrlState *state)
  71. {
  72. state->stream_weight = NGHTTP2_DEFAULT_WEIGHT;
  73. }
  74. /*
  75. * Curl_http2_init_userset() is called when the easy handle is created and
  76. * allows for HTTP/2 specific user-set fields.
  77. */
  78. void Curl_http2_init_userset(struct UserDefined *set)
  79. {
  80. set->stream_weight = NGHTTP2_DEFAULT_WEIGHT;
  81. }
  82. static int http2_perform_getsock(const struct connectdata *conn,
  83. curl_socket_t *sock)
  84. {
  85. const struct http_conn *c = &conn->proto.httpc;
  86. struct SingleRequest *k = &conn->data->req;
  87. int bitmap = GETSOCK_BLANK;
  88. sock[0] = conn->sock[FIRSTSOCKET];
  89. /* in a HTTP/2 connection we can basically always get a frame so we should
  90. always be ready for one */
  91. bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
  92. /* we're still uploading or the HTTP/2 layer wants to send data */
  93. if(((k->keepon & (KEEP_SEND|KEEP_SEND_PAUSE)) == KEEP_SEND) ||
  94. nghttp2_session_want_write(c->h2))
  95. bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
  96. return bitmap;
  97. }
  98. static int http2_getsock(struct connectdata *conn,
  99. curl_socket_t *socks)
  100. {
  101. return http2_perform_getsock(conn, socks);
  102. }
  103. /*
  104. * http2_stream_free() free HTTP2 stream related data
  105. */
  106. static void http2_stream_free(struct HTTP *http)
  107. {
  108. if(http) {
  109. Curl_dyn_free(&http->header_recvbuf);
  110. for(; http->push_headers_used > 0; --http->push_headers_used) {
  111. free(http->push_headers[http->push_headers_used - 1]);
  112. }
  113. free(http->push_headers);
  114. http->push_headers = NULL;
  115. }
  116. }
  117. /*
  118. * Disconnects *a* connection used for HTTP/2. It might be an old one from the
  119. * connection cache and not the "main" one. Don't touch the easy handle!
  120. */
  121. static CURLcode http2_disconnect(struct connectdata *conn,
  122. bool dead_connection)
  123. {
  124. struct http_conn *c = &conn->proto.httpc;
  125. (void)dead_connection;
  126. H2BUGF(infof(conn->data, "HTTP/2 DISCONNECT starts now\n"));
  127. nghttp2_session_del(c->h2);
  128. Curl_safefree(c->inbuf);
  129. H2BUGF(infof(conn->data, "HTTP/2 DISCONNECT done\n"));
  130. return CURLE_OK;
  131. }
  132. /*
  133. * The server may send us data at any point (e.g. PING frames). Therefore,
  134. * we cannot assume that an HTTP/2 socket is dead just because it is readable.
  135. *
  136. * Instead, if it is readable, run Curl_connalive() to peek at the socket
  137. * and distinguish between closed and data.
  138. */
  139. static bool http2_connisdead(struct connectdata *conn)
  140. {
  141. int sval;
  142. bool dead = TRUE;
  143. if(conn->bits.close)
  144. return TRUE;
  145. sval = SOCKET_READABLE(conn->sock[FIRSTSOCKET], 0);
  146. if(sval == 0) {
  147. /* timeout */
  148. dead = FALSE;
  149. }
  150. else if(sval & CURL_CSELECT_ERR) {
  151. /* socket is in an error state */
  152. dead = TRUE;
  153. }
  154. else if(sval & CURL_CSELECT_IN) {
  155. /* readable with no error. could still be closed */
  156. dead = !Curl_connalive(conn);
  157. if(!dead) {
  158. /* This happens before we've sent off a request and the connection is
  159. not in use by any other transfer, there shouldn't be any data here,
  160. only "protocol frames" */
  161. CURLcode result;
  162. struct http_conn *httpc = &conn->proto.httpc;
  163. ssize_t nread = -1;
  164. if(httpc->recv_underlying)
  165. /* if called "too early", this pointer isn't setup yet! */
  166. nread = ((Curl_recv *)httpc->recv_underlying)(
  167. conn, FIRSTSOCKET, httpc->inbuf, H2_BUFSIZE, &result);
  168. if(nread != -1) {
  169. infof(conn->data,
  170. "%d bytes stray data read before trying h2 connection\n",
  171. (int)nread);
  172. httpc->nread_inbuf = 0;
  173. httpc->inbuflen = nread;
  174. (void)h2_process_pending_input(conn, httpc, &result);
  175. }
  176. else
  177. /* the read failed so let's say this is dead anyway */
  178. dead = TRUE;
  179. }
  180. }
  181. return dead;
  182. }
  183. static unsigned int http2_conncheck(struct connectdata *check,
  184. unsigned int checks_to_perform)
  185. {
  186. unsigned int ret_val = CONNRESULT_NONE;
  187. struct http_conn *c = &check->proto.httpc;
  188. int rc;
  189. bool send_frames = false;
  190. if(checks_to_perform & CONNCHECK_ISDEAD) {
  191. if(http2_connisdead(check))
  192. ret_val |= CONNRESULT_DEAD;
  193. }
  194. if(checks_to_perform & CONNCHECK_KEEPALIVE) {
  195. struct curltime now = Curl_now();
  196. timediff_t elapsed = Curl_timediff(now, check->keepalive);
  197. if(elapsed > check->upkeep_interval_ms) {
  198. /* Perform an HTTP/2 PING */
  199. rc = nghttp2_submit_ping(c->h2, 0, ZERO_NULL);
  200. if(!rc) {
  201. /* Successfully added a PING frame to the session. Need to flag this
  202. so the frame is sent. */
  203. send_frames = true;
  204. }
  205. else {
  206. failf(check->data, "nghttp2_submit_ping() failed: %s(%d)",
  207. nghttp2_strerror(rc), rc);
  208. }
  209. check->keepalive = now;
  210. }
  211. }
  212. if(send_frames) {
  213. rc = nghttp2_session_send(c->h2);
  214. if(rc)
  215. failf(check->data, "nghttp2_session_send() failed: %s(%d)",
  216. nghttp2_strerror(rc), rc);
  217. }
  218. return ret_val;
  219. }
  220. /* called from http_setup_conn */
  221. void Curl_http2_setup_req(struct Curl_easy *data)
  222. {
  223. struct HTTP *http = data->req.protop;
  224. http->bodystarted = FALSE;
  225. http->status_code = -1;
  226. http->pausedata = NULL;
  227. http->pauselen = 0;
  228. http->closed = FALSE;
  229. http->close_handled = FALSE;
  230. http->mem = NULL;
  231. http->len = 0;
  232. http->memlen = 0;
  233. }
  234. /* called from http_setup_conn */
  235. void Curl_http2_setup_conn(struct connectdata *conn)
  236. {
  237. conn->proto.httpc.settings.max_concurrent_streams =
  238. DEFAULT_MAX_CONCURRENT_STREAMS;
  239. conn->proto.httpc.error_code = NGHTTP2_NO_ERROR;
  240. }
  241. /*
  242. * HTTP2 handler interface. This isn't added to the general list of protocols
  243. * but will be used at run-time when the protocol is dynamically switched from
  244. * HTTP to HTTP2.
  245. */
  246. static const struct Curl_handler Curl_handler_http2 = {
  247. "HTTP", /* scheme */
  248. ZERO_NULL, /* setup_connection */
  249. Curl_http, /* do_it */
  250. Curl_http_done, /* done */
  251. ZERO_NULL, /* do_more */
  252. ZERO_NULL, /* connect_it */
  253. ZERO_NULL, /* connecting */
  254. ZERO_NULL, /* doing */
  255. http2_getsock, /* proto_getsock */
  256. http2_getsock, /* doing_getsock */
  257. ZERO_NULL, /* domore_getsock */
  258. http2_perform_getsock, /* perform_getsock */
  259. http2_disconnect, /* disconnect */
  260. ZERO_NULL, /* readwrite */
  261. http2_conncheck, /* connection_check */
  262. PORT_HTTP, /* defport */
  263. CURLPROTO_HTTP, /* protocol */
  264. PROTOPT_STREAM /* flags */
  265. };
  266. static const struct Curl_handler Curl_handler_http2_ssl = {
  267. "HTTPS", /* scheme */
  268. ZERO_NULL, /* setup_connection */
  269. Curl_http, /* do_it */
  270. Curl_http_done, /* done */
  271. ZERO_NULL, /* do_more */
  272. ZERO_NULL, /* connect_it */
  273. ZERO_NULL, /* connecting */
  274. ZERO_NULL, /* doing */
  275. http2_getsock, /* proto_getsock */
  276. http2_getsock, /* doing_getsock */
  277. ZERO_NULL, /* domore_getsock */
  278. http2_perform_getsock, /* perform_getsock */
  279. http2_disconnect, /* disconnect */
  280. ZERO_NULL, /* readwrite */
  281. http2_conncheck, /* connection_check */
  282. PORT_HTTP, /* defport */
  283. CURLPROTO_HTTPS, /* protocol */
  284. PROTOPT_SSL | PROTOPT_STREAM /* flags */
  285. };
  286. /*
  287. * Store nghttp2 version info in this buffer, Prefix with a space. Return
  288. * total length written.
  289. */
  290. int Curl_http2_ver(char *p, size_t len)
  291. {
  292. nghttp2_info *h2 = nghttp2_version(0);
  293. return msnprintf(p, len, "nghttp2/%s", h2->version_str);
  294. }
  295. /*
  296. * The implementation of nghttp2_send_callback type. Here we write |data| with
  297. * size |length| to the network and return the number of bytes actually
  298. * written. See the documentation of nghttp2_send_callback for the details.
  299. */
  300. static ssize_t send_callback(nghttp2_session *h2,
  301. const uint8_t *data, size_t length, int flags,
  302. void *userp)
  303. {
  304. struct connectdata *conn = (struct connectdata *)userp;
  305. struct http_conn *c = &conn->proto.httpc;
  306. ssize_t written;
  307. CURLcode result = CURLE_OK;
  308. (void)h2;
  309. (void)flags;
  310. if(!c->send_underlying)
  311. /* called before setup properly! */
  312. return NGHTTP2_ERR_CALLBACK_FAILURE;
  313. written = ((Curl_send*)c->send_underlying)(conn, FIRSTSOCKET,
  314. data, length, &result);
  315. if(result == CURLE_AGAIN) {
  316. return NGHTTP2_ERR_WOULDBLOCK;
  317. }
  318. if(written == -1) {
  319. failf(conn->data, "Failed sending HTTP2 data");
  320. return NGHTTP2_ERR_CALLBACK_FAILURE;
  321. }
  322. if(!written)
  323. return NGHTTP2_ERR_WOULDBLOCK;
  324. return written;
  325. }
  326. /* We pass a pointer to this struct in the push callback, but the contents of
  327. the struct are hidden from the user. */
  328. struct curl_pushheaders {
  329. struct Curl_easy *data;
  330. const nghttp2_push_promise *frame;
  331. };
  332. /*
  333. * push header access function. Only to be used from within the push callback
  334. */
  335. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  336. {
  337. /* Verify that we got a good easy handle in the push header struct, mostly to
  338. detect rubbish input fast(er). */
  339. if(!h || !GOOD_EASY_HANDLE(h->data))
  340. return NULL;
  341. else {
  342. struct HTTP *stream = h->data->req.protop;
  343. if(num < stream->push_headers_used)
  344. return stream->push_headers[num];
  345. }
  346. return NULL;
  347. }
  348. /*
  349. * push header access function. Only to be used from within the push callback
  350. */
  351. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  352. {
  353. /* Verify that we got a good easy handle in the push header struct,
  354. mostly to detect rubbish input fast(er). Also empty header name
  355. is just a rubbish too. We have to allow ":" at the beginning of
  356. the header, but header == ":" must be rejected. If we have ':' in
  357. the middle of header, it could be matched in middle of the value,
  358. this is because we do prefix match.*/
  359. if(!h || !GOOD_EASY_HANDLE(h->data) || !header || !header[0] ||
  360. !strcmp(header, ":") || strchr(header + 1, ':'))
  361. return NULL;
  362. else {
  363. struct HTTP *stream = h->data->req.protop;
  364. size_t len = strlen(header);
  365. size_t i;
  366. for(i = 0; i<stream->push_headers_used; i++) {
  367. if(!strncmp(header, stream->push_headers[i], len)) {
  368. /* sub-match, make sure that it is followed by a colon */
  369. if(stream->push_headers[i][len] != ':')
  370. continue;
  371. return &stream->push_headers[i][len + 1];
  372. }
  373. }
  374. }
  375. return NULL;
  376. }
  377. /*
  378. * This specific transfer on this connection has been "drained".
  379. */
  380. static void drained_transfer(struct Curl_easy *data,
  381. struct http_conn *httpc)
  382. {
  383. DEBUGASSERT(httpc->drain_total >= data->state.drain);
  384. httpc->drain_total -= data->state.drain;
  385. data->state.drain = 0;
  386. }
  387. /*
  388. * Mark this transfer to get "drained".
  389. */
  390. static void drain_this(struct Curl_easy *data,
  391. struct http_conn *httpc)
  392. {
  393. data->state.drain++;
  394. httpc->drain_total++;
  395. DEBUGASSERT(httpc->drain_total >= data->state.drain);
  396. }
  397. static struct Curl_easy *duphandle(struct Curl_easy *data)
  398. {
  399. struct Curl_easy *second = curl_easy_duphandle(data);
  400. if(second) {
  401. /* setup the request struct */
  402. struct HTTP *http = calloc(1, sizeof(struct HTTP));
  403. if(!http) {
  404. (void)Curl_close(&second);
  405. }
  406. else {
  407. second->req.protop = http;
  408. Curl_dyn_init(&http->header_recvbuf, DYN_H2_HEADERS);
  409. Curl_http2_setup_req(second);
  410. second->state.stream_weight = data->state.stream_weight;
  411. }
  412. }
  413. return second;
  414. }
  415. static int set_transfer_url(struct Curl_easy *data,
  416. struct curl_pushheaders *hp)
  417. {
  418. const char *v;
  419. CURLU *u = curl_url();
  420. CURLUcode uc;
  421. char *url;
  422. v = curl_pushheader_byname(hp, ":scheme");
  423. if(v) {
  424. uc = curl_url_set(u, CURLUPART_SCHEME, v, 0);
  425. if(uc)
  426. return 1;
  427. }
  428. v = curl_pushheader_byname(hp, ":authority");
  429. if(v) {
  430. uc = curl_url_set(u, CURLUPART_HOST, v, 0);
  431. if(uc)
  432. return 2;
  433. }
  434. v = curl_pushheader_byname(hp, ":path");
  435. if(v) {
  436. uc = curl_url_set(u, CURLUPART_PATH, v, 0);
  437. if(uc)
  438. return 3;
  439. }
  440. uc = curl_url_get(u, CURLUPART_URL, &url, 0);
  441. if(uc)
  442. return 4;
  443. curl_url_cleanup(u);
  444. if(data->change.url_alloc)
  445. free(data->change.url);
  446. data->change.url_alloc = TRUE;
  447. data->change.url = url;
  448. return 0;
  449. }
  450. static int push_promise(struct Curl_easy *data,
  451. struct connectdata *conn,
  452. const nghttp2_push_promise *frame)
  453. {
  454. int rv; /* one of the CURL_PUSH_* defines */
  455. H2BUGF(infof(data, "PUSH_PROMISE received, stream %u!\n",
  456. frame->promised_stream_id));
  457. if(data->multi->push_cb) {
  458. struct HTTP *stream;
  459. struct HTTP *newstream;
  460. struct curl_pushheaders heads;
  461. CURLMcode rc;
  462. struct http_conn *httpc;
  463. size_t i;
  464. /* clone the parent */
  465. struct Curl_easy *newhandle = duphandle(data);
  466. if(!newhandle) {
  467. infof(data, "failed to duplicate handle\n");
  468. rv = CURL_PUSH_DENY; /* FAIL HARD */
  469. goto fail;
  470. }
  471. heads.data = data;
  472. heads.frame = frame;
  473. /* ask the application */
  474. H2BUGF(infof(data, "Got PUSH_PROMISE, ask application!\n"));
  475. stream = data->req.protop;
  476. if(!stream) {
  477. failf(data, "Internal NULL stream!\n");
  478. (void)Curl_close(&newhandle);
  479. rv = CURL_PUSH_DENY;
  480. goto fail;
  481. }
  482. rv = set_transfer_url(newhandle, &heads);
  483. if(rv) {
  484. rv = CURL_PUSH_DENY;
  485. goto fail;
  486. }
  487. Curl_set_in_callback(data, true);
  488. rv = data->multi->push_cb(data, newhandle,
  489. stream->push_headers_used, &heads,
  490. data->multi->push_userp);
  491. Curl_set_in_callback(data, false);
  492. /* free the headers again */
  493. for(i = 0; i<stream->push_headers_used; i++)
  494. free(stream->push_headers[i]);
  495. free(stream->push_headers);
  496. stream->push_headers = NULL;
  497. stream->push_headers_used = 0;
  498. if(rv) {
  499. DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
  500. /* denied, kill off the new handle again */
  501. http2_stream_free(newhandle->req.protop);
  502. newhandle->req.protop = NULL;
  503. (void)Curl_close(&newhandle);
  504. goto fail;
  505. }
  506. newstream = newhandle->req.protop;
  507. newstream->stream_id = frame->promised_stream_id;
  508. newhandle->req.maxdownload = -1;
  509. newhandle->req.size = -1;
  510. /* approved, add to the multi handle and immediately switch to PERFORM
  511. state with the given connection !*/
  512. rc = Curl_multi_add_perform(data->multi, newhandle, conn);
  513. if(rc) {
  514. infof(data, "failed to add handle to multi\n");
  515. http2_stream_free(newhandle->req.protop);
  516. newhandle->req.protop = NULL;
  517. Curl_close(&newhandle);
  518. rv = CURL_PUSH_DENY;
  519. goto fail;
  520. }
  521. httpc = &conn->proto.httpc;
  522. rv = nghttp2_session_set_stream_user_data(httpc->h2,
  523. frame->promised_stream_id,
  524. newhandle);
  525. if(rv) {
  526. infof(data, "failed to set user_data for stream %d\n",
  527. frame->promised_stream_id);
  528. DEBUGASSERT(0);
  529. rv = CURL_PUSH_DENY;
  530. goto fail;
  531. }
  532. }
  533. else {
  534. H2BUGF(infof(data, "Got PUSH_PROMISE, ignore it!\n"));
  535. rv = CURL_PUSH_DENY;
  536. }
  537. fail:
  538. return rv;
  539. }
  540. /*
  541. * multi_connchanged() is called to tell that there is a connection in
  542. * this multi handle that has changed state (multiplexing become possible, the
  543. * number of allowed streams changed or similar), and a subsequent use of this
  544. * multi handle should move CONNECT_PEND handles back to CONNECT to have them
  545. * retry.
  546. */
  547. static void multi_connchanged(struct Curl_multi *multi)
  548. {
  549. multi->recheckstate = TRUE;
  550. }
  551. static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
  552. void *userp)
  553. {
  554. struct connectdata *conn = (struct connectdata *)userp;
  555. struct http_conn *httpc = &conn->proto.httpc;
  556. struct Curl_easy *data_s = NULL;
  557. struct HTTP *stream = NULL;
  558. int rv;
  559. size_t left, ncopy;
  560. int32_t stream_id = frame->hd.stream_id;
  561. CURLcode result;
  562. if(!stream_id) {
  563. /* stream ID zero is for connection-oriented stuff */
  564. if(frame->hd.type == NGHTTP2_SETTINGS) {
  565. uint32_t max_conn = httpc->settings.max_concurrent_streams;
  566. H2BUGF(infof(conn->data, "Got SETTINGS\n"));
  567. httpc->settings.max_concurrent_streams =
  568. nghttp2_session_get_remote_settings(
  569. session, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
  570. httpc->settings.enable_push =
  571. nghttp2_session_get_remote_settings(
  572. session, NGHTTP2_SETTINGS_ENABLE_PUSH);
  573. H2BUGF(infof(conn->data, "MAX_CONCURRENT_STREAMS == %d\n",
  574. httpc->settings.max_concurrent_streams));
  575. H2BUGF(infof(conn->data, "ENABLE_PUSH == %s\n",
  576. httpc->settings.enable_push?"TRUE":"false"));
  577. if(max_conn != httpc->settings.max_concurrent_streams) {
  578. /* only signal change if the value actually changed */
  579. infof(conn->data,
  580. "Connection state changed (MAX_CONCURRENT_STREAMS == %u)!\n",
  581. httpc->settings.max_concurrent_streams);
  582. multi_connchanged(conn->data->multi);
  583. }
  584. }
  585. return 0;
  586. }
  587. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  588. if(!data_s) {
  589. H2BUGF(infof(conn->data,
  590. "No Curl_easy associated with stream: %x\n",
  591. stream_id));
  592. return 0;
  593. }
  594. stream = data_s->req.protop;
  595. if(!stream) {
  596. H2BUGF(infof(data_s, "No proto pointer for stream: %x\n",
  597. stream_id));
  598. return NGHTTP2_ERR_CALLBACK_FAILURE;
  599. }
  600. H2BUGF(infof(data_s, "on_frame_recv() header %x stream %x\n",
  601. frame->hd.type, stream_id));
  602. switch(frame->hd.type) {
  603. case NGHTTP2_DATA:
  604. /* If body started on this stream, then receiving DATA is illegal. */
  605. if(!stream->bodystarted) {
  606. rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  607. stream_id, NGHTTP2_PROTOCOL_ERROR);
  608. if(nghttp2_is_fatal(rv)) {
  609. return NGHTTP2_ERR_CALLBACK_FAILURE;
  610. }
  611. }
  612. break;
  613. case NGHTTP2_HEADERS:
  614. if(stream->bodystarted) {
  615. /* Only valid HEADERS after body started is trailer HEADERS. We
  616. buffer them in on_header callback. */
  617. break;
  618. }
  619. /* nghttp2 guarantees that :status is received, and we store it to
  620. stream->status_code. Fuzzing has proven this can still be reached
  621. without status code having been set. */
  622. if(stream->status_code == -1)
  623. return NGHTTP2_ERR_CALLBACK_FAILURE;
  624. /* Only final status code signals the end of header */
  625. if(stream->status_code / 100 != 1) {
  626. stream->bodystarted = TRUE;
  627. stream->status_code = -1;
  628. }
  629. result = Curl_dyn_add(&stream->header_recvbuf, "\r\n");
  630. if(result)
  631. return NGHTTP2_ERR_CALLBACK_FAILURE;
  632. left = Curl_dyn_len(&stream->header_recvbuf) -
  633. stream->nread_header_recvbuf;
  634. ncopy = CURLMIN(stream->len, left);
  635. memcpy(&stream->mem[stream->memlen],
  636. Curl_dyn_ptr(&stream->header_recvbuf) +
  637. stream->nread_header_recvbuf,
  638. ncopy);
  639. stream->nread_header_recvbuf += ncopy;
  640. H2BUGF(infof(data_s, "Store %zu bytes headers from stream %u at %p\n",
  641. ncopy, stream_id, stream->mem));
  642. stream->len -= ncopy;
  643. stream->memlen += ncopy;
  644. drain_this(data_s, httpc);
  645. {
  646. /* get the pointer from userp again since it was re-assigned above */
  647. struct connectdata *conn_s = (struct connectdata *)userp;
  648. /* if we receive data for another handle, wake that up */
  649. if(conn_s->data != data_s)
  650. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  651. }
  652. break;
  653. case NGHTTP2_PUSH_PROMISE:
  654. rv = push_promise(data_s, conn, &frame->push_promise);
  655. if(rv) { /* deny! */
  656. int h2;
  657. DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
  658. h2 = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  659. frame->push_promise.promised_stream_id,
  660. NGHTTP2_CANCEL);
  661. if(nghttp2_is_fatal(h2))
  662. return NGHTTP2_ERR_CALLBACK_FAILURE;
  663. else if(rv == CURL_PUSH_ERROROUT) {
  664. DEBUGF(infof(data_s, "Fail the parent stream (too)\n"));
  665. return NGHTTP2_ERR_CALLBACK_FAILURE;
  666. }
  667. }
  668. break;
  669. default:
  670. H2BUGF(infof(data_s, "Got frame type %x for stream %u!\n",
  671. frame->hd.type, stream_id));
  672. break;
  673. }
  674. return 0;
  675. }
  676. static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  677. int32_t stream_id,
  678. const uint8_t *data, size_t len, void *userp)
  679. {
  680. struct HTTP *stream;
  681. struct Curl_easy *data_s;
  682. size_t nread;
  683. struct connectdata *conn = (struct connectdata *)userp;
  684. (void)session;
  685. (void)flags;
  686. (void)data;
  687. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  688. /* get the stream from the hash based on Stream ID */
  689. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  690. if(!data_s)
  691. /* Receiving a Stream ID not in the hash should not happen, this is an
  692. internal error more than anything else! */
  693. return NGHTTP2_ERR_CALLBACK_FAILURE;
  694. stream = data_s->req.protop;
  695. if(!stream)
  696. return NGHTTP2_ERR_CALLBACK_FAILURE;
  697. nread = CURLMIN(stream->len, len);
  698. memcpy(&stream->mem[stream->memlen], data, nread);
  699. stream->len -= nread;
  700. stream->memlen += nread;
  701. drain_this(data_s, &conn->proto.httpc);
  702. /* if we receive data for another handle, wake that up */
  703. if(conn->data != data_s)
  704. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  705. H2BUGF(infof(data_s, "%zu data received for stream %u "
  706. "(%zu left in buffer %p, total %zu)\n",
  707. nread, stream_id,
  708. stream->len, stream->mem,
  709. stream->memlen));
  710. if(nread < len) {
  711. stream->pausedata = data + nread;
  712. stream->pauselen = len - nread;
  713. H2BUGF(infof(data_s, "NGHTTP2_ERR_PAUSE - %zu bytes out of buffer"
  714. ", stream %u\n",
  715. len - nread, stream_id));
  716. data_s->conn->proto.httpc.pause_stream_id = stream_id;
  717. return NGHTTP2_ERR_PAUSE;
  718. }
  719. /* pause execution of nghttp2 if we received data for another handle
  720. in order to process them first. */
  721. if(conn->data != data_s) {
  722. data_s->conn->proto.httpc.pause_stream_id = stream_id;
  723. return NGHTTP2_ERR_PAUSE;
  724. }
  725. return 0;
  726. }
  727. static int on_stream_close(nghttp2_session *session, int32_t stream_id,
  728. uint32_t error_code, void *userp)
  729. {
  730. struct Curl_easy *data_s;
  731. struct HTTP *stream;
  732. struct connectdata *conn = (struct connectdata *)userp;
  733. int rv;
  734. (void)session;
  735. (void)stream_id;
  736. if(stream_id) {
  737. struct http_conn *httpc;
  738. /* get the stream from the hash based on Stream ID, stream ID zero is for
  739. connection-oriented stuff */
  740. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  741. if(!data_s) {
  742. /* We could get stream ID not in the hash. For example, if we
  743. decided to reject stream (e.g., PUSH_PROMISE). */
  744. return 0;
  745. }
  746. H2BUGF(infof(data_s, "on_stream_close(), %s (err %d), stream %u\n",
  747. nghttp2_http2_strerror(error_code), error_code, stream_id));
  748. stream = data_s->req.protop;
  749. if(!stream)
  750. return NGHTTP2_ERR_CALLBACK_FAILURE;
  751. stream->closed = TRUE;
  752. httpc = &conn->proto.httpc;
  753. drain_this(data_s, httpc);
  754. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  755. httpc->error_code = error_code;
  756. /* remove the entry from the hash as the stream is now gone */
  757. rv = nghttp2_session_set_stream_user_data(session, stream_id, 0);
  758. if(rv) {
  759. infof(data_s, "http/2: failed to clear user_data for stream %d!\n",
  760. stream_id);
  761. DEBUGASSERT(0);
  762. }
  763. if(stream_id == httpc->pause_stream_id) {
  764. H2BUGF(infof(data_s, "Stopped the pause stream!\n"));
  765. httpc->pause_stream_id = 0;
  766. }
  767. H2BUGF(infof(data_s, "Removed stream %u hash!\n", stream_id));
  768. stream->stream_id = 0; /* cleared */
  769. }
  770. return 0;
  771. }
  772. static int on_begin_headers(nghttp2_session *session,
  773. const nghttp2_frame *frame, void *userp)
  774. {
  775. struct HTTP *stream;
  776. struct Curl_easy *data_s = NULL;
  777. (void)userp;
  778. data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
  779. if(!data_s) {
  780. return 0;
  781. }
  782. H2BUGF(infof(data_s, "on_begin_headers() was called\n"));
  783. if(frame->hd.type != NGHTTP2_HEADERS) {
  784. return 0;
  785. }
  786. stream = data_s->req.protop;
  787. if(!stream || !stream->bodystarted) {
  788. return 0;
  789. }
  790. return 0;
  791. }
  792. /* Decode HTTP status code. Returns -1 if no valid status code was
  793. decoded. */
  794. static int decode_status_code(const uint8_t *value, size_t len)
  795. {
  796. int i;
  797. int res;
  798. if(len != 3) {
  799. return -1;
  800. }
  801. res = 0;
  802. for(i = 0; i < 3; ++i) {
  803. char c = value[i];
  804. if(c < '0' || c > '9') {
  805. return -1;
  806. }
  807. res *= 10;
  808. res += c - '0';
  809. }
  810. return res;
  811. }
  812. /* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
  813. static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
  814. const uint8_t *name, size_t namelen,
  815. const uint8_t *value, size_t valuelen,
  816. uint8_t flags,
  817. void *userp)
  818. {
  819. struct HTTP *stream;
  820. struct Curl_easy *data_s;
  821. int32_t stream_id = frame->hd.stream_id;
  822. struct connectdata *conn = (struct connectdata *)userp;
  823. CURLcode result;
  824. (void)flags;
  825. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  826. /* get the stream from the hash based on Stream ID */
  827. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  828. if(!data_s)
  829. /* Receiving a Stream ID not in the hash should not happen, this is an
  830. internal error more than anything else! */
  831. return NGHTTP2_ERR_CALLBACK_FAILURE;
  832. stream = data_s->req.protop;
  833. if(!stream) {
  834. failf(data_s, "Internal NULL stream! 5\n");
  835. return NGHTTP2_ERR_CALLBACK_FAILURE;
  836. }
  837. /* Store received PUSH_PROMISE headers to be used when the subsequent
  838. PUSH_PROMISE callback comes */
  839. if(frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  840. char *h;
  841. if(!strcmp(":authority", (const char *)name)) {
  842. /* pseudo headers are lower case */
  843. int rc = 0;
  844. char *check = aprintf("%s:%d", conn->host.name, conn->remote_port);
  845. if(!check)
  846. /* no memory */
  847. return NGHTTP2_ERR_CALLBACK_FAILURE;
  848. if(!Curl_strcasecompare(check, (const char *)value) &&
  849. ((conn->remote_port != conn->given->defport) ||
  850. !Curl_strcasecompare(conn->host.name, (const char *)value))) {
  851. /* This is push is not for the same authority that was asked for in
  852. * the URL. RFC 7540 section 8.2 says: "A client MUST treat a
  853. * PUSH_PROMISE for which the server is not authoritative as a stream
  854. * error of type PROTOCOL_ERROR."
  855. */
  856. (void)nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
  857. stream_id, NGHTTP2_PROTOCOL_ERROR);
  858. rc = NGHTTP2_ERR_CALLBACK_FAILURE;
  859. }
  860. free(check);
  861. if(rc)
  862. return rc;
  863. }
  864. if(!stream->push_headers) {
  865. stream->push_headers_alloc = 10;
  866. stream->push_headers = malloc(stream->push_headers_alloc *
  867. sizeof(char *));
  868. if(!stream->push_headers)
  869. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  870. stream->push_headers_used = 0;
  871. }
  872. else if(stream->push_headers_used ==
  873. stream->push_headers_alloc) {
  874. char **headp;
  875. stream->push_headers_alloc *= 2;
  876. headp = Curl_saferealloc(stream->push_headers,
  877. stream->push_headers_alloc * sizeof(char *));
  878. if(!headp) {
  879. stream->push_headers = NULL;
  880. return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
  881. }
  882. stream->push_headers = headp;
  883. }
  884. h = aprintf("%s:%s", name, value);
  885. if(h)
  886. stream->push_headers[stream->push_headers_used++] = h;
  887. return 0;
  888. }
  889. if(stream->bodystarted) {
  890. /* This is a trailer */
  891. struct dynbuf trail;
  892. H2BUGF(infof(data_s, "h2 trailer: %.*s: %.*s\n", namelen, name, valuelen,
  893. value));
  894. Curl_dyn_init(&trail, DYN_H2_TRAILER);
  895. result = Curl_dyn_addf(&trail,
  896. "%.*s: %.*s\r\n", namelen, name,
  897. valuelen, value);
  898. if(!result)
  899. result = Curl_client_write(conn, CLIENTWRITE_HEADER,
  900. Curl_dyn_ptr(&trail),
  901. Curl_dyn_len(&trail));
  902. Curl_dyn_free(&trail);
  903. if(result)
  904. return NGHTTP2_ERR_CALLBACK_FAILURE;
  905. return 0;
  906. }
  907. if(namelen == sizeof(":status") - 1 &&
  908. memcmp(":status", name, namelen) == 0) {
  909. /* nghttp2 guarantees :status is received first and only once, and
  910. value is 3 digits status code, and decode_status_code always
  911. succeeds. */
  912. stream->status_code = decode_status_code(value, valuelen);
  913. DEBUGASSERT(stream->status_code != -1);
  914. result = Curl_dyn_add(&stream->header_recvbuf, "HTTP/2 ");
  915. if(result)
  916. return NGHTTP2_ERR_CALLBACK_FAILURE;
  917. result = Curl_dyn_addn(&stream->header_recvbuf, value, valuelen);
  918. if(result)
  919. return NGHTTP2_ERR_CALLBACK_FAILURE;
  920. /* the space character after the status code is mandatory */
  921. result = Curl_dyn_add(&stream->header_recvbuf, " \r\n");
  922. if(result)
  923. return NGHTTP2_ERR_CALLBACK_FAILURE;
  924. /* if we receive data for another handle, wake that up */
  925. if(conn->data != data_s)
  926. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  927. H2BUGF(infof(data_s, "h2 status: HTTP/2 %03d (easy %p)\n",
  928. stream->status_code, data_s));
  929. return 0;
  930. }
  931. /* nghttp2 guarantees that namelen > 0, and :status was already
  932. received, and this is not pseudo-header field . */
  933. /* convert to a HTTP1-style header */
  934. result = Curl_dyn_addn(&stream->header_recvbuf, name, namelen);
  935. if(result)
  936. return NGHTTP2_ERR_CALLBACK_FAILURE;
  937. result = Curl_dyn_add(&stream->header_recvbuf, ": ");
  938. if(result)
  939. return NGHTTP2_ERR_CALLBACK_FAILURE;
  940. result = Curl_dyn_addn(&stream->header_recvbuf, value, valuelen);
  941. if(result)
  942. return NGHTTP2_ERR_CALLBACK_FAILURE;
  943. result = Curl_dyn_add(&stream->header_recvbuf, "\r\n");
  944. if(result)
  945. return NGHTTP2_ERR_CALLBACK_FAILURE;
  946. /* if we receive data for another handle, wake that up */
  947. if(conn->data != data_s)
  948. Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
  949. H2BUGF(infof(data_s, "h2 header: %.*s: %.*s\n", namelen, name, valuelen,
  950. value));
  951. return 0; /* 0 is successful */
  952. }
  953. static ssize_t data_source_read_callback(nghttp2_session *session,
  954. int32_t stream_id,
  955. uint8_t *buf, size_t length,
  956. uint32_t *data_flags,
  957. nghttp2_data_source *source,
  958. void *userp)
  959. {
  960. struct Curl_easy *data_s;
  961. struct HTTP *stream = NULL;
  962. size_t nread;
  963. (void)source;
  964. (void)userp;
  965. if(stream_id) {
  966. /* get the stream from the hash based on Stream ID, stream ID zero is for
  967. connection-oriented stuff */
  968. data_s = nghttp2_session_get_stream_user_data(session, stream_id);
  969. if(!data_s)
  970. /* Receiving a Stream ID not in the hash should not happen, this is an
  971. internal error more than anything else! */
  972. return NGHTTP2_ERR_CALLBACK_FAILURE;
  973. stream = data_s->req.protop;
  974. if(!stream)
  975. return NGHTTP2_ERR_CALLBACK_FAILURE;
  976. }
  977. else
  978. return NGHTTP2_ERR_INVALID_ARGUMENT;
  979. nread = CURLMIN(stream->upload_len, length);
  980. if(nread > 0) {
  981. memcpy(buf, stream->upload_mem, nread);
  982. stream->upload_mem += nread;
  983. stream->upload_len -= nread;
  984. if(data_s->state.infilesize != -1)
  985. stream->upload_left -= nread;
  986. }
  987. if(stream->upload_left == 0)
  988. *data_flags = NGHTTP2_DATA_FLAG_EOF;
  989. else if(nread == 0)
  990. return NGHTTP2_ERR_DEFERRED;
  991. H2BUGF(infof(data_s, "data_source_read_callback: "
  992. "returns %zu bytes stream %u\n",
  993. nread, stream_id));
  994. return nread;
  995. }
  996. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  997. static int error_callback(nghttp2_session *session,
  998. const char *msg,
  999. size_t len,
  1000. void *userp)
  1001. {
  1002. struct connectdata *conn = (struct connectdata *)userp;
  1003. (void)session;
  1004. infof(conn->data, "http2 error: %.*s\n", len, msg);
  1005. return 0;
  1006. }
  1007. #endif
  1008. static void populate_settings(struct connectdata *conn,
  1009. struct http_conn *httpc)
  1010. {
  1011. nghttp2_settings_entry *iv = httpc->local_settings;
  1012. DEBUGASSERT(conn->data);
  1013. iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
  1014. iv[0].value = Curl_multi_max_concurrent_streams(conn->data->multi);
  1015. iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
  1016. iv[1].value = HTTP2_HUGE_WINDOW_SIZE;
  1017. iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
  1018. iv[2].value = conn->data->multi->push_cb != NULL;
  1019. httpc->local_settings_num = 3;
  1020. }
  1021. void Curl_http2_done(struct Curl_easy *data, bool premature)
  1022. {
  1023. struct HTTP *http = data->req.protop;
  1024. struct http_conn *httpc = &data->conn->proto.httpc;
  1025. /* there might be allocated resources done before this got the 'h2' pointer
  1026. setup */
  1027. Curl_dyn_free(&http->header_recvbuf);
  1028. if(http->push_headers) {
  1029. /* if they weren't used and then freed before */
  1030. for(; http->push_headers_used > 0; --http->push_headers_used) {
  1031. free(http->push_headers[http->push_headers_used - 1]);
  1032. }
  1033. free(http->push_headers);
  1034. http->push_headers = NULL;
  1035. }
  1036. if(!(data->conn->handler->protocol&PROTO_FAMILY_HTTP) ||
  1037. !httpc->h2) /* not HTTP/2 ? */
  1038. return;
  1039. if(premature) {
  1040. /* RST_STREAM */
  1041. if(!nghttp2_submit_rst_stream(httpc->h2, NGHTTP2_FLAG_NONE,
  1042. http->stream_id, NGHTTP2_STREAM_CLOSED))
  1043. (void)nghttp2_session_send(httpc->h2);
  1044. if(http->stream_id == httpc->pause_stream_id) {
  1045. infof(data, "stopped the pause stream!\n");
  1046. httpc->pause_stream_id = 0;
  1047. }
  1048. }
  1049. if(data->state.drain)
  1050. drained_transfer(data, httpc);
  1051. /* -1 means unassigned and 0 means cleared */
  1052. if(http->stream_id > 0) {
  1053. int rv = nghttp2_session_set_stream_user_data(httpc->h2,
  1054. http->stream_id, 0);
  1055. if(rv) {
  1056. infof(data, "http/2: failed to clear user_data for stream %d!\n",
  1057. http->stream_id);
  1058. DEBUGASSERT(0);
  1059. }
  1060. http->stream_id = 0;
  1061. }
  1062. if(0 == nghttp2_session_check_request_allowed(httpc->h2)) {
  1063. /* No more requests are allowed in the current session, so the connection
  1064. may not be reused. This is set when a GOAWAY frame has been received or
  1065. when the limit of stream identifiers has been reached. */
  1066. connclose(data->conn, "http/2: No new requests allowed");
  1067. }
  1068. }
  1069. /*
  1070. * Initialize nghttp2 for a Curl connection
  1071. */
  1072. static CURLcode http2_init(struct connectdata *conn)
  1073. {
  1074. if(!conn->proto.httpc.h2) {
  1075. int rc;
  1076. nghttp2_session_callbacks *callbacks;
  1077. conn->proto.httpc.inbuf = malloc(H2_BUFSIZE);
  1078. if(conn->proto.httpc.inbuf == NULL)
  1079. return CURLE_OUT_OF_MEMORY;
  1080. rc = nghttp2_session_callbacks_new(&callbacks);
  1081. if(rc) {
  1082. failf(conn->data, "Couldn't initialize nghttp2 callbacks!");
  1083. return CURLE_OUT_OF_MEMORY; /* most likely at least */
  1084. }
  1085. /* nghttp2_send_callback */
  1086. nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
  1087. /* nghttp2_on_frame_recv_callback */
  1088. nghttp2_session_callbacks_set_on_frame_recv_callback
  1089. (callbacks, on_frame_recv);
  1090. /* nghttp2_on_data_chunk_recv_callback */
  1091. nghttp2_session_callbacks_set_on_data_chunk_recv_callback
  1092. (callbacks, on_data_chunk_recv);
  1093. /* nghttp2_on_stream_close_callback */
  1094. nghttp2_session_callbacks_set_on_stream_close_callback
  1095. (callbacks, on_stream_close);
  1096. /* nghttp2_on_begin_headers_callback */
  1097. nghttp2_session_callbacks_set_on_begin_headers_callback
  1098. (callbacks, on_begin_headers);
  1099. /* nghttp2_on_header_callback */
  1100. nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header);
  1101. nghttp2_session_callbacks_set_error_callback(callbacks, error_callback);
  1102. /* The nghttp2 session is not yet setup, do it */
  1103. rc = nghttp2_session_client_new(&conn->proto.httpc.h2, callbacks, conn);
  1104. nghttp2_session_callbacks_del(callbacks);
  1105. if(rc) {
  1106. failf(conn->data, "Couldn't initialize nghttp2!");
  1107. return CURLE_OUT_OF_MEMORY; /* most likely at least */
  1108. }
  1109. }
  1110. return CURLE_OK;
  1111. }
  1112. /*
  1113. * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
  1114. */
  1115. CURLcode Curl_http2_request_upgrade(struct dynbuf *req,
  1116. struct connectdata *conn)
  1117. {
  1118. CURLcode result;
  1119. ssize_t binlen;
  1120. char *base64;
  1121. size_t blen;
  1122. struct SingleRequest *k = &conn->data->req;
  1123. uint8_t *binsettings = conn->proto.httpc.binsettings;
  1124. struct http_conn *httpc = &conn->proto.httpc;
  1125. populate_settings(conn, httpc);
  1126. /* this returns number of bytes it wrote */
  1127. binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
  1128. httpc->local_settings,
  1129. httpc->local_settings_num);
  1130. if(!binlen) {
  1131. failf(conn->data, "nghttp2 unexpectedly failed on pack_settings_payload");
  1132. Curl_dyn_free(req);
  1133. return CURLE_FAILED_INIT;
  1134. }
  1135. conn->proto.httpc.binlen = binlen;
  1136. result = Curl_base64url_encode(conn->data, (const char *)binsettings, binlen,
  1137. &base64, &blen);
  1138. if(result) {
  1139. Curl_dyn_free(req);
  1140. return result;
  1141. }
  1142. result = Curl_dyn_addf(req,
  1143. "Connection: Upgrade, HTTP2-Settings\r\n"
  1144. "Upgrade: %s\r\n"
  1145. "HTTP2-Settings: %s\r\n",
  1146. NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
  1147. free(base64);
  1148. k->upgr101 = UPGR101_REQUESTED;
  1149. return result;
  1150. }
  1151. /*
  1152. * Returns nonzero if current HTTP/2 session should be closed.
  1153. */
  1154. static int should_close_session(struct http_conn *httpc)
  1155. {
  1156. return httpc->drain_total == 0 && !nghttp2_session_want_read(httpc->h2) &&
  1157. !nghttp2_session_want_write(httpc->h2);
  1158. }
  1159. /*
  1160. * h2_process_pending_input() processes pending input left in
  1161. * httpc->inbuf. Then, call h2_session_send() to send pending data.
  1162. * This function returns 0 if it succeeds, or -1 and error code will
  1163. * be assigned to *err.
  1164. */
  1165. static int h2_process_pending_input(struct connectdata *conn,
  1166. struct http_conn *httpc,
  1167. CURLcode *err)
  1168. {
  1169. ssize_t nread;
  1170. char *inbuf;
  1171. ssize_t rv;
  1172. struct Curl_easy *data = conn->data;
  1173. nread = httpc->inbuflen - httpc->nread_inbuf;
  1174. inbuf = httpc->inbuf + httpc->nread_inbuf;
  1175. rv = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)inbuf, nread);
  1176. if(rv < 0) {
  1177. failf(data,
  1178. "h2_process_pending_input: nghttp2_session_mem_recv() returned "
  1179. "%zd:%s\n", rv, nghttp2_strerror((int)rv));
  1180. *err = CURLE_RECV_ERROR;
  1181. return -1;
  1182. }
  1183. if(nread == rv) {
  1184. H2BUGF(infof(data,
  1185. "h2_process_pending_input: All data in connection buffer "
  1186. "processed\n"));
  1187. httpc->inbuflen = 0;
  1188. httpc->nread_inbuf = 0;
  1189. }
  1190. else {
  1191. httpc->nread_inbuf += rv;
  1192. H2BUGF(infof(data,
  1193. "h2_process_pending_input: %zu bytes left in connection "
  1194. "buffer\n",
  1195. httpc->inbuflen - httpc->nread_inbuf));
  1196. }
  1197. rv = h2_session_send(data, httpc->h2);
  1198. if(rv != 0) {
  1199. *err = CURLE_SEND_ERROR;
  1200. return -1;
  1201. }
  1202. if(should_close_session(httpc)) {
  1203. H2BUGF(infof(data,
  1204. "h2_process_pending_input: nothing to do in this session\n"));
  1205. if(httpc->error_code)
  1206. *err = CURLE_HTTP2;
  1207. else {
  1208. /* not an error per se, but should still close the connection */
  1209. connclose(conn, "GOAWAY received");
  1210. *err = CURLE_OK;
  1211. }
  1212. return -1;
  1213. }
  1214. return 0;
  1215. }
  1216. /*
  1217. * Called from transfer.c:done_sending when we stop uploading.
  1218. */
  1219. CURLcode Curl_http2_done_sending(struct connectdata *conn)
  1220. {
  1221. CURLcode result = CURLE_OK;
  1222. if((conn->handler == &Curl_handler_http2_ssl) ||
  1223. (conn->handler == &Curl_handler_http2)) {
  1224. /* make sure this is only attempted for HTTP/2 transfers */
  1225. struct HTTP *stream = conn->data->req.protop;
  1226. struct http_conn *httpc = &conn->proto.httpc;
  1227. nghttp2_session *h2 = httpc->h2;
  1228. if(stream->upload_left) {
  1229. /* If the stream still thinks there's data left to upload. */
  1230. stream->upload_left = 0; /* DONE! */
  1231. /* resume sending here to trigger the callback to get called again so
  1232. that it can signal EOF to nghttp2 */
  1233. (void)nghttp2_session_resume_data(h2, stream->stream_id);
  1234. (void)h2_process_pending_input(conn, httpc, &result);
  1235. }
  1236. /* If nghttp2 still has pending frames unsent */
  1237. if(nghttp2_session_want_write(h2)) {
  1238. struct Curl_easy *data = conn->data;
  1239. struct SingleRequest *k = &data->req;
  1240. int rv;
  1241. H2BUGF(infof(data, "HTTP/2 still wants to send data (easy %p)\n", data));
  1242. /* re-set KEEP_SEND to make sure we are called again */
  1243. k->keepon |= KEEP_SEND;
  1244. /* and attempt to send the pending frames */
  1245. rv = h2_session_send(data, h2);
  1246. if(rv != 0)
  1247. result = CURLE_SEND_ERROR;
  1248. }
  1249. }
  1250. return result;
  1251. }
  1252. static ssize_t http2_handle_stream_close(struct connectdata *conn,
  1253. struct Curl_easy *data,
  1254. struct HTTP *stream, CURLcode *err)
  1255. {
  1256. struct http_conn *httpc = &conn->proto.httpc;
  1257. if(httpc->pause_stream_id == stream->stream_id) {
  1258. httpc->pause_stream_id = 0;
  1259. }
  1260. drained_transfer(data, httpc);
  1261. if(httpc->pause_stream_id == 0) {
  1262. if(h2_process_pending_input(conn, httpc, err) != 0) {
  1263. return -1;
  1264. }
  1265. }
  1266. DEBUGASSERT(data->state.drain == 0);
  1267. /* Reset to FALSE to prevent infinite loop in readwrite_data function. */
  1268. stream->closed = FALSE;
  1269. if(httpc->error_code == NGHTTP2_REFUSED_STREAM) {
  1270. H2BUGF(infof(data, "REFUSED_STREAM (%d), try again on a new connection!\n",
  1271. stream->stream_id));
  1272. connclose(conn, "REFUSED_STREAM"); /* don't use this anymore */
  1273. data->state.refused_stream = TRUE;
  1274. *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
  1275. return -1;
  1276. }
  1277. else if(httpc->error_code != NGHTTP2_NO_ERROR) {
  1278. failf(data, "HTTP/2 stream %d was not closed cleanly: %s (err %u)",
  1279. stream->stream_id, nghttp2_http2_strerror(httpc->error_code),
  1280. httpc->error_code);
  1281. *err = CURLE_HTTP2_STREAM;
  1282. return -1;
  1283. }
  1284. if(!stream->bodystarted) {
  1285. failf(data, "HTTP/2 stream %d was closed cleanly, but before getting "
  1286. " all response header fields, treated as error",
  1287. stream->stream_id);
  1288. *err = CURLE_HTTP2_STREAM;
  1289. return -1;
  1290. }
  1291. stream->close_handled = TRUE;
  1292. H2BUGF(infof(data, "http2_recv returns 0, http2_handle_stream_close\n"));
  1293. return 0;
  1294. }
  1295. /*
  1296. * h2_pri_spec() fills in the pri_spec struct, used by nghttp2 to send weight
  1297. * and dependency to the peer. It also stores the updated values in the state
  1298. * struct.
  1299. */
  1300. static void h2_pri_spec(struct Curl_easy *data,
  1301. nghttp2_priority_spec *pri_spec)
  1302. {
  1303. struct HTTP *depstream = (data->set.stream_depends_on?
  1304. data->set.stream_depends_on->req.protop:NULL);
  1305. int32_t depstream_id = depstream? depstream->stream_id:0;
  1306. nghttp2_priority_spec_init(pri_spec, depstream_id, data->set.stream_weight,
  1307. data->set.stream_depends_e);
  1308. data->state.stream_weight = data->set.stream_weight;
  1309. data->state.stream_depends_e = data->set.stream_depends_e;
  1310. data->state.stream_depends_on = data->set.stream_depends_on;
  1311. }
  1312. /*
  1313. * h2_session_send() checks if there's been an update in the priority /
  1314. * dependency settings and if so it submits a PRIORITY frame with the updated
  1315. * info.
  1316. */
  1317. static int h2_session_send(struct Curl_easy *data,
  1318. nghttp2_session *h2)
  1319. {
  1320. struct HTTP *stream = data->req.protop;
  1321. if((data->set.stream_weight != data->state.stream_weight) ||
  1322. (data->set.stream_depends_e != data->state.stream_depends_e) ||
  1323. (data->set.stream_depends_on != data->state.stream_depends_on) ) {
  1324. /* send new weight and/or dependency */
  1325. nghttp2_priority_spec pri_spec;
  1326. int rv;
  1327. h2_pri_spec(data, &pri_spec);
  1328. H2BUGF(infof(data, "Queuing PRIORITY on stream %u (easy %p)\n",
  1329. stream->stream_id, data));
  1330. DEBUGASSERT(stream->stream_id != -1);
  1331. rv = nghttp2_submit_priority(h2, NGHTTP2_FLAG_NONE, stream->stream_id,
  1332. &pri_spec);
  1333. if(rv)
  1334. return rv;
  1335. }
  1336. return nghttp2_session_send(h2);
  1337. }
  1338. static ssize_t http2_recv(struct connectdata *conn, int sockindex,
  1339. char *mem, size_t len, CURLcode *err)
  1340. {
  1341. CURLcode result = CURLE_OK;
  1342. ssize_t rv;
  1343. ssize_t nread;
  1344. struct http_conn *httpc = &conn->proto.httpc;
  1345. struct Curl_easy *data = conn->data;
  1346. struct HTTP *stream = data->req.protop;
  1347. (void)sockindex; /* we always do HTTP2 on sockindex 0 */
  1348. if(should_close_session(httpc)) {
  1349. H2BUGF(infof(data,
  1350. "http2_recv: nothing to do in this session\n"));
  1351. if(conn->bits.close) {
  1352. /* already marked for closure, return OK and we're done */
  1353. *err = CURLE_OK;
  1354. return 0;
  1355. }
  1356. *err = CURLE_HTTP2;
  1357. return -1;
  1358. }
  1359. /* Nullify here because we call nghttp2_session_send() and they
  1360. might refer to the old buffer. */
  1361. stream->upload_mem = NULL;
  1362. stream->upload_len = 0;
  1363. /*
  1364. * At this point 'stream' is just in the Curl_easy the connection
  1365. * identifies as its owner at this time.
  1366. */
  1367. if(stream->bodystarted &&
  1368. stream->nread_header_recvbuf < Curl_dyn_len(&stream->header_recvbuf)) {
  1369. /* If there is header data pending for this stream to return, do that */
  1370. size_t left =
  1371. Curl_dyn_len(&stream->header_recvbuf) - stream->nread_header_recvbuf;
  1372. size_t ncopy = CURLMIN(len, left);
  1373. memcpy(mem, Curl_dyn_ptr(&stream->header_recvbuf) +
  1374. stream->nread_header_recvbuf, ncopy);
  1375. stream->nread_header_recvbuf += ncopy;
  1376. H2BUGF(infof(data, "http2_recv: Got %d bytes from header_recvbuf\n",
  1377. (int)ncopy));
  1378. return ncopy;
  1379. }
  1380. H2BUGF(infof(data, "http2_recv: easy %p (stream %u) win %u/%u\n",
  1381. data, stream->stream_id,
  1382. nghttp2_session_get_local_window_size(httpc->h2),
  1383. nghttp2_session_get_stream_local_window_size(httpc->h2,
  1384. stream->stream_id)
  1385. ));
  1386. if((data->state.drain) && stream->memlen) {
  1387. H2BUGF(infof(data, "http2_recv: DRAIN %zu bytes stream %u!! (%p => %p)\n",
  1388. stream->memlen, stream->stream_id,
  1389. stream->mem, mem));
  1390. if(mem != stream->mem) {
  1391. /* if we didn't get the same buffer this time, we must move the data to
  1392. the beginning */
  1393. memmove(mem, stream->mem, stream->memlen);
  1394. stream->len = len - stream->memlen;
  1395. stream->mem = mem;
  1396. }
  1397. if(httpc->pause_stream_id == stream->stream_id && !stream->pausedata) {
  1398. /* We have paused nghttp2, but we have no pause data (see
  1399. on_data_chunk_recv). */
  1400. httpc->pause_stream_id = 0;
  1401. if(h2_process_pending_input(conn, httpc, &result) != 0) {
  1402. *err = result;
  1403. return -1;
  1404. }
  1405. }
  1406. }
  1407. else if(stream->pausedata) {
  1408. DEBUGASSERT(httpc->pause_stream_id == stream->stream_id);
  1409. nread = CURLMIN(len, stream->pauselen);
  1410. memcpy(mem, stream->pausedata, nread);
  1411. stream->pausedata += nread;
  1412. stream->pauselen -= nread;
  1413. if(stream->pauselen == 0) {
  1414. H2BUGF(infof(data, "Unpaused by stream %u\n", stream->stream_id));
  1415. DEBUGASSERT(httpc->pause_stream_id == stream->stream_id);
  1416. httpc->pause_stream_id = 0;
  1417. stream->pausedata = NULL;
  1418. stream->pauselen = 0;
  1419. /* When NGHTTP2_ERR_PAUSE is returned from
  1420. data_source_read_callback, we might not process DATA frame
  1421. fully. Calling nghttp2_session_mem_recv() again will
  1422. continue to process DATA frame, but if there is no incoming
  1423. frames, then we have to call it again with 0-length data.
  1424. Without this, on_stream_close callback will not be called,
  1425. and stream could be hanged. */
  1426. if(h2_process_pending_input(conn, httpc, &result) != 0) {
  1427. *err = result;
  1428. return -1;
  1429. }
  1430. }
  1431. H2BUGF(infof(data, "http2_recv: returns unpaused %zd bytes on stream %u\n",
  1432. nread, stream->stream_id));
  1433. return nread;
  1434. }
  1435. else if(httpc->pause_stream_id) {
  1436. /* If a stream paused nghttp2_session_mem_recv previously, and has
  1437. not processed all data, it still refers to the buffer in
  1438. nghttp2_session. If we call nghttp2_session_mem_recv(), we may
  1439. overwrite that buffer. To avoid that situation, just return
  1440. here with CURLE_AGAIN. This could be busy loop since data in
  1441. socket is not read. But it seems that usually streams are
  1442. notified with its drain property, and socket is read again
  1443. quickly. */
  1444. if(stream->closed)
  1445. /* closed overrides paused */
  1446. return 0;
  1447. H2BUGF(infof(data, "stream %x is paused, pause id: %x\n",
  1448. stream->stream_id, httpc->pause_stream_id));
  1449. *err = CURLE_AGAIN;
  1450. return -1;
  1451. }
  1452. else {
  1453. char *inbuf;
  1454. /* remember where to store incoming data for this stream and how big the
  1455. buffer is */
  1456. stream->mem = mem;
  1457. stream->len = len;
  1458. stream->memlen = 0;
  1459. if(httpc->inbuflen == 0) {
  1460. nread = ((Curl_recv *)httpc->recv_underlying)(
  1461. conn, FIRSTSOCKET, httpc->inbuf, H2_BUFSIZE, &result);
  1462. if(nread == -1) {
  1463. if(result != CURLE_AGAIN)
  1464. failf(data, "Failed receiving HTTP2 data");
  1465. else if(stream->closed)
  1466. /* received when the stream was already closed! */
  1467. return http2_handle_stream_close(conn, data, stream, err);
  1468. *err = result;
  1469. return -1;
  1470. }
  1471. if(nread == 0) {
  1472. H2BUGF(infof(data, "end of stream\n"));
  1473. *err = CURLE_OK;
  1474. return 0;
  1475. }
  1476. H2BUGF(infof(data, "nread=%zd\n", nread));
  1477. httpc->inbuflen = nread;
  1478. inbuf = httpc->inbuf;
  1479. }
  1480. else {
  1481. nread = httpc->inbuflen - httpc->nread_inbuf;
  1482. inbuf = httpc->inbuf + httpc->nread_inbuf;
  1483. H2BUGF(infof(data, "Use data left in connection buffer, nread=%zd\n",
  1484. nread));
  1485. }
  1486. rv = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)inbuf, nread);
  1487. if(nghttp2_is_fatal((int)rv)) {
  1488. failf(data, "nghttp2_session_mem_recv() returned %zd:%s\n",
  1489. rv, nghttp2_strerror((int)rv));
  1490. *err = CURLE_RECV_ERROR;
  1491. return -1;
  1492. }
  1493. H2BUGF(infof(data, "nghttp2_session_mem_recv() returns %zd\n", rv));
  1494. if(nread == rv) {
  1495. H2BUGF(infof(data, "All data in connection buffer processed\n"));
  1496. httpc->inbuflen = 0;
  1497. httpc->nread_inbuf = 0;
  1498. }
  1499. else {
  1500. httpc->nread_inbuf += rv;
  1501. H2BUGF(infof(data, "%zu bytes left in connection buffer\n",
  1502. httpc->inbuflen - httpc->nread_inbuf));
  1503. }
  1504. /* Always send pending frames in nghttp2 session, because
  1505. nghttp2_session_mem_recv() may queue new frame */
  1506. rv = h2_session_send(data, httpc->h2);
  1507. if(rv != 0) {
  1508. *err = CURLE_SEND_ERROR;
  1509. return -1;
  1510. }
  1511. if(should_close_session(httpc)) {
  1512. H2BUGF(infof(data, "http2_recv: nothing to do in this session\n"));
  1513. *err = CURLE_HTTP2;
  1514. return -1;
  1515. }
  1516. }
  1517. if(stream->memlen) {
  1518. ssize_t retlen = stream->memlen;
  1519. H2BUGF(infof(data, "http2_recv: returns %zd for stream %u\n",
  1520. retlen, stream->stream_id));
  1521. stream->memlen = 0;
  1522. if(httpc->pause_stream_id == stream->stream_id) {
  1523. /* data for this stream is returned now, but this stream caused a pause
  1524. already so we need it called again asap */
  1525. H2BUGF(infof(data, "Data returned for PAUSED stream %u\n",
  1526. stream->stream_id));
  1527. }
  1528. else if(!stream->closed) {
  1529. drained_transfer(data, httpc);
  1530. }
  1531. else
  1532. /* this stream is closed, trigger a another read ASAP to detect that */
  1533. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  1534. return retlen;
  1535. }
  1536. if(stream->closed)
  1537. return 0;
  1538. *err = CURLE_AGAIN;
  1539. H2BUGF(infof(data, "http2_recv returns AGAIN for stream %u\n",
  1540. stream->stream_id));
  1541. return -1;
  1542. }
  1543. /* Index where :authority header field will appear in request header
  1544. field list. */
  1545. #define AUTHORITY_DST_IDX 3
  1546. /* USHRT_MAX is 65535 == 0xffff */
  1547. #define HEADER_OVERFLOW(x) \
  1548. (x.namelen > 0xffff || x.valuelen > 0xffff - x.namelen)
  1549. /*
  1550. * Check header memory for the token "trailers".
  1551. * Parse the tokens as separated by comma and surrounded by whitespace.
  1552. * Returns TRUE if found or FALSE if not.
  1553. */
  1554. static bool contains_trailers(const char *p, size_t len)
  1555. {
  1556. const char *end = p + len;
  1557. for(;;) {
  1558. for(; p != end && (*p == ' ' || *p == '\t'); ++p)
  1559. ;
  1560. if(p == end || (size_t)(end - p) < sizeof("trailers") - 1)
  1561. return FALSE;
  1562. if(strncasecompare("trailers", p, sizeof("trailers") - 1)) {
  1563. p += sizeof("trailers") - 1;
  1564. for(; p != end && (*p == ' ' || *p == '\t'); ++p)
  1565. ;
  1566. if(p == end || *p == ',')
  1567. return TRUE;
  1568. }
  1569. /* skip to next token */
  1570. for(; p != end && *p != ','; ++p)
  1571. ;
  1572. if(p == end)
  1573. return FALSE;
  1574. ++p;
  1575. }
  1576. }
  1577. typedef enum {
  1578. /* Send header to server */
  1579. HEADERINST_FORWARD,
  1580. /* Don't send header to server */
  1581. HEADERINST_IGNORE,
  1582. /* Discard header, and replace it with "te: trailers" */
  1583. HEADERINST_TE_TRAILERS
  1584. } header_instruction;
  1585. /* Decides how to treat given header field. */
  1586. static header_instruction inspect_header(const char *name, size_t namelen,
  1587. const char *value, size_t valuelen) {
  1588. switch(namelen) {
  1589. case 2:
  1590. if(!strncasecompare("te", name, namelen))
  1591. return HEADERINST_FORWARD;
  1592. return contains_trailers(value, valuelen) ?
  1593. HEADERINST_TE_TRAILERS : HEADERINST_IGNORE;
  1594. case 7:
  1595. return strncasecompare("upgrade", name, namelen) ?
  1596. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1597. case 10:
  1598. return (strncasecompare("connection", name, namelen) ||
  1599. strncasecompare("keep-alive", name, namelen)) ?
  1600. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1601. case 16:
  1602. return strncasecompare("proxy-connection", name, namelen) ?
  1603. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1604. case 17:
  1605. return strncasecompare("transfer-encoding", name, namelen) ?
  1606. HEADERINST_IGNORE : HEADERINST_FORWARD;
  1607. default:
  1608. return HEADERINST_FORWARD;
  1609. }
  1610. }
  1611. static ssize_t http2_send(struct connectdata *conn, int sockindex,
  1612. const void *mem, size_t len, CURLcode *err)
  1613. {
  1614. /*
  1615. * Currently, we send request in this function, but this function is also
  1616. * used to send request body. It would be nice to add dedicated function for
  1617. * request.
  1618. */
  1619. int rv;
  1620. struct http_conn *httpc = &conn->proto.httpc;
  1621. struct HTTP *stream = conn->data->req.protop;
  1622. nghttp2_nv *nva = NULL;
  1623. size_t nheader;
  1624. size_t i;
  1625. size_t authority_idx;
  1626. char *hdbuf = (char *)mem;
  1627. char *end, *line_end;
  1628. nghttp2_data_provider data_prd;
  1629. int32_t stream_id;
  1630. nghttp2_session *h2 = httpc->h2;
  1631. nghttp2_priority_spec pri_spec;
  1632. (void)sockindex;
  1633. H2BUGF(infof(conn->data, "http2_send len=%zu\n", len));
  1634. if(stream->stream_id != -1) {
  1635. if(stream->close_handled) {
  1636. infof(conn->data, "stream %d closed\n", stream->stream_id);
  1637. *err = CURLE_HTTP2_STREAM;
  1638. return -1;
  1639. }
  1640. else if(stream->closed) {
  1641. return http2_handle_stream_close(conn, conn->data, stream, err);
  1642. }
  1643. /* If stream_id != -1, we have dispatched request HEADERS, and now
  1644. are going to send or sending request body in DATA frame */
  1645. stream->upload_mem = mem;
  1646. stream->upload_len = len;
  1647. rv = nghttp2_session_resume_data(h2, stream->stream_id);
  1648. if(nghttp2_is_fatal(rv)) {
  1649. *err = CURLE_SEND_ERROR;
  1650. return -1;
  1651. }
  1652. rv = h2_session_send(conn->data, h2);
  1653. if(nghttp2_is_fatal(rv)) {
  1654. *err = CURLE_SEND_ERROR;
  1655. return -1;
  1656. }
  1657. len -= stream->upload_len;
  1658. /* Nullify here because we call nghttp2_session_send() and they
  1659. might refer to the old buffer. */
  1660. stream->upload_mem = NULL;
  1661. stream->upload_len = 0;
  1662. if(should_close_session(httpc)) {
  1663. H2BUGF(infof(conn->data, "http2_send: nothing to do in this session\n"));
  1664. *err = CURLE_HTTP2;
  1665. return -1;
  1666. }
  1667. if(stream->upload_left) {
  1668. /* we are sure that we have more data to send here. Calling the
  1669. following API will make nghttp2_session_want_write() return
  1670. nonzero if remote window allows it, which then libcurl checks
  1671. socket is writable or not. See http2_perform_getsock(). */
  1672. nghttp2_session_resume_data(h2, stream->stream_id);
  1673. }
  1674. H2BUGF(infof(conn->data, "http2_send returns %zu for stream %u\n", len,
  1675. stream->stream_id));
  1676. return len;
  1677. }
  1678. /* Calculate number of headers contained in [mem, mem + len) */
  1679. /* Here, we assume the curl http code generate *correct* HTTP header
  1680. field block */
  1681. nheader = 0;
  1682. for(i = 1; i < len; ++i) {
  1683. if(hdbuf[i] == '\n' && hdbuf[i - 1] == '\r') {
  1684. ++nheader;
  1685. ++i;
  1686. }
  1687. }
  1688. if(nheader < 2)
  1689. goto fail;
  1690. /* We counted additional 2 \r\n in the first and last line. We need 3
  1691. new headers: :method, :path and :scheme. Therefore we need one
  1692. more space. */
  1693. nheader += 1;
  1694. nva = malloc(sizeof(nghttp2_nv) * nheader);
  1695. if(nva == NULL) {
  1696. *err = CURLE_OUT_OF_MEMORY;
  1697. return -1;
  1698. }
  1699. /* Extract :method, :path from request line
  1700. We do line endings with CRLF so checking for CR is enough */
  1701. line_end = memchr(hdbuf, '\r', len);
  1702. if(!line_end)
  1703. goto fail;
  1704. /* Method does not contain spaces */
  1705. end = memchr(hdbuf, ' ', line_end - hdbuf);
  1706. if(!end || end == hdbuf)
  1707. goto fail;
  1708. nva[0].name = (unsigned char *)":method";
  1709. nva[0].namelen = strlen((char *)nva[0].name);
  1710. nva[0].value = (unsigned char *)hdbuf;
  1711. nva[0].valuelen = (size_t)(end - hdbuf);
  1712. nva[0].flags = NGHTTP2_NV_FLAG_NONE;
  1713. if(HEADER_OVERFLOW(nva[0])) {
  1714. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1715. goto fail;
  1716. }
  1717. hdbuf = end + 1;
  1718. /* Path may contain spaces so scan backwards */
  1719. end = NULL;
  1720. for(i = (size_t)(line_end - hdbuf); i; --i) {
  1721. if(hdbuf[i - 1] == ' ') {
  1722. end = &hdbuf[i - 1];
  1723. break;
  1724. }
  1725. }
  1726. if(!end || end == hdbuf)
  1727. goto fail;
  1728. nva[1].name = (unsigned char *)":path";
  1729. nva[1].namelen = strlen((char *)nva[1].name);
  1730. nva[1].value = (unsigned char *)hdbuf;
  1731. nva[1].valuelen = (size_t)(end - hdbuf);
  1732. nva[1].flags = NGHTTP2_NV_FLAG_NONE;
  1733. if(HEADER_OVERFLOW(nva[1])) {
  1734. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1735. goto fail;
  1736. }
  1737. nva[2].name = (unsigned char *)":scheme";
  1738. nva[2].namelen = strlen((char *)nva[2].name);
  1739. if(conn->handler->flags & PROTOPT_SSL)
  1740. nva[2].value = (unsigned char *)"https";
  1741. else
  1742. nva[2].value = (unsigned char *)"http";
  1743. nva[2].valuelen = strlen((char *)nva[2].value);
  1744. nva[2].flags = NGHTTP2_NV_FLAG_NONE;
  1745. if(HEADER_OVERFLOW(nva[2])) {
  1746. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1747. goto fail;
  1748. }
  1749. authority_idx = 0;
  1750. i = 3;
  1751. while(i < nheader) {
  1752. size_t hlen;
  1753. hdbuf = line_end + 2;
  1754. /* check for next CR, but only within the piece of data left in the given
  1755. buffer */
  1756. line_end = memchr(hdbuf, '\r', len - (hdbuf - (char *)mem));
  1757. if(!line_end || (line_end == hdbuf))
  1758. goto fail;
  1759. /* header continuation lines are not supported */
  1760. if(*hdbuf == ' ' || *hdbuf == '\t')
  1761. goto fail;
  1762. for(end = hdbuf; end < line_end && *end != ':'; ++end)
  1763. ;
  1764. if(end == hdbuf || end == line_end)
  1765. goto fail;
  1766. hlen = end - hdbuf;
  1767. if(hlen == 4 && strncasecompare("host", hdbuf, 4)) {
  1768. authority_idx = i;
  1769. nva[i].name = (unsigned char *)":authority";
  1770. nva[i].namelen = strlen((char *)nva[i].name);
  1771. }
  1772. else {
  1773. nva[i].namelen = (size_t)(end - hdbuf);
  1774. /* Lower case the header name for HTTP/2 */
  1775. Curl_strntolower((char *)hdbuf, hdbuf, nva[i].namelen);
  1776. nva[i].name = (unsigned char *)hdbuf;
  1777. }
  1778. hdbuf = end + 1;
  1779. while(*hdbuf == ' ' || *hdbuf == '\t')
  1780. ++hdbuf;
  1781. end = line_end;
  1782. switch(inspect_header((const char *)nva[i].name, nva[i].namelen, hdbuf,
  1783. end - hdbuf)) {
  1784. case HEADERINST_IGNORE:
  1785. /* skip header fields prohibited by HTTP/2 specification. */
  1786. --nheader;
  1787. continue;
  1788. case HEADERINST_TE_TRAILERS:
  1789. nva[i].value = (uint8_t*)"trailers";
  1790. nva[i].valuelen = sizeof("trailers") - 1;
  1791. break;
  1792. default:
  1793. nva[i].value = (unsigned char *)hdbuf;
  1794. nva[i].valuelen = (size_t)(end - hdbuf);
  1795. }
  1796. nva[i].flags = NGHTTP2_NV_FLAG_NONE;
  1797. if(HEADER_OVERFLOW(nva[i])) {
  1798. failf(conn->data, "Failed sending HTTP request: Header overflow");
  1799. goto fail;
  1800. }
  1801. ++i;
  1802. }
  1803. /* :authority must come before non-pseudo header fields */
  1804. if(authority_idx != 0 && authority_idx != AUTHORITY_DST_IDX) {
  1805. nghttp2_nv authority = nva[authority_idx];
  1806. for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
  1807. nva[i] = nva[i - 1];
  1808. }
  1809. nva[i] = authority;
  1810. }
  1811. /* Warn stream may be rejected if cumulative length of headers is too large.
  1812. It appears nghttp2 will not send a header frame larger than 64KB. */
  1813. #define MAX_ACC 60000 /* <64KB to account for some overhead */
  1814. {
  1815. size_t acc = 0;
  1816. for(i = 0; i < nheader; ++i) {
  1817. acc += nva[i].namelen + nva[i].valuelen;
  1818. H2BUGF(infof(conn->data, "h2 header: %.*s:%.*s\n",
  1819. nva[i].namelen, nva[i].name,
  1820. nva[i].valuelen, nva[i].value));
  1821. }
  1822. if(acc > MAX_ACC) {
  1823. infof(conn->data, "http2_send: Warning: The cumulative length of all "
  1824. "headers exceeds %zu bytes and that could cause the "
  1825. "stream to be rejected.\n", MAX_ACC);
  1826. }
  1827. }
  1828. h2_pri_spec(conn->data, &pri_spec);
  1829. H2BUGF(infof(conn->data, "http2_send request allowed %d (easy handle %p)\n",
  1830. nghttp2_session_check_request_allowed(h2), (void *)conn->data));
  1831. switch(conn->data->state.httpreq) {
  1832. case HTTPREQ_POST:
  1833. case HTTPREQ_POST_FORM:
  1834. case HTTPREQ_POST_MIME:
  1835. case HTTPREQ_PUT:
  1836. if(conn->data->state.infilesize != -1)
  1837. stream->upload_left = conn->data->state.infilesize;
  1838. else
  1839. /* data sending without specifying the data amount up front */
  1840. stream->upload_left = -1; /* unknown, but not zero */
  1841. data_prd.read_callback = data_source_read_callback;
  1842. data_prd.source.ptr = NULL;
  1843. stream_id = nghttp2_submit_request(h2, &pri_spec, nva, nheader,
  1844. &data_prd, conn->data);
  1845. break;
  1846. default:
  1847. stream_id = nghttp2_submit_request(h2, &pri_spec, nva, nheader,
  1848. NULL, conn->data);
  1849. }
  1850. Curl_safefree(nva);
  1851. if(stream_id < 0) {
  1852. H2BUGF(infof(conn->data, "http2_send() send error\n"));
  1853. *err = CURLE_SEND_ERROR;
  1854. return -1;
  1855. }
  1856. infof(conn->data, "Using Stream ID: %x (easy handle %p)\n",
  1857. stream_id, (void *)conn->data);
  1858. stream->stream_id = stream_id;
  1859. /* this does not call h2_session_send() since there can not have been any
  1860. * priority upodate since the nghttp2_submit_request() call above */
  1861. rv = nghttp2_session_send(h2);
  1862. if(rv != 0) {
  1863. *err = CURLE_SEND_ERROR;
  1864. return -1;
  1865. }
  1866. if(should_close_session(httpc)) {
  1867. H2BUGF(infof(conn->data, "http2_send: nothing to do in this session\n"));
  1868. *err = CURLE_HTTP2;
  1869. return -1;
  1870. }
  1871. /* If whole HEADERS frame was sent off to the underlying socket, the nghttp2
  1872. library calls data_source_read_callback. But only it found that no data
  1873. available, so it deferred the DATA transmission. Which means that
  1874. nghttp2_session_want_write() returns 0 on http2_perform_getsock(), which
  1875. results that no writable socket check is performed. To workaround this,
  1876. we issue nghttp2_session_resume_data() here to bring back DATA
  1877. transmission from deferred state. */
  1878. nghttp2_session_resume_data(h2, stream->stream_id);
  1879. return len;
  1880. fail:
  1881. free(nva);
  1882. *err = CURLE_SEND_ERROR;
  1883. return -1;
  1884. }
  1885. CURLcode Curl_http2_setup(struct connectdata *conn)
  1886. {
  1887. CURLcode result;
  1888. struct http_conn *httpc = &conn->proto.httpc;
  1889. struct HTTP *stream = conn->data->req.protop;
  1890. DEBUGASSERT(conn->data->state.buffer);
  1891. stream->stream_id = -1;
  1892. Curl_dyn_init(&stream->header_recvbuf, DYN_H2_HEADERS);
  1893. if((conn->handler == &Curl_handler_http2_ssl) ||
  1894. (conn->handler == &Curl_handler_http2))
  1895. return CURLE_OK; /* already done */
  1896. if(conn->handler->flags & PROTOPT_SSL)
  1897. conn->handler = &Curl_handler_http2_ssl;
  1898. else
  1899. conn->handler = &Curl_handler_http2;
  1900. result = http2_init(conn);
  1901. if(result) {
  1902. Curl_dyn_free(&stream->header_recvbuf);
  1903. return result;
  1904. }
  1905. infof(conn->data, "Using HTTP2, server supports multi-use\n");
  1906. stream->upload_left = 0;
  1907. stream->upload_mem = NULL;
  1908. stream->upload_len = 0;
  1909. stream->mem = conn->data->state.buffer;
  1910. stream->len = conn->data->set.buffer_size;
  1911. httpc->inbuflen = 0;
  1912. httpc->nread_inbuf = 0;
  1913. httpc->pause_stream_id = 0;
  1914. httpc->drain_total = 0;
  1915. conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  1916. conn->httpversion = 20;
  1917. conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  1918. infof(conn->data, "Connection state changed (HTTP/2 confirmed)\n");
  1919. multi_connchanged(conn->data->multi);
  1920. return CURLE_OK;
  1921. }
  1922. CURLcode Curl_http2_switched(struct connectdata *conn,
  1923. const char *mem, size_t nread)
  1924. {
  1925. CURLcode result;
  1926. struct http_conn *httpc = &conn->proto.httpc;
  1927. int rv;
  1928. ssize_t nproc;
  1929. struct Curl_easy *data = conn->data;
  1930. struct HTTP *stream = conn->data->req.protop;
  1931. result = Curl_http2_setup(conn);
  1932. if(result)
  1933. return result;
  1934. httpc->recv_underlying = conn->recv[FIRSTSOCKET];
  1935. httpc->send_underlying = conn->send[FIRSTSOCKET];
  1936. conn->recv[FIRSTSOCKET] = http2_recv;
  1937. conn->send[FIRSTSOCKET] = http2_send;
  1938. if(conn->data->req.upgr101 == UPGR101_RECEIVED) {
  1939. /* stream 1 is opened implicitly on upgrade */
  1940. stream->stream_id = 1;
  1941. /* queue SETTINGS frame (again) */
  1942. rv = nghttp2_session_upgrade(httpc->h2, httpc->binsettings,
  1943. httpc->binlen, NULL);
  1944. if(rv != 0) {
  1945. failf(data, "nghttp2_session_upgrade() failed: %s(%d)",
  1946. nghttp2_strerror(rv), rv);
  1947. return CURLE_HTTP2;
  1948. }
  1949. rv = nghttp2_session_set_stream_user_data(httpc->h2,
  1950. stream->stream_id,
  1951. data);
  1952. if(rv) {
  1953. infof(data, "http/2: failed to set user_data for stream %d!\n",
  1954. stream->stream_id);
  1955. DEBUGASSERT(0);
  1956. }
  1957. }
  1958. else {
  1959. populate_settings(conn, httpc);
  1960. /* stream ID is unknown at this point */
  1961. stream->stream_id = -1;
  1962. rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE,
  1963. httpc->local_settings,
  1964. httpc->local_settings_num);
  1965. if(rv != 0) {
  1966. failf(data, "nghttp2_submit_settings() failed: %s(%d)",
  1967. nghttp2_strerror(rv), rv);
  1968. return CURLE_HTTP2;
  1969. }
  1970. }
  1971. rv = nghttp2_session_set_local_window_size(httpc->h2, NGHTTP2_FLAG_NONE, 0,
  1972. HTTP2_HUGE_WINDOW_SIZE);
  1973. if(rv != 0) {
  1974. failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
  1975. nghttp2_strerror(rv), rv);
  1976. return CURLE_HTTP2;
  1977. }
  1978. /* we are going to copy mem to httpc->inbuf. This is required since
  1979. mem is part of buffer pointed by stream->mem, and callbacks
  1980. called by nghttp2_session_mem_recv() will write stream specific
  1981. data into stream->mem, overwriting data already there. */
  1982. if(H2_BUFSIZE < nread) {
  1983. failf(data, "connection buffer size is too small to store data following "
  1984. "HTTP Upgrade response header: buflen=%zu, datalen=%zu",
  1985. H2_BUFSIZE, nread);
  1986. return CURLE_HTTP2;
  1987. }
  1988. infof(conn->data, "Copying HTTP/2 data in stream buffer to connection buffer"
  1989. " after upgrade: len=%zu\n",
  1990. nread);
  1991. if(nread)
  1992. memcpy(httpc->inbuf, mem, nread);
  1993. httpc->inbuflen = nread;
  1994. nproc = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)httpc->inbuf,
  1995. httpc->inbuflen);
  1996. if(nghttp2_is_fatal((int)nproc)) {
  1997. failf(data, "nghttp2_session_mem_recv() failed: %s(%d)",
  1998. nghttp2_strerror((int)nproc), (int)nproc);
  1999. return CURLE_HTTP2;
  2000. }
  2001. H2BUGF(infof(data, "nghttp2_session_mem_recv() returns %zd\n", nproc));
  2002. if((ssize_t)nread == nproc) {
  2003. httpc->inbuflen = 0;
  2004. httpc->nread_inbuf = 0;
  2005. }
  2006. else {
  2007. httpc->nread_inbuf += nproc;
  2008. }
  2009. /* Try to send some frames since we may read SETTINGS already. */
  2010. rv = h2_session_send(data, httpc->h2);
  2011. if(rv != 0) {
  2012. failf(data, "nghttp2_session_send() failed: %s(%d)",
  2013. nghttp2_strerror(rv), rv);
  2014. return CURLE_HTTP2;
  2015. }
  2016. if(should_close_session(httpc)) {
  2017. H2BUGF(infof(data,
  2018. "nghttp2_session_send(): nothing to do in this session\n"));
  2019. return CURLE_HTTP2;
  2020. }
  2021. return CURLE_OK;
  2022. }
  2023. CURLcode Curl_http2_stream_pause(struct Curl_easy *data, bool pause)
  2024. {
  2025. DEBUGASSERT(data);
  2026. DEBUGASSERT(data->conn);
  2027. /* if it isn't HTTP/2, we're done */
  2028. if(!data->conn->proto.httpc.h2)
  2029. return CURLE_OK;
  2030. #ifdef NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE
  2031. else {
  2032. struct HTTP *stream = data->req.protop;
  2033. struct http_conn *httpc = &data->conn->proto.httpc;
  2034. uint32_t window = !pause * HTTP2_HUGE_WINDOW_SIZE;
  2035. int rv = nghttp2_session_set_local_window_size(httpc->h2,
  2036. NGHTTP2_FLAG_NONE,
  2037. stream->stream_id,
  2038. window);
  2039. if(rv) {
  2040. failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
  2041. nghttp2_strerror(rv), rv);
  2042. return CURLE_HTTP2;
  2043. }
  2044. /* make sure the window update gets sent */
  2045. rv = h2_session_send(data, httpc->h2);
  2046. if(rv)
  2047. return CURLE_SEND_ERROR;
  2048. DEBUGF(infof(data, "Set HTTP/2 window size to %u for stream %u\n",
  2049. window, stream->stream_id));
  2050. #ifdef DEBUGBUILD
  2051. {
  2052. /* read out the stream local window again */
  2053. uint32_t window2 =
  2054. nghttp2_session_get_stream_local_window_size(httpc->h2,
  2055. stream->stream_id);
  2056. DEBUGF(infof(data, "HTTP/2 window size is now %u for stream %u\n",
  2057. window2, stream->stream_id));
  2058. }
  2059. #endif
  2060. }
  2061. #endif
  2062. return CURLE_OK;
  2063. }
  2064. CURLcode Curl_http2_add_child(struct Curl_easy *parent,
  2065. struct Curl_easy *child,
  2066. bool exclusive)
  2067. {
  2068. if(parent) {
  2069. struct Curl_http2_dep **tail;
  2070. struct Curl_http2_dep *dep = calloc(1, sizeof(struct Curl_http2_dep));
  2071. if(!dep)
  2072. return CURLE_OUT_OF_MEMORY;
  2073. dep->data = child;
  2074. if(parent->set.stream_dependents && exclusive) {
  2075. struct Curl_http2_dep *node = parent->set.stream_dependents;
  2076. while(node) {
  2077. node->data->set.stream_depends_on = child;
  2078. node = node->next;
  2079. }
  2080. tail = &child->set.stream_dependents;
  2081. while(*tail)
  2082. tail = &(*tail)->next;
  2083. DEBUGASSERT(!*tail);
  2084. *tail = parent->set.stream_dependents;
  2085. parent->set.stream_dependents = 0;
  2086. }
  2087. tail = &parent->set.stream_dependents;
  2088. while(*tail) {
  2089. (*tail)->data->set.stream_depends_e = FALSE;
  2090. tail = &(*tail)->next;
  2091. }
  2092. DEBUGASSERT(!*tail);
  2093. *tail = dep;
  2094. }
  2095. child->set.stream_depends_on = parent;
  2096. child->set.stream_depends_e = exclusive;
  2097. return CURLE_OK;
  2098. }
  2099. void Curl_http2_remove_child(struct Curl_easy *parent, struct Curl_easy *child)
  2100. {
  2101. struct Curl_http2_dep *last = 0;
  2102. struct Curl_http2_dep *data = parent->set.stream_dependents;
  2103. DEBUGASSERT(child->set.stream_depends_on == parent);
  2104. while(data && data->data != child) {
  2105. last = data;
  2106. data = data->next;
  2107. }
  2108. DEBUGASSERT(data);
  2109. if(data) {
  2110. if(last) {
  2111. last->next = data->next;
  2112. }
  2113. else {
  2114. parent->set.stream_dependents = data->next;
  2115. }
  2116. free(data);
  2117. }
  2118. child->set.stream_depends_on = 0;
  2119. child->set.stream_depends_e = FALSE;
  2120. }
  2121. void Curl_http2_cleanup_dependencies(struct Curl_easy *data)
  2122. {
  2123. while(data->set.stream_dependents) {
  2124. struct Curl_easy *tmp = data->set.stream_dependents->data;
  2125. Curl_http2_remove_child(data, tmp);
  2126. if(data->set.stream_depends_on)
  2127. Curl_http2_add_child(data->set.stream_depends_on, tmp, FALSE);
  2128. }
  2129. if(data->set.stream_depends_on)
  2130. Curl_http2_remove_child(data->set.stream_depends_on, data);
  2131. }
  2132. /* Only call this function for a transfer that already got a HTTP/2
  2133. CURLE_HTTP2_STREAM error! */
  2134. bool Curl_h2_http_1_1_error(struct connectdata *conn)
  2135. {
  2136. struct http_conn *httpc = &conn->proto.httpc;
  2137. return (httpc->error_code == NGHTTP2_HTTP_1_1_REQUIRED);
  2138. }
  2139. #else /* !USE_NGHTTP2 */
  2140. /* Satisfy external references even if http2 is not compiled in. */
  2141. #include <curl/curl.h>
  2142. char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
  2143. {
  2144. (void) h;
  2145. (void) num;
  2146. return NULL;
  2147. }
  2148. char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
  2149. {
  2150. (void) h;
  2151. (void) header;
  2152. return NULL;
  2153. }
  2154. #endif /* USE_NGHTTP2 */