Coverage Report

Created: 2026-12-01 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/http2.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 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
#include "curl_setup.h"
25
26
#if !defined(CURL_DISABLE_HTTP) && defined(USE_NGHTTP2)
27
#include <nghttp2/nghttp2.h>
28
29
#include "urldata.h"
30
#include "bufq.h"
31
#include "u32_ptrset.h"
32
#include "http1.h"
33
#include "http2.h"
34
#include "http.h"
35
#include "sendf.h"
36
#include "curl_trc.h"
37
#include "select.h"
38
#include "curlx/base64.h"
39
#include "multiif.h"
40
#include "ngh2_shim.h"
41
#include "progress.h"
42
#include "url.h"
43
#include "urlapi-int.h"
44
#include "cfilters.h"
45
#include "connect.h"
46
#include "transfer.h"
47
#include "bufref.h"
48
#include "curlx/dynbuf.h"
49
#include "headers.h"
50
#include "curl_share.h"
51
52
#if NGHTTP2_VERSION_NUM < 0x010f00
53
#error "nghttp2 1.15.0 or greater required"
54
#endif
55
56
#define NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE 1
57
58
/* buffer dimensioning:
59
 * use 16K as chunk size, as that fits H2 DATA frames well */
60
45.7k
#define H2_CHUNK_SIZE           (16 * 1024)
61
/* connection window size */
62
22.8k
#define H2_CONN_WINDOW_SIZE     (10 * 1024 * 1024)
63
/* on receiving from TLS, we prep for holding a full stream window */
64
11.4k
#define H2_NW_RECV_CHUNKS       (H2_CONN_WINDOW_SIZE / H2_CHUNK_SIZE)
65
/* on send into TLS, we want to accumulate small frames */
66
11.4k
#define H2_NW_SEND_CHUNKS       1
67
/* this is how much we want "in flight" for a stream, unthrottled */
68
150k
#define H2_STREAM_WINDOW_SIZE_MAX   (10 * 1024 * 1024)
69
/* this is how much we want "in flight" for a stream, initially, IFF
70
 * nghttp2 allows us to tweak the local window size. */
71
#if NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE
72
35.4k
#define H2_STREAM_WINDOW_SIZE_INITIAL  (64 * 1024)
73
#else
74
#define H2_STREAM_WINDOW_SIZE_INITIAL H2_STREAM_WINDOW_SIZE_MAX
75
#endif
76
/* keep smaller stream upload buffer (default h2 window size) to have
77
 * our progress bars and "upload done" reporting closer to reality */
78
11.4k
#define H2_STREAM_SEND_CHUNKS   ((64 * 1024) / H2_CHUNK_SIZE)
79
/* spare chunks we keep for a full window */
80
11.4k
#define H2_STREAM_POOL_SPARES   (H2_CONN_WINDOW_SIZE / H2_CHUNK_SIZE)
81
82
/* We need to accommodate the max number of streams with their window sizes on
83
 * the overall connection. Streams might become PAUSED which will block their
84
 * received QUOTA in the connection window. If we run out of space, the server
85
 * is blocked from sending us any data. See #10988 for an issue with this. */
86
11.3k
#define HTTP2_HUGE_WINDOW_SIZE (100 * H2_STREAM_WINDOW_SIZE_MAX)
87
88
#define H2_SETTINGS_IV_LEN 3
89
1.24k
#define H2_BINSETTINGS_LEN 80
90
91
struct cf_h2_ctx {
92
  struct curl_ngh2 ngh2;
93
  /* The easy handle used in the current filter call, cleared at return */
94
  struct cf_call_data call_data;
95
96
  struct bufq inbufq;           /* network input */
97
  struct bufq outbufq;          /* network output */
98
  struct bufc_pool stream_bufcp; /* spares for stream buffers */
99
  struct dynbuf scratch;        /* scratch buffer for temp use */
100
101
  struct u32_ptrset streams; /* hash of `data->mid` to `h2_stream_ctx` */
102
  size_t drain_total; /* sum of all stream's UrlState drain */
103
  uint32_t initial_win_size; /* current initial window size (settings) */
104
  uint32_t max_concurrent_streams;
105
  uint32_t goaway_error;        /* goaway error code from server */
106
  int32_t remote_max_sid;       /* max id processed by server */
107
  int32_t local_max_sid;        /* max id processed by us */
108
  BIT(initialized);
109
  BIT(via_h1_upgrade);
110
  BIT(conn_closed);
111
  BIT(rcvd_goaway);
112
  BIT(sent_goaway);
113
  BIT(enable_push);
114
  BIT(nw_out_blocked);
115
};
116
117
/* How to access `call_data` from a cf_h2 filter */
118
#undef CF_CTX_CALL_DATA
119
890k
#define CF_CTX_CALL_DATA(cf) ((struct cf_h2_ctx *)(cf)->ctx)->call_data
120
121
/**
122
 * All about the H2 internals of a stream
123
 */
124
struct h2_stream_ctx {
125
  struct bufq sendbuf; /* request buffer */
126
  struct h1_req_parser h1; /* parsing the request */
127
  struct dynhds resp_trailers; /* response trailer fields */
128
  size_t resp_hds_len; /* amount of response header bytes in recvbuf */
129
  curl_off_t xfer_id;  /* transfer `id` owning this stream */
130
  curl_off_t nrcvd_data;  /* number of DATA bytes received */
131
132
  char **push_headers;       /* allocated array */
133
  size_t push_headers_used;  /* number of entries filled in */
134
  size_t push_headers_alloc; /* number of entries allocated */
135
136
  int32_t id; /* HTTP/2 protocol identifier for stream */
137
  uint32_t mid; /* transfer `mid` owning this stream */
138
  uint32_t error; /* stream error code */
139
  CURLcode xfer_result; /* Result of writing out response */
140
  int32_t local_window_size; /* the local recv window size */
141
  int32_t status_code; /* HTTP response status code */
142
  BIT(resp_hds_complete); /* we have a complete, final response */
143
  BIT(closed); /* TRUE on stream close */
144
  BIT(reset);  /* TRUE on stream reset */
145
  BIT(reset_by_server);  /* TRUE on stream reset by server */
146
  BIT(close_handled); /* TRUE if stream closure is handled by libcurl */
147
  BIT(bodystarted);
148
  BIT(body_eos);    /* the complete body has been added to `sendbuf` and
149
                     * is being/has been processed from there. */
150
  BIT(write_paused);  /* stream write is paused */
151
};
152
153
static void free_push_headers(struct h2_stream_ctx *stream)
154
11.4k
{
155
11.4k
  size_t i;
156
11.4k
  for(i = 0; i < stream->push_headers_used; i++)
157
0
    curlx_free(stream->push_headers[i]);
158
11.4k
  curlx_safefree(stream->push_headers);
159
11.4k
  stream->push_headers_used = 0;
160
11.4k
}
161
162
static void h2_stream_ctx_free(struct h2_stream_ctx *stream)
163
11.4k
{
164
11.4k
  Curl_bufq_free(&stream->sendbuf);
165
11.4k
  Curl_h1_req_parse_free(&stream->h1);
166
11.4k
  Curl_dynhds_cleanup(&stream->resp_trailers);
167
11.4k
  free_push_headers(stream);
168
11.4k
  curlx_free(stream);
169
11.4k
}
170
171
static void h2_stream_hash_free(uint32_t id, void *stream)
172
11.4k
{
173
11.4k
  (void)id;
174
11.4k
  DEBUGASSERT(stream);
175
11.4k
  h2_stream_ctx_free((struct h2_stream_ctx *)stream);
176
11.4k
}
177
178
static void cf_h2_ctx_init(struct cf_h2_ctx *ctx, bool via_h1_upgrade)
179
11.4k
{
180
11.4k
  Curl_bufcp_init(&ctx->stream_bufcp, H2_CHUNK_SIZE, H2_STREAM_POOL_SPARES);
181
11.4k
  Curl_bufq_initp(&ctx->inbufq, &ctx->stream_bufcp, H2_NW_RECV_CHUNKS, 0);
182
11.4k
  Curl_bufq_initp(&ctx->outbufq, &ctx->stream_bufcp, H2_NW_SEND_CHUNKS, 0);
183
11.4k
  curlx_dyn_init(&ctx->scratch, CURL_MAX_HTTP_HEADER);
184
11.4k
  Curl_u32_ptrset_init(&ctx->streams, h2_stream_hash_free);
185
11.4k
  ctx->remote_max_sid = INT32_MAX;
186
11.4k
  ctx->via_h1_upgrade = via_h1_upgrade;
187
11.4k
  ctx->initialized = TRUE;
188
11.4k
}
189
190
static void cf_h2_ctx_free(struct cf_h2_ctx *ctx)
191
11.4k
{
192
11.4k
  if(ctx && ctx->initialized) {
193
11.4k
    Curl_ngh2_del(&ctx->ngh2);
194
11.4k
    Curl_bufq_free(&ctx->inbufq);
195
11.4k
    Curl_bufq_free(&ctx->outbufq);
196
11.4k
    Curl_bufcp_free(&ctx->stream_bufcp);
197
11.4k
    curlx_dyn_free(&ctx->scratch);
198
11.4k
    Curl_u32_ptrset_clear(&ctx->streams);
199
11.4k
    memset(ctx, 0, sizeof(*ctx));
200
11.4k
  }
201
11.4k
  curlx_free(ctx);
202
11.4k
}
203
204
static uint32_t cf_h2_initial_win_size(struct Curl_easy *data)
205
23.9k
{
206
23.9k
#if NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE
207
  /* If the transfer has a rate-limit lower than the default initial
208
   * stream window size, use that. It needs to be at least 8k or servers
209
   * may be unhappy. */
210
23.9k
  curl_off_t rps = Curl_rlimit_per_step(&data->progress.dl.rlimit);
211
23.9k
  if((rps > 0) && (rps < H2_STREAM_WINDOW_SIZE_INITIAL))
212
0
    return CURLMAX((uint32_t)rps, 8192);
213
23.9k
#endif
214
23.9k
  return H2_STREAM_WINDOW_SIZE_INITIAL;
215
23.9k
}
216
217
static size_t populate_settings(nghttp2_settings_entry *iv,
218
                                struct Curl_easy *data,
219
                                struct cf_h2_ctx *ctx)
220
12.6k
{
221
12.6k
  iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
222
12.6k
  iv[0].value = Curl_multi_max_concurrent_streams(data->multi);
223
224
12.6k
  iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
225
12.6k
  iv[1].value = cf_h2_initial_win_size(data);
226
12.6k
  if(ctx)
227
11.3k
    ctx->initial_win_size = iv[1].value;
228
12.6k
  iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
229
12.6k
  iv[2].value = !!data->multi->push_cb;
230
231
12.6k
  return 3;
232
12.6k
}
233
234
static ssize_t populate_binsettings(uint8_t *binsettings,
235
                                    struct Curl_easy *data)
236
1.24k
{
237
1.24k
  nghttp2_settings_entry iv[H2_SETTINGS_IV_LEN];
238
1.24k
  size_t ivlen;
239
240
1.24k
  ivlen = populate_settings(iv, data, NULL);
241
  /* this returns number of bytes it wrote or a negative number on error. */
242
1.24k
  return nghttp2_pack_settings_payload(binsettings, H2_BINSETTINGS_LEN,
243
1.24k
                                       iv, ivlen);
244
1.24k
}
245
246
static CURLcode cf_h2_update_settings(struct cf_h2_ctx *ctx,
247
                                      uint32_t initial_win_size)
248
0
{
249
0
  nghttp2_settings_entry entry;
250
0
  entry.settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
251
0
  entry.value = initial_win_size;
252
0
  if(Curl_ngh2_submit_settings(&ctx->ngh2, NGHTTP2_FLAG_NONE, &entry, 1))
253
0
    return CURLE_SEND_ERROR;
254
0
  ctx->initial_win_size = initial_win_size;
255
0
  return CURLE_OK;
256
0
}
257
258
#define H2_STREAM_CTX(ctx, data)                                         \
259
209k
  ((struct h2_stream_ctx *)(                                             \
260
209k
    (data) ? Curl_u32_ptrset_get(&(ctx)->streams, (data)->mid) : NULL))
261
262
static struct h2_stream_ctx *h2_stream_ctx_create(struct cf_h2_ctx *ctx,
263
                                                  struct Curl_easy *data)
264
11.4k
{
265
11.4k
  struct h2_stream_ctx *stream;
266
267
11.4k
  stream = curlx_calloc(1, sizeof(*stream));
268
11.4k
  if(!stream)
269
1
    return NULL;
270
271
11.4k
  stream->id = -1;
272
11.4k
  stream->xfer_id = data->id;
273
11.4k
  stream->mid = data->mid;
274
11.4k
  Curl_bufq_initp(&stream->sendbuf, &ctx->stream_bufcp,
275
11.4k
                  H2_STREAM_SEND_CHUNKS, BUFQ_OPT_NONE);
276
11.4k
  Curl_h1_req_parse_init(&stream->h1, H1_PARSE_DEFAULT_MAX_LINE_LEN);
277
11.4k
  Curl_dynhds_init(&stream->resp_trailers,
278
11.4k
                   MAX_HTTP_RESP_HEADER_COUNT, DYN_HTTP_REQUEST);
279
11.4k
  stream->bodystarted = FALSE;
280
11.4k
  stream->status_code = -1;
281
11.4k
  stream->closed = FALSE;
282
11.4k
  stream->close_handled = FALSE;
283
11.4k
  stream->error = NGHTTP2_NO_ERROR;
284
11.4k
  stream->local_window_size = H2_STREAM_WINDOW_SIZE_INITIAL;
285
11.4k
  stream->nrcvd_data = 0;
286
11.4k
  return stream;
287
11.4k
}
288
289
static struct Curl_easy *cf_h2_get_stream_xfer(struct Curl_cfilter *cf,
290
                                               int32_t stream_id,
291
                                               struct h2_stream_ctx **pstream)
292
78.7k
{
293
78.7k
  struct cf_h2_ctx *ctx = cf->ctx;
294
78.7k
  struct h2_stream_ctx *stream =
295
78.7k
    Curl_ngh2_session_get_stream_user_data(&ctx->ngh2, stream_id);
296
78.7k
  if(pstream)
297
78.7k
    *pstream = stream;
298
78.7k
  if(stream && (stream->mid != UINT32_MAX)) {
299
73.6k
    struct Curl_easy *data, *call_data = CF_DATA_CURRENT(cf);
300
73.6k
    if(!call_data || !call_data->multi) {
301
0
      DEBUGASSERT(0);
302
0
      return NULL;
303
0
    }
304
73.6k
    data = Curl_multi_get_easy(call_data->multi, stream->mid);
305
73.6k
    if(data && (data->id == stream->xfer_id))
306
73.6k
      return data;
307
73.6k
  }
308
5.09k
  return NULL;
309
78.7k
}
310
311
#ifdef NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE
312
static int32_t cf_h2_get_desired_local_win(struct Curl_cfilter *cf,
313
                                           struct Curl_easy *data)
314
139k
{
315
139k
  curl_off_t avail = Curl_rlimit_avail(&data->progress.dl.rlimit, NULL);
316
317
139k
  (void)cf;
318
139k
  if(avail < CURL_OFF_T_MAX) { /* limit in place */
319
0
    if(avail <= 0)
320
0
      return 0;
321
0
    else if(avail < H2_STREAM_WINDOW_SIZE_MAX)
322
0
      return (int32_t)avail;
323
0
  }
324
139k
  return H2_STREAM_WINDOW_SIZE_MAX;
325
139k
}
326
327
static CURLcode cf_h2_update_local_win(struct Curl_cfilter *cf,
328
                                       struct Curl_easy *data,
329
                                       struct h2_stream_ctx *stream)
330
139k
{
331
139k
  struct cf_h2_ctx *ctx = cf->ctx;
332
139k
  int32_t dwsize;
333
139k
  int rv;
334
335
139k
  dwsize = (stream->write_paused || stream->xfer_result) ?
336
139k
           0 : cf_h2_get_desired_local_win(cf, data);
337
139k
  if(dwsize != stream->local_window_size) {
338
11.2k
    int32_t wsize = Curl_ngh2_session_get_stream_effective_local_window_size(
339
11.2k
      &ctx->ngh2, stream->id);
340
11.2k
    if(dwsize > wsize) {
341
11.2k
      rv = Curl_ngh2_session_set_local_window_size(
342
11.2k
        &ctx->ngh2, NGHTTP2_FLAG_NONE, stream->id, dwsize);
343
11.2k
      if(rv) {
344
14
        failf(data, "[%d] nghttp2 set_local_window_size(%d) failed: "
345
14
              "%s(%d)", stream->id, dwsize, nghttp2_strerror(rv), rv);
346
14
        return CURLE_HTTP2;
347
14
      }
348
11.2k
      rv = Curl_ngh2_submit_window_update(
349
11.2k
        &ctx->ngh2, NGHTTP2_FLAG_NONE, stream->id, dwsize - wsize);
350
11.2k
      if(rv) {
351
6
        failf(data, "[%d] nghttp2_submit_window_update() failed: "
352
6
              "%s(%d)", stream->id, nghttp2_strerror(rv), rv);
353
6
        return CURLE_HTTP2;
354
6
      }
355
11.2k
      stream->local_window_size = dwsize;
356
11.2k
      CURL_TRC_CF(data, cf, "[%d] local window update by %d",
357
11.2k
                  stream->id, dwsize - wsize);
358
11.2k
    }
359
0
    else {
360
0
      rv = Curl_ngh2_session_set_local_window_size(
361
0
        &ctx->ngh2, NGHTTP2_FLAG_NONE, stream->id, dwsize);
362
0
      if(rv) {
363
0
        failf(data, "[%d] nghttp2_session_set_local_window_size() failed: "
364
0
              "%s(%d)", stream->id, nghttp2_strerror(rv), rv);
365
0
        return CURLE_HTTP2;
366
0
      }
367
0
      stream->local_window_size = dwsize;
368
0
      CURL_TRC_CF(data, cf, "[%d] local window size now %d",
369
0
                  stream->id, dwsize);
370
0
    }
371
11.2k
  }
372
139k
  return CURLE_OK;
373
139k
}
374
375
#else /* NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE */
376
377
static CURLcode cf_h2_update_local_win(struct Curl_cfilter *cf,
378
                                       struct Curl_easy *data,
379
                                       struct h2_stream_ctx *stream)
