http2.c 67 KB

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