Coverage Report

Created: 2022-09-28 06:21

/src/curl/lib/http2.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 1998 - 2022, 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
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#ifdef USE_NGHTTP2
28
#include <nghttp2/nghttp2.h>
29
#include "urldata.h"
30
#include "http2.h"
31
#include "http.h"
32
#include "sendf.h"
33
#include "select.h"
34
#include "curl_base64.h"
35
#include "strcase.h"
36
#include "multiif.h"
37
#include "url.h"
38
#include "connect.h"
39
#include "strtoofft.h"
40
#include "strdup.h"
41
#include "transfer.h"
42
#include "dynbuf.h"
43
#include "h2h3.h"
44
#include "headers.h"
45
/* The last 3 #include files should be in this order */
46
#include "curl_printf.h"
47
#include "curl_memory.h"
48
#include "memdebug.h"
49
50
22.5k
#define H2_BUFSIZE 32768
51
52
#if (NGHTTP2_VERSION_NUM < 0x010c00)
53
#error too old nghttp2 version, upgrade!
54
#endif
55
56
#ifdef CURL_DISABLE_VERBOSE_STRINGS
57
#define nghttp2_session_callbacks_set_error_callback(x,y)
58
#endif
59
60
#if (NGHTTP2_VERSION_NUM >= 0x010c00)
61
#define NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE 1
62
#endif
63
64
15.7k
#define HTTP2_HUGE_WINDOW_SIZE (32 * 1024 * 1024) /* 32 MB */
65
66
#ifdef DEBUG_HTTP2
67
#define H2BUGF(x) x
68
#else
69
2.62M
#define H2BUGF(x) do { } while(0)
70
#endif
71
72
static ssize_t http2_recv(struct Curl_easy *data, int sockindex,
73
                          char *mem, size_t len, CURLcode *err);
74
static bool http2_connisdead(struct Curl_easy *data,
75
                             struct connectdata *conn);
76
static int h2_session_send(struct Curl_easy *data,
77
                           nghttp2_session *h2);
78
static int h2_process_pending_input(struct Curl_easy *data,
79
                                    struct http_conn *httpc,
80
                                    CURLcode *err);
81
82
/*
83
 * Curl_http2_init_state() is called when the easy handle is created and
84
 * allows for HTTP/2 specific init of state.
85
 */
86
void Curl_http2_init_state(struct UrlState *state)
87
87.0k
{
88
87.0k
  state->stream_weight = NGHTTP2_DEFAULT_WEIGHT;
89
87.0k
}
90
91
/*
92
 * Curl_http2_init_userset() is called when the easy handle is created and
93
 * allows for HTTP/2 specific user-set fields.
94
 */
95
void Curl_http2_init_userset(struct UserDefined *set)
96
194k
{
97
194k
  set->stream_weight = NGHTTP2_DEFAULT_WEIGHT;
98
194k
}
99
100
static int http2_getsock(struct Curl_easy *data,
101
                         struct connectdata *conn,
102
                         curl_socket_t *sock)
103
4.67M
{
104
4.67M
  const struct http_conn *c = &conn->proto.httpc;
105
4.67M
  struct SingleRequest *k = &data->req;
106
4.67M
  int bitmap = GETSOCK_BLANK;
107
4.67M
  struct HTTP *stream = data->req.p.http;
108
109
4.67M
  sock[0] = conn->sock[FIRSTSOCKET];
110
111
4.67M
  if(!(k->keepon & KEEP_RECV_PAUSE))
112
    /* Unless paused - in a HTTP/2 connection we can basically always get a
113
       frame so we should always be ready for one */
114
4.67M
    bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
115
116
  /* we're (still uploading OR the HTTP/2 layer wants to send data) AND
117
     there's a window to send data in */
118
4.67M
  if((((k->keepon & (KEEP_SEND|KEEP_SEND_PAUSE)) == KEEP_SEND) ||
119
4.67M
      nghttp2_session_want_write(c->h2)) &&
120
4.67M
     (nghttp2_session_get_remote_window_size(c->h2) &&
121
4.67M
      nghttp2_session_get_stream_remote_window_size(c->h2,
122
4.35M
                                                    stream->stream_id)))
123
4.16M
    bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
124
125
4.67M
  return bitmap;
126
4.67M
}
127
128
/*
129
 * http2_stream_free() free HTTP2 stream related data
130
 */
131
static void http2_stream_free(struct HTTP *http)
132
0
{
133
0
  if(http) {
134
0
    Curl_dyn_free(&http->header_recvbuf);
135
0
    for(; http->push_headers_used > 0; --http->push_headers_used) {
136
0
      free(http->push_headers[http->push_headers_used - 1]);
137
0
    }
138
0
    free(http->push_headers);
139
0
    http->push_headers = NULL;
140
0
  }
141
0
}
142
143
/*
144
 * Disconnects *a* connection used for HTTP/2. It might be an old one from the
145
 * connection cache and not the "main" one. Don't touch the easy handle!
146
 */
147
148
static CURLcode http2_disconnect(struct Curl_easy *data,
149
                                 struct connectdata *conn,
150
                                 bool dead_connection)
151
8.37k
{
152
8.37k
  struct http_conn *c = &conn->proto.httpc;
153
8.37k
  (void)dead_connection;
154
8.37k
#ifndef DEBUG_HTTP2
155
8.37k
  (void)data;
156
8.37k
#endif
157
158
8.37k
  H2BUGF(infof(data, "HTTP/2 DISCONNECT starts now"));
159
160
8.37k
  nghttp2_session_del(c->h2);
161
8.37k
  Curl_safefree(c->inbuf);
162
163
8.37k
  H2BUGF(infof(data, "HTTP/2 DISCONNECT done"));
164
165
8.37k
  return CURLE_OK;
166
8.37k
}
167
168
/*
169
 * The server may send us data at any point (e.g. PING frames). Therefore,
170
 * we cannot assume that an HTTP/2 socket is dead just because it is readable.
171
 *
172
 * Instead, if it is readable, run Curl_connalive() to peek at the socket
173
 * and distinguish between closed and data.
174
 */
175
static bool http2_connisdead(struct Curl_easy *data, struct connectdata *conn)
176
802
{
177
802
  int sval;
178
802
  bool dead = TRUE;
179
180
802
  if(conn->bits.close)
181
0
    return TRUE;
182
183
802
  sval = SOCKET_READABLE(conn->sock[FIRSTSOCKET], 0);
184
802
  if(sval == 0) {
185
    /* timeout */
186
664
    dead = FALSE;
187
664
  }
188
138
  else if(sval & CURL_CSELECT_ERR) {
189
    /* socket is in an error state */
190
0
    dead = TRUE;
191
0
  }
192
138
  else if(sval & CURL_CSELECT_IN) {
193
    /* readable with no error. could still be closed */
194
138
    dead = !Curl_connalive(conn);
195
138
    if(!dead) {
196
      /* This happens before we've sent off a request and the connection is
197
         not in use by any other transfer, there shouldn't be any data here,
198
         only "protocol frames" */
199
2
      CURLcode result;
200
2
      struct http_conn *httpc = &conn->proto.httpc;
201
2
      ssize_t nread = -1;
202
2
      if(httpc->recv_underlying)
203
        /* if called "too early", this pointer isn't setup yet! */
204
2
        nread = ((Curl_recv *)httpc->recv_underlying)(
205
2
          data, FIRSTSOCKET, httpc->inbuf, H2_BUFSIZE, &result);
206
2
      if(nread != -1) {
207
2
        H2BUGF(infof(data,
208
2
                     "%d bytes stray data read before trying h2 connection",
209
2
                     (int)nread));
210
2
        httpc->nread_inbuf = 0;
211
2
        httpc->inbuflen = nread;
212
2
        if(h2_process_pending_input(data, httpc, &result) < 0)
213
          /* immediate error, considered dead */
214
1
          dead = TRUE;
215
2
      }
216
0
      else
217
        /* the read failed so let's say this is dead anyway */
218
0
        dead = TRUE;
219
2
    }
220
138
  }
221
222
802
  return dead;
223
802
}
224
225
/*
226
 * Set the transfer that is currently using this HTTP/2 connection.
227
 */
228
static void set_transfer(struct http_conn *c,
229
                         struct Curl_easy *data)
230
2.34M
{
231
2.34M
  c->trnsfr = data;
232
2.34M
}
233
234
/*
235
 * Get the transfer that is currently using this HTTP/2 connection.
236
 */
237
static struct Curl_easy *get_transfer(struct http_conn *c)
238
108k
{
239
108k
  DEBUGASSERT(c && c->trnsfr);
240
108k
  return c->trnsfr;
241
108k
}
242
243
static unsigned int http2_conncheck(struct Curl_easy *data,
244
                                    struct connectdata *conn,
245
                                    unsigned int checks_to_perform)
246
802
{
247
802
  unsigned int ret_val = CONNRESULT_NONE;
248
802
  struct http_conn *c = &conn->proto.httpc;
249
802
  int rc;
250
802
  bool send_frames = false;
251
252
802
  if(checks_to_perform & CONNCHECK_ISDEAD) {
253
802
    if(http2_connisdead(data, conn))
254
137
      ret_val |= CONNRESULT_DEAD;
255
802
  }
256
257
802
  if(checks_to_perform & CONNCHECK_KEEPALIVE) {
258
0
    struct curltime now = Curl_now();
259
0
    timediff_t elapsed = Curl_timediff(now, conn->keepalive);
260
261
0
    if(elapsed > data->set.upkeep_interval_ms) {
262
      /* Perform an HTTP/2 PING */
263
0
      rc = nghttp2_submit_ping(c->h2, 0, ZERO_NULL);
264
0
      if(!rc) {
265
        /* Successfully added a PING frame to the session. Need to flag this
266
           so the frame is sent. */
267
0
        send_frames = true;
268
0
      }
269
0
      else {
270
0
       failf(data, "nghttp2_submit_ping() failed: %s(%d)",
271
0
             nghttp2_strerror(rc), rc);
272
0
      }
273
274
0
      conn->keepalive = now;
275
0
    }
276
0
  }
277
278
802
  if(send_frames) {
279
0
    set_transfer(c, data); /* set the transfer */
280
0
    rc = nghttp2_session_send(c->h2);
281
0
    if(rc)
282
0
      failf(data, "nghttp2_session_send() failed: %s(%d)",
283
0
            nghttp2_strerror(rc), rc);
284
0
  }
285
286
802
  return ret_val;
287
802
}
288
289
/* called from http_setup_conn */
290
void Curl_http2_setup_req(struct Curl_easy *data)
291
26.7k
{
292
26.7k
  struct HTTP *http = data->req.p.http;
293
26.7k
  http->bodystarted = FALSE;
294
26.7k
  http->status_code = -1;
295
26.7k
  http->pausedata = NULL;
296
26.7k
  http->pauselen = 0;
297
26.7k
  http->closed = FALSE;
298
26.7k
  http->close_handled = FALSE;
299
26.7k
  http->mem = NULL;
300
26.7k
  http->len = 0;
301
26.7k
  http->memlen = 0;
302
26.7k
  http->error = NGHTTP2_NO_ERROR;
303
26.7k
}
304
305
/* called from http_setup_conn */
306
void Curl_http2_setup_conn(struct connectdata *conn)
307
26.7k
{
308
26.7k
  conn->proto.httpc.settings.max_concurrent_streams =
309
26.7k
    DEFAULT_MAX_CONCURRENT_STREAMS;
310
26.7k
}
311
312
/*
313
 * HTTP2 handler interface. This isn't added to the general list of protocols
314
 * but will be used at run-time when the protocol is dynamically switched from
315
 * HTTP to HTTP2.
316
 */