380
{
381
  (void)cf;
382
  (void)data;
383
  (void)stream;
384
  return CURLE_OK;
385
}
386
#endif /* !NGHTTP2_HAS_SET_LOCAL_WINDOW_SIZE */
387
388
static CURLcode http2_data_setup(struct Curl_cfilter *cf,
389
                                 struct Curl_easy *data,
390
                                 struct h2_stream_ctx **pstream)
391
11.5k
{
392
11.5k
  struct cf_h2_ctx *ctx = cf->ctx;
393
11.5k
  struct h2_stream_ctx *stream;
394
395
11.5k
  DEBUGASSERT(data);
396
397
11.5k
  if(!data)
398
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
399
400
11.5k
  stream = H2_STREAM_CTX(ctx, data);
401
11.5k
  if(stream) {
402
126
    *pstream = stream;
403
126
    return CURLE_OK;
404
126
  }
405
406
11.4k
  stream = h2_stream_ctx_create(ctx, data);
407
11.4k
  if(!stream)
408
1
    return CURLE_OUT_OF_MEMORY;
409
410
11.4k
  if(Curl_u32_ptrset_set(&ctx->streams, data->mid, stream)) {
411
4
    h2_stream_ctx_free(stream);
412
4
    return CURLE_OUT_OF_MEMORY;
413
4
  }
414
415
11.4k
  *pstream = stream;
416
11.4k
  return CURLE_OK;
417
11.4k
}
418
419
static CURLcode nw_out_flush(struct Curl_cfilter *cf,
420
                             struct Curl_easy *data)
421
81.9k
{
422
81.9k
  struct cf_h2_ctx *ctx = cf->ctx;
423
81.9k
  size_t nwritten;
424
81.9k
  CURLcode result;
425
426
81.9k
  if(Curl_bufq_is_empty(&ctx->outbufq))
427
24.4k
    return CURLE_OK;
428
429
57.4k
  result = Curl_cf_send_bufq(cf->next, data, &ctx->outbufq, NULL, 0,
430
57.4k
                             &nwritten);
431
57.4k
  if(result) {
432
17.7k
    if(result == CURLE_AGAIN) {
433
17.7k
      CURL_TRC_CF(data, cf, "flush nw send buffer(%zu) -> EAGAIN",
434
17.7k
                  Curl_bufq_len(&ctx->outbufq));
435
17.7k
      ctx->nw_out_blocked = 1;
436
17.7k
    }
437
17.7k
    return result;
438
17.7k
  }
439
39.7k
  return Curl_bufq_is_empty(&ctx->outbufq) ? CURLE_OK : CURLE_AGAIN;
440
57.4k
}
441
442
static void http2_data_done(struct Curl_cfilter *cf, struct Curl_easy *data)
443
11.4k
{
444
11.4k
  struct cf_h2_ctx *ctx = cf->ctx;
445
11.4k
  struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
446
447
11.4k
  DEBUGASSERT(ctx);
448
11.4k
  if(!stream || !ctx->initialized)
449
59
    return;
450
451
11.4k
  if(Curl_ngh2_valid(&ctx->ngh2)) {
452
11.2k
    bool flush_egress = FALSE;
453
    /* returns error if stream not known, which is fine here */
454
11.2k
    (void)Curl_ngh2_session_set_stream_user_data(&ctx->ngh2, stream->id, NULL);
455
456
11.2k
    if(!stream->closed && stream->id > 0) {
457
      /* RST_STREAM */
458
6.15k
      CURL_TRC_CF(data, cf, "[%d] premature DATA_DONE, RST stream",
459
6.15k
                  stream->id);
460
6.15k
      stream->closed = TRUE;
461
6.15k
      stream->reset = TRUE;
462
6.15k
      Curl_ngh2_submit_rst_stream(&ctx->ngh2, NGHTTP2_FLAG_NONE,
463
6.15k
                                  stream->id, NGHTTP2_STREAM_CLOSED);
464
6.15k
      flush_egress = TRUE;
465
6.15k
    }
466
467
11.2k
    if(flush_egress) {
468
6.15k
      (void)Curl_ngh2_session_send(&ctx->ngh2);
469
6.15k
      (void)nw_out_flush(cf, data);
470
6.15k
    }
471
11.2k
  }
472
473
11.4k
  Curl_u32_ptrset_unset(&ctx->streams, data->mid);
474
11.4k
}
475
476
static int h2_client_new(struct Curl_cfilter *cf,
477
                         nghttp2_session_callbacks *cbs)
478
11.4k
{
479
11.4k
  struct cf_h2_ctx *ctx = cf->ctx;
480
11.4k
  nghttp2_option *o;
481
11.4k
  nghttp2_mem mem = { NULL, Curl_nghttp2_malloc, Curl_nghttp2_free,
482
11.4k
                      Curl_nghttp2_calloc, Curl_nghttp2_realloc };
483
484
11.4k
  int rc = nghttp2_option_new(&o);
485
11.4k
  if(rc)
486
0
    return rc;
487
  /* We handle window updates ourself to enforce buffer limits */
488
11.4k
  nghttp2_option_set_no_auto_window_update(o, 1);
489
11.4k
#if NGHTTP2_VERSION_NUM >= 0x013200 /* with 1.50.0 */
490
  /* turn off RFC 9113 leading and trailing white spaces validation against
491
     HTTP field value. */
492
11.4k
  nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1);
493
11.4k
#endif
494
11.4k
  rc = Curl_ngh2_session_client_new3(&ctx->ngh2, cbs, cf, o, &mem);
495
11.4k
  nghttp2_option_del(o);
496
11.4k
  return rc;
497
11.4k
}
498
499
/*
500
 * Returns non-zero if current HTTP/2 session should be closed.
501
 */
502
static int should_close_session(struct cf_h2_ctx *ctx)
503
60.8k
{
504
60.8k
  return ctx->drain_total == 0 && !Curl_ngh2_session_want_read(&ctx->ngh2) &&
505
385
    !Curl_ngh2_session_want_write(&ctx->ngh2);
506
60.8k
}
507
508
/*
509
 * Processes pending input left in network input buffer.
510
 * This function returns 0 if it succeeds, or -1 and error code will
511
 * be assigned to *err.
512
 */
513
static CURLcode h2_process_pending_input(struct Curl_cfilter *cf,
514
                                         struct Curl_easy *data)
515
26.8k
{
516
26.8k
  struct cf_h2_ctx *ctx = cf->ctx;
517
26.8k
  const unsigned char *buf;
518
26.8k
  size_t blen, nread;
519
26.8k
  ssize_t rv;
520
521
26.8k
  while(Curl_bufq_peek(&ctx->inbufq, &buf, &blen)) {
522
26.8k
    rv = Curl_ngh2_session_mem_recv(&ctx->ngh2, (const uint8_t *)buf, blen);
523
26.8k
    if(!curlx_sztouz(rv, &nread)) {
524
51
      failf(data, "nghttp2 recv error %zd: %s", rv, nghttp2_strerror((int)rv));
525
51
      return CURLE_RECV_ERROR;
526
51
    }
527
26.7k
    Curl_bufq_skip(&ctx->inbufq, nread);
528
26.7k
    if(Curl_bufq_is_empty(&ctx->inbufq)) {
529
26.7k
      break;
530
26.7k
    }
531
0
    else {
532
0
      CURL_TRC_CF(data, cf, "process_pending_input: %zu bytes left "
533
0
                  "in connection buffer", Curl_bufq_len(&ctx->inbufq));
534
0
    }
535
26.7k
  }
536
537
26.8k
  if(Curl_ngh2_session_check_request_allowed(&ctx->ngh2) == 0) {
538
    /* No more requests are allowed in the current session, so
539
       the connection may not be reused. This is set when a
540
       GOAWAY frame has been received or when the limit of stream
541
       identifiers has been reached. */
542
11.2k
    CURL_TRC_M(data, "http/2: No new requests allowed");
543
11.2k
    connclose(CF_CONN(cf));
544
11.2k
  }
545
546
26.8k
  return CURLE_OK;
547
26.8k
}
548
549
/*
550
 * The server may send us data at any point (e.g. PING frames). Therefore, we
551
 * cannot assume that an HTTP/2 socket is dead because it is readable.
552
 *
553
 * Check the lower filters first and, if successful, peek at the socket
554
 * and distinguish between closed and data.
555
 */
556
static bool http2_connisalive(struct Curl_cfilter *cf, struct Curl_easy *data,
557
                              bool *input_pending)
558
80
{
559
80
  struct cf_h2_ctx *ctx = cf->ctx;
560
80
  bool alive = TRUE;
561
562
80
  *input_pending = FALSE;
563
80
  if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
564
0
    return FALSE;
565
566
80
  if(*input_pending) {
567
    /* This happens before we have sent off a request and the connection is
568
       not in use by any other transfer, there should not be any data here,
569
       only "protocol frames" */
570
47
    CURLcode result;
571
47
    size_t nread;
572
573
47
    *input_pending = FALSE;
574
47
    result = Curl_cf_recv_bufq(cf->next, data, &ctx->inbufq, 0, &nread);
575
47
    CURL_TRC_CF(data, cf, "connisalive, recv pending input -> %d, %zu",
576
47
                (int)result, nread);
577
47
    if(!result) {
578
47
      result = h2_process_pending_input(cf, data);
579
47
      if(result)
580
        /* immediate error, considered dead */
581
0
        alive = FALSE;
582
47
      else {
583
47
        alive = !should_close_session(ctx);
584
47
      }
585
47
    }
586
0
    else if(result != CURLE_AGAIN) {
587
      /* the read failed so let's say this is dead anyway */
588
0
      alive = FALSE;
589
0
    }
590
47
  }
591
592
80
  return alive;
593
80
}
594
595
static CURLcode http2_send_ping(struct Curl_cfilter *cf,
596
                                struct Curl_easy *data)
597
0
{
598
0
  struct cf_h2_ctx *ctx = cf->ctx;
599
0
  int rc;
600
601
0
  rc = Curl_ngh2_submit_ping(&ctx->ngh2, 0, ZERO_NULL);
602
0
  if(rc) {
603
0
    failf(data, "nghttp2_submit_ping() failed: %s(%d)",
604
0
          nghttp2_strerror(rc), rc);
605
0
    return CURLE_HTTP2;
606
0
  }
607
608
0
  rc = Curl_ngh2_session_send(&ctx->ngh2);
609
0
  if(rc) {
610
0
    failf(data, "nghttp2_session_send() failed: %s(%d)",
611
0
          nghttp2_strerror(rc), rc);
612
0
    return CURLE_SEND_ERROR;
613
0
  }
614
0
  return CURLE_OK;
615
0
}
616
617
/*
618
 * Store nghttp2 version info in this buffer.
619
 */
620
void Curl_http2_ver(char *p, size_t len)
621
0
{
622
0
  nghttp2_info *h2 = nghttp2_version(0);
623
0
  (void)curl_msnprintf(p, len, "nghttp2/%s", h2->version_str);
624
0
}
625
626
/*
627
 * The implementation of nghttp2_send_callback type. Here we write |data| with
628
 * size |length| to the network and return the number of bytes actually
629
 * written. See the documentation of nghttp2_send_callback for the details.
630
 */
631
static ssize_t send_callback(nghttp2_session *h2,
632
                             const uint8_t *buf, size_t blen, int flags,
633
                             void *userp)
634
108k
{
635
108k
  struct Curl_cfilter *cf = userp;
636
108k
  struct cf_h2_ctx *ctx = cf->ctx;
637
108k
  struct Curl_easy *data = CF_DATA_CURRENT(cf);
638
108k
  size_t nwritten;
639
108k
  CURLcode result = CURLE_OK;
640
641
108k
  (void)h2;
642
108k
  (void)flags;
643
108k
  DEBUGASSERT(data);
644
645
108k
  if(!cf->connected)
646
34.1k
    result = Curl_bufq_write(&ctx->outbufq, buf, blen, &nwritten);
647
73.8k
  else
648
73.8k
    result = Curl_cf_send_bufq(cf->next, data, &ctx->outbufq, buf, blen,
649
73.8k
                               &nwritten);
650
651
108k
  if(result) {
652
14.3k
    if(result == CURLE_AGAIN) {
653
14.3k
      ctx->nw_out_blocked = 1;
654
14.3k
      return NGHTTP2_ERR_WOULDBLOCK;
655
14.3k
    }
656
17
    failf(data, "Failed sending HTTP2 data");
657
17
    return NGHTTP2_ERR_CALLBACK_FAILURE;
658
14.3k
  }
659
660
93.6k
  if(!nwritten) {
661
0
    ctx->nw_out_blocked = 1;
662
0
    return NGHTTP2_ERR_WOULDBLOCK;
663
0
  }
664
93.6k
  return (nwritten > SSIZE_MAX) ?
665
93.6k
    NGHTTP2_ERR_CALLBACK_FAILURE : (ssize_t)nwritten;
666
93.6k
}
667
668
/* We pass a pointer to this struct in the push callback, but the contents of
669
   the struct are hidden from the user. */
670
struct curl_pushheaders {
671
  struct Curl_easy *data;
672
  struct h2_stream_ctx *stream;
673
  const nghttp2_push_promise *frame;
674
};
675
676
/*
677
 * push header access function. Only to be used from within the push callback
678
 */
679
char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
680
0
{
681
  /* Verify that we got a good easy handle in the push header struct, mostly to
682
     detect rubbish input fast(er). */
683
0
  if(!h || !GOOD_EASY_HANDLE(h->data))
684
0
    return NULL;
685
0
  else {
686
0
    if(h->stream && num < h->stream->push_headers_used)
687
0
      return h->stream->push_headers[num];
688
0
  }
689
0
  return NULL;
690
0
}
691
692
/*
693
 * push header access function. Only to be used from within the push callback
694
 */
695
char *curl_pushheader_byname(struct curl_pushheaders *h, const char *name)
696
0
{
697
0
  struct h2_stream_ctx *stream;
698
0
  size_t len;
699
0
  size_t i;
700
  /* Verify that we got a good easy handle in the push header struct,
701
     mostly to detect rubbish input fast(er). Also empty header name
702
     is rubbish too. We have to allow ":" at the beginning of
703
     the header, but header == ":" must be rejected. If we have ':' in
704
     the middle of header, it could be matched in middle of the value,
705
     this is because we do prefix match.*/
706
0
  if(!h || !GOOD_EASY_HANDLE(h->data) || !name || !name[0] ||
707
0
     !strcmp(name, ":") || strchr(name + 1, ':'))
708
0
    return NULL;
709
710
0
  stream = h->stream;
711
0
  if(!stream)
712
0
    return NULL;
713
714
0
  len = strlen(name);
715
0
  for(i = 0; i < stream->push_headers_used; i++) {
716
0
    if(!strncmp(name, stream->push_headers[i], len)) {
717
      /* sub-match, make sure that it is followed by a colon */
718
0
      if(stream->push_headers[i][len] != ':')
719
0
        continue;
720
0
      return &stream->push_headers[i][len + 1];
721
0
    }
722
0
  }
723
0
  return NULL;
724
0
}
725
726
static struct Curl_easy *h2_duphandle(struct Curl_cfilter *cf,
727
                                      struct Curl_easy *data)
728
0
{
729
0
  struct Curl_easy *second = curl_easy_init();
730
0
  if(second) {
731
0
    struct h2_stream_ctx *second_stream;
732
0
    http2_data_setup(cf, second, &second_stream);
733
0
    second->state.weight = data->state.weight;
734
0
    Curl_peer_link(&second->state.initial_origin, data->state.initial_origin);
735
0
    if(data->share)
736
0
      (void)Curl_share_easy_link(second, data->share);
737
    /* Never retry this request should it fail */
738
0
    second->set.never_retry = TRUE;
739
    /* copy a range of options from the parent handle */
740
0
    second->set.fwrite_func = data->set.fwrite_func;
741
0
    second->set.out = data->set.out;
742
0
    second->set.fwrite_header = data->set.fwrite_header;
743
0
    second->set.writeheader = data->set.writeheader;
744
0
    second->set.fdebug = data->set.fdebug;
745
0
    second->set.debugdata = data->set.debugdata;
746
0
    second->set.fxferinfo = data->set.fxferinfo;
747
0
    second->set.fprogress = data->set.fprogress;
748
0
    second->set.progress_client = data->set.progress_client;
749
0
    second->progress.hide = data->progress.hide;
750
0
    second->progress.callback = data->progress.callback;
751
0
  }
752
0
  return second;
753
0
}
754
755
static int set_transfer_url(struct Curl_easy *newhandle,
756
                            struct curl_pushheaders *hp,
757
                            struct Curl_easy *data)
758
0
{
759
0
  const char *v;
760
0
  CURLUcode uc;
761
0
  char *url = NULL;
762
0
  int rc = 0;
763
0
  CURLU *u = curl_url();
764
765
0
  if(!u)
766
0
    return 5;
767
768
0
  v = curl_pushheader_byname(hp, HTTP_PSEUDO_SCHEME);
769
0
  if(v) {
770
0
    uc = curl_url_set(u, CURLUPART_SCHEME, v, 0);
771
0
    if(uc) {
772
0
      rc = 1;
773
0
      goto fail;
774
0
    }
775
0
  }
776
777
0
  v = curl_pushheader_byname(hp, HTTP_PSEUDO_AUTHORITY);
778
0
  if(v) {
779
0
    uc = Curl_url_set_authority(u, v);
780
0
    if(uc) {
781
0
      rc = 2;
782
0
      goto fail;
783
0
    }
784
0
  }
785
786
0
  v = curl_pushheader_byname(hp, HTTP_PSEUDO_PATH);
787
0
  if(v) {
788
0
    uc = curl_url_set(u, CURLUPART_PATH, v, 0);
789
0
    if(uc) {
790
0
      rc = 3;
791
0
      goto fail;
792
0
    }
793
0
  }
794
795
  /* We can only allow PUSH of resource from the same origin, e.g.
796
   * scheme + hostname + port */
797
0
  if(!Curl_url_same_origin(data->state.uh, u)) {
798
0
    rc = 1;
799
0
    goto fail;
800
0
  }
801
802
0
  uc = curl_url_get(u, CURLUPART_URL, &url, 0);
803
0
  if(uc)
804
0
    rc = 4;
805
0
fail:
806
0
  if(rc) {
807
0
    curl_url_cleanup(u);
808
0
    return rc;
809
0
  }
810
811
0
  DEBUGASSERT(!newhandle->state.uh);
812
0
  newhandle->state.uh = u;
813
0
  Curl_bufref_set(&newhandle->state.url, url, 0, curl_free);
814
0
  return 0;
815
0
}
816
817
static void discard_newhandle(struct Curl_cfilter *cf,
818
                              struct Curl_easy *newhandle)
