2
0

quiche.c 24 KB

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