Coverage Report

Created: 2025-12-14 06:23

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