http2.c 63 KB

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