317
static const struct Curl_handler Curl_handler_http2 = {
318
  "HTTP",                               /* scheme */
319
  ZERO_NULL,                            /* setup_connection */
320
  Curl_http,                            /* do_it */
321
  Curl_http_done,                       /* done */
322
  ZERO_NULL,                            /* do_more */
323
  ZERO_NULL,                            /* connect_it */
324
  ZERO_NULL,                            /* connecting */
325
  ZERO_NULL,                            /* doing */
326
  http2_getsock,                        /* proto_getsock */
327
  http2_getsock,                        /* doing_getsock */
328
  ZERO_NULL,                            /* domore_getsock */
329
  http2_getsock,                        /* perform_getsock */
330
  http2_disconnect,                     /* disconnect */
331
  ZERO_NULL,                            /* readwrite */
332
  http2_conncheck,                      /* connection_check */
333
  ZERO_NULL,                            /* attach connection */
334
  PORT_HTTP,                            /* defport */
335
  CURLPROTO_HTTP,                       /* protocol */
336
  CURLPROTO_HTTP,                       /* family */
337
  PROTOPT_STREAM                        /* flags */
338
};
339
340
static const struct Curl_handler Curl_handler_http2_ssl = {
341
  "HTTPS",                              /* scheme */
342
  ZERO_NULL,                            /* setup_connection */
343
  Curl_http,                            /* do_it */
344
  Curl_http_done,                       /* done */
345
  ZERO_NULL,                            /* do_more */
346
  ZERO_NULL,                            /* connect_it */
347
  ZERO_NULL,                            /* connecting */
348
  ZERO_NULL,                            /* doing */
349
  http2_getsock,                        /* proto_getsock */
350
  http2_getsock,                        /* doing_getsock */
351
  ZERO_NULL,                            /* domore_getsock */
352
  http2_getsock,                        /* perform_getsock */
353
  http2_disconnect,                     /* disconnect */
354
  ZERO_NULL,                            /* readwrite */
355
  http2_conncheck,                      /* connection_check */
356
  ZERO_NULL,                            /* attach connection */
357
  PORT_HTTP,                            /* defport */
358
  CURLPROTO_HTTPS,                      /* protocol */
359
  CURLPROTO_HTTP,                       /* family */
360
  PROTOPT_SSL | PROTOPT_STREAM          /* flags */
361
};
362
363
/*
364
 * Store nghttp2 version info in this buffer.
365
 */
366
void Curl_http2_ver(char *p, size_t len)
367
0
{
368
0
  nghttp2_info *h2 = nghttp2_version(0);
369
0
  (void)msnprintf(p, len, "nghttp2/%s", h2->version_str);
370
0
}
371
372
/*
373
 * The implementation of nghttp2_send_callback type. Here we write |data| with
374
 * size |length| to the network and return the number of bytes actually
375
 * written. See the documentation of nghttp2_send_callback for the details.
376
 */
377
static ssize_t send_callback(nghttp2_session *h2,
378
                             const uint8_t *mem, size_t length, int flags,
379
                             void *userp)
380
45.4k
{
381
45.4k
  struct connectdata *conn = (struct connectdata *)userp;
382
45.4k
  struct http_conn *c = &conn->proto.httpc;
383
45.4k
  struct Curl_easy *data = get_transfer(c);
384
45.4k
  ssize_t written;
385
45.4k
  CURLcode result = CURLE_OK;
386
387
45.4k
  (void)h2;
388
45.4k
  (void)flags;
389
390
45.4k
  if(!c->send_underlying)
391
    /* called before setup properly! */
392
946
    return NGHTTP2_ERR_CALLBACK_FAILURE;
393
394
44.4k
  written = ((Curl_send*)c->send_underlying)(data, FIRSTSOCKET,
395
44.4k
                                             mem, length, &result);
396
397
44.4k
  if(result == CURLE_AGAIN) {
398
0
    return NGHTTP2_ERR_WOULDBLOCK;
399
0
  }
400
401
44.4k
  if(written == -1) {
402
0
    failf(data, "Failed sending HTTP2 data");
403
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
404
0
  }
405
406
44.4k
  if(!written)
407
0
    return NGHTTP2_ERR_WOULDBLOCK;
408
409
44.4k
  return written;
410
44.4k
}
411
412
413
/* We pass a pointer to this struct in the push callback, but the contents of
414
   the struct are hidden from the user. */
415
struct curl_pushheaders {
416
  struct Curl_easy *data;
417
  const nghttp2_push_promise *frame;
418
};
419
420
/*
421
 * push header access function. Only to be used from within the push callback
422
 */
423
char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
424
0
{
425
  /* Verify that we got a good easy handle in the push header struct, mostly to
426
     detect rubbish input fast(er). */
427
0
  if(!h || !GOOD_EASY_HANDLE(h->data))
428
0
    return NULL;
429
0
  else {
430
0
    struct HTTP *stream = h->data->req.p.http;
431
0
    if(num < stream->push_headers_used)
432
0
      return stream->push_headers[num];
433
0
  }
434
0
  return NULL;
435
0
}
436
437
/*
438
 * push header access function. Only to be used from within the push callback
439
 */
440
char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
441
0
{
442
  /* Verify that we got a good easy handle in the push header struct,
443
     mostly to detect rubbish input fast(er). Also empty header name
444
     is just a rubbish too. We have to allow ":" at the beginning of
445
     the header, but header == ":" must be rejected. If we have ':' in
446
     the middle of header, it could be matched in middle of the value,
447
     this is because we do prefix match.*/
448
0
  if(!h || !GOOD_EASY_HANDLE(h->data) || !header || !header[0] ||
449
0
     !strcmp(header, ":") || strchr(header + 1, ':'))
450
0
    return NULL;
451
0
  else {
452
0
    struct HTTP *stream = h->data->req.p.http;
453
0
    size_t len = strlen(header);
454
0
    size_t i;
455
0
    for(i = 0; i<stream->push_headers_used; i++) {
456
0
      if(!strncmp(header, stream->push_headers[i], len)) {
457
        /* sub-match, make sure that it is followed by a colon */
458
0
        if(stream->push_headers[i][len] != ':')
459
0
          continue;
460
0
        return &stream->push_headers[i][len + 1];
461
0
      }
462
0
    }
463
0
  }
464
0
  return NULL;
465
0
}
466
467
/*
468
 * This specific transfer on this connection has been "drained".
469
 */
470
static void drained_transfer(struct Curl_easy *data,
471
                             struct http_conn *httpc)
472
7.17k
{
473
7.17k
  DEBUGASSERT(httpc->drain_total >= data->state.drain);
474
7.17k
  httpc->drain_total -= data->state.drain;
475
7.17k
  data->state.drain = 0;
476
7.17k
}
477
478
/*
479
 * Mark this transfer to get "drained".
480
 */
481
static void drain_this(struct Curl_easy *data,
482
                       struct http_conn *httpc)
483
8.20k
{
484
8.20k
  data->state.drain++;
485
8.20k
  httpc->drain_total++;
486
8.20k
  DEBUGASSERT(httpc->drain_total >= data->state.drain);
487
8.20k
}
488
489
static struct Curl_easy *duphandle(struct Curl_easy *data)
490
0
{
491
0
  struct Curl_easy *second = curl_easy_duphandle(data);
492
0
  if(second) {
493
    /* setup the request struct */
494
0
    struct HTTP *http = calloc(1, sizeof(struct HTTP));
495
0
    if(!http) {
496
0
      (void)Curl_close(&second);
497
0
    }
498
0
    else {
499
0
      second->req.p.http = http;
500
0
      Curl_dyn_init(&http->header_recvbuf, DYN_H2_HEADERS);
501
0
      Curl_http2_setup_req(second);
502
0
      second->state.stream_weight = data->state.stream_weight;
503
0
    }
504
0
  }
505
0
  return second;
506
0
}
507
508
static int set_transfer_url(struct Curl_easy *data,
509
                            struct curl_pushheaders *hp)
510
0
{
511
0
  const char *v;
512
0
  CURLUcode uc;
513
0
  char *url = NULL;
514
0
  int rc = 0;
515
0
  CURLU *u = curl_url();
516
517
0
  if(!u)
518
0
    return 5;
519
520
0
  v = curl_pushheader_byname(hp, H2H3_PSEUDO_SCHEME);
521
0
  if(v) {
522
0
    uc = curl_url_set(u, CURLUPART_SCHEME, v, 0);
523
0
    if(uc) {
524
0
      rc = 1;
525
0
      goto fail;
526
0
    }
527
0
  }
528
529
0
  v = curl_pushheader_byname(hp, H2H3_PSEUDO_AUTHORITY);
530
0
  if(v) {
531
0
    uc = curl_url_set(u, CURLUPART_HOST, v, 0);
532
0
    if(uc) {
533
0
      rc = 2;
534
0
      goto fail;
535
0
    }
536
0
  }
537
538
0
  v = curl_pushheader_byname(hp, H2H3_PSEUDO_PATH);
539
0
  if(v) {
540
0
    uc = curl_url_set(u, CURLUPART_PATH, v, 0);
541
0
    if(uc) {
542
0
      rc = 3;
543
0
      goto fail;
544
0
    }
545
0
  }
546
547
0
  uc = curl_url_get(u, CURLUPART_URL, &url, 0);
548
0
  if(uc)
549
0
    rc = 4;
550
0
  fail:
551
0
  curl_url_cleanup(u);
552
0
  if(rc)
553
0
    return rc;
554
555
0
  if(data->state.url_alloc)
556
0
    free(data->state.url);
557
0
  data->state.url_alloc = TRUE;
558
0
  data->state.url = url;
559
0
  return 0;
560
0
}
561
562
static int push_promise(struct Curl_easy *data,
563
                        struct connectdata *conn,
564
                        const nghttp2_push_promise *frame)
565
0
{
566
0
  int rv; /* one of the CURL_PUSH_* defines */
567
0
  H2BUGF(infof(data, "PUSH_PROMISE received, stream %u",
568
0
               frame->promised_stream_id));
569
0
  if(data->multi->push_cb) {
570
0
    struct HTTP *stream;
571
0
    struct HTTP *newstream;
572
0
    struct curl_pushheaders heads;
573
0
    CURLMcode rc;
574
0
    struct http_conn *httpc;
575
0
    size_t i;
576
    /* clone the parent */
577
0
    struct Curl_easy *newhandle = duphandle(data);
578
0
    if(!newhandle) {
579
0
      infof(data, "failed to duplicate handle");
580
0
      rv = CURL_PUSH_DENY; /* FAIL HARD */
581
0
      goto fail;
582
0
    }
583
584
0
    heads.data = data;
585
0
    heads.frame = frame;
586
    /* ask the application */
587
0
    H2BUGF(infof(data, "Got PUSH_PROMISE, ask application"));
588
589
0
    stream = data->req.p.http;
590
0
    if(!stream) {
591
0
      failf(data, "Internal NULL stream");
592
0
      (void)Curl_close(&newhandle);
593
0
      rv = CURL_PUSH_DENY;
594
0
      goto fail;
595
0
    }
596
597
0
    rv = set_transfer_url(newhandle, &heads);
598
0
    if(rv) {
599
0
      (void)Curl_close(&newhandle);
600
0
      rv = CURL_PUSH_DENY;
601
0
      goto fail;
602
0
    }
603
604
0
    Curl_set_in_callback(data, true);
605
0
    rv = data->multi->push_cb(data, newhandle,
606
0
                              stream->push_headers_used, &heads,
607
0
                              data->multi->push_userp);
608
0
    Curl_set_in_callback(data, false);
609
610
    /* free the headers again */
611
0
    for(i = 0; i<stream->push_headers_used; i++)
612
0
      free(stream->push_headers[i]);
613
0
    free(stream->push_headers);
614
0
    stream->push_headers = NULL;
615
0
    stream->push_headers_used = 0;
616
617
0
    if(rv) {
618
0
      DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
619
      /* denied, kill off the new handle again */
620
0
      http2_stream_free(newhandle->req.p.http);
621
0
      newhandle->req.p.http = NULL;
622
0
      (void)Curl_close(&newhandle);
623
0
      goto fail;
624
0
    }
625
626
0
    newstream = newhandle->req.p.http;
627
0
    newstream->stream_id = frame->promised_stream_id;
628
0
    newhandle->req.maxdownload = -1;
629
0
    newhandle->req.size = -1;
630
631
    /* approved, add to the multi handle and immediately switch to PERFORM
632
       state with the given connection !*/
633
0
    rc = Curl_multi_add_perform(data->multi, newhandle, conn);
634
0
    if(rc) {
635
0
      infof(data, "failed to add handle to multi");
636
0
      http2_stream_free(newhandle->req.p.http);
637
0
      newhandle->req.p.http = NULL;
638
0
      Curl_close(&newhandle);
639
0
      rv = CURL_PUSH_DENY;
640
0
      goto fail;
641
0
    }
642
643
0
    httpc = &conn->proto.httpc;
644
0
    rv = nghttp2_session_set_stream_user_data(httpc->h2,
645
0
                                              frame->promised_stream_id,
646
0
                                              newhandle);
647
0
    if(rv) {
648
0
      infof(data, "failed to set user_data for stream %u",
649
0
            frame->promised_stream_id);
650
0
      DEBUGASSERT(0);
651
0
      rv = CURL_PUSH_DENY;
652
0
      goto fail;
653
0
    }
654
0
    Curl_dyn_init(&newstream->header_recvbuf, DYN_H2_HEADERS);
655
0
    Curl_dyn_init(&newstream->trailer_recvbuf, DYN_H2_TRAILERS);
656
0
  }
657
0
  else {
658
0
    H2BUGF(infof(data, "Got PUSH_PROMISE, ignore it"));
659
0
    rv = CURL_PUSH_DENY;
660
0
  }
661
0
  fail:
662
0
  return rv;
663
0
}
664
665
/*
666
 * multi_connchanged() is called to tell that there is a connection in
667
 * this multi handle that has changed state (multiplexing become possible, the
668
 * number of allowed streams changed or similar), and a subsequent use of this
669
 * multi handle should move CONNECT_PEND handles back to CONNECT to have them
670
 * retry.
671
 */
