quiche.c 25 KB

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