Coverage Report

Created: 2025-08-24 06:12

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