672
static void multi_connchanged(struct Curl_multi *multi)
673
14.0k
{
674
14.0k
  multi->recheckstate = TRUE;
675
14.0k
}
676
677
static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
678
                         void *userp)
679
9.51k
{
680
9.51k
  struct connectdata *conn = (struct connectdata *)userp;
681
9.51k
  struct http_conn *httpc = &conn->proto.httpc;
682
9.51k
  struct Curl_easy *data_s = NULL;
683
9.51k
  struct HTTP *stream = NULL;
684
9.51k
  struct Curl_easy *data = get_transfer(httpc);
685
9.51k
  int rv;
686
9.51k
  size_t left, ncopy;
687
9.51k
  int32_t stream_id = frame->hd.stream_id;
688
9.51k
  CURLcode result;
689
690
9.51k
  if(!stream_id) {
691
    /* stream ID zero is for connection-oriented stuff */
692
6.61k
    if(frame->hd.type == NGHTTP2_SETTINGS) {
693
6.17k
      uint32_t max_conn = httpc->settings.max_concurrent_streams;
694
6.17k
      H2BUGF(infof(data, "Got SETTINGS"));
695
6.17k
      httpc->settings.max_concurrent_streams =
696
6.17k
        nghttp2_session_get_remote_settings(
697
6.17k
          session, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
698
6.17k
      httpc->settings.enable_push =
699
6.17k
        nghttp2_session_get_remote_settings(
700
6.17k
          session, NGHTTP2_SETTINGS_ENABLE_PUSH);
701
6.17k
      H2BUGF(infof(data, "MAX_CONCURRENT_STREAMS == %d",
702
6.17k
                   httpc->settings.max_concurrent_streams));
703
6.17k
      H2BUGF(infof(data, "ENABLE_PUSH == %s",
704
6.17k
                   httpc->settings.enable_push?"TRUE":"false"));
705
6.17k
      if(max_conn != httpc->settings.max_concurrent_streams) {
706
        /* only signal change if the value actually changed */
707
5.04k
        infof(data,
708
5.04k
              "Connection state changed (MAX_CONCURRENT_STREAMS == %u)!",
709
5.04k
              httpc->settings.max_concurrent_streams);
710
5.04k
        multi_connchanged(data->multi);
711
5.04k
      }
712
6.17k
    }
713
6.61k
    return 0;
714
6.61k
  }
715
2.90k
  data_s = nghttp2_session_get_stream_user_data(session, stream_id);
716
2.90k
  if(!data_s) {
717
136
    H2BUGF(infof(data,
718
136
                 "No Curl_easy associated with stream: %u",
719
136
                 stream_id));
720
136
    return 0;
721
136
  }
722
723
2.76k
  stream = data_s->req.p.http;
724
2.76k
  if(!stream) {
725
0
    H2BUGF(infof(data_s, "No proto pointer for stream: %u",
726
0
                 stream_id));
727
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
728
0
  }
729
730
2.76k
  H2BUGF(infof(data_s, "on_frame_recv() header %x stream %u",
731
2.76k
               frame->hd.type, stream_id));
732
733
2.76k
  switch(frame->hd.type) {
734
263
  case NGHTTP2_DATA:
735
    /* If body started on this stream, then receiving DATA is illegal. */
736
263
    if(!stream->bodystarted) {
737
4
      rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
738
4
                                     stream_id, NGHTTP2_PROTOCOL_ERROR);
739
740
4
      if(nghttp2_is_fatal(rv)) {
741
0
        return NGHTTP2_ERR_CALLBACK_FAILURE;
742
0
      }
743
4
    }
744
263
    break;
745
1.64k
  case NGHTTP2_HEADERS:
746
1.64k
    if(stream->bodystarted) {
747
      /* Only valid HEADERS after body started is trailer HEADERS.  We
748
         buffer them in on_header callback. */
749
5
      break;
750
5
    }
751
752
    /* nghttp2 guarantees that :status is received, and we store it to
753
       stream->status_code. Fuzzing has proven this can still be reached
754
       without status code having been set. */
755
1.64k
    if(stream->status_code == -1)
756
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
757
758
    /* Only final status code signals the end of header */
759
1.64k
    if(stream->status_code / 100 != 1) {
760
1.55k
      stream->bodystarted = TRUE;
761
1.55k
      stream->status_code = -1;
762
1.55k
    }
763
764
1.64k
    result = Curl_dyn_addn(&stream->header_recvbuf, STRCONST("\r\n"));
765
1.64k
    if(result)
766
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
767
768
1.64k
    left = Curl_dyn_len(&stream->header_recvbuf) -
769
1.64k
      stream->nread_header_recvbuf;
770
1.64k
    ncopy = CURLMIN(stream->len, left);
771
772
1.64k
    memcpy(&stream->mem[stream->memlen],
773
1.64k
           Curl_dyn_ptr(&stream->header_recvbuf) +
774
1.64k
           stream->nread_header_recvbuf,
775
1.64k
           ncopy);
776
1.64k
    stream->nread_header_recvbuf += ncopy;
777
778
1.64k
    DEBUGASSERT(stream->mem);
779
1.64k
    H2BUGF(infof(data_s, "Store %zu bytes headers from stream %u at %p",
780
1.64k
                 ncopy, stream_id, stream->mem));
781
782
1.64k
    stream->len -= ncopy;
783
1.64k
    stream->memlen += ncopy;
784
785
1.64k
    drain_this(data_s, httpc);
786
    /* if we receive data for another handle, wake that up */
787
1.64k
    if(get_transfer(httpc) != data_s)
788
0
      Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
789
1.64k
    break;
790
0
  case NGHTTP2_PUSH_PROMISE:
791
0
    rv = push_promise(data_s, conn, &frame->push_promise);
792
0
    if(rv) { /* deny! */
793
0
      int h2;
794
0
      DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
795
0
      h2 = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
796
0
                                     frame->push_promise.promised_stream_id,
797
0
                                     NGHTTP2_CANCEL);
798
0
      if(nghttp2_is_fatal(h2))
799
0
        return NGHTTP2_ERR_CALLBACK_FAILURE;
800
0
      else if(rv == CURL_PUSH_ERROROUT) {
801
0
        DEBUGF(infof(data_s, "Fail the parent stream (too)"));
802
0
        return NGHTTP2_ERR_CALLBACK_FAILURE;
803
0
      }
804
0
    }
805
0
    break;
806
854
  default:
807
854
    H2BUGF(infof(data_s, "Got frame type %x for stream %u",
808
854
                 frame->hd.type, stream_id));
809
854
    break;
810
2.76k
  }
811
2.76k
  return 0;
812
2.76k
}
813
814
static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
815
                              int32_t stream_id,
816
                              const uint8_t *mem, size_t len, void *userp)
817
276
{
818
276
  struct HTTP *stream;
819
276
  struct Curl_easy *data_s;
820
276
  size_t nread;
821
276
  struct connectdata *conn = (struct connectdata *)userp;
822
276
  struct http_conn *httpc = &conn->proto.httpc;
823
276
  (void)session;
824
276
  (void)flags;
825
826
276
  DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
827
828
  /* get the stream from the hash based on Stream ID */
829
276
  data_s = nghttp2_session_get_stream_user_data(session, stream_id);
830
276
  if(!data_s) {
831
    /* Receiving a Stream ID not in the hash should not happen - unless
832
       we have aborted a transfer artificially and there were more data
833
       in the pipeline. Silently ignore. */
834
0
    H2BUGF(fprintf(stderr, "Data for stream %u but it doesn't exist\n",
835
0
                   stream_id));
836
0
    return 0;
837
0
  }
838
839
276
  stream = data_s->req.p.http;
840
276
  if(!stream)
841
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
842
843
276
  nread = CURLMIN(stream->len, len);
844
276
  memcpy(&stream->mem[stream->memlen], mem, nread);
845
846
276
  stream->len -= nread;
847
276
  stream->memlen += nread;
848
849
276
  drain_this(data_s, &conn->proto.httpc);
850
851
  /* if we receive data for another handle, wake that up */
852
276
  if(get_transfer(httpc) != data_s)
853
0
    Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
854
855
276
  H2BUGF(infof(data_s, "%zu data received for stream %u "
856
276
               "(%zu left in buffer %p, total %zu)",
857
276
               nread, stream_id,
858
276
               stream->len, stream->mem,
859
276
               stream->memlen));
860
861
276
  if(nread < len) {
862
14
    stream->pausedata = mem + nread;
863
14
    stream->pauselen = len - nread;
864
14
    H2BUGF(infof(data_s, "NGHTTP2_ERR_PAUSE - %zu bytes out of buffer"
865
14
                 ", stream %u",
866
14
                 len - nread, stream_id));
867
14
    data_s->conn->proto.httpc.pause_stream_id = stream_id;
868
869
14
    return NGHTTP2_ERR_PAUSE;
870
14
  }
871
872
  /* pause execution of nghttp2 if we received data for another handle
873
     in order to process them first. */
874
262
  if(get_transfer(httpc) != data_s) {
875
0
    data_s->conn->proto.httpc.pause_stream_id = stream_id;
876
877
0
    return NGHTTP2_ERR_PAUSE;
878
0
  }
879
880
262
  return 0;
881
262
}
882
883
static int on_stream_close(nghttp2_session *session, int32_t stream_id,
884
                           uint32_t error_code, void *userp)
885
6.28k
{
886
6.28k
  struct Curl_easy *data_s;
887
6.28k
  struct HTTP *stream;
888
6.28k
  struct connectdata *conn = (struct connectdata *)userp;
889
6.28k
  int rv;
890
6.28k
  (void)session;
891
6.28k
  (void)stream_id;
892
893
6.28k
  if(stream_id) {
894
6.28k
    struct http_conn *httpc;
895
    /* get the stream from the hash based on Stream ID, stream ID zero is for
896
       connection-oriented stuff */
897
6.28k
    data_s = nghttp2_session_get_stream_user_data(session, stream_id);
898
6.28k
    if(!data_s) {
899
      /* We could get stream ID not in the hash.  For example, if we
900
         decided to reject stream (e.g., PUSH_PROMISE). */
901
0
      return 0;
902
0
    }
903
6.28k
    H2BUGF(infof(data_s, "on_stream_close(), %s (err %d), stream %u",
904
6.28k
                 nghttp2_http2_strerror(error_code), error_code, stream_id));
905
6.28k
    stream = data_s->req.p.http;
906
6.28k
    if(!stream)
907
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
908
909
6.28k
    stream->closed = TRUE;
910
6.28k
    httpc = &conn->proto.httpc;
911
6.28k
    drain_this(data_s, httpc);
912
6.28k
    Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
913
6.28k
    stream->error = error_code;
914
915
    /* remove the entry from the hash as the stream is now gone */
916
6.28k
    rv = nghttp2_session_set_stream_user_data(session, stream_id, 0);
917
6.28k
    if(rv) {
918
0
      infof(data_s, "http/2: failed to clear user_data for stream %u",
919
0
            stream_id);
920
0
      DEBUGASSERT(0);
921
0
    }
922
6.28k
    if(stream_id == httpc->pause_stream_id) {
923
1
      H2BUGF(infof(data_s, "Stopped the pause stream"));
924
1
      httpc->pause_stream_id = 0;
925
1
    }
926
6.28k
    H2BUGF(infof(data_s, "Removed stream %u hash", stream_id));
927
6.28k
    stream->stream_id = 0; /* cleared */
928
6.28k
  }
929
6.28k
  return 0;
930
6.28k
}
931
932
static int on_begin_headers(nghttp2_session *session,
933
                            const nghttp2_frame *frame, void *userp)
934
2.96k
{
935
2.96k
  struct HTTP *stream;
936
2.96k
  struct Curl_easy *data_s = NULL;
937
2.96k
  (void)userp;
938
939
2.96k
  data_s = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
940
2.96k
  if(!data_s) {
941
0
    return 0;
942
0
  }
943
944
2.96k
  H2BUGF(infof(data_s, "on_begin_headers() was called"));
945
946
2.96k
  if(frame->hd.type != NGHTTP2_HEADERS) {
947
0
    return 0;
948
0
  }
949
950
2.96k
  stream = data_s->req.p.http;
951
2.96k
  if(!stream || !stream->bodystarted) {
952
2.89k
    return 0;
953
2.89k
  }
954
955
71
  return 0;
956
2.96k
}
957
958
/* Decode HTTP status code.  Returns -1 if no valid status code was
959
   decoded. */