819
0
{
820
0
  http2_data_done(cf, newhandle);
821
0
  (void)Curl_close(&newhandle);
822
0
}
823
824
static int push_promise(struct Curl_cfilter *cf,
825
                        struct Curl_easy *data,
826
                        const nghttp2_push_promise *frame)
827
0
{
828
0
  struct cf_h2_ctx *ctx = cf->ctx;
829
0
  int rv; /* one of the CURL_PUSH_* defines */
830
831
0
  CURL_TRC_CF(data, cf, "[%d] PUSH_PROMISE received",
832
0
              frame->promised_stream_id);
833
834
  /* push is never accepted on internal handles (like DoH) */
835
0
  if(data->state.internal) {
836
0
    struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
837
0
    if(stream)
838
0
      free_push_headers(stream);
839
0
    return CURL_PUSH_DENY;
840
0
  }
841
0
  if(data->multi->push_cb) {
842
0
    struct h2_stream_ctx *stream;
843
0
    struct h2_stream_ctx *newstream;
844
0
    struct curl_pushheaders heads;
845
0
    CURLMcode mresult;
846
0
    CURLcode result;
847
    /* clone the parent */
848
0
    struct Curl_easy *newhandle = h2_duphandle(cf, data);
849
0
    if(!newhandle) {
850
0
      infof(data, "failed to duplicate handle");
851
0
      rv = CURL_PUSH_DENY; /* FAIL HARD */
852
0
      goto fail;
853
0
    }
854
855
0
    stream = H2_STREAM_CTX(ctx, data);
856
0
    if(!stream) {
857
0
      failf(data, "Internal NULL stream");
858
0
      discard_newhandle(cf, newhandle);
859
0
      rv = CURL_PUSH_DENY;
860
0
      goto fail;
861
0
    }
862
863
0
    heads.data = data;
864
0
    heads.stream = stream;
865
0
    heads.frame = frame;
866
867
0
    rv = set_transfer_url(newhandle, &heads, data);
868
0
    if(rv) {
869
0
      CURL_TRC_CF(data, cf, "[%d] PUSH_PROMISE, failed to set URL -> %d",
870
0
                  frame->promised_stream_id, rv);
871
0
      discard_newhandle(cf, newhandle);
872
0
      rv = CURL_PUSH_DENY;
873
0
      goto fail;
874
0
    }
875
876
0
    {
877
0
      struct Curl_mapi_guard guard;
878
0
      CURL_CBAPI_START(&guard, data, multi_push_cb);
879
0
      rv = data->multi->push_cb(data, newhandle,
880
0
                                stream->push_headers_used, &heads,
881
0
                                data->multi->push_userp);
882
0
      CURL_CBAPI_END(&guard);
883
0
    }
884
885
    /* free the headers again */
886
0
    free_push_headers(stream);
887
888
0
    if(rv) {
889
0
      DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
890
      /* denied, kill off the new handle again */
891
0
      CURL_TRC_CF(data, cf, "[%d] PUSH_PROMISE, denied by application -> %d",
892
0
                  frame->promised_stream_id, rv);
893
0
      discard_newhandle(cf, newhandle);
894
0
      goto fail;
895
0
    }
896
897
    /* approved, add to the multi handle for processing. This
898
     * assigns newhandle->mid. For the new `mid` we assign the
899
     * h2_stream instance and remember the stream_id already known. */
900
0
    mresult = Curl_multi_add_perform(data->multi, newhandle, CF_CONN(cf));
901
0
    if(mresult) {
902
0
      infof(data, "failed to add handle to multi");
903
0
      discard_newhandle(cf, newhandle);
904
0
      rv = CURL_PUSH_DENY;
905
0
      goto fail;
906
0
    }
907
908
0
    result = http2_data_setup(cf, newhandle, &newstream);
909
0
    if(result) {
910
0
      failf(data, "error setting up stream: %d", (int)result);
911
0
      discard_newhandle(cf, newhandle);
912
0
      rv = CURL_PUSH_DENY;
913
0
      goto fail;
914
0
    }
915
916
0
    DEBUGASSERT(newstream);
917
0
    newstream->id = frame->promised_stream_id;
918
0
    newhandle->req.maxdownload = -1;
919
0
    newhandle->req.size = -1;
920
921
0
    CURL_TRC_CF(data, cf, "promise easy handle added to multi, mid=%u",
922
0
                newhandle->mid);
923
0
    rv = Curl_ngh2_session_set_stream_user_data(
924
0
      &ctx->ngh2, newstream->id, newstream);
925
0
    if(rv) {
926
0
      infof(data, "failed to set user_data for stream %d",
927
0
            newstream->id);
928
0
      DEBUGASSERT(0);
929
0
      discard_newhandle(cf, newhandle);
930
0
      rv = CURL_PUSH_DENY;
931
0
      goto fail;
932
0
    }
933
934
    /* success, remember max stream id processed */
935
0
    if(newstream->id > ctx->local_max_sid)
936
0
      ctx->local_max_sid = newstream->id;
937
0
  }
938
0
  else {
939
0
    CURL_TRC_CF(data, cf, "Got PUSH_PROMISE, ignore it");
940
0
    rv = CURL_PUSH_DENY;
941
0
  }
942
0
fail:
943
0
  return rv;
944
0
}
945
946
static void h2_xfer_write_resp_hd(struct Curl_cfilter *cf,
947
                                  struct Curl_easy *data,
948
                                  struct h2_stream_ctx *stream,
949
                                  const char *buf, size_t blen, bool eos)
950
54.7k
{
951
  /* If we already encountered an error, skip further writes */
952
54.7k
  if(!stream->xfer_result) {
953
54.0k
    stream->xfer_result = Curl_xfer_write_resp_hd(data, buf, blen, eos);
954
54.0k
    if(!stream->xfer_result && !eos)
955
53.9k
      stream->xfer_result = cf_h2_update_local_win(cf, data, stream);
956
54.0k
    if(stream->xfer_result)
957
66
      CURL_TRC_CF(data, cf, "[%d] error %d writing %zu bytes of headers",
958
54.0k
                  stream->id, (int)stream->xfer_result, blen);
959
54.0k
  }
960
54.7k
}
961
962
static void h2_xfer_write_resp(struct Curl_cfilter *cf,
963
                               struct Curl_easy *data,
964
                               struct h2_stream_ctx *stream,
965
                               const char *buf, size_t blen, bool eos)
966
206
{
967
  /* If we already encountered an error, skip further writes */
968
206
  if(!stream->xfer_result)
969
206
    stream->xfer_result = Curl_xfer_write_resp(data, buf, blen, eos);
970
  /* If the transfer write is errored, we do not want any more data */
971
206
  if(stream->xfer_result) {
972
5
    struct cf_h2_ctx *ctx = cf->ctx;
973
5
    CURL_TRC_CF(data, cf, "[%d] error %d writing %zu bytes of data, "
974
5
                "RST-ing stream",
975
5
                stream->id, (int)stream->xfer_result, blen);
976
5
    Curl_ngh2_submit_rst_stream(
977
5
      &ctx->ngh2, 0, stream->id, (uint32_t)NGHTTP2_ERR_CALLBACK_FAILURE);
978
5
  }
979
201
  else if(!stream->write_paused && Curl_xfer_write_is_paused(data)) {
980
0
    CURL_TRC_CF(data, cf, "[%d] stream output paused", stream->id);
981
0
    stream->write_paused = TRUE;
982
0
  }
983
201
  else if(stream->write_paused && !Curl_xfer_write_is_paused(data)) {
984
0
    CURL_TRC_CF(data, cf, "[%d] stream output unpaused", stream->id);
985
0
    stream->write_paused = FALSE;
986
0
  }
987
988
206
  if(!stream->xfer_result && !eos)
989
201
    stream->xfer_result = cf_h2_update_local_win(cf, data, stream);
990
206
}
991
992
static CURLcode on_stream_frame(struct Curl_cfilter *cf,
993
                                struct Curl_easy *data,
994
                                struct h2_stream_ctx *stream,
995
                                const nghttp2_frame *frame)
996
301
{
997
301
  struct cf_h2_ctx *ctx = cf->ctx;
998
301
  int32_t stream_id = frame->hd.stream_id;
999
301
  int rv;
1000
1001
301
  if(!stream) {
1002
0
    CURL_TRC_CF(data, cf, "[%d] No stream_ctx set", stream_id);
1003
0
    return CURLE_FAILED_INIT;
1004
0
  }
1005
1006
301
  switch(frame->hd.type) {
1007
18
  case NGHTTP2_DATA:
1008
18
    CURL_TRC_CF(data, cf, "[%d] DATA, window=%d/%d",
1009
18
                stream_id,
1010
18
                Curl_ngh2_session_get_stream_effective_recv_data_length(
1011
18
                  &ctx->ngh2, stream->id),
1012
18
                Curl_ngh2_session_get_stream_effective_local_window_size(
1013
18
                  &ctx->ngh2, stream->id));
1014
    /* If !body started on this stream, then receiving DATA is illegal. */
1015
18
    if(!stream->bodystarted) {
1016
0
      if(Curl_ngh2_is_fatal(&ctx->ngh2,
1017
0
           Curl_ngh2_submit_rst_stream(&ctx->ngh2, NGHTTP2_FLAG_NONE,
1018
0
                                       stream_id, NGHTTP2_PROTOCOL_ERROR))) {
1019
0
        return CURLE_RECV_ERROR;
1020
0
      }
1021
0
    }
1022
18
    break;
1023
192
  case NGHTTP2_HEADERS:
1024
192
    if(stream->bodystarted) {
1025
      /* Only valid HEADERS after body started is trailer HEADERS. We
1026
         buffer them in on_header callback. */
1027
0
      break;
1028
0
    }
1029
1030
    /* nghttp2 guarantees that :status is received, and we store it to
1031
       stream->status_code. Fuzzing has proven this can still be reached
1032
       without status code having been set. */
1033
192
    if(stream->status_code == -1)
1034
0
      return CURLE_RECV_ERROR;
1035
1036
    /* Only final status code signals the end of header */
1037
192
    if(stream->status_code / 100 != 1) {
1038
192
      stream->resp_hds_complete = TRUE;
1039
192
      stream->bodystarted = TRUE;
1040
192
    }
1041
0
    else
1042
0
      stream->status_code = -1;
1043
1044
192
    h2_xfer_write_resp_hd(cf, data, stream, STRCONST("\r\n"),
1045
192
                          (bool)stream->closed);
1046
1047
192
    Curl_multi_mark_dirty(data);
1048
192
    break;
1049
0
  case NGHTTP2_PUSH_PROMISE:
1050
0
    rv = push_promise(cf, data, &frame->push_promise);
1051
0
    if(rv) { /* deny! */
1052
0
      DEBUGASSERT((rv > CURL_PUSH_OK) && (rv <= CURL_PUSH_ERROROUT));
1053
0
      if(Curl_ngh2_is_fatal(&ctx->ngh2,
1054
0
           Curl_ngh2_submit_rst_stream(&ctx->ngh2, NGHTTP2_FLAG_NONE,
1055
0
             frame->push_promise.promised_stream_id, NGHTTP2_CANCEL)))
1056
0
        return CURLE_SEND_ERROR;
1057
0
      else if(rv == CURL_PUSH_ERROROUT) {
1058
        /* fail the parent transfer, not the whole session */
1059
0
        CURL_TRC_CF(data, cf, "[%d] fail in PUSH_PROMISE received",
1060
0
                    stream_id);
1061
0
        if(!stream->xfer_result)
1062
0
          stream->xfer_result = CURLE_RECV_ERROR;
1063
0
        Curl_multi_mark_dirty(data);
1064
0
      }
1065
0
    }
1066
0
    break;
1067
43
  case NGHTTP2_RST_STREAM:
1068
43
    if(frame->rst_stream.error_code)
1069
42
      stream->reset_by_server = TRUE;
1070
43
    Curl_multi_mark_dirty(data);
1071
43
    break;
1072
48
  case NGHTTP2_WINDOW_UPDATE:
1073
48
    if(CURL_REQ_WANT_SEND(data) && Curl_bufq_is_empty(&stream->sendbuf)) {
1074
      /* need more data, force processing of transfer */
1075
16
      Curl_multi_mark_dirty(data);
1076
16
    }
1077
32
    else if(!Curl_bufq_is_empty(&stream->sendbuf)) {
1078
      /* resume the potentially suspended stream */
1079
5
      if(Curl_ngh2_is_fatal(&ctx->ngh2,
1080
5
           Curl_ngh2_session_resume_data(&ctx->ngh2, stream->id)))
1081
0
        return CURLE_SEND_ERROR;
1082
5
    }
1083
48
    break;
1084
48
  default:
1085
0
    break;
1086
301
  }
1087
1088
301
  if(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
1089
130
    if(!stream->closed && !stream->body_eos &&
1090
20
       ((stream->status_code >= 400) || (stream->status_code < 200))) {
1091
      /* The server did not give us a positive response and we are not
1092
       * done uploading the request body. We need to stop doing that and
1093
       * also inform the server that we aborted our side. */
1094
7
      CURL_TRC_CF(data, cf, "[%d] EOS frame with unfinished upload and "
1095
7
                  "HTTP status %d, abort upload by RST",
1096
7
                  stream_id, stream->status_code);
1097
7
      Curl_ngh2_submit_rst_stream(
1098
7
        &ctx->ngh2, NGHTTP2_FLAG_NONE, stream->id, NGHTTP2_STREAM_CLOSED);
1099
7
      stream->closed = TRUE;
1100
7
    }
1101
130
    Curl_multi_mark_dirty(data);
1102
130
  }
1103
301
  return CURLE_OK;
1104
301
}
1105
1106
#ifdef CURLVERBOSE
1107
int Curl_nghttp2_fr_print(const nghttp2_frame *frame, char *buffer,
1108
                          size_t blen)
1109
9.09k
{
1110
9.09k
  switch(frame->hd.type) {
1111
1.15k
  case NGHTTP2_DATA: {
1112
1.15k
    return curl_msnprintf(buffer, blen,
1113
1.15k
                          "FRAME[DATA, len=%d, eos=%d, padlen=%d]",
1114
1.15k
                          (int)frame->hd.length,
1115
1.15k
                          !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM),
1116
1.15k
                          (int)frame->data.padlen);
1117
0
  }
1118
804
  case NGHTTP2_HEADERS: {
1119
804
    return curl_msnprintf(buffer, blen,
1120
804
                          "FRAME[HEADERS, len=%d, hend=%d, eos=%d]",
1121
804
                          (int)frame->hd.length,
1122
804
                          !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS),
1123
804
                          !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM));
1124
0
  }
1125
0
  case NGHTTP2_PRIORITY: {
1126
0
    return curl_msnprintf(buffer, blen,
1127
0
                          "FRAME[PRIORITY, len=%d, flags=%d]",
1128
0
                          (int)frame->hd.length, frame->hd.flags);
1129
0
  }
1130
162
  case NGHTTP2_RST_STREAM: {
1131
162
    return curl_msnprintf(buffer, blen,
1132
162
                          "FRAME[RST_STREAM, len=%d, flags=%d, error=%u]",
1133
162
                          (int)frame->hd.length, frame->hd.flags,
1134
162
                          frame->rst_stream.error_code);
1135
0
  }
1136
4.01k
  case NGHTTP2_SETTINGS: {
1137
4.01k
    if(frame->hd.flags & NGHTTP2_FLAG_ACK) {
1138
1.64k
      return curl_msnprintf(buffer, blen, "FRAME[SETTINGS, ack=1]");
1139
1.64k
    }
1140
2.37k
    return curl_msnprintf(buffer, blen,
1141
2.37k
                          "FRAME[SETTINGS, len=%d]", (int)frame->hd.length);
1142
4.01k
  }
1143
15
  case NGHTTP2_PUSH_PROMISE:
1144
15
    return curl_msnprintf(buffer, blen,
1145
15
                          "FRAME[PUSH_PROMISE, len=%d, hend=%d]",
1146
15
                          (int)frame->hd.length,
1147
15
                          !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS));
1148
37
  case NGHTTP2_PING:
1149
37
    return curl_msnprintf(buffer, blen,
1150
37
                          "FRAME[PING, len=%d, ack=%d]",
1151
37
                          (int)frame->hd.length,
1152
37
                          frame->hd.flags & NGHTTP2_FLAG_ACK);
1153
1.60k
  case NGHTTP2_GOAWAY: {
1154
1.60k
    char scratch[128];
1155
1.60k
    size_t s_len = CURL_ARRAYSIZE(scratch);
1156
1.60k
    size_t len = (frame->goaway.opaque_data_len < s_len) ?
1157
1.59k
      frame->goaway.opaque_data_len : s_len - 1;
1158
1.60k
    if(len)
1159
305
      memcpy(scratch, frame->goaway.opaque_data, len);
1160
1.60k
    scratch[len] = '\0';
1161
1.60k
    return curl_msnprintf(buffer, blen,
1162
1.60k
                          "FRAME[GOAWAY, error=%u, reason='%s', "
1163
1.60k
                          "last_stream=%d]", frame->goaway.error_code,
1164
1.60k
                          scratch, frame->goaway.last_stream_id);
1165
4.01k
  }
1166
1.30k
  case NGHTTP2_WINDOW_UPDATE: {
1167
1.30k
    return curl_msnprintf(buffer, blen,
1168
1.30k
                          "FRAME[WINDOW_UPDATE, incr=%d]",
1169
1.30k
                          frame->window_update.window_size_increment);
1170
4.01k
  }
1171
0
  default:
1172
0
    return curl_msnprintf(buffer, blen, "FRAME[%d, len=%d, flags=%d]",
1173
0
                          frame->hd.type, (int)frame->hd.length,
1174
0
                          frame->hd.flags);
