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