960
static int decode_status_code(const uint8_t *value, size_t len)
961
1.84k
{
962
1.84k
  int i;
963
1.84k
  int res;
964
965
1.84k
  if(len != 3) {
966
0
    return -1;
967
0
  }
968
969
1.84k
  res = 0;
970
971
7.39k
  for(i = 0; i < 3; ++i) {
972
5.54k
    char c = value[i];
973
974
5.54k
    if(c < '0' || c > '9') {
975
0
      return -1;
976
0
    }
977
978
5.54k
    res *= 10;
979
5.54k
    res += c - '0';
980
5.54k
  }
981
982
1.84k
  return res;
983
1.84k
}
984
985
/* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
986
static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
987
                     const uint8_t *name, size_t namelen,
988
                     const uint8_t *value, size_t valuelen,
989
                     uint8_t flags,
990
                     void *userp)
991
51.5k
{
992
51.5k
  struct HTTP *stream;
993
51.5k
  struct Curl_easy *data_s;
994
51.5k
  int32_t stream_id = frame->hd.stream_id;
995
51.5k
  struct connectdata *conn = (struct connectdata *)userp;
996
51.5k
  struct http_conn *httpc = &conn->proto.httpc;
997
51.5k
  CURLcode result;
998
51.5k
  (void)flags;
999
1000
51.5k
  DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
1001
1002
  /* get the stream from the hash based on Stream ID */
1003
51.5k
  data_s = nghttp2_session_get_stream_user_data(session, stream_id);
1004
51.5k
  if(!data_s)
1005
    /* Receiving a Stream ID not in the hash should not happen, this is an
1006
       internal error more than anything else! */
1007
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1008
1009
51.5k
  stream = data_s->req.p.http;
1010
51.5k
  if(!stream) {
1011
0
    failf(data_s, "Internal NULL stream");
1012
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1013
0
  }
1014
1015
  /* Store received PUSH_PROMISE headers to be used when the subsequent
1016
     PUSH_PROMISE callback comes */
1017
51.5k
  if(frame->hd.type == NGHTTP2_PUSH_PROMISE) {
1018
0
    char *h;
1019
1020
0
    if(!strcmp(H2H3_PSEUDO_AUTHORITY, (const char *)name)) {
1021
      /* pseudo headers are lower case */
1022
0
      int rc = 0;
1023
0
      char *check = aprintf("%s:%d", conn->host.name, conn->remote_port);
1024
0
      if(!check)
1025
        /* no memory */
1026
0
        return NGHTTP2_ERR_CALLBACK_FAILURE;
1027
0
      if(!Curl_strcasecompare(check, (const char *)value) &&
1028
0
         ((conn->remote_port != conn->given->defport) ||
1029
0
          !Curl_strcasecompare(conn->host.name, (const char *)value))) {
1030
        /* This is push is not for the same authority that was asked for in
1031
         * the URL. RFC 7540 section 8.2 says: "A client MUST treat a
1032
         * PUSH_PROMISE for which the server is not authoritative as a stream
1033
         * error of type PROTOCOL_ERROR."
1034
         */
1035
0
        (void)nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
1036
0
                                        stream_id, NGHTTP2_PROTOCOL_ERROR);
1037
0
        rc = NGHTTP2_ERR_CALLBACK_FAILURE;
1038
0
      }
1039
0
      free(check);
1040
0
      if(rc)
1041
0
        return rc;
1042
0
    }
1043
1044
0
    if(!stream->push_headers) {
1045
0
      stream->push_headers_alloc = 10;
1046
0
      stream->push_headers = malloc(stream->push_headers_alloc *
1047
0
                                    sizeof(char *));
1048
0
      if(!stream->push_headers)
1049
0
        return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
1050
0
      stream->push_headers_used = 0;
1051
0
    }
1052
0
    else if(stream->push_headers_used ==
1053
0
            stream->push_headers_alloc) {
1054
0
      char **headp;
1055
0
      if(stream->push_headers_alloc > 1000) {
1056
        /* this is beyond crazy many headers, bail out */
1057
0
        failf(data_s, "Too many PUSH_PROMISE headers");
1058
0
        Curl_safefree(stream->push_headers);
1059
0
        return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
1060
0
      }
1061
0
      stream->push_headers_alloc *= 2;
1062
0
      headp = Curl_saferealloc(stream->push_headers,
1063
0
                               stream->push_headers_alloc * sizeof(char *));
1064
0
      if(!headp) {
1065
0
        stream->push_headers = NULL;
1066
0
        return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
1067
0
      }
1068
0
      stream->push_headers = headp;
1069
0
    }
1070
0
    h = aprintf("%s:%s", name, value);
1071
0
    if(h)
1072
0
      stream->push_headers[stream->push_headers_used++] = h;
1073
0
    return 0;
1074
0
  }
1075
1076
51.5k
  if(stream->bodystarted) {
1077
    /* This is a trailer */
1078
513
    H2BUGF(infof(data_s, "h2 trailer: %.*s: %.*s", namelen, name, valuelen,
1079
513
                 value));
1080
513
    result = Curl_dyn_addf(&stream->trailer_recvbuf,
1081
513
                           "%.*s: %.*s\r\n", namelen, name,
1082
513
                           valuelen, value);
1083
513
    if(result)
1084
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1085
1086
513
    return 0;
1087
513
  }
1088
1089
51.0k
  if(namelen == sizeof(H2H3_PSEUDO_STATUS) - 1 &&
1090
51.0k
     memcmp(H2H3_PSEUDO_STATUS, name, namelen) == 0) {
1091
    /* nghttp2 guarantees :status is received first and only once, and
1092
       value is 3 digits status code, and decode_status_code always
1093
       succeeds. */
1094
1.84k
    char buffer[32];
1095
1.84k
    stream->status_code = decode_status_code(value, valuelen);
1096
1.84k
    DEBUGASSERT(stream->status_code != -1);
1097
1.84k
    msnprintf(buffer, sizeof(buffer), H2H3_PSEUDO_STATUS ":%u\r",
1098
1.84k
              stream->status_code);
1099
1.84k
    result = Curl_headers_push(data_s, buffer, CURLH_PSEUDO);
1100
1.84k
    if(result)
1101
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1102
1.84k
    result = Curl_dyn_addn(&stream->header_recvbuf, STRCONST("HTTP/2 "));
1103
1.84k
    if(result)
1104
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1105
1.84k
    result = Curl_dyn_addn(&stream->header_recvbuf, value, valuelen);
1106
1.84k
    if(result)
1107
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1108
    /* the space character after the status code is mandatory */
1109
1.84k
    result = Curl_dyn_addn(&stream->header_recvbuf, STRCONST(" \r\n"));
1110
1.84k
    if(result)
1111
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1112
    /* if we receive data for another handle, wake that up */
1113
1.84k
    if(get_transfer(httpc) != data_s)
1114
0
      Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
1115
1116
1.84k
    H2BUGF(infof(data_s, "h2 status: HTTP/2 %03d (easy %p)",
1117
1.84k
                 stream->status_code, data_s));
1118
1.84k
    return 0;
1119
1.84k
  }
1120
1121
  /* nghttp2 guarantees that namelen > 0, and :status was already
1122
     received, and this is not pseudo-header field . */
1123
  /* convert to a HTTP1-style header */
1124
49.1k
  result = Curl_dyn_addn(&stream->header_recvbuf, name, namelen);
1125
49.1k
  if(result)
1126
1
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1127
49.1k
  result = Curl_dyn_addn(&stream->header_recvbuf, STRCONST(": "));
1128
49.1k
  if(result)
1129
1
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1130
49.1k
  result = Curl_dyn_addn(&stream->header_recvbuf, value, valuelen);
1131
49.1k
  if(result)
1132
3
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1133
49.1k
  result = Curl_dyn_addn(&stream->header_recvbuf, STRCONST("\r\n"));
1134
49.1k
  if(result)
1135
1
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1136
  /* if we receive data for another handle, wake that up */
1137
49.1k
  if(get_transfer(httpc) != data_s)
1138
0
    Curl_expire(data_s, 0, EXPIRE_RUN_NOW);
1139
1140
49.1k
  H2BUGF(infof(data_s, "h2 header: %.*s: %.*s", namelen, name, valuelen,
1141
49.1k
               value));
1142
1143
49.1k
  return 0; /* 0 is successful */
1144
49.1k
}
1145
1146
static ssize_t data_source_read_callback(nghttp2_session *session,
1147
                                         int32_t stream_id,
1148
                                         uint8_t *buf, size_t length,
1149
                                         uint32_t *data_flags,
1150
                                         nghttp2_data_source *source,
1151
                                         void *userp)
1152
3.15k
{
1153
3.15k
  struct Curl_easy *data_s;
1154
3.15k
  struct HTTP *stream = NULL;
1155
3.15k
  size_t nread;
1156
3.15k
  (void)source;
1157
3.15k
  (void)userp;
1158
1159
3.15k
  if(stream_id) {
1160
    /* get the stream from the hash based on Stream ID, stream ID zero is for
1161
       connection-oriented stuff */
1162
3.15k
    data_s = nghttp2_session_get_stream_user_data(session, stream_id);
1163
3.15k
    if(!data_s)
1164
      /* Receiving a Stream ID not in the hash should not happen, this is an
1165
         internal error more than anything else! */
1166
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1167
1168
3.15k
    stream = data_s->req.p.http;
1169
3.15k
    if(!stream)
1170
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1171
3.15k
  }
1172
0
  else
1173
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
1174
1175
3.15k
  nread = CURLMIN(stream->upload_len, length);
1176
3.15k
  if(nread > 0) {
1177
1.15k
    memcpy(buf, stream->upload_mem, nread);
1178
1.15k
    stream->upload_mem += nread;
1179
1.15k
    stream->upload_len -= nread;
1180
1.15k
    if(data_s->state.infilesize != -1)
1181
1.00k
      stream->upload_left -= nread;
1182
1.15k
  }
1183
1184
3.15k
  if(stream->upload_left == 0)
1185
544
    *data_flags = NGHTTP2_DATA_FLAG_EOF;
1186
2.60k
  else if(nread == 0)
1187
1.68k
    return NGHTTP2_ERR_DEFERRED;
1188
1189
1.47k
  H2BUGF(infof(data_s, "data_source_read_callback: "
1190
1.47k
               "returns %zu bytes stream %u",
1191
1.47k
               nread, stream_id));
1192
1193
1.47k
  return nread;
1194
3.15k
}
1195
1196
#if !defined(CURL_DISABLE_VERBOSE_STRINGS)
1197
static int error_callback(nghttp2_session *session,
1198
                          const char *msg,
1199
                          size_t len,
1200
                          void *userp)
1201
630
{
1202
630
  (void)session;
1203
630
  (void)msg;
1204
630
  (void)len;
1205
630
  (void)userp;
1206
630
  return 0;
1207
630
}
1208
#endif
1209
1210
static void populate_settings(struct Curl_easy *data,
1211
                              struct http_conn *httpc)
1212
7.86k
{
1213
7.86k
  nghttp2_settings_entry *iv = httpc->local_settings;
1214
1215
7.86k
  iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
1216
7.86k
  iv[0].value = Curl_multi_max_concurrent_streams(data->multi);
1217
1218
7.86k
  iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
1219
7.86k
  iv[1].value = HTTP2_HUGE_WINDOW_SIZE;
1220
1221
7.86k
  iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
1222
7.86k
  iv[2].value = data->multi->push_cb != NULL;
1223
1224
7.86k
  httpc->local_settings_num = 3;
1225
7.86k
}
1226
1227
void Curl_http2_done(struct Curl_easy *data, bool premature)
1228
34.0k
{
1229
34.0k
  struct HTTP *http = data->req.p.http;
1230
34.0k
  struct http_conn *httpc = &data->conn->proto.httpc;
1231
1232
  /* there might be allocated resources done before this got the 'h2' pointer
1233
     setup */
1234
34.0k
  Curl_dyn_free(&http->header_recvbuf);
1235
34.0k
  Curl_dyn_free(&http->trailer_recvbuf);
1236
34.0k
  if(http->push_headers) {
1237
    /* if they weren't used and then freed before */
1238
0
    for(; http->push_headers_used > 0; --http->push_headers_used) {
1239
0
      free(http->push_headers[http->push_headers_used - 1]);
1240
0
    }
1241
0
    free(http->push_headers);
1242
0
    http->push_headers = NULL;
1243
0
  }
1244
1245
34.0k
  if(!(data->conn->handler->protocol&PROTO_FAMILY_HTTP) ||
1246
34.0k
     !httpc->h2) /* not HTTP/2 ? */
1247
24.9k
    return;
1248
1249
  /* do this before the reset handling, as that might clear ->stream_id */
1250
9.02k
  if(http->stream_id == httpc->pause_stream_id) {
1251
2.03k
    H2BUGF(infof(data, "DONE the pause stream (%u)", http->stream_id));
1252
2.03k
    httpc->pause_stream_id = 0;
1253
2.03k
  }
1254
9.02k
  if(premature || (!http->closed && http->stream_id)) {
1255
    /* RST_STREAM */
1256
7.71k
    set_transfer(httpc, data); /* set the transfer */
1257
7.71k
    H2BUGF(infof(data, "RST stream %u", http->stream_id));
1258
7.71k
    if(!nghttp2_submit_rst_stream(httpc->h2, NGHTTP2_FLAG_NONE,
1259
7.71k
                                  http->stream_id, NGHTTP2_STREAM_CLOSED))
1260
6.99k
      (void)nghttp2_session_send(httpc->h2);
1261
7.71k
  }
1262
1263
9.02k
  if(data->state.drain)
1264
4.78k
    drained_transfer(data, httpc);
1265
1266
  /* -1 means unassigned and 0 means cleared */
1267
9.02k
  if(http->stream_id > 0) {
1268
1.77k
    int rv = nghttp2_session_set_stream_user_data(httpc->h2,
1269
1.77k
                                                  http->stream_id, 0);
1270
1.77k
    if(rv) {
1271
0
      infof(data, "http/2: failed to clear user_data for stream %u",
1272
0
            http->stream_id);
1273
0
      DEBUGASSERT(0);
1274
0
    }
1275
1.77k
    set_transfer(httpc, NULL);
1276
1.77k
    http->stream_id = 0;
1277
1.77k
  }
1278
9.02k
}
1279
1280
static int client_new(struct connectdata *conn,
1281
                      nghttp2_session_callbacks *callbacks)
1282
8.37k
{
1283
8.37k
#if NGHTTP2_VERSION_NUM < 0x013200
1284
  /* before 1.50.0 */
1285
8.37k
  return nghttp2_session_client_new(&conn->proto.httpc.h2, callbacks, conn);
1286
#else
1287
  nghttp2_option *o;
1288
  int rc = nghttp2_option_new(&o);
1289
  if(rc)
1290
    return rc;
1291
  /* turn off RFC 9113 leading and trailing white spaces validation against
1292
     HTTP field value. */
1293
  nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1);
1294
  rc = nghttp2_session_client_new2(&conn->proto.httpc.h2, callbacks, conn,
1295
                                   o);
1296
  nghttp2_option_del(o);
1297
  return rc;
1298
#endif
1299
8.37k
}
1300
1301
/*
1302
 * Initialize nghttp2 for a Curl connection
1303
 */
1304
static CURLcode http2_init(struct Curl_easy *data, struct connectdata *conn)
1305
8.37k
{
1306
8.37k
  if(!conn->proto.httpc.h2) {
1307
8.37k
    int rc;
1308
8.37k
    nghttp2_session_callbacks *callbacks;
1309
1310
8.37k
    conn->proto.httpc.inbuf = malloc(H2_BUFSIZE);
1311
8.37k
    if(!conn->proto.httpc.inbuf)
1312
0
      return CURLE_OUT_OF_MEMORY;
1313
1314
8.37k
    rc = nghttp2_session_callbacks_new(&callbacks);
1315
1316
8.37k
    if(rc) {
1317
0
      failf(data, "Couldn't initialize nghttp2 callbacks");
1318
0
      return CURLE_OUT_OF_MEMORY; /* most likely at least */
1319
0
    }
1320
1321
    /* nghttp2_send_callback */
1322
8.37k
    nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
1323
    /* nghttp2_on_frame_recv_callback */
1324
8.37k
    nghttp2_session_callbacks_set_on_frame_recv_callback
1325
8.37k
      (callbacks, on_frame_recv);
1326
    /* nghttp2_on_data_chunk_recv_callback */
1327
8.37k
    nghttp2_session_callbacks_set_on_data_chunk_recv_callback
1328
8.37k
      (callbacks, on_data_chunk_recv);
1329
    /* nghttp2_on_stream_close_callback */
1330
8.37k
    nghttp2_session_callbacks_set_on_stream_close_callback
1331
8.37k
      (callbacks, on_stream_close);
1332
    /* nghttp2_on_begin_headers_callback */
1333
8.37k
    nghttp2_session_callbacks_set_on_begin_headers_callback
1334
8.37k
      (callbacks, on_begin_headers);
1335
    /* nghttp2_on_header_callback */
1336
8.37k
    nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header);
1337
1338
8.37k
    nghttp2_session_callbacks_set_error_callback(callbacks, error_callback);
1339
1340
    /* The nghttp2 session is not yet setup, do it */
1341
8.37k
    rc = client_new(conn, callbacks);
1342
1343
8.37k
    nghttp2_session_callbacks_del(callbacks);
1344
1345
8.37k
    if(rc) {
1346
0
      failf(data, "Couldn't initialize nghttp2");
1347
0
      return CURLE_OUT_OF_MEMORY; /* most likely at least */
1348
0
    }
1349
8.37k
  }
1350
8.37k
  return CURLE_OK;
1351
8.37k
}
1352
1353
/*
1354
 * Append headers to ask for a HTTP1.1 to HTTP2 upgrade.
1355
 */
1356
CURLcode Curl_http2_request_upgrade(struct dynbuf *req,
1357
                                    struct Curl_easy *data)
1358
26
{
1359
26
  CURLcode result;
1360
26
  ssize_t binlen;
1361
26
  char *base64;
1362
26
  size_t blen;
1363
26
  struct connectdata *conn = data->conn;
1364
26
  struct SingleRequest *k = &data->req;
1365
26
  uint8_t *binsettings = conn->proto.httpc.binsettings;
1366
26
  struct http_conn *httpc = &conn->proto.httpc;
1367
1368
26
  populate_settings(data, httpc);
1369
1370
  /* this returns number of bytes it wrote */
1371
26
  binlen = nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
1372
26
                                         httpc->local_settings,
1373
26
                                         httpc->local_settings_num);
1374
26
  if(binlen <= 0) {
1375
0
    failf(data, "nghttp2 unexpectedly failed on pack_settings_payload");
1376
0
    Curl_dyn_free(req);
1377
0
    return CURLE_FAILED_INIT;
1378
0
  }
1379
26
  conn->proto.httpc.binlen = binlen;
1380
1381
26
  result = Curl_base64url_encode((const char *)binsettings, binlen,
1382
26
                                 &base64, &blen);
1383
26
  if(result) {
1384
0
    Curl_dyn_free(req);
1385
0
    return result;
1386
0
  }
1387
1388
26
  result = Curl_dyn_addf(req,
1389
26
                         "Connection: Upgrade, HTTP2-Settings\r\n"
1390
26
                         "Upgrade: %s\r\n"
1391
26
                         "HTTP2-Settings: %s\r\n",
1392
26
                         NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
1393
26
  free(base64);
1394
1395
26
  k->upgr101 = UPGR101_H2;
1396
1397
26
  return result;
1398
26
}
1399
1400
/*
1401
 * Returns nonzero if current HTTP/2 session should be closed.
1402
 */
1403
static int should_close_session(struct http_conn *httpc)
1404
2.48M
{
1405
2.48M
  return httpc->drain_total == 0 && !nghttp2_session_want_read(httpc->h2) &&
1406
2.48M
    !nghttp2_session_want_write(httpc->h2);
1407
2.48M
}
1408
1409
/*
1410
 * h2_process_pending_input() processes pending input left in
1411
 * httpc->inbuf.  Then, call h2_session_send() to send pending data.
1412
 * This function returns 0 if it succeeds, or -1 and error code will
1413
 * be assigned to *err.
1414
 */
1415
static int h2_process_pending_input(struct Curl_easy *data,
1416
                                    struct http_conn *httpc,
1417
                                    CURLcode *err)
1418
15.4k
{
1419
15.4k
  ssize_t nread;
1420
15.4k
  char *inbuf;
1421
15.4k
  ssize_t rv;
1422
1423
15.4k
  nread = httpc->inbuflen - httpc->nread_inbuf;
1424
15.4k
  inbuf = httpc->inbuf + httpc->nread_inbuf;
1425
1426
15.4k
  set_transfer(httpc, data); /* set the transfer */
1427
15.4k
  rv = nghttp2_session_mem_recv(httpc->h2, (const uint8_t *)inbuf, nread);
1428
15.4k
  if(rv < 0) {
1429
6
    failf(data,
1430
6
          "h2_process_pending_input: nghttp2_session_mem_recv() returned "
1431
6
          "%zd:%s", rv, nghttp2_strerror((int)rv));
1432
6
    *err = CURLE_RECV_ERROR;
1433
6
    return -1;
1434
6
  }
1435
1436
15.4k
  if(nread == rv) {
1437
15.4k
    H2BUGF(infof(data,
1438
15.4k
                 "h2_process_pending_input: All data in connection buffer "
1439
15.4k
                 "processed"));
1440
15.4k
    httpc->inbuflen = 0;
1441
15.4k
    httpc->nread_inbuf = 0;
1442
15.4k
  }
1443
10
  else {
1444
10
    httpc->nread_inbuf += rv;
1445
10
    H2BUGF(infof(data,
1446
10
                 "h2_process_pending_input: %zu bytes left in connection "
1447
10
                 "buffer",
1448
10
                 httpc->inbuflen - httpc->nread_inbuf));
1449
10
  }
1450
1451
15.4k
  rv = h2_session_send(data, httpc->h2);
1452
15.4k
  if(rv) {
1453
0
    *err = CURLE_SEND_ERROR;
1454
0
    return -1;
1455
0
  }
1456
1457
15.4k
  if(nghttp2_session_check_request_allowed(httpc->h2) == 0) {
1458
    /* No more requests are allowed in the current session, so
1459
       the connection may not be reused. This is set when a
1460
       GOAWAY frame has been received or when the limit of stream
1461
       identifiers has been reached. */
1462
1.98k
    connclose(data->conn, "http/2: No new requests allowed");
1463
1.98k
  }
1464
1465
15.4k
  if(should_close_session(httpc)) {
1466
1.58k
    struct HTTP *stream = data->req.p.http;
1467
1.58k
    H2BUGF(infof(data,
1468
1.58k
                 "h2_process_pending_input: nothing to do in this session"));
1469
1.58k
    if(stream->error)
1470
85
      *err = CURLE_HTTP2;
1471
1.49k
    else {
1472
      /* not an error per se, but should still close the connection */
1473
1.49k
      connclose(data->conn, "GOAWAY received");
1474
1.49k
      *err = CURLE_OK;
1475
1.49k
    }
1476
1.58k
    return -1;
1477
1.58k
  }
1478
13.8k
  return 0;
1479
15.4k
}
1480
1481
/*
1482
 * Called from transfer.c:done_sending when we stop uploading.
1483
 */
1484
CURLcode Curl_http2_done_sending(struct Curl_easy *data,
1485
                                 struct connectdata *conn)
1486
3.77k
{
1487
3.77k
  CURLcode result = CURLE_OK;
1488
1489
3.77k
  if((conn->handler == &Curl_handler_http2_ssl) ||
1490
3.77k
     (conn->handler == &Curl_handler_http2)) {
1491
    /* make sure this is only attempted for HTTP/2 transfers */
1492
646
    struct HTTP *stream = data->req.p.http;
1493
646
    struct http_conn *httpc = &conn->proto.httpc;
1494
646
    nghttp2_session *h2 = httpc->h2;
1495
1496
646
    if(stream->upload_left) {
1497
      /* If the stream still thinks there's data left to upload. */
1498
1499
203
      stream->upload_left = 0; /* DONE! */
1500
1501
      /* resume sending here to trigger the callback to get called again so
1502
         that it can signal EOF to nghttp2 */
1503
203
      (void)nghttp2_session_resume_data(h2, stream->stream_id);
1504
203
      (void)h2_process_pending_input(data, httpc, &result);
1505
203
    }
1506
1507
    /* If nghttp2 still has pending frames unsent */
1508
646
    if(nghttp2_session_want_write(h2)) {
1509
68
      struct SingleRequest *k = &data->req;
1510
68
      int rv;
1511
1512
68
      H2BUGF(infof(data, "HTTP/2 still wants to send data (easy %p)", data));
1513
1514
      /* and attempt to send the pending frames */
1515
68
      rv = h2_session_send(data, h2);
1516
68
      if(rv)
1517
40
        result = CURLE_SEND_ERROR;
1518
1519
68
      if(nghttp2_session_want_write(h2)) {
1520
         /* re-set KEEP_SEND to make sure we are called again */
1521
40
         k->keepon |= KEEP_SEND;
1522
40
      }
1523
68
    }
1524
646
  }
1525
3.77k
  return result;
1526
3.77k
}
1527
1528
static ssize_t http2_handle_stream_close(struct connectdata *conn,
1529
                                         struct Curl_easy *data,
1530
                                         struct HTTP *stream, CURLcode *err)
