quiche.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef USE_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_FALSE
  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_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. #if 0
  177. /* store the used address as a string */
  178. if(!Curl_addr2string((struct sockaddr*)addr,
  179. conn->primary_ip, &conn->primary_port)) {
  180. char buffer[STRERROR_LEN];
  181. failf(data, "ssrem inet_ntop() failed with errno %d: %s",
  182. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  183. return CURLE_BAD_FUNCTION_ARGUMENT;
  184. }
  185. memcpy(conn->ip_addr_str, conn->primary_ip, MAX_IPADR_LEN);
  186. #endif
  187. /* for connection reuse purposes: */
  188. conn->ssl[FIRSTSOCKET].state = ssl_connection_complete;
  189. infof(data, "Sent QUIC client Initial, ALPN: %s\n",
  190. QUICHE_H3_APPLICATION_PROTOCOL + 1);
  191. return CURLE_OK;
  192. }
  193. static CURLcode quiche_has_connected(struct connectdata *conn,
  194. int sockindex,
  195. int tempindex)
  196. {
  197. CURLcode result;
  198. struct quicsocket *qs = conn->quic = &conn->hequic[tempindex];
  199. conn->recv[sockindex] = h3_stream_recv;
  200. conn->send[sockindex] = h3_stream_send;
  201. conn->handler = &Curl_handler_http3;
  202. conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  203. conn->httpversion = 30;
  204. conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  205. qs->h3config = quiche_h3_config_new(0, 1024, 0, 0);
  206. if(!qs->h3config)
  207. return CURLE_OUT_OF_MEMORY;
  208. /* Create a new HTTP/3 connection on the QUIC connection. */
  209. qs->h3c = quiche_h3_conn_new_with_transport(qs->conn, qs->h3config);
  210. if(!qs->h3c) {
  211. result = CURLE_OUT_OF_MEMORY;
  212. goto fail;
  213. }
  214. if(conn->hequic[1-tempindex].cfg) {
  215. qs = &conn->hequic[1-tempindex];
  216. quiche_config_free(qs->cfg);
  217. quiche_conn_free(qs->conn);
  218. qs->cfg = NULL;
  219. qs->conn = NULL;
  220. }
  221. return CURLE_OK;
  222. fail:
  223. quiche_h3_config_free(qs->h3config);
  224. quiche_h3_conn_free(qs->h3c);
  225. return result;
  226. }
  227. /*
  228. * This function gets polled to check if this QUIC connection has connected.
  229. */
  230. CURLcode Curl_quic_is_connected(struct connectdata *conn, int sockindex,
  231. bool *done)
  232. {
  233. CURLcode result;
  234. struct quicsocket *qs = &conn->hequic[sockindex];
  235. curl_socket_t sockfd = conn->tempsock[sockindex];
  236. result = process_ingress(conn, sockfd, qs);
  237. if(result)
  238. return result;
  239. result = flush_egress(conn, sockfd, qs);
  240. if(result)
  241. return result;
  242. if(quiche_conn_is_established(qs->conn)) {
  243. *done = TRUE;
  244. result = quiche_has_connected(conn, 0, sockindex);
  245. DEBUGF(infof(conn->data, "quiche established connection!\n"));
  246. }
  247. return result;
  248. }
  249. static CURLcode process_ingress(struct connectdata *conn, int sockfd,
  250. struct quicsocket *qs)
  251. {
  252. ssize_t recvd;
  253. struct Curl_easy *data = conn->data;
  254. uint8_t *buf = (uint8_t *)data->state.buffer;
  255. size_t bufsize = data->set.buffer_size;
  256. /* in case the timeout expired */
  257. quiche_conn_on_timeout(qs->conn);
  258. do {
  259. recvd = recv(sockfd, buf, bufsize, 0);
  260. if((recvd < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))
  261. break;
  262. if(recvd < 0) {
  263. failf(conn->data, "quiche: recv() unexpectedly returned %d "
  264. "(errno: %d, socket %d)", recvd, SOCKERRNO, sockfd);
  265. return CURLE_RECV_ERROR;
  266. }
  267. recvd = quiche_conn_recv(qs->conn, buf, recvd);
  268. if(recvd == QUICHE_ERR_DONE)
  269. break;
  270. if(recvd < 0) {
  271. failf(conn->data, "quiche_conn_recv() == %d", recvd);
  272. return CURLE_RECV_ERROR;
  273. }
  274. } while(1);
  275. return CURLE_OK;
  276. }
  277. /*
  278. * flush_egress drains the buffers and sends off data.
  279. * Calls failf() on errors.
  280. */
  281. static CURLcode flush_egress(struct connectdata *conn, int sockfd,
  282. struct quicsocket *qs)
  283. {
  284. ssize_t sent;
  285. static uint8_t out[1200];
  286. int64_t timeout_ns;
  287. do {
  288. sent = quiche_conn_send(qs->conn, out, sizeof(out));
  289. if(sent == QUICHE_ERR_DONE)
  290. break;
  291. if(sent < 0) {
  292. failf(conn->data, "quiche_conn_send returned %zd\n",
  293. sent);
  294. return CURLE_SEND_ERROR;
  295. }
  296. sent = send(sockfd, out, sent, 0);
  297. if(sent < 0) {
  298. failf(conn->data, "send() returned %zd\n", sent);
  299. return CURLE_SEND_ERROR;
  300. }
  301. } while(1);
  302. /* time until the next timeout event, as nanoseconds. */
  303. timeout_ns = quiche_conn_timeout_as_nanos(qs->conn);
  304. if(timeout_ns)
  305. /* expire uses milliseconds */
  306. Curl_expire(conn->data, (timeout_ns + 999999) / 1000000, EXPIRE_QUIC);
  307. return CURLE_OK;
  308. }
  309. struct h3h1header {
  310. char *dest;
  311. size_t destlen; /* left to use */
  312. size_t nlen; /* used */
  313. };
  314. static int cb_each_header(uint8_t *name, size_t name_len,
  315. uint8_t *value, size_t value_len,
  316. void *argp)
  317. {
  318. struct h3h1header *headers = (struct h3h1header *)argp;
  319. size_t olen = 0;
  320. if((name_len == 7) && !strncmp(":status", (char *)name, 7)) {
  321. msnprintf(headers->dest,
  322. headers->destlen, "HTTP/3 %.*s\n",
  323. (int) value_len, value);
  324. }
  325. else {
  326. msnprintf(headers->dest,
  327. headers->destlen, "%.*s: %.*s\n",
  328. (int)name_len, name, (int) value_len, value);
  329. }
  330. olen = strlen(headers->dest);
  331. headers->destlen -= olen;
  332. headers->nlen += olen;
  333. headers->dest += olen;
  334. return 0;
  335. }
  336. static ssize_t h3_stream_recv(struct connectdata *conn,
  337. int sockindex,
  338. char *buf,
  339. size_t buffersize,
  340. CURLcode *curlcode)
  341. {
  342. ssize_t recvd = -1;
  343. ssize_t rcode;
  344. struct quicsocket *qs = conn->quic;
  345. curl_socket_t sockfd = conn->sock[sockindex];
  346. quiche_h3_event *ev;
  347. int rc;
  348. struct h3h1header headers;
  349. struct HTTP *stream = conn->data->req.protop;
  350. headers.dest = buf;
  351. headers.destlen = buffersize;
  352. headers.nlen = 0;
  353. if(process_ingress(conn, sockfd, qs)) {
  354. infof(conn->data, "h3_stream_recv returns on ingress\n");
  355. *curlcode = CURLE_RECV_ERROR;
  356. return -1;
  357. }
  358. while(recvd < 0) {
  359. int64_t s = quiche_h3_conn_poll(qs->h3c, qs->conn, &ev);
  360. if(s < 0)
  361. /* nothing more to do */
  362. break;
  363. if(s != stream->stream3_id) {
  364. /* another transfer, ignore for now */
  365. infof(conn->data, "Got h3 for stream %u, expects %u\n",
  366. s, stream->stream3_id);
  367. continue;
  368. }
  369. switch(quiche_h3_event_type(ev)) {
  370. case QUICHE_H3_EVENT_HEADERS:
  371. rc = quiche_h3_event_for_each_header(ev, cb_each_header, &headers);
  372. if(rc) {
  373. /* what do we do about this? */
  374. }
  375. recvd = headers.nlen;
  376. break;
  377. case QUICHE_H3_EVENT_DATA:
  378. if(!stream->firstbody) {
  379. /* add a header-body separator CRLF */
  380. buf[0] = '\r';
  381. buf[1] = '\n';
  382. buf += 2;
  383. buffersize -= 2;
  384. stream->firstbody = TRUE;
  385. recvd = 2; /* two bytes already */
  386. }
  387. else
  388. recvd = 0;
  389. rcode = quiche_h3_recv_body(qs->h3c, qs->conn, s, (unsigned char *)buf,
  390. buffersize);
  391. if(rcode <= 0) {
  392. recvd = -1;
  393. break;
  394. }
  395. recvd += rcode;
  396. break;
  397. case QUICHE_H3_EVENT_FINISHED:
  398. if(quiche_conn_close(qs->conn, true, 0, NULL, 0) < 0) {
  399. ;
  400. }
  401. recvd = 0; /* end of stream */
  402. break;
  403. default:
  404. break;
  405. }
  406. quiche_h3_event_free(ev);
  407. }
  408. if(flush_egress(conn, sockfd, qs)) {
  409. *curlcode = CURLE_SEND_ERROR;
  410. return -1;
  411. }
  412. *curlcode = (-1 == recvd)? CURLE_AGAIN : CURLE_OK;
  413. if(recvd >= 0)
  414. /* Get this called again to drain the event queue */
  415. Curl_expire(conn->data, 0, EXPIRE_QUIC);
  416. return recvd;
  417. }
  418. static ssize_t h3_stream_send(struct connectdata *conn,
  419. int sockindex,
  420. const void *mem,
  421. size_t len,
  422. CURLcode *curlcode)
  423. {
  424. ssize_t sent;
  425. struct quicsocket *qs = conn->quic;
  426. curl_socket_t sockfd = conn->sock[sockindex];
  427. struct HTTP *stream = conn->data->req.protop;
  428. if(!stream->h3req) {
  429. CURLcode result = http_request(conn, mem, len);
  430. if(result) {
  431. *curlcode = CURLE_SEND_ERROR;
  432. return -1;
  433. }
  434. sent = len;
  435. }
  436. else {
  437. H3BUGF(infof(conn->data, "Pass on %zd body bytes to quiche\n",
  438. len));
  439. sent = quiche_h3_send_body(qs->h3c, qs->conn, stream->stream3_id,
  440. (uint8_t *)mem, len, FALSE);
  441. if(sent < 0) {
  442. *curlcode = CURLE_SEND_ERROR;
  443. return -1;
  444. }
  445. }
  446. if(flush_egress(conn, sockfd, qs)) {
  447. *curlcode = CURLE_SEND_ERROR;
  448. return -1;
  449. }
  450. *curlcode = CURLE_OK;
  451. return sent;
  452. }
  453. /*
  454. * Store quiche version info in this buffer, Prefix with a space. Return total
  455. * length written.
  456. */
  457. int Curl_quic_ver(char *p, size_t len)
  458. {
  459. return msnprintf(p, len, " quiche/%s", quiche_version());
  460. }
  461. /* Index where :authority header field will appear in request header
  462. field list. */
  463. #define AUTHORITY_DST_IDX 3
  464. static CURLcode http_request(struct connectdata *conn, const void *mem,
  465. size_t len)
  466. {
  467. /*
  468. */
  469. struct HTTP *stream = conn->data->req.protop;
  470. size_t nheader;
  471. size_t i;
  472. size_t authority_idx;
  473. char *hdbuf = (char *)mem;
  474. char *end, *line_end;
  475. int64_t stream3_id;
  476. quiche_h3_header *nva = NULL;
  477. struct quicsocket *qs = conn->quic;
  478. CURLcode result = CURLE_OK;
  479. struct Curl_easy *data = conn->data;
  480. stream->h3req = TRUE; /* senf off! */
  481. /* Calculate number of headers contained in [mem, mem + len). Assumes a
  482. correctly generated HTTP header field block. */
  483. nheader = 0;
  484. for(i = 1; i < len; ++i) {
  485. if(hdbuf[i] == '\n' && hdbuf[i - 1] == '\r') {
  486. ++nheader;
  487. ++i;
  488. }
  489. }
  490. if(nheader < 2)
  491. goto fail;
  492. /* We counted additional 2 \r\n in the first and last line. We need 3
  493. new headers: :method, :path and :scheme. Therefore we need one
  494. more space. */
  495. nheader += 1;
  496. nva = malloc(sizeof(quiche_h3_header) * nheader);
  497. if(!nva) {
  498. result = CURLE_OUT_OF_MEMORY;
  499. goto fail;
  500. }
  501. /* Extract :method, :path from request line
  502. We do line endings with CRLF so checking for CR is enough */
  503. line_end = memchr(hdbuf, '\r', len);
  504. if(!line_end) {
  505. result = CURLE_BAD_FUNCTION_ARGUMENT; /* internal error */
  506. goto fail;
  507. }
  508. /* Method does not contain spaces */
  509. end = memchr(hdbuf, ' ', line_end - hdbuf);
  510. if(!end || end == hdbuf)
  511. goto fail;
  512. nva[0].name = (unsigned char *)":method";
  513. nva[0].name_len = strlen((char *)nva[0].name);
  514. nva[0].value = (unsigned char *)hdbuf;
  515. nva[0].value_len = (size_t)(end - hdbuf);
  516. hdbuf = end + 1;
  517. /* Path may contain spaces so scan backwards */
  518. end = NULL;
  519. for(i = (size_t)(line_end - hdbuf); i; --i) {
  520. if(hdbuf[i - 1] == ' ') {
  521. end = &hdbuf[i - 1];
  522. break;
  523. }
  524. }
  525. if(!end || end == hdbuf)
  526. goto fail;
  527. nva[1].name = (unsigned char *)":path";
  528. nva[1].name_len = strlen((char *)nva[1].name);
  529. nva[1].value = (unsigned char *)hdbuf;
  530. nva[1].value_len = (size_t)(end - hdbuf);
  531. nva[2].name = (unsigned char *)":scheme";
  532. nva[2].name_len = strlen((char *)nva[2].name);
  533. if(conn->handler->flags & PROTOPT_SSL)
  534. nva[2].value = (unsigned char *)"https";
  535. else
  536. nva[2].value = (unsigned char *)"http";
  537. nva[2].value_len = strlen((char *)nva[2].value);
  538. authority_idx = 0;
  539. i = 3;
  540. while(i < nheader) {
  541. size_t hlen;
  542. hdbuf = line_end + 2;
  543. /* check for next CR, but only within the piece of data left in the given
  544. buffer */
  545. line_end = memchr(hdbuf, '\r', len - (hdbuf - (char *)mem));
  546. if(!line_end || (line_end == hdbuf))
  547. goto fail;
  548. /* header continuation lines are not supported */
  549. if(*hdbuf == ' ' || *hdbuf == '\t')
  550. goto fail;
  551. for(end = hdbuf; end < line_end && *end != ':'; ++end)
  552. ;
  553. if(end == hdbuf || end == line_end)
  554. goto fail;
  555. hlen = end - hdbuf;
  556. if(hlen == 4 && strncasecompare("host", hdbuf, 4)) {
  557. authority_idx = i;
  558. nva[i].name = (unsigned char *)":authority";
  559. nva[i].name_len = strlen((char *)nva[i].name);
  560. }
  561. else {
  562. nva[i].name = (unsigned char *)hdbuf;
  563. nva[i].name_len = (size_t)(end - hdbuf);
  564. }
  565. hdbuf = end + 1;
  566. while(*hdbuf == ' ' || *hdbuf == '\t')
  567. ++hdbuf;
  568. end = line_end;
  569. #if 0 /* This should probably go in more or less like this */
  570. switch(inspect_header((const char *)nva[i].name, nva[i].namelen, hdbuf,
  571. end - hdbuf)) {
  572. case HEADERINST_IGNORE:
  573. /* skip header fields prohibited by HTTP/2 specification. */
  574. --nheader;
  575. continue;
  576. case HEADERINST_TE_TRAILERS:
  577. nva[i].value = (uint8_t*)"trailers";
  578. nva[i].value_len = sizeof("trailers") - 1;
  579. break;
  580. default:
  581. nva[i].value = (unsigned char *)hdbuf;
  582. nva[i].value_len = (size_t)(end - hdbuf);
  583. }
  584. #endif
  585. nva[i].value = (unsigned char *)hdbuf;
  586. nva[i].value_len = (size_t)(end - hdbuf);
  587. ++i;
  588. }
  589. /* :authority must come before non-pseudo header fields */
  590. if(authority_idx != 0 && authority_idx != AUTHORITY_DST_IDX) {
  591. quiche_h3_header authority = nva[authority_idx];
  592. for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
  593. nva[i] = nva[i - 1];
  594. }
  595. nva[i] = authority;
  596. }
  597. /* Warn stream may be rejected if cumulative length of headers is too
  598. large. */
  599. #define MAX_ACC 60000 /* <64KB to account for some overhead */
  600. {
  601. size_t acc = 0;
  602. for(i = 0; i < nheader; ++i) {
  603. acc += nva[i].name_len + nva[i].value_len;
  604. H3BUGF(infof(data, "h3 [%.*s: %.*s]\n",
  605. nva[i].name_len, nva[i].name,
  606. nva[i].value_len, nva[i].value));
  607. }
  608. if(acc > MAX_ACC) {
  609. infof(data, "http_request: Warning: The cumulative length of all "
  610. "headers exceeds %zu bytes and that could cause the "
  611. "stream to be rejected.\n", MAX_ACC);
  612. }
  613. }
  614. switch(data->set.httpreq) {
  615. case HTTPREQ_POST:
  616. case HTTPREQ_POST_FORM:
  617. case HTTPREQ_POST_MIME:
  618. case HTTPREQ_PUT:
  619. if(data->state.infilesize != -1)
  620. stream->upload_left = data->state.infilesize;
  621. else
  622. /* data sending without specifying the data amount up front */
  623. stream->upload_left = -1; /* unknown, but not zero */
  624. stream3_id = quiche_h3_send_request(qs->h3c, qs->conn, nva, nheader,
  625. stream->upload_left ? FALSE: TRUE);
  626. if((stream3_id >= 0) && data->set.postfields) {
  627. ssize_t sent = quiche_h3_send_body(qs->h3c, qs->conn, stream3_id,
  628. (uint8_t *)data->set.postfields,
  629. stream->upload_left, TRUE);
  630. if(sent <= 0) {
  631. failf(data, "quiche_h3_send_body failed!");
  632. result = CURLE_SEND_ERROR;
  633. }
  634. stream->upload_left = 0; /* nothing left to send */
  635. }
  636. break;
  637. default:
  638. stream3_id = quiche_h3_send_request(qs->h3c, qs->conn, nva, nheader,
  639. TRUE);
  640. break;
  641. }
  642. Curl_safefree(nva);
  643. if(stream3_id < 0) {
  644. H3BUGF(infof(data, "quiche_h3_send_request returned %d\n",
  645. stream3_id));
  646. result = CURLE_SEND_ERROR;
  647. goto fail;
  648. }
  649. infof(data, "Using HTTP/3 Stream ID: %x (easy handle %p)\n",
  650. stream3_id, (void *)data);
  651. stream->stream3_id = stream3_id;
  652. return CURLE_OK;
  653. fail:
  654. free(nva);
  655. return result;
  656. }
  657. /*
  658. * Called from transfer.c:done_sending when we stop HTTP/3 uploading.
  659. */
  660. CURLcode Curl_quic_done_sending(struct connectdata *conn)
  661. {
  662. if(conn->handler == &Curl_handler_http3) {
  663. /* only for HTTP/3 transfers */
  664. ssize_t sent;
  665. struct HTTP *stream = conn->data->req.protop;
  666. struct quicsocket *qs = conn->quic;
  667. fprintf(stderr, "!!! Curl_quic_done_sending\n");
  668. stream->upload_done = TRUE;
  669. sent = quiche_h3_send_body(qs->h3c, qs->conn, stream->stream3_id,
  670. NULL, 0, TRUE);
  671. if(sent < 0)
  672. return CURLE_SEND_ERROR;
  673. }
  674. return CURLE_OK;
  675. }
  676. #endif