1175
9.09k
  }
1176
9.09k
}
1177
1178
static int on_frame_send(nghttp2_session *session, const nghttp2_frame *frame,
1179
                         void *userp)
1180
80.9k
{
1181
80.9k
  struct Curl_cfilter *cf = userp;
1182
80.9k
  struct cf_h2_ctx *ctx = cf->ctx;
1183
80.9k
  struct Curl_easy *data = CF_DATA_CURRENT(cf);
1184
1185
80.9k
  (void)session;
1186
80.9k
  DEBUGASSERT(data);
1187
80.9k
  if(Curl_trc_cf_is_verbose(cf, data)) {
1188
5.40k
    char buffer[256];
1189
5.40k
    int len;
1190
5.40k
    len = Curl_nghttp2_fr_print(frame, buffer, sizeof(buffer) - 1);
1191
5.40k
    buffer[len] = 0;
1192
5.40k
    CURL_TRC_CF(data, cf, "[%d] -> %s", frame->hd.stream_id, buffer);
1193
5.40k
  }
1194
80.9k
  if((frame->hd.type == NGHTTP2_GOAWAY) && !ctx->sent_goaway) {
1195
    /* A GOAWAY not initiated by us, but by nghttp2 itself on detecting
1196
     * a protocol error on the connection */
1197
2.48k
    failf(data, "nghttp2 shuts down connection with error %u: %s",
1198
2.48k
          frame->goaway.error_code,
1199
2.48k
          nghttp2_http2_strerror(frame->goaway.error_code));
1200
2.48k
  }
1201
80.9k
  return 0;
1202
80.9k
}
1203
#endif /* CURLVERBOSE */
1204
1205
static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
1206
                         void *userp)
1207
24.6k
{
1208
24.6k
  struct Curl_cfilter *cf = userp;
1209
24.6k
  struct cf_h2_ctx *ctx = cf->ctx;
1210
24.6k
  struct Curl_easy *data = CF_DATA_CURRENT(cf), *data_s;
1211
24.6k
  struct h2_stream_ctx *stream;
1212
24.6k
  int32_t stream_id = frame->hd.stream_id;
1213
1214
24.6k
  (void)session;
1215
24.6k
  DEBUGASSERT(data);
1216
24.6k
#ifdef CURLVERBOSE
1217
24.6k
  if(Curl_trc_cf_is_verbose(cf, data)) {
1218
3.38k
    char buffer[256];
1219
3.38k
    int len;
1220
3.38k
    len = Curl_nghttp2_fr_print(frame, buffer, sizeof(buffer) - 1);
1221
3.38k
    buffer[len] = 0;
1222
3.38k
    CURL_TRC_CF(data, cf, "[%d] <- %s", frame->hd.stream_id, buffer);
1223
3.38k
  }
1224
24.6k
#endif /* CURLVERBOSE */
1225
1226
24.6k
  if(!stream_id) {
1227
    /* stream ID zero is for connection-oriented stuff */
1228
24.3k
    DEBUGASSERT(data);
1229
24.3k
    switch(frame->hd.type) {
1230
14.8k
    case NGHTTP2_SETTINGS: {
1231
14.8k
      if(!(frame->hd.flags & NGHTTP2_FLAG_ACK)) {
1232
14.7k
        uint32_t max_conn = ctx->max_concurrent_streams;
1233
14.7k
        ctx->max_concurrent_streams =
1234
14.7k
          Curl_ngh2_session_get_remote_settings(
1235
14.7k
            &ctx->ngh2, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
1236
14.7k
        ctx->enable_push = Curl_ngh2_session_get_remote_settings(
1237
14.7k
          &ctx->ngh2, NGHTTP2_SETTINGS_ENABLE_PUSH) != 0;
1238
14.7k
        CURL_TRC_CF(data, cf, "[0] MAX_CONCURRENT_STREAMS: %u",
1239
14.7k
                    ctx->max_concurrent_streams);
1240
14.7k
        CURL_TRC_CF(data, cf, "[0] ENABLE_PUSH: %s",
1241
14.7k
                    ctx->enable_push ? "TRUE" : "false");
1242
14.7k
        if(data && max_conn != ctx->max_concurrent_streams) {
1243
          /* only signal change if the value actually changed */
1244
8.51k
          CURL_TRC_CF(data, cf, "[0] notify MAX_CONCURRENT_STREAMS: %u",
1245
8.51k
                      ctx->max_concurrent_streams);
1246
8.51k
          Curl_multi_connchanged(data->multi);
1247
8.51k
        }
1248
        /* Since the initial stream window is 64K, a request might be on HOLD,
1249
         * due to exhaustion. The (initial) SETTINGS may announce a much larger
1250
         * window and *assume* that we treat this like a WINDOW_UPDATE. Some
1251
         * servers send an explicit WINDOW_UPDATE, but not all seem to do that.
1252
         * To be safe, we UNHOLD a stream in order not to stall. */
1253
14.7k
        if(CURL_REQ_WANT_SEND(data))
1254
3.11k
          Curl_multi_mark_dirty(data);
1255
14.7k
      }
1256
14.8k
      break;
1257
0
    }
1258
9.36k
    case NGHTTP2_GOAWAY:
1259
9.36k
      ctx->rcvd_goaway = TRUE;
1260
9.36k
      ctx->goaway_error = frame->goaway.error_code;
1261
9.36k
      ctx->remote_max_sid = frame->goaway.last_stream_id;
1262
9.36k
      if(data) {
1263
9.36k
        infof(data, "received GOAWAY, error=%u, last_stream=%d",
1264
9.36k
              ctx->goaway_error, ctx->remote_max_sid);
1265
9.36k
        Curl_multi_connchanged(data->multi);
1266
9.36k
      }
1267
9.36k
      break;
1268
171
    default:
1269
171
      break;
1270
24.3k
    }
1271
24.3k
    return 0;
1272
24.3k
  }
1273
1274
310
  data_s = cf_h2_get_stream_xfer(cf, stream_id, &stream);
1275
310
  if(!data_s) {
1276
9
    CURL_TRC_CF(data, cf, "[%d] No Curl_easy associated", stream_id);
1277
9
    return 0;
1278
9
  }
1279
1280
301
  return on_stream_frame(cf, data_s, stream, frame) ?
1281
301
         NGHTTP2_ERR_CALLBACK_FAILURE : 0;
1282
310
}
1283
1284
static int cf_h2_on_invalid_frame_recv(nghttp2_session *session,
1285
                                       const nghttp2_frame *frame,
1286
                                       int ngerr, void *userp)
1287
931
{
1288
931
  struct Curl_cfilter *cf = userp;
1289
931
  struct cf_h2_ctx *ctx = cf->ctx;
1290
931
  struct Curl_easy *data;
1291
931
  struct h2_stream_ctx *stream;
1292
931
  int32_t stream_id = frame->hd.stream_id;
1293
1294
931
  (void)session;
1295
931
  data = cf_h2_get_stream_xfer(cf, stream_id, &stream);
1296
931
  if(data) {
1297
300
#ifdef CURLVERBOSE
1298
300
    char buffer[256];
1299
300
    int len;
1300
300
    len = Curl_nghttp2_fr_print(frame, buffer, sizeof(buffer) - 1);
1301
300
    buffer[len] = 0;
1302
300
    failf(data, "[HTTP2] [%d] received invalid frame: %s, error %d: %s",
1303
300
          stream_id, buffer, ngerr, nghttp2_strerror(ngerr));
1304
300
#endif /* CURLVERBOSE */
1305
300
    if(stream) {
1306
300
      Curl_ngh2_submit_rst_stream(
1307
300
        &ctx->ngh2, NGHTTP2_FLAG_NONE, stream->id, NGHTTP2_STREAM_CLOSED);
1308
300
      stream->error = ngerr;
1309
300
      stream->closed = TRUE;
1310
300
      stream->reset = TRUE;
1311
300
    }
1312
300
  }
1313
  /* Return no error, nghttp2 will RST/GOAWAY by itself when needed */
1314
931
  return 0;
1315
931
}
1316
1317
static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
1318
                              int32_t stream_id,
1319
                              const uint8_t *mem, size_t len, void *userp)
1320
206
{
1321
206
  struct Curl_cfilter *cf = userp;
1322
206
  struct cf_h2_ctx *ctx = cf->ctx;
1323
206
  struct h2_stream_ctx *stream;
1324
206
  struct Curl_easy *data_s;
1325
1326
206
  (void)session;
1327
206
  (void)flags;
1328
1329
206
  DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
1330
206
  DEBUGASSERT(CF_DATA_CURRENT(cf));
1331
1332
  /* get the stream from the hash based on Stream ID */
1333
206
  data_s = cf_h2_get_stream_xfer(cf, stream_id, &stream);
1334
206
  if(!data_s) {
1335
    /* Receiving a Stream ID not in the hash should not happen - unless
1336
       we have aborted a transfer artificially and there were more data
1337
       in the pipeline. Silently ignore. */
1338
0
    CURL_TRC_CF(CF_DATA_CURRENT(cf), cf, "[%d] Data for unknown", stream_id);
1339
    /* consumed explicitly as no one will read it */
1340
0
    Curl_ngh2_session_consume(&ctx->ngh2, stream_id, len);
1341
0
    return 0;
1342
0
  }
1343
1344
206
  if(!stream)
1345
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1346
1347
206
  h2_xfer_write_resp(cf, data_s, stream, (const char *)mem, len, FALSE);
1348
1349
206
  Curl_ngh2_session_consume(&ctx->ngh2, stream_id, len);
1350
206
  stream->nrcvd_data += (curl_off_t)len;
1351
206
  return 0;
1352
206
}
1353
1354
static int on_stream_close(nghttp2_session *session, int32_t stream_id,
1355
                           uint32_t error_code, void *userp)
1356
9.45k
{
1357
9.45k
  struct Curl_cfilter *cf = userp;
1358
9.45k
  struct cf_h2_ctx *ctx = cf->ctx;
1359
9.45k
  struct Curl_easy *data_s, *call_data = CF_DATA_CURRENT(cf);
1360
9.45k
  struct h2_stream_ctx *stream = NULL;
1361
9.45k
  int rv;
1362
9.45k
  (void)session;
1363
1364
9.45k
  DEBUGASSERT(call_data);
1365
  /* stream id 0 is the connection, do not look there for streams. */
1366
9.45k
  data_s = stream_id ? cf_h2_get_stream_xfer(cf, stream_id, &stream) : NULL;
1367
9.45k
  if(!data_s) {
1368
4.45k
    CURL_TRC_CF(call_data, cf,
1369
4.45k
                "[%d] on_stream_close, no easy set on stream", stream_id);
1370
4.45k
    return 0;
1371
4.45k
  }
1372
4.99k
  if(!GOOD_EASY_HANDLE(data_s)) {
1373
    /* nghttp2 still has an easy registered for the stream which has
1374
     * been freed be libcurl. This points to a code path that does not
1375
     * trigger DONE or DETACH events as it must. */
1376
0
    CURL_TRC_CF(call_data, cf,
1377
0
                "[%d] on_stream_close, not a GOOD easy on stream", stream_id);
1378
0
    (void)Curl_ngh2_session_set_stream_user_data(
1379
0
      &ctx->ngh2, stream_id, NULL);
1380
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1381
0
  }
1382
4.99k
  if(!stream) {
1383
0
    CURL_TRC_CF(data_s, cf,
1384
0
                "[%d] on_stream_close, GOOD easy but no stream", stream_id);
1385
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1386
0
  }
1387
1388
4.99k
  stream->closed = TRUE;
1389
4.99k
  stream->error = error_code;
1390
4.99k
  if(stream->error) {
1391
4.88k
    stream->reset = TRUE;
1392
4.88k
    if((stream->error == NGHTTP2_REFUSED_STREAM) &&
1393
4.63k
       ctx->rcvd_goaway && ctx->goaway_error)
1394
    /* REFUSED_STREAM is used by nghttp2 on a GOAWAY from the
1395
     * server where it indicated it will no longer process this stream.
1396
     * If the GOAWAY carried an error code, take that as the error for
1397
     * the stream. */
1398
113
    stream->error = ctx->goaway_error;
1399
4.88k
  }
1400
1401
4.99k
  if(stream->error)
1402
4.88k
    CURL_TRC_CF(data_s, cf, "[%d] RESET: %s (err %u)",
1403
4.99k
              stream_id, nghttp2_http2_strerror(error_code), error_code);
1404
116
  else
1405
116
    CURL_TRC_CF(data_s, cf, "[%d] CLOSED", stream_id);
1406
4.99k
  Curl_multi_mark_dirty(data_s);
1407
1408
  /* remove `stream_ctx` from the nghttp2 stream */
1409
4.99k
  rv = Curl_ngh2_session_set_stream_user_data(
1410
4.99k
    &ctx->ngh2, stream_id, NULL);
1411
4.99k
  if(rv) {
1412
0
    infof(data_s, "http/2: failed to clear user_data for stream %d",
1413
0
          stream_id);
1414
0
    DEBUGASSERT(0);
1415
0
  }
1416
4.99k
  return 0;
1417
4.99k
}
1418
1419
static void cf_h2_header_error(struct Curl_cfilter *cf,
1420
                               struct Curl_easy *data,
1421
                               struct h2_stream_ctx *stream,
1422
                               CURLcode result)
1423
3
{
1424
3
  struct cf_h2_ctx *ctx = cf->ctx;
1425
1426
3
  failf(data, "Error receiving HTTP2 header: %d(%s)", (int)result,
1427
3
        curl_easy_strerror(result));
1428
3
  if(stream) {
1429
3
    Curl_ngh2_submit_rst_stream(
1430
3
      &ctx->ngh2, NGHTTP2_FLAG_NONE, stream->id, NGHTTP2_STREAM_CLOSED);
1431
3
    stream->closed = TRUE;
1432
3
    stream->reset = TRUE;
1433
3
  }
1434
3
}
1435
1436
/* frame->hd.type is either NGHTTP2_HEADERS or NGHTTP2_PUSH_PROMISE */
1437
static int on_header(nghttp2_session *session, const nghttp2_frame *frame,
1438
                     const uint8_t *name, size_t namelen,
1439
                     const uint8_t *value, size_t valuelen,
1440
                     uint8_t flags,
1441
                     void *userp)