1531
1.50k
{
1532
1.50k
  struct http_conn *httpc = &conn->proto.httpc;
1533
1534
1.50k
  if(httpc->pause_stream_id == stream->stream_id) {
1535
1.50k
    httpc->pause_stream_id = 0;
1536
1.50k
  }
1537
1538
1.50k
  drained_transfer(data, httpc);
1539
1540
1.50k
  if(httpc->pause_stream_id == 0) {
1541
1.50k
    if(h2_process_pending_input(data, httpc, err) != 0) {
1542
89
      return -1;
1543
89
    }
1544
1.50k
  }
1545
1546
1.41k
  DEBUGASSERT(data->state.drain == 0);
1547
1548
  /* Reset to FALSE to prevent infinite loop in readwrite_data function. */
1549
1.41k
  stream->closed = FALSE;
1550
1.41k
  if(stream->error == NGHTTP2_REFUSED_STREAM) {
1551
484
    H2BUGF(infof(data, "REFUSED_STREAM (%u), try again on a new connection",
1552
484
                 stream->stream_id));
1553
484
    connclose(conn, "REFUSED_STREAM"); /* don't use this anymore */
1554
484
    data->state.refused_stream = TRUE;
1555
484
    *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
1556
484
    return -1;
1557
484
  }
1558
930
  else if(stream->error != NGHTTP2_NO_ERROR) {
1559
647
    failf(data, "HTTP/2 stream %u was not closed cleanly: %s (err %u)",
1560
647
          stream->stream_id, nghttp2_http2_strerror(stream->error),
1561
647
          stream->error);
1562
647
    *err = CURLE_HTTP2_STREAM;
1563
647
    return -1;
1564
647
  }
1565
1566
283
  if(!stream->bodystarted) {
1567
2
    failf(data, "HTTP/2 stream %u was closed cleanly, but before getting "
1568
2
          " all response header fields, treated as error",
1569
2
          stream->stream_id);
1570
2
    *err = CURLE_HTTP2_STREAM;
1571
2
    return -1;
1572
2
  }
1573
1574
281
  if(Curl_dyn_len(&stream->trailer_recvbuf)) {
1575
4
    char *trailp = Curl_dyn_ptr(&stream->trailer_recvbuf);
1576
4
    char *lf;
1577
1578
14
    do {
1579
14
      size_t len = 0;
1580
14
      CURLcode result;
1581
      /* each trailer line ends with a newline */
1582
14
      lf = strchr(trailp, '\n');
1583
14
      if(!lf)
1584
4
        break;
1585
10
      len = lf + 1 - trailp;
1586
1587
10
      Curl_debug(data, CURLINFO_HEADER_IN, trailp, len);
1588
      /* pass the trailers one by one to the callback */
1589
10
      result = Curl_client_write(data, CLIENTWRITE_HEADER, trailp, len);
1590
10
      if(result) {
1591
0
        *err = result;
1592
0
        return -1;
1593
0
      }
1594
10
      trailp = ++lf;
1595
10
    } while(lf);
1596
4
  }
1597
1598
281
  stream->close_handled = TRUE;
1599
1600
281
  H2BUGF(infof(data, "http2_recv returns 0, http2_handle_stream_close"));
1601
281
  return 0;
1602
281
}
1603
1604
/*
1605
 * h2_pri_spec() fills in the pri_spec struct, used by nghttp2 to send weight
1606
 * and dependency to the peer. It also stores the updated values in the state
1607
 * struct.
1608
 */
1609
1610
static void h2_pri_spec(struct Curl_easy *data,
1611
                        nghttp2_priority_spec *pri_spec)
1612
8.05k
{
1613
8.05k
  struct HTTP *depstream = (data->set.stream_depends_on?
1614
0
                            data->set.stream_depends_on->req.p.http:NULL);
1615
8.05k
  int32_t depstream_id = depstream? depstream->stream_id:0;
1616
8.05k
  nghttp2_priority_spec_init(pri_spec, depstream_id, data->set.stream_weight,
1617
8.05k
                             data->set.stream_depends_e);
1618
8.05k
  data->state.stream_weight = data->set.stream_weight;
1619
8.05k
  data->state.stream_depends_e = data->set.stream_depends_e;
1620
8.05k
  data->state.stream_depends_on = data->set.stream_depends_on;
1621
8.05k
}
1622
1623
/*
1624
 * h2_session_send() checks if there's been an update in the priority /
1625
 * dependency settings and if so it submits a PRIORITY frame with the updated
1626
 * info.
1627
 */
1628
static int h2_session_send(struct Curl_easy *data,
1629
                           nghttp2_session *h2)
1630
2.31M
{
1631
2.31M
  struct HTTP *stream = data->req.p.http;
1632
2.31M
  struct http_conn *httpc = &data->conn->proto.httpc;
1633
2.31M
  set_transfer(httpc, data);
1634
2.31M
  if((data->set.stream_weight != data->state.stream_weight) ||
1635
2.31M
     (data->set.stream_depends_e != data->state.stream_depends_e) ||
1636
2.31M
     (data->set.stream_depends_on != data->state.stream_depends_on) ) {
1637
    /* send new weight and/or dependency */
1638
0
    nghttp2_priority_spec pri_spec;
1639
0
    int rv;
1640
1641
0
    h2_pri_spec(data, &pri_spec);
1642
1643
0
    H2BUGF(infof(data, "Queuing PRIORITY on stream %u (easy %p)",
1644
0
                 stream->stream_id, data));
1645
0
    DEBUGASSERT(stream->stream_id != -1);
1646
0
    rv = nghttp2_submit_priority(h2, NGHTTP2_FLAG_NONE, stream->stream_id,
1647
0
                                 &pri_spec);
1648
0
    if(rv)
1649
0
      return rv;
1650
0
  }
1651
1652
2.31M
  return nghttp2_session_send(h2);
1653
2.31M
}
1654
1655
static ssize_t http2_recv(struct Curl_easy *data, int sockindex,
1656
                          char *mem, size_t len, CURLcode *err)
1657
165k
{
1658
165k
  ssize_t nread;
1659
165k
  struct connectdata *conn = data->conn;
1660
165k
  struct http_conn *httpc = &conn->proto.httpc;
1661
165k
  struct HTTP *stream = data->req.p.http;
1662
1663
165k
  (void)sockindex; /* we always do HTTP2 on sockindex 0 */
1664
1665
165k
  if(should_close_session(httpc)) {
1666
150k
    H2BUGF(infof(data,
1667
150k
                 "http2_recv: nothing to do in this session"));
1668
150k
    if(conn->bits.close) {
1669
      /* already marked for closure, return OK and we're done */
1670
150k
      *err = CURLE_OK;
1671
150k
      return 0;
1672
150k
    }
1673
0
    *err = CURLE_HTTP2;
1674
0
    return -1;
1675
150k
  }
1676
1677
  /* Nullify here because we call nghttp2_session_send() and they
1678
     might refer to the old buffer. */
1679
14.7k
  stream->upload_mem = NULL;
1680
14.7k
  stream->upload_len = 0;
1681
1682
  /*
1683
   * At this point 'stream' is just in the Curl_easy the connection
1684
   * identifies as its owner at this time.
1685
   */
1686
1687
14.7k
  if(stream->bodystarted &&
1688
14.7k
     stream->nread_header_recvbuf < Curl_dyn_len(&stream->header_recvbuf)) {
1689
    /* If there is header data pending for this stream to return, do that */
1690
21
    size_t left =
1691
21
      Curl_dyn_len(&stream->header_recvbuf) - stream->nread_header_recvbuf;
1692
21
    size_t ncopy = CURLMIN(len, left);
1693
21
    memcpy(mem, Curl_dyn_ptr(&stream->header_recvbuf) +
1694
21
           stream->nread_header_recvbuf, ncopy);
1695
21
    stream->nread_header_recvbuf += ncopy;
1696
1697
21
    H2BUGF(infof(data, "http2_recv: Got %d bytes from header_recvbuf",
1698
21
                 (int)ncopy));
1699
21
    return ncopy;
1700
21
  }
1701
1702
14.6k
  H2BUGF(infof(data, "http2_recv: easy %p (stream %u) win %u/%u",
1703
14.6k
               data, stream->stream_id,
1704
14.6k
               nghttp2_session_get_local_window_size(httpc->h2),
1705
14.6k
               nghttp2_session_get_stream_local_window_size(httpc->h2,
1706
14.6k
                                                            stream->stream_id)
1707
14.6k
           ));
1708
1709
14.6k
  if((data->state.drain) && stream->memlen) {
1710
0
    H2BUGF(infof(data, "http2_recv: DRAIN %zu bytes stream %u (%p => %p)",
1711
0
                 stream->memlen, stream->stream_id,
1712
0
                 stream->mem, mem));
1713
0
    if(mem != stream->mem) {
1714
      /* if we didn't get the same buffer this time, we must move the data to
1715
         the beginning */
1716
0
      memmove(mem, stream->mem, stream->memlen);
1717
0
      stream->len = len - stream->memlen;
1718
0
      stream->mem = mem;
1719
0
    }
1720
0
    if(httpc->pause_stream_id == stream->stream_id && !stream->pausedata) {
1721
      /* We have paused nghttp2, but we have no pause data (see
1722
         on_data_chunk_recv). */
1723
0
      httpc->pause_stream_id = 0;
1724
0
      if(h2_process_pending_input(data, httpc, err) != 0) {
1725
0
        return -1;
1726
0
      }
1727
0
    }
1728
0
  }
1729
14.6k
  else if(stream->pausedata) {
1730
14
    DEBUGASSERT(httpc->pause_stream_id == stream->stream_id);
1731
14
    nread = CURLMIN(len, stream->pauselen);
1732
14
    memcpy(mem, stream->pausedata, nread);
1733
1734
14
    stream->pausedata += nread;
1735
14
    stream->pauselen -= nread;
1736
1737
14
    if(stream->pauselen == 0) {
1738
14
      H2BUGF(infof(data, "Unpaused by stream %u", stream->stream_id));
1739
14
      DEBUGASSERT(httpc->pause_stream_id == stream->stream_id);
1740
14
      httpc->pause_stream_id = 0;
1741
1742
14
      stream->pausedata = NULL;
1743
14
      stream->pauselen = 0;
1744
1745
      /* When NGHTTP2_ERR_PAUSE is returned from
1746
         data_source_read_callback, we might not process DATA frame
1747
         fully.  Calling nghttp2_session_mem_recv() again will
1748
         continue to process DATA frame, but if there is no incoming
1749
         frames, then we have to call it again with 0-length data.
1750
         Without this, on_stream_close callback will not be called,
1751
         and stream could be hanged. */
1752
14
      if(h2_process_pending_input(data, httpc, err) != 0) {
1753
0
        return -1;
1754
0
      }
1755
14
    }
1756
14
    H2BUGF(infof(data, "http2_recv: returns unpaused %zd bytes on stream %u",
1757
14
                 nread, stream->stream_id));
1758
14
    return nread;
1759
14
  }
1760
14.6k
  else if(httpc->pause_stream_id) {
1761
    /* If a stream paused nghttp2_session_mem_recv previously, and has
1762
       not processed all data, it still refers to the buffer in
1763
       nghttp2_session.  If we call nghttp2_session_mem_recv(), we may
1764
       overwrite that buffer.  To avoid that situation, just return
1765
       here with CURLE_AGAIN.  This could be busy loop since data in
1766
       socket is not read.  But it seems that usually streams are
1767
       notified with its drain property, and socket is read again
1768
       quickly. */
1769
0
    if(stream->closed)
1770
      /* closed overrides paused */
1771
0
      return 0;
1772
0
    H2BUGF(infof(data, "stream %u is paused, pause id: %u",
1773
0
                 stream->stream_id, httpc->pause_stream_id));
1774
0
    *err = CURLE_AGAIN;
1775
0
    return -1;
1776
0
  }
1777
14.6k
  else {
1778
    /* remember where to store incoming data for this stream and how big the
1779
       buffer is */
1780
14.6k
    stream->mem = mem;
1781
14.6k
    stream->len = len;
1782
14.6k
    stream->memlen = 0;
1783
1784
14.6k
    if(httpc->inbuflen == 0) {
1785
14.6k
      nread = ((Curl_recv *)httpc->recv_underlying)(
1786
14.6k
        data, FIRSTSOCKET, httpc->inbuf, H2_BUFSIZE, err);
1787
1788
14.6k
      if(nread == -1) {
1789
619
        if(*err != CURLE_AGAIN)
1790
0
          failf(data, "Failed receiving HTTP2 data");
1791
619
        else if(stream->closed)
1792
          /* received when the stream was already closed! */
1793
278
          return http2_handle_stream_close(conn, data, stream, err);
1794
1795
341
        return -1;
1796
619
      }
1797
1798
14.0k
      if(nread == 0) {
1799
8.14k
        if(!stream->closed) {
1800
          /* This will happen when the server or proxy server is SIGKILLed
1801
             during data transfer. We should emit an error since our data
1802
             received may be incomplete. */
1803
4.13k
          failf(data, "HTTP/2 stream %u was not closed cleanly before"
1804
4.13k
                " end of the underlying stream",
1805
4.13k
                stream->stream_id);
1806
4.13k
          *err = CURLE_HTTP2_STREAM;
1807
4.13k
          return -1;
1808
4.13k
        }
1809
1810
4.01k
        H2BUGF(infof(data, "end of stream"));
1811
4.01k
        *err = CURLE_OK;
1812
4.01k
        return 0;
1813
8.14k
      }
1814
1815
5.91k
      H2BUGF(infof(data, "nread=%zd", nread));
1816
1817
5.91k
      httpc->inbuflen = nread;
1818
1819
5.91k
      DEBUGASSERT(httpc->nread_inbuf == 0);
1820
5.91k
    }
1821
0
    else {
1822
0
      nread = httpc->inbuflen - httpc->nread_inbuf;
1823
0
      (void)nread;  /* silence warning, used in debug */
1824
0
      H2BUGF(infof(data, "Use data left in connection buffer, nread=%zd",
1825
0
                   nread));
1826
0
    }
1827
1828
5.91k
    if(h2_process_pending_input(data, httpc, err))
1829
1.49k
      return -1;
1830
5.91k
  }
1831
4.41k
  if(stream->memlen) {
1832
1.59k
    ssize_t retlen = stream->memlen;
1833
1.59k
    H2BUGF(infof(data, "http2_recv: returns %zd for stream %u",
1834
1.59k
                 retlen, stream->stream_id));
1835
1.59k
    stream->memlen = 0;
1836
1837
1.59k
    if(httpc->pause_stream_id == stream->stream_id) {
1838
      /* data for this stream is returned now, but this stream caused a pause
1839
         already so we need it called again asap */
1840
709
      H2BUGF(infof(data, "Data returned for PAUSED stream %u",
1841
709
                   stream->stream_id));
1842
709
    }
1843
881
    else if(!stream->closed) {
1844
881
      drained_transfer(data, httpc);
1845
881
    }
1846
0
    else
1847
      /* this stream is closed, trigger a another read ASAP to detect that */
1848
0
      Curl_expire(data, 0, EXPIRE_RUN_NOW);
1849
1850
1.59k
    return retlen;
1851
1.59k
  }
1852
2.82k
  if(stream->closed)
1853
1.21k
    return http2_handle_stream_close(conn, data, stream, err);
1854
1.60k
  *err = CURLE_AGAIN;
1855
1.60k
  H2BUGF(infof(data, "http2_recv returns AGAIN for stream %u",
1856
1.60k
               stream->stream_id));
1857
1.60k
  return -1;
1858
2.82k
}
1859
1860
static ssize_t http2_send(struct Curl_easy *data, int sockindex,
1861
                          const void *mem, size_t len, CURLcode *err)
1862
2.30M
{
1863
  /*
1864
   * Currently, we send request in this function, but this function is also
1865
   * used to send request body. It would be nice to add dedicated function for
1866
   * request.
1867
   */
1868
2.30M
  int rv;
1869
2.30M
  struct connectdata *conn = data->conn;
1870
2.30M
  struct http_conn *httpc = &conn->proto.httpc;
1871
2.30M
  struct HTTP *stream = data->req.p.http;
1872
2.30M
  nghttp2_nv *nva = NULL;
1873
2.30M
  size_t nheader;
1874
2.30M
  nghttp2_data_provider data_prd;
1875
2.30M
  int32_t stream_id;
1876
2.30M
  nghttp2_session *h2 = httpc->h2;
1877
2.30M
  nghttp2_priority_spec pri_spec;
1878
2.30M
  CURLcode result;
1879
2.30M
  struct h2h3req *hreq;
1880
1881
2.30M
  (void)sockindex;
1882
1883
2.30M
  H2BUGF(infof(data, "http2_send len=%zu", len));
1884
1885
2.30M
  if(stream->stream_id != -1) {
1886
2.29M
    if(stream->close_handled) {
1887
5
      infof(data, "stream %u closed", stream->stream_id);
1888
5
      *err = CURLE_HTTP2_STREAM;
1889
5
      return -1;
1890
5
    }
1891
2.29M
    else if(stream->closed) {
1892
9
      return http2_handle_stream_close(conn, data, stream, err);
1893
9
    }
1894
    /* If stream_id != -1, we have dispatched request HEADERS, and now
1895
       are going to send or sending request body in DATA frame */
1896
2.29M
    stream->upload_mem = mem;
1897
2.29M
    stream->upload_len = len;
1898
2.29M
    rv = nghttp2_session_resume_data(h2, stream->stream_id);
1899
2.29M
    if(nghttp2_is_fatal(rv)) {
1900
0
      *err = CURLE_SEND_ERROR;
1901
0
      return -1;
1902
0
    }
1903
2.29M
    rv = h2_session_send(data, h2);
1904
2.29M
    if(nghttp2_is_fatal(rv)) {
1905
0
      *err = CURLE_SEND_ERROR;
1906
0
      return -1;
1907
0
    }
1908
2.29M
    len -= stream->upload_len;
1909
1910
    /* Nullify here because we call nghttp2_session_send() and they
1911
       might refer to the old buffer. */
1912
2.29M
    stream->upload_mem = NULL;
1913
2.29M
    stream->upload_len = 0;
1914
1915
2.29M
    if(should_close_session(httpc)) {
1916
0
      H2BUGF(infof(data, "http2_send: nothing to do in this session"));
1917
0
      *err = CURLE_HTTP2;
1918
0
      return -1;
1919
0
    }
1920
1921
2.29M
    if(stream->upload_left) {
1922
      /* we are sure that we have more data to send here.  Calling the
1923
         following API will make nghttp2_session_want_write() return
1924
         nonzero if remote window allows it, which then libcurl checks
1925
         socket is writable or not.  See http2_perform_getsock(). */
1926
1.39M
      nghttp2_session_resume_data(h2, stream->stream_id);
1927
1.39M
    }
1928
1929
#ifdef DEBUG_HTTP2
1930
    if(!len) {
1931
      infof(data, "http2_send: easy %p (stream %u) win %u/%u",
1932
            data, stream->stream_id,
1933
            nghttp2_session_get_remote_window_size(httpc->h2),
1934
            nghttp2_session_get_stream_remote_window_size(httpc->h2,
1935
                                                          stream->stream_id)
1936
        );
1937
1938
    }
1939
    infof(data, "http2_send returns %zu for stream %u", len,
1940
          stream->stream_id);
1941
#endif
1942
2.29M
    return len;
1943
2.29M
  }
1944
1945
8.09k
  result = Curl_pseudo_headers(data, mem, len, &hreq);
1946
8.09k
  if(result) {
1947
39
    *err = result;
1948
39
    return -1;
1949
39
  }
1950
8.05k
  nheader = hreq->entries;
1951
1952
8.05k
  nva = malloc(sizeof(nghttp2_nv) * nheader);
1953
8.05k
  if(!nva) {
1954
0
    Curl_pseudo_free(hreq);
1955
0
    *err = CURLE_OUT_OF_MEMORY;
1956
0
    return -1;
1957
0
  }
1958
8.05k
  else {
1959
8.05k
    unsigned int i;
1960
262k
    for(i = 0; i < nheader; i++) {
1961
254k
      nva[i].name = (unsigned char *)hreq->header[i].name;
1962
254k
      nva[i].namelen = hreq->header[i].namelen;
1963
254k
      nva[i].value = (unsigned char *)hreq->header[i].value;
1964
254k
      nva[i].valuelen = hreq->header[i].valuelen;
1965
254k
      nva[i].flags = NGHTTP2_NV_FLAG_NONE;
1966
254k
    }
1967
8.05k
    Curl_pseudo_free(hreq);
1968
8.05k
  }
1969
1970
8.05k
  h2_pri_spec(data, &pri_spec);
1971
1972
8.05k
  H2BUGF(infof(data, "http2_send request allowed %d (easy handle %p)",
1973
8.05k
               nghttp2_session_check_request_allowed(h2), (void *)data));
1974
1975
8.05k
  switch(data->state.httpreq) {
1976
179
  case HTTPREQ_POST:
1977
179
  case HTTPREQ_POST_FORM:
1978
1.16k
  case HTTPREQ_POST_MIME:
1979
1.23k
  case HTTPREQ_PUT:
1980
1.23k
    if(data->state.infilesize != -1)
1981
930
      stream->upload_left = data->state.infilesize;
1982
301
    else
1983
      /* data sending without specifying the data amount up front */
1984
301
      stream->upload_left = -1; /* unknown, but not zero */
1985
1986
1.23k
    data_prd.read_callback = data_source_read_callback;
1987
1.23k
    data_prd.source.ptr = NULL;
1988
1.23k
    stream_id = nghttp2_submit_request(h2, &pri_spec, nva, nheader,
1989
1.23k
                                       &data_prd, data);
1990
1.23k
    break;
1991
6.82k
  default:
1992
6.82k
    stream_id = nghttp2_submit_request(h2, &pri_spec, nva, nheader,
1993
6.82k
                                       NULL, data);
1994
8.05k
  }
1995
1996
8.05k
  Curl_safefree(nva);
1997
1998
8.05k
  if(stream_id < 0) {
1999
0
    H2BUGF(infof(data,
2000
0
                 "http2_send() nghttp2_submit_request error (%s)%u",
2001
0
                 nghttp2_strerror(stream_id), stream_id));
2002
0
    *err = CURLE_SEND_ERROR;
2003
0
    return -1;
2004
0
  }
2005
2006
8.05k
  infof(data, "Using Stream ID: %u (easy handle %p)",
2007
8.05k
        stream_id, (void *)data);
2008
8.05k
  stream->stream_id = stream_id;
2009
2010
8.05k
  rv = h2_session_send(data, h2);
2011
8.05k
  if(rv) {
2012
0
    H2BUGF(infof(data,
2013
0
                 "http2_send() nghttp2_session_send error (%s)%d",
2014
0
                 nghttp2_strerror(rv), rv));
2015
2016
0
    *err = CURLE_SEND_ERROR;
2017
0
    return -1;
2018
0
  }
2019
2020
8.05k
  if(should_close_session(httpc)) {
2021
0
    H2BUGF(infof(data, "http2_send: nothing to do in this session"));
2022
0
    *err = CURLE_HTTP2;
2023
0
    return -1;
2024
0
  }
2025
2026
  /* If whole HEADERS frame was sent off to the underlying socket, the nghttp2
2027
     library calls data_source_read_callback. But only it found that no data
2028
     available, so it deferred the DATA transmission. Which means that
2029
     nghttp2_session_want_write() returns 0 on http2_perform_getsock(), which
2030
     results that no writable socket check is performed. To workaround this,
2031
     we issue nghttp2_session_resume_data() here to bring back DATA
2032
     transmission from deferred state. */
2033
8.05k
  nghttp2_session_resume_data(h2, stream->stream_id);
2034
2035
8.05k
  return len;
2036
8.05k
}
2037
2038
CURLcode Curl_http2_setup(struct Curl_easy *data,
2039
                          struct connectdata *conn)
2040
9.02k
{
2041
9.02k
  CURLcode result;
2042
9.02k
  struct http_conn *httpc = &conn->proto.httpc;
2043
9.02k
  struct HTTP *stream = data->req.p.http;
2044
2045
9.02k
  DEBUGASSERT(data->state.buffer);
2046
2047
9.02k
  stream->stream_id = -1;
2048
2049
9.02k
  Curl_dyn_init(&stream->header_recvbuf, DYN_H2_HEADERS);
2050
9.02k
  Curl_dyn_init(&stream->trailer_recvbuf, DYN_H2_TRAILERS);
2051
2052
9.02k
  stream->upload_left = 0;
2053
9.02k
  stream->upload_mem = NULL;
2054
9.02k
  stream->upload_len = 0;
2055
9.02k
  stream->mem = data->state.buffer;
2056
9.02k
  stream->len = data->set.buffer_size;
2057
2058
9.02k
  multi_connchanged(data->multi);
2059
  /* below this point only connection related inits are done, which only needs
2060
     to be done once per connection */
2061
2062
9.02k
  if((conn->handler == &Curl_handler_http2_ssl) ||
2063
9.02k
     (conn->handler == &Curl_handler_http2))
2064
650
    return CURLE_OK; /* already done */
2065
2066
8.37k
  if(conn->handler->flags & PROTOPT_SSL)
2067
0
    conn->handler = &Curl_handler_http2_ssl;
2068
8.37k
  else
2069
8.37k
    conn->handler = &Curl_handler_http2;
2070
2071
8.37k
  result = http2_init(data, conn);
2072
8.37k
  if(result) {
2073
0
    Curl_dyn_free(&stream->header_recvbuf);
2074
0
    return result;
2075
0
  }
2076
2077
8.37k
  infof(data, "Using HTTP2, server supports multiplexing");
2078
2079
8.37k
  conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
2080
8.37k
  conn->httpversion = 20;
2081
8.37k
  conn->bundle->multiuse = BUNDLE_MULTIPLEX;
2082
2083
8.37k
  httpc->inbuflen = 0;
2084
8.37k
  httpc->nread_inbuf = 0;
2085
2086
8.37k
  httpc->pause_stream_id = 0;
2087
8.37k
  httpc->drain_total = 0;
2088
2089
8.37k
  return CURLE_OK;
2090
8.37k
}
2091
2092
CURLcode Curl_http2_switched(struct Curl_easy *data,
2093
                             const char *mem, size_t nread)
2094
7.84k
{
2095
7.84k
  CURLcode result;
2096
7.84k
  struct connectdata *conn = data->conn;
2097
7.84k
  struct http_conn *httpc = &conn->proto.httpc;
2098
7.84k
  int rv;
2099
7.84k
  struct HTTP *stream = data->req.p.http;
2100
2101
7.84k
  result = Curl_http2_setup(data, conn);
2102
7.84k
  if(result)
2103
0
    return result;
2104
2105
7.84k
  httpc->recv_underlying = conn->recv[FIRSTSOCKET];
2106
7.84k
  httpc->send_underlying = conn->send[FIRSTSOCKET];
2107
7.84k
  conn->recv[FIRSTSOCKET] = http2_recv;
2108
7.84k
  conn->send[FIRSTSOCKET] = http2_send;
2109
2110
7.84k
  if(data->req.upgr101 == UPGR101_RECEIVED) {
2111
    /* stream 1 is opened implicitly on upgrade */
2112
3
    stream->stream_id = 1;
2113
    /* queue SETTINGS frame (again) */
2114
3
    rv = nghttp2_session_upgrade2(httpc->h2, httpc->binsettings, httpc->binlen,
2115
3
                                  data->state.httpreq == HTTPREQ_HEAD, NULL);
2116
3
    if(rv) {
2117
0
      failf(data, "nghttp2_session_upgrade2() failed: %s(%d)",
2118
0
            nghttp2_strerror(rv), rv);
2119
0
      return CURLE_HTTP2;
2120
0
    }
2121
2122
3
    rv = nghttp2_session_set_stream_user_data(httpc->h2,
2123
3
                                              stream->stream_id,
2124
3
                                              data);
2125
3
    if(rv) {
2126
0
      infof(data, "http/2: failed to set user_data for stream %u",
2127
0
            stream->stream_id);
2128
0
      DEBUGASSERT(0);
2129
0
    }
2130
3
  }
2131
7.83k
  else {
2132
7.83k
    populate_settings(data, httpc);
2133
2134
    /* stream ID is unknown at this point */
2135
7.83k
    stream->stream_id = -1;
2136
7.83k
    rv = nghttp2_submit_settings(httpc->h2, NGHTTP2_FLAG_NONE,
2137
7.83k
                                 httpc->local_settings,
2138
7.83k
                                 httpc->local_settings_num);
2139
7.83k
    if(rv) {
2140
0
      failf(data, "nghttp2_submit_settings() failed: %s(%d)",
2141
0
            nghttp2_strerror(rv), rv);
2142
0
      return CURLE_HTTP2;
2143
0
    }
2144
7.83k
  }
2145
2146
7.84k
  rv = nghttp2_session_set_local_window_size(httpc->h2, NGHTTP2_FLAG_NONE, 0,
2147
7.84k
                                             HTTP2_HUGE_WINDOW_SIZE);
2148
7.84k
  if(rv) {
2149
0
    failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
2150
0
          nghttp2_strerror(rv), rv);
2151
0
    return CURLE_HTTP2;
2152
0
  }
2153
2154
  /* we are going to copy mem to httpc->inbuf.  This is required since
2155
     mem is part of buffer pointed by stream->mem, and callbacks
2156
     called by nghttp2_session_mem_recv() will write stream specific
2157
     data into stream->mem, overwriting data already there. */
2158
7.84k
  if(H2_BUFSIZE < nread) {
2159
0
    failf(data, "connection buffer size is too small to store data following "
2160
0
          "HTTP Upgrade response header: buflen=%d, datalen=%zu",
2161
0
          H2_BUFSIZE, nread);
2162
0
    return CURLE_HTTP2;
2163
0
  }
2164
2165
7.84k
  infof(data, "Copying HTTP/2 data in stream buffer to connection buffer"
2166
7.84k
        " after upgrade: len=%zu",
2167
7.84k
        nread);
2168
2169
7.84k
  if(nread)
2170
3
    memcpy(httpc->inbuf, mem, nread);
2171
2172
7.84k
  httpc->inbuflen = nread;
2173
2174
7.84k
  DEBUGASSERT(httpc->nread_inbuf == 0);
2175
2176
7.84k
  if(-1 == h2_process_pending_input(data, httpc, &result))
2177
2
    return CURLE_HTTP2;
2178
2179
7.83k
  return CURLE_OK;
2180
7.84k
}
2181
2182
CURLcode Curl_http2_stream_pause(struct Curl_easy *data, bool pause)
2183
0
{
2184
0
  DEBUGASSERT(data);
2185
0
  DEBUGASSERT(data->conn);
2186
  /* if it isn't HTTP/2, we're done */
2187
0
  if(!(data->conn->handler->protocol & PROTO_FAMILY_HTTP) ||
2188
0
     !data->conn->proto.httpc.h2)
2189
0
    return CURLE_OK;
2190
0
#ifdef NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE
2191
0
  else {
2192
0
    struct HTTP *stream = data->req.p.http;
2193
0
    struct http_conn *httpc = &data->conn->proto.httpc;
2194
0
    uint32_t window = !pause * HTTP2_HUGE_WINDOW_SIZE;
2195
0
    int rv = nghttp2_session_set_local_window_size(httpc->h2,
2196
0
                                                   NGHTTP2_FLAG_NONE,
2197
0
                                                   stream->stream_id,
2198
0
                                                   window);
2199
0
    if(rv) {
2200
0
      failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
2201
0
            nghttp2_strerror(rv), rv);
2202
0
      return CURLE_HTTP2;
2203
0
    }
2204
2205
    /* make sure the window update gets sent */
2206
0
    rv = h2_session_send(data, httpc->h2);
2207
0
    if(rv)
2208
0
      return CURLE_SEND_ERROR;
2209
2210
0
    DEBUGF(infof(data, "Set HTTP/2 window size to %u for stream %u",
2211
0
                 window, stream->stream_id));
2212
2213
0
#ifdef DEBUGBUILD
2214
0
    {
2215
      /* read out the stream local window again */
2216
0
      uint32_t window2 =
2217
0
        nghttp2_session_get_stream_local_window_size(httpc->h2,
2218
0
                                                     stream->stream_id);
2219
0
      DEBUGF(infof(data, "HTTP/2 window size is now %u for stream %u",
2220
0
                   window2, stream->stream_id));
2221
0
    }
2222
0
#endif
2223
0
  }
2224
0
#endif
2225
0
  return CURLE_OK;
2226
0
}
2227
2228
CURLcode Curl_http2_add_child(struct Curl_easy *parent,
2229
                              struct Curl_easy *child,
2230
                              bool exclusive)
2231
0
{
2232
0
  if(parent) {
2233
0
    struct Curl_http2_dep **tail;
2234
0
    struct Curl_http2_dep *dep = calloc(1, sizeof(struct Curl_http2_dep));
2235
0
    if(!dep)
2236
0
      return CURLE_OUT_OF_MEMORY;
2237
0
    dep->data = child;
2238
2239
0
    if(parent->set.stream_dependents && exclusive) {
2240
0
      struct Curl_http2_dep *node = parent->set.stream_dependents;
2241
0
      while(node) {
2242
0
        node->data->set.stream_depends_on = child;
2243
0
        node = node->next;
2244
0
      }
2245
2246
0
      tail = &child->set.stream_dependents;
2247
0
      while(*tail)
2248
0
        tail = &(*tail)->next;
2249
2250
0
      DEBUGASSERT(!*tail);
2251
0
      *tail = parent->set.stream_dependents;
2252
0
      parent->set.stream_dependents = 0;
2253
0
    }
2254
2255
0
    tail = &parent->set.stream_dependents;
2256
0
    while(*tail) {
2257
0
      (*tail)->data->set.stream_depends_e = FALSE;
2258
0
      tail = &(*tail)->next;
2259
0
    }
2260
2261
0
    DEBUGASSERT(!*tail);
2262
0
    *tail = dep;
2263
0
  }
2264
2265
0
  child->set.stream_depends_on = parent;
2266
0
  child->set.stream_depends_e = exclusive;
2267
0
  return CURLE_OK;
2268
0
}
2269
2270
void Curl_http2_remove_child(struct Curl_easy *parent, struct Curl_easy *child)
2271
0
{
2272
0
  struct Curl_http2_dep *last = 0;
2273
0
  struct Curl_http2_dep *data = parent->set.stream_dependents;
2274
0
  DEBUGASSERT(child->set.stream_depends_on == parent);
2275
2276
0
  while(data && data->data != child) {
2277
0
    last = data;
2278
0
    data = data->next;
2279
0
  }
2280
2281
0
  DEBUGASSERT(data);
2282
2283
0
  if(data) {
2284
0
    if(last) {
2285
0
      last->next = data->next;
2286
0
    }
2287
0
    else {
2288
0
      parent->set.stream_dependents = data->next;
2289
0
    }
2290
0
    free(data);
2291
0
  }
2292
2293
0
  child->set.stream_depends_on = 0;
2294
0
  child->set.stream_depends_e = FALSE;
2295
0
}
2296
2297
void Curl_http2_cleanup_dependencies(struct Curl_easy *data)
2298
194k
{
2299
194k
  while(data->set.stream_dependents) {
2300
0
    struct Curl_easy *tmp = data->set.stream_dependents->data;
2301
0
    Curl_http2_remove_child(data, tmp);
2302
0
    if(data->set.stream_depends_on)
2303
0
      Curl_http2_add_child(data->set.stream_depends_on, tmp, FALSE);
2304
0
  }
2305
2306
194k
  if(data->set.stream_depends_on)
2307
0
    Curl_http2_remove_child(data->set.stream_depends_on, data);
2308
194k
}
2309
2310
/* Only call this function for a transfer that already got a HTTP/2
2311
   CURLE_HTTP2_STREAM error! */
2312
bool Curl_h2_http_1_1_error(struct Curl_easy *data)
2313
4.78k
{
2314
4.78k
  struct HTTP *stream = data->req.p.http;
2315
4.78k
  return (stream->error == NGHTTP2_HTTP_1_1_REQUIRED);
2316
4.78k
}
2317
2318
#else /* !USE_NGHTTP2 */
2319
2320
/* Satisfy external references even if http2 is not compiled in. */
2321
#include <curl/curl.h>
2322
2323
char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
2324
{
2325
  (void) h;
2326
  (void) num;
2327
  return NULL;
2328
}
2329
2330
char *curl_pushheader_byname(struct curl_pushheaders *h, const char *header)
2331
{
2332
  (void) h;
2333
  (void) header;
2334
  return NULL;
2335
}
2336
2337
#endif /* USE_NGHTTP2 */