quiche.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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.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_QUICHE
  24. #include <quiche.h>
  25. #include <openssl/err.h>
  26. #include "urldata.h"
  27. #include "sendf.h"
  28. #include "strdup.h"
  29. #include "rand.h"
  30. #include "quic.h"
  31. #include "strcase.h"
  32. #include "multiif.h"
  33. #include "connect.h"
  34. #include "strerror.h"
  35. #include "vquic.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 DEBUG_HTTP3
  41. /* #define DEBUG_QUICHE */
  42. #ifdef DEBUG_HTTP3
  43. #define H3BUGF(x) x
  44. #else
  45. #define H3BUGF(x) do { } while(0)
  46. #endif
  47. #define QUIC_MAX_STREAMS (256*1024)
  48. #define QUIC_MAX_DATA (1*1024*1024)
  49. #define QUIC_IDLE_TIMEOUT (60 * 1000) /* milliseconds */
  50. static CURLcode process_ingress(struct Curl_easy *data,
  51. curl_socket_t sockfd,
  52. struct quicsocket *qs);
  53. static CURLcode flush_egress(struct Curl_easy *data, curl_socket_t sockfd,
  54. struct quicsocket *qs);
  55. static CURLcode http_request(struct Curl_easy *data, const void *mem,
  56. size_t len);
  57. static Curl_recv h3_stream_recv;
  58. static Curl_send h3_stream_send;
  59. static int quiche_getsock(struct Curl_easy *data,
  60. struct connectdata *conn, curl_socket_t *socks)
  61. {
  62. struct SingleRequest *k = &data->req;
  63. int bitmap = GETSOCK_BLANK;
  64. socks[0] = conn->sock[FIRSTSOCKET];
  65. /* in a HTTP/2 connection we can basically always get a frame so we should
  66. always be ready for one */
  67. bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
  68. /* we're still uploading or the HTTP/2 layer wants to send data */
  69. if((k->keepon & (KEEP_SEND|KEEP_SEND_PAUSE)) == KEEP_SEND)
  70. bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
  71. return bitmap;
  72. }
  73. static CURLcode qs_disconnect(struct Curl_easy *data,
  74. struct quicsocket *qs)
  75. {
  76. DEBUGASSERT(qs);
  77. if(qs->conn) {
  78. (void)quiche_conn_close(qs->conn, TRUE, 0, NULL, 0);
  79. /* flushing the egress is not a failsafe way to deliver all the
  80. outstanding packets, but we also don't want to get stuck here... */
  81. (void)flush_egress(data, qs->sockfd, qs);
  82. quiche_conn_free(qs->conn);
  83. qs->conn = NULL;
  84. }
  85. if(qs->h3config)
  86. quiche_h3_config_free(qs->h3config);
  87. if(qs->h3c)
  88. quiche_h3_conn_free(qs->h3c);
  89. if(qs->cfg) {
  90. quiche_config_free(qs->cfg);
  91. qs->cfg = NULL;
  92. }
  93. return CURLE_OK;
  94. }
  95. static CURLcode quiche_disconnect(struct Curl_easy *data,
  96. struct connectdata *conn,
  97. bool dead_connection)
  98. {
  99. struct quicsocket *qs = conn->quic;
  100. (void)dead_connection;
  101. return qs_disconnect(data, qs);
  102. }
  103. void Curl_quic_disconnect(struct Curl_easy *data,
  104. struct connectdata *conn,
  105. int tempindex)
  106. {
  107. if(conn->transport == TRNSPRT_QUIC)
  108. qs_disconnect(data, &conn->hequic[tempindex]);
  109. }
  110. static unsigned int quiche_conncheck(struct Curl_easy *data,
  111. struct connectdata *conn,
  112. unsigned int checks_to_perform)
  113. {
  114. (void)data;
  115. (void)conn;
  116. (void)checks_to_perform;
  117. return CONNRESULT_NONE;
  118. }
  119. static CURLcode quiche_do(struct Curl_easy *data, bool *done)
  120. {
  121. struct HTTP *stream = data->req.p.http;
  122. stream->h3req = FALSE; /* not sent */
  123. return Curl_http(data, done);
  124. }
  125. static const struct Curl_handler Curl_handler_http3 = {
  126. "HTTPS", /* scheme */
  127. ZERO_NULL, /* setup_connection */
  128. quiche_do, /* do_it */
  129. Curl_http_done, /* done */
  130. ZERO_NULL, /* do_more */
  131. ZERO_NULL, /* connect_it */
  132. ZERO_NULL, /* connecting */
  133. ZERO_NULL, /* doing */
  134. quiche_getsock, /* proto_getsock */
  135. quiche_getsock, /* doing_getsock */
  136. ZERO_NULL, /* domore_getsock */
  137. quiche_getsock, /* perform_getsock */
  138. quiche_disconnect, /* disconnect */
  139. ZERO_NULL, /* readwrite */
  140. quiche_conncheck, /* connection_check */
  141. PORT_HTTP, /* defport */
  142. CURLPROTO_HTTPS, /* protocol */
  143. CURLPROTO_HTTP, /* family */
  144. PROTOPT_SSL | PROTOPT_STREAM /* flags */
  145. };
  146. #ifdef DEBUG_QUICHE
  147. static void quiche_debug_log(const char *line, void *argp)
  148. {
  149. (void)argp;
  150. fprintf(stderr, "%s\n", line);
  151. }
  152. #endif
  153. CURLcode Curl_quic_connect(struct Curl_easy *data,
  154. struct connectdata *conn, curl_socket_t sockfd,
  155. int sockindex,
  156. const struct sockaddr *addr, socklen_t addrlen)
  157. {
  158. CURLcode result;
  159. struct quicsocket *qs = &conn->hequic[sockindex];
  160. char *keylog_file = NULL;
  161. char ipbuf[40];
  162. long port;
  163. #ifdef DEBUG_QUICHE
  164. /* initialize debug log callback only once */
  165. static int debug_log_init = 0;
  166. if(!debug_log_init) {
  167. quiche_enable_debug_logging(quiche_debug_log, NULL);
  168. debug_log_init = 1;
  169. }
  170. #endif
  171. (void)addr;
  172. (void)addrlen;
  173. qs->sockfd = sockfd;
  174. qs->cfg = quiche_config_new(QUICHE_PROTOCOL_VERSION);
  175. if(!qs->cfg) {
  176. failf(data, "can't create quiche config");
  177. return CURLE_FAILED_INIT;
  178. }
  179. quiche_config_set_max_idle_timeout(qs->cfg, QUIC_IDLE_TIMEOUT);
  180. quiche_config_set_initial_max_data(qs->cfg, QUIC_MAX_DATA);
  181. quiche_config_set_initial_max_stream_data_bidi_local(qs->cfg, QUIC_MAX_DATA);
  182. quiche_config_set_initial_max_stream_data_bidi_remote(qs->cfg,
  183. QUIC_MAX_DATA);
  184. quiche_config_set_initial_max_stream_data_uni(qs->cfg, QUIC_MAX_DATA);
  185. quiche_config_set_initial_max_streams_bidi(qs->cfg, QUIC_MAX_STREAMS);
  186. quiche_config_set_initial_max_streams_uni(qs->cfg, QUIC_MAX_STREAMS);
  187. quiche_config_set_application_protos(qs->cfg,
  188. (uint8_t *)
  189. QUICHE_H3_APPLICATION_PROTOCOL,
  190. sizeof(QUICHE_H3_APPLICATION_PROTOCOL)
  191. - 1);
  192. result = Curl_rand(data, qs->scid, sizeof(qs->scid));
  193. if(result)
  194. return result;
  195. keylog_file = getenv("SSLKEYLOGFILE");
  196. if(keylog_file)
  197. quiche_config_log_keys(qs->cfg);
  198. qs->conn = quiche_connect(conn->host.name, (const uint8_t *) qs->scid,
  199. sizeof(qs->scid), qs->cfg);
  200. if(!qs->conn) {
  201. failf(data, "can't create quiche connection");
  202. return CURLE_OUT_OF_MEMORY;
  203. }
  204. if(keylog_file)
  205. quiche_conn_set_keylog_path(qs->conn, keylog_file);
  206. /* Known to not work on Windows */
  207. #if !defined(WIN32) && defined(HAVE_QUICHE_CONN_SET_QLOG_FD)
  208. {
  209. int qfd;
  210. (void)Curl_qlogdir(data, qs->scid, sizeof(qs->scid), &qfd);
  211. if(qfd != -1)
  212. quiche_conn_set_qlog_fd(qs->conn, qfd,
  213. "qlog title", "curl qlog");
  214. }
  215. #endif
  216. result = flush_egress(data, sockfd, qs);
  217. if(result)
  218. return result;
  219. /* extract the used address as a string */
  220. if(!Curl_addr2string((struct sockaddr*)addr, addrlen, ipbuf, &port)) {
  221. char buffer[STRERROR_LEN];
  222. failf(data, "ssrem inet_ntop() failed with errno %d: %s",
  223. SOCKERRNO, Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  224. return CURLE_BAD_FUNCTION_ARGUMENT;
  225. }
  226. infof(data, "Connect socket %d over QUIC to %s:%ld\n",
  227. sockfd, ipbuf, port);
  228. Curl_persistconninfo(data, conn, NULL, -1);
  229. /* for connection reuse purposes: */
  230. conn->ssl[FIRSTSOCKET].state = ssl_connection_complete;
  231. {
  232. unsigned char alpn_protocols[] = QUICHE_H3_APPLICATION_PROTOCOL;
  233. unsigned alpn_len, offset = 0;
  234. /* Replace each ALPN length prefix by a comma. */
  235. while(offset < sizeof(alpn_protocols) - 1) {
  236. alpn_len = alpn_protocols[offset];
  237. alpn_protocols[offset] = ',';
  238. offset += 1 + alpn_len;
  239. }
  240. infof(data, "Sent QUIC client Initial, ALPN: %s\n",
  241. alpn_protocols + 1);
  242. }
  243. return CURLE_OK;
  244. }
  245. static CURLcode quiche_has_connected(struct connectdata *conn,
  246. int sockindex,
  247. int tempindex)
  248. {
  249. CURLcode result;
  250. struct quicsocket *qs = conn->quic = &conn->hequic[tempindex];
  251. conn->recv[sockindex] = h3_stream_recv;
  252. conn->send[sockindex] = h3_stream_send;
  253. conn->handler = &Curl_handler_http3;
  254. conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  255. conn->httpversion = 30;
  256. conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  257. qs->h3config = quiche_h3_config_new();
  258. if(!qs->h3config)
  259. return CURLE_OUT_OF_MEMORY;
  260. /* Create a new HTTP/3 connection on the QUIC connection. */
  261. qs->h3c = quiche_h3_conn_new_with_transport(qs->conn, qs->h3config);
  262. if(!qs->h3c) {
  263. result = CURLE_OUT_OF_MEMORY;
  264. goto fail;
  265. }
  266. if(conn->hequic[1-tempindex].cfg) {
  267. qs = &conn->hequic[1-tempindex];
  268. quiche_config_free(qs->cfg);
  269. quiche_conn_free(qs->conn);
  270. qs->cfg = NULL;
  271. qs->conn = NULL;
  272. }
  273. return CURLE_OK;
  274. fail:
  275. quiche_h3_config_free(qs->h3config);
  276. quiche_h3_conn_free(qs->h3c);
  277. return result;
  278. }
  279. /*
  280. * This function gets polled to check if this QUIC connection has connected.
  281. */
  282. CURLcode Curl_quic_is_connected(struct Curl_easy *data,
  283. struct connectdata *conn,
  284. int sockindex,
  285. bool *done)
  286. {
  287. CURLcode result;
  288. struct quicsocket *qs = &conn->hequic[sockindex];
  289. curl_socket_t sockfd = conn->tempsock[sockindex];
  290. result = process_ingress(data, sockfd, qs);
  291. if(result)
  292. goto error;
  293. result = flush_egress(data, sockfd, qs);
  294. if(result)
  295. goto error;
  296. if(quiche_conn_is_established(qs->conn)) {
  297. *done = TRUE;
  298. result = quiche_has_connected(conn, 0, sockindex);
  299. DEBUGF(infof(data, "quiche established connection!\n"));
  300. }
  301. return result;
  302. error:
  303. qs_disconnect(data, qs);
  304. return result;
  305. }
  306. static CURLcode process_ingress(struct Curl_easy *data, int sockfd,
  307. struct quicsocket *qs)
  308. {
  309. ssize_t recvd;
  310. uint8_t *buf = (uint8_t *)data->state.buffer;
  311. size_t bufsize = data->set.buffer_size;
  312. /* in case the timeout expired */
  313. quiche_conn_on_timeout(qs->conn);
  314. do {
  315. recvd = recv(sockfd, buf, bufsize, 0);
  316. if((recvd < 0) && ((SOCKERRNO == EAGAIN) || (SOCKERRNO == EWOULDBLOCK)))
  317. break;
  318. if(recvd < 0) {
  319. failf(data, "quiche: recv() unexpectedly returned %zd "
  320. "(errno: %d, socket %d)", recvd, SOCKERRNO, sockfd);
  321. return CURLE_RECV_ERROR;
  322. }
  323. recvd = quiche_conn_recv(qs->conn, buf, recvd);
  324. if(recvd == QUICHE_ERR_DONE)
  325. break;
  326. if(recvd < 0) {
  327. failf(data, "quiche_conn_recv() == %zd", recvd);
  328. return CURLE_RECV_ERROR;
  329. }
  330. } while(1);
  331. return CURLE_OK;
  332. }
  333. /*
  334. * flush_egress drains the buffers and sends off data.
  335. * Calls failf() on errors.
  336. */
  337. static CURLcode flush_egress(struct Curl_easy *data, int sockfd,
  338. struct quicsocket *qs)
  339. {
  340. ssize_t sent;
  341. uint8_t out[1200];
  342. int64_t timeout_ns;
  343. do {
  344. sent = quiche_conn_send(qs->conn, out, sizeof(out));
  345. if(sent == QUICHE_ERR_DONE)
  346. break;
  347. if(sent < 0) {
  348. failf(data, "quiche_conn_send returned %zd", sent);
  349. return CURLE_SEND_ERROR;
  350. }
  351. sent = send(sockfd, out, sent, 0);
  352. if(sent < 0) {
  353. failf(data, "send() returned %zd", sent);
  354. return CURLE_SEND_ERROR;
  355. }
  356. } while(1);
  357. /* time until the next timeout event, as nanoseconds. */
  358. timeout_ns = quiche_conn_timeout_as_nanos(qs->conn);
  359. if(timeout_ns)
  360. /* expire uses milliseconds */
  361. Curl_expire(data, (timeout_ns + 999999) / 1000000, EXPIRE_QUIC);
  362. return CURLE_OK;
  363. }
  364. struct h3h1header {
  365. char *dest;
  366. size_t destlen; /* left to use */
  367. size_t nlen; /* used */
  368. };
  369. static int cb_each_header(uint8_t *name, size_t name_len,
  370. uint8_t *value, size_t value_len,
  371. void *argp)
  372. {
  373. struct h3h1header *headers = (struct h3h1header *)argp;
  374. size_t olen = 0;
  375. if((name_len == 7) && !strncmp(":status", (char *)name, 7)) {
  376. msnprintf(headers->dest,
  377. headers->destlen, "HTTP/3 %.*s\n",
  378. (int) value_len, value);
  379. }
  380. else if(!headers->nlen) {
  381. return CURLE_HTTP3;
  382. }
  383. else {
  384. msnprintf(headers->dest,
  385. headers->destlen, "%.*s: %.*s\n",
  386. (int)name_len, name, (int) value_len, value);
  387. }
  388. olen = strlen(headers->dest);
  389. headers->destlen -= olen;
  390. headers->nlen += olen;
  391. headers->dest += olen;
  392. return 0;
  393. }
  394. static ssize_t h3_stream_recv(struct Curl_easy *data,
  395. int sockindex,
  396. char *buf,
  397. size_t buffersize,
  398. CURLcode *curlcode)
  399. {
  400. ssize_t recvd = -1;
  401. ssize_t rcode;
  402. struct connectdata *conn = data->conn;
  403. struct quicsocket *qs = conn->quic;
  404. curl_socket_t sockfd = conn->sock[sockindex];
  405. quiche_h3_event *ev;
  406. int rc;
  407. struct h3h1header headers;
  408. struct HTTP *stream = data->req.p.http;
  409. headers.dest = buf;
  410. headers.destlen = buffersize;
  411. headers.nlen = 0;
  412. if(process_ingress(data, sockfd, qs)) {
  413. infof(data, "h3_stream_recv returns on ingress\n");
  414. *curlcode = CURLE_RECV_ERROR;
  415. return -1;
  416. }
  417. while(recvd < 0) {
  418. int64_t s = quiche_h3_conn_poll(qs->h3c, qs->conn, &ev);
  419. if(s < 0)
  420. /* nothing more to do */
  421. break;
  422. if(s != stream->stream3_id) {
  423. /* another transfer, ignore for now */
  424. infof(data, "Got h3 for stream %u, expects %u\n",
  425. s, stream->stream3_id);
  426. continue;
  427. }
  428. switch(quiche_h3_event_type(ev)) {
  429. case QUICHE_H3_EVENT_HEADERS:
  430. rc = quiche_h3_event_for_each_header(ev, cb_each_header, &headers);
  431. if(rc) {
  432. *curlcode = rc;
  433. failf(data, "Error in HTTP/3 response header");
  434. break;
  435. }
  436. recvd = headers.nlen;
  437. break;
  438. case QUICHE_H3_EVENT_DATA:
  439. if(!stream->firstbody) {
  440. /* add a header-body separator CRLF */
  441. buf[0] = '\r';
  442. buf[1] = '\n';
  443. buf += 2;
  444. buffersize -= 2;
  445. stream->firstbody = TRUE;
  446. recvd = 2; /* two bytes already */
  447. }
  448. else
  449. recvd = 0;
  450. rcode = quiche_h3_recv_body(qs->h3c, qs->conn, s, (unsigned char *)buf,
  451. buffersize);
  452. if(rcode <= 0) {
  453. recvd = -1;
  454. break;
  455. }
  456. recvd += rcode;
  457. break;
  458. case QUICHE_H3_EVENT_FINISHED:
  459. streamclose(conn, "End of stream");
  460. recvd = 0; /* end of stream */
  461. break;
  462. default:
  463. break;
  464. }
  465. quiche_h3_event_free(ev);
  466. }
  467. if(flush_egress(data, sockfd, qs)) {
  468. *curlcode = CURLE_SEND_ERROR;
  469. return -1;
  470. }
  471. *curlcode = (-1 == recvd)? CURLE_AGAIN : CURLE_OK;
  472. if(recvd >= 0)
  473. /* Get this called again to drain the event queue */
  474. Curl_expire(data, 0, EXPIRE_QUIC);
  475. data->state.drain = (recvd >= 0) ? 1 : 0;
  476. return recvd;
  477. }
  478. static ssize_t h3_stream_send(struct Curl_easy *data,
  479. int sockindex,
  480. const void *mem,
  481. size_t len,
  482. CURLcode *curlcode)
  483. {
  484. ssize_t sent;
  485. struct connectdata *conn = data->conn;
  486. struct quicsocket *qs = conn->quic;
  487. curl_socket_t sockfd = conn->sock[sockindex];
  488. struct HTTP *stream = data->req.p.http;
  489. if(!stream->h3req) {
  490. CURLcode result = http_request(data, mem, len);
  491. if(result) {
  492. *curlcode = CURLE_SEND_ERROR;
  493. return -1;
  494. }
  495. sent = len;
  496. }
  497. else {
  498. H3BUGF(infof(data, "Pass on %zd body bytes to quiche\n", len));
  499. sent = quiche_h3_send_body(qs->h3c, qs->conn, stream->stream3_id,
  500. (uint8_t *)mem, len, FALSE);
  501. if(sent < 0) {
  502. *curlcode = CURLE_SEND_ERROR;
  503. return -1;
  504. }
  505. }
  506. if(flush_egress(data, sockfd, qs)) {
  507. *curlcode = CURLE_SEND_ERROR;
  508. return -1;
  509. }
  510. *curlcode = CURLE_OK;
  511. return sent;
  512. }
  513. /*
  514. * Store quiche version info in this buffer, Prefix with a space. Return total
  515. * length written.
  516. */
  517. int Curl_quic_ver(char *p, size_t len)
  518. {
  519. return msnprintf(p, len, "quiche/%s", quiche_version());
  520. }
  521. /* Index where :authority header field will appear in request header
  522. field list. */
  523. #define AUTHORITY_DST_IDX 3
  524. static CURLcode http_request(struct Curl_easy *data, const void *mem,
  525. size_t len)
  526. {
  527. /*
  528. */
  529. struct connectdata *conn = data->conn;
  530. struct HTTP *stream = data->req.p.http;
  531. size_t nheader;
  532. size_t i;
  533. size_t authority_idx;
  534. char *hdbuf = (char *)mem;
  535. char *end, *line_end;
  536. int64_t stream3_id;
  537. quiche_h3_header *nva = NULL;
  538. struct quicsocket *qs = conn->quic;
  539. CURLcode result = CURLE_OK;
  540. stream->h3req = TRUE; /* senf off! */
  541. /* Calculate number of headers contained in [mem, mem + len). Assumes a
  542. correctly generated HTTP header field block. */
  543. nheader = 0;
  544. for(i = 1; i < len; ++i) {
  545. if(hdbuf[i] == '\n' && hdbuf[i - 1] == '\r') {
  546. ++nheader;
  547. ++i;
  548. }
  549. }
  550. if(nheader < 2)
  551. goto fail;
  552. /* We counted additional 2 \r\n in the first and last line. We need 3
  553. new headers: :method, :path and :scheme. Therefore we need one
  554. more space. */
  555. nheader += 1;
  556. nva = malloc(sizeof(quiche_h3_header) * nheader);
  557. if(!nva) {
  558. result = CURLE_OUT_OF_MEMORY;
  559. goto fail;
  560. }
  561. /* Extract :method, :path from request line
  562. We do line endings with CRLF so checking for CR is enough */
  563. line_end = memchr(hdbuf, '\r', len);
  564. if(!line_end) {
  565. result = CURLE_BAD_FUNCTION_ARGUMENT; /* internal error */
  566. goto fail;
  567. }
  568. /* Method does not contain spaces */
  569. end = memchr(hdbuf, ' ', line_end - hdbuf);
  570. if(!end || end == hdbuf)
  571. goto fail;
  572. nva[0].name = (unsigned char *)":method";
  573. nva[0].name_len = strlen((char *)nva[0].name);
  574. nva[0].value = (unsigned char *)hdbuf;
  575. nva[0].value_len = (size_t)(end - hdbuf);
  576. hdbuf = end + 1;
  577. /* Path may contain spaces so scan backwards */
  578. end = NULL;
  579. for(i = (size_t)(line_end - hdbuf); i; --i) {
  580. if(hdbuf[i - 1] == ' ') {
  581. end = &hdbuf[i - 1];
  582. break;
  583. }
  584. }
  585. if(!end || end == hdbuf)
  586. goto fail;
  587. nva[1].name = (unsigned char *)":path";
  588. nva[1].name_len = strlen((char *)nva[1].name);
  589. nva[1].value = (unsigned char *)hdbuf;
  590. nva[1].value_len = (size_t)(end - hdbuf);
  591. nva[2].name = (unsigned char *)":scheme";
  592. nva[2].name_len = strlen((char *)nva[2].name);
  593. if(conn->handler->flags & PROTOPT_SSL)
  594. nva[2].value = (unsigned char *)"https";
  595. else
  596. nva[2].value = (unsigned char *)"http";
  597. nva[2].value_len = strlen((char *)nva[2].value);
  598. authority_idx = 0;
  599. i = 3;
  600. while(i < nheader) {
  601. size_t hlen;
  602. hdbuf = line_end + 2;
  603. /* check for next CR, but only within the piece of data left in the given
  604. buffer */
  605. line_end = memchr(hdbuf, '\r', len - (hdbuf - (char *)mem));
  606. if(!line_end || (line_end == hdbuf))
  607. goto fail;
  608. /* header continuation lines are not supported */
  609. if(*hdbuf == ' ' || *hdbuf == '\t')
  610. goto fail;
  611. for(end = hdbuf; end < line_end && *end != ':'; ++end)
  612. ;
  613. if(end == hdbuf || end == line_end)
  614. goto fail;
  615. hlen = end - hdbuf;
  616. if(hlen == 4 && strncasecompare("host", hdbuf, 4)) {
  617. authority_idx = i;
  618. nva[i].name = (unsigned char *)":authority";
  619. nva[i].name_len = strlen((char *)nva[i].name);
  620. }
  621. else {
  622. nva[i].name_len = (size_t)(end - hdbuf);
  623. /* Lower case the header name for HTTP/3 */
  624. Curl_strntolower((char *)hdbuf, hdbuf, nva[i].name_len);
  625. nva[i].name = (unsigned char *)hdbuf;
  626. }
  627. hdbuf = end + 1;
  628. while(*hdbuf == ' ' || *hdbuf == '\t')
  629. ++hdbuf;
  630. end = line_end;
  631. #if 0 /* This should probably go in more or less like this */
  632. switch(inspect_header((const char *)nva[i].name, nva[i].namelen, hdbuf,
  633. end - hdbuf)) {
  634. case HEADERINST_IGNORE:
  635. /* skip header fields prohibited by HTTP/2 specification. */
  636. --nheader;
  637. continue;
  638. case HEADERINST_TE_TRAILERS:
  639. nva[i].value = (uint8_t*)"trailers";
  640. nva[i].value_len = sizeof("trailers") - 1;
  641. break;
  642. default:
  643. nva[i].value = (unsigned char *)hdbuf;
  644. nva[i].value_len = (size_t)(end - hdbuf);
  645. }
  646. #endif
  647. nva[i].value = (unsigned char *)hdbuf;
  648. nva[i].value_len = (size_t)(end - hdbuf);
  649. ++i;
  650. }
  651. /* :authority must come before non-pseudo header fields */
  652. if(authority_idx != 0 && authority_idx != AUTHORITY_DST_IDX) {
  653. quiche_h3_header authority = nva[authority_idx];
  654. for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
  655. nva[i] = nva[i - 1];
  656. }
  657. nva[i] = authority;
  658. }
  659. /* Warn stream may be rejected if cumulative length of headers is too
  660. large. */
  661. #define MAX_ACC 60000 /* <64KB to account for some overhead */
  662. {
  663. size_t acc = 0;
  664. for(i = 0; i < nheader; ++i) {
  665. acc += nva[i].name_len + nva[i].value_len;
  666. H3BUGF(infof(data, "h3 [%.*s: %.*s]\n",
  667. nva[i].name_len, nva[i].name,
  668. nva[i].value_len, nva[i].value));
  669. }
  670. if(acc > MAX_ACC) {
  671. infof(data, "http_request: Warning: The cumulative length of all "
  672. "headers exceeds %d bytes and that could cause the "
  673. "stream to be rejected.\n", MAX_ACC);
  674. }
  675. }
  676. switch(data->state.httpreq) {
  677. case HTTPREQ_POST:
  678. case HTTPREQ_POST_FORM:
  679. case HTTPREQ_POST_MIME:
  680. case HTTPREQ_PUT:
  681. if(data->state.infilesize != -1)
  682. stream->upload_left = data->state.infilesize;
  683. else
  684. /* data sending without specifying the data amount up front */
  685. stream->upload_left = -1; /* unknown, but not zero */
  686. stream3_id = quiche_h3_send_request(qs->h3c, qs->conn, nva, nheader,
  687. stream->upload_left ? FALSE: TRUE);
  688. if((stream3_id >= 0) && data->set.postfields) {
  689. ssize_t sent = quiche_h3_send_body(qs->h3c, qs->conn, stream3_id,
  690. (uint8_t *)data->set.postfields,
  691. stream->upload_left, TRUE);
  692. if(sent <= 0) {
  693. failf(data, "quiche_h3_send_body failed!");
  694. result = CURLE_SEND_ERROR;
  695. }
  696. stream->upload_left = 0; /* nothing left to send */
  697. }
  698. break;
  699. default:
  700. stream3_id = quiche_h3_send_request(qs->h3c, qs->conn, nva, nheader,
  701. TRUE);
  702. break;
  703. }
  704. Curl_safefree(nva);
  705. if(stream3_id < 0) {
  706. H3BUGF(infof(data, "quiche_h3_send_request returned %d\n",
  707. stream3_id));
  708. result = CURLE_SEND_ERROR;
  709. goto fail;
  710. }
  711. infof(data, "Using HTTP/3 Stream ID: %x (easy handle %p)\n",
  712. stream3_id, (void *)data);
  713. stream->stream3_id = stream3_id;
  714. return CURLE_OK;
  715. fail:
  716. free(nva);
  717. return result;
  718. }
  719. /*
  720. * Called from transfer.c:done_sending when we stop HTTP/3 uploading.
  721. */
  722. CURLcode Curl_quic_done_sending(struct Curl_easy *data)
  723. {
  724. struct connectdata *conn = data->conn;
  725. DEBUGASSERT(conn);
  726. if(conn->handler == &Curl_handler_http3) {
  727. /* only for HTTP/3 transfers */
  728. ssize_t sent;
  729. struct HTTP *stream = data->req.p.http;
  730. struct quicsocket *qs = conn->quic;
  731. stream->upload_done = TRUE;
  732. sent = quiche_h3_send_body(qs->h3c, qs->conn, stream->stream3_id,
  733. NULL, 0, TRUE);
  734. if(sent < 0)
  735. return CURLE_SEND_ERROR;
  736. }
  737. return CURLE_OK;
  738. }
  739. /*
  740. * Called from http.c:Curl_http_done when a request completes.
  741. */
  742. void Curl_quic_done(struct Curl_easy *data, bool premature)
  743. {
  744. (void)data;
  745. (void)premature;
  746. }
  747. /*
  748. * Called from transfer.c:data_pending to know if we should keep looping
  749. * to receive more data from the connection.
  750. */
  751. bool Curl_quic_data_pending(const struct Curl_easy *data)
  752. {
  753. (void)data;
  754. return FALSE;
  755. }
  756. #endif