quiche.c 23 KB

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