1442
54.5k
{
1443
54.5k
  struct Curl_cfilter *cf = userp;
1444
54.5k
  struct cf_h2_ctx *ctx = cf->ctx;
1445
54.5k
  struct h2_stream_ctx *stream;
1446
54.5k
  struct Curl_easy *data;
1447
54.5k
  int32_t stream_id = frame->hd.stream_id;
1448
54.5k
  CURLcode result;
1449
1450
54.5k
  (void)session;
1451
54.5k
  (void)flags;
1452
1453
54.5k
  DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
1454
1455
  /* get the stream from the hash based on Stream ID */
1456
54.5k
  data = cf_h2_get_stream_xfer(cf, stream_id, &stream);
1457
54.5k
  if(!GOOD_EASY_HANDLE(data))
1458
    /* Receiving a Stream ID not in the hash should not happen, this is an
1459
       internal error more than anything else! */
1460
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1461
1462
54.5k
  if(!stream) {
1463
0
    failf(data, "Internal NULL stream");
1464
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1465
0
  }
1466
1467
  /* Store received PUSH_PROMISE headers to be used when the subsequent
1468
     PUSH_PROMISE callback comes */
1469
54.5k
  if(frame->hd.type == NGHTTP2_PUSH_PROMISE) {
1470
0
    char *h;
1471
1472
0
    if((namelen == CURL_CSTRLEN(HTTP_PSEUDO_AUTHORITY)) &&
1473
0
       !strncmp(HTTP_PSEUDO_AUTHORITY, (const char *)name, namelen)) {
1474
      /* pseudo headers are lower case */
1475
0
      int rc = 0;
1476
0
      char *check = curl_maprintf("%s:%d", data->state.origin->hostname,
1477
0
                                  data->state.origin->port);
1478
0
      if(!check)
1479
        /* no memory */
1480
0
        return NGHTTP2_ERR_CALLBACK_FAILURE;
1481
0
      if(!curl_strequal(check, (const char *)value) &&
1482
0
         ((data->state.origin->port != data->state.origin->scheme->defport) ||
1483
0
          !curl_strequal(data->state.origin->hostname, (const char *)value))) {
1484
        /* This is push is not for the same authority that was asked for in
1485
         * the URL. RFC 7540 section 8.2 says: "A client MUST treat a
1486
         * PUSH_PROMISE for which the server is not authoritative as a stream
1487
         * error of type PROTOCOL_ERROR."
1488
         */
1489
0
        (void)Curl_ngh2_submit_rst_stream(
1490
0
          &ctx->ngh2, NGHTTP2_FLAG_NONE, stream_id, NGHTTP2_PROTOCOL_ERROR);
1491
0
        rc = NGHTTP2_ERR_CALLBACK_FAILURE;
1492
0
      }
1493
0
      curlx_free(check);
1494
0
      if(rc)
1495
0
        return rc;
1496
0
    }
1497
1498
0
    if(!stream->push_headers) {
1499
0
      stream->push_headers_alloc = 10;
1500
0
      stream->push_headers = curlx_malloc(stream->push_headers_alloc *
1501
0
                                          sizeof(char *));
1502
0
      if(!stream->push_headers)
1503
0
        return NGHTTP2_ERR_CALLBACK_FAILURE;
1504
0
      stream->push_headers_used = 0;
1505
0
    }
1506
0
    else if(stream->push_headers_used ==
1507
0
            stream->push_headers_alloc) {
1508
0
      char **headp;
1509
0
      if(stream->push_headers_alloc > 1000) {
1510
        /* this is beyond crazy many headers, bail out */
1511
0
        failf(data, "Too many PUSH_PROMISE headers");
1512
0
        free_push_headers(stream);
1513
0
        return NGHTTP2_ERR_CALLBACK_FAILURE;
1514
0
      }
1515
0
      stream->push_headers_alloc *= 2;
1516
0
      headp = curlx_realloc(stream->push_headers,
1517
0
                            stream->push_headers_alloc * sizeof(char *));
1518
0
      if(!headp) {
1519
0
        free_push_headers(stream);
1520
0
        return NGHTTP2_ERR_CALLBACK_FAILURE;
1521
0
      }
1522
0
      stream->push_headers = headp;
1523
0
    }
1524
0
    h = curl_maprintf("%s:%s", name, value);
1525
0
    if(!h) {
1526
0
      free_push_headers(stream);
1527
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1528
0
    }
1529
0
    stream->push_headers[stream->push_headers_used++] = h;
1530
0
    return 0;
1531
0
  }
1532
1533
54.5k
  if(stream->bodystarted) {
1534
    /* This is a trailer */
1535
4
    CURL_TRC_CF(data, cf, "[%d] trailer: %.*s: %.*s",
1536
4
                stream->id, (int)namelen, name, (int)valuelen, value);
1537
4
    result = Curl_dynhds_add(&stream->resp_trailers,
1538
4
                             (const char *)name, namelen,
1539
4
                             (const char *)value, valuelen);
1540
4
    if(result) {
1541
0
      cf_h2_header_error(cf, data, stream, result);
1542
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1543
0
    }
1544
1545
4
    return 0;
1546
4
  }
1547
1548
54.5k
  if(namelen == CURL_CSTRLEN(HTTP_PSEUDO_STATUS) &&
1549
3.33k
     !memcmp(HTTP_PSEUDO_STATUS, name, namelen)) {
1550
    /* nghttp2 guarantees :status is received first and only once. */
1551
2.31k
    char buffer[32];
1552
2.31k
    size_t hlen;
1553
2.31k
    result = Curl_http_decode_status(&stream->status_code,
1554
2.31k
                                     (const char *)value, valuelen);
1555
2.31k
    if(result) {
1556
0
      cf_h2_header_error(cf, data, stream, result);
1557
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1558
0
    }
1559
2.31k
    hlen = curl_msnprintf(buffer, sizeof(buffer), HTTP_PSEUDO_STATUS ":%d\r",
1560
2.31k
                          stream->status_code);
1561
2.31k
    result = Curl_headers_push(data, buffer, hlen, CURLH_PSEUDO);
1562
2.31k
    if(result) {
1563
0
      cf_h2_header_error(cf, data, stream, result);
1564
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1565
0
    }
1566
2.31k
    curlx_dyn_reset(&ctx->scratch);
1567
2.31k
    result = curlx_dyn_addn(&ctx->scratch, STRCONST("HTTP/2 "));
1568
2.31k
    if(!result)
1569
2.31k
      result = curlx_dyn_addn(&ctx->scratch, value, valuelen);
1570
2.31k
    if(!result)
1571
2.31k
      result = curlx_dyn_addn(&ctx->scratch, STRCONST(" \r\n"));
1572
2.31k
    if(!result)
1573
2.31k
      h2_xfer_write_resp_hd(cf, data, stream, curlx_dyn_ptr(&ctx->scratch),
1574
2.31k
                            curlx_dyn_len(&ctx->scratch), FALSE);
1575
2.31k
    if(result) {
1576
2
      cf_h2_header_error(cf, data, stream, result);
1577
2
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1578
2
    }
1579
    /* if we receive data for another handle, wake that up */
1580
2.31k
    if(CF_DATA_CURRENT(cf) != data)
1581
0
      Curl_multi_mark_dirty(data);
1582
1583
2.31k
    CURL_TRC_CF(data, cf, "[%d] status: HTTP/2 %03d",
1584
2.31k
                stream->id, stream->status_code);
1585
2.31k
    return 0;
1586
2.31k
  }
1587
1588
  /* nghttp2 guarantees that namelen > 0, and :status was already
1589
     received, and this is not pseudo-header field . */
1590
  /* convert to an HTTP1-style header */
1591
52.2k
  curlx_dyn_reset(&ctx->scratch);
1592
52.2k
  result = curlx_dyn_addn(&ctx->scratch, (const char *)name, namelen);
1593
52.2k
  if(!result)
1594
52.2k
    result = curlx_dyn_addn(&ctx->scratch, STRCONST(": "));
1595
52.2k
  if(!result)
1596
52.2k
    result = curlx_dyn_addn(&ctx->scratch, (const char *)value, valuelen);
1597
52.2k
  if(!result)
1598
52.2k
    result = curlx_dyn_addn(&ctx->scratch, STRCONST("\r\n"));
1599
52.2k
  if(!result)
1600
52.2k
    h2_xfer_write_resp_hd(cf, data, stream, curlx_dyn_ptr(&ctx->scratch),
1601
52.2k
                          curlx_dyn_len(&ctx->scratch), FALSE);
1602
52.2k
  if(result) {
1603
1
    cf_h2_header_error(cf, data, stream, result);
1604
1
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1605
1
  }
1606
  /* if we receive data for another handle, wake that up */
1607
52.2k
  if(CF_DATA_CURRENT(cf) != data)
1608
0
    Curl_multi_mark_dirty(data);
1609
1610
52.2k
  CURL_TRC_CF(data, cf, "[%d] header: %.*s: %.*s",
1611
52.2k
              stream->id, (int)namelen, name, (int)valuelen, value);
1612
1613
52.2k
  return 0; /* 0 is successful */
1614
52.2k
}
1615
1616
static ssize_t req_body_read_callback(nghttp2_session *session,
1617
                                      int32_t stream_id,
1618
                                      uint8_t *buf, size_t length,
1619
                                      uint32_t *data_flags,
1620
                                      nghttp2_data_source *source,
1621
                                      void *userp)
1622
13.2k
{
1623
13.2k
  struct Curl_cfilter *cf = userp;
1624
13.2k
  struct Curl_easy *data_s;
1625
13.2k
  struct h2_stream_ctx *stream = NULL;
1626
13.2k
  CURLcode result;
1627
13.2k
  ssize_t nread;
1628
13.2k
  size_t n;
1629
13.2k
  (void)source;
1630
1631
13.2k
  (void)session;
1632
13.2k
  if(!stream_id)
1633
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
1634
1635
  /* get the stream from the hash based on Stream ID, stream ID zero is for
1636
     connection-oriented stuff */
1637
13.2k
  data_s = cf_h2_get_stream_xfer(cf, stream_id, &stream);
1638
13.2k
  if(!data_s || !stream)
1639
    /* Receiving a Stream ID not in the hash should not happen, this is an
1640
       internal error more than anything else! */
1641
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1642
1643
13.2k
  result = Curl_bufq_read(&stream->sendbuf, buf, length, &n);
1644
13.2k
  if(result) {
1645
5.11k
    if(result != CURLE_AGAIN)
1646
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1647
5.11k
    nread = 0;
1648
5.11k
  }
1649
8.17k
  else
1650
8.17k
    nread = (ssize_t)n;
1651
1652
13.2k
  CURL_TRC_CF(data_s, cf, "[%d] req_body_read(len=%zu) eos=%d -> %zd, %d",
1653
13.2k
              stream_id, length, stream->body_eos, nread, (int)result);
1654
1655
13.2k
  if(stream->body_eos && Curl_bufq_is_empty(&stream->sendbuf)) {
1656
1.88k
    *data_flags = NGHTTP2_DATA_FLAG_EOF;
1657
1.88k
    return nread;
1658
1.88k
  }
1659
11.4k
  return (nread == 0) ? NGHTTP2_ERR_DEFERRED : nread;
1660
13.2k
}
1661
1662
#ifdef CURLVERBOSE
1663
static int error_callback(nghttp2_session *session,
1664
                          const char *msg,
1665
                          size_t len,
1666
                          void *userp)
1667
4.81k
{
1668
4.81k
  struct Curl_cfilter *cf = userp;
1669
4.81k
  struct Curl_easy *data = CF_DATA_CURRENT(cf);
1670
4.81k
  (void)session;
1671
4.81k
  failf(data, "%.*s", (int)len, msg);
1672
4.81k
  return 0;
1673
4.81k
}
1674
#endif
1675
1676
/*
1677
 * Append headers to ask for an HTTP1.1 to HTTP2 upgrade.
1678
 */
1679
CURLcode Curl_http2_request_upgrade(struct dynbuf *req,
1680
                                    struct Curl_easy *data)
1681
1.23k
{
1682
1.23k
  CURLcode result;
1683
1.23k
  char *base64;
1684
1.23k
  size_t blen;
1685
1.23k
  struct SingleRequest *k = &data->req;
1686
1.23k
  uint8_t binsettings[H2_BINSETTINGS_LEN];
1687
1.23k
  ssize_t rc;
1688
1.23k
  size_t binlen; /* length of the binsettings data */
1689
1690
1.23k
  rc = populate_binsettings(binsettings, data);
1691
1.23k
  if(!curlx_sztouz(rc, &binlen) || !binlen) {
1692
0
    failf(data, "nghttp2 unexpectedly failed on pack_settings_payload");
1693
0
    curlx_dyn_free(req);
1694
0
    return CURLE_FAILED_INIT;
1695
0
  }
1696
1697
1.23k
  result = curlx_base64url_encode(binsettings, binlen, &base64, &blen);
1698
1.23k
  if(result) {
1699
0
    curlx_dyn_free(req);
1700
0
    return result;
1701
0
  }
1702
1703
1.23k
  data->state.http_hd_upgrade = TRUE;
1704
1.23k
  data->state.http_hd_h2_settings = TRUE;
1705
1.23k
  result = curlx_dyn_addf(req,
1706
1.23k
                          "Upgrade: %s\r\n"
1707
1.23k
                          "HTTP2-Settings: %s\r\n",
1708
1.23k
                          NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, base64);
1709
1.23k
  curlx_free(base64);
1710
1711
1.23k
  k->upgr101 = UPGR101_H2;
1712
1.23k
  data->conn->bits.upgrade_in_progress = TRUE;
1713
1714
1.23k
  return result;
1715
1.23k
}
1716
1717
static CURLcode http2_handle_stream_close(struct Curl_cfilter *cf,
1718
                                          struct Curl_easy *data,
1719
                                          struct h2_stream_ctx *stream,
1720
                                          size_t *pnlen)
1721
5.03k
{
1722
5.03k
  CURLcode result;
1723
1724
5.03k
  *pnlen = 0;
1725
5.03k
  if(stream->close_handled)
1726
2
    return CURLE_OK;
1727
1728
5.03k
  if(stream->reset) {
1729
4.91k
    if(stream->error == NGHTTP2_REFUSED_STREAM) {
1730
4.48k
      infof(data, "HTTP/2 stream %d refused by server, try again on a new "
1731
4.48k
                  "connection", stream->id);
1732
4.48k
      connclose(CF_CONN(cf)); /* do not use this anymore */
1733
4.48k
      data->req.refused_stream = TRUE;
1734
4.48k
      return CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
1735
4.48k
    }
1736
432
    else if(stream->resp_hds_complete && data->req.no_body) {
1737
0
      CURL_TRC_CF(data, cf, "[%d] error after response headers, but we did "
1738
0
                  "not want a body anyway, ignore: %s (err %u)",
1739
0
                  stream->id, nghttp2_http2_strerror(stream->error),
1740
0
                  stream->error);
1741
0
      stream->close_handled = TRUE;
1742
0
      return CURLE_OK;
1743
0
    }
1744
432
    failf(data, "HTTP/2 stream %d reset by %s (error 0x%x %s)",
1745
432
          stream->id, stream->reset_by_server ? "server" : "curl",
1746
432
          stream->error, nghttp2_http2_strerror(stream->error));
1747
432
    return stream->error ? CURLE_HTTP2_STREAM :
1748
432
           (data->req.bytecount ? CURLE_PARTIAL_FILE : CURLE_HTTP2);
1749
4.91k
  }
1750
121
  else if(!stream->bodystarted) {
1751
1
    failf(data, "HTTP/2 stream %d was closed cleanly, but before getting "
1752
1
          "all response header fields, treated as error", stream->id);
1753
1
    return CURLE_HTTP2_STREAM;
1754
1
  }
1755
1756
120
  if(Curl_dynhds_count(&stream->resp_trailers)) {
1757
0
    struct dynhds_entry *e;
1758
0
    struct dynbuf dbuf;
1759
0
    uint32_t i, ntrailers = Curl_dynhds_count(&stream->resp_trailers);
1760
1761
0
    result = CURLE_OK;
1762
0
    curlx_dyn_init(&dbuf, DYN_TRAILERS);
1763
0
    for(i = 0; !result && (i < ntrailers); ++i) {
1764
0
      e = Curl_dynhds_getn(&stream->resp_trailers, i);
1765
0
      if(!e)
1766
0
        break;
1767
0
      curlx_dyn_reset(&dbuf);
1768
0
      result = curlx_dyn_addf(&dbuf, "%.*s: %.*s\x0d\x0a",
1769
0
                              (int)e->namelen, e->name,
1770
0
                              (int)e->valuelen, e->value);
1771
0
      if(result)
1772
0
        break;
1773
0
      Curl_debug(data, CURLINFO_HEADER_IN, curlx_dyn_ptr(&dbuf),
1774
0
                 curlx_dyn_len(&dbuf));
1775
0
      result = Curl_client_write(data,
1776
0
                                 CLIENTWRITE_HEADER | CLIENTWRITE_TRAILER,
1777
0
                                 curlx_dyn_ptr(&dbuf), curlx_dyn_len(&dbuf));
1778
0
      if(result)
1779
0
        break;
1780
0
      result = Curl_bump_headersize(data, curlx_dyn_len(&dbuf), FALSE);
1781
0
      if(result)
1782
0
        break;
1783
0
    }
1784
0
    curlx_dyn_free(&dbuf);
1785
0
    if(result)
1786
0
      goto out;
1787
0
  }
1788
1789
120
  stream->close_handled = TRUE;
1790
120
  result = CURLE_OK;
1791
1792
120
out:
1793
120
  CURL_TRC_CF(data, cf, "handle_stream_close -> %d, %zu", (int)result, *pnlen);
1794
120
  return result;
1795
120
}
1796
1797
static int sweight_wanted(const struct Curl_easy *data)
1798
98.6k
{
1799
  /* 0 weight is not set by user and we take the nghttp2 default one */
1800
98.6k
  return data->set.weight ?
1801
98.6k
    data->set.weight : NGHTTP2_DEFAULT_WEIGHT;
1802
98.6k
}
1803
1804
static int sweight_in_effect(const struct Curl_easy *data)
1805
87.2k
{
1806
  /* 0 weight is not set by user and we take the nghttp2 default one */
1807
87.2k
  return data->state.weight ?
1808
87.2k
    data->state.weight : NGHTTP2_DEFAULT_WEIGHT;
1809
87.2k
}
1810
1811
/*
1812
 * h2_pri_spec() fills in the pri_spec struct, used by nghttp2 to send weight
1813
 * and dependency to the peer. It also stores the updated values in the state
1814
 * struct.
1815
 */
1816
1817
static void h2_pri_spec(struct Curl_easy *data,
1818
                        nghttp2_priority_spec *pri_spec)
1819
11.3k
{
1820
11.3k
  int prio = data->set.weight;
1821
11.3k
  nghttp2_priority_spec_init(pri_spec, 0, sweight_wanted(data), FALSE);
1822
11.3k
  data->state.weight = prio;
1823
11.3k
}
1824
1825
/*
1826
 * Check if there is been an update in the priority /
1827
 * dependency settings and if so it submits a PRIORITY frame with the updated
1828
 * info.
1829
 * Flush any out data pending in the network buffer.
1830
 */
1831
static CURLcode h2_progress_egress(struct Curl_cfilter *cf,
1832
                                   struct Curl_easy *data)
1833
87.2k
{
1834
87.2k
  struct cf_h2_ctx *ctx = cf->ctx;
1835
87.2k
  int rv = 0;
1836
1837
87.2k
  if(sweight_wanted(data) != sweight_in_effect(data)) {
1838
0
    struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
1839
0
    if(stream && stream->id > 0 &&
1840
0
       (sweight_wanted(data) != sweight_in_effect(data))) {
1841
      /* send new weight and/or dependency */
1842
0
      nghttp2_priority_spec pri_spec;
1843
1844
0
      h2_pri_spec(data, &pri_spec);
1845
0
      CURL_TRC_CF(data, cf, "[%d] Queuing PRIORITY", stream->id);
1846
0
      DEBUGASSERT(stream->id != -1);
1847
0
      rv = Curl_ngh2_submit_priority(
1848
0
        &ctx->ngh2, NGHTTP2_FLAG_NONE, stream->id, &pri_spec);
1849
0
      if(rv)
1850
0
        goto out;
1851
0
    }
1852
0
  }
1853
1854
87.2k
  ctx->nw_out_blocked = 0;
1855
150k
  while(!rv && !ctx->nw_out_blocked &&
1856
135k
        Curl_ngh2_session_want_write(&ctx->ngh2))
1857
62.8k
    rv = Curl_ngh2_session_send(&ctx->ngh2);
1858
1859
87.2k
out:
1860
87.2k
  if(Curl_ngh2_is_fatal(&ctx->ngh2, rv)) {
1861
141
    VERBOSE(rv = Curl_ngh2_fatal(&ctx->ngh2));
1862
141
    CURL_TRC_CF(data, cf, "nghttp2_session_send error (%s)%d",
1863
141
                nghttp2_strerror(rv), rv);
1864
141
    return CURLE_SEND_ERROR;
1865
141
  }
1866
  /* Defer flushing during the connect phase so that the SETTINGS and
1867
   * other initial frames are sent together with the first request.
1868
   * Unless we are 'connect_only' where the request will never come. */
1869
87.1k
  if(!cf->connected && !CF_CONN(cf)->bits.connect_only)
1870
11.3k
    return CURLE_OK;
1871
75.7k
  return nw_out_flush(cf, data);
1872
87.1k
}
1873
1874
static CURLcode stream_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
1875
                            struct h2_stream_ctx *stream,
1876
                            char *buf, size_t len, size_t *pnread)
1877
85.0k
{
1878
85.0k
  struct cf_h2_ctx *ctx = cf->ctx;
1879
85.0k
  CURLcode result = CURLE_AGAIN;
1880
1881
85.0k
  (void)buf;
1882
85.0k
  (void)len;
1883
85.0k
  *pnread = 0;
1884
1885
85.0k
  if(!stream->xfer_result)
1886
84.9k
    stream->xfer_result = cf_h2_update_local_win(cf, data, stream);
1887
1888
85.0k
  if(stream->xfer_result) {
1889
88
    CURL_TRC_CF(data, cf, "[%d] xfer write failed", stream->id);
1890
88
    result = stream->xfer_result;
1891
88
  }
1892
84.9k
  else if(stream->closed) {
1893
5.03k
    CURL_TRC_CF(data, cf, "[%d] returning CLOSE", stream->id);
1894
5.03k
    result = http2_handle_stream_close(cf, data, stream, pnread);
1895
5.03k
  }
1896
79.9k
  else if(stream->reset ||
1897
79.9k
          (ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) ||
1898
74.2k
          (ctx->rcvd_goaway && ctx->remote_max_sid < stream->id)) {
1899
5.65k
    CURL_TRC_CF(data, cf, "[%d] returning ERR", stream->id);
1900
5.65k
    result = data->req.bytecount ? CURLE_PARTIAL_FILE : CURLE_HTTP2;
1901
5.65k
  }
1902
1903
85.0k
  if(result && (result != CURLE_AGAIN))
1904
10.6k
    CURL_TRC_CF(data, cf, "[%d] stream_recv(len=%zu) -> %d, %zu",
1905
85.0k
                stream->id, len, (int)result, *pnread);
1906
85.0k
  return result;
1907
85.0k
}
1908
1909
static CURLcode h2_progress_ingress(struct Curl_cfilter *cf,
1910
                                    struct Curl_easy *data,
1911
                                    size_t data_max_bytes)
1912
42.7k
{
1913
42.7k
  struct cf_h2_ctx *ctx = cf->ctx;
1914
42.7k
  CURLcode result = CURLE_OK;
1915
42.7k
  size_t nread;
1916
1917
42.7k
  if(should_close_session(ctx)) {
1918
345
    CURL_TRC_CF(data, cf, "[0] ingress: session is closed");
1919
345
    return CURLE_HTTP2;
1920
345
  }
1921
1922
  /* Process network input buffer first */
1923
42.3k
  if(!Curl_bufq_is_empty(&ctx->inbufq)) {
1924
4
    CURL_TRC_CF(data, cf, "Process %zu bytes in connection buffer",
1925
4
                Curl_bufq_len(&ctx->inbufq));
1926
4
    result = h2_process_pending_input(cf, data);
1927
4
    if(result)
1928
0
      return result;
1929
4
  }
1930
1931
42.3k
  if(!data_max_bytes)
1932
0
    data_max_bytes = H2_CHUNK_SIZE;
1933
1934
  /* Receive data from the "lower" filters, e.g. network until
1935
   * it is time to stop due to connection close or us not processing
1936
   * all network input */
1937
69.1k
  while(!ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) {
1938
69.1k
    struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
1939
69.1k
    if(stream && (stream->closed || !data_max_bytes)) {
1940
      /* We would like to abort here and stop processing, so that the transfer
1941
       * loop can handle the data/close here. This may leave data in
1942
       * underlying buffers that will not be consumed. */
1943
5.04k
      if(!cf->next || !cf->next->cft->has_data_pending(cf->next, data))
1944
5.04k
        Curl_multi_mark_dirty(data);
1945
5.04k
      break;
1946
5.04k
    }
1947
1948
64.0k
    result = Curl_cf_recv_bufq(cf->next, data, &ctx->inbufq, 0, &nread);
1949
64.0k
    if(result) {
1950
31.5k
      if(result != CURLE_AGAIN) {
1951
0
        failf(data, "Failed receiving HTTP2 data: %d(%s)", (int)result,
1952
0
              curl_easy_strerror(result));
1953
0
        return result;
1954
0
      }
1955
31.5k
      break;
1956
31.5k
    }
1957
32.5k
    else if(nread == 0) {
1958
5.70k
      CURL_TRC_CF(data, cf, "[0] ingress: connection closed");
1959
5.70k
      ctx->conn_closed = TRUE;
1960
5.70k
      break;
1961
5.70k
    }
1962
26.8k
    else {
1963
26.8k
      CURL_TRC_CF(data, cf, "[0] ingress: read %zu bytes", nread);
1964
26.8k
      data_max_bytes = (data_max_bytes > nread) ? (data_max_bytes - nread) : 0;
1965
26.8k
    }
1966
1967
26.8k
    result = h2_process_pending_input(cf, data);
1968
26.8k
    if(result)
1969
51
      return result;
1970
26.7k
    CURL_TRC_CF(data, cf, "[0] ingress: nw-in buffered %zu",
1971
26.7k
                Curl_bufq_len(&ctx->inbufq));
1972
26.7k
    if(ctx->sent_goaway) {
1973
      /* Buffered input needs another shutdown pass without a socket event. */
1974
0
      if(Curl_ngh2_session_want_read(&ctx->ngh2) &&
1975
0
         Curl_cf_def_data_pending(cf, data))
1976
0
        Curl_multi_mark_dirty(data);
1977
0
      break;
1978
0
    }
1979
26.7k
  }
1980
1981
42.3k
  if(ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) {
1982
5.70k
    CURL_TRC_CF(data, cf, "server closed %s",
1983
5.70k
                ctx->rcvd_goaway ? "with GOAWAY" : "abruptly");
1984
5.70k
    connclose(CF_CONN(cf));
1985
5.70k
  }
1986
1987
42.3k
  CURL_TRC_CF(data, cf, "[0] ingress: done");
1988
42.3k
  return CURLE_OK;
1989
42.3k
}
1990
1991
static CURLcode cf_h2_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
1992
                           char *buf, size_t len, size_t *pnread)
1993
42.7k
{
1994
42.7k
  struct cf_h2_ctx *ctx = cf->ctx;
1995
42.7k
  struct h2_stream_ctx *stream;
1996
42.7k
  CURLcode result, r2;
1997
42.7k
  struct cf_call_data save;
1998
1999
42.7k
  if(!data)
2000
0
    return CURLE_HTTP2;
2001
2002
42.7k
  stream = H2_STREAM_CTX(ctx, data);
2003
2004
42.7k
  *pnread = 0;
2005
42.7k
  if(!stream) {
2006
    /* Abnormal call sequence: either this transfer has never opened a stream
2007
     * (unlikely) or the transfer has been done, cleaned up its resources, but
2008
     * a read() is called anyway. It is not clear what the calling sequence
2009
     * is for such a case. */
2010
0
    failf(data, "http/2 recv on a transfer never opened "
2011
0
          "or already cleared, mid=%u", data->mid);
2012
0
    return CURLE_HTTP2;
2013
0
  }
2014
2015
42.7k
  CF_DATA_SAVE(save, cf, data);
2016
2017
42.7k
  result = stream_recv(cf, data, stream, buf, len, pnread);
2018
42.7k
  if(result && (result != CURLE_AGAIN))
2019
20
    goto out;
2020
2021
42.7k
  if(result) {
2022
42.7k
    result = h2_progress_ingress(cf, data, len);
2023
42.7k
    if(result)
2024
396
      goto out;
2025
2026
42.3k
    result = stream_recv(cf, data, stream, buf, len, pnread);
2027
42.3k
  }
2028
2029
42.3k
  if(*pnread > 0) {
2030
    /* Now that we transferred this to the upper layer, we report
2031
     * the actual amount of DATA consumed to the H2 session, so
2032
     * that it adjusts stream flow control */
2033
0
    Curl_ngh2_session_consume(&ctx->ngh2, stream->id, *pnread);
2034
0
    if(stream->closed) {
2035
0
      CURL_TRC_CF(data, cf, "[%d] DRAIN closed stream", stream->id);
2036
0
      Curl_multi_mark_dirty(data);
2037
0
    }
2038
0
  }
2039
2040
42.7k
out:
2041
42.7k
  r2 = h2_progress_egress(cf, data);
2042
42.7k
  if(r2 == CURLE_AGAIN) {
2043
    /* pending data to send, need to be called again. Ideally, we
2044
     * monitor the socket for POLLOUT, but when not SENDING
2045
     * any more, we force processing of the transfer. */
2046
5.26k
    if(!CURL_REQ_WANT_SEND(data))
2047
2.68k
      Curl_multi_mark_dirty(data);
2048
5.26k
  }
2049
37.4k
  else if(r2 && (result == CURLE_AGAIN)) {
2050
    /* Do not replace a previous error result NOR a successful result
2051
     * with an egress error. Example: the server might close the
2052
     * connection after sending the complete response with EOS
2053
     * and everything. Further sending may fail, but that MUST
2054
     * not change that we had a successful response. */
2055
2
    result = r2;
2056
2
  }
2057
2058
42.7k
  CURL_TRC_CF(data, cf, "[%d] cf_recv(len=%zu) -> %d, %zu, "
2059
42.7k
              "window=%d/%d, connection %d/%d",
2060
42.7k
              stream->id, len, (int)result, *pnread,
2061
42.7k
              Curl_ngh2_session_get_stream_effective_recv_data_length(
2062
42.7k
                &ctx->ngh2, stream->id),
2063
42.7k
              Curl_ngh2_session_get_stream_effective_local_window_size(
2064
42.7k
                &ctx->ngh2, stream->id),
2065
42.7k
              Curl_ngh2_session_get_local_window_size(&ctx->ngh2),
2066
42.7k
              HTTP2_HUGE_WINDOW_SIZE);
2067
2068
42.7k
  CF_DATA_RESTORE(cf, save);
2069
42.7k
  return result;
2070
42.7k
}
2071
2072
static CURLcode cf_h2_body_send(struct Curl_cfilter *cf,
2073
                                struct Curl_easy *data,
2074
                                struct h2_stream_ctx *stream,
2075
                                const void *buf, size_t blen, bool eos,
2076
                                size_t *pnwritten)
2077
18.1k
{
2078
18.1k
  struct cf_h2_ctx *ctx = cf->ctx;
2079
18.1k
  CURLcode result;
2080
2081
18.1k
  *pnwritten = 0;
2082
18.1k
  if(stream->closed) {
2083
143
    if(stream->resp_hds_complete) {
2084
      /* Server decided to close the stream after having sent us a final
2085
       * response. This is valid if it is not interested in the request
2086
       * body. This happens on 30x or 40x responses.
2087
       * We silently discard the data sent, since this is not a transport
2088
       * error situation. */
2089
0
      CURL_TRC_CF(data, cf, "[%d] discarding data"
2090
0
                  "on closed stream with response", stream->id);
2091
0
      if(eos)
2092
0
        stream->body_eos = TRUE;
2093
0
      *pnwritten = blen;
2094
0
      return CURLE_OK;
2095
0
    }
2096
    /* Server closed before we got a response, this is an error */
2097
143
    infof(data, "stream %d closed", stream->id);
2098
143
    return CURLE_SEND_ERROR;
2099
143
  }
2100
2101
17.9k
  result = Curl_bufq_write(&stream->sendbuf, buf, blen, pnwritten);
2102
17.9k
  if(result)
2103
1.87k
    return result;
2104
2105
16.0k
  if(eos && (blen == *pnwritten))
2106
10.0k
    stream->body_eos = TRUE;
2107
2108
16.0k
  if(eos || !Curl_bufq_is_empty(&stream->sendbuf)) {
2109
    /* resume the potentially suspended stream */
2110
16.0k
    if(Curl_ngh2_is_fatal(&ctx->ngh2,
2111
16.0k
         Curl_ngh2_session_resume_data(&ctx->ngh2, stream->id)))
2112
0
      return CURLE_SEND_ERROR;
2113
16.0k
  }
2114
2115
16.0k
  return CURLE_OK;
2116
16.0k
}
2117
2118
static CURLcode h2_submit(struct h2_stream_ctx **pstream,
2119
                          struct Curl_cfilter *cf, struct Curl_easy *data,
2120
                          const uint8_t *buf, size_t len,
2121
                          bool eos, size_t *pnwritten)
2122
11.5k
{
2123
11.5k
  struct cf_h2_ctx *ctx = cf->ctx;
2124
11.5k
  struct h2_stream_ctx *stream = NULL;
2125
11.5k
  struct dynhds h2_headers;
2126
11.5k
  nghttp2_nv *nva = NULL;
2127
11.5k
  const void *body = NULL;
2128
11.5k
  size_t nheader, bodylen;
2129
11.5k
  nghttp2_data_provider data_prd;
2130
11.5k
  int32_t stream_id;
2131
11.5k
  nghttp2_priority_spec pri_spec;
2132
11.5k
  size_t nwritten;
2133
11.5k
  CURLcode result = CURLE_OK;
2134
11.5k
  uint32_t initial_win_size;
2135
2136
11.5k
  *pnwritten = 0;
2137
11.5k
  Curl_dynhds_init(&h2_headers, MAX_HTTP_RESP_HEADER_COUNT, DYN_HTTP_REQUEST);
2138
2139
11.5k
  result = http2_data_setup(cf, data, &stream);
2140
11.5k
  if(result)
2141
4
    goto out;
2142
2143
11.5k
  result = Curl_h1_req_parse_read(
2144
11.5k
    &stream->h1, buf, len, NULL,
2145
11.5k
    !data->state.http_ignorecustom ?
2146
11.5k
    CURL_EASY_STR(data, STRING_CUSTOMREQUEST) : NULL,
2147
11.5k
    0, &nwritten);
2148
11.5k
  if(result)
2149
58
    goto out;
2150
11.4k
  *pnwritten = nwritten;
2151
11.4k
  if(!stream->h1.done) {
2152
    /* need more data */
2153
126
    goto out;
2154
126
  }
2155
11.3k
  DEBUGASSERT(stream->h1.req);
2156
2157
11.3k
  result = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
2158
11.3k
  if(result)
2159
13
    goto out;
2160
  /* no longer needed */
2161
11.3k
  Curl_h1_req_parse_free(&stream->h1);
2162
2163
11.3k
  nva = Curl_dynhds_to_nva(&h2_headers, &nheader);
2164
11.3k
  if(!nva) {
2165
2
    result = CURLE_OUT_OF_MEMORY;
2166
2
    goto out;
2167
2
  }
2168
2169
11.3k
  h2_pri_spec(data, &pri_spec);
2170
11.3k
  if(!Curl_ngh2_session_check_request_allowed(&ctx->ngh2))
2171
0
    CURL_TRC_CF(data, cf, "send request NOT allowed (via nghttp2)");
2172
2173
  /* Check the initial windows size of the transfer (rate-limits?) and
2174
   * send an updated settings on changes from previous value. */
2175
11.3k
  initial_win_size = cf_h2_initial_win_size(data);
2176
11.3k
  if(initial_win_size != ctx->initial_win_size) {
2177
0
    result = cf_h2_update_settings(ctx, initial_win_size);
2178
0
    if(result)
2179
0
      goto out;
2180
0
  }
2181
2182
11.3k
  switch(data->state.httpreq) {
2183
193
  case HTTPREQ_POST:
2184
193
  case HTTPREQ_POST_FORM:
2185
3.26k
  case HTTPREQ_POST_MIME:
2186
3.49k
  case HTTPREQ_PUT:
2187
3.49k
    data_prd.read_callback = req_body_read_callback;
2188
3.49k
    data_prd.source.ptr = NULL;
2189
3.49k
    stream_id = Curl_ngh2_submit_request(
2190
3.49k
      &ctx->ngh2, &pri_spec, nva, nheader, &data_prd, stream);
2191
3.49k
    break;
2192
7.85k
  default:
2193
7.85k
    stream_id = Curl_ngh2_submit_request(
2194
7.85k
      &ctx->ngh2, &pri_spec, nva, nheader, NULL, stream);
2195
11.3k
  }
2196
2197
11.3k
  if(stream_id < 0) {
2198
6
    CURL_TRC_CF(data, cf, "send: nghttp2_submit_request error (%s)%d",
2199
6
                nghttp2_strerror(stream_id), stream_id);
2200
6
    result = CURLE_SEND_ERROR;
2201
6
    goto out;
2202
6
  }
2203
2204
11.3k
#ifdef CURLVERBOSE
2205
11.3k
#define MAX_ACC 60000  /* <64KB to account for some overhead */
2206
11.3k
  if(Curl_trc_is_verbose(data)) {
2207
3.37k
    size_t acc = 0, i;
2208
2209
3.37k
    infof(data, "[HTTP/2] [%d] OPENED stream for %s",
2210
3.37k
          stream_id, Curl_bufref_ptr(&data->state.url));
2211
53.1k
    for(i = 0; i < nheader; ++i) {
2212
49.8k
      acc += nva[i].namelen + nva[i].valuelen;
2213
2214
49.8k
      infof(data, "[HTTP/2] [%d] [%.*s: %.*s]", stream_id,
2215
49.8k
            (int)nva[i].namelen, nva[i].name,
2216
49.8k
            (int)nva[i].valuelen, nva[i].value);
2217
49.8k
    }
2218
2219
3.37k
    if(acc > MAX_ACC) {
2220
13
      infof(data, "[HTTP/2] Warning: The cumulative length of all "
2221
13
            "headers exceeds %d bytes and that could cause the "
2222
13
            "stream to be rejected.", MAX_ACC);
2223
13
    }
2224
3.37k
  }
2225
11.3k
#endif
2226
2227
11.3k
  stream->id = stream_id;
2228
2229
11.3k
  body = (const char *)buf + *pnwritten;
2230
11.3k
  bodylen = len - *pnwritten;
2231
2232
11.3k
  if(bodylen || eos) {
2233
11.2k
    size_t n;
2234
11.2k
    result = cf_h2_body_send(cf, data, stream, body, bodylen, eos, &n);
2235
11.2k
    if(!result)
2236
11.2k
      *pnwritten += n;
2237
9
    else if(result == CURLE_AGAIN)
2238
0
      result = CURLE_OK;
2239
9
    else {
2240
9
      result = CURLE_SEND_ERROR;
2241
9
    }
2242
11.2k
  }
2243
2244
11.5k
out:
2245
11.5k
  CURL_TRC_CF(data, cf, "[%d] submit -> %d, %zu",
2246
11.5k
              stream ? stream->id : -1, (int)result, *pnwritten);
2247
11.5k
  curlx_safefree(nva);
2248
11.5k
  *pstream = stream;
2249
11.5k
  Curl_dynhds_cleanup(&h2_headers);
2250
11.5k
  return result;
2251
11.3k
}
2252
2253
static CURLcode cf_h2_send(struct Curl_cfilter *cf, struct Curl_easy *data,
2254
                           const uint8_t *buf, size_t len, bool eos,
2255
                           size_t *pnwritten)
2256
18.3k
{
2257
18.3k
  struct cf_h2_ctx *ctx = cf->ctx;
2258
18.3k
  struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
2259
18.3k
  struct cf_call_data save;
2260
18.3k
  CURLcode result = CURLE_OK, r2;
2261
2262
18.3k
  CF_DATA_SAVE(save, cf, data);
2263
18.3k
  *pnwritten = 0;
2264
2265
18.3k
  if(!stream || stream->id == -1) {
2266
11.5k
    result = h2_submit(&stream, cf, data, buf, len, eos, pnwritten);
2267
11.5k
    if(result)
2268
92
      goto out;
2269
11.4k
    DEBUGASSERT(stream);
2270
11.4k
  }
2271
6.83k
  else if(stream->body_eos) {
2272
    /* We already wrote this, but CURLE_AGAIN-ed the call due to not
2273
     * being able to flush stream->sendbuf. Make a 0-length write
2274
     * to trigger flushing again.
2275
     * If this works, we report to have written `len` bytes. */
2276
0
    size_t n;
2277
0
    DEBUGASSERT(eos);
2278
0
    result = cf_h2_body_send(cf, data, stream, buf, 0, eos, &n);
2279
0
    CURL_TRC_CF(data, cf, "[%d] cf_body_send last CHUNK -> %d, %zu, eos=%d",
2280
0
                stream->id, (int)result, n, eos);
2281
0
    if(result)
2282
0
      goto out;
2283
0
    *pnwritten = len;
2284
0
  }
2285
6.83k
  else {
2286
6.83k
    result = cf_h2_body_send(cf, data, stream, buf, len, eos, pnwritten);
2287
6.83k
    CURL_TRC_CF(data, cf, "[%d] cf_body_send(len=%zu) -> %d, %zu, eos=%d",
2288
6.83k
                stream->id, len, (int)result, *pnwritten, eos);
2289
6.83k
  }
2290
2291
  /* Call the nghttp2 send loop and flush to write ALL buffered data,
2292
   * headers and/or request body completely out to the network */
2293
18.3k
  r2 = h2_progress_egress(cf, data);
2294
2295
  /* if the stream has been closed in egress handling (nghttp2 does that
2296
   * when it does not like the headers, for example */
2297
18.3k
  if(stream && stream->closed && !stream->resp_hds_complete) {
2298
194
    infof(data, "stream %d closed", stream->id);
2299
194
    result = CURLE_SEND_ERROR;
2300
194
    goto out;
2301
194
  }
2302
18.1k
  else if(r2 && (r2 != CURLE_AGAIN)) {
2303
30
    result = r2;
2304
30
    goto out;
2305
30
  }
2306
2307
18.0k
  if(should_close_session(ctx)) {
2308
    /* nghttp2 thinks this session is done. If the stream has not been
2309
     * closed, this is an error state for out transfer */
2310
40
    if(stream && stream->closed) {
2311
2
      result = http2_handle_stream_close(cf, data, stream, pnwritten);
2312
2
    }
2313
38
    else {
2314
38
      CURL_TRC_CF(data, cf, "send: nothing to do in this session");
2315
38
      result = CURLE_HTTP2;
2316
38
    }
2317
40
  }
2318
2319
18.3k
out:
2320
18.3k
  if(stream) {
2321
18.3k
    CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) -> %d, %zu, "
2322
18.3k
                "eos=%d, h2 windows %d-%d (stream-conn), "
2323
18.3k
                "buffers %zu-%zu (stream-conn)",
2324
18.3k
                stream->id, len, (int)result, *pnwritten,
2325
18.3k
                stream->body_eos,
2326
18.3k
                Curl_ngh2_session_get_stream_remote_window_size(
2327
18.3k
                  &ctx->ngh2, stream->id),
2328
18.3k
                Curl_ngh2_session_get_remote_window_size(&ctx->ngh2),
2329
18.3k
                Curl_bufq_len(&stream->sendbuf),
2330
18.3k
                Curl_bufq_len(&ctx->outbufq));
2331
18.3k
  }
2332
4
  else {
2333
4
    CURL_TRC_CF(data, cf, "cf_send(len=%zu) -> %d, %zu, "
2334
4
                "connection-window=%d, nw_send_buffer(%zu)",
2335
4
                len, (int)result, *pnwritten,
2336
4
                Curl_ngh2_session_get_remote_window_size(&ctx->ngh2),
2337
4
                Curl_bufq_len(&ctx->outbufq));
2338
4
  }
2339
18.3k
  CF_DATA_RESTORE(cf, save);
2340
18.3k
  return result;
2341
18.3k
}
2342
2343
static CURLcode cf_h2_flush(struct Curl_cfilter *cf,
2344
                            struct Curl_easy *data)
2345
4.85k
{
2346
4.85k
  struct cf_h2_ctx *ctx = cf->ctx;
2347
4.85k
  struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
2348
4.85k
  struct cf_call_data save;
2349
4.85k
  CURLcode result = CURLE_OK;
2350
2351
4.85k
  CF_DATA_SAVE(save, cf, data);
2352
4.85k
  if(stream && !Curl_bufq_is_empty(&stream->sendbuf)) {
2353
    /* resume the potentially suspended stream */
2354
3.80k
    if(Curl_ngh2_is_fatal(&ctx->ngh2,
2355
3.80k
         Curl_ngh2_session_resume_data(&ctx->ngh2, stream->id))) {
2356
1
      result = CURLE_SEND_ERROR;
2357
1
      goto out;
2358
1
    }
2359
3.80k
  }
2360
2361
4.85k
  result = h2_progress_egress(cf, data);
2362
2363
4.85k
out:
2364
4.85k
  if(stream) {
2365
4.85k
    CURL_TRC_CF(data, cf, "[%d] flush -> %d, "
2366
4.85k
                "h2 windows %d-%d (stream-conn), "
2367
4.85k
                "buffers %zu-%zu (stream-conn)",
2368
4.85k
                stream->id, (int)result,
2369
4.85k
                Curl_ngh2_session_get_stream_remote_window_size(
2370
4.85k
                  &ctx->ngh2, stream->id),
2371
4.85k
                Curl_ngh2_session_get_remote_window_size(&ctx->ngh2),
2372
4.85k
                Curl_bufq_len(&stream->sendbuf),
2373
4.85k
                Curl_bufq_len(&ctx->outbufq));
2374
4.85k
  }
2375
0
  else {
2376
0
    CURL_TRC_CF(data, cf, "flush -> %d, "
2377
0
                "connection-window=%d, nw_send_buffer(%zu)", (int)result,
2378
0
                Curl_ngh2_session_get_remote_window_size(&ctx->ngh2),
2379
0
                Curl_bufq_len(&ctx->outbufq));
2380
0
  }
2381
4.85k
  CF_DATA_RESTORE(cf, save);
2382
4.85k
  return result;
2383
4.85k
}
2384
2385
static CURLcode cf_h2_adjust_pollset(struct Curl_cfilter *cf,
2386
                                     struct Curl_easy *data,
2387
                                     struct easy_pollset *ps)
2388
5.23k
{
2389
5.23k
  struct cf_h2_ctx *ctx = cf->ctx;
2390
5.23k
  struct cf_call_data save;
2391
5.23k
  curl_socket_t sock;
2392
5.23k
  bool want_recv, want_send;
2393
5.23k
  CURLcode result = CURLE_OK;
2394
2395
5.23k
  if(!Curl_ngh2_valid(&ctx->ngh2))
2396
0
    return CURLE_OK;
2397
2398
5.23k
  sock = cf->chain->sock;
2399
5.23k
  Curl_pollset_check(data, ps, sock, &want_recv, &want_send);
2400
5.23k
  if(want_recv || want_send) {
2401
5.09k
    struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
2402
5.09k
    bool c_exhaust, s_exhaust;
2403
2404
5.09k
    CF_DATA_SAVE(save, cf, data);
2405
5.09k
    c_exhaust = want_send &&
2406
681
                !Curl_ngh2_session_get_remote_window_size(&ctx->ngh2);
2407
5.09k
    s_exhaust = want_send && stream && stream->id >= 0 &&
2408
681
                !Curl_ngh2_session_get_stream_remote_window_size(
2409
681
                  &ctx->ngh2, stream->id);
2410
5.09k
    want_recv = (want_recv || c_exhaust || s_exhaust);
2411
5.09k
    want_send = (!s_exhaust && want_send) ||
2412
4.46k
                (!c_exhaust && Curl_ngh2_session_want_write(&ctx->ngh2)) ||
2413
4.44k
                !Curl_bufq_is_empty(&ctx->outbufq);
2414
2415
5.09k
    result = Curl_pollset_set(data, ps, sock, want_recv, want_send);
2416
5.09k
    CF_DATA_RESTORE(cf, save);
2417
5.09k
  }
2418
145
  else if(ctx->sent_goaway && !cf->shutdown) {
2419
    /* shutdown in progress */
2420
145
    CF_DATA_SAVE(save, cf, data);
2421
145
    want_send = Curl_ngh2_session_want_write(&ctx->ngh2) ||
2422
56
                !Curl_bufq_is_empty(&ctx->outbufq);
2423
145
    want_recv = Curl_ngh2_session_want_read(&ctx->ngh2);
2424
145
    result = Curl_pollset_set(data, ps, sock, want_recv, want_send);
2425
145
    CF_DATA_RESTORE(cf, save);
2426
145
  }
2427
5.23k
  return result;
2428
5.23k
}
2429
2430
static CURLcode cf_h2_ctx_open(struct Curl_cfilter *cf,
2431
                               struct Curl_easy *data)
2432
11.4k
{
2433
11.4k
  struct cf_h2_ctx *ctx = cf->ctx;
2434
11.4k
  struct h2_stream_ctx *stream;
2435
11.4k
  CURLcode result = CURLE_OUT_OF_MEMORY;
2436
11.4k
  int rc;
2437
11.4k
  nghttp2_session_callbacks *cbs = NULL;
2438
2439
11.4k
  DEBUGASSERT(!ctx->ngh2.s);
2440
11.4k
  DEBUGASSERT(!ctx->ngh2.fatal_rv);
2441
11.4k
  DEBUGASSERT(ctx->initialized);
2442
2443
11.4k
  rc = nghttp2_session_callbacks_new(&cbs);
2444
11.4k
  if(rc) {
2445
0
    failf(data, "Could not initialize nghttp2 callbacks");
2446
0
    goto out;
2447
0
  }
2448
2449
11.4k
  nghttp2_session_callbacks_set_send_callback(cbs, send_callback);
2450
11.4k
  nghttp2_session_callbacks_set_on_frame_recv_callback(cbs, on_frame_recv);
2451
11.4k
  nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(cbs,
2452
11.4k
    cf_h2_on_invalid_frame_recv);
2453
11.4k
#ifdef CURLVERBOSE
2454
11.4k
  nghttp2_session_callbacks_set_on_frame_send_callback(cbs, on_frame_send);
2455
11.4k
#endif
2456
11.4k
  nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
2457
11.4k
    cbs, on_data_chunk_recv);
2458
11.4k
  nghttp2_session_callbacks_set_on_stream_close_callback(cbs, on_stream_close);
2459
11.4k
  nghttp2_session_callbacks_set_on_header_callback(cbs, on_header);
2460
11.4k
#ifdef CURLVERBOSE
2461
11.4k
  nghttp2_session_callbacks_set_error_callback(cbs, error_callback);
2462
11.4k
#endif
2463
2464
  /* The nghttp2 session is not yet setup, do it */
2465
11.4k
  rc = h2_client_new(cf, cbs);
2466
11.4k
  if(rc) {
2467
8
    failf(data, "Could not initialize nghttp2");
2468
8
    goto out;
2469
8
  }
2470
11.4k
  ctx->max_concurrent_streams = data->multi ?
2471
11.4k
    Curl_multi_max_concurrent_streams(data->multi) :
2472
11.4k
    DEFAULT_MAX_CONCURRENT_STREAMS;
2473
2474
11.4k
  if(ctx->via_h1_upgrade) {
2475
    /* HTTP/1.1 Upgrade issued. H2 Settings have already been submitted
2476
     * in the H1 request and we upgrade from there. This stream
2477
     * is opened implicitly as #1. */
2478
11
    uint8_t binsettings[H2_BINSETTINGS_LEN];
2479
11
    ssize_t rclen;
2480
11
    size_t binlen; /* length of the binsettings data */
2481
2482
11
    rclen = populate_binsettings(binsettings, data);
2483
2484
11
    if(!curlx_sztouz(rclen, &binlen) || !binlen) {
2485
0
      failf(data, "nghttp2 unexpectedly failed on pack_settings_payload");
2486
0
      result = CURLE_FAILED_INIT;
2487
0
      goto out;
2488
0
    }
2489
2490
11
    result = http2_data_setup(cf, data, &stream);
2491
11
    if(result)
2492
1
      goto out;
2493
10
    DEBUGASSERT(stream);
2494
10
    stream->id = 1;
2495
    /* queue SETTINGS frame (again) */
2496
10
    rc = Curl_ngh2_session_upgrade2(
2497
10
      &ctx->ngh2, binsettings, binlen, data->state.httpreq == HTTPREQ_HEAD,
2498
10
      NULL);
2499
10
    if(rc) {
2500
3
      failf(data, "nghttp2_session_upgrade2() failed: %s(%d)",
2501
3
            nghttp2_strerror(rc), rc);
2502
3
      result = CURLE_HTTP2;
2503
3
      goto out;
2504
3
    }
2505
2506
7
    rc = Curl_ngh2_session_set_stream_user_data(
2507
7
      &ctx->ngh2, stream->id, stream);
2508
7
    if(rc) {
2509
0
      infof(data, "http/2: failed to set user_data for stream %d",
2510
0
            stream->id);
2511
0
      DEBUGASSERT(0);
2512
0
    }
2513
7
    CURL_TRC_CF(data, cf, "created session via Upgrade");
2514
7
  }
2515
11.3k
  else {
2516
11.3k
    nghttp2_settings_entry iv[H2_SETTINGS_IV_LEN];
2517
11.3k
    size_t ivlen;
2518
2519
11.3k
    ivlen = populate_settings(iv, data, ctx);
2520
11.3k
    rc = Curl_ngh2_submit_settings(
2521
11.3k
      &ctx->ngh2, NGHTTP2_FLAG_NONE, iv, ivlen);
2522
11.3k
    if(rc) {
2523
7
      failf(data, "nghttp2_submit_settings() failed: %s(%d)",
2524
7
            nghttp2_strerror(rc), rc);
2525
7
      result = CURLE_HTTP2;
2526
7
      goto out;
2527
7
    }
2528
11.3k
  }
2529
2530
11.3k
  rc = Curl_ngh2_session_set_local_window_size(
2531
11.3k
    &ctx->ngh2, NGHTTP2_FLAG_NONE, 0, HTTP2_HUGE_WINDOW_SIZE);
2532
11.3k
  if(rc) {
2533
2
    failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
2534
2
          nghttp2_strerror(rc), rc);
2535
2
    result = CURLE_HTTP2;
2536
2
    goto out;
2537
2
  }
2538
2539
  /* all set, traffic will be send on connect */
2540
11.3k
  result = CURLE_OK;
2541
11.3k
  CURL_TRC_CF(data, cf, "[0] created h2 session%s",
2542
11.3k
              ctx->via_h1_upgrade ? " (via h1 upgrade)" : "");
2543
2544
11.4k
out:
2545
11.4k
  if(cbs)
2546
11.4k
    nghttp2_session_callbacks_del(cbs);
2547
11.4k
  return result;
2548
11.3k
}
2549
2550
static CURLcode cf_h2_connect(struct Curl_cfilter *cf,
2551
                              struct Curl_easy *data,
2552
                              bool *done)
2553
11.4k
{
2554
11.4k
  struct cf_h2_ctx *ctx = cf->ctx;
2555
11.4k
  CURLcode result = CURLE_OK;
2556
11.4k
  struct cf_call_data save;
2557
11.4k
  bool first_time = FALSE;
2558
2559
11.4k
  if(cf->connected) {
2560
0
    *done = TRUE;
2561
0
    return CURLE_OK;
2562
0
  }
2563
2564
  /* Connect the lower filters first */
2565
11.4k
  if(!cf->next->connected) {
2566
0
    result = Curl_conn_cf_connect(cf->next, data, done);
2567
0
    if(result || !*done)
2568
0
      return result;
2569
0
  }
2570
2571
11.4k
  *done = FALSE;
2572
2573
11.4k
  CF_DATA_SAVE(save, cf, data);
2574
11.4k
  DEBUGASSERT(ctx->initialized);
2575
11.4k
  if(!ctx->ngh2.s) {
2576
11.4k
    result = cf_h2_ctx_open(cf, data);
2577
11.4k
    if(result)
2578
21
      goto out;
2579
11.3k
    first_time = TRUE;
2580
11.3k
  }
2581
2582
11.3k
  if(!first_time) {
2583
0
    result = h2_progress_ingress(cf, data, 0);
2584
0
    if(result)
2585
0
      goto out;
2586
0
  }
2587
2588
  /* Send out our SETTINGS and ACKs and such. If that blocks, we
2589
   * have it buffered and can count this filter as being connected */
2590
11.3k
  result = h2_progress_egress(cf, data);
2591
11.3k
  if(result && (result != CURLE_AGAIN))
2592
1
    goto out;
2593
2594
11.3k
  *done = TRUE;
2595
11.3k
  cf->connected = TRUE;
2596
11.3k
  result = CURLE_OK;
2597
2598
11.4k
out:
2599
11.4k
  CURL_TRC_CF(data, cf, "cf_connect() -> %d, %d, ", (int)result, *done);
2600
11.4k
  CF_DATA_RESTORE(cf, save);
2601
11.4k
  return result;
2602
11.4k
}
2603
2604
static void cf_h2_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
2605
11.4k
{
2606
11.4k
  struct cf_h2_ctx *ctx = cf->ctx;
2607
2608
11.4k
  (void)data;
2609
11.4k
  if(ctx) {
2610
11.4k
    cf_h2_ctx_free(ctx);
2611
11.4k
    cf->ctx = NULL;
2612
11.4k
  }
2613
11.4k
}
2614
2615
static CURLcode cf_h2_shutdown(struct Curl_cfilter *cf,
2616
                               struct Curl_easy *data, bool *done)
2617
11.0k
{
2618
11.0k
  struct cf_h2_ctx *ctx = cf->ctx;
2619
11.0k
  struct cf_call_data save;
2620
11.0k
  CURLcode result;
2621
11.0k
  int rv;
2622
2623
11.0k
  if(!cf->connected || !Curl_ngh2_valid(&ctx->ngh2) ||
2624
10.9k
     cf->shutdown || ctx->conn_closed) {
2625
68
    *done = TRUE;
2626
68
    return CURLE_OK;
2627
68
  }
2628
2629
10.9k
  CF_DATA_SAVE(save, cf, data);
2630
2631
10.9k
  if(!ctx->sent_goaway) {
2632
4.97k
    ctx->sent_goaway = TRUE;
2633
4.97k
    rv = Curl_ngh2_submit_goaway(
2634
4.97k
      &ctx->ngh2, NGHTTP2_FLAG_NONE, ctx->local_max_sid, 0,
2635
4.97k
      (const uint8_t *)"shutdown", sizeof("shutdown"));
2636
4.97k
    if(rv) {
2637
5
      failf(data, "nghttp2_submit_goaway() failed: %s(%d)",
2638
5
            nghttp2_strerror(rv), rv);
2639
5
      result = CURLE_SEND_ERROR;
2640
5
      goto out;
2641
5
    }
2642
4.97k
  }
2643
  /* GOAWAY submitted, process egress and ingress until nghttp2 is done. */
2644
10.9k
  result = CURLE_OK;
2645
10.9k
  if(Curl_ngh2_session_want_write(&ctx->ngh2) ||
2646
2.66k
     !Curl_bufq_is_empty(&ctx->outbufq))
2647
9.97k
    result = h2_progress_egress(cf, data);
2648
10.9k
  if(!result && Curl_ngh2_session_want_read(&ctx->ngh2))
2649
0
    result = h2_progress_ingress(cf, data, 0);
2650
2651
10.9k
  if(result == CURLE_AGAIN)
2652
6.39k
    result = CURLE_OK;
2653
2654
10.9k
  *done = (ctx->conn_closed ||
2655
10.9k
           (!result && !Curl_ngh2_session_want_write(&ctx->ngh2) &&
2656
6.38k
            !Curl_ngh2_session_want_read(&ctx->ngh2) &&
2657
6.38k
            Curl_bufq_is_empty(&ctx->outbufq)));
2658
2659
10.9k
out:
2660
10.9k
  CF_DATA_RESTORE(cf, save);
2661
10.9k
  cf->shutdown = (result || *done);
2662
10.9k
  return result;
2663
10.9k
}
2664
2665
static CURLcode http2_data_pause(struct Curl_cfilter *cf,
2666
                                 struct Curl_easy *data,
2667
                                 bool pause)
2668
0
{
2669
0
  struct cf_h2_ctx *ctx = cf->ctx;
2670
0
  struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
2671
2672
0
  DEBUGASSERT(data);
2673
0
  if(ctx && Curl_ngh2_valid(&ctx->ngh2) && stream) {
2674
0
    CURLcode result;
2675
2676
0
    stream->write_paused = pause;
2677
0
    result = cf_h2_update_local_win(cf, data, stream);
2678
0
    if(result)
2679
0
      return result;
2680
2681
    /* attempt to send the window update */
2682
0
    (void)h2_progress_egress(cf, data);
2683
2684
0
    if(!pause) {
2685
      /* Unpausing a h2 transfer, requires it to be run again. The server
2686
       * may send new DATA on us increasing the flow window, and it may
2687
       * not. We may have already buffered and exhausted the new window
2688
       * by operating on things in flight during the handling of other
2689
       * transfers. */
2690
0
      Curl_multi_mark_dirty(data);
2691
0
    }
2692
0
    CURL_TRC_CF(data, cf, "[%d] stream now %spaused", stream->id,
2693
0
                pause ? "" : "un");
2694
0
  }
2695
0
  return CURLE_OK;
2696
0
}
2697
2698
static CURLcode cf_h2_cntrl(struct Curl_cfilter *cf,
2699
                            struct Curl_easy *data,
2700
                            int event, int arg1, void *arg2)
2701
39.2k
{
2702
39.2k
  CURLcode result = CURLE_OK;
2703
39.2k
  struct cf_call_data save;
2704
2705
39.2k
  (void)arg2;
2706
2707
39.2k
  CF_DATA_SAVE(save, cf, data);
2708
39.2k
  switch(event) {
2709
75
  case CF_CTRL_DATA_SETUP:
2710
75
    break;
2711
0
  case CF_CTRL_DATA_PAUSE:
2712
0
    result = http2_data_pause(cf, data, (arg1 != 0));
2713
0
    break;
2714
4.85k
  case CF_CTRL_FLUSH:
2715
4.85k
    result = cf_h2_flush(cf, data);
2716
4.85k
    break;
2717
11.4k
  case CF_CTRL_DATA_DONE:
2718
11.4k
    http2_data_done(cf, data);
2719
11.4k
    break;
2720
11.4k
  case CF_CTRL_CONN_INFO_UPDATE:
2721
11.4k
    if(cf->connected)
2722
11.3k
      Curl_conn_set_multiplex(cf->chain, 20);
2723
11.4k
    break;
2724
11.4k
  default:
2725
11.4k
    break;
2726
39.2k
  }
2727
39.2k
  CF_DATA_RESTORE(cf, save);
2728
39.2k
  return result;
2729
39.2k
}
2730
2731
static bool cf_h2_data_pending(struct Curl_cfilter *cf,
2732
                               const struct Curl_easy *data)
2733
36.6k
{
2734
36.6k
  struct cf_h2_ctx *ctx = cf->ctx;
2735
2736
36.6k
  if(ctx && !Curl_bufq_is_empty(&ctx->inbufq))
2737
0
    return TRUE;
2738
36.6k
  return cf->next ? cf->next->cft->has_data_pending(cf->next, data) : FALSE;
2739
36.6k
}
2740
2741
static bool cf_h2_is_alive(struct Curl_cfilter *cf,
2742
                           struct Curl_easy *data,
2743
                           bool *input_pending)
2744
80
{
2745
80
  struct cf_h2_ctx *ctx = cf->ctx;
2746
80
  bool alive;
2747
80
  struct cf_call_data save;
2748
2749
80
  *input_pending = FALSE;
2750
80
  CF_DATA_SAVE(save, cf, data);
2751
80
  alive = (ctx && Curl_ngh2_valid(&ctx->ngh2) &&
2752
80
           http2_connisalive(cf, data, input_pending));
2753
80
  CURL_TRC_CF(data, cf, "conn alive -> %d, input_pending=%d",
2754
80
              alive, *input_pending);
2755
80
  CF_DATA_RESTORE(cf, save);
2756
80
  return alive;
2757
80
}
2758
2759
static CURLcode cf_h2_keep_alive(struct Curl_cfilter *cf,
2760
                                 struct Curl_easy *data)
2761
0
{
2762
0
  CURLcode result;
2763
0
  struct cf_call_data save;
2764
2765
0
  CF_DATA_SAVE(save, cf, data);
2766
0
  result = http2_send_ping(cf, data);
2767
0
  CF_DATA_RESTORE(cf, save);
2768
0
  return result;
2769
0
}
2770
2771
static CURLcode cf_h2_query(struct Curl_cfilter *cf,
2772
                            struct Curl_easy *data,
2773
                            int query, int *pres1, void *pres2)
2774
61.8k
{
2775
61.8k
  struct cf_h2_ctx *ctx = cf->ctx;
2776
61.8k
  struct cf_call_data save;
2777
61.8k
  size_t effective_max;
2778
2779
61.8k
  switch(query) {
2780
662
  case CF_QUERY_MAX_CONCURRENT:
2781
662
    DEBUGASSERT(pres1);
2782
2783
662
    CF_DATA_SAVE(save, cf, data);
2784
662
    if(!Curl_ngh2_valid(&ctx->ngh2) ||
2785
535
       !Curl_ngh2_session_check_request_allowed(&ctx->ngh2)) {
2786
      /* the limit is what we have in use right now */
2787
138
      effective_max = CF_CONN(cf)->attached_xfers;
2788
138
    }
2789
524
    else {
2790
524
      effective_max = ctx->max_concurrent_streams;
2791
524
    }
2792
662
    *pres1 = (effective_max > INT_MAX) ? INT_MAX : (int)effective_max;
2793
662
    CF_DATA_RESTORE(cf, save);
2794
662
    return CURLE_OK;
2795
433
  case CF_QUERY_STREAM_ERROR: {
2796
433
    struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
2797
433
    *pres1 = stream ? (int)stream->error : 0;
2798
433
    return CURLE_OK;
2799
662
  }
2800
45.4k
  case CF_QUERY_NEED_FLUSH: {
2801
45.4k
    struct h2_stream_ctx *stream = H2_STREAM_CTX(ctx, data);
2802
45.4k
    if(!Curl_bufq_is_empty(&ctx->outbufq) ||
2803
35.4k
       (stream && !Curl_bufq_is_empty(&stream->sendbuf))) {
2804
16.2k
      *pres1 = TRUE;
2805
16.2k
      return CURLE_OK;
2806
16.2k
    }
2807
29.1k
    break;
2808
45.4k
  }
2809
29.1k
  case CF_QUERY_HTTP_VERSION:
2810
11.9k
    *pres1 = 20;
2811
11.9k
    return CURLE_OK;
2812
3.35k
  default:
2813
3.35k
    break;
2814
61.8k
  }
2815
32.5k
  return cf->next ?
2816
32.5k
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
2817
32.5k
    CURLE_UNKNOWN_OPTION;
2818
61.8k
}
2819
2820
struct Curl_cftype Curl_cft_nghttp2 = {
2821
  "HTTP/2",
2822
  CF_TYPE_MULTIPLEX | CF_TYPE_HTTP | CF_TYPE_SAFE_EOS,
2823
  CURL_LOG_LVL_NONE,
2824
  cf_h2_destroy,
2825
  cf_h2_connect,
2826
  cf_h2_shutdown,
2827
  cf_h2_adjust_pollset,
2828
  cf_h2_data_pending,
2829
  cf_h2_send,
2830
  cf_h2_recv,
2831
  cf_h2_cntrl,
2832
  cf_h2_is_alive,
2833
  cf_h2_keep_alive,
2834
  cf_h2_query,
2835
};
2836
2837
static CURLcode http2_cfilter_add(struct Curl_cfilter **pcf,
2838
                                  struct Curl_cfchain *chain,
2839
                                  bool via_h1_upgrade)
2840
14
{
2841
14
  struct Curl_cfilter *cf = NULL;
2842
14
  struct cf_h2_ctx *ctx;
2843
14
  CURLcode result = CURLE_OUT_OF_MEMORY;
2844
2845
14
  ctx = curlx_calloc(1, sizeof(*ctx));
2846
14
  if(!ctx)
2847
0
    goto out;
2848
14
  cf_h2_ctx_init(ctx, via_h1_upgrade);
2849
2850
14
  result = Curl_cf_create(&cf, &Curl_cft_nghttp2, ctx);
2851
14
  if(result)
2852
1
    goto out;
2853
2854
13
  ctx = NULL;
2855
13
  Curl_cfchain_add(chain, cf);
2856
2857
14
out:
2858
14
  if(result)
2859
1
    cf_h2_ctx_free(ctx);
2860
14
  *pcf = result ? NULL : cf;
2861
14
  return result;
2862
13
}
2863
2864
static CURLcode http2_cfilter_insert_after(struct Curl_cfilter *cf,
2865
                                           struct Curl_easy *data,
2866
                                           bool via_h1_upgrade)
2867
11.4k
{
2868
11.4k
  struct Curl_cfilter *cf_h2 = NULL;
2869
11.4k
  struct cf_h2_ctx *ctx;
2870
11.4k
  CURLcode result = CURLE_OUT_OF_MEMORY;
2871
2872
11.4k
  (void)data;
2873
11.4k
  ctx = curlx_calloc(1, sizeof(*ctx));
2874
11.4k
  if(!ctx)
2875
0
    goto out;
2876
11.4k
  cf_h2_ctx_init(ctx, via_h1_upgrade);
2877
2878
11.4k
  result = Curl_cf_create(&cf_h2, &Curl_cft_nghttp2, ctx);
2879
11.4k
  if(result)
2880
1
    goto out;
2881
2882
11.4k
  ctx = NULL;
2883
11.4k
  Curl_conn_cf_insert_after(cf, cf_h2);
2884
2885
11.4k
out:
2886
11.4k
  if(result)
2887
1
    cf_h2_ctx_free(ctx);
2888
11.4k
  return result;
2889
11.4k
}
2890
2891
bool Curl_http2_may_switch_at(struct Curl_easy *data, struct Curl_cfilter *cf)
2892
28.9k
{
2893
28.9k
  if(Curl_conn_cf_http_version(data, cf) < 20 &&
2894
28.9k
     (data->state.http_neg.wanted & CURL_HTTP_V2x) &&
2895
25.4k
     data->state.http_neg.h2_prior_knowledge) {
2896
11.4k
#ifndef CURL_DISABLE_PROXY
2897
11.4k
    if(data->conn->bits.origin_is_proxy) {
2898
0
      infof(data, "Ignoring HTTP/2 prior knowledge due to proxy");
2899
0
      return FALSE;
2900
0
    }
2901
11.4k
#endif
2902
11.4k
    return TRUE;
2903
11.4k
  }
2904
17.5k
  return FALSE;
2905
28.9k
}
2906
2907
CURLcode Curl_http2_switch_at(struct Curl_easy *data, struct Curl_cfilter *cf)
2908
11.4k
{
2909
11.4k
  DEBUGASSERT(Curl_conn_cf_http_version(data, cf) < 20);
2910
11.4k
  return http2_cfilter_insert_after(cf, data, FALSE);
2911
11.4k
}
2912
2913
CURLcode Curl_http2_upgrade(struct Curl_easy *data,
2914
                            const char *mem, size_t nread)
2915
14
{
2916
14
  struct connectdata *conn = data->conn;
2917
14
  struct Curl_cfilter *cf;
2918
14
  struct cf_h2_ctx *ctx;
2919
14
  CURLcode result;
2920
2921
14
  DEBUGASSERT(Curl_conn_http_version(data, conn) < 20);
2922
2923
14
  result = http2_cfilter_add(&cf, &conn->chain, TRUE);
2924
14
  if(result)
2925
1
    return result;
2926
13
  CURL_TRC_CF(data, cf, "upgrading connection to HTTP/2");
2927
2928
13
  DEBUGASSERT(cf->cft == &Curl_cft_nghttp2);
2929
13
  ctx = cf->ctx;
2930
2931
13
  data->req.httpversion_sent = 20; /* it is an h2 request now */
2932
13
  data->req.header = TRUE; /* we expect the real response to come in h2 */
2933
13
  data->req.headerline = 0; /* restart the header line counter */
2934
2935
13
  if(nread > 0) {
2936
    /* Remaining data from the protocol switch reply is already using
2937
     * the switched protocol, ie. HTTP/2. We add that to the network
2938
     * inbufq. */
2939
10
    size_t copied;
2940
2941
10
    result = Curl_bufq_write(&ctx->inbufq,
2942
10
                             (const unsigned char *)mem, nread, &copied);
2943
10
    if(result) {
2944
1
      failf(data, "error on copying HTTP Upgrade response: %d", (int)result);
2945
1
      return CURLE_RECV_ERROR;
2946
1
    }
2947
9
    if(copied < nread) {
2948
0
      failf(data, "connection buffer size could not take all data "
2949
0
            "from HTTP Upgrade response header: copied=%zu, datalen=%zu",
2950
0
            copied, nread);
2951
0
      return CURLE_HTTP2;
2952
0
    }
2953
9
    infof(data, "Copied HTTP/2 data in stream buffer to connection buffer"
2954
9
          " after upgrade: len=%zu", nread);
2955
9
  }
2956
2957
12
  if(cf->next) {
2958
12
    bool done;
2959
12
    result = Curl_conn_cf_connect(cf, data, &done);
2960
12
    if(!result)
2961
7
      cf->cft->cntrl(cf, data, CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
2962
12
  }
2963
12
  return result;
2964
13
}
2965
2966
/* Only call this function for a transfer that already got an HTTP/2
2967
   CURLE_HTTP2_STREAM error! */
2968
bool Curl_h2_http_1_1_error(struct Curl_easy *data)
2969
433
{
2970
433
  if(Curl_conn_http_version(data, data->conn) == 20) {
2971
433
    int err = Curl_conn_get_stream_error(data, data->conn);
2972
433
    return err == NGHTTP2_HTTP_1_1_REQUIRED;
2973
433
  }
2974
0
  return FALSE;
2975
433
}
2976
2977
void *Curl_nghttp2_malloc(size_t size, void *user_data)
2978
377k
{
2979
377k
  (void)user_data;
2980
377k
  return Curl_cmalloc(size);
2981
377k
}
2982
2983
void Curl_nghttp2_free(void *ptr, void *user_data)
2984
662k
{
2985
662k
  (void)user_data;
2986
662k
  Curl_cfree(ptr);
2987
662k
}
2988
2989
void *Curl_nghttp2_calloc(size_t nmemb, size_t size, void *user_data)
2990
22.6k
{
2991
22.6k
  (void)user_data;
2992
22.6k
  return Curl_ccalloc(nmemb, size);
2993
22.6k
}
2994
2995
void *Curl_nghttp2_realloc(void *ptr, size_t size, void *user_data)
2996
15.0k
{
2997
15.0k
  (void)user_data;
2998
15.0k
  return Curl_crealloc(ptr, size);
2999
15.0k
}
3000
3001
#else /* CURL_DISABLE_HTTP || !USE_NGHTTP2 */
3002
3003
/* Satisfy external references even if http2 is not compiled in. */
3004
3005
char *curl_pushheader_bynum(struct curl_pushheaders *h, size_t num)
3006
{
3007
  (void)h;
3008
  (void)num;
3009
  return NULL;
3010
}
3011
3012
char *curl_pushheader_byname(struct curl_pushheaders *h, const char *name)
3013
{
3014
  (void)h;
3015
  (void)name;
3016
  return NULL;
3017
}
3018
3019
#endif /* !CURL_DISABLE_HTTP && USE_NGHTTP2 */