Coverage Report

Created: 2026-08-08 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nghttp2/lib/nghttp2_session.c
Line
Count
Source
1
/*
2
 * nghttp2 - HTTP/2 C Library
3
 *
4
 * Copyright (c) 2012 Tatsuhiro Tsujikawa
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining
7
 * a copy of this software and associated documentation files (the
8
 * "Software"), to deal in the Software without restriction, including
9
 * without limitation the rights to use, copy, modify, merge, publish,
10
 * distribute, sublicense, and/or sell copies of the Software, and to
11
 * permit persons to whom the Software is furnished to do so, subject to
12
 * the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 */
25
#include "nghttp2_session.h"
26
27
#include <string.h>
28
#include <stddef.h>
29
#include <stdio.h>
30
#include <assert.h>
31
#include <stdarg.h>
32
33
#include "nghttp2_helper.h"
34
#include "nghttp2_net.h"
35
#include "nghttp2_priority_spec.h"
36
#include "nghttp2_option.h"
37
#include "nghttp2_http.h"
38
#include "nghttp2_pq.h"
39
#include "nghttp2_extpri.h"
40
#include "nghttp2_time.h"
41
#include "nghttp2_debug.h"
42
#include "nghttp2_submit.h"
43
44
nghttp2_stream nghttp2_stream_root;
45
46
/*
47
 * Returns non-zero if the number of outgoing opened streams is larger
48
 * than or equal to
49
 * remote_settings.max_concurrent_streams.
50
 */
51
static int
52
0
session_is_outgoing_concurrent_streams_max(nghttp2_session *session) {
53
0
  return session->remote_settings.max_concurrent_streams <=
54
0
         session->num_outgoing_streams;
55
0
}
56
57
/*
58
 * Returns non-zero if the number of incoming opened streams is larger
59
 * than or equal to
60
 * local_settings.max_concurrent_streams.
61
 */
62
static int
63
0
session_is_incoming_concurrent_streams_max(nghttp2_session *session) {
64
0
  return session->local_settings.max_concurrent_streams <=
65
0
         session->num_incoming_streams;
66
0
}
67
68
/*
69
 * Returns non-zero if the number of incoming opened streams is larger
70
 * than or equal to
71
 * session->pending_local_max_concurrent_stream.
72
 */
73
static int
74
0
session_is_incoming_concurrent_streams_pending_max(nghttp2_session *session) {
75
0
  return session->pending_local_max_concurrent_stream <=
76
0
         session->num_incoming_streams;
77
0
}
78
79
/*
80
 * Returns non-zero if |lib_error| is non-fatal error.
81
 */
82
0
static int is_non_fatal(int lib_error_code) {
83
0
  return lib_error_code < 0 && lib_error_code > NGHTTP2_ERR_FATAL;
84
0
}
85
86
0
int nghttp2_is_fatal(int lib_error_code) {
87
0
  return lib_error_code < NGHTTP2_ERR_FATAL;
88
0
}
89
90
0
static int session_enforce_http_messaging(nghttp2_session *session) {
91
0
  return (session->opt_flags & NGHTTP2_OPTMASK_NO_HTTP_MESSAGING) == 0;
92
0
}
93
94
/*
95
 * Returns nonzero if |frame| is trailer headers.
96
 */
97
static int session_trailer_headers(nghttp2_session *session,
98
                                   nghttp2_stream *stream,
99
0
                                   nghttp2_frame *frame) {
100
0
  if (!stream || frame->hd.type != NGHTTP2_HEADERS) {
101
0
    return 0;
102
0
  }
103
0
  if (session->server) {
104
0
    return frame->headers.cat == NGHTTP2_HCAT_HEADERS;
105
0
  }
106
107
0
  return frame->headers.cat == NGHTTP2_HCAT_HEADERS &&
108
0
         (stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) == 0;
109
0
}
110
111
/* Returns nonzero if the |stream| is in reserved(remote) state */
112
static int state_reserved_remote(nghttp2_session *session,
113
0
                                 nghttp2_stream *stream) {
114
0
  return stream->state == NGHTTP2_STREAM_RESERVED &&
115
0
         !nghttp2_session_is_my_stream_id(session, stream->stream_id);
116
0
}
117
118
/* Returns nonzero if the |stream| is in reserved(local) state */
119
static int state_reserved_local(nghttp2_session *session,
120
0
                                nghttp2_stream *stream) {
121
0
  return stream->state == NGHTTP2_STREAM_RESERVED &&
122
0
         nghttp2_session_is_my_stream_id(session, stream->stream_id);
123
0
}
124
125
/*
126
 * Checks whether received stream_id is valid.  This function returns
127
 * 1 if it succeeds, or 0.
128
 */
129
static int session_is_new_peer_stream_id(nghttp2_session *session,
130
0
                                         int32_t stream_id) {
131
0
  return stream_id != 0 &&
132
0
         !nghttp2_session_is_my_stream_id(session, stream_id) &&
133
0
         session->last_recv_stream_id < stream_id;
134
0
}
135
136
static int session_detect_idle_stream(nghttp2_session *session,
137
0
                                      int32_t stream_id) {
138
  /* Assume that stream object with stream_id does not exist */
139
0
  if (nghttp2_session_is_my_stream_id(session, stream_id)) {
140
0
    if (session->last_sent_stream_id < stream_id) {
141
0
      return 1;
142
0
    }
143
0
    return 0;
144
0
  }
145
0
  if (session_is_new_peer_stream_id(session, stream_id)) {
146
0
    return 1;
147
0
  }
148
0
  return 0;
149
0
}
150
151
0
static int check_ext_type_set(const uint8_t *ext_types, uint8_t type) {
152
0
  return (ext_types[type / 8] & (1 << (type & 0x7))) > 0;
153
0
}
154
155
static int session_call_error_callback(nghttp2_session *session,
156
                                       int lib_error_code, const char *fmt,
157
0
                                       ...) {
158
0
  size_t bufsize;
159
0
  va_list ap;
160
0
  char *buf;
161
0
  int rv;
162
0
  nghttp2_mem *mem;
163
164
0
  if (!session->callbacks.error_callback &&
165
0
      !session->callbacks.error_callback2) {
166
0
    return 0;
167
0
  }
168
169
0
  mem = &session->mem;
170
171
0
  va_start(ap, fmt);
172
0
  rv = vsnprintf(NULL, 0, fmt, ap);
173
0
  va_end(ap);
174
175
0
  if (rv < 0) {
176
0
    return NGHTTP2_ERR_NOMEM;
177
0
  }
178
179
0
  bufsize = (size_t)(rv + 1);
180
181
0
  buf = nghttp2_mem_malloc(mem, bufsize);
182
0
  if (buf == NULL) {
183
0
    return NGHTTP2_ERR_NOMEM;
184
0
  }
185
186
0
  va_start(ap, fmt);
187
0
  rv = vsnprintf(buf, bufsize, fmt, ap);
188
0
  va_end(ap);
189
190
0
  if (rv < 0) {
191
0
    nghttp2_mem_free(mem, buf);
192
    /* vsnprintf may return error because of various things we can
193
       imagine, but typically we don't want to drop session just for
194
       debug callback. */
195
0
    DEBUGF("error_callback: vsnprintf failed. The template was %s\n", fmt);
196
0
    return 0;
197
0
  }
198
199
0
  if (session->callbacks.error_callback2) {
200
0
    rv = session->callbacks.error_callback2(session, lib_error_code, buf,
201
0
                                            (size_t)rv, session->user_data);
202
0
  } else {
203
0
    rv = session->callbacks.error_callback(session, buf, (size_t)rv,
204
0
                                           session->user_data);
205
0
  }
206
207
0
  nghttp2_mem_free(mem, buf);
208
209
0
  if (rv != 0) {
210
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
211
0
  }
212
213
0
  return 0;
214
0
}
215
216
static int session_terminate_session(nghttp2_session *session,
217
                                     int32_t last_stream_id,
218
0
                                     uint32_t error_code, const char *reason) {
219
0
  int rv;
220
0
  const uint8_t *debug_data;
221
0
  size_t debug_datalen;
222
223
0
  if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
224
0
    return 0;
225
0
  }
226
227
  /* Ignore all incoming frames because we are going to tear down the
228
     session. */
229
0
  session->iframe.state = NGHTTP2_IB_IGN_ALL;
230
231
0
  if (reason == NULL) {
232
0
    debug_data = NULL;
233
0
    debug_datalen = 0;
234
0
  } else {
235
0
    debug_data = (const uint8_t *)reason;
236
0
    debug_datalen = strlen(reason);
237
0
  }
238
239
0
  rv =
240
0
    nghttp2_session_add_goaway(session, last_stream_id, error_code, debug_data,
241
0
                               debug_datalen, NGHTTP2_GOAWAY_AUX_TERM_ON_SEND);
242
243
0
  if (rv != 0) {
244
0
    return rv;
245
0
  }
246
247
0
  session->goaway_flags |= NGHTTP2_GOAWAY_TERM_ON_SEND;
248
249
0
  return 0;
250
0
}
251
252
int nghttp2_session_terminate_session(nghttp2_session *session,
253
0
                                      uint32_t error_code) {
254
0
  return session_terminate_session(session, session->last_proc_stream_id,
255
0
                                   error_code, NULL);
256
0
}
257
258
int nghttp2_session_terminate_session2(nghttp2_session *session,
259
                                       int32_t last_stream_id,
260
0
                                       uint32_t error_code) {
261
0
  return session_terminate_session(session, last_stream_id, error_code, NULL);
262
0
}
263
264
int nghttp2_session_terminate_session_with_reason(nghttp2_session *session,
265
                                                  uint32_t error_code,
266
0
                                                  const char *reason) {
267
0
  return session_terminate_session(session, session->last_proc_stream_id,
268
0
                                   error_code, reason);
269
0
}
270
271
int nghttp2_session_is_my_stream_id(nghttp2_session *session,
272
0
                                    int32_t stream_id) {
273
0
  int rem;
274
0
  if (stream_id == 0) {
275
0
    return 0;
276
0
  }
277
0
  rem = stream_id & 0x1;
278
0
  if (session->server) {
279
0
    return rem == 0;
280
0
  }
281
0
  return rem == 1;
282
0
}
283
284
nghttp2_stream *nghttp2_session_get_stream(nghttp2_session *session,
285
0
                                           int32_t stream_id) {
286
0
  nghttp2_stream *stream;
287
288
0
  stream = (nghttp2_stream *)nghttp2_map_find(&session->streams, stream_id);
289
290
0
  if (stream == NULL || (stream->flags & NGHTTP2_STREAM_FLAG_CLOSED) ||
291
0
      stream->state == NGHTTP2_STREAM_IDLE) {
292
0
    return NULL;
293
0
  }
294
295
0
  return stream;
296
0
}
297
298
nghttp2_stream *nghttp2_session_get_stream_raw(nghttp2_session *session,
299
0
                                               int32_t stream_id) {
300
0
  return (nghttp2_stream *)nghttp2_map_find(&session->streams, stream_id);
301
0
}
302
303
0
static void session_inbound_frame_reset(nghttp2_session *session) {
304
0
  nghttp2_inbound_frame *iframe = &session->iframe;
305
0
  nghttp2_mem *mem = &session->mem;
306
  /* A bit risky code, since if this function is called from
307
     nghttp2_session_new(), we rely on the fact that
308
     iframe->frame.hd.type is 0, so that no free is performed. */
309
0
  switch (iframe->frame.hd.type) {
310
0
  case NGHTTP2_DATA:
311
0
    break;
312
0
  case NGHTTP2_HEADERS:
313
0
    nghttp2_frame_headers_free(&iframe->frame.headers, mem);
314
0
    break;
315
0
  case NGHTTP2_PRIORITY:
316
0
    nghttp2_frame_priority_free(&iframe->frame.priority);
317
0
    break;
318
0
  case NGHTTP2_RST_STREAM:
319
0
    nghttp2_frame_rst_stream_free(&iframe->frame.rst_stream);
320
0
    break;
321
0
  case NGHTTP2_SETTINGS:
322
0
    nghttp2_frame_settings_free(&iframe->frame.settings, mem);
323
324
0
    nghttp2_mem_free(mem, iframe->iv);
325
326
0
    iframe->iv = NULL;
327
0
    iframe->niv = 0;
328
0
    iframe->max_niv = 0;
329
330
0
    break;
331
0
  case NGHTTP2_PUSH_PROMISE:
332
0
    nghttp2_frame_push_promise_free(&iframe->frame.push_promise, mem);
333
0
    break;
334
0
  case NGHTTP2_PING:
335
0
    nghttp2_frame_ping_free(&iframe->frame.ping);
336
0
    break;
337
0
  case NGHTTP2_GOAWAY:
338
0
    nghttp2_frame_goaway_free(&iframe->frame.goaway, mem);
339
0
    break;
340
0
  case NGHTTP2_WINDOW_UPDATE:
341
0
    nghttp2_frame_window_update_free(&iframe->frame.window_update);
342
0
    break;
343
0
  default:
344
    /* extension frame */
345
0
    if (check_ext_type_set(session->user_recv_ext_types,
346
0
                           iframe->frame.hd.type)) {
347
0
      nghttp2_frame_extension_free(&iframe->frame.ext);
348
0
    } else {
349
0
      switch (iframe->frame.hd.type) {
350
0
      case NGHTTP2_ALTSVC:
351
0
        if ((session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ALTSVC) == 0) {
352
0
          break;
353
0
        }
354
0
        nghttp2_frame_altsvc_free(&iframe->frame.ext, mem);
355
0
        break;
356
0
      case NGHTTP2_ORIGIN:
357
0
        if ((session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ORIGIN) == 0) {
358
0
          break;
359
0
        }
360
0
        nghttp2_frame_origin_free(&iframe->frame.ext, mem);
361
0
        break;
362
0
      case NGHTTP2_PRIORITY_UPDATE:
363
0
        if ((session->builtin_recv_ext_types &
364
0
             NGHTTP2_TYPEMASK_PRIORITY_UPDATE) == 0) {
365
0
          break;
366
0
        }
367
        /* Do not call nghttp2_frame_priority_update_free, because all
368
           fields point to sbuf. */
369
0
        break;
370
0
      }
371
0
    }
372
373
0
    break;
374
0
  }
375
376
0
  memset(&iframe->frame, 0, sizeof(nghttp2_frame));
377
0
  memset(&iframe->ext_frame_payload, 0, sizeof(nghttp2_ext_frame_payload));
378
379
0
  iframe->state = NGHTTP2_IB_READ_HEAD;
380
381
0
  nghttp2_buf_wrap_init(&iframe->sbuf, iframe->raw_sbuf,
382
0
                        sizeof(iframe->raw_sbuf));
383
0
  iframe->sbuf.mark += NGHTTP2_FRAME_HDLEN;
384
385
0
  nghttp2_buf_free(&iframe->lbuf, mem);
386
0
  nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
387
388
0
  iframe->raw_lbuf = NULL;
389
390
0
  iframe->payloadleft = 0;
391
0
  iframe->padlen = 0;
392
0
}
393
394
0
static void init_settings(nghttp2_settings_storage *settings) {
395
0
  *settings = (nghttp2_settings_storage){
396
0
    .header_table_size = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE,
397
0
    .enable_push = 1,
398
0
    .max_concurrent_streams = NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS,
399
0
    .initial_window_size = NGHTTP2_INITIAL_WINDOW_SIZE,
400
0
    .max_frame_size = NGHTTP2_MAX_FRAME_SIZE_MIN,
401
0
    .max_header_list_size = UINT32_MAX,
402
0
    .no_rfc7540_priorities = UINT32_MAX,
403
0
  };
404
0
}
405
406
static void active_outbound_item_reset(nghttp2_active_outbound_item *aob,
407
0
                                       nghttp2_mem *mem) {
408
0
  DEBUGF("send: reset nghttp2_active_outbound_item\n");
409
0
  DEBUGF("send: aob->item = %p\n", aob->item);
410
0
  nghttp2_outbound_item_free(aob->item, mem);
411
0
  nghttp2_mem_free(mem, aob->item);
412
0
  aob->item = NULL;
413
0
  nghttp2_bufs_reset(&aob->framebufs);
414
0
  aob->state = NGHTTP2_OB_POP_ITEM;
415
0
}
416
417
0
#define NGHTTP2_STREAM_MAX_CYCLE_GAP ((uint64_t)NGHTTP2_MAX_FRAME_SIZE_MAX)
418
419
0
static int stream_less(const void *lhsx, const void *rhsx) {
420
0
  const nghttp2_stream *lhs, *rhs;
421
422
0
  lhs = nghttp2_struct_of(lhsx, nghttp2_stream, pq_entry);
423
0
  rhs = nghttp2_struct_of(rhsx, nghttp2_stream, pq_entry);
424
425
0
  if (lhs->cycle == rhs->cycle) {
426
0
    return lhs->seq < rhs->seq;
427
0
  }
428
429
0
  return rhs->cycle - lhs->cycle <= NGHTTP2_STREAM_MAX_CYCLE_GAP;
430
0
}
431
432
int nghttp2_enable_strict_preface = 1;
433
434
static int session_new(nghttp2_session **session_ptr,
435
                       const nghttp2_session_callbacks *callbacks,
436
                       void *user_data, int server,
437
0
                       const nghttp2_option *option, nghttp2_mem *mem) {
438
0
  int rv;
439
0
  size_t nbuffer;
440
0
  size_t max_deflate_dynamic_table_size =
441
0
    NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE;
442
0
  size_t i;
443
0
  uint64_t map_seed;
444
445
0
  if (mem == NULL) {
446
0
    mem = nghttp2_mem_default();
447
0
  }
448
449
0
  *session_ptr = nghttp2_mem_calloc(mem, 1, sizeof(nghttp2_session));
450
0
  if (*session_ptr == NULL) {
451
0
    rv = NGHTTP2_ERR_NOMEM;
452
0
    goto fail_session;
453
0
  }
454
455
0
  (*session_ptr)->mem = *mem;
456
0
  mem = &(*session_ptr)->mem;
457
458
  /* next_stream_id is initialized in either
459
     nghttp2_session_client_new2 or nghttp2_session_server_new2 */
460
461
0
  (*session_ptr)->remote_window_size = NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
462
0
  (*session_ptr)->recv_window_size = 0;
463
0
  (*session_ptr)->consumed_size = 0;
464
0
  (*session_ptr)->recv_reduction = 0;
465
0
  (*session_ptr)->local_window_size = NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
466
467
0
  (*session_ptr)->goaway_flags = NGHTTP2_GOAWAY_NONE;
468
0
  (*session_ptr)->local_last_stream_id = (1u << 31) - 1;
469
0
  (*session_ptr)->remote_last_stream_id = (1u << 31) - 1;
470
471
0
  (*session_ptr)->pending_local_max_concurrent_stream =
472
0
    NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS;
473
0
  (*session_ptr)->pending_enable_push = 1;
474
0
  (*session_ptr)->pending_no_rfc7540_priorities = UINT8_MAX;
475
476
0
  nghttp2_ratelim_init(&(*session_ptr)->stream_reset_ratelim,
477
0
                       NGHTTP2_DEFAULT_STREAM_RESET_BURST,
478
0
                       NGHTTP2_DEFAULT_STREAM_RESET_RATE);
479
480
0
  nghttp2_ratelim_init(&(*session_ptr)->glitch_ratelim,
481
0
                       NGHTTP2_DEFAULT_GLITCH_BURST,
482
0
                       NGHTTP2_DEFAULT_GLITCH_RATE);
483
484
0
  if (server) {
485
0
    (*session_ptr)->server = 1;
486
0
  }
487
488
0
  init_settings(&(*session_ptr)->remote_settings);
489
0
  init_settings(&(*session_ptr)->local_settings);
490
491
0
  (*session_ptr)->max_incoming_reserved_streams =
492
0
    NGHTTP2_MAX_INCOMING_RESERVED_STREAMS;
493
494
  /* Limit max outgoing concurrent streams to sensible value */
495
0
  (*session_ptr)->remote_settings.max_concurrent_streams = 100;
496
497
0
  (*session_ptr)->max_send_header_block_length = NGHTTP2_MAX_HEADERSLEN;
498
0
  (*session_ptr)->max_outbound_ack = NGHTTP2_DEFAULT_MAX_OBQ_FLOOD_ITEM;
499
0
  (*session_ptr)->max_settings = NGHTTP2_DEFAULT_MAX_SETTINGS;
500
0
  (*session_ptr)->max_continuations = NGHTTP2_DEFAULT_MAX_CONTINUATIONS;
501
0
  (*session_ptr)->max_outbound_queue_size =
502
0
    NGHTTP2_DEFAULT_MAX_OUTBOUND_QUEUE_SIZE;
503
504
0
  if (option) {
505
0
    if ((option->opt_set_mask & NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE) &&
506
0
        option->no_auto_window_update) {
507
0
      (*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE;
508
0
    }
509
510
0
    if (option->opt_set_mask & NGHTTP2_OPT_PEER_MAX_CONCURRENT_STREAMS) {
511
0
      (*session_ptr)->remote_settings.max_concurrent_streams =
512
0
        option->peer_max_concurrent_streams;
513
0
    }
514
515
0
    if (option->opt_set_mask & NGHTTP2_OPT_MAX_RESERVED_REMOTE_STREAMS) {
516
0
      (*session_ptr)->max_incoming_reserved_streams =
517
0
        option->max_reserved_remote_streams;
518
0
    }
519
520
0
    if ((option->opt_set_mask & NGHTTP2_OPT_NO_RECV_CLIENT_MAGIC) &&
521
0
        option->no_recv_client_magic) {
522
0
      (*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_RECV_CLIENT_MAGIC;
523
0
    }
524
525
0
    if ((option->opt_set_mask & NGHTTP2_OPT_NO_HTTP_MESSAGING) &&
526
0
        option->no_http_messaging) {
527
0
      (*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_HTTP_MESSAGING;
528
0
    }
529
530
0
    if (option->opt_set_mask & NGHTTP2_OPT_USER_RECV_EXT_TYPES) {
531
0
      memcpy((*session_ptr)->user_recv_ext_types, option->user_recv_ext_types,
532
0
             sizeof((*session_ptr)->user_recv_ext_types));
533
0
    }
534
535
0
    if (option->opt_set_mask & NGHTTP2_OPT_BUILTIN_RECV_EXT_TYPES) {
536
0
      (*session_ptr)->builtin_recv_ext_types = option->builtin_recv_ext_types;
537
0
    }
538
539
0
    if ((option->opt_set_mask & NGHTTP2_OPT_NO_AUTO_PING_ACK) &&
540
0
        option->no_auto_ping_ack) {
541
0
      (*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_AUTO_PING_ACK;
542
0
    }
543
544
0
    if (option->opt_set_mask & NGHTTP2_OPT_MAX_SEND_HEADER_BLOCK_LENGTH) {
545
0
      (*session_ptr)->max_send_header_block_length =
546
0
        option->max_send_header_block_length;
547
0
    }
548
549
0
    if (option->opt_set_mask & NGHTTP2_OPT_MAX_DEFLATE_DYNAMIC_TABLE_SIZE) {
550
0
      max_deflate_dynamic_table_size = option->max_deflate_dynamic_table_size;
551
0
    }
552
553
0
    if (option->opt_set_mask & NGHTTP2_OPT_MAX_OUTBOUND_ACK) {
554
0
      (*session_ptr)->max_outbound_ack = option->max_outbound_ack;
555
0
    }
556
557
0
    if ((option->opt_set_mask & NGHTTP2_OPT_MAX_SETTINGS) &&
558
0
        option->max_settings) {
559
0
      (*session_ptr)->max_settings = option->max_settings;
560
0
    }
561
562
0
    if ((option->opt_set_mask &
563
0
         NGHTTP2_OPT_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION) &&
564
0
        option->no_rfc9113_leading_and_trailing_ws_validation) {
565
0
      (*session_ptr)->opt_flags |=
566
0
        NGHTTP2_OPTMASK_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION;
567
0
    }
568
569
0
    if (option->opt_set_mask & NGHTTP2_OPT_STREAM_RESET_RATE_LIMIT) {
570
0
      nghttp2_ratelim_init(&(*session_ptr)->stream_reset_ratelim,
571
0
                           option->stream_reset_burst,
572
0
                           option->stream_reset_rate);
573
0
    }
574
575
0
    if (option->opt_set_mask & NGHTTP2_OPT_MAX_CONTINUATIONS) {
576
0
      (*session_ptr)->max_continuations = option->max_continuations;
577
0
    }
578
579
0
    if (option->opt_set_mask & NGHTTP2_OPT_GLITCH_RATE_LIMIT) {
580
0
      nghttp2_ratelim_init(&(*session_ptr)->glitch_ratelim,
581
0
                           option->glitch_burst, option->glitch_rate);
582
0
    }
583
584
0
    if (option->opt_set_mask & NGHTTP2_OPT_MAX_OUTBOUND_QUEUE_SIZE) {
585
0
      (*session_ptr)->max_outbound_queue_size = option->max_outbound_queue_size;
586
0
    }
587
0
  }
588
589
0
  rv = nghttp2_hd_deflate_init2(&(*session_ptr)->hd_deflater,
590
0
                                max_deflate_dynamic_table_size, mem);
591
0
  if (rv != 0) {
592
0
    goto fail_hd_deflater;
593
0
  }
594
0
  rv = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater, mem);
595
0
  if (rv != 0) {
596
0
    goto fail_hd_inflater;
597
0
  }
598
599
0
  nbuffer = ((*session_ptr)->max_send_header_block_length +
600
0
             NGHTTP2_FRAMEBUF_CHUNKLEN - 1) /
601
0
            NGHTTP2_FRAMEBUF_CHUNKLEN;
602
603
0
  if (nbuffer == 0) {
604
0
    nbuffer = 1;
605
0
  }
606
607
  /* 1 for Pad Field. */
608
0
  rv = nghttp2_bufs_init3(&(*session_ptr)->aob.framebufs,
609
0
                          NGHTTP2_FRAMEBUF_CHUNKLEN, nbuffer, 1,
610
0
                          NGHTTP2_FRAME_HDLEN + 1, mem);
611
0
  if (rv != 0) {
612
0
    goto fail_aob_framebuf;
613
0
  }
614
615
0
  if (callbacks->rand_callback) {
616
0
    callbacks->rand_callback((uint8_t *)&map_seed, sizeof(map_seed));
617
0
  } else {
618
0
    map_seed = 0;
619
0
  }
620
621
0
  nghttp2_map_init(&(*session_ptr)->streams, map_seed, mem);
622
623
0
  active_outbound_item_reset(&(*session_ptr)->aob, mem);
624
625
0
  (*session_ptr)->callbacks = *callbacks;
626
0
  (*session_ptr)->user_data = user_data;
627
628
0
  session_inbound_frame_reset(*session_ptr);
629
630
0
  if (nghttp2_enable_strict_preface) {
631
0
    nghttp2_inbound_frame *iframe = &(*session_ptr)->iframe;
632
633
0
    if (server && ((*session_ptr)->opt_flags &
634
0
                   NGHTTP2_OPTMASK_NO_RECV_CLIENT_MAGIC) == 0) {
635
0
      iframe->state = NGHTTP2_IB_READ_CLIENT_MAGIC;
636
0
      iframe->payloadleft = NGHTTP2_CLIENT_MAGIC_LEN;
637
0
    } else {
638
0
      iframe->state = NGHTTP2_IB_READ_FIRST_SETTINGS;
639
0
    }
640
641
0
    if (!server) {
642
0
      (*session_ptr)->aob.state = NGHTTP2_OB_SEND_CLIENT_MAGIC;
643
0
      nghttp2_bufs_add(&(*session_ptr)->aob.framebufs, NGHTTP2_CLIENT_MAGIC,
644
0
                       NGHTTP2_CLIENT_MAGIC_LEN);
645
0
    }
646
0
  }
647
648
0
  for (i = 0; i < NGHTTP2_EXTPRI_URGENCY_LEVELS; ++i) {
649
0
    nghttp2_pq_init(&(*session_ptr)->sched[i].ob_data, stream_less, mem);
650
0
  }
651
652
0
  return 0;
653
654
0
fail_aob_framebuf:
655
0
  nghttp2_hd_inflate_free(&(*session_ptr)->hd_inflater);
656
0
fail_hd_inflater:
657
0
  nghttp2_hd_deflate_free(&(*session_ptr)->hd_deflater);
658
0
fail_hd_deflater:
659
0
  nghttp2_mem_free(mem, *session_ptr);
660
0
fail_session:
661
0
  return rv;
662
0
}
663
664
int nghttp2_session_client_new(nghttp2_session **session_ptr,
665
                               const nghttp2_session_callbacks *callbacks,
666
0
                               void *user_data) {
667
0
  return nghttp2_session_client_new3(session_ptr, callbacks, user_data, NULL,
668
0
                                     NULL);
669
0
}
670
671
int nghttp2_session_client_new2(nghttp2_session **session_ptr,
672
                                const nghttp2_session_callbacks *callbacks,
673
0
                                void *user_data, const nghttp2_option *option) {
674
0
  return nghttp2_session_client_new3(session_ptr, callbacks, user_data, option,
675
0
                                     NULL);
676
0
}
677
678
int nghttp2_session_client_new3(nghttp2_session **session_ptr,
679
                                const nghttp2_session_callbacks *callbacks,
680
                                void *user_data, const nghttp2_option *option,
681
0
                                nghttp2_mem *mem) {
682
0
  int rv;
683
0
  nghttp2_session *session;
684
685
0
  rv = session_new(&session, callbacks, user_data, 0, option, mem);
686
687
0
  if (rv != 0) {
688
0
    return rv;
689
0
  }
690
  /* IDs for use in client */
691
0
  session->next_stream_id = 1;
692
693
0
  *session_ptr = session;
694
695
0
  return 0;
696
0
}
697
698
int nghttp2_session_server_new(nghttp2_session **session_ptr,
699
                               const nghttp2_session_callbacks *callbacks,
700
0
                               void *user_data) {
701
0
  return nghttp2_session_server_new3(session_ptr, callbacks, user_data, NULL,
702
0
                                     NULL);
703
0
}
704
705
int nghttp2_session_server_new2(nghttp2_session **session_ptr,
706
                                const nghttp2_session_callbacks *callbacks,
707
0
                                void *user_data, const nghttp2_option *option) {
708
0
  return nghttp2_session_server_new3(session_ptr, callbacks, user_data, option,
709
0
                                     NULL);
710
0
}
711
712
int nghttp2_session_server_new3(nghttp2_session **session_ptr,
713
                                const nghttp2_session_callbacks *callbacks,
714
                                void *user_data, const nghttp2_option *option,
715
0
                                nghttp2_mem *mem) {
716
0
  int rv;
717
0
  nghttp2_session *session;
718
719
0
  rv = session_new(&session, callbacks, user_data, 1, option, mem);
720
721
0
  if (rv != 0) {
722
0
    return rv;
723
0
  }
724
  /* IDs for use in client */
725
0
  session->next_stream_id = 2;
726
727
0
  *session_ptr = session;
728
729
0
  return 0;
730
0
}
731
732
0
static int free_streams(void *entry, void *ptr) {
733
0
  nghttp2_session *session;
734
0
  nghttp2_stream *stream;
735
0
  nghttp2_outbound_item *item;
736
0
  nghttp2_mem *mem;
737
738
0
  session = (nghttp2_session *)ptr;
739
0
  mem = &session->mem;
740
0
  stream = (nghttp2_stream *)entry;
741
0
  item = stream->item;
742
743
0
  if (item && !item->queued && item != session->aob.item) {
744
0
    nghttp2_outbound_item_free(item, mem);
745
0
    nghttp2_mem_free(mem, item);
746
0
  }
747
748
0
  nghttp2_stream_free(stream);
749
0
  nghttp2_mem_free(mem, stream);
750
751
0
  return 0;
752
0
}
753
754
0
static void ob_q_free(nghttp2_outbound_queue *q, nghttp2_mem *mem) {
755
0
  nghttp2_outbound_item *item, *next;
756
0
  for (item = q->head; item;) {
757
0
    next = item->qnext;
758
0
    nghttp2_outbound_item_free(item, mem);
759
0
    nghttp2_mem_free(mem, item);
760
0
    item = next;
761
0
  }
762
0
}
763
764
static int inflight_settings_new(nghttp2_inflight_settings **settings_ptr,
765
                                 const nghttp2_settings_entry *iv, size_t niv,
766
0
                                 nghttp2_mem *mem) {
767
0
  *settings_ptr = nghttp2_mem_malloc(mem, sizeof(nghttp2_inflight_settings));
768
0
  if (!*settings_ptr) {
769
0
    return NGHTTP2_ERR_NOMEM;
770
0
  }
771
772
0
  if (niv > 0) {
773
0
    (*settings_ptr)->iv = nghttp2_frame_iv_copy(iv, niv, mem);
774
0
    if (!(*settings_ptr)->iv) {
775
0
      nghttp2_mem_free(mem, *settings_ptr);
776
0
      return NGHTTP2_ERR_NOMEM;
777
0
    }
778
0
  } else {
779
0
    (*settings_ptr)->iv = NULL;
780
0
  }
781
782
0
  (*settings_ptr)->niv = niv;
783
0
  (*settings_ptr)->next = NULL;
784
785
0
  return 0;
786
0
}
787
788
static void inflight_settings_del(nghttp2_inflight_settings *settings,
789
0
                                  nghttp2_mem *mem) {
790
0
  if (!settings) {
791
0
    return;
792
0
  }
793
794
0
  nghttp2_mem_free(mem, settings->iv);
795
0
  nghttp2_mem_free(mem, settings);
796
0
}
797
798
0
void nghttp2_session_del(nghttp2_session *session) {
799
0
  nghttp2_mem *mem;
800
0
  nghttp2_inflight_settings *settings;
801
0
  size_t i;
802
803
0
  if (session == NULL) {
804
0
    return;
805
0
  }
806
807
0
  mem = &session->mem;
808
809
0
  for (settings = session->inflight_settings_head; settings;) {
810
0
    nghttp2_inflight_settings *next = settings->next;
811
0
    inflight_settings_del(settings, mem);
812
0
    settings = next;
813
0
  }
814
815
0
  for (i = 0; i < NGHTTP2_EXTPRI_URGENCY_LEVELS; ++i) {
816
0
    nghttp2_pq_free(&session->sched[i].ob_data);
817
0
  }
818
819
  /* Have to free streams first, so that we can check
820
     stream->item->queued */
821
0
  nghttp2_map_each(&session->streams, free_streams, session);
822
0
  nghttp2_map_free(&session->streams);
823
824
0
  ob_q_free(&session->ob_urgent, mem);
825
0
  ob_q_free(&session->ob_reg, mem);
826
0
  ob_q_free(&session->ob_syn, mem);
827
828
0
  active_outbound_item_reset(&session->aob, mem);
829
0
  session_inbound_frame_reset(session);
830
0
  nghttp2_hd_deflate_free(&session->hd_deflater);
831
0
  nghttp2_hd_inflate_free(&session->hd_inflater);
832
0
  nghttp2_bufs_free(&session->aob.framebufs);
833
0
  nghttp2_mem_free(mem, session);
834
0
}
835
836
0
static uint64_t pq_get_first_cycle(nghttp2_pq *pq) {
837
0
  nghttp2_stream *stream;
838
839
0
  if (nghttp2_pq_empty(pq)) {
840
0
    return 0;
841
0
  }
842
843
0
  stream = nghttp2_struct_of(nghttp2_pq_top(pq), nghttp2_stream, pq_entry);
844
0
  return stream->cycle;
845
0
}
846
847
static int session_ob_data_push(nghttp2_session *session,
848
0
                                nghttp2_stream *stream) {
849
0
  int rv;
850
0
  uint32_t urgency;
851
0
  int inc;
852
0
  nghttp2_pq *pq;
853
854
0
  assert(stream->queued == 0);
855
856
0
  urgency = nghttp2_extpri_uint8_urgency(stream->extpri);
857
0
  inc = nghttp2_extpri_uint8_inc(stream->extpri);
858
859
0
  assert(urgency < NGHTTP2_EXTPRI_URGENCY_LEVELS);
860
861
0
  pq = &session->sched[urgency].ob_data;
862
863
0
  stream->cycle = pq_get_first_cycle(pq);
864
0
  if (inc) {
865
0
    stream->cycle += stream->last_writelen;
866
0
  }
867
868
0
  rv = nghttp2_pq_push(pq, &stream->pq_entry);
869
0
  if (rv != 0) {
870
0
    return rv;
871
0
  }
872
873
0
  stream->queued = 1;
874
875
0
  return 0;
876
0
}
877
878
static void session_ob_data_remove(nghttp2_session *session,
879
0
                                   nghttp2_stream *stream) {
880
0
  uint32_t urgency;
881
882
0
  assert(stream->queued == 1);
883
884
0
  urgency = nghttp2_extpri_uint8_urgency(stream->extpri);
885
886
0
  assert(urgency < NGHTTP2_EXTPRI_URGENCY_LEVELS);
887
888
0
  nghttp2_pq_remove(&session->sched[urgency].ob_data, &stream->pq_entry);
889
890
0
  stream->queued = 0;
891
0
}
892
893
static int session_attach_stream_item(nghttp2_session *session,
894
                                      nghttp2_stream *stream,
895
0
                                      nghttp2_outbound_item *item) {
896
0
  int rv;
897
898
0
  nghttp2_stream_attach_item(stream, item);
899
900
0
  rv = session_ob_data_push(session, stream);
901
0
  if (rv != 0) {
902
0
    nghttp2_stream_detach_item(stream);
903
904
0
    return rv;
905
0
  }
906
907
0
  return 0;
908
0
}
909
910
static void session_detach_stream_item(nghttp2_session *session,
911
0
                                       nghttp2_stream *stream) {
912
0
  nghttp2_stream_detach_item(stream);
913
914
0
  if (!stream->queued) {
915
0
    return;
916
0
  }
917
918
0
  session_ob_data_remove(session, stream);
919
0
}
920
921
static void session_defer_stream_item(nghttp2_session *session,
922
0
                                      nghttp2_stream *stream, uint8_t flags) {
923
0
  nghttp2_stream_defer_item(stream, flags);
924
925
0
  if (!stream->queued) {
926
0
    return;
927
0
  }
928
929
0
  session_ob_data_remove(session, stream);
930
0
}
931
932
static int session_resume_deferred_stream_item(nghttp2_session *session,
933
                                               nghttp2_stream *stream,
934
0
                                               uint8_t flags) {
935
0
  nghttp2_stream_resume_deferred_item(stream, flags);
936
937
0
  if (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL) {
938
0
    return 0;
939
0
  }
940
941
0
  return session_ob_data_push(session, stream);
942
0
}
943
944
static nghttp2_outbound_item *
945
0
session_sched_get_next_outbound_item(nghttp2_session *session) {
946
0
  size_t i;
947
0
  nghttp2_pq_entry *ent;
948
0
  nghttp2_stream *stream;
949
950
0
  for (i = 0; i < NGHTTP2_EXTPRI_URGENCY_LEVELS; ++i) {
951
0
    ent = nghttp2_pq_top(&session->sched[i].ob_data);
952
0
    if (!ent) {
953
0
      continue;
954
0
    }
955
956
0
    stream = nghttp2_struct_of(ent, nghttp2_stream, pq_entry);
957
0
    return stream->item;
958
0
  }
959
960
0
  return NULL;
961
0
}
962
963
0
static int session_sched_empty(nghttp2_session *session) {
964
0
  size_t i;
965
966
0
  for (i = 0; i < NGHTTP2_EXTPRI_URGENCY_LEVELS; ++i) {
967
0
    if (!nghttp2_pq_empty(&session->sched[i].ob_data)) {
968
0
      return 0;
969
0
    }
970
0
  }
971
972
0
  return 1;
973
0
}
974
975
static void session_sched_reschedule_stream(nghttp2_session *session,
976
0
                                            nghttp2_stream *stream) {
977
0
  nghttp2_pq *pq;
978
0
  uint32_t urgency = nghttp2_extpri_uint8_urgency(stream->extpri);
979
0
  int inc = nghttp2_extpri_uint8_inc(stream->extpri);
980
0
  uint64_t penalty = (uint64_t)stream->last_writelen;
981
0
  int rv;
982
983
0
  (void)rv;
984
985
0
  assert(urgency < NGHTTP2_EXTPRI_URGENCY_LEVELS);
986
987
0
  pq = &session->sched[urgency].ob_data;
988
989
0
  if (!inc || nghttp2_pq_size(pq) == 1) {
990
0
    return;
991
0
  }
992
993
0
  nghttp2_pq_remove(pq, &stream->pq_entry);
994
995
0
  stream->cycle += penalty;
996
997
0
  rv = nghttp2_pq_push(pq, &stream->pq_entry);
998
999
0
  assert(0 == rv);
1000
0
}
1001
1002
static int session_update_stream_priority(nghttp2_session *session,
1003
                                          nghttp2_stream *stream,
1004
0
                                          uint8_t u8extpri) {
1005
0
  if (stream->extpri == u8extpri) {
1006
0
    return 0;
1007
0
  }
1008
1009
0
  if (stream->queued) {
1010
0
    session_ob_data_remove(session, stream);
1011
1012
0
    stream->extpri = u8extpri;
1013
1014
0
    return session_ob_data_push(session, stream);
1015
0
  }
1016
1017
0
  stream->extpri = u8extpri;
1018
1019
0
  return 0;
1020
0
}
1021
1022
int nghttp2_session_add_item(nghttp2_session *session,
1023
0
                             nghttp2_outbound_item *item) {
1024
  /* TODO Return error if stream is not found for the frame requiring
1025
     stream presence. */
1026
0
  int rv = 0;
1027
0
  nghttp2_stream *stream;
1028
0
  nghttp2_frame *frame;
1029
1030
0
  frame = &item->frame;
1031
0
  stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
1032
1033
0
  switch (frame->hd.type) {
1034
0
  case NGHTTP2_DATA:
1035
0
    if (!stream) {
1036
0
      return NGHTTP2_ERR_STREAM_CLOSED;
1037
0
    }
1038
1039
0
    if (stream->item) {
1040
0
      return NGHTTP2_ERR_DATA_EXIST;
1041
0
    }
1042
1043
0
    rv = session_attach_stream_item(session, stream, item);
1044
1045
0
    if (rv != 0) {
1046
0
      return rv;
1047
0
    }
1048
1049
0
    return 0;
1050
0
  case NGHTTP2_HEADERS:
1051
    /* We push request HEADERS and push response HEADERS to
1052
       dedicated queue because their transmission is affected by
1053
       SETTINGS_MAX_CONCURRENT_STREAMS */
1054
    /* TODO If 2 HEADERS are submitted for reserved stream, then
1055
       both of them are queued into ob_syn, which is not
1056
       desirable. */
1057
0
    if (frame->headers.cat == NGHTTP2_HCAT_REQUEST ||
1058
0
        (stream && stream->state == NGHTTP2_STREAM_RESERVED)) {
1059
0
      nghttp2_outbound_queue_push(&session->ob_syn, item);
1060
0
      item->queued = 1;
1061
0
      return 0;
1062
0
    }
1063
1064
0
    nghttp2_outbound_queue_push(&session->ob_reg, item);
1065
0
    item->queued = 1;
1066
0
    return 0;
1067
0
  case NGHTTP2_SETTINGS:
1068
0
  case NGHTTP2_PING:
1069
0
    nghttp2_outbound_queue_push(&session->ob_urgent, item);
1070
0
    item->queued = 1;
1071
0
    return 0;
1072
0
  case NGHTTP2_RST_STREAM:
1073
0
    if (stream) {
1074
0
      stream->state = NGHTTP2_STREAM_CLOSING;
1075
0
    }
1076
0
    nghttp2_outbound_queue_push(&session->ob_reg, item);
1077
0
    item->queued = 1;
1078
0
    return 0;
1079
0
  case NGHTTP2_PUSH_PROMISE: {
1080
0
    nghttp2_headers_aux_data *aux_data;
1081
1082
0
    aux_data = &item->aux_data.headers;
1083
1084
0
    if (!stream) {
1085
0
      return NGHTTP2_ERR_STREAM_CLOSED;
1086
0
    }
1087
1088
0
    if (!nghttp2_session_open_stream(
1089
0
          session, frame->push_promise.promised_stream_id,
1090
0
          NGHTTP2_STREAM_FLAG_NONE, NGHTTP2_STREAM_RESERVED,
1091
0
          aux_data->stream_user_data)) {
1092
0
      return NGHTTP2_ERR_NOMEM;
1093
0
    }
1094
1095
0
    nghttp2_outbound_queue_push(&session->ob_reg, item);
1096
0
    item->queued = 1;
1097
1098
0
    return 0;
1099
0
  }
1100
0
  case NGHTTP2_WINDOW_UPDATE:
1101
0
    if (stream) {
1102
0
      stream->window_update_queued = 1;
1103
0
    } else if (frame->hd.stream_id == 0) {
1104
0
      session->window_update_queued = 1;
1105
0
    }
1106
0
    nghttp2_outbound_queue_push(&session->ob_reg, item);
1107
0
    item->queued = 1;
1108
0
    return 0;
1109
0
  default:
1110
0
    nghttp2_outbound_queue_push(&session->ob_reg, item);
1111
0
    item->queued = 1;
1112
0
    return 0;
1113
0
  }
1114
0
}
1115
1116
int nghttp2_session_add_rst_stream(nghttp2_session *session, int32_t stream_id,
1117
0
                                   uint32_t error_code) {
1118
0
  return nghttp2_session_add_rst_stream_continue(
1119
0
    session, stream_id, error_code,
1120
0
    /* continue_without_stream = */ 1);
1121
0
}
1122
1123
int nghttp2_session_add_rst_stream_continue(nghttp2_session *session,
1124
                                            int32_t stream_id,
1125
                                            uint32_t error_code,
1126
0
                                            int continue_without_stream) {
1127
0
  int rv;
1128
0
  nghttp2_outbound_item *item;
1129
0
  nghttp2_frame *frame;
1130
0
  nghttp2_stream *stream;
1131
0
  nghttp2_mem *mem;
1132
1133
0
  mem = &session->mem;
1134
0
  stream = nghttp2_session_get_stream(session, stream_id);
1135
0
  if (stream && stream->state == NGHTTP2_STREAM_CLOSING) {
1136
0
    return 0;
1137
0
  }
1138
1139
  /* Sending RST_STREAM to an idle stream is subject to protocol
1140
     violation.  Historically, nghttp2 allows this.  In order not to
1141
     disrupt the existing applications, we don't error out this case
1142
     and simply ignore it. */
1143
0
  if (nghttp2_session_is_my_stream_id(session, stream_id)) {
1144
0
    if ((uint32_t)stream_id >= session->next_stream_id) {
1145
0
      return 0;
1146
0
    }
1147
0
  } else if (session->last_recv_stream_id < stream_id) {
1148
0
    return 0;
1149
0
  }
1150
1151
  /* Cancel pending request HEADERS in ob_syn if this RST_STREAM
1152
     refers to that stream. */
1153
0
  if (!session->server && nghttp2_session_is_my_stream_id(session, stream_id) &&
1154
0
      nghttp2_outbound_queue_top(&session->ob_syn)) {
1155
0
    nghttp2_headers_aux_data *aux_data;
1156
0
    nghttp2_frame *headers_frame;
1157
1158
0
    headers_frame = &nghttp2_outbound_queue_top(&session->ob_syn)->frame;
1159
0
    assert(headers_frame->hd.type == NGHTTP2_HEADERS);
1160
1161
0
    if (headers_frame->hd.stream_id <= stream_id) {
1162
0
      for (item = session->ob_syn.head; item; item = item->qnext) {
1163
0
        aux_data = &item->aux_data.headers;
1164
1165
0
        if (item->frame.hd.stream_id < stream_id) {
1166
0
          continue;
1167
0
        }
1168
1169
        /* stream_id in ob_syn queue must be strictly increasing.  If
1170
           we found larger ID, then we can break here. */
1171
0
        if (item->frame.hd.stream_id > stream_id || aux_data->canceled) {
1172
0
          break;
1173
0
        }
1174
1175
0
        aux_data->error_code = error_code;
1176
0
        aux_data->canceled = 1;
1177
1178
0
        return 0;
1179
0
      }
1180
0
    }
1181
0
  }
1182
1183
  /* To keep the old behaviour, do not fail if stream was not
1184
     found. */
1185
0
  if (!continue_without_stream && !stream) {
1186
0
    return 0;
1187
0
  }
1188
1189
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
1190
0
  if (item == NULL) {
1191
0
    return NGHTTP2_ERR_NOMEM;
1192
0
  }
1193
1194
0
  nghttp2_outbound_item_init(item);
1195
1196
0
  frame = &item->frame;
1197
1198
0
  nghttp2_frame_rst_stream_init(&frame->rst_stream, stream_id, error_code);
1199
1200
0
  item->aux_data.rst_stream.continue_without_stream =
1201
0
    (uint8_t)(continue_without_stream != 0);
1202
1203
0
  rv = nghttp2_session_add_item(session, item);
1204
0
  if (rv != 0) {
1205
0
    nghttp2_frame_rst_stream_free(&frame->rst_stream);
1206
0
    nghttp2_mem_free(mem, item);
1207
0
    return rv;
1208
0
  }
1209
0
  return 0;
1210
0
}
1211
1212
nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session,
1213
                                            int32_t stream_id, uint8_t flags,
1214
                                            nghttp2_stream_state initial_state,
1215
0
                                            void *stream_user_data) {
1216
0
  int rv;
1217
0
  nghttp2_stream *stream;
1218
0
  int stream_alloc = 0;
1219
0
  nghttp2_mem *mem;
1220
1221
0
  mem = &session->mem;
1222
0
  stream = nghttp2_session_get_stream_raw(session, stream_id);
1223
1224
0
  if (session->opt_flags &
1225
0
      NGHTTP2_OPTMASK_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION) {
1226
0
    flags |= NGHTTP2_STREAM_FLAG_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION;
1227
0
  }
1228
1229
0
  if (stream) {
1230
0
    assert(stream->state == NGHTTP2_STREAM_IDLE);
1231
0
    assert(initial_state != NGHTTP2_STREAM_IDLE);
1232
1233
0
    --session->num_idle_streams;
1234
0
  } else {
1235
0
    stream = nghttp2_mem_malloc(mem, sizeof(nghttp2_stream));
1236
0
    if (stream == NULL) {
1237
0
      return NULL;
1238
0
    }
1239
1240
0
    stream_alloc = 1;
1241
0
  }
1242
1243
0
  if (initial_state == NGHTTP2_STREAM_RESERVED) {
1244
0
    flags |= NGHTTP2_STREAM_FLAG_PUSH;
1245
0
  }
1246
1247
0
  if (stream_alloc) {
1248
0
    nghttp2_stream_init(stream, stream_id, flags, initial_state,
1249
0
                        (int32_t)session->remote_settings.initial_window_size,
1250
0
                        (int32_t)session->local_settings.initial_window_size,
1251
0
                        stream_user_data);
1252
0
    stream->seq = session->stream_seq++;
1253
1254
0
    rv = nghttp2_map_insert(&session->streams, stream_id, stream);
1255
0
    if (rv != 0) {
1256
0
      nghttp2_stream_free(stream);
1257
0
      nghttp2_mem_free(mem, stream);
1258
0
      return NULL;
1259
0
    }
1260
0
  } else {
1261
0
    stream->flags = flags;
1262
0
    stream->state = initial_state;
1263
0
    stream->stream_user_data = stream_user_data;
1264
0
  }
1265
1266
0
  switch (initial_state) {
1267
0
  case NGHTTP2_STREAM_RESERVED:
1268
0
    if (nghttp2_session_is_my_stream_id(session, stream_id)) {
1269
      /* reserved (local) */
1270
0
      nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
1271
0
    } else {
1272
      /* reserved (remote) */
1273
0
      nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
1274
0
      ++session->num_incoming_reserved_streams;
1275
0
    }
1276
    /* Reserved stream does not count in the concurrent streams
1277
       limit. That is one of the DOS vector. */
1278
0
    break;
1279
0
  case NGHTTP2_STREAM_IDLE:
1280
0
    ++session->num_idle_streams;
1281
0
    break;
1282
0
  default:
1283
0
    if (nghttp2_session_is_my_stream_id(session, stream_id)) {
1284
0
      ++session->num_outgoing_streams;
1285
0
    } else {
1286
0
      ++session->num_incoming_streams;
1287
0
    }
1288
0
  }
1289
1290
0
  return stream;
1291
0
}
1292
1293
int nghttp2_session_close_stream(nghttp2_session *session, int32_t stream_id,
1294
0
                                 uint32_t error_code) {
1295
0
  nghttp2_stream *stream;
1296
0
  nghttp2_mem *mem;
1297
0
  int is_my_stream_id;
1298
1299
0
  mem = &session->mem;
1300
0
  stream = nghttp2_session_get_stream(session, stream_id);
1301
1302
0
  if (!stream) {
1303
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
1304
0
  }
1305
1306
0
  DEBUGF("stream: stream(%p)=%d close\n", stream, stream->stream_id);
1307
1308
  /* We call on_stream_close_callback even if stream->state is
1309
     NGHTTP2_STREAM_INITIAL. This will happen while sending request
1310
     HEADERS, a local endpoint receives RST_STREAM for that stream. It
1311
     may be PROTOCOL_ERROR, but without notifying stream closure will
1312
     hang the stream in a local endpoint.
1313
  */
1314
1315
0
  if (session->callbacks.on_stream_close_callback) {
1316
0
    if (session->callbacks.on_stream_close_callback(
1317
0
          session, stream_id, error_code, session->user_data) != 0) {
1318
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
1319
0
    }
1320
0
  }
1321
1322
0
  if (stream->item) {
1323
0
    nghttp2_outbound_item *item;
1324
1325
0
    item = stream->item;
1326
1327
0
    session_detach_stream_item(session, stream);
1328
1329
    /* If item is queued, it will be deleted when it is popped
1330
       (nghttp2_session_prep_frame() will fail).  If session->aob.item
1331
       points to this item, let active_outbound_item_reset()
1332
       free the item. */
1333
0
    if (!item->queued && item != session->aob.item) {
1334
0
      nghttp2_outbound_item_free(item, mem);
1335
0
      nghttp2_mem_free(mem, item);
1336
0
    }
1337
0
  }
1338
1339
0
  is_my_stream_id = nghttp2_session_is_my_stream_id(session, stream_id);
1340
1341
  /* pushed streams which is not opened yet is not counted toward max
1342
     concurrent limits */
1343
0
  if ((stream->flags & NGHTTP2_STREAM_FLAG_PUSH)) {
1344
0
    if (!is_my_stream_id) {
1345
0
      --session->num_incoming_reserved_streams;
1346
0
    }
1347
0
  } else {
1348
0
    if (is_my_stream_id) {
1349
0
      --session->num_outgoing_streams;
1350
0
    } else {
1351
0
      --session->num_incoming_streams;
1352
0
    }
1353
0
  }
1354
1355
  /* Closes both directions just in case they are not closed yet */
1356
0
  stream->flags |= NGHTTP2_STREAM_FLAG_CLOSED;
1357
1358
0
  nghttp2_session_destroy_stream(session, stream);
1359
1360
0
  return 0;
1361
0
}
1362
1363
void nghttp2_session_destroy_stream(nghttp2_session *session,
1364
0
                                    nghttp2_stream *stream) {
1365
0
  nghttp2_mem *mem;
1366
1367
0
  DEBUGF("stream: destroy closed stream(%p)=%d\n", stream, stream->stream_id);
1368
1369
0
  mem = &session->mem;
1370
1371
0
  if (stream->queued) {
1372
0
    session_ob_data_remove(session, stream);
1373
0
  }
1374
1375
0
  nghttp2_map_remove(&session->streams, stream->stream_id);
1376
0
  nghttp2_stream_free(stream);
1377
0
  nghttp2_mem_free(mem, stream);
1378
0
}
1379
1380
/*
1381
 * Closes stream with stream ID |stream_id| if both transmission and
1382
 * reception of the stream were disallowed. The |error_code| indicates
1383
 * the reason of the closure.
1384
 *
1385
 * This function returns 0 if it succeeds, or one of the following
1386
 * negative error codes:
1387
 *
1388
 * NGHTTP2_ERR_INVALID_ARGUMENT
1389
 *   The stream is not found.
1390
 * NGHTTP2_ERR_CALLBACK_FAILURE
1391
 *   The callback function failed.
1392
 */
1393
int nghttp2_session_close_stream_if_shut_rdwr(nghttp2_session *session,
1394
0
                                              nghttp2_stream *stream) {
1395
0
  if ((stream->shut_flags & NGHTTP2_SHUT_RDWR) == NGHTTP2_SHUT_RDWR) {
1396
0
    return nghttp2_session_close_stream(session, stream->stream_id,
1397
0
                                        NGHTTP2_NO_ERROR);
1398
0
  }
1399
0
  return 0;
1400
0
}
1401
1402
/*
1403
 * Returns nonzero if local endpoint allows reception of new stream
1404
 * from remote.
1405
 */
1406
0
static int session_allow_incoming_new_stream(nghttp2_session *session) {
1407
0
  return (session->goaway_flags &
1408
0
          (NGHTTP2_GOAWAY_TERM_ON_SEND | NGHTTP2_GOAWAY_SENT)) == 0;
1409
0
}
1410
1411
/*
1412
 * This function returns nonzero if session is closing.
1413
 */
1414
0
static int session_is_closing(nghttp2_session *session) {
1415
0
  return (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) != 0 ||
1416
0
         (nghttp2_session_want_read(session) == 0 &&
1417
0
          nghttp2_session_want_write(session) == 0);
1418
0
}
1419
1420
/*
1421
 * Check that we can send a frame to the |stream|. This function
1422
 * returns 0 if we can send a frame to the |frame|, or one of the
1423
 * following negative error codes:
1424
 *
1425
 * NGHTTP2_ERR_STREAM_CLOSED
1426
 *   The stream is already closed.
1427
 * NGHTTP2_ERR_STREAM_SHUT_WR
1428
 *   The stream is half-closed for transmission.
1429
 * NGHTTP2_ERR_SESSION_CLOSING
1430
 *   This session is closing.
1431
 */
1432
static int session_predicate_for_stream_send(nghttp2_session *session,
1433
0
                                             nghttp2_stream *stream) {
1434
0
  if (stream == NULL) {
1435
0
    return NGHTTP2_ERR_STREAM_CLOSED;
1436
0
  }
1437
0
  if (session_is_closing(session)) {
1438
0
    return NGHTTP2_ERR_SESSION_CLOSING;
1439
0
  }
1440
0
  if (stream->shut_flags & NGHTTP2_SHUT_WR) {
1441
0
    return NGHTTP2_ERR_STREAM_SHUT_WR;
1442
0
  }
1443
0
  return 0;
1444
0
}
1445
1446
0
int nghttp2_session_check_request_allowed(nghttp2_session *session) {
1447
0
  return !session->server && session->next_stream_id <= INT32_MAX &&
1448
0
         (session->goaway_flags & NGHTTP2_GOAWAY_RECV) == 0 &&
1449
0
         !session_is_closing(session);
1450
0
}
1451
1452
/*
1453
 * This function checks request HEADERS frame, which opens stream, can
1454
 * be sent at this time.
1455
 *
1456
 * This function returns 0 if it succeeds, or one of the following
1457
 * negative error codes:
1458
 *
1459
 * NGHTTP2_ERR_START_STREAM_NOT_ALLOWED
1460
 *     New stream cannot be created because of GOAWAY: session is
1461
 *     going down or received last_stream_id is strictly less than
1462
 *     frame->hd.stream_id.
1463
 * NGHTTP2_ERR_STREAM_CLOSING
1464
 *     request HEADERS was canceled by RST_STREAM while it is in queue.
1465
 */
1466
static int session_predicate_request_headers_send(nghttp2_session *session,
1467
0
                                                  nghttp2_outbound_item *item) {
1468
0
  if (item->aux_data.headers.canceled) {
1469
0
    return NGHTTP2_ERR_STREAM_CLOSING;
1470
0
  }
1471
  /* If we are terminating session (NGHTTP2_GOAWAY_TERM_ON_SEND),
1472
     GOAWAY was received from peer, or session is about to close, new
1473
     request is not allowed. */
1474
0
  if ((session->goaway_flags & NGHTTP2_GOAWAY_RECV) ||
1475
0
      session_is_closing(session)) {
1476
0
    return NGHTTP2_ERR_START_STREAM_NOT_ALLOWED;
1477
0
  }
1478
0
  return 0;
1479
0
}
1480
1481
/*
1482
 * This function checks HEADERS, which is the first frame from the
1483
 * server, with the |stream| can be sent at this time.  The |stream|
1484
 * can be NULL.
1485
 *
1486
 * This function returns 0 if it succeeds, or one of the following
1487
 * negative error codes:
1488
 *
1489
 * NGHTTP2_ERR_STREAM_CLOSED
1490
 *     The stream is already closed or does not exist.
1491
 * NGHTTP2_ERR_STREAM_SHUT_WR
1492
 *     The transmission is not allowed for this stream (e.g., a frame
1493
 *     with END_STREAM flag set has already sent)
1494
 * NGHTTP2_ERR_INVALID_STREAM_ID
1495
 *     The stream ID is invalid.
1496
 * NGHTTP2_ERR_STREAM_CLOSING
1497
 *     RST_STREAM was queued for this stream.
1498
 * NGHTTP2_ERR_INVALID_STREAM_STATE
1499
 *     The state of the stream is not valid.
1500
 * NGHTTP2_ERR_SESSION_CLOSING
1501
 *     This session is closing.
1502
 * NGHTTP2_ERR_PROTO
1503
 *     Client side attempted to send response.
1504
 */
1505
static int session_predicate_response_headers_send(nghttp2_session *session,
1506
0
                                                   nghttp2_stream *stream) {
1507
0
  int rv;
1508
0
  rv = session_predicate_for_stream_send(session, stream);
1509
0
  if (rv != 0) {
1510
0
    return rv;
1511
0
  }
1512
0
  assert(stream);
1513
0
  if (!session->server) {
1514
0
    return NGHTTP2_ERR_PROTO;
1515
0
  }
1516
0
  if (nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
1517
0
    return NGHTTP2_ERR_INVALID_STREAM_ID;
1518
0
  }
1519
0
  switch (stream->state) {
1520
0
  case NGHTTP2_STREAM_OPENING:
1521
0
    return 0;
1522
0
  case NGHTTP2_STREAM_CLOSING:
1523
0
    return NGHTTP2_ERR_STREAM_CLOSING;
1524
0
  default:
1525
0
    return NGHTTP2_ERR_INVALID_STREAM_STATE;
1526
0
  }
1527
0
}
1528
1529
/*
1530
 * This function checks HEADERS for reserved stream can be sent. The
1531
 * |stream| must be reserved state and the |session| is server side.
1532
 * The |stream| can be NULL.
1533
 *
1534
 * This function returns 0 if it succeeds, or one of the following
1535
 * error codes:
1536
 *
1537
 * NGHTTP2_ERR_STREAM_CLOSED
1538
 *   The stream is already closed.
1539
 * NGHTTP2_ERR_STREAM_SHUT_WR
1540
 *   The stream is half-closed for transmission.
1541
 * NGHTTP2_ERR_PROTO
1542
 *   The stream is not reserved state
1543
 * NGHTTP2_ERR_STREAM_CLOSED
1544
 *   RST_STREAM was queued for this stream.
1545
 * NGHTTP2_ERR_SESSION_CLOSING
1546
 *   This session is closing.
1547
 * NGHTTP2_ERR_START_STREAM_NOT_ALLOWED
1548
 *   New stream cannot be created because GOAWAY is already sent or
1549
 *   received.
1550
 * NGHTTP2_ERR_PROTO
1551
 *   Client side attempted to send push response.
1552
 */
1553
static int
1554
session_predicate_push_response_headers_send(nghttp2_session *session,
1555
0
                                             nghttp2_stream *stream) {
1556
0
  int rv;
1557
  /* TODO Should disallow HEADERS if GOAWAY has already been issued? */
1558
0
  rv = session_predicate_for_stream_send(session, stream);
1559
0
  if (rv != 0) {
1560
0
    return rv;
1561
0
  }
1562
0
  assert(stream);
1563
0
  if (!session->server) {
1564
0
    return NGHTTP2_ERR_PROTO;
1565
0
  }
1566
0
  if (stream->state != NGHTTP2_STREAM_RESERVED) {
1567
0
    return NGHTTP2_ERR_PROTO;
1568
0
  }
1569
0
  if (session->goaway_flags & NGHTTP2_GOAWAY_RECV) {
1570
0
    return NGHTTP2_ERR_START_STREAM_NOT_ALLOWED;
1571
0
  }
1572
0
  return 0;
1573
0
}
1574
1575
/*
1576
 * This function checks HEADERS, which is neither stream-opening nor
1577
 * first response header, with the |stream| can be sent at this time.
1578
 * The |stream| can be NULL.
1579
 *
1580
 * This function returns 0 if it succeeds, or one of the following
1581
 * negative error codes:
1582
 *
1583
 * NGHTTP2_ERR_STREAM_CLOSED
1584
 *     The stream is already closed or does not exist.
1585
 * NGHTTP2_ERR_STREAM_SHUT_WR
1586
 *     The transmission is not allowed for this stream (e.g., a frame
1587
 *     with END_STREAM flag set has already sent)
1588
 * NGHTTP2_ERR_STREAM_CLOSING
1589
 *     RST_STREAM was queued for this stream.
1590
 * NGHTTP2_ERR_INVALID_STREAM_STATE
1591
 *     The state of the stream is not valid.
1592
 * NGHTTP2_ERR_SESSION_CLOSING
1593
 *   This session is closing.
1594
 */
1595
static int session_predicate_headers_send(nghttp2_session *session,
1596
0
                                          nghttp2_stream *stream) {
1597
0
  int rv;
1598
0
  rv = session_predicate_for_stream_send(session, stream);
1599
0
  if (rv != 0) {
1600
0
    return rv;
1601
0
  }
1602
0
  assert(stream);
1603
1604
0
  switch (stream->state) {
1605
0
  case NGHTTP2_STREAM_OPENED:
1606
0
    return 0;
1607
0
  case NGHTTP2_STREAM_CLOSING:
1608
0
    return NGHTTP2_ERR_STREAM_CLOSING;
1609
0
  default:
1610
0
    if (nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
1611
0
      return 0;
1612
0
    }
1613
0
    return NGHTTP2_ERR_INVALID_STREAM_STATE;
1614
0
  }
1615
0
}
1616
1617
/*
1618
 * This function checks PUSH_PROMISE frame |frame| with the |stream|
1619
 * can be sent at this time.  The |stream| can be NULL.
1620
 *
1621
 * This function returns 0 if it succeeds, or one of the following
1622
 * negative error codes:
1623
 *
1624
 * NGHTTP2_ERR_START_STREAM_NOT_ALLOWED
1625
 *     New stream cannot be created because GOAWAY is already sent or
1626
 *     received.
1627
 * NGHTTP2_ERR_PROTO
1628
 *     The client side attempts to send PUSH_PROMISE, or the server
1629
 *     sends PUSH_PROMISE for the stream not initiated by the client.
1630
 * NGHTTP2_ERR_STREAM_CLOSED
1631
 *     The stream is already closed or does not exist.
1632
 * NGHTTP2_ERR_STREAM_CLOSING
1633
 *     RST_STREAM was queued for this stream.
1634
 * NGHTTP2_ERR_STREAM_SHUT_WR
1635
 *     The transmission is not allowed for this stream (e.g., a frame
1636
 *     with END_STREAM flag set has already sent)
1637
 * NGHTTP2_ERR_PUSH_DISABLED
1638
 *     The remote peer disabled reception of PUSH_PROMISE.
1639
 * NGHTTP2_ERR_SESSION_CLOSING
1640
 *   This session is closing.
1641
 */
1642
static int session_predicate_push_promise_send(nghttp2_session *session,
1643
0
                                               nghttp2_stream *stream) {
1644
0
  int rv;
1645
1646
0
  if (!session->server) {
1647
0
    return NGHTTP2_ERR_PROTO;
1648
0
  }
1649
1650
0
  rv = session_predicate_for_stream_send(session, stream);
1651
0
  if (rv != 0) {
1652
0
    return rv;
1653
0
  }
1654
1655
0
  assert(stream);
1656
1657
0
  if (session->remote_settings.enable_push == 0) {
1658
0
    return NGHTTP2_ERR_PUSH_DISABLED;
1659
0
  }
1660
0
  if (stream->state == NGHTTP2_STREAM_CLOSING) {
1661
0
    return NGHTTP2_ERR_STREAM_CLOSING;
1662
0
  }
1663
0
  if (session->goaway_flags & NGHTTP2_GOAWAY_RECV) {
1664
0
    return NGHTTP2_ERR_START_STREAM_NOT_ALLOWED;
1665
0
  }
1666
0
  return 0;
1667
0
}
1668
1669
/*
1670
 * This function checks WINDOW_UPDATE with the stream ID |stream_id|
1671
 * can be sent at this time. Note that END_STREAM flag of the previous
1672
 * frame does not affect the transmission of the WINDOW_UPDATE frame.
1673
 *
1674
 * This function returns 0 if it succeeds, or one of the following
1675
 * negative error codes:
1676
 *
1677
 * NGHTTP2_ERR_STREAM_CLOSED
1678
 *     The stream is already closed or does not exist.
1679
 * NGHTTP2_ERR_STREAM_CLOSING
1680
 *     RST_STREAM was queued for this stream.
1681
 * NGHTTP2_ERR_INVALID_STREAM_STATE
1682
 *     The state of the stream is not valid.
1683
 * NGHTTP2_ERR_SESSION_CLOSING
1684
 *   This session is closing.
1685
 */
1686
static int session_predicate_window_update_send(nghttp2_session *session,
1687
0
                                                int32_t stream_id) {
1688
0
  nghttp2_stream *stream;
1689
1690
0
  if (session_is_closing(session)) {
1691
0
    return NGHTTP2_ERR_SESSION_CLOSING;
1692
0
  }
1693
1694
0
  if (stream_id == 0) {
1695
    /* Connection-level window update */
1696
0
    return 0;
1697
0
  }
1698
0
  stream = nghttp2_session_get_stream(session, stream_id);
1699
0
  if (stream == NULL) {
1700
0
    return NGHTTP2_ERR_STREAM_CLOSED;
1701
0
  }
1702
0
  if (stream->state == NGHTTP2_STREAM_CLOSING) {
1703
0
    return NGHTTP2_ERR_STREAM_CLOSING;
1704
0
  }
1705
0
  if (state_reserved_local(session, stream)) {
1706
0
    return NGHTTP2_ERR_INVALID_STREAM_STATE;
1707
0
  }
1708
0
  return 0;
1709
0
}
1710
1711
static int session_predicate_altsvc_send(nghttp2_session *session,
1712
0
                                         int32_t stream_id) {
1713
0
  nghttp2_stream *stream;
1714
1715
0
  if (session_is_closing(session)) {
1716
0
    return NGHTTP2_ERR_SESSION_CLOSING;
1717
0
  }
1718
1719
0
  if (stream_id == 0) {
1720
0
    return 0;
1721
0
  }
1722
1723
0
  stream = nghttp2_session_get_stream(session, stream_id);
1724
0
  if (stream == NULL) {
1725
0
    return NGHTTP2_ERR_STREAM_CLOSED;
1726
0
  }
1727
0
  if (stream->state == NGHTTP2_STREAM_CLOSING) {
1728
0
    return NGHTTP2_ERR_STREAM_CLOSING;
1729
0
  }
1730
1731
0
  return 0;
1732
0
}
1733
1734
0
static int session_predicate_origin_send(nghttp2_session *session) {
1735
0
  if (session_is_closing(session)) {
1736
0
    return NGHTTP2_ERR_SESSION_CLOSING;
1737
0
  }
1738
0
  return 0;
1739
0
}
1740
1741
static int session_predicate_priority_update_send(nghttp2_session *session,
1742
0
                                                  int32_t stream_id) {
1743
0
  nghttp2_stream *stream;
1744
1745
0
  if (session_is_closing(session)) {
1746
0
    return NGHTTP2_ERR_SESSION_CLOSING;
1747
0
  }
1748
1749
0
  stream = nghttp2_session_get_stream(session, stream_id);
1750
0
  if (stream == NULL) {
1751
0
    return 0;
1752
0
  }
1753
0
  if (stream->state == NGHTTP2_STREAM_CLOSING) {
1754
0
    return NGHTTP2_ERR_STREAM_CLOSING;
1755
0
  }
1756
0
  if (stream->shut_flags & NGHTTP2_SHUT_RD) {
1757
0
    return NGHTTP2_ERR_INVALID_STREAM_STATE;
1758
0
  }
1759
1760
0
  return 0;
1761
0
}
1762
1763
/* Take into account settings max frame size and both connection-level
1764
   flow control here */
1765
static nghttp2_ssize nghttp2_session_enforce_flow_control_limits(
1766
  nghttp2_session *session, nghttp2_stream *stream,
1767
0
  nghttp2_ssize requested_window_size) {
1768
0
  DEBUGF("send: remote windowsize connection=%d, remote maxframsize=%u, "
1769
0
         "stream(id %d)=%d\n",
1770
0
         session->remote_window_size, session->remote_settings.max_frame_size,
1771
0
         stream->stream_id, stream->remote_window_size);
1772
1773
0
  return nghttp2_min_int32(
1774
0
    nghttp2_min_int32(nghttp2_min_int32((int32_t)requested_window_size,
1775
0
                                        stream->remote_window_size),
1776
0
                      session->remote_window_size),
1777
0
    (int32_t)session->remote_settings.max_frame_size);
1778
0
}
1779
1780
/*
1781
 * Returns the maximum length of next data read. If the
1782
 * connection-level and/or stream-wise flow control are enabled, the
1783
 * return value takes into account those current window sizes. The remote
1784
 * settings for max frame size is also taken into account.
1785
 */
1786
static size_t nghttp2_session_next_data_read(nghttp2_session *session,
1787
0
                                             nghttp2_stream *stream) {
1788
0
  nghttp2_ssize window_size;
1789
1790
0
  window_size = nghttp2_session_enforce_flow_control_limits(
1791
0
    session, stream, NGHTTP2_DATA_PAYLOADLEN);
1792
1793
0
  DEBUGF("send: available window=%td\n", window_size);
1794
1795
0
  return window_size > 0 ? (size_t)window_size : 0;
1796
0
}
1797
1798
/*
1799
 * This function checks DATA with the |stream| can be sent at this
1800
 * time.  The |stream| can be NULL.
1801
 *
1802
 * This function returns 0 if it succeeds, or one of the following
1803
 * negative error codes:
1804
 *
1805
 * NGHTTP2_ERR_STREAM_CLOSED
1806
 *     The stream is already closed or does not exist.
1807
 * NGHTTP2_ERR_STREAM_SHUT_WR
1808
 *     The transmission is not allowed for this stream (e.g., a frame
1809
 *     with END_STREAM flag set has already sent)
1810
 * NGHTTP2_ERR_STREAM_CLOSING
1811
 *     RST_STREAM was queued for this stream.
1812
 * NGHTTP2_ERR_INVALID_STREAM_STATE
1813
 *     The state of the stream is not valid.
1814
 * NGHTTP2_ERR_SESSION_CLOSING
1815
 *   This session is closing.
1816
 */
1817
static int nghttp2_session_predicate_data_send(nghttp2_session *session,
1818
0
                                               nghttp2_stream *stream) {
1819
0
  int rv;
1820
0
  rv = session_predicate_for_stream_send(session, stream);
1821
0
  if (rv != 0) {
1822
0
    return rv;
1823
0
  }
1824
0
  assert(stream);
1825
0
  if (nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
1826
    /* Request body data */
1827
    /* If stream->state is NGHTTP2_STREAM_CLOSING, RST_STREAM was
1828
       queued but not yet sent. In this case, we won't send DATA
1829
       frames. */
1830
0
    if (stream->state == NGHTTP2_STREAM_CLOSING) {
1831
0
      return NGHTTP2_ERR_STREAM_CLOSING;
1832
0
    }
1833
0
    if (stream->state == NGHTTP2_STREAM_RESERVED) {
1834
0
      return NGHTTP2_ERR_INVALID_STREAM_STATE;
1835
0
    }
1836
0
    return 0;
1837
0
  }
1838
  /* Response body data */
1839
0
  if (stream->state == NGHTTP2_STREAM_OPENED) {
1840
0
    return 0;
1841
0
  }
1842
0
  if (stream->state == NGHTTP2_STREAM_CLOSING) {
1843
0
    return NGHTTP2_ERR_STREAM_CLOSING;
1844
0
  }
1845
0
  return NGHTTP2_ERR_INVALID_STREAM_STATE;
1846
0
}
1847
1848
static nghttp2_ssize session_call_select_padding(nghttp2_session *session,
1849
                                                 const nghttp2_frame *frame,
1850
0
                                                 size_t max_payloadlen) {
1851
0
  nghttp2_ssize rv;
1852
0
  size_t max_paddedlen;
1853
1854
0
  if (frame->hd.length >= max_payloadlen ||
1855
0
      (!session->callbacks.select_padding_callback2 &&
1856
0
       !session->callbacks.select_padding_callback)) {
1857
0
    return (nghttp2_ssize)frame->hd.length;
1858
0
  }
1859
1860
0
  max_paddedlen =
1861
0
    nghttp2_min_size(frame->hd.length + NGHTTP2_MAX_PADLEN, max_payloadlen);
1862
1863
0
  if (session->callbacks.select_padding_callback2) {
1864
0
    rv = session->callbacks.select_padding_callback2(
1865
0
      session, frame, max_paddedlen, session->user_data);
1866
0
  } else {
1867
0
    rv = (nghttp2_ssize)session->callbacks.select_padding_callback(
1868
0
      session, frame, max_paddedlen, session->user_data);
1869
0
  }
1870
0
  if (rv < (nghttp2_ssize)frame->hd.length ||
1871
0
      rv > (nghttp2_ssize)max_paddedlen) {
1872
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1873
0
  }
1874
0
  return rv;
1875
0
}
1876
1877
/* Add padding to HEADERS or PUSH_PROMISE. We use
1878
   frame->headers.padlen in this function to use the fact that
1879
   frame->push_promise has also padlen in the same position. */
1880
static int session_headers_add_pad(nghttp2_session *session,
1881
0
                                   nghttp2_frame *frame) {
1882
0
  nghttp2_ssize padded_payloadlen;
1883
0
  nghttp2_active_outbound_item *aob;
1884
0
  nghttp2_bufs *framebufs;
1885
0
  size_t padlen;
1886
0
  size_t max_payloadlen;
1887
1888
0
  aob = &session->aob;
1889
0
  framebufs = &aob->framebufs;
1890
1891
0
  max_payloadlen = nghttp2_min_size(NGHTTP2_MAX_PAYLOADLEN,
1892
0
                                    frame->hd.length + NGHTTP2_MAX_PADLEN);
1893
1894
0
  padded_payloadlen =
1895
0
    session_call_select_padding(session, frame, max_payloadlen);
1896
1897
0
  if (nghttp2_is_fatal((int)padded_payloadlen)) {
1898
0
    return (int)padded_payloadlen;
1899
0
  }
1900
1901
0
  padlen = (size_t)padded_payloadlen - frame->hd.length;
1902
1903
0
  DEBUGF("send: padding selected: payloadlen=%td, padlen=%zu\n",
1904
0
         padded_payloadlen, padlen);
1905
1906
0
  nghttp2_frame_add_pad(framebufs, &frame->hd, padlen, 0);
1907
1908
0
  frame->headers.padlen = padlen;
1909
1910
0
  return 0;
1911
0
}
1912
1913
static size_t session_estimate_headers_payload(nghttp2_session *session,
1914
                                               const nghttp2_nv *nva,
1915
                                               size_t nvlen,
1916
0
                                               size_t additional) {
1917
0
  return nghttp2_hd_deflate_bound(&session->hd_deflater, nva, nvlen) +
1918
0
         additional;
1919
0
}
1920
1921
static int session_pack_extension(nghttp2_session *session, nghttp2_bufs *bufs,
1922
0
                                  nghttp2_frame *frame) {
1923
0
  nghttp2_ssize rv;
1924
0
  nghttp2_buf *buf;
1925
0
  size_t buflen;
1926
0
  size_t framelen;
1927
1928
0
  assert(session->callbacks.pack_extension_callback2 ||
1929
0
         session->callbacks.pack_extension_callback);
1930
1931
0
  buf = &bufs->head->buf;
1932
0
  buflen = nghttp2_min_size(nghttp2_buf_avail(buf), NGHTTP2_MAX_PAYLOADLEN);
1933
1934
0
  if (session->callbacks.pack_extension_callback2) {
1935
0
    rv = session->callbacks.pack_extension_callback2(session, buf->last, buflen,
1936
0
                                                     frame, session->user_data);
1937
0
  } else {
1938
0
    rv = (nghttp2_ssize)session->callbacks.pack_extension_callback(
1939
0
      session, buf->last, buflen, frame, session->user_data);
1940
0
  }
1941
0
  if (rv == NGHTTP2_ERR_CANCEL) {
1942
0
    return (int)rv;
1943
0
  }
1944
1945
0
  if (rv < 0 || (size_t)rv > buflen) {
1946
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
1947
0
  }
1948
1949
0
  framelen = (size_t)rv;
1950
1951
0
  frame->hd.length = framelen;
1952
1953
0
  assert(buf->pos == buf->last);
1954
0
  buf->last += framelen;
1955
0
  buf->pos -= NGHTTP2_FRAME_HDLEN;
1956
1957
0
  nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
1958
1959
0
  return 0;
1960
0
}
1961
1962
/*
1963
 * This function serializes frame for transmission.
1964
 *
1965
 * This function returns 0 if it succeeds, or one of negative error
1966
 * codes, including both fatal and non-fatal ones.
1967
 */
1968
static int session_prep_frame(nghttp2_session *session,
1969
0
                              nghttp2_outbound_item *item) {
1970
0
  int rv;
1971
0
  nghttp2_frame *frame;
1972
0
  nghttp2_mem *mem;
1973
1974
0
  mem = &session->mem;
1975
0
  frame = &item->frame;
1976
1977
0
  switch (frame->hd.type) {
1978
0
  case NGHTTP2_DATA: {
1979
0
    size_t next_readmax;
1980
0
    nghttp2_stream *stream;
1981
1982
0
    stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
1983
1984
0
    if (stream) {
1985
0
      assert(stream->item == item);
1986
0
    }
1987
1988
0
    rv = nghttp2_session_predicate_data_send(session, stream);
1989
0
    if (rv != 0) {
1990
      /* If stream was already closed, nghttp2_session_get_stream()
1991
         returns NULL, but item is still attached to the stream.
1992
         Search stream including closed again. */
1993
0
      stream = nghttp2_session_get_stream_raw(session, frame->hd.stream_id);
1994
0
      if (stream) {
1995
0
        session_detach_stream_item(session, stream);
1996
0
      }
1997
1998
0
      return rv;
1999
0
    }
2000
    /* Assuming stream is not NULL */
2001
0
    assert(stream);
2002
0
    next_readmax = nghttp2_session_next_data_read(session, stream);
2003
2004
0
    if (next_readmax == 0) {
2005
      /* This must be true since we only pop DATA frame item from
2006
         queue when session->remote_window_size > 0 */
2007
0
      assert(session->remote_window_size > 0);
2008
2009
0
      session_defer_stream_item(session, stream,
2010
0
                                NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
2011
2012
0
      session->aob.item = NULL;
2013
0
      active_outbound_item_reset(&session->aob, mem);
2014
0
      return NGHTTP2_ERR_DEFERRED;
2015
0
    }
2016
2017
0
    rv =
2018
0
      nghttp2_session_pack_data(session, &session->aob.framebufs, next_readmax,
2019
0
                                frame, &item->aux_data.data, stream);
2020
0
    if (rv == NGHTTP2_ERR_PAUSE) {
2021
0
      return rv;
2022
0
    }
2023
0
    if (rv == NGHTTP2_ERR_DEFERRED) {
2024
0
      session_defer_stream_item(session, stream,
2025
0
                                NGHTTP2_STREAM_FLAG_DEFERRED_USER);
2026
2027
0
      session->aob.item = NULL;
2028
0
      active_outbound_item_reset(&session->aob, mem);
2029
0
      return NGHTTP2_ERR_DEFERRED;
2030
0
    }
2031
0
    if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
2032
0
      session_detach_stream_item(session, stream);
2033
2034
0
      rv = nghttp2_session_add_rst_stream(session, frame->hd.stream_id,
2035
0
                                          NGHTTP2_INTERNAL_ERROR);
2036
0
      if (nghttp2_is_fatal(rv)) {
2037
0
        return rv;
2038
0
      }
2039
0
      return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
2040
0
    }
2041
0
    if (rv != 0) {
2042
0
      session_detach_stream_item(session, stream);
2043
2044
0
      return rv;
2045
0
    }
2046
0
    return 0;
2047
0
  }
2048
0
  case NGHTTP2_HEADERS: {
2049
0
    nghttp2_headers_aux_data *aux_data;
2050
0
    size_t estimated_payloadlen;
2051
2052
0
    aux_data = &item->aux_data.headers;
2053
2054
0
    if (frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
2055
      /* initial HEADERS, which opens stream */
2056
0
      nghttp2_stream *stream;
2057
2058
0
      stream = nghttp2_session_open_stream(
2059
0
        session, frame->hd.stream_id, NGHTTP2_STREAM_FLAG_NONE,
2060
0
        NGHTTP2_STREAM_INITIAL, aux_data->stream_user_data);
2061
2062
0
      if (stream == NULL) {
2063
0
        return NGHTTP2_ERR_NOMEM;
2064
0
      }
2065
2066
0
      rv = session_predicate_request_headers_send(session, item);
2067
0
      if (rv != 0) {
2068
0
        return rv;
2069
0
      }
2070
2071
0
      if (session_enforce_http_messaging(session)) {
2072
0
        nghttp2_http_record_request_method(stream, frame);
2073
0
      }
2074
0
    } else {
2075
0
      nghttp2_stream *stream;
2076
2077
0
      stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
2078
2079
0
      if (stream && stream->state == NGHTTP2_STREAM_RESERVED) {
2080
0
        rv = session_predicate_push_response_headers_send(session, stream);
2081
0
        if (rv == 0) {
2082
0
          frame->headers.cat = NGHTTP2_HCAT_PUSH_RESPONSE;
2083
2084
0
          if (aux_data->stream_user_data) {
2085
0
            stream->stream_user_data = aux_data->stream_user_data;
2086
0
          }
2087
0
        }
2088
0
      } else if (session_predicate_response_headers_send(session, stream) ==
2089
0
                 0) {
2090
0
        frame->headers.cat = NGHTTP2_HCAT_RESPONSE;
2091
0
        rv = 0;
2092
0
      } else {
2093
0
        frame->headers.cat = NGHTTP2_HCAT_HEADERS;
2094
2095
0
        rv = session_predicate_headers_send(session, stream);
2096
0
      }
2097
2098
0
      if (rv != 0) {
2099
0
        return rv;
2100
0
      }
2101
0
    }
2102
2103
0
    estimated_payloadlen = session_estimate_headers_payload(
2104
0
      session, frame->headers.nva, frame->headers.nvlen,
2105
0
      NGHTTP2_PRIORITY_SPECLEN);
2106
2107
0
    if (estimated_payloadlen > session->max_send_header_block_length) {
2108
0
      return NGHTTP2_ERR_FRAME_SIZE_ERROR;
2109
0
    }
2110
2111
0
    rv = nghttp2_frame_pack_headers(&session->aob.framebufs, &frame->headers,
2112
0
                                    &session->hd_deflater);
2113
2114
0
    if (rv != 0) {
2115
0
      return rv;
2116
0
    }
2117
2118
0
    DEBUGF("send: before padding, HEADERS serialized in %zu bytes\n",
2119
0
           nghttp2_bufs_len(&session->aob.framebufs));
2120
2121
0
    rv = session_headers_add_pad(session, frame);
2122
2123
0
    if (rv != 0) {
2124
0
      return rv;
2125
0
    }
2126
2127
0
    DEBUGF("send: HEADERS finally serialized in %zu bytes\n",
2128
0
           nghttp2_bufs_len(&session->aob.framebufs));
2129
2130
0
    if (frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
2131
0
      assert(session->last_sent_stream_id < frame->hd.stream_id);
2132
0
      session->last_sent_stream_id = frame->hd.stream_id;
2133
0
    }
2134
2135
0
    return 0;
2136
0
  }
2137
0
  case NGHTTP2_PRIORITY: {
2138
0
    if (session_is_closing(session)) {
2139
0
      return NGHTTP2_ERR_SESSION_CLOSING;
2140
0
    }
2141
    /* PRIORITY frame can be sent at any time and to any stream
2142
       ID. */
2143
0
    nghttp2_frame_pack_priority(&session->aob.framebufs, &frame->priority);
2144
2145
    /* Peer can send PRIORITY frame against idle stream to create
2146
       "anchor" in dependency tree.  Only client can do this in
2147
       nghttp2.  In nghttp2, only server retains non-active (closed
2148
       or idle) streams in memory, so we don't open stream here. */
2149
0
    return 0;
2150
0
  }
2151
0
  case NGHTTP2_RST_STREAM:
2152
0
    if (session_is_closing(session)) {
2153
0
      return NGHTTP2_ERR_SESSION_CLOSING;
2154
0
    }
2155
2156
0
    if (!item->aux_data.rst_stream.continue_without_stream &&
2157
0
        !nghttp2_session_get_stream(session, frame->rst_stream.hd.stream_id)) {
2158
0
      return NGHTTP2_ERR_STREAM_CLOSED;
2159
0
    }
2160
2161
0
    nghttp2_frame_pack_rst_stream(&session->aob.framebufs, &frame->rst_stream);
2162
0
    return 0;
2163
0
  case NGHTTP2_SETTINGS: {
2164
0
    if (frame->hd.flags & NGHTTP2_FLAG_ACK) {
2165
0
      assert(session->obq_flood_counter_ > 0);
2166
0
      --session->obq_flood_counter_;
2167
      /* When session is about to close, don't send SETTINGS ACK.
2168
         We are required to send SETTINGS without ACK though; for
2169
         example, we have to send SETTINGS as a part of connection
2170
         preface. */
2171
0
      if (session_is_closing(session)) {
2172
0
        return NGHTTP2_ERR_SESSION_CLOSING;
2173
0
      }
2174
0
    }
2175
2176
0
    rv = nghttp2_frame_pack_settings(&session->aob.framebufs, &frame->settings);
2177
0
    if (rv != 0) {
2178
0
      return rv;
2179
0
    }
2180
0
    return 0;
2181
0
  }
2182
0
  case NGHTTP2_PUSH_PROMISE: {
2183
0
    nghttp2_stream *stream;
2184
0
    size_t estimated_payloadlen;
2185
2186
    /* stream could be NULL if associated stream was already
2187
       closed. */
2188
0
    stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
2189
2190
    /* predicate should fail if stream is NULL. */
2191
0
    rv = session_predicate_push_promise_send(session, stream);
2192
0
    if (rv != 0) {
2193
0
      return rv;
2194
0
    }
2195
2196
0
    assert(stream);
2197
2198
0
    estimated_payloadlen = session_estimate_headers_payload(
2199
0
      session, frame->push_promise.nva, frame->push_promise.nvlen, 0);
2200
2201
0
    if (estimated_payloadlen > session->max_send_header_block_length) {
2202
0
      return NGHTTP2_ERR_FRAME_SIZE_ERROR;
2203
0
    }
2204
2205
0
    rv = nghttp2_frame_pack_push_promise(
2206
0
      &session->aob.framebufs, &frame->push_promise, &session->hd_deflater);
2207
0
    if (rv != 0) {
2208
0
      return rv;
2209
0
    }
2210
0
    rv = session_headers_add_pad(session, frame);
2211
0
    if (rv != 0) {
2212
0
      return rv;
2213
0
    }
2214
2215
0
    assert(session->last_sent_stream_id + 2 <=
2216
0
           frame->push_promise.promised_stream_id);
2217
0
    session->last_sent_stream_id = frame->push_promise.promised_stream_id;
2218
2219
0
    return 0;
2220
0
  }
2221
0
  case NGHTTP2_PING:
2222
0
    if (frame->hd.flags & NGHTTP2_FLAG_ACK) {
2223
0
      assert(session->obq_flood_counter_ > 0);
2224
0
      --session->obq_flood_counter_;
2225
0
    }
2226
    /* PING frame is allowed to be sent unless termination GOAWAY is
2227
       sent */
2228
0
    if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
2229
0
      return NGHTTP2_ERR_SESSION_CLOSING;
2230
0
    }
2231
0
    nghttp2_frame_pack_ping(&session->aob.framebufs, &frame->ping);
2232
0
    return 0;
2233
0
  case NGHTTP2_GOAWAY:
2234
0
    rv = nghttp2_frame_pack_goaway(&session->aob.framebufs, &frame->goaway);
2235
0
    if (rv != 0) {
2236
0
      return rv;
2237
0
    }
2238
0
    session->local_last_stream_id = frame->goaway.last_stream_id;
2239
2240
0
    return 0;
2241
0
  case NGHTTP2_WINDOW_UPDATE:
2242
0
    rv = session_predicate_window_update_send(session, frame->hd.stream_id);
2243
0
    if (rv != 0) {
2244
0
      return rv;
2245
0
    }
2246
0
    nghttp2_frame_pack_window_update(&session->aob.framebufs,
2247
0
                                     &frame->window_update);
2248
0
    return 0;
2249
0
  case NGHTTP2_CONTINUATION:
2250
    /* We never handle CONTINUATION here. */
2251
0
    assert(0);
2252
0
    return 0;
2253
0
  default: {
2254
0
    nghttp2_ext_aux_data *aux_data;
2255
2256
    /* extension frame */
2257
2258
0
    aux_data = &item->aux_data.ext;
2259
2260
0
    if (aux_data->builtin == 0) {
2261
0
      if (session_is_closing(session)) {
2262
0
        return NGHTTP2_ERR_SESSION_CLOSING;
2263
0
      }
2264
2265
0
      return session_pack_extension(session, &session->aob.framebufs, frame);
2266
0
    }
2267
2268
0
    switch (frame->hd.type) {
2269
0
    case NGHTTP2_ALTSVC:
2270
0
      rv = session_predicate_altsvc_send(session, frame->hd.stream_id);
2271
0
      if (rv != 0) {
2272
0
        return rv;
2273
0
      }
2274
2275
0
      nghttp2_frame_pack_altsvc(&session->aob.framebufs, &frame->ext);
2276
2277
0
      return 0;
2278
0
    case NGHTTP2_ORIGIN:
2279
0
      rv = session_predicate_origin_send(session);
2280
0
      if (rv != 0) {
2281
0
        return rv;
2282
0
      }
2283
2284
0
      rv = nghttp2_frame_pack_origin(&session->aob.framebufs, &frame->ext);
2285
0
      if (rv != 0) {
2286
0
        return rv;
2287
0
      }
2288
2289
0
      return 0;
2290
0
    case NGHTTP2_PRIORITY_UPDATE: {
2291
0
      nghttp2_ext_priority_update *priority_update = frame->ext.payload;
2292
0
      rv = session_predicate_priority_update_send(session,
2293
0
                                                  priority_update->stream_id);
2294
0
      if (rv != 0) {
2295
0
        return rv;
2296
0
      }
2297
2298
0
      nghttp2_frame_pack_priority_update(&session->aob.framebufs, &frame->ext);
2299
2300
0
      return 0;
2301
0
    }
2302
0
    default:
2303
      /* Unreachable here */
2304
0
      assert(0);
2305
0
      return 0;
2306
0
    }
2307
0
  }
2308
0
  }
2309
0
}
2310
2311
nghttp2_outbound_item *
2312
0
nghttp2_session_get_next_ob_item(nghttp2_session *session) {
2313
0
  if (nghttp2_outbound_queue_top(&session->ob_urgent)) {
2314
0
    return nghttp2_outbound_queue_top(&session->ob_urgent);
2315
0
  }
2316
2317
0
  if (nghttp2_outbound_queue_top(&session->ob_reg)) {
2318
0
    return nghttp2_outbound_queue_top(&session->ob_reg);
2319
0
  }
2320
2321
0
  if (!session_is_outgoing_concurrent_streams_max(session)) {
2322
0
    if (nghttp2_outbound_queue_top(&session->ob_syn)) {
2323
0
      return nghttp2_outbound_queue_top(&session->ob_syn);
2324
0
    }
2325
0
  }
2326
2327
0
  if (session->remote_window_size > 0) {
2328
0
    return session_sched_get_next_outbound_item(session);
2329
0
  }
2330
2331
0
  return NULL;
2332
0
}
2333
2334
nghttp2_outbound_item *
2335
0
nghttp2_session_pop_next_ob_item(nghttp2_session *session) {
2336
0
  nghttp2_outbound_item *item;
2337
2338
0
  item = nghttp2_outbound_queue_top(&session->ob_urgent);
2339
0
  if (item) {
2340
0
    nghttp2_outbound_queue_pop(&session->ob_urgent);
2341
0
    item->queued = 0;
2342
0
    return item;
2343
0
  }
2344
2345
0
  item = nghttp2_outbound_queue_top(&session->ob_reg);
2346
0
  if (item) {
2347
0
    nghttp2_outbound_queue_pop(&session->ob_reg);
2348
0
    item->queued = 0;
2349
0
    return item;
2350
0
  }
2351
2352
0
  if (!session_is_outgoing_concurrent_streams_max(session)) {
2353
0
    item = nghttp2_outbound_queue_top(&session->ob_syn);
2354
0
    if (item) {
2355
0
      nghttp2_outbound_queue_pop(&session->ob_syn);
2356
0
      item->queued = 0;
2357
0
      return item;
2358
0
    }
2359
0
  }
2360
2361
0
  if (session->remote_window_size > 0) {
2362
0
    return session_sched_get_next_outbound_item(session);
2363
0
  }
2364
2365
0
  return NULL;
2366
0
}
2367
2368
static int session_call_before_frame_send(nghttp2_session *session,
2369
0
                                          nghttp2_frame *frame) {
2370
0
  int rv;
2371
0
  if (session->callbacks.before_frame_send_callback) {
2372
0
    rv = session->callbacks.before_frame_send_callback(session, frame,
2373
0
                                                       session->user_data);
2374
0
    if (rv == NGHTTP2_ERR_CANCEL) {
2375
0
      return rv;
2376
0
    }
2377
2378
0
    if (rv != 0) {
2379
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
2380
0
    }
2381
0
  }
2382
0
  return 0;
2383
0
}
2384
2385
static int session_call_on_frame_send(nghttp2_session *session,
2386
0
                                      nghttp2_frame *frame) {
2387
0
  int rv;
2388
0
  if (session->callbacks.on_frame_send_callback) {
2389
0
    rv = session->callbacks.on_frame_send_callback(session, frame,
2390
0
                                                   session->user_data);
2391
0
    if (rv != 0) {
2392
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
2393
0
    }
2394
0
  }
2395
0
  return 0;
2396
0
}
2397
2398
0
static int find_stream_on_goaway_func(void *entry, void *ptr) {
2399
0
  nghttp2_close_stream_on_goaway_arg *arg;
2400
0
  nghttp2_stream *stream;
2401
2402
0
  arg = (nghttp2_close_stream_on_goaway_arg *)ptr;
2403
0
  stream = (nghttp2_stream *)entry;
2404
2405
0
  if (nghttp2_session_is_my_stream_id(arg->session, stream->stream_id)) {
2406
0
    if (arg->incoming) {
2407
0
      return 0;
2408
0
    }
2409
0
  } else if (!arg->incoming) {
2410
0
    return 0;
2411
0
  }
2412
2413
0
  if (stream->state != NGHTTP2_STREAM_IDLE &&
2414
0
      (stream->flags & NGHTTP2_STREAM_FLAG_CLOSED) == 0 &&
2415
0
      stream->stream_id > arg->last_stream_id) {
2416
    /* We are collecting streams to close because we cannot call
2417
       nghttp2_session_close_stream() inside nghttp2_map_each().
2418
       Reuse closed_next member.. bad choice? */
2419
0
    assert(stream->closed_next == NULL);
2420
2421
0
    if (arg->head) {
2422
0
      stream->closed_next = arg->head;
2423
0
      arg->head = stream;
2424
0
    } else {
2425
0
      arg->head = stream;
2426
0
    }
2427
0
  }
2428
2429
0
  return 0;
2430
0
}
2431
2432
/* Closes non-idle and non-closed streams whose stream ID >
2433
   last_stream_id.  If incoming is nonzero, we are going to close
2434
   incoming streams.  Otherwise, close outgoing streams. */
2435
static int session_close_stream_on_goaway(nghttp2_session *session,
2436
                                          int32_t last_stream_id,
2437
0
                                          int incoming) {
2438
0
  int rv;
2439
0
  nghttp2_stream *stream, *next_stream;
2440
0
  nghttp2_close_stream_on_goaway_arg arg = {session, NULL, last_stream_id,
2441
0
                                            incoming};
2442
2443
0
  rv = nghttp2_map_each(&session->streams, find_stream_on_goaway_func, &arg);
2444
0
  assert(rv == 0);
2445
2446
0
  stream = arg.head;
2447
0
  while (stream) {
2448
0
    next_stream = stream->closed_next;
2449
0
    stream->closed_next = NULL;
2450
0
    rv = nghttp2_session_close_stream(session, stream->stream_id,
2451
0
                                      NGHTTP2_REFUSED_STREAM);
2452
2453
    /* stream may be deleted here */
2454
2455
0
    stream = next_stream;
2456
2457
0
    if (nghttp2_is_fatal(rv)) {
2458
      /* Clean up closed_next member just in case */
2459
0
      while (stream) {
2460
0
        next_stream = stream->closed_next;
2461
0
        stream->closed_next = NULL;
2462
0
        stream = next_stream;
2463
0
      }
2464
0
      return rv;
2465
0
    }
2466
0
  }
2467
2468
0
  return 0;
2469
0
}
2470
2471
static void session_reschedule_stream(nghttp2_session *session,
2472
0
                                      nghttp2_stream *stream) {
2473
0
  stream->last_writelen = stream->item->frame.hd.length;
2474
2475
0
  if (!session->server) {
2476
0
    return;
2477
0
  }
2478
2479
0
  session_sched_reschedule_stream(session, stream);
2480
0
}
2481
2482
static int session_update_stream_consumed_size(nghttp2_session *session,
2483
                                               nghttp2_stream *stream,
2484
                                               size_t delta_size);
2485
2486
static int session_update_connection_consumed_size(nghttp2_session *session,
2487
                                                   size_t delta_size);
2488
2489
/*
2490
 * Called after a frame is sent.  This function runs
2491
 * on_frame_send_callback and handles stream closure upon END_STREAM
2492
 * or RST_STREAM.  This function does not reset session->aob.  It is a
2493
 * responsibility of session_after_frame_sent2.
2494
 *
2495
 * This function returns 0 if it succeeds, or one of the following
2496
 * negative error codes:
2497
 *
2498
 * NGHTTP2_ERR_NOMEM
2499
 *     Out of memory.
2500
 * NGHTTP2_ERR_CALLBACK_FAILURE
2501
 *     The callback function failed.
2502
 */
2503
0
static int session_after_frame_sent1(nghttp2_session *session) {
2504
0
  int rv;
2505
0
  nghttp2_active_outbound_item *aob = &session->aob;
2506
0
  nghttp2_outbound_item *item = aob->item;
2507
0
  nghttp2_bufs *framebufs = &aob->framebufs;
2508
0
  nghttp2_frame *frame;
2509
0
  nghttp2_stream *stream;
2510
2511
0
  frame = &item->frame;
2512
2513
0
  if (frame->hd.type == NGHTTP2_DATA) {
2514
0
    nghttp2_data_aux_data *aux_data;
2515
2516
0
    aux_data = &item->aux_data.data;
2517
2518
0
    stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
2519
    /* We update flow control window after a frame was completely
2520
       sent. This is possible because we choose payload length not to
2521
       exceed the window */
2522
0
    session->remote_window_size -= (int32_t)frame->hd.length;
2523
0
    if (stream) {
2524
0
      stream->remote_window_size -= (int32_t)frame->hd.length;
2525
0
    }
2526
2527
0
    if (stream && aux_data->eof) {
2528
0
      session_detach_stream_item(session, stream);
2529
2530
      /* Call on_frame_send_callback after
2531
         nghttp2_stream_detach_item(), so that application can issue
2532
         nghttp2_submit_data2() in the callback. */
2533
0
      if (session->callbacks.on_frame_send_callback) {
2534
0
        rv = session_call_on_frame_send(session, frame);
2535
0
        if (nghttp2_is_fatal(rv)) {
2536
0
          return rv;
2537
0
        }
2538
0
      }
2539
2540
0
      if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
2541
0
        int stream_closed;
2542
2543
0
        stream_closed =
2544
0
          (stream->shut_flags & NGHTTP2_SHUT_RDWR) == NGHTTP2_SHUT_RDWR;
2545
2546
0
        nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
2547
2548
0
        rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
2549
0
        if (nghttp2_is_fatal(rv)) {
2550
0
          return rv;
2551
0
        }
2552
        /* stream may be NULL if it was closed */
2553
0
        if (stream_closed) {
2554
0
          stream = NULL;
2555
0
        }
2556
0
      }
2557
0
      return 0;
2558
0
    }
2559
2560
0
    if (session->callbacks.on_frame_send_callback) {
2561
0
      rv = session_call_on_frame_send(session, frame);
2562
0
      if (nghttp2_is_fatal(rv)) {
2563
0
        return rv;
2564
0
      }
2565
0
    }
2566
2567
0
    return 0;
2568
0
  }
2569
2570
  /* non-DATA frame */
2571
2572
0
  if (frame->hd.type == NGHTTP2_HEADERS ||
2573
0
      frame->hd.type == NGHTTP2_PUSH_PROMISE) {
2574
0
    if (nghttp2_bufs_next_present(framebufs)) {
2575
0
      DEBUGF("send: CONTINUATION exists, just return\n");
2576
0
      return 0;
2577
0
    }
2578
0
  }
2579
0
  rv = session_call_on_frame_send(session, frame);
2580
0
  if (nghttp2_is_fatal(rv)) {
2581
0
    return rv;
2582
0
  }
2583
0
  switch (frame->hd.type) {
2584
0
  case NGHTTP2_HEADERS: {
2585
0
    nghttp2_headers_aux_data *aux_data;
2586
2587
0
    stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
2588
0
    if (!stream) {
2589
0
      return 0;
2590
0
    }
2591
2592
0
    switch (frame->headers.cat) {
2593
0
    case NGHTTP2_HCAT_REQUEST: {
2594
0
      stream->state = NGHTTP2_STREAM_OPENING;
2595
0
      if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
2596
0
        nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
2597
0
      }
2598
0
      rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
2599
0
      if (nghttp2_is_fatal(rv)) {
2600
0
        return rv;
2601
0
      }
2602
      /* We assume aux_data is a pointer to nghttp2_headers_aux_data */
2603
0
      aux_data = &item->aux_data.headers;
2604
0
      if (nghttp2_data_provider_wrap_contains_read_callback(&aux_data->dpw)) {
2605
        /* nghttp2_submit_data_shared() makes a copy of
2606
           aux_data->dpw */
2607
0
        rv = nghttp2_submit_data_shared(session, NGHTTP2_FLAG_END_STREAM,
2608
0
                                        frame->hd.stream_id, &aux_data->dpw);
2609
0
        if (nghttp2_is_fatal(rv)) {
2610
0
          return rv;
2611
0
        }
2612
        /* TODO nghttp2_submit_data_shared() may fail if stream has
2613
           already DATA frame item.  We might have to handle it
2614
           here. */
2615
0
      }
2616
0
      return 0;
2617
0
    }
2618
0
    case NGHTTP2_HCAT_PUSH_RESPONSE:
2619
0
      stream->flags = (uint8_t)(stream->flags & ~NGHTTP2_STREAM_FLAG_PUSH);
2620
0
      ++session->num_outgoing_streams;
2621
    /* Fall through */
2622
0
    case NGHTTP2_HCAT_RESPONSE:
2623
0
      stream->state = NGHTTP2_STREAM_OPENED;
2624
    /* Fall through */
2625
0
    case NGHTTP2_HCAT_HEADERS:
2626
0
      if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
2627
0
        nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
2628
0
      }
2629
0
      rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
2630
0
      if (nghttp2_is_fatal(rv)) {
2631
0
        return rv;
2632
0
      }
2633
      /* We assume aux_data is a pointer to nghttp2_headers_aux_data */
2634
0
      aux_data = &item->aux_data.headers;
2635
0
      if (nghttp2_data_provider_wrap_contains_read_callback(&aux_data->dpw)) {
2636
0
        rv = nghttp2_submit_data_shared(session, NGHTTP2_FLAG_END_STREAM,
2637
0
                                        frame->hd.stream_id, &aux_data->dpw);
2638
0
        if (nghttp2_is_fatal(rv)) {
2639
0
          return rv;
2640
0
        }
2641
        /* TODO nghttp2_submit_data_shared() may fail if stream has
2642
           already DATA frame item.  We might have to handle it
2643
           here. */
2644
0
      }
2645
0
      return 0;
2646
0
    default:
2647
      /* Unreachable */
2648
0
      assert(0);
2649
0
      return 0;
2650
0
    }
2651
0
  }
2652
0
  case NGHTTP2_PRIORITY:
2653
0
    return 0;
2654
0
  case NGHTTP2_RST_STREAM:
2655
0
    rv = nghttp2_session_close_stream(session, frame->hd.stream_id,
2656
0
                                      frame->rst_stream.error_code);
2657
0
    if (nghttp2_is_fatal(rv)) {
2658
0
      return rv;
2659
0
    }
2660
0
    return 0;
2661
0
  case NGHTTP2_GOAWAY: {
2662
0
    nghttp2_goaway_aux_data *aux_data;
2663
2664
0
    aux_data = &item->aux_data.goaway;
2665
2666
0
    if ((aux_data->flags & NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE) == 0) {
2667
0
      if (aux_data->flags & NGHTTP2_GOAWAY_AUX_TERM_ON_SEND) {
2668
0
        session->goaway_flags |= NGHTTP2_GOAWAY_TERM_SENT;
2669
0
      }
2670
2671
0
      session->goaway_flags |= NGHTTP2_GOAWAY_SENT;
2672
2673
0
      rv = session_close_stream_on_goaway(session, frame->goaway.last_stream_id,
2674
0
                                          1);
2675
2676
0
      if (nghttp2_is_fatal(rv)) {
2677
0
        return rv;
2678
0
      }
2679
0
    }
2680
2681
0
    return 0;
2682
0
  }
2683
0
  case NGHTTP2_WINDOW_UPDATE:
2684
0
    if (frame->hd.stream_id == 0) {
2685
0
      session->window_update_queued = 0;
2686
0
      if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
2687
0
        rv = session_update_connection_consumed_size(session, 0);
2688
0
      } else {
2689
0
        rv = nghttp2_session_update_recv_connection_window_size(session, 0);
2690
0
      }
2691
2692
0
      if (nghttp2_is_fatal(rv)) {
2693
0
        return rv;
2694
0
      }
2695
2696
0
      return 0;
2697
0
    }
2698
2699
0
    stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
2700
0
    if (!stream) {
2701
0
      return 0;
2702
0
    }
2703
2704
0
    stream->window_update_queued = 0;
2705
2706
    /* We don't have to send WINDOW_UPDATE if END_STREAM from peer
2707
       is seen. */
2708
0
    if (stream->shut_flags & NGHTTP2_SHUT_RD) {
2709
0
      return 0;
2710
0
    }
2711
2712
0
    if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
2713
0
      rv = session_update_stream_consumed_size(session, stream, 0);
2714
0
    } else {
2715
0
      rv =
2716
0
        nghttp2_session_update_recv_stream_window_size(session, stream, 0, 1);
2717
0
    }
2718
2719
0
    if (nghttp2_is_fatal(rv)) {
2720
0
      return rv;
2721
0
    }
2722
2723
0
    return 0;
2724
0
  default:
2725
0
    return 0;
2726
0
  }
2727
0
}
2728
2729
/*
2730
 * Called after a frame is sent and session_after_frame_sent1.  This
2731
 * function is responsible to reset session->aob.
2732
 */
2733
0
static void session_after_frame_sent2(nghttp2_session *session) {
2734
0
  nghttp2_active_outbound_item *aob = &session->aob;
2735
0
  nghttp2_outbound_item *item = aob->item;
2736
0
  nghttp2_bufs *framebufs = &aob->framebufs;
2737
0
  nghttp2_frame *frame;
2738
0
  nghttp2_mem *mem;
2739
0
  nghttp2_stream *stream;
2740
0
  nghttp2_data_aux_data *aux_data;
2741
2742
0
  mem = &session->mem;
2743
0
  frame = &item->frame;
2744
2745
0
  if (frame->hd.type != NGHTTP2_DATA) {
2746
0
    if (frame->hd.type == NGHTTP2_HEADERS ||
2747
0
        frame->hd.type == NGHTTP2_PUSH_PROMISE) {
2748
0
      if (nghttp2_bufs_next_present(framebufs)) {
2749
0
        framebufs->cur = framebufs->cur->next;
2750
2751
0
        DEBUGF("send: next CONTINUATION frame, %zu bytes\n",
2752
0
               nghttp2_buf_len(&framebufs->cur->buf));
2753
2754
0
        return;
2755
0
      }
2756
0
    }
2757
2758
0
    active_outbound_item_reset(&session->aob, mem);
2759
2760
0
    return;
2761
0
  }
2762
2763
  /* DATA frame */
2764
2765
0
  aux_data = &item->aux_data.data;
2766
2767
  /* On EOF, we have already detached data.  Please note that
2768
     application may issue nghttp2_submit_data2() in
2769
     on_frame_send_callback (call from session_after_frame_sent1),
2770
     which attach data to stream.  We don't want to detach it. */
2771
0
  if (aux_data->eof) {
2772
0
    active_outbound_item_reset(aob, mem);
2773
2774
0
    return;
2775
0
  }
2776
2777
  /* Reset no_copy here because next write may not use this. */
2778
0
  aux_data->no_copy = 0;
2779
2780
0
  stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
2781
2782
  /* If session is closed or RST_STREAM was queued, we won't send
2783
     further data. */
2784
0
  if (nghttp2_session_predicate_data_send(session, stream) != 0) {
2785
0
    if (stream) {
2786
0
      session_detach_stream_item(session, stream);
2787
0
    }
2788
2789
0
    active_outbound_item_reset(aob, mem);
2790
2791
0
    return;
2792
0
  }
2793
2794
0
  aob->item = NULL;
2795
0
  active_outbound_item_reset(&session->aob, mem);
2796
2797
0
  return;
2798
0
}
2799
2800
static int session_call_send_data(nghttp2_session *session,
2801
                                  nghttp2_outbound_item *item,
2802
0
                                  nghttp2_bufs *framebufs) {
2803
0
  int rv;
2804
0
  nghttp2_buf *buf;
2805
0
  size_t length;
2806
0
  nghttp2_frame *frame;
2807
0
  nghttp2_data_aux_data *aux_data;
2808
2809
0
  buf = &framebufs->cur->buf;
2810
0
  frame = &item->frame;
2811
0
  length = frame->hd.length - frame->data.padlen;
2812
0
  aux_data = &item->aux_data.data;
2813
2814
0
  rv = session->callbacks.send_data_callback(session, frame, buf->pos, length,
2815
                                             /* This is fine because
2816
                                                of Common Initial
2817
                                                Sequence rule. */
2818
0
                                             &aux_data->dpw.data_prd.v2.source,
2819
0
                                             session->user_data);
2820
2821
0
  switch (rv) {
2822
0
  case 0:
2823
0
  case NGHTTP2_ERR_WOULDBLOCK:
2824
0
  case NGHTTP2_ERR_PAUSE:
2825
0
  case NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE:
2826
0
    return rv;
2827
0
  default:
2828
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
2829
0
  }
2830
0
}
2831
2832
static nghttp2_ssize nghttp2_session_mem_send_internal(nghttp2_session *session,
2833
                                                       const uint8_t **data_ptr,
2834
0
                                                       int fast_cb) {
2835
0
  int rv;
2836
0
  nghttp2_active_outbound_item *aob;
2837
0
  nghttp2_bufs *framebufs;
2838
0
  nghttp2_mem *mem;
2839
2840
0
  mem = &session->mem;
2841
0
  aob = &session->aob;
2842
0
  framebufs = &aob->framebufs;
2843
2844
0
  for (;;) {
2845
0
    switch (aob->state) {
2846
0
    case NGHTTP2_OB_POP_ITEM: {
2847
0
      nghttp2_outbound_item *item;
2848
2849
0
      item = nghttp2_session_pop_next_ob_item(session);
2850
0
      if (item == NULL) {
2851
0
        return 0;
2852
0
      }
2853
2854
0
      rv = session_prep_frame(session, item);
2855
0
      if (rv == NGHTTP2_ERR_PAUSE) {
2856
0
        return 0;
2857
0
      }
2858
0
      if (rv == NGHTTP2_ERR_DEFERRED) {
2859
0
        DEBUGF("send: frame transmission deferred\n");
2860
0
        break;
2861
0
      }
2862
0
      if (rv < 0) {
2863
0
        int32_t opened_stream_id = 0;
2864
0
        uint32_t error_code = NGHTTP2_INTERNAL_ERROR;
2865
0
        int rv2 = 0;
2866
2867
0
        DEBUGF("send: frame preparation failed with %s\n",
2868
0
               nghttp2_strerror(rv));
2869
        /* TODO If the error comes from compressor, the connection
2870
           must be closed. */
2871
0
        if (item->frame.hd.type != NGHTTP2_DATA &&
2872
0
            session->callbacks.on_frame_not_send_callback && is_non_fatal(rv)) {
2873
0
          nghttp2_frame *frame = &item->frame;
2874
          /* The library is responsible for the transmission of
2875
             WINDOW_UPDATE frame, so we don't call error callback for
2876
             it.  As for RST_STREAM, if it is not sent due to missing
2877
             stream, we also do not call error callback because it may
2878
             cause a lot of noises.*/
2879
0
          if (frame->hd.type != NGHTTP2_WINDOW_UPDATE &&
2880
0
              (frame->hd.type != NGHTTP2_RST_STREAM ||
2881
0
               rv != NGHTTP2_ERR_STREAM_CLOSED) &&
2882
0
              session->callbacks.on_frame_not_send_callback(
2883
0
                session, frame, rv, session->user_data) != 0) {
2884
0
            nghttp2_outbound_item_free(item, mem);
2885
0
            nghttp2_mem_free(mem, item);
2886
2887
0
            return NGHTTP2_ERR_CALLBACK_FAILURE;
2888
0
          }
2889
0
        }
2890
        /* We have to close stream opened by failed request HEADERS
2891
           or PUSH_PROMISE. */
2892
0
        switch (item->frame.hd.type) {
2893
0
        case NGHTTP2_HEADERS:
2894
0
          if (item->frame.headers.cat == NGHTTP2_HCAT_REQUEST) {
2895
0
            opened_stream_id = item->frame.hd.stream_id;
2896
0
            if (item->aux_data.headers.canceled) {
2897
0
              error_code = item->aux_data.headers.error_code;
2898
0
            } else {
2899
              /* Set error_code to REFUSED_STREAM so that application
2900
                 can send request again. */
2901
0
              error_code = NGHTTP2_REFUSED_STREAM;
2902
0
            }
2903
0
          }
2904
0
          break;
2905
0
        case NGHTTP2_PUSH_PROMISE:
2906
0
          opened_stream_id = item->frame.push_promise.promised_stream_id;
2907
0
          break;
2908
0
        }
2909
0
        if (opened_stream_id) {
2910
          /* careful not to override rv */
2911
0
          rv2 =
2912
0
            nghttp2_session_close_stream(session, opened_stream_id, error_code);
2913
0
        }
2914
2915
0
        nghttp2_outbound_item_free(item, mem);
2916
0
        nghttp2_mem_free(mem, item);
2917
0
        active_outbound_item_reset(aob, mem);
2918
2919
0
        if (nghttp2_is_fatal(rv2)) {
2920
0
          return rv2;
2921
0
        }
2922
2923
0
        if (rv == NGHTTP2_ERR_HEADER_COMP) {
2924
          /* If header compression error occurred, should terminate
2925
             connection. */
2926
0
          rv =
2927
0
            nghttp2_session_terminate_session(session, NGHTTP2_INTERNAL_ERROR);
2928
0
        }
2929
0
        if (nghttp2_is_fatal(rv)) {
2930
0
          return rv;
2931
0
        }
2932
0
        break;
2933
0
      }
2934
2935
0
      aob->item = item;
2936
2937
0
      nghttp2_bufs_rewind(framebufs);
2938
2939
0
      if (item->frame.hd.type != NGHTTP2_DATA) {
2940
0
        nghttp2_frame *frame;
2941
2942
0
        frame = &item->frame;
2943
2944
0
        DEBUGF("send: next frame: payloadlen=%zu, type=%u, flags=0x%02x, "
2945
0
               "stream_id=%d\n",
2946
0
               frame->hd.length, frame->hd.type, frame->hd.flags,
2947
0
               frame->hd.stream_id);
2948
2949
0
        rv = session_call_before_frame_send(session, frame);
2950
0
        if (nghttp2_is_fatal(rv)) {
2951
0
          return rv;
2952
0
        }
2953
2954
0
        if (rv == NGHTTP2_ERR_CANCEL) {
2955
0
          int32_t opened_stream_id = 0;
2956
0
          uint32_t error_code = NGHTTP2_INTERNAL_ERROR;
2957
2958
0
          if (session->callbacks.on_frame_not_send_callback) {
2959
0
            if (session->callbacks.on_frame_not_send_callback(
2960
0
                  session, frame, rv, session->user_data) != 0) {
2961
0
              return NGHTTP2_ERR_CALLBACK_FAILURE;
2962
0
            }
2963
0
          }
2964
2965
          /* We have to close stream opened by canceled request
2966
             HEADERS or PUSH_PROMISE. */
2967
0
          switch (item->frame.hd.type) {
2968
0
          case NGHTTP2_HEADERS:
2969
0
            if (item->frame.headers.cat == NGHTTP2_HCAT_REQUEST) {
2970
0
              opened_stream_id = item->frame.hd.stream_id;
2971
              /* We don't have to check
2972
                 item->aux_data.headers.canceled since it has already
2973
                 been checked. */
2974
              /* Set error_code to REFUSED_STREAM so that application
2975
                 can send request again. */
2976
0
              error_code = NGHTTP2_REFUSED_STREAM;
2977
0
            }
2978
0
            break;
2979
0
          case NGHTTP2_PUSH_PROMISE:
2980
0
            opened_stream_id = item->frame.push_promise.promised_stream_id;
2981
0
            break;
2982
0
          }
2983
0
          if (opened_stream_id) {
2984
            /* careful not to override rv */
2985
0
            int rv2;
2986
0
            rv2 = nghttp2_session_close_stream(session, opened_stream_id,
2987
0
                                               error_code);
2988
2989
0
            if (nghttp2_is_fatal(rv2)) {
2990
0
              return rv2;
2991
0
            }
2992
0
          }
2993
2994
0
          active_outbound_item_reset(aob, mem);
2995
2996
0
          break;
2997
0
        }
2998
0
      } else {
2999
0
        DEBUGF("send: next frame: DATA\n");
3000
3001
0
        if (item->aux_data.data.no_copy) {
3002
0
          aob->state = NGHTTP2_OB_SEND_NO_COPY;
3003
0
          break;
3004
0
        }
3005
0
      }
3006
3007
0
      DEBUGF("send: start transmitting frame type=%u, length=%td\n",
3008
0
             framebufs->cur->buf.pos[3],
3009
0
             framebufs->cur->buf.last - framebufs->cur->buf.pos);
3010
3011
0
      aob->state = NGHTTP2_OB_SEND_DATA;
3012
3013
0
      break;
3014
0
    }
3015
0
    case NGHTTP2_OB_SEND_DATA: {
3016
0
      size_t datalen;
3017
0
      nghttp2_buf *buf;
3018
3019
0
      buf = &framebufs->cur->buf;
3020
3021
0
      if (buf->pos == buf->last) {
3022
0
        DEBUGF("send: end transmission of a frame\n");
3023
3024
        /* Frame has completely sent */
3025
0
        if (fast_cb) {
3026
0
          session_after_frame_sent2(session);
3027
0
        } else {
3028
0
          rv = session_after_frame_sent1(session);
3029
0
          if (rv < 0) {
3030
            /* FATAL */
3031
0
            assert(nghttp2_is_fatal(rv));
3032
0
            return rv;
3033
0
          }
3034
0
          session_after_frame_sent2(session);
3035
0
        }
3036
        /* We have already adjusted the next state */
3037
0
        break;
3038
0
      }
3039
3040
0
      *data_ptr = buf->pos;
3041
0
      datalen = nghttp2_buf_len(buf);
3042
3043
      /* We increment the offset here. If send_callback does not send
3044
         everything, we will adjust it. */
3045
0
      buf->pos += datalen;
3046
3047
0
      return (nghttp2_ssize)datalen;
3048
0
    }
3049
0
    case NGHTTP2_OB_SEND_NO_COPY: {
3050
0
      nghttp2_stream *stream;
3051
0
      nghttp2_frame *frame;
3052
0
      int pause;
3053
3054
0
      DEBUGF("send: no copy DATA\n");
3055
3056
0
      frame = &aob->item->frame;
3057
3058
0
      stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
3059
0
      if (stream == NULL) {
3060
0
        DEBUGF("send: no copy DATA cancelled because stream was closed\n");
3061
3062
0
        active_outbound_item_reset(aob, mem);
3063
3064
0
        break;
3065
0
      }
3066
3067
0
      rv = session_call_send_data(session, aob->item, framebufs);
3068
0
      if (nghttp2_is_fatal(rv)) {
3069
0
        return rv;
3070
0
      }
3071
3072
0
      if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
3073
0
        session_detach_stream_item(session, stream);
3074
3075
0
        rv = nghttp2_session_add_rst_stream(session, frame->hd.stream_id,
3076
0
                                            NGHTTP2_INTERNAL_ERROR);
3077
0
        if (nghttp2_is_fatal(rv)) {
3078
0
          return rv;
3079
0
        }
3080
3081
0
        active_outbound_item_reset(aob, mem);
3082
3083
0
        break;
3084
0
      }
3085
3086
0
      if (rv == NGHTTP2_ERR_WOULDBLOCK) {
3087
0
        return 0;
3088
0
      }
3089
3090
0
      pause = (rv == NGHTTP2_ERR_PAUSE);
3091
3092
0
      rv = session_after_frame_sent1(session);
3093
0
      if (rv < 0) {
3094
0
        assert(nghttp2_is_fatal(rv));
3095
0
        return rv;
3096
0
      }
3097
0
      session_after_frame_sent2(session);
3098
3099
      /* We have already adjusted the next state */
3100
3101
0
      if (pause) {
3102
0
        return 0;
3103
0
      }
3104
3105
0
      break;
3106
0
    }
3107
0
    case NGHTTP2_OB_SEND_CLIENT_MAGIC: {
3108
0
      size_t datalen;
3109
0
      nghttp2_buf *buf;
3110
3111
0
      buf = &framebufs->cur->buf;
3112
3113
0
      if (buf->pos == buf->last) {
3114
0
        DEBUGF("send: end transmission of client magic\n");
3115
0
        active_outbound_item_reset(aob, mem);
3116
0
        break;
3117
0
      }
3118
3119
0
      *data_ptr = buf->pos;
3120
0
      datalen = nghttp2_buf_len(buf);
3121
3122
0
      buf->pos += datalen;
3123
3124
0
      return (nghttp2_ssize)datalen;
3125
0
    }
3126
0
    }
3127
0
  }
3128
0
}
3129
3130
ssize_t nghttp2_session_mem_send(nghttp2_session *session,
3131
0
                                 const uint8_t **data_ptr) {
3132
0
  return (ssize_t)nghttp2_session_mem_send2(session, data_ptr);
3133
0
}
3134
3135
nghttp2_ssize nghttp2_session_mem_send2(nghttp2_session *session,
3136
0
                                        const uint8_t **data_ptr) {
3137
0
  int rv;
3138
0
  nghttp2_ssize len;
3139
3140
0
  *data_ptr = NULL;
3141
3142
0
  len = nghttp2_session_mem_send_internal(session, data_ptr, 1);
3143
0
  if (len <= 0) {
3144
0
    return len;
3145
0
  }
3146
3147
0
  if (session->aob.item) {
3148
    /* We have to call session_after_frame_sent1 here to handle stream
3149
       closure upon transmission of frames.  Otherwise, END_STREAM may
3150
       be reached to client before we call nghttp2_session_mem_send
3151
       again and we may get exceeding number of incoming streams. */
3152
0
    rv = session_after_frame_sent1(session);
3153
0
    if (rv < 0) {
3154
0
      assert(nghttp2_is_fatal(rv));
3155
0
      return (nghttp2_ssize)rv;
3156
0
    }
3157
0
  }
3158
3159
0
  return len;
3160
0
}
3161
3162
0
int nghttp2_session_send(nghttp2_session *session) {
3163
0
  const uint8_t *data = NULL;
3164
0
  nghttp2_ssize datalen;
3165
0
  nghttp2_ssize sentlen;
3166
0
  nghttp2_bufs *framebufs;
3167
3168
0
  framebufs = &session->aob.framebufs;
3169
3170
0
  for (;;) {
3171
0
    datalen = nghttp2_session_mem_send_internal(session, &data, 0);
3172
0
    if (datalen <= 0) {
3173
0
      return (int)datalen;
3174
0
    }
3175
0
    if (session->callbacks.send_callback2) {
3176
0
      sentlen = session->callbacks.send_callback2(
3177
0
        session, data, (size_t)datalen, 0, session->user_data);
3178
0
    } else {
3179
0
      sentlen = (nghttp2_ssize)session->callbacks.send_callback(
3180
0
        session, data, (size_t)datalen, 0, session->user_data);
3181
0
    }
3182
0
    if (sentlen < 0) {
3183
0
      if (sentlen == NGHTTP2_ERR_WOULDBLOCK) {
3184
        /* Transmission canceled. Rewind the offset */
3185
0
        framebufs->cur->buf.pos -= datalen;
3186
3187
0
        return 0;
3188
0
      }
3189
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
3190
0
    }
3191
    /* Rewind the offset to the amount of unsent bytes */
3192
0
    framebufs->cur->buf.pos -= datalen - sentlen;
3193
0
  }
3194
0
}
3195
3196
static nghttp2_ssize session_recv(nghttp2_session *session, uint8_t *buf,
3197
0
                                  size_t len) {
3198
0
  nghttp2_ssize rv;
3199
3200
0
  if (session->callbacks.recv_callback2) {
3201
0
    rv = session->callbacks.recv_callback2(session, buf, len, 0,
3202
0
                                           session->user_data);
3203
0
  } else {
3204
0
    rv = (nghttp2_ssize)session->callbacks.recv_callback(session, buf, len, 0,
3205
0
                                                         session->user_data);
3206
0
  }
3207
0
  if (rv > 0) {
3208
0
    if ((size_t)rv > len) {
3209
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
3210
0
    }
3211
0
  } else if (rv < 0 && rv != NGHTTP2_ERR_WOULDBLOCK && rv != NGHTTP2_ERR_EOF) {
3212
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
3213
0
  }
3214
0
  return rv;
3215
0
}
3216
3217
static int session_call_on_begin_frame(nghttp2_session *session,
3218
0
                                       const nghttp2_frame_hd *hd) {
3219
0
  int rv;
3220
3221
0
  if (session->callbacks.on_begin_frame_callback) {
3222
0
    rv = session->callbacks.on_begin_frame_callback(session, hd,
3223
0
                                                    session->user_data);
3224
3225
0
    if (rv != 0) {
3226
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
3227
0
    }
3228
0
  }
3229
3230
0
  return 0;
3231
0
}
3232
3233
static int session_call_on_frame_received(nghttp2_session *session,
3234
0
                                          nghttp2_frame *frame) {
3235
0
  int rv;
3236
0
  if (session->callbacks.on_frame_recv_callback) {
3237
0
    rv = session->callbacks.on_frame_recv_callback(session, frame,
3238
0
                                                   session->user_data);
3239
0
    if (rv != 0) {
3240
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
3241
0
    }
3242
0
  }
3243
0
  return 0;
3244
0
}
3245
3246
static int session_call_on_begin_headers(nghttp2_session *session,
3247
0
                                         nghttp2_frame *frame) {
3248
0
  int rv;
3249
0
  DEBUGF("recv: call on_begin_headers callback stream_id=%d\n",
3250
0
         frame->hd.stream_id);
3251
0
  if (session->callbacks.on_begin_headers_callback) {
3252
0
    rv = session->callbacks.on_begin_headers_callback(session, frame,
3253
0
                                                      session->user_data);
3254
0
    if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
3255
0
      return rv;
3256
0
    }
3257
0
    if (rv != 0) {
3258
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
3259
0
    }
3260
0
  }
3261
0
  return 0;
3262
0
}
3263
3264
static int session_call_on_header(nghttp2_session *session,
3265
                                  const nghttp2_frame *frame,
3266
0
                                  const nghttp2_hd_nv *nv) {
3267
0
  int rv = 0;
3268
0
  if (session->callbacks.on_header_callback2) {
3269
0
    rv = session->callbacks.on_header_callback2(
3270
0
      session, frame, nv->name, nv->value, nv->flags, session->user_data);
3271
0
  } else if (session->callbacks.on_header_callback) {
3272
0
    rv = session->callbacks.on_header_callback(
3273
0
      session, frame, nv->name->base, nv->name->len, nv->value->base,
3274
0
      nv->value->len, nv->flags, session->user_data);
3275
0
  }
3276
3277
0
  if (rv == NGHTTP2_ERR_PAUSE || rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
3278
0
    return rv;
3279
0
  }
3280
0
  if (rv != 0) {
3281
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
3282
0
  }
3283
3284
0
  return 0;
3285
0
}
3286
3287
static int session_call_on_invalid_header(nghttp2_session *session,
3288
                                          const nghttp2_frame *frame,
3289
0
                                          const nghttp2_hd_nv *nv) {
3290
0
  int rv;
3291
0
  if (session->callbacks.on_invalid_header_callback2) {
3292
0
    rv = session->callbacks.on_invalid_header_callback2(
3293
0
      session, frame, nv->name, nv->value, nv->flags, session->user_data);
3294
0
  } else if (session->callbacks.on_invalid_header_callback) {
3295
0
    rv = session->callbacks.on_invalid_header_callback(
3296
0
      session, frame, nv->name->base, nv->name->len, nv->value->base,
3297
0
      nv->value->len, nv->flags, session->user_data);
3298
0
  } else {
3299
    /* If both callbacks are not set, the invalid field nv is
3300
       ignored. */
3301
0
    return 0;
3302
0
  }
3303
3304
0
  if (rv == NGHTTP2_ERR_PAUSE || rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
3305
0
    return rv;
3306
0
  }
3307
0
  if (rv != 0) {
3308
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
3309
0
  }
3310
3311
0
  return 0;
3312
0
}
3313
3314
static int
3315
session_call_on_extension_chunk_recv_callback(nghttp2_session *session,
3316
0
                                              const uint8_t *data, size_t len) {
3317
0
  int rv;
3318
0
  nghttp2_inbound_frame *iframe = &session->iframe;
3319
0
  nghttp2_frame *frame = &iframe->frame;
3320
3321
0
  if (session->callbacks.on_extension_chunk_recv_callback) {
3322
0
    rv = session->callbacks.on_extension_chunk_recv_callback(
3323
0
      session, &frame->hd, data, len, session->user_data);
3324
0
    if (rv == NGHTTP2_ERR_CANCEL) {
3325
0
      return rv;
3326
0
    }
3327
0
    if (rv != 0) {
3328
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
3329
0
    }
3330
0
  }
3331
3332
0
  return 0;
3333
0
}
3334
3335
0
static int session_call_unpack_extension_callback(nghttp2_session *session) {
3336
0
  int rv;
3337
0
  nghttp2_inbound_frame *iframe = &session->iframe;
3338
0
  nghttp2_frame *frame = &iframe->frame;
3339
0
  void *payload = NULL;
3340
3341
0
  rv = session->callbacks.unpack_extension_callback(
3342
0
    session, &payload, &frame->hd, session->user_data);
3343
0
  if (rv == NGHTTP2_ERR_CANCEL) {
3344
0
    return rv;
3345
0
  }
3346
0
  if (rv != 0) {
3347
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
3348
0
  }
3349
3350
0
  frame->ext.payload = payload;
3351
3352
0
  return 0;
3353
0
}
3354
3355
/*
3356
 * Handles frame size error.
3357
 *
3358
 * This function returns 0 if it succeeds, or one of the following
3359
 * negative error codes:
3360
 *
3361
 * NGHTTP2_ERR_NOMEM
3362
 *   Out of memory.
3363
 */
3364
0
static int session_handle_frame_size_error(nghttp2_session *session) {
3365
  /* TODO Currently no callback is called for this error, because we
3366
     call this callback before reading any payload */
3367
0
  return nghttp2_session_terminate_session(session, NGHTTP2_FRAME_SIZE_ERROR);
3368
0
}
3369
3370
0
static uint32_t get_error_code_from_lib_error_code(int lib_error_code) {
3371
0
  switch (lib_error_code) {
3372
0
  case NGHTTP2_ERR_STREAM_CLOSED:
3373
0
    return NGHTTP2_STREAM_CLOSED;
3374
0
  case NGHTTP2_ERR_HEADER_COMP:
3375
0
    return NGHTTP2_COMPRESSION_ERROR;
3376
0
  case NGHTTP2_ERR_FRAME_SIZE_ERROR:
3377
0
    return NGHTTP2_FRAME_SIZE_ERROR;
3378
0
  case NGHTTP2_ERR_FLOW_CONTROL:
3379
0
    return NGHTTP2_FLOW_CONTROL_ERROR;
3380
0
  case NGHTTP2_ERR_REFUSED_STREAM:
3381
0
    return NGHTTP2_REFUSED_STREAM;
3382
0
  case NGHTTP2_ERR_PROTO:
3383
0
  case NGHTTP2_ERR_HTTP_HEADER:
3384
0
  case NGHTTP2_ERR_HTTP_MESSAGING:
3385
0
    return NGHTTP2_PROTOCOL_ERROR;
3386
0
  case NGHTTP2_ERR_INTERNAL:
3387
0
    return NGHTTP2_INTERNAL_ERROR;
3388
0
  case NGHTTP2_ERR_PUSH_CANCEL:
3389
0
    return NGHTTP2_CANCEL;
3390
0
  default:
3391
0
    return NGHTTP2_INTERNAL_ERROR;
3392
0
  }
3393
0
}
3394
3395
/*
3396
 * Calls on_invalid_frame_recv_callback if it is set to |session|.
3397
 *
3398
 * This function returns 0 if it succeeds, or one of the following
3399
 * negative error codes:
3400
 *
3401
 * NGHTTP2_ERR_CALLBACK_FAILURE
3402
 *   User defined callback function fails.
3403
 */
3404
static int session_call_on_invalid_frame_recv_callback(nghttp2_session *session,
3405
                                                       nghttp2_frame *frame,
3406
0
                                                       int lib_error_code) {
3407
0
  if (session->callbacks.on_invalid_frame_recv_callback) {
3408
0
    if (session->callbacks.on_invalid_frame_recv_callback(
3409
0
          session, frame, lib_error_code, session->user_data) != 0) {
3410
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
3411
0
    }
3412
0
  }
3413
0
  return 0;
3414
0
}
3415
3416
0
static int session_update_glitch_ratelim(nghttp2_session *session) {
3417
0
  if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
3418
0
    return 0;
3419
0
  }
3420
3421
0
  nghttp2_ratelim_update(&session->glitch_ratelim, nghttp2_time_now_sec());
3422
3423
0
  if (nghttp2_ratelim_drain(&session->glitch_ratelim, 1) == 0) {
3424
0
    return 0;
3425
0
  }
3426
3427
0
  return nghttp2_session_terminate_session(session, NGHTTP2_ENHANCE_YOUR_CALM);
3428
0
}
3429
3430
static int session_handle_invalid_stream2(nghttp2_session *session,
3431
                                          int32_t stream_id,
3432
                                          nghttp2_frame *frame,
3433
0
                                          int lib_error_code) {
3434
0
  int rv;
3435
3436
0
  rv = nghttp2_session_add_rst_stream(
3437
0
    session, stream_id, get_error_code_from_lib_error_code(lib_error_code));
3438
0
  if (rv != 0) {
3439
0
    return rv;
3440
0
  }
3441
0
  if (frame && session->callbacks.on_invalid_frame_recv_callback) {
3442
0
    if (session->callbacks.on_invalid_frame_recv_callback(
3443
0
          session, frame, lib_error_code, session->user_data) != 0) {
3444
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
3445
0
    }
3446
0
  }
3447
0
  return 0;
3448
0
}
3449
3450
static int session_handle_invalid_stream(nghttp2_session *session,
3451
                                         nghttp2_frame *frame,
3452
0
                                         int lib_error_code) {
3453
0
  return session_handle_invalid_stream2(session, frame->hd.stream_id, frame,
3454
0
                                        lib_error_code);
3455
0
}
3456
3457
static int session_inflate_handle_invalid_stream(nghttp2_session *session,
3458
                                                 nghttp2_frame *frame,
3459
0
                                                 int lib_error_code) {
3460
0
  int rv;
3461
0
  rv = session_handle_invalid_stream(session, frame, lib_error_code);
3462
0
  if (nghttp2_is_fatal(rv)) {
3463
0
    return rv;
3464
0
  }
3465
0
  return NGHTTP2_ERR_IGN_HEADER_BLOCK;
3466
0
}
3467
3468
/*
3469
 * Handles invalid frame which causes connection error.
3470
 */
3471
static int session_handle_invalid_connection(nghttp2_session *session,
3472
                                             nghttp2_frame *frame,
3473
                                             int lib_error_code,
3474
0
                                             const char *reason) {
3475
0
  if (session->callbacks.on_invalid_frame_recv_callback) {
3476
0
    if (session->callbacks.on_invalid_frame_recv_callback(
3477
0
          session, frame, lib_error_code, session->user_data) != 0) {
3478
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
3479
0
    }
3480
0
  }
3481
0
  return nghttp2_session_terminate_session_with_reason(
3482
0
    session, get_error_code_from_lib_error_code(lib_error_code), reason);
3483
0
}
3484
3485
static int session_inflate_handle_invalid_connection(nghttp2_session *session,
3486
                                                     nghttp2_frame *frame,
3487
                                                     int lib_error_code,
3488
0
                                                     const char *reason) {
3489
0
  int rv;
3490
0
  rv =
3491
0
    session_handle_invalid_connection(session, frame, lib_error_code, reason);
3492
0
  if (nghttp2_is_fatal(rv)) {
3493
0
    return rv;
3494
0
  }
3495
0
  return NGHTTP2_ERR_IGN_HEADER_BLOCK;
3496
0
}
3497
3498
/*
3499
 * Inflates header block in the memory pointed by |in| with |inlen|
3500
 * bytes. If this function returns NGHTTP2_ERR_PAUSE, the caller must
3501
 * call this function again, until it returns 0 or one of negative
3502
 * error code.  If |call_header_cb| is zero, the on_header_callback
3503
 * are not invoked and the function never return NGHTTP2_ERR_PAUSE. If
3504
 * the given |in| is the last chunk of header block, the |final| must
3505
 * be nonzero. If header block is successfully processed (which is
3506
 * indicated by the return value 0, NGHTTP2_ERR_PAUSE or
3507
 * NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE), the number of processed
3508
 * input bytes is assigned to the |*readlen_ptr|.
3509
 *
3510
 * This function return 0 if it succeeds, or one of the negative error
3511
 * codes:
3512
 *
3513
 * NGHTTP2_ERR_CALLBACK_FAILURE
3514
 *     The callback function failed.
3515
 * NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE
3516
 *     The callback returns this error code, indicating that this
3517
 *     stream should be RST_STREAMed.
3518
 * NGHTTP2_ERR_NOMEM
3519
 *     Out of memory.
3520
 * NGHTTP2_ERR_PAUSE
3521
 *     The callback function returned NGHTTP2_ERR_PAUSE
3522
 * NGHTTP2_ERR_HEADER_COMP
3523
 *     Header decompression failed
3524
 */
3525
static int inflate_header_block(nghttp2_session *session, nghttp2_frame *frame,
3526
                                size_t *readlen_ptr, uint8_t *in, size_t inlen,
3527
0
                                int final, int call_header_cb) {
3528
0
  nghttp2_inbound_frame *iframe = &session->iframe;
3529
0
  nghttp2_ssize proclen;
3530
0
  int rv;
3531
0
  int inflate_flags;
3532
0
  nghttp2_hd_nv nv;
3533
0
  nghttp2_stream *stream;
3534
0
  nghttp2_stream *subject_stream;
3535
0
  int trailer = 0;
3536
3537
0
  *readlen_ptr = 0;
3538
0
  stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
3539
3540
0
  if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
3541
0
    subject_stream = nghttp2_session_get_stream(
3542
0
      session, frame->push_promise.promised_stream_id);
3543
0
  } else {
3544
0
    subject_stream = stream;
3545
0
    trailer = session_trailer_headers(session, stream, frame);
3546
0
  }
3547
3548
0
  DEBUGF("recv: decoding header block %zu bytes\n", inlen);
3549
0
  for (;;) {
3550
0
    inflate_flags = 0;
3551
0
    proclen = nghttp2_hd_inflate_hd_nv(&session->hd_inflater, &nv,
3552
0
                                       &inflate_flags, in, inlen, final);
3553
0
    if (nghttp2_is_fatal((int)proclen)) {
3554
0
      return (int)proclen;
3555
0
    }
3556
0
    if (proclen < 0) {
3557
0
      if (session->iframe.state == NGHTTP2_IB_READ_HEADER_BLOCK) {
3558
0
        if (subject_stream && subject_stream->state != NGHTTP2_STREAM_CLOSING) {
3559
          /* Adding RST_STREAM here is very important. It prevents
3560
             from invoking subsequent callbacks for the same stream
3561
             ID. */
3562
0
          rv = nghttp2_session_add_rst_stream(
3563
0
            session, subject_stream->stream_id, NGHTTP2_COMPRESSION_ERROR);
3564
3565
0
          if (nghttp2_is_fatal(rv)) {
3566
0
            return rv;
3567
0
          }
3568
0
        }
3569
0
      }
3570
0
      rv =
3571
0
        nghttp2_session_terminate_session(session, NGHTTP2_COMPRESSION_ERROR);
3572
0
      if (nghttp2_is_fatal(rv)) {
3573
0
        return rv;
3574
0
      }
3575
3576
0
      return NGHTTP2_ERR_HEADER_COMP;
3577
0
    }
3578
0
    in += proclen;
3579
0
    inlen -= (size_t)proclen;
3580
0
    *readlen_ptr += (size_t)proclen;
3581
3582
0
    DEBUGF("recv: proclen=%td\n", proclen);
3583
3584
0
    if (call_header_cb && (inflate_flags & NGHTTP2_HD_INFLATE_EMIT)) {
3585
0
      rv = 0;
3586
0
      if (subject_stream) {
3587
0
        if (session_enforce_http_messaging(session)) {
3588
0
          rv = nghttp2_http_on_header(session, subject_stream, frame, &nv,
3589
0
                                      trailer);
3590
3591
0
          if (rv == NGHTTP2_ERR_IGN_HTTP_HEADER) {
3592
            /* Don't overwrite rv here */
3593
0
            int rv2;
3594
3595
0
            rv2 = session_call_on_invalid_header(session, frame, &nv);
3596
0
            if (rv2 == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
3597
0
              DEBUGF("recv: HTTP error: type=%u, id=%d, header %.*s: %.*s\n",
3598
0
                     frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
3599
0
                     nv.name->base, (int)nv.value->len, nv.value->base);
3600
3601
0
              rv = session_call_error_callback(
3602
0
                session, NGHTTP2_ERR_HTTP_HEADER,
3603
0
                "Invalid HTTP header field was received: frame type: "
3604
0
                "%u, stream: %d, name: [%.*s], value: [%.*s]",
3605
0
                frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
3606
0
                nv.name->base, (int)nv.value->len, nv.value->base);
3607
3608
0
              if (nghttp2_is_fatal(rv)) {
3609
0
                return rv;
3610
0
              }
3611
3612
0
              rv = session_handle_invalid_stream2(
3613
0
                session, subject_stream->stream_id, frame,
3614
0
                NGHTTP2_ERR_HTTP_HEADER);
3615
0
              if (nghttp2_is_fatal(rv)) {
3616
0
                return rv;
3617
0
              }
3618
3619
0
              return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
3620
0
            } else {
3621
0
              if (rv2 != 0) {
3622
0
                return rv2;
3623
0
              }
3624
3625
              /* header is ignored */
3626
0
              DEBUGF("recv: HTTP ignored: type=%u, id=%d, header %.*s: %.*s\n",
3627
0
                     frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
3628
0
                     nv.name->base, (int)nv.value->len, nv.value->base);
3629
3630
0
              rv2 = session_call_error_callback(
3631
0
                session, NGHTTP2_ERR_HTTP_HEADER,
3632
0
                "Ignoring received invalid HTTP header field: frame type: "
3633
0
                "%u, stream: %d, name: [%.*s], value: [%.*s]",
3634
0
                frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
3635
0
                nv.name->base, (int)nv.value->len, nv.value->base);
3636
3637
0
              if (nghttp2_is_fatal(rv2)) {
3638
0
                return rv2;
3639
0
              }
3640
0
            }
3641
0
          }
3642
3643
0
          if (rv == NGHTTP2_ERR_HTTP_HEADER) {
3644
0
            DEBUGF("recv: HTTP error: type=%u, id=%d, header %.*s: %.*s\n",
3645
0
                   frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
3646
0
                   nv.name->base, (int)nv.value->len, nv.value->base);
3647
3648
0
            rv = session_call_error_callback(
3649
0
              session, NGHTTP2_ERR_HTTP_HEADER,
3650
0
              "Invalid HTTP header field was received: frame type: "
3651
0
              "%u, stream: %d, name: [%.*s], value: [%.*s]",
3652
0
              frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
3653
0
              nv.name->base, (int)nv.value->len, nv.value->base);
3654
3655
0
            if (nghttp2_is_fatal(rv)) {
3656
0
              return rv;
3657
0
            }
3658
3659
0
            rv =
3660
0
              session_handle_invalid_stream2(session, subject_stream->stream_id,
3661
0
                                             frame, NGHTTP2_ERR_HTTP_HEADER);
3662
0
            if (nghttp2_is_fatal(rv)) {
3663
0
              return rv;
3664
0
            }
3665
3666
0
            rv = session_update_glitch_ratelim(session);
3667
0
            if (rv != 0) {
3668
0
              return rv;
3669
0
            }
3670
3671
0
            if (iframe->state == NGHTTP2_IB_IGN_ALL) {
3672
0
              return 0;
3673
0
            }
3674
3675
0
            return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
3676
0
          }
3677
0
        }
3678
0
        if (rv == 0) {
3679
0
          rv = session_call_on_header(session, frame, &nv);
3680
          /* This handles NGHTTP2_ERR_PAUSE and
3681
             NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE as well */
3682
0
          if (rv != 0) {
3683
0
            return rv;
3684
0
          }
3685
0
        }
3686
0
      }
3687
0
    }
3688
0
    if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
3689
0
      nghttp2_hd_inflate_end_headers(&session->hd_inflater);
3690
0
      break;
3691
0
    }
3692
0
    if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 && inlen == 0) {
3693
0
      break;
3694
0
    }
3695
0
  }
3696
0
  return 0;
3697
0
}
3698
3699
/*
3700
 * Call this function when HEADERS frame was completely received.
3701
 *
3702
 * This function returns 0 if it succeeds, or one of negative error
3703
 * codes:
3704
 *
3705
 * NGHTTP2_ERR_CALLBACK_FAILURE
3706
 *     The callback function failed.
3707
 * NGHTTP2_ERR_NOMEM
3708
 *     Out of memory.
3709
 */
3710
static int session_end_stream_headers_received(nghttp2_session *session,
3711
                                               nghttp2_frame *frame,
3712
0
                                               nghttp2_stream *stream) {
3713
0
  int rv;
3714
3715
0
  assert(frame->hd.type == NGHTTP2_HEADERS);
3716
3717
0
  if (session->server && session_enforce_http_messaging(session) &&
3718
0
      frame->headers.cat == NGHTTP2_HCAT_REQUEST &&
3719
0
      !(stream->flags & NGHTTP2_STREAM_FLAG_IGNORE_CLIENT_PRIORITIES) &&
3720
0
      (stream->http_flags & NGHTTP2_HTTP_FLAG_PRIORITY)) {
3721
0
    rv = session_update_stream_priority(session, stream, stream->http_extpri);
3722
0
    if (rv != 0) {
3723
0
      assert(nghttp2_is_fatal(rv));
3724
0
      return rv;
3725
0
    }
3726
0
  }
3727
3728
0
  if ((frame->hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
3729
0
    return 0;
3730
0
  }
3731
3732
0
  nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
3733
0
  rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
3734
0
  if (nghttp2_is_fatal(rv)) {
3735
0
    return rv;
3736
0
  }
3737
3738
0
  return 0;
3739
0
}
3740
3741
0
static int session_after_header_block_received(nghttp2_session *session) {
3742
0
  int rv = 0;
3743
0
  nghttp2_frame *frame = &session->iframe.frame;
3744
0
  nghttp2_inbound_frame *iframe = &session->iframe;
3745
0
  nghttp2_stream *stream;
3746
3747
  /* We don't call on_frame_recv_callback if stream has been closed
3748
     already or being closed. */
3749
0
  stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
3750
0
  if (!stream || stream->state == NGHTTP2_STREAM_CLOSING) {
3751
0
    return 0;
3752
0
  }
3753
3754
0
  if (session_enforce_http_messaging(session)) {
3755
0
    if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
3756
0
      nghttp2_stream *subject_stream;
3757
3758
0
      subject_stream = nghttp2_session_get_stream(
3759
0
        session, frame->push_promise.promised_stream_id);
3760
0
      if (subject_stream) {
3761
0
        rv = nghttp2_http_on_request_headers(subject_stream, frame);
3762
0
      }
3763
0
    } else {
3764
0
      assert(frame->hd.type == NGHTTP2_HEADERS);
3765
0
      switch (frame->headers.cat) {
3766
0
      case NGHTTP2_HCAT_REQUEST:
3767
0
        rv = nghttp2_http_on_request_headers(stream, frame);
3768
0
        break;
3769
0
      case NGHTTP2_HCAT_RESPONSE:
3770
0
      case NGHTTP2_HCAT_PUSH_RESPONSE:
3771
0
        rv = nghttp2_http_on_response_headers(stream);
3772
0
        break;
3773
0
      case NGHTTP2_HCAT_HEADERS:
3774
0
        if (stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) {
3775
0
          assert(!session->server);
3776
0
          rv = nghttp2_http_on_response_headers(stream);
3777
0
        } else {
3778
0
          rv = nghttp2_http_on_trailer_headers(stream, frame);
3779
0
        }
3780
0
        break;
3781
0
      default:
3782
0
        assert(0);
3783
0
      }
3784
0
      if (rv == 0 && (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
3785
0
        rv = nghttp2_http_on_remote_end_stream(stream);
3786
0
      }
3787
0
    }
3788
0
    if (rv != 0) {
3789
0
      int32_t stream_id;
3790
3791
0
      if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
3792
0
        stream_id = frame->push_promise.promised_stream_id;
3793
0
      } else {
3794
0
        stream_id = frame->hd.stream_id;
3795
0
      }
3796
3797
0
      rv = session_handle_invalid_stream2(session, stream_id, frame,
3798
0
                                          NGHTTP2_ERR_HTTP_MESSAGING);
3799
0
      if (nghttp2_is_fatal(rv)) {
3800
0
        return rv;
3801
0
      }
3802
3803
0
      rv = session_update_glitch_ratelim(session);
3804
0
      if (rv != 0) {
3805
0
        return rv;
3806
0
      }
3807
3808
0
      if (iframe->state == NGHTTP2_IB_IGN_ALL) {
3809
0
        return 0;
3810
0
      }
3811
3812
0
      if (frame->hd.type == NGHTTP2_HEADERS &&
3813
0
          (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
3814
0
        nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
3815
        /* Don't call nghttp2_session_close_stream_if_shut_rdwr
3816
           because RST_STREAM has been submitted. */
3817
0
      }
3818
3819
0
      return 0;
3820
0
    }
3821
0
  }
3822
3823
0
  rv = session_call_on_frame_received(session, frame);
3824
0
  if (nghttp2_is_fatal(rv)) {
3825
0
    return rv;
3826
0
  }
3827
3828
0
  if (frame->hd.type != NGHTTP2_HEADERS) {
3829
0
    return 0;
3830
0
  }
3831
3832
0
  return session_end_stream_headers_received(session, frame, stream);
3833
0
}
3834
3835
int nghttp2_session_on_request_headers_received(nghttp2_session *session,
3836
0
                                                nghttp2_frame *frame) {
3837
0
  int rv = 0;
3838
0
  nghttp2_stream *stream;
3839
0
  if (frame->hd.stream_id == 0) {
3840
0
    return session_inflate_handle_invalid_connection(
3841
0
      session, frame, NGHTTP2_ERR_PROTO, "request HEADERS: stream_id == 0");
3842
0
  }
3843
3844
  /* If client receives idle stream from server, it is invalid
3845
     regardless stream ID is even or odd.  This is because client is
3846
     not expected to receive request from server. */
3847
0
  if (!session->server) {
3848
0
    if (session_detect_idle_stream(session, frame->hd.stream_id)) {
3849
0
      return session_inflate_handle_invalid_connection(
3850
0
        session, frame, NGHTTP2_ERR_PROTO,
3851
0
        "request HEADERS: client received request");
3852
0
    }
3853
3854
0
    return NGHTTP2_ERR_IGN_HEADER_BLOCK;
3855
0
  }
3856
3857
0
  assert(session->server);
3858
3859
0
  if (!session_is_new_peer_stream_id(session, frame->hd.stream_id)) {
3860
0
    if (frame->hd.stream_id == 0 ||
3861
0
        nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
3862
0
      return session_inflate_handle_invalid_connection(
3863
0
        session, frame, NGHTTP2_ERR_PROTO,
3864
0
        "request HEADERS: invalid stream_id");
3865
0
    }
3866
3867
    /* RFC 7540 says if an endpoint receives a HEADERS with invalid
3868
     * stream ID (e.g, numerically smaller than previous), it MUST
3869
     * issue connection error with error code PROTOCOL_ERROR.  It is a
3870
     * bit hard to detect this, since we cannot remember all streams
3871
     * we observed so far.
3872
     *
3873
     * You might imagine this is really easy.  But no.  HTTP/2 is
3874
     * asynchronous protocol, and usually client and server do not
3875
     * share the complete picture of open/closed stream status.  For
3876
     * example, after server sends RST_STREAM for a stream, client may
3877
     * send trailer HEADERS for that stream.  If naive server detects
3878
     * that, and issued connection error, then it is a bug of server
3879
     * implementation since client is not wrong if it did not get
3880
     * RST_STREAM when it issued trailer HEADERS.
3881
     *
3882
     * At the moment, we are very conservative here.  We only use
3883
     * connection error if stream ID refers idle stream, or we are
3884
     * sure that stream is half-closed(remote) or closed.  Otherwise
3885
     * we just ignore HEADERS for now.
3886
     */
3887
0
    stream = nghttp2_session_get_stream_raw(session, frame->hd.stream_id);
3888
0
    if (stream && (stream->shut_flags & NGHTTP2_SHUT_RD)) {
3889
0
      return session_inflate_handle_invalid_connection(
3890
0
        session, frame, NGHTTP2_ERR_STREAM_CLOSED, "HEADERS: stream closed");
3891
0
    }
3892
3893
0
    return NGHTTP2_ERR_IGN_HEADER_BLOCK;
3894
0
  }
3895
0
  session->last_recv_stream_id = frame->hd.stream_id;
3896
3897
0
  if (session_is_incoming_concurrent_streams_max(session)) {
3898
0
    return session_inflate_handle_invalid_connection(
3899
0
      session, frame, NGHTTP2_ERR_PROTO,
3900
0
      "request HEADERS: max concurrent streams exceeded");
3901
0
  }
3902
3903
0
  if (!session_allow_incoming_new_stream(session)) {
3904
    /* We just ignore stream after GOAWAY was sent */
3905
0
    return NGHTTP2_ERR_IGN_HEADER_BLOCK;
3906
0
  }
3907
3908
0
  if (frame->headers.pri_spec.stream_id == frame->hd.stream_id) {
3909
0
    return session_inflate_handle_invalid_connection(
3910
0
      session, frame, NGHTTP2_ERR_PROTO, "request HEADERS: depend on itself");
3911
0
  }
3912
3913
0
  if (session_is_incoming_concurrent_streams_pending_max(session)) {
3914
0
    return session_inflate_handle_invalid_stream(session, frame,
3915
0
                                                 NGHTTP2_ERR_REFUSED_STREAM);
3916
0
  }
3917
3918
0
  stream = nghttp2_session_open_stream(session, frame->hd.stream_id,
3919
0
                                       NGHTTP2_STREAM_FLAG_NONE,
3920
0
                                       NGHTTP2_STREAM_OPENING, NULL);
3921
0
  if (!stream) {
3922
0
    return NGHTTP2_ERR_NOMEM;
3923
0
  }
3924
3925
0
  session->last_proc_stream_id = session->last_recv_stream_id;
3926
3927
0
  rv = session_call_on_begin_headers(session, frame);
3928
0
  if (rv != 0) {
3929
0
    return rv;
3930
0
  }
3931
0
  return 0;
3932
0
}
3933
3934
int nghttp2_session_on_response_headers_received(nghttp2_session *session,
3935
                                                 nghttp2_frame *frame,
3936
0
                                                 nghttp2_stream *stream) {
3937
0
  int rv;
3938
  /* This function is only called if stream->state ==
3939
     NGHTTP2_STREAM_OPENING and stream_id is local side initiated. */
3940
0
  assert(stream->state == NGHTTP2_STREAM_OPENING &&
3941
0
         nghttp2_session_is_my_stream_id(session, frame->hd.stream_id));
3942
0
  if (frame->hd.stream_id == 0) {
3943
0
    return session_inflate_handle_invalid_connection(
3944
0
      session, frame, NGHTTP2_ERR_PROTO, "response HEADERS: stream_id == 0");
3945
0
  }
3946
0
  if (stream->shut_flags & NGHTTP2_SHUT_RD) {
3947
    /* half closed (remote): from the spec:
3948
3949
       If an endpoint receives additional frames for a stream that is
3950
       in this state it MUST respond with a stream error (Section
3951
       5.4.2) of type STREAM_CLOSED.
3952
3953
       We go further, and make it connection error.
3954
    */
3955
0
    return session_inflate_handle_invalid_connection(
3956
0
      session, frame, NGHTTP2_ERR_STREAM_CLOSED, "HEADERS: stream closed");
3957
0
  }
3958
0
  stream->state = NGHTTP2_STREAM_OPENED;
3959
0
  rv = session_call_on_begin_headers(session, frame);
3960
0
  if (rv != 0) {
3961
0
    return rv;
3962
0
  }
3963
0
  return 0;
3964
0
}
3965
3966
int nghttp2_session_on_push_response_headers_received(nghttp2_session *session,
3967
                                                      nghttp2_frame *frame,
3968
0
                                                      nghttp2_stream *stream) {
3969
0
  int rv = 0;
3970
0
  assert(stream->state == NGHTTP2_STREAM_RESERVED);
3971
0
  if (frame->hd.stream_id == 0) {
3972
0
    return session_inflate_handle_invalid_connection(
3973
0
      session, frame, NGHTTP2_ERR_PROTO,
3974
0
      "push response HEADERS: stream_id == 0");
3975
0
  }
3976
3977
0
  if (session->server) {
3978
0
    return session_inflate_handle_invalid_connection(
3979
0
      session, frame, NGHTTP2_ERR_PROTO,
3980
0
      "HEADERS: no HEADERS allowed from client in reserved state");
3981
0
  }
3982
3983
0
  if (session_is_incoming_concurrent_streams_max(session)) {
3984
0
    return session_inflate_handle_invalid_connection(
3985
0
      session, frame, NGHTTP2_ERR_PROTO,
3986
0
      "push response HEADERS: max concurrent streams exceeded");
3987
0
  }
3988
3989
0
  if (!session_allow_incoming_new_stream(session)) {
3990
    /* We don't accept new stream after GOAWAY was sent. */
3991
0
    return NGHTTP2_ERR_IGN_HEADER_BLOCK;
3992
0
  }
3993
3994
0
  if (session_is_incoming_concurrent_streams_pending_max(session)) {
3995
0
    return session_inflate_handle_invalid_stream(session, frame,
3996
0
                                                 NGHTTP2_ERR_REFUSED_STREAM);
3997
0
  }
3998
3999
0
  nghttp2_stream_promise_fulfilled(stream);
4000
0
  if (!nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
4001
0
    --session->num_incoming_reserved_streams;
4002
0
  }
4003
0
  ++session->num_incoming_streams;
4004
0
  rv = session_call_on_begin_headers(session, frame);
4005
0
  if (rv != 0) {
4006
0
    return rv;
4007
0
  }
4008
0
  return 0;
4009
0
}
4010
4011
int nghttp2_session_on_headers_received(nghttp2_session *session,
4012
                                        nghttp2_frame *frame,
4013
0
                                        nghttp2_stream *stream) {
4014
0
  int rv = 0;
4015
0
  if (frame->hd.stream_id == 0) {
4016
0
    return session_inflate_handle_invalid_connection(
4017
0
      session, frame, NGHTTP2_ERR_PROTO, "HEADERS: stream_id == 0");
4018
0
  }
4019
0
  if ((stream->shut_flags & NGHTTP2_SHUT_RD)) {
4020
    /* half closed (remote): from the spec:
4021
4022
       If an endpoint receives additional frames for a stream that is
4023
       in this state it MUST respond with a stream error (Section
4024
       5.4.2) of type STREAM_CLOSED.
4025
4026
       we go further, and make it connection error.
4027
    */
4028
0
    return session_inflate_handle_invalid_connection(
4029
0
      session, frame, NGHTTP2_ERR_STREAM_CLOSED, "HEADERS: stream closed");
4030
0
  }
4031
0
  if (nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
4032
0
    if (stream->state == NGHTTP2_STREAM_OPENED) {
4033
0
      rv = session_call_on_begin_headers(session, frame);
4034
0
      if (rv != 0) {
4035
0
        return rv;
4036
0
      }
4037
0
      return 0;
4038
0
    }
4039
4040
0
    return NGHTTP2_ERR_IGN_HEADER_BLOCK;
4041
0
  }
4042
  /* If this is remote peer initiated stream, it is OK unless it
4043
     has sent END_STREAM frame already. But if stream is in
4044
     NGHTTP2_STREAM_CLOSING, we discard the frame. This is a race
4045
     condition. */
4046
0
  if (stream->state != NGHTTP2_STREAM_CLOSING) {
4047
0
    rv = session_call_on_begin_headers(session, frame);
4048
0
    if (rv != 0) {
4049
0
      return rv;
4050
0
    }
4051
0
    return 0;
4052
0
  }
4053
0
  return NGHTTP2_ERR_IGN_HEADER_BLOCK;
4054
0
}
4055
4056
0
static int session_process_headers_frame(nghttp2_session *session) {
4057
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4058
0
  nghttp2_frame *frame = &iframe->frame;
4059
0
  nghttp2_stream *stream;
4060
4061
0
  nghttp2_frame_unpack_headers_payload(&frame->headers, iframe->sbuf.pos);
4062
4063
0
  stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
4064
0
  if (!stream) {
4065
0
    frame->headers.cat = NGHTTP2_HCAT_REQUEST;
4066
0
    return nghttp2_session_on_request_headers_received(session, frame);
4067
0
  }
4068
4069
0
  if (stream->state == NGHTTP2_STREAM_RESERVED) {
4070
0
    frame->headers.cat = NGHTTP2_HCAT_PUSH_RESPONSE;
4071
0
    return nghttp2_session_on_push_response_headers_received(session, frame,
4072
0
                                                             stream);
4073
0
  }
4074
4075
0
  if (stream->state == NGHTTP2_STREAM_OPENING &&
4076
0
      nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
4077
0
    frame->headers.cat = NGHTTP2_HCAT_RESPONSE;
4078
0
    return nghttp2_session_on_response_headers_received(session, frame, stream);
4079
0
  }
4080
4081
0
  frame->headers.cat = NGHTTP2_HCAT_HEADERS;
4082
0
  return nghttp2_session_on_headers_received(session, frame, stream);
4083
0
}
4084
4085
0
static int session_update_stream_reset_ratelim(nghttp2_session *session) {
4086
0
  if (!session->server || (session->goaway_flags & NGHTTP2_GOAWAY_SUBMITTED)) {
4087
0
    return 0;
4088
0
  }
4089
4090
0
  nghttp2_ratelim_update(&session->stream_reset_ratelim,
4091
0
                         nghttp2_time_now_sec());
4092
4093
0
  if (nghttp2_ratelim_drain(&session->stream_reset_ratelim, 1) == 0) {
4094
0
    return 0;
4095
0
  }
4096
4097
0
  return nghttp2_session_add_goaway(session, session->last_recv_stream_id,
4098
0
                                    NGHTTP2_INTERNAL_ERROR, NULL, 0,
4099
0
                                    NGHTTP2_GOAWAY_AUX_NONE);
4100
0
}
4101
4102
int nghttp2_session_on_rst_stream_received(nghttp2_session *session,
4103
0
                                           nghttp2_frame *frame) {
4104
0
  int rv;
4105
0
  nghttp2_stream *stream;
4106
0
  if (frame->hd.stream_id == 0) {
4107
0
    return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
4108
0
                                             "RST_STREAM: stream_id == 0");
4109
0
  }
4110
4111
0
  if (session_detect_idle_stream(session, frame->hd.stream_id)) {
4112
0
    return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
4113
0
                                             "RST_STREAM: stream in idle");
4114
0
  }
4115
4116
0
  stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
4117
0
  if (stream) {
4118
    /* We may use stream->shut_flags for strict error checking. */
4119
0
    nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
4120
0
  }
4121
4122
0
  rv = session_call_on_frame_received(session, frame);
4123
0
  if (rv != 0) {
4124
0
    return rv;
4125
0
  }
4126
0
  rv = nghttp2_session_close_stream(session, frame->hd.stream_id,
4127
0
                                    frame->rst_stream.error_code);
4128
0
  if (nghttp2_is_fatal(rv)) {
4129
0
    return rv;
4130
0
  }
4131
4132
0
  return session_update_stream_reset_ratelim(session);
4133
0
}
4134
4135
0
static int session_process_rst_stream_frame(nghttp2_session *session) {
4136
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4137
0
  nghttp2_frame *frame = &iframe->frame;
4138
4139
0
  nghttp2_frame_unpack_rst_stream_payload(&frame->rst_stream, iframe->sbuf.pos);
4140
4141
0
  return nghttp2_session_on_rst_stream_received(session, frame);
4142
0
}
4143
4144
0
static int update_remote_initial_window_size_func(void *entry, void *ptr) {
4145
0
  int rv;
4146
0
  nghttp2_update_window_size_arg *arg;
4147
0
  nghttp2_stream *stream;
4148
4149
0
  arg = (nghttp2_update_window_size_arg *)ptr;
4150
0
  stream = (nghttp2_stream *)entry;
4151
4152
0
  rv = nghttp2_stream_update_remote_initial_window_size(
4153
0
    stream, arg->new_window_size, arg->old_window_size);
4154
0
  if (rv != 0) {
4155
0
    return NGHTTP2_ERR_FLOW_CONTROL;
4156
0
  }
4157
4158
  /* If window size gets positive, push deferred DATA frame to
4159
     outbound queue. */
4160
0
  if (stream->remote_window_size > 0 &&
4161
0
      nghttp2_stream_check_deferred_by_flow_control(stream)) {
4162
0
    rv = session_resume_deferred_stream_item(
4163
0
      arg->session, stream, NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
4164
4165
0
    if (nghttp2_is_fatal(rv)) {
4166
0
      return rv;
4167
0
    }
4168
0
  }
4169
0
  return 0;
4170
0
}
4171
4172
/*
4173
 * Updates the remote initial window size of all active streams.  If
4174
 * error occurs, all streams may not be updated.
4175
 *
4176
 * This function returns 0 if it succeeds, or one of the following
4177
 * negative error codes:
4178
 *
4179
 * NGHTTP2_ERR_NOMEM
4180
 *     Out of memory.
4181
 * NGHTTP2_ERR_FLOW_CONTROL
4182
 *     Window size gets out of range.
4183
 */
4184
static int
4185
session_update_remote_initial_window_size(nghttp2_session *session,
4186
0
                                          int32_t new_initial_window_size) {
4187
0
  return nghttp2_map_each(
4188
0
    &session->streams, update_remote_initial_window_size_func,
4189
0
    &(nghttp2_update_window_size_arg){
4190
0
      .session = session,
4191
0
      .new_window_size = new_initial_window_size,
4192
0
      .old_window_size = (int32_t)session->remote_settings.initial_window_size,
4193
0
    });
4194
0
}
4195
4196
0
static int update_local_initial_window_size_func(void *entry, void *ptr) {
4197
0
  int rv;
4198
0
  nghttp2_update_window_size_arg *arg;
4199
0
  nghttp2_stream *stream;
4200
0
  arg = (nghttp2_update_window_size_arg *)ptr;
4201
0
  stream = (nghttp2_stream *)entry;
4202
0
  rv = nghttp2_stream_update_local_initial_window_size(
4203
0
    stream, arg->new_window_size, arg->old_window_size);
4204
0
  if (rv != 0) {
4205
0
    return NGHTTP2_ERR_FLOW_CONTROL;
4206
0
  }
4207
4208
0
  if (stream->window_update_queued) {
4209
0
    return 0;
4210
0
  }
4211
4212
0
  if (arg->session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
4213
0
    return session_update_stream_consumed_size(arg->session, stream, 0);
4214
0
  }
4215
4216
0
  if (nghttp2_should_send_window_update(stream->local_window_size,
4217
0
                                        stream->recv_window_size)) {
4218
0
    rv = nghttp2_session_add_window_update(arg->session, NGHTTP2_FLAG_NONE,
4219
0
                                           stream->stream_id,
4220
0
                                           stream->recv_window_size);
4221
0
    if (rv != 0) {
4222
0
      return rv;
4223
0
    }
4224
4225
0
    stream->recv_window_size = 0;
4226
0
  }
4227
0
  return 0;
4228
0
}
4229
4230
/*
4231
 * Updates the local initial window size of all active streams.  If
4232
 * error occurs, all streams may not be updated.
4233
 *
4234
 * This function returns 0 if it succeeds, or one of the following
4235
 * negative error codes:
4236
 *
4237
 * NGHTTP2_ERR_NOMEM
4238
 *     Out of memory.
4239
 * NGHTTP2_ERR_FLOW_CONTROL
4240
 *     Window size gets out of range.
4241
 */
4242
static int
4243
session_update_local_initial_window_size(nghttp2_session *session,
4244
                                         int32_t new_initial_window_size,
4245
0
                                         int32_t old_initial_window_size) {
4246
0
  return nghttp2_map_each(&session->streams,
4247
0
                          update_local_initial_window_size_func,
4248
0
                          &(nghttp2_update_window_size_arg){
4249
0
                            .session = session,
4250
0
                            .new_window_size = new_initial_window_size,
4251
0
                            .old_window_size = old_initial_window_size,
4252
0
                          });
4253
0
}
4254
4255
/*
4256
 * Apply SETTINGS values |iv| having |niv| elements to the local
4257
 * settings.  We assumes that all values in |iv| is correct, since we
4258
 * validated them in nghttp2_session_add_settings() already.
4259
 *
4260
 * This function returns 0 if it succeeds, or one of the following
4261
 * negative error codes:
4262
 *
4263
 * NGHTTP2_ERR_HEADER_COMP
4264
 *     The header table size is out of range
4265
 * NGHTTP2_ERR_NOMEM
4266
 *     Out of memory
4267
 */
4268
int nghttp2_session_update_local_settings(nghttp2_session *session,
4269
                                          nghttp2_settings_entry *iv,
4270
0
                                          size_t niv) {
4271
0
  int rv;
4272
0
  size_t i;
4273
0
  int32_t new_initial_window_size = -1;
4274
0
  uint32_t header_table_size = 0;
4275
0
  uint32_t min_header_table_size = UINT32_MAX;
4276
0
  uint8_t header_table_size_seen = 0;
4277
  /* For NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, use the value last
4278
     seen.  For NGHTTP2_SETTINGS_HEADER_TABLE_SIZE, use both minimum
4279
     value and last seen value. */
4280
0
  for (i = 0; i < niv; ++i) {
4281
0
    switch (iv[i].settings_id) {
4282
0
    case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
4283
0
      header_table_size_seen = 1;
4284
0
      header_table_size = iv[i].value;
4285
0
      min_header_table_size =
4286
0
        nghttp2_min_uint32(min_header_table_size, iv[i].value);
4287
0
      break;
4288
0
    case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
4289
0
      new_initial_window_size = (int32_t)iv[i].value;
4290
0
      break;
4291
0
    }
4292
0
  }
4293
0
  if (header_table_size_seen) {
4294
0
    if (min_header_table_size < header_table_size) {
4295
0
      rv = nghttp2_hd_inflate_change_table_size(&session->hd_inflater,
4296
0
                                                min_header_table_size);
4297
0
      if (rv != 0) {
4298
0
        return rv;
4299
0
      }
4300
0
    }
4301
4302
0
    rv = nghttp2_hd_inflate_change_table_size(&session->hd_inflater,
4303
0
                                              header_table_size);
4304
0
    if (rv != 0) {
4305
0
      return rv;
4306
0
    }
4307
0
  }
4308
0
  if (new_initial_window_size != -1) {
4309
0
    rv = session_update_local_initial_window_size(
4310
0
      session, new_initial_window_size,
4311
0
      (int32_t)session->local_settings.initial_window_size);
4312
0
    if (rv != 0) {
4313
0
      return rv;
4314
0
    }
4315
0
  }
4316
4317
0
  for (i = 0; i < niv; ++i) {
4318
0
    switch (iv[i].settings_id) {
4319
0
    case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
4320
0
      session->local_settings.header_table_size = iv[i].value;
4321
0
      break;
4322
0
    case NGHTTP2_SETTINGS_ENABLE_PUSH:
4323
0
      session->local_settings.enable_push = iv[i].value;
4324
0
      break;
4325
0
    case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
4326
0
      session->local_settings.max_concurrent_streams = iv[i].value;
4327
0
      break;
4328
0
    case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
4329
0
      session->local_settings.initial_window_size = iv[i].value;
4330
0
      break;
4331
0
    case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
4332
0
      session->local_settings.max_frame_size = iv[i].value;
4333
0
      break;
4334
0
    case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
4335
0
      session->local_settings.max_header_list_size = iv[i].value;
4336
0
      break;
4337
0
    case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
4338
0
      session->local_settings.enable_connect_protocol = iv[i].value;
4339
0
      break;
4340
0
    case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
4341
0
      session->local_settings.no_rfc7540_priorities = iv[i].value;
4342
0
      break;
4343
0
    }
4344
0
  }
4345
4346
0
  return 0;
4347
0
}
4348
4349
int nghttp2_session_on_settings_received(nghttp2_session *session,
4350
0
                                         nghttp2_frame *frame, int noack) {
4351
0
  int rv;
4352
0
  size_t i;
4353
0
  nghttp2_mem *mem;
4354
0
  nghttp2_inflight_settings *settings;
4355
4356
0
  mem = &session->mem;
4357
4358
0
  if (frame->hd.stream_id != 0) {
4359
0
    return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
4360
0
                                             "SETTINGS: stream_id != 0");
4361
0
  }
4362
0
  if (frame->hd.flags & NGHTTP2_FLAG_ACK) {
4363
0
    if (frame->settings.niv != 0) {
4364
0
      return session_handle_invalid_connection(
4365
0
        session, frame, NGHTTP2_ERR_FRAME_SIZE_ERROR,
4366
0
        "SETTINGS: ACK and payload != 0");
4367
0
    }
4368
4369
0
    settings = session->inflight_settings_head;
4370
4371
0
    if (!settings) {
4372
0
      return session_handle_invalid_connection(
4373
0
        session, frame, NGHTTP2_ERR_PROTO, "SETTINGS: unexpected ACK");
4374
0
    }
4375
4376
0
    rv = nghttp2_session_update_local_settings(session, settings->iv,
4377
0
                                               settings->niv);
4378
4379
0
    session->inflight_settings_head = settings->next;
4380
4381
0
    inflight_settings_del(settings, mem);
4382
4383
0
    if (rv != 0) {
4384
0
      if (nghttp2_is_fatal(rv)) {
4385
0
        return rv;
4386
0
      }
4387
0
      return session_handle_invalid_connection(session, frame, rv, NULL);
4388
0
    }
4389
0
    return session_call_on_frame_received(session, frame);
4390
0
  }
4391
4392
0
  if (!session->remote_settings_received) {
4393
0
    session->remote_settings.max_concurrent_streams =
4394
0
      NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS;
4395
0
    session->remote_settings_received = 1;
4396
0
  }
4397
4398
0
  for (i = 0; i < frame->settings.niv; ++i) {
4399
0
    nghttp2_settings_entry *entry = &frame->settings.iv[i];
4400
4401
0
    switch (entry->settings_id) {
4402
0
    case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
4403
4404
0
      rv = nghttp2_hd_deflate_change_table_size(&session->hd_deflater,
4405
0
                                                entry->value);
4406
0
      if (rv != 0) {
4407
0
        if (nghttp2_is_fatal(rv)) {
4408
0
          return rv;
4409
0
        } else {
4410
0
          return session_handle_invalid_connection(
4411
0
            session, frame, NGHTTP2_ERR_HEADER_COMP, NULL);
4412
0
        }
4413
0
      }
4414
4415
0
      session->remote_settings.header_table_size = entry->value;
4416
4417
0
      break;
4418
0
    case NGHTTP2_SETTINGS_ENABLE_PUSH:
4419
4420
0
      if (entry->value != 0 && entry->value != 1) {
4421
0
        return session_handle_invalid_connection(
4422
0
          session, frame, NGHTTP2_ERR_PROTO,
4423
0
          "SETTINGS: invalid SETTINGS_ENBLE_PUSH");
4424
0
      }
4425
4426
0
      if (!session->server && entry->value != 0) {
4427
0
        return session_handle_invalid_connection(
4428
0
          session, frame, NGHTTP2_ERR_PROTO,
4429
0
          "SETTINGS: server attempted to enable push");
4430
0
      }
4431
4432
0
      session->remote_settings.enable_push = entry->value;
4433
4434
0
      break;
4435
0
    case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
4436
4437
0
      session->remote_settings.max_concurrent_streams = entry->value;
4438
4439
0
      break;
4440
0
    case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
4441
4442
      /* Update the initial window size of the all active streams */
4443
      /* Check that initial_window_size < (1u << 31) */
4444
0
      if (entry->value > NGHTTP2_MAX_WINDOW_SIZE) {
4445
0
        return session_handle_invalid_connection(
4446
0
          session, frame, NGHTTP2_ERR_FLOW_CONTROL,
4447
0
          "SETTINGS: too large SETTINGS_INITIAL_WINDOW_SIZE");
4448
0
      }
4449
4450
0
      rv = session_update_remote_initial_window_size(session,
4451
0
                                                     (int32_t)entry->value);
4452
4453
0
      if (nghttp2_is_fatal(rv)) {
4454
0
        return rv;
4455
0
      }
4456
4457
0
      if (rv != 0) {
4458
0
        return session_handle_invalid_connection(
4459
0
          session, frame, NGHTTP2_ERR_FLOW_CONTROL, NULL);
4460
0
      }
4461
4462
0
      session->remote_settings.initial_window_size = entry->value;
4463
4464
0
      break;
4465
0
    case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
4466
4467
0
      if (entry->value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
4468
0
          entry->value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
4469
0
        return session_handle_invalid_connection(
4470
0
          session, frame, NGHTTP2_ERR_PROTO,
4471
0
          "SETTINGS: invalid SETTINGS_MAX_FRAME_SIZE");
4472
0
      }
4473
4474
0
      session->remote_settings.max_frame_size = entry->value;
4475
4476
0
      break;
4477
0
    case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
4478
4479
0
      session->remote_settings.max_header_list_size = entry->value;
4480
4481
0
      break;
4482
0
    case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
4483
4484
0
      if (entry->value != 0 && entry->value != 1) {
4485
0
        return session_handle_invalid_connection(
4486
0
          session, frame, NGHTTP2_ERR_PROTO,
4487
0
          "SETTINGS: invalid SETTINGS_ENABLE_CONNECT_PROTOCOL");
4488
0
      }
4489
4490
0
      if (!session->server &&
4491
0
          session->remote_settings.enable_connect_protocol &&
4492
0
          entry->value == 0) {
4493
0
        return session_handle_invalid_connection(
4494
0
          session, frame, NGHTTP2_ERR_PROTO,
4495
0
          "SETTINGS: server attempted to disable "
4496
0
          "SETTINGS_ENABLE_CONNECT_PROTOCOL");
4497
0
      }
4498
4499
0
      session->remote_settings.enable_connect_protocol = entry->value;
4500
4501
0
      break;
4502
0
    case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
4503
4504
0
      if (entry->value != 0 && entry->value != 1) {
4505
0
        return session_handle_invalid_connection(
4506
0
          session, frame, NGHTTP2_ERR_PROTO,
4507
0
          "SETTINGS: invalid SETTINGS_NO_RFC7540_PRIORITIES");
4508
0
      }
4509
4510
0
      if (session->remote_settings.no_rfc7540_priorities != UINT32_MAX &&
4511
0
          session->remote_settings.no_rfc7540_priorities != entry->value) {
4512
0
        return session_handle_invalid_connection(
4513
0
          session, frame, NGHTTP2_ERR_PROTO,
4514
0
          "SETTINGS: SETTINGS_NO_RFC7540_PRIORITIES cannot be changed");
4515
0
      }
4516
4517
0
      session->remote_settings.no_rfc7540_priorities = entry->value;
4518
4519
0
      break;
4520
0
    }
4521
0
  }
4522
4523
0
  if (session->remote_settings.no_rfc7540_priorities == UINT32_MAX) {
4524
0
    session->remote_settings.no_rfc7540_priorities = 0;
4525
0
  }
4526
4527
0
  if (!noack && !session_is_closing(session)) {
4528
0
    rv = nghttp2_session_add_settings(session, NGHTTP2_FLAG_ACK, NULL, 0);
4529
4530
0
    if (rv != 0) {
4531
0
      if (nghttp2_is_fatal(rv)) {
4532
0
        return rv;
4533
0
      }
4534
4535
0
      return session_handle_invalid_connection(session, frame,
4536
0
                                               NGHTTP2_ERR_INTERNAL, NULL);
4537
0
    }
4538
0
  }
4539
4540
0
  return session_call_on_frame_received(session, frame);
4541
0
}
4542
4543
0
static int session_process_settings_frame(nghttp2_session *session) {
4544
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4545
0
  nghttp2_frame *frame = &iframe->frame;
4546
0
  size_t i;
4547
0
  nghttp2_settings_entry min_header_size_entry;
4548
4549
0
  if (iframe->max_niv) {
4550
0
    min_header_size_entry = iframe->iv[iframe->max_niv - 1];
4551
4552
0
    if (min_header_size_entry.value < UINT32_MAX) {
4553
      /* If we have less value, then we must have
4554
         SETTINGS_HEADER_TABLE_SIZE in i < iframe->niv */
4555
0
      for (i = 0; i < iframe->niv; ++i) {
4556
0
        if (iframe->iv[i].settings_id == NGHTTP2_SETTINGS_HEADER_TABLE_SIZE) {
4557
0
          break;
4558
0
        }
4559
0
      }
4560
4561
0
      assert(i < iframe->niv);
4562
4563
0
      if (min_header_size_entry.value != iframe->iv[i].value) {
4564
0
        iframe->iv[iframe->niv++] = iframe->iv[i];
4565
0
        iframe->iv[i] = min_header_size_entry;
4566
0
      }
4567
0
    }
4568
0
  }
4569
4570
0
  nghttp2_frame_unpack_settings_payload(&frame->settings, iframe->iv,
4571
0
                                        iframe->niv);
4572
4573
0
  iframe->iv = NULL;
4574
0
  iframe->niv = 0;
4575
0
  iframe->max_niv = 0;
4576
4577
0
  return nghttp2_session_on_settings_received(session, frame, 0 /* ACK */);
4578
0
}
4579
4580
int nghttp2_session_on_push_promise_received(nghttp2_session *session,
4581
0
                                             nghttp2_frame *frame) {
4582
0
  int rv;
4583
0
  nghttp2_stream *stream;
4584
0
  nghttp2_stream *promised_stream;
4585
4586
0
  if (frame->hd.stream_id == 0) {
4587
0
    return session_inflate_handle_invalid_connection(
4588
0
      session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: stream_id == 0");
4589
0
  }
4590
0
  if (session->server || session->local_settings.enable_push == 0) {
4591
0
    return session_inflate_handle_invalid_connection(
4592
0
      session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: push disabled");
4593
0
  }
4594
4595
0
  if (!nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
4596
0
    return session_inflate_handle_invalid_connection(
4597
0
      session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: invalid stream_id");
4598
0
  }
4599
4600
0
  if (!session_allow_incoming_new_stream(session)) {
4601
    /* We just discard PUSH_PROMISE after GOAWAY was sent */
4602
0
    return NGHTTP2_ERR_IGN_HEADER_BLOCK;
4603
0
  }
4604
4605
0
  if (!session_is_new_peer_stream_id(session,
4606
0
                                     frame->push_promise.promised_stream_id)) {
4607
    /* The spec says if an endpoint receives a PUSH_PROMISE with
4608
       illegal stream ID is subject to a connection error of type
4609
       PROTOCOL_ERROR. */
4610
0
    return session_inflate_handle_invalid_connection(
4611
0
      session, frame, NGHTTP2_ERR_PROTO,
4612
0
      "PUSH_PROMISE: invalid promised_stream_id");
4613
0
  }
4614
4615
0
  if (session_detect_idle_stream(session, frame->hd.stream_id)) {
4616
0
    return session_inflate_handle_invalid_connection(
4617
0
      session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: stream in idle");
4618
0
  }
4619
4620
0
  session->last_recv_stream_id = frame->push_promise.promised_stream_id;
4621
0
  stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
4622
0
  if (!stream || stream->state == NGHTTP2_STREAM_CLOSING ||
4623
0
      !session->pending_enable_push ||
4624
0
      session->num_incoming_reserved_streams >=
4625
0
        session->max_incoming_reserved_streams) {
4626
    /* Currently, client does not retain closed stream, so we don't
4627
       check NGHTTP2_SHUT_RD condition here. */
4628
0
    rv = session_handle_invalid_stream2(session,
4629
0
                                        frame->push_promise.promised_stream_id,
4630
0
                                        NULL, NGHTTP2_ERR_PUSH_CANCEL);
4631
0
    if (rv != 0) {
4632
0
      return rv;
4633
0
    }
4634
0
    return NGHTTP2_ERR_IGN_HEADER_BLOCK;
4635
0
  }
4636
4637
0
  if (stream->shut_flags & NGHTTP2_SHUT_RD) {
4638
0
    return session_inflate_handle_invalid_connection(
4639
0
      session, frame, NGHTTP2_ERR_STREAM_CLOSED, "PUSH_PROMISE: stream closed");
4640
0
  }
4641
4642
0
  promised_stream = nghttp2_session_open_stream(
4643
0
    session, frame->push_promise.promised_stream_id, NGHTTP2_STREAM_FLAG_NONE,
4644
0
    NGHTTP2_STREAM_RESERVED, NULL);
4645
4646
0
  if (!promised_stream) {
4647
0
    return NGHTTP2_ERR_NOMEM;
4648
0
  }
4649
4650
0
  session->last_proc_stream_id = session->last_recv_stream_id;
4651
0
  rv = session_call_on_begin_headers(session, frame);
4652
0
  if (rv != 0) {
4653
0
    return rv;
4654
0
  }
4655
0
  return 0;
4656
0
}
4657
4658
0
static int session_process_push_promise_frame(nghttp2_session *session) {
4659
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4660
0
  nghttp2_frame *frame = &iframe->frame;
4661
4662
0
  nghttp2_frame_unpack_push_promise_payload(&frame->push_promise,
4663
0
                                            iframe->sbuf.pos);
4664
4665
0
  return nghttp2_session_on_push_promise_received(session, frame);
4666
0
}
4667
4668
int nghttp2_session_on_ping_received(nghttp2_session *session,
4669
0
                                     nghttp2_frame *frame) {
4670
0
  int rv = 0;
4671
0
  if (frame->hd.stream_id != 0) {
4672
0
    return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
4673
0
                                             "PING: stream_id != 0");
4674
0
  }
4675
0
  if ((session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_PING_ACK) == 0 &&
4676
0
      (frame->hd.flags & NGHTTP2_FLAG_ACK) == 0 &&
4677
0
      !session_is_closing(session)) {
4678
    /* Peer sent ping, so ping it back */
4679
0
    rv = nghttp2_session_add_ping(session, NGHTTP2_FLAG_ACK,
4680
0
                                  frame->ping.opaque_data);
4681
0
    if (rv != 0) {
4682
0
      return rv;
4683
0
    }
4684
0
  }
4685
0
  return session_call_on_frame_received(session, frame);
4686
0
}
4687
4688
0
static int session_process_ping_frame(nghttp2_session *session) {
4689
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4690
0
  nghttp2_frame *frame = &iframe->frame;
4691
4692
0
  nghttp2_frame_unpack_ping_payload(&frame->ping, iframe->sbuf.pos);
4693
4694
0
  return nghttp2_session_on_ping_received(session, frame);
4695
0
}
4696
4697
int nghttp2_session_on_goaway_received(nghttp2_session *session,
4698
0
                                       nghttp2_frame *frame) {
4699
0
  int rv;
4700
4701
0
  if (frame->hd.stream_id != 0) {
4702
0
    return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
4703
0
                                             "GOAWAY: stream_id != 0");
4704
0
  }
4705
  /* Spec says Endpoints MUST NOT increase the value they send in the
4706
     last stream identifier. */
4707
0
  if ((frame->goaway.last_stream_id > 0 &&
4708
0
       !nghttp2_session_is_my_stream_id(session,
4709
0
                                        frame->goaway.last_stream_id)) ||
4710
0
      session->remote_last_stream_id < frame->goaway.last_stream_id) {
4711
0
    return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
4712
0
                                             "GOAWAY: invalid last_stream_id");
4713
0
  }
4714
4715
0
  session->goaway_flags |= NGHTTP2_GOAWAY_RECV;
4716
4717
0
  session->remote_last_stream_id = frame->goaway.last_stream_id;
4718
4719
0
  rv = session_call_on_frame_received(session, frame);
4720
4721
0
  if (nghttp2_is_fatal(rv)) {
4722
0
    return rv;
4723
0
  }
4724
4725
0
  return session_close_stream_on_goaway(session, frame->goaway.last_stream_id,
4726
0
                                        0);
4727
0
}
4728
4729
0
static int session_process_goaway_frame(nghttp2_session *session) {
4730
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4731
0
  nghttp2_frame *frame = &iframe->frame;
4732
4733
0
  nghttp2_frame_unpack_goaway_payload(&frame->goaway, iframe->sbuf.pos,
4734
0
                                      iframe->lbuf.pos,
4735
0
                                      nghttp2_buf_len(&iframe->lbuf));
4736
4737
0
  nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
4738
4739
0
  return nghttp2_session_on_goaway_received(session, frame);
4740
0
}
4741
4742
static int
4743
session_on_connection_window_update_received(nghttp2_session *session,
4744
0
                                             nghttp2_frame *frame) {
4745
  /* Handle connection-level flow control */
4746
0
  if (frame->window_update.window_size_increment == 0) {
4747
0
    return session_handle_invalid_connection(
4748
0
      session, frame, NGHTTP2_ERR_PROTO,
4749
0
      "WINDOW_UPDATE: window_size_increment == 0");
4750
0
  }
4751
4752
0
  if (NGHTTP2_MAX_WINDOW_SIZE - frame->window_update.window_size_increment <
4753
0
      session->remote_window_size) {
4754
0
    return session_handle_invalid_connection(session, frame,
4755
0
                                             NGHTTP2_ERR_FLOW_CONTROL, NULL);
4756
0
  }
4757
0
  session->remote_window_size += frame->window_update.window_size_increment;
4758
4759
0
  return session_call_on_frame_received(session, frame);
4760
0
}
4761
4762
static int session_on_stream_window_update_received(nghttp2_session *session,
4763
0
                                                    nghttp2_frame *frame) {
4764
0
  int rv;
4765
0
  nghttp2_stream *stream;
4766
4767
0
  if (session_detect_idle_stream(session, frame->hd.stream_id)) {
4768
0
    return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
4769
0
                                             "WINDOW_UPDATE to idle stream");
4770
0
  }
4771
4772
0
  stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
4773
0
  if (!stream) {
4774
0
    return 0;
4775
0
  }
4776
0
  if (state_reserved_remote(session, stream)) {
4777
0
    return session_handle_invalid_connection(
4778
0
      session, frame, NGHTTP2_ERR_PROTO, "WINDOW_UPADATE to reserved stream");
4779
0
  }
4780
0
  if (frame->window_update.window_size_increment == 0) {
4781
0
    return session_handle_invalid_connection(
4782
0
      session, frame, NGHTTP2_ERR_PROTO,
4783
0
      "WINDOW_UPDATE: window_size_increment == 0");
4784
0
  }
4785
0
  if (NGHTTP2_MAX_WINDOW_SIZE - frame->window_update.window_size_increment <
4786
0
      stream->remote_window_size) {
4787
0
    return session_handle_invalid_connection(
4788
0
      session, frame, NGHTTP2_ERR_FLOW_CONTROL,
4789
0
      "WINDOW_UPDATE: window size overflow");
4790
0
  }
4791
0
  stream->remote_window_size += frame->window_update.window_size_increment;
4792
4793
0
  if (stream->remote_window_size > 0 &&
4794
0
      nghttp2_stream_check_deferred_by_flow_control(stream)) {
4795
0
    rv = session_resume_deferred_stream_item(
4796
0
      session, stream, NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
4797
4798
0
    if (nghttp2_is_fatal(rv)) {
4799
0
      return rv;
4800
0
    }
4801
0
  }
4802
0
  return session_call_on_frame_received(session, frame);
4803
0
}
4804
4805
int nghttp2_session_on_window_update_received(nghttp2_session *session,
4806
0
                                              nghttp2_frame *frame) {
4807
0
  if (frame->hd.stream_id == 0) {
4808
0
    return session_on_connection_window_update_received(session, frame);
4809
0
  } else {
4810
0
    return session_on_stream_window_update_received(session, frame);
4811
0
  }
4812
0
}
4813
4814
0
static int session_process_window_update_frame(nghttp2_session *session) {
4815
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4816
0
  nghttp2_frame *frame = &iframe->frame;
4817
4818
0
  nghttp2_frame_unpack_window_update_payload(&frame->window_update,
4819
0
                                             iframe->sbuf.pos);
4820
4821
0
  return nghttp2_session_on_window_update_received(session, frame);
4822
0
}
4823
4824
int nghttp2_session_on_altsvc_received(nghttp2_session *session,
4825
0
                                       nghttp2_frame *frame) {
4826
0
  nghttp2_ext_altsvc *altsvc;
4827
0
  nghttp2_stream *stream;
4828
4829
0
  altsvc = frame->ext.payload;
4830
4831
  /* session->server case has been excluded */
4832
4833
0
  if (frame->hd.stream_id == 0) {
4834
0
    if (altsvc->origin_len == 0) {
4835
0
      return session_call_on_invalid_frame_recv_callback(session, frame,
4836
0
                                                         NGHTTP2_ERR_PROTO);
4837
0
    }
4838
0
  } else {
4839
0
    if (altsvc->origin_len > 0) {
4840
0
      return session_call_on_invalid_frame_recv_callback(session, frame,
4841
0
                                                         NGHTTP2_ERR_PROTO);
4842
0
    }
4843
4844
0
    stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
4845
0
    if (!stream) {
4846
0
      return 0;
4847
0
    }
4848
4849
0
    if (stream->state == NGHTTP2_STREAM_CLOSING) {
4850
0
      return 0;
4851
0
    }
4852
0
  }
4853
4854
0
  if (altsvc->field_value_len == 0) {
4855
0
    return session_call_on_invalid_frame_recv_callback(session, frame,
4856
0
                                                       NGHTTP2_ERR_PROTO);
4857
0
  }
4858
4859
0
  return session_call_on_frame_received(session, frame);
4860
0
}
4861
4862
int nghttp2_session_on_origin_received(nghttp2_session *session,
4863
0
                                       nghttp2_frame *frame) {
4864
0
  return session_call_on_frame_received(session, frame);
4865
0
}
4866
4867
int nghttp2_session_on_priority_update_received(nghttp2_session *session,
4868
0
                                                nghttp2_frame *frame) {
4869
0
  nghttp2_ext_priority_update *priority_update;
4870
0
  nghttp2_stream *stream;
4871
0
  nghttp2_extpri extpri;
4872
0
  int rv;
4873
4874
0
  assert(session->server);
4875
4876
0
  priority_update = frame->ext.payload;
4877
4878
0
  if (frame->hd.stream_id != 0) {
4879
0
    return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
4880
0
                                             "PRIORITY_UPDATE: stream_id == 0");
4881
0
  }
4882
4883
0
  if (nghttp2_session_is_my_stream_id(session, priority_update->stream_id)) {
4884
0
    if (session_detect_idle_stream(session, priority_update->stream_id)) {
4885
0
      return session_handle_invalid_connection(
4886
0
        session, frame, NGHTTP2_ERR_PROTO,
4887
0
        "PRIORITY_UPDATE: prioritizing idle push is not allowed");
4888
0
    }
4889
4890
    /* TODO Ignore priority signal to a push stream for now */
4891
0
    return session_call_on_frame_received(session, frame);
4892
0
  }
4893
4894
0
  stream = nghttp2_session_get_stream_raw(session, priority_update->stream_id);
4895
0
  if (stream) {
4896
    /* Stream already exists. */
4897
0
    if (stream->flags & NGHTTP2_STREAM_FLAG_IGNORE_CLIENT_PRIORITIES) {
4898
0
      return session_call_on_frame_received(session, frame);
4899
0
    }
4900
0
  } else if (session_detect_idle_stream(session, priority_update->stream_id)) {
4901
0
    if (session->num_idle_streams + session->num_incoming_streams >=
4902
0
        session->local_settings.max_concurrent_streams) {
4903
0
      return session_handle_invalid_connection(
4904
0
        session, frame, NGHTTP2_ERR_PROTO,
4905
0
        "PRIORITY_UPDATE: max concurrent streams exceeded");
4906
0
    }
4907
4908
0
    stream =
4909
0
      nghttp2_session_open_stream(session, priority_update->stream_id,
4910
0
                                  NGHTTP2_FLAG_NONE, NGHTTP2_STREAM_IDLE, NULL);
4911
0
    if (!stream) {
4912
0
      return NGHTTP2_ERR_NOMEM;
4913
0
    }
4914
0
  } else {
4915
0
    return session_call_on_frame_received(session, frame);
4916
0
  }
4917
4918
0
  extpri.urgency = NGHTTP2_EXTPRI_DEFAULT_URGENCY;
4919
0
  extpri.inc = 0;
4920
4921
0
  rv = nghttp2_http_parse_priority(&extpri, priority_update->field_value,
4922
0
                                   priority_update->field_value_len);
4923
0
  if (rv != 0) {
4924
    /* Just ignore field_value if it cannot be parsed. */
4925
0
    return session_call_on_frame_received(session, frame);
4926
0
  }
4927
4928
0
  rv = session_update_stream_priority(session, stream,
4929
0
                                      nghttp2_extpri_to_uint8(&extpri));
4930
0
  if (rv != 0) {
4931
0
    if (nghttp2_is_fatal(rv)) {
4932
0
      return rv;
4933
0
    }
4934
0
  }
4935
4936
0
  return session_call_on_frame_received(session, frame);
4937
0
}
4938
4939
0
static int session_process_altsvc_frame(nghttp2_session *session) {
4940
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4941
0
  nghttp2_frame *frame = &iframe->frame;
4942
4943
0
  nghttp2_frame_unpack_altsvc_payload(
4944
0
    &frame->ext, nghttp2_get_uint16(iframe->sbuf.pos), iframe->lbuf.pos,
4945
0
    nghttp2_buf_len(&iframe->lbuf));
4946
4947
  /* nghttp2_frame_unpack_altsvc_payload steals buffer from
4948
     iframe->lbuf */
4949
0
  nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
4950
4951
0
  return nghttp2_session_on_altsvc_received(session, frame);
4952
0
}
4953
4954
0
static int session_process_origin_frame(nghttp2_session *session) {
4955
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4956
0
  nghttp2_frame *frame = &iframe->frame;
4957
0
  nghttp2_mem *mem = &session->mem;
4958
0
  int rv;
4959
4960
0
  rv = nghttp2_frame_unpack_origin_payload(&frame->ext, iframe->lbuf.pos,
4961
0
                                           nghttp2_buf_len(&iframe->lbuf), mem);
4962
0
  if (rv != 0) {
4963
0
    if (nghttp2_is_fatal(rv)) {
4964
0
      return rv;
4965
0
    }
4966
    /* Ignore ORIGIN frame which cannot be parsed. */
4967
0
    return 0;
4968
0
  }
4969
4970
0
  return nghttp2_session_on_origin_received(session, frame);
4971
0
}
4972
4973
0
static int session_process_priority_update_frame(nghttp2_session *session) {
4974
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4975
0
  nghttp2_frame *frame = &iframe->frame;
4976
4977
0
  nghttp2_frame_unpack_priority_update_payload(&frame->ext, iframe->sbuf.pos,
4978
0
                                               nghttp2_buf_len(&iframe->sbuf));
4979
4980
0
  return nghttp2_session_on_priority_update_received(session, frame);
4981
0
}
4982
4983
0
static int session_process_extension_frame(nghttp2_session *session) {
4984
0
  int rv;
4985
0
  nghttp2_inbound_frame *iframe = &session->iframe;
4986
0
  nghttp2_frame *frame = &iframe->frame;
4987
4988
0
  rv = session_call_unpack_extension_callback(session);
4989
0
  if (nghttp2_is_fatal(rv)) {
4990
0
    return rv;
4991
0
  }
4992
4993
  /* This handles the case where rv == NGHTTP2_ERR_CANCEL as well */
4994
0
  if (rv != 0) {
4995
0
    return 0;
4996
0
  }
4997
4998
0
  return session_call_on_frame_received(session, frame);
4999
0
}
5000
5001
int nghttp2_session_on_data_received(nghttp2_session *session,
5002
0
                                     nghttp2_frame *frame) {
5003
0
  int rv = 0;
5004
0
  nghttp2_stream *stream;
5005
0
  nghttp2_inbound_frame *iframe = &session->iframe;
5006
5007
  /* We don't call on_frame_recv_callback if stream has been closed
5008
     already or being closed. */
5009
0
  stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
5010
0
  if (!stream || stream->state == NGHTTP2_STREAM_CLOSING) {
5011
    /* This should be treated as stream error, but it results in lots
5012
       of RST_STREAM. So just ignore frame against nonexistent stream
5013
       for now. */
5014
0
    return 0;
5015
0
  }
5016
5017
0
  if (session_enforce_http_messaging(session) &&
5018
0
      (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
5019
0
    if (nghttp2_http_on_remote_end_stream(stream) != 0) {
5020
0
      rv = session_handle_invalid_stream2(session, stream->stream_id, frame,
5021
0
                                          NGHTTP2_ERR_HTTP_MESSAGING);
5022
0
      if (nghttp2_is_fatal(rv)) {
5023
0
        return rv;
5024
0
      }
5025
5026
0
      rv = session_update_glitch_ratelim(session);
5027
0
      if (rv != 0) {
5028
0
        return rv;
5029
0
      }
5030
5031
0
      if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5032
0
        return 0;
5033
0
      }
5034
5035
0
      nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
5036
5037
      /* Don't call nghttp2_session_close_stream_if_shut_rdwr because
5038
         RST_STREAM has been submitted. */
5039
5040
0
      return 0;
5041
0
    }
5042
0
  }
5043
5044
0
  rv = session_call_on_frame_received(session, frame);
5045
0
  if (nghttp2_is_fatal(rv)) {
5046
0
    return rv;
5047
0
  }
5048
5049
0
  if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
5050
0
    nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
5051
0
    rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
5052
0
    if (nghttp2_is_fatal(rv)) {
5053
0
      return rv;
5054
0
    }
5055
0
  }
5056
0
  return 0;
5057
0
}
5058
5059
/* For errors, this function only returns FATAL error. */
5060
0
static int session_process_data_frame(nghttp2_session *session) {
5061
0
  int rv;
5062
0
  nghttp2_frame *public_data_frame = &session->iframe.frame;
5063
0
  rv = nghttp2_session_on_data_received(session, public_data_frame);
5064
0
  if (nghttp2_is_fatal(rv)) {
5065
0
    return rv;
5066
0
  }
5067
0
  return 0;
5068
0
}
5069
5070
/*
5071
 * Now we have SETTINGS synchronization, flow control error can be
5072
 * detected strictly. If DATA frame is received with length > 0 and
5073
 * current received window size + delta length is strictly larger than
5074
 * local window size, it is subject to FLOW_CONTROL_ERROR, so return
5075
 * -1. Note that local_window_size is calculated after SETTINGS ACK is
5076
 * received from peer, so peer must honor this limit. If the resulting
5077
 * recv_window_size is strictly larger than NGHTTP2_MAX_WINDOW_SIZE,
5078
 * return -1 too.
5079
 */
5080
static int adjust_recv_window_size(int32_t *recv_window_size_ptr, size_t delta,
5081
0
                                   int32_t local_window_size) {
5082
0
  if (*recv_window_size_ptr > local_window_size - (int32_t)delta ||
5083
0
      *recv_window_size_ptr > NGHTTP2_MAX_WINDOW_SIZE - (int32_t)delta) {
5084
0
    return -1;
5085
0
  }
5086
0
  *recv_window_size_ptr += (int32_t)delta;
5087
0
  return 0;
5088
0
}
5089
5090
int nghttp2_session_update_recv_stream_window_size(nghttp2_session *session,
5091
                                                   nghttp2_stream *stream,
5092
                                                   size_t delta_size,
5093
0
                                                   int send_window_update) {
5094
0
  int rv;
5095
0
  rv = adjust_recv_window_size(&stream->recv_window_size, delta_size,
5096
0
                               stream->local_window_size);
5097
0
  if (rv != 0) {
5098
0
    return nghttp2_session_terminate_session(session,
5099
0
                                             NGHTTP2_FLOW_CONTROL_ERROR);
5100
0
  }
5101
  /* We don't have to send WINDOW_UPDATE if the data received is the
5102
     last chunk in the incoming stream. */
5103
  /* We have to use local_settings here because it is the constraint
5104
     the remote endpoint should honor. */
5105
0
  if (send_window_update &&
5106
0
      !(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) &&
5107
0
      stream->window_update_queued == 0 &&
5108
0
      nghttp2_should_send_window_update(stream->local_window_size,
5109
0
                                        stream->recv_window_size)) {
5110
0
    rv = nghttp2_session_add_window_update(
5111
0
      session, NGHTTP2_FLAG_NONE, stream->stream_id, stream->recv_window_size);
5112
0
    if (rv != 0) {
5113
0
      return rv;
5114
0
    }
5115
5116
0
    stream->recv_window_size = 0;
5117
0
  }
5118
0
  return 0;
5119
0
}
5120
5121
int nghttp2_session_update_recv_connection_window_size(nghttp2_session *session,
5122
0
                                                       size_t delta_size) {
5123
0
  int rv;
5124
0
  rv = adjust_recv_window_size(&session->recv_window_size, delta_size,
5125
0
                               session->local_window_size);
5126
0
  if (rv != 0) {
5127
0
    return nghttp2_session_terminate_session(session,
5128
0
                                             NGHTTP2_FLOW_CONTROL_ERROR);
5129
0
  }
5130
0
  if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) &&
5131
0
      session->window_update_queued == 0 &&
5132
0
      nghttp2_should_send_window_update(session->local_window_size,
5133
0
                                        session->recv_window_size)) {
5134
    /* Use stream ID 0 to update connection-level flow control
5135
       window */
5136
0
    rv = nghttp2_session_add_window_update(session, NGHTTP2_FLAG_NONE, 0,
5137
0
                                           session->recv_window_size);
5138
0
    if (rv != 0) {
5139
0
      return rv;
5140
0
    }
5141
5142
0
    session->recv_window_size = 0;
5143
0
  }
5144
0
  return 0;
5145
0
}
5146
5147
static int session_update_consumed_size(nghttp2_session *session,
5148
                                        int32_t *consumed_size_ptr,
5149
                                        int32_t *recv_window_size_ptr,
5150
                                        uint8_t window_update_queued,
5151
                                        int32_t stream_id, size_t delta_size,
5152
0
                                        int32_t local_window_size) {
5153
0
  int32_t recv_size;
5154
0
  int rv;
5155
5156
0
  if ((size_t)*consumed_size_ptr > NGHTTP2_MAX_WINDOW_SIZE - delta_size) {
5157
0
    return nghttp2_session_terminate_session(session,
5158
0
                                             NGHTTP2_FLOW_CONTROL_ERROR);
5159
0
  }
5160
5161
0
  *consumed_size_ptr += (int32_t)delta_size;
5162
5163
0
  if (window_update_queued == 0) {
5164
    /* recv_window_size may be smaller than consumed_size, because it
5165
       may be decreased by negative value with
5166
       nghttp2_submit_window_update(). */
5167
0
    recv_size = nghttp2_min_int32(*consumed_size_ptr, *recv_window_size_ptr);
5168
5169
0
    if (nghttp2_should_send_window_update(local_window_size, recv_size)) {
5170
0
      rv = nghttp2_session_add_window_update(session, NGHTTP2_FLAG_NONE,
5171
0
                                             stream_id, recv_size);
5172
5173
0
      if (rv != 0) {
5174
0
        return rv;
5175
0
      }
5176
5177
0
      *recv_window_size_ptr -= recv_size;
5178
0
      *consumed_size_ptr -= recv_size;
5179
0
    }
5180
0
  }
5181
5182
0
  return 0;
5183
0
}
5184
5185
static int session_update_stream_consumed_size(nghttp2_session *session,
5186
                                               nghttp2_stream *stream,
5187
0
                                               size_t delta_size) {
5188
0
  return session_update_consumed_size(
5189
0
    session, &stream->consumed_size, &stream->recv_window_size,
5190
0
    stream->window_update_queued, stream->stream_id, delta_size,
5191
0
    stream->local_window_size);
5192
0
}
5193
5194
static int session_update_connection_consumed_size(nghttp2_session *session,
5195
0
                                                   size_t delta_size) {
5196
0
  return session_update_consumed_size(
5197
0
    session, &session->consumed_size, &session->recv_window_size,
5198
0
    session->window_update_queued, 0, delta_size, session->local_window_size);
5199
0
}
5200
5201
/*
5202
 * Checks that we can receive the DATA frame for stream, which is
5203
 * indicated by |session->iframe.frame.hd.stream_id|. If it is a
5204
 * connection error situation, GOAWAY frame will be issued by this
5205
 * function.
5206
 *
5207
 * If the DATA frame is allowed, returns 0.
5208
 *
5209
 * This function returns 0 if it succeeds, or one of the following
5210
 * negative error codes:
5211
 *
5212
 * NGHTTP2_ERR_IGN_PAYLOAD
5213
 *   The reception of DATA frame is connection error; or should be
5214
 *   ignored.
5215
 * NGHTTP2_ERR_NOMEM
5216
 *   Out of memory.
5217
 */
5218
0
static int session_on_data_received_fail_fast(nghttp2_session *session) {
5219
0
  int rv;
5220
0
  nghttp2_stream *stream;
5221
0
  nghttp2_inbound_frame *iframe;
5222
0
  int32_t stream_id;
5223
0
  const char *failure_reason;
5224
0
  uint32_t error_code = NGHTTP2_PROTOCOL_ERROR;
5225
5226
0
  iframe = &session->iframe;
5227
0
  stream_id = iframe->frame.hd.stream_id;
5228
5229
0
  if (stream_id == 0) {
5230
    /* The spec says that if a DATA frame is received whose stream ID
5231
       is 0, the recipient MUST respond with a connection error of
5232
       type PROTOCOL_ERROR. */
5233
0
    failure_reason = "DATA: stream_id == 0";
5234
0
    goto fail;
5235
0
  }
5236
5237
0
  if (session_detect_idle_stream(session, stream_id)) {
5238
0
    failure_reason = "DATA: stream in idle";
5239
0
    error_code = NGHTTP2_PROTOCOL_ERROR;
5240
0
    goto fail;
5241
0
  }
5242
5243
0
  stream = nghttp2_session_get_stream(session, stream_id);
5244
0
  if (!stream) {
5245
0
    stream = nghttp2_session_get_stream_raw(session, stream_id);
5246
0
    if (stream && (stream->shut_flags & NGHTTP2_SHUT_RD)) {
5247
0
      failure_reason = "DATA: stream closed";
5248
0
      error_code = NGHTTP2_STREAM_CLOSED;
5249
0
      goto fail;
5250
0
    }
5251
5252
0
    return NGHTTP2_ERR_IGN_PAYLOAD;
5253
0
  }
5254
0
  if (stream->shut_flags & NGHTTP2_SHUT_RD) {
5255
0
    failure_reason = "DATA: stream in half-closed(remote)";
5256
0
    error_code = NGHTTP2_STREAM_CLOSED;
5257
0
    goto fail;
5258
0
  }
5259
5260
0
  if (nghttp2_session_is_my_stream_id(session, stream_id)) {
5261
0
    if (stream->state == NGHTTP2_STREAM_CLOSING) {
5262
0
      return NGHTTP2_ERR_IGN_PAYLOAD;
5263
0
    }
5264
0
    if (stream->state != NGHTTP2_STREAM_OPENED) {
5265
0
      failure_reason = "DATA: stream not opened";
5266
0
      goto fail;
5267
0
    }
5268
0
    return 0;
5269
0
  }
5270
0
  if (stream->state == NGHTTP2_STREAM_RESERVED) {
5271
0
    failure_reason = "DATA: stream in reserved";
5272
0
    goto fail;
5273
0
  }
5274
0
  if (stream->state == NGHTTP2_STREAM_CLOSING) {
5275
0
    return NGHTTP2_ERR_IGN_PAYLOAD;
5276
0
  }
5277
0
  return 0;
5278
0
fail:
5279
0
  rv = nghttp2_session_terminate_session_with_reason(session, error_code,
5280
0
                                                     failure_reason);
5281
0
  if (nghttp2_is_fatal(rv)) {
5282
0
    return rv;
5283
0
  }
5284
0
  return NGHTTP2_ERR_IGN_PAYLOAD;
5285
0
}
5286
5287
static size_t inbound_frame_payload_readlen(nghttp2_inbound_frame *iframe,
5288
                                            const uint8_t *in,
5289
0
                                            const uint8_t *last) {
5290
0
  return nghttp2_min_size((size_t)(last - in), iframe->payloadleft);
5291
0
}
5292
5293
/*
5294
 * Resets iframe->sbuf and advance its mark pointer by |left| bytes.
5295
 */
5296
0
static void inbound_frame_set_mark(nghttp2_inbound_frame *iframe, size_t left) {
5297
0
  nghttp2_buf_reset(&iframe->sbuf);
5298
0
  iframe->sbuf.mark += left;
5299
0
}
5300
5301
static size_t inbound_frame_buf_read(nghttp2_inbound_frame *iframe,
5302
0
                                     const uint8_t *in, const uint8_t *last) {
5303
0
  size_t readlen;
5304
5305
0
  readlen = nghttp2_min_size((size_t)(last - in),
5306
0
                             nghttp2_buf_mark_avail(&iframe->sbuf));
5307
5308
0
  iframe->sbuf.last = nghttp2_cpymem(iframe->sbuf.last, in, readlen);
5309
5310
0
  return readlen;
5311
0
}
5312
5313
/*
5314
 * Unpacks SETTINGS entry in iframe->sbuf.
5315
 */
5316
0
static void inbound_frame_set_settings_entry(nghttp2_inbound_frame *iframe) {
5317
0
  nghttp2_settings_entry iv;
5318
0
  nghttp2_settings_entry *min_header_table_size_entry;
5319
0
  size_t i;
5320
5321
0
  nghttp2_frame_unpack_settings_entry(&iv, iframe->sbuf.pos);
5322
5323
0
  switch (iv.settings_id) {
5324
0
  case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
5325
0
  case NGHTTP2_SETTINGS_ENABLE_PUSH:
5326
0
  case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
5327
0
  case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
5328
0
  case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
5329
0
  case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
5330
0
  case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
5331
0
  case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
5332
0
    break;
5333
0
  default:
5334
0
    DEBUGF("recv: unknown settings id=0x%02x\n", iv.settings_id);
5335
5336
0
    iframe->iv[iframe->niv++] = iv;
5337
5338
0
    return;
5339
0
  }
5340
5341
0
  for (i = 0; i < iframe->niv; ++i) {
5342
0
    if (iframe->iv[i].settings_id == iv.settings_id) {
5343
0
      iframe->iv[i] = iv;
5344
0
      break;
5345
0
    }
5346
0
  }
5347
5348
0
  if (i == iframe->niv) {
5349
0
    iframe->iv[iframe->niv++] = iv;
5350
0
  }
5351
5352
0
  if (iv.settings_id == NGHTTP2_SETTINGS_HEADER_TABLE_SIZE) {
5353
    /* Keep track of minimum value of SETTINGS_HEADER_TABLE_SIZE */
5354
0
    min_header_table_size_entry = &iframe->iv[iframe->max_niv - 1];
5355
5356
0
    if (iv.value < min_header_table_size_entry->value) {
5357
0
      min_header_table_size_entry->value = iv.value;
5358
0
    }
5359
0
  }
5360
0
}
5361
5362
/*
5363
 * Checks PADDED flags and set iframe->sbuf to read them accordingly.
5364
 * If padding is set, this function returns 1.  If no padding is set,
5365
 * this function returns 0.  On error, returns -1.
5366
 */
5367
static int inbound_frame_handle_pad(nghttp2_inbound_frame *iframe,
5368
0
                                    nghttp2_frame_hd *hd) {
5369
0
  if (hd->flags & NGHTTP2_FLAG_PADDED) {
5370
0
    if (hd->length < 1) {
5371
0
      return -1;
5372
0
    }
5373
0
    inbound_frame_set_mark(iframe, 1);
5374
0
    return 1;
5375
0
  }
5376
0
  DEBUGF("recv: no padding in payload\n");
5377
0
  return 0;
5378
0
}
5379
5380
/*
5381
 * Computes number of padding based on flags. This function returns
5382
 * the calculated length if it succeeds, or -1.
5383
 */
5384
0
static nghttp2_ssize inbound_frame_compute_pad(nghttp2_inbound_frame *iframe) {
5385
0
  size_t padlen;
5386
5387
  /* 1 for Pad Length field */
5388
0
  padlen = (size_t)(iframe->sbuf.pos[0] + 1);
5389
5390
0
  DEBUGF("recv: padlen=%zu\n", padlen);
5391
5392
  /* We cannot use iframe->frame.hd.length because of CONTINUATION */
5393
0
  if (padlen - 1 > iframe->payloadleft) {
5394
0
    return -1;
5395
0
  }
5396
5397
0
  iframe->padlen = padlen;
5398
5399
0
  return (nghttp2_ssize)padlen;
5400
0
}
5401
5402
/*
5403
 * This function returns the effective payload length in the data of
5404
 * length |readlen| when the remaining payload is |payloadleft|. The
5405
 * |payloadleft| does not include |readlen|. If padding was started
5406
 * strictly before this data chunk, this function returns -1.
5407
 */
5408
static nghttp2_ssize
5409
inbound_frame_effective_readlen(nghttp2_inbound_frame *iframe,
5410
0
                                size_t payloadleft, size_t readlen) {
5411
0
  size_t trail_padlen =
5412
0
    nghttp2_frame_trail_padlen(&iframe->frame, iframe->padlen);
5413
5414
0
  if (trail_padlen > payloadleft) {
5415
0
    size_t padlen;
5416
0
    padlen = trail_padlen - payloadleft;
5417
0
    if (readlen < padlen) {
5418
0
      return -1;
5419
0
    }
5420
0
    return (nghttp2_ssize)(readlen - padlen);
5421
0
  }
5422
0
  return (nghttp2_ssize)(readlen);
5423
0
}
5424
5425
static const uint8_t static_in[] = {0};
5426
5427
ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in,
5428
0
                                 size_t inlen) {
5429
0
  return (ssize_t)nghttp2_session_mem_recv2(session, in, inlen);
5430
0
}
5431
5432
static nghttp2_ssize session_mem_recv(nghttp2_session *session,
5433
0
                                      const uint8_t *in, size_t inlen) {
5434
0
  const uint8_t *first, *last;
5435
0
  nghttp2_inbound_frame *iframe = &session->iframe;
5436
0
  size_t readlen;
5437
0
  nghttp2_ssize padlen;
5438
0
  int rv;
5439
0
  int busy = 0;
5440
0
  nghttp2_frame_hd cont_hd;
5441
0
  nghttp2_stream *stream;
5442
0
  size_t pri_fieldlen;
5443
0
  nghttp2_mem *mem;
5444
5445
0
  if (in == NULL) {
5446
0
    assert(inlen == 0);
5447
0
    in = static_in;
5448
0
  }
5449
5450
0
  first = in;
5451
0
  last = in + inlen;
5452
5453
0
  DEBUGF("recv: connection recv_window_size=%d, local_window=%d\n",
5454
0
         session->recv_window_size, session->local_window_size);
5455
5456
0
  mem = &session->mem;
5457
5458
0
  if (!nghttp2_session_want_read(session)) {
5459
0
    return (nghttp2_ssize)inlen;
5460
0
  }
5461
5462
0
  for (;;) {
5463
0
    switch (iframe->state) {
5464
0
    case NGHTTP2_IB_READ_CLIENT_MAGIC:
5465
0
      readlen = nghttp2_min_size(inlen, iframe->payloadleft);
5466
5467
0
      if (memcmp(&NGHTTP2_CLIENT_MAGIC[NGHTTP2_CLIENT_MAGIC_LEN -
5468
0
                                       iframe->payloadleft],
5469
0
                 in, readlen) != 0) {
5470
0
        return NGHTTP2_ERR_BAD_CLIENT_MAGIC;
5471
0
      }
5472
5473
0
      iframe->payloadleft -= readlen;
5474
0
      in += readlen;
5475
5476
0
      if (iframe->payloadleft == 0) {
5477
0
        session_inbound_frame_reset(session);
5478
0
        iframe->state = NGHTTP2_IB_READ_FIRST_SETTINGS;
5479
0
      }
5480
5481
0
      break;
5482
0
    case NGHTTP2_IB_READ_FIRST_SETTINGS:
5483
0
      DEBUGF("recv: [IB_READ_FIRST_SETTINGS]\n");
5484
5485
0
      readlen = inbound_frame_buf_read(iframe, in, last);
5486
0
      in += readlen;
5487
5488
0
      if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
5489
0
        return (nghttp2_ssize)(in - first);
5490
0
      }
5491
5492
0
      if (iframe->sbuf.pos[3] != NGHTTP2_SETTINGS ||
5493
0
          (iframe->sbuf.pos[4] & NGHTTP2_FLAG_ACK)) {
5494
0
        rv = session_call_error_callback(
5495
0
          session, NGHTTP2_ERR_SETTINGS_EXPECTED,
5496
0
          "Remote peer returned unexpected data while we expected "
5497
0
          "SETTINGS frame.  Perhaps, peer does not support HTTP/2 "
5498
0
          "properly.");
5499
5500
0
        if (nghttp2_is_fatal(rv)) {
5501
0
          return rv;
5502
0
        }
5503
5504
0
        rv = nghttp2_session_terminate_session_with_reason(
5505
0
          session, NGHTTP2_PROTOCOL_ERROR, "SETTINGS expected");
5506
5507
0
        if (nghttp2_is_fatal(rv)) {
5508
0
          return rv;
5509
0
        }
5510
5511
0
        return (nghttp2_ssize)inlen;
5512
0
      }
5513
5514
0
      iframe->state = NGHTTP2_IB_READ_HEAD;
5515
5516
    /* Fall through */
5517
0
    case NGHTTP2_IB_READ_HEAD: {
5518
0
      int on_begin_frame_called = 0;
5519
5520
0
      DEBUGF("recv: [IB_READ_HEAD]\n");
5521
5522
0
      readlen = inbound_frame_buf_read(iframe, in, last);
5523
0
      in += readlen;
5524
5525
0
      if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
5526
0
        return (nghttp2_ssize)(in - first);
5527
0
      }
5528
5529
0
      nghttp2_frame_unpack_frame_hd(&iframe->frame.hd, iframe->sbuf.pos);
5530
0
      iframe->payloadleft = iframe->frame.hd.length;
5531
5532
0
      DEBUGF("recv: payloadlen=%zu, type=%u, flags=0x%02x, stream_id=%d\n",
5533
0
             iframe->frame.hd.length, iframe->frame.hd.type,
5534
0
             iframe->frame.hd.flags, iframe->frame.hd.stream_id);
5535
5536
0
      if (iframe->frame.hd.length > session->local_settings.max_frame_size) {
5537
0
        DEBUGF("recv: length is too large %zu > %u\n", iframe->frame.hd.length,
5538
0
               session->local_settings.max_frame_size);
5539
5540
0
        rv = nghttp2_session_terminate_session_with_reason(
5541
0
          session, NGHTTP2_FRAME_SIZE_ERROR, "too large frame size");
5542
5543
0
        if (nghttp2_is_fatal(rv)) {
5544
0
          return rv;
5545
0
        }
5546
5547
0
        return (nghttp2_ssize)inlen;
5548
0
      }
5549
5550
0
      switch (iframe->frame.hd.type) {
5551
0
      case NGHTTP2_DATA: {
5552
0
        DEBUGF("recv: DATA\n");
5553
5554
0
        iframe->frame.hd.flags &=
5555
0
          (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PADDED);
5556
        /* Check stream is open. If it is not open or closing,
5557
           ignore payload. */
5558
0
        busy = 1;
5559
5560
0
        rv = session_on_data_received_fail_fast(session);
5561
0
        if (nghttp2_is_fatal(rv)) {
5562
0
          return rv;
5563
0
        }
5564
5565
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5566
0
          return (nghttp2_ssize)inlen;
5567
0
        }
5568
0
        if (rv == NGHTTP2_ERR_IGN_PAYLOAD) {
5569
0
          DEBUGF("recv: DATA not allowed stream_id=%d\n",
5570
0
                 iframe->frame.hd.stream_id);
5571
5572
0
          iframe->state = NGHTTP2_IB_IGN_DATA;
5573
0
          break;
5574
0
        }
5575
5576
0
        rv = inbound_frame_handle_pad(iframe, &iframe->frame.hd);
5577
0
        if (rv < 0) {
5578
0
          rv = nghttp2_session_terminate_session_with_reason(
5579
0
            session, NGHTTP2_PROTOCOL_ERROR,
5580
0
            "DATA: insufficient padding space");
5581
5582
0
          if (nghttp2_is_fatal(rv)) {
5583
0
            return rv;
5584
0
          }
5585
0
          return (nghttp2_ssize)inlen;
5586
0
        }
5587
5588
0
        if (rv == 1) {
5589
0
          iframe->state = NGHTTP2_IB_READ_PAD_DATA;
5590
0
          break;
5591
0
        }
5592
5593
        /* Empty DATA frame without END_STREAM flag set is
5594
           suspicious. */
5595
0
        if (iframe->payloadleft == 0 &&
5596
0
            (iframe->frame.hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
5597
0
          rv = session_update_glitch_ratelim(session);
5598
0
          if (rv != 0) {
5599
0
            return rv;
5600
0
          }
5601
5602
0
          if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5603
0
            return (nghttp2_ssize)inlen;
5604
0
          }
5605
0
        }
5606
5607
0
        iframe->state = NGHTTP2_IB_READ_DATA;
5608
0
        break;
5609
0
      }
5610
0
      case NGHTTP2_HEADERS:
5611
5612
0
        DEBUGF("recv: HEADERS\n");
5613
5614
0
        iframe->frame.hd.flags &=
5615
0
          (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS |
5616
0
           NGHTTP2_FLAG_PADDED | NGHTTP2_FLAG_PRIORITY);
5617
5618
0
        rv = inbound_frame_handle_pad(iframe, &iframe->frame.hd);
5619
0
        if (rv < 0) {
5620
0
          rv = nghttp2_session_terminate_session_with_reason(
5621
0
            session, NGHTTP2_PROTOCOL_ERROR,
5622
0
            "HEADERS: insufficient padding space");
5623
0
          if (nghttp2_is_fatal(rv)) {
5624
0
            return rv;
5625
0
          }
5626
0
          return (nghttp2_ssize)inlen;
5627
0
        }
5628
5629
0
        if (rv == 1) {
5630
0
          iframe->state = NGHTTP2_IB_READ_NBYTE;
5631
0
          break;
5632
0
        }
5633
5634
0
        pri_fieldlen = nghttp2_frame_priority_len(iframe->frame.hd.flags);
5635
5636
0
        if (pri_fieldlen > 0) {
5637
0
          if (iframe->payloadleft < pri_fieldlen) {
5638
0
            busy = 1;
5639
0
            iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
5640
0
            break;
5641
0
          }
5642
5643
0
          iframe->state = NGHTTP2_IB_READ_NBYTE;
5644
5645
0
          inbound_frame_set_mark(iframe, pri_fieldlen);
5646
5647
0
          break;
5648
0
        }
5649
5650
        /* Call on_begin_frame_callback here because
5651
           session_process_headers_frame() may call
5652
           on_begin_headers_callback */
5653
0
        rv = session_call_on_begin_frame(session, &iframe->frame.hd);
5654
5655
0
        if (nghttp2_is_fatal(rv)) {
5656
0
          return rv;
5657
0
        }
5658
5659
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5660
0
          return (nghttp2_ssize)inlen;
5661
0
        }
5662
5663
0
        on_begin_frame_called = 1;
5664
5665
0
        rv = session_process_headers_frame(session);
5666
0
        if (nghttp2_is_fatal(rv)) {
5667
0
          return rv;
5668
0
        }
5669
5670
0
        busy = 1;
5671
5672
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5673
0
          return (nghttp2_ssize)inlen;
5674
0
        }
5675
5676
0
        if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
5677
0
          rv = session_handle_invalid_stream2(
5678
0
            session, iframe->frame.hd.stream_id, NULL, NGHTTP2_ERR_INTERNAL);
5679
0
          if (nghttp2_is_fatal(rv)) {
5680
0
            return rv;
5681
0
          }
5682
0
          iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
5683
0
          break;
5684
0
        }
5685
5686
0
        if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) {
5687
0
          rv = session_update_glitch_ratelim(session);
5688
0
          if (rv != 0) {
5689
0
            return rv;
5690
0
          }
5691
5692
0
          if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5693
0
            return (nghttp2_ssize)inlen;
5694
0
          }
5695
5696
0
          iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
5697
0
          break;
5698
0
        }
5699
5700
0
        iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
5701
5702
0
        break;
5703
0
      case NGHTTP2_PRIORITY:
5704
0
        DEBUGF("recv: PRIORITY\n");
5705
5706
0
        iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
5707
5708
0
        if (iframe->payloadleft != NGHTTP2_PRIORITY_SPECLEN) {
5709
0
          busy = 1;
5710
5711
0
          iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
5712
5713
0
          break;
5714
0
        }
5715
5716
        /* This is deprecated RFC 7540 priorities mechanism which is
5717
           very unpopular.  We do not expect it is received so
5718
           frequently. */
5719
0
        rv = session_update_glitch_ratelim(session);
5720
0
        if (rv != 0) {
5721
0
          return rv;
5722
0
        }
5723
5724
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5725
0
          return (nghttp2_ssize)inlen;
5726
0
        }
5727
5728
0
        iframe->state = NGHTTP2_IB_READ_NBYTE;
5729
5730
0
        inbound_frame_set_mark(iframe, NGHTTP2_PRIORITY_SPECLEN);
5731
5732
0
        break;
5733
0
      case NGHTTP2_RST_STREAM:
5734
0
      case NGHTTP2_WINDOW_UPDATE:
5735
#ifdef DEBUGBUILD
5736
        switch (iframe->frame.hd.type) {
5737
        case NGHTTP2_RST_STREAM:
5738
          DEBUGF("recv: RST_STREAM\n");
5739
          break;
5740
        case NGHTTP2_WINDOW_UPDATE:
5741
          DEBUGF("recv: WINDOW_UPDATE\n");
5742
          break;
5743
        }
5744
#endif /* defined(DEBUGBUILD) */
5745
5746
0
        iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
5747
5748
0
        if (iframe->payloadleft != 4) {
5749
0
          busy = 1;
5750
0
          iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
5751
0
          break;
5752
0
        }
5753
5754
0
        iframe->state = NGHTTP2_IB_READ_NBYTE;
5755
5756
0
        inbound_frame_set_mark(iframe, 4);
5757
5758
0
        break;
5759
0
      case NGHTTP2_SETTINGS:
5760
0
        DEBUGF("recv: SETTINGS\n");
5761
5762
0
        iframe->frame.hd.flags &= NGHTTP2_FLAG_ACK;
5763
5764
0
        if ((iframe->frame.hd.length % NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) ||
5765
0
            ((iframe->frame.hd.flags & NGHTTP2_FLAG_ACK) &&
5766
0
             iframe->payloadleft > 0)) {
5767
0
          busy = 1;
5768
0
          iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
5769
0
          break;
5770
0
        }
5771
5772
        /* Check the settings flood counter early to be safe */
5773
0
        if (session->obq_flood_counter_ >= session->max_outbound_ack &&
5774
0
            !(iframe->frame.hd.flags & NGHTTP2_FLAG_ACK)) {
5775
0
          return NGHTTP2_ERR_FLOODED;
5776
0
        }
5777
5778
0
        iframe->state = NGHTTP2_IB_READ_SETTINGS;
5779
5780
0
        if (iframe->payloadleft) {
5781
0
          nghttp2_settings_entry *min_header_table_size_entry;
5782
5783
          /* We allocate iv with additional one entry, to store the
5784
             minimum header table size. */
5785
0
          iframe->max_niv =
5786
0
            iframe->frame.hd.length / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH + 1;
5787
5788
0
          if (iframe->max_niv - 1 > session->max_settings) {
5789
0
            rv = nghttp2_session_terminate_session_with_reason(
5790
0
              session, NGHTTP2_ENHANCE_YOUR_CALM,
5791
0
              "SETTINGS: too many setting entries");
5792
0
            if (nghttp2_is_fatal(rv)) {
5793
0
              return rv;
5794
0
            }
5795
0
            return (nghttp2_ssize)inlen;
5796
0
          }
5797
5798
0
          iframe->iv = nghttp2_mem_malloc(mem, sizeof(nghttp2_settings_entry) *
5799
0
                                                 iframe->max_niv);
5800
5801
0
          if (!iframe->iv) {
5802
0
            return NGHTTP2_ERR_NOMEM;
5803
0
          }
5804
5805
0
          min_header_table_size_entry = &iframe->iv[iframe->max_niv - 1];
5806
0
          min_header_table_size_entry->settings_id =
5807
0
            NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
5808
0
          min_header_table_size_entry->value = UINT32_MAX;
5809
5810
0
          inbound_frame_set_mark(iframe, NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH);
5811
0
          break;
5812
0
        }
5813
5814
0
        busy = 1;
5815
5816
0
        inbound_frame_set_mark(iframe, 0);
5817
5818
0
        break;
5819
0
      case NGHTTP2_PUSH_PROMISE:
5820
0
        DEBUGF("recv: PUSH_PROMISE\n");
5821
5822
0
        iframe->frame.hd.flags &=
5823
0
          (NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_PADDED);
5824
5825
0
        rv = inbound_frame_handle_pad(iframe, &iframe->frame.hd);
5826
0
        if (rv < 0) {
5827
0
          rv = nghttp2_session_terminate_session_with_reason(
5828
0
            session, NGHTTP2_PROTOCOL_ERROR,
5829
0
            "PUSH_PROMISE: insufficient padding space");
5830
0
          if (nghttp2_is_fatal(rv)) {
5831
0
            return rv;
5832
0
          }
5833
0
          return (nghttp2_ssize)inlen;
5834
0
        }
5835
5836
0
        if (rv == 1) {
5837
0
          iframe->state = NGHTTP2_IB_READ_NBYTE;
5838
0
          break;
5839
0
        }
5840
5841
0
        if (iframe->payloadleft < 4) {
5842
0
          busy = 1;
5843
0
          iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
5844
0
          break;
5845
0
        }
5846
5847
0
        iframe->state = NGHTTP2_IB_READ_NBYTE;
5848
5849
0
        inbound_frame_set_mark(iframe, 4);
5850
5851
0
        break;
5852
0
      case NGHTTP2_PING:
5853
0
        DEBUGF("recv: PING\n");
5854
5855
0
        iframe->frame.hd.flags &= NGHTTP2_FLAG_ACK;
5856
5857
0
        if (iframe->payloadleft != 8) {
5858
0
          busy = 1;
5859
0
          iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
5860
0
          break;
5861
0
        }
5862
5863
0
        iframe->state = NGHTTP2_IB_READ_NBYTE;
5864
0
        inbound_frame_set_mark(iframe, 8);
5865
5866
0
        break;
5867
0
      case NGHTTP2_GOAWAY:
5868
0
        DEBUGF("recv: GOAWAY\n");
5869
5870
0
        iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
5871
5872
0
        if (iframe->payloadleft < 8) {
5873
0
          busy = 1;
5874
0
          iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
5875
0
          break;
5876
0
        }
5877
5878
0
        iframe->state = NGHTTP2_IB_READ_NBYTE;
5879
0
        inbound_frame_set_mark(iframe, 8);
5880
5881
0
        break;
5882
0
      case NGHTTP2_CONTINUATION:
5883
0
        DEBUGF("recv: unexpected CONTINUATION\n");
5884
5885
        /* Receiving CONTINUATION in this state are subject to
5886
           connection error of type PROTOCOL_ERROR */
5887
0
        rv = nghttp2_session_terminate_session_with_reason(
5888
0
          session, NGHTTP2_PROTOCOL_ERROR, "CONTINUATION: unexpected");
5889
0
        if (nghttp2_is_fatal(rv)) {
5890
0
          return rv;
5891
0
        }
5892
5893
0
        return (nghttp2_ssize)inlen;
5894
0
      default:
5895
0
        DEBUGF("recv: extension frame\n");
5896
5897
0
        if (check_ext_type_set(session->user_recv_ext_types,
5898
0
                               iframe->frame.hd.type)) {
5899
0
          if (!session->callbacks.unpack_extension_callback) {
5900
            /* Receiving too frequent unknown frames is suspicious. */
5901
0
            rv = session_update_glitch_ratelim(session);
5902
0
            if (rv != 0) {
5903
0
              return rv;
5904
0
            }
5905
5906
0
            if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5907
0
              return (nghttp2_ssize)inlen;
5908
0
            }
5909
5910
            /* Silently ignore unknown frame type. */
5911
0
            busy = 1;
5912
5913
0
            iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
5914
5915
0
            break;
5916
0
          }
5917
5918
0
          busy = 1;
5919
5920
0
          iframe->state = NGHTTP2_IB_READ_EXTENSION_PAYLOAD;
5921
5922
0
          break;
5923
0
        } else {
5924
0
          switch (iframe->frame.hd.type) {
5925
0
          case NGHTTP2_ALTSVC:
5926
0
            if ((session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ALTSVC) ==
5927
0
                0) {
5928
              /* Receiving too frequent unknown frames is suspicious. */
5929
0
              rv = session_update_glitch_ratelim(session);
5930
0
              if (rv != 0) {
5931
0
                return rv;
5932
0
              }
5933
5934
0
              if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5935
0
                return (nghttp2_ssize)inlen;
5936
0
              }
5937
5938
0
              busy = 1;
5939
0
              iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
5940
0
              break;
5941
0
            }
5942
5943
0
            DEBUGF("recv: ALTSVC\n");
5944
5945
0
            iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
5946
0
            iframe->frame.ext.payload = &iframe->ext_frame_payload.altsvc;
5947
5948
0
            if (session->server) {
5949
              /* Receiving too frequent ALTSVC from client is
5950
                 suspicious. */
5951
0
              rv = session_update_glitch_ratelim(session);
5952
0
              if (rv != 0) {
5953
0
                return rv;
5954
0
              }
5955
5956
0
              if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5957
0
                return (nghttp2_ssize)inlen;
5958
0
              }
5959
5960
0
              busy = 1;
5961
0
              iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
5962
0
              break;
5963
0
            }
5964
5965
0
            if (iframe->payloadleft < 2) {
5966
0
              busy = 1;
5967
0
              iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
5968
0
              break;
5969
0
            }
5970
5971
0
            busy = 1;
5972
5973
0
            iframe->state = NGHTTP2_IB_READ_NBYTE;
5974
0
            inbound_frame_set_mark(iframe, 2);
5975
5976
0
            break;
5977
0
          case NGHTTP2_ORIGIN:
5978
0
            if (!(session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ORIGIN)) {
5979
              /* Receiving too frequent unknown frames is suspicious. */
5980
0
              rv = session_update_glitch_ratelim(session);
5981
0
              if (rv != 0) {
5982
0
                return rv;
5983
0
              }
5984
5985
0
              if (iframe->state == NGHTTP2_IB_IGN_ALL) {
5986
0
                return (nghttp2_ssize)inlen;
5987
0
              }
5988
5989
0
              busy = 1;
5990
0
              iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
5991
0
              break;
5992
0
            }
5993
5994
0
            DEBUGF("recv: ORIGIN\n");
5995
5996
0
            iframe->frame.ext.payload = &iframe->ext_frame_payload.origin;
5997
5998
0
            if (session->server || iframe->frame.hd.stream_id ||
5999
0
                (iframe->frame.hd.flags & 0xF0)) {
6000
              /* Receiving too frequent invalid frames is
6001
                 suspicious. */
6002
0
              rv = session_update_glitch_ratelim(session);
6003
0
              if (rv != 0) {
6004
0
                return rv;
6005
0
              }
6006
6007
0
              if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6008
0
                return (nghttp2_ssize)inlen;
6009
0
              }
6010
6011
0
              busy = 1;
6012
0
              iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
6013
0
              break;
6014
0
            }
6015
6016
0
            iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
6017
6018
0
            if (iframe->payloadleft) {
6019
0
              iframe->raw_lbuf = nghttp2_mem_malloc(mem, iframe->payloadleft);
6020
6021
0
              if (iframe->raw_lbuf == NULL) {
6022
0
                return NGHTTP2_ERR_NOMEM;
6023
0
              }
6024
6025
0
              nghttp2_buf_wrap_init(&iframe->lbuf, iframe->raw_lbuf,
6026
0
                                    iframe->payloadleft);
6027
0
            } else {
6028
0
              busy = 1;
6029
0
            }
6030
6031
0
            iframe->state = NGHTTP2_IB_READ_ORIGIN_PAYLOAD;
6032
6033
0
            break;
6034
0
          case NGHTTP2_PRIORITY_UPDATE:
6035
0
            if ((session->builtin_recv_ext_types &
6036
0
                 NGHTTP2_TYPEMASK_PRIORITY_UPDATE) == 0) {
6037
              /* Receiving too frequent unknown frames is suspicious. */
6038
0
              rv = session_update_glitch_ratelim(session);
6039
0
              if (rv != 0) {
6040
0
                return rv;
6041
0
              }
6042
6043
0
              if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6044
0
                return (nghttp2_ssize)inlen;
6045
0
              }
6046
6047
0
              busy = 1;
6048
0
              iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
6049
0
              break;
6050
0
            }
6051
6052
0
            DEBUGF("recv: PRIORITY_UPDATE\n");
6053
6054
0
            iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
6055
0
            iframe->frame.ext.payload =
6056
0
              &iframe->ext_frame_payload.priority_update;
6057
6058
0
            if (!session->server) {
6059
0
              rv = nghttp2_session_terminate_session_with_reason(
6060
0
                session, NGHTTP2_PROTOCOL_ERROR,
6061
0
                "PRIORITY_UPDATE is received from server");
6062
0
              if (nghttp2_is_fatal(rv)) {
6063
0
                return rv;
6064
0
              }
6065
0
              return (nghttp2_ssize)inlen;
6066
0
            }
6067
6068
0
            if (iframe->payloadleft < 4) {
6069
0
              busy = 1;
6070
0
              iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
6071
0
              break;
6072
0
            }
6073
6074
            /* Receiving too frequent PRIORITY_UPDATE is
6075
               suspicious. */
6076
0
            rv = session_update_glitch_ratelim(session);
6077
0
            if (rv != 0) {
6078
0
              return rv;
6079
0
            }
6080
6081
0
            if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6082
0
              return (nghttp2_ssize)inlen;
6083
0
            }
6084
6085
0
            if (iframe->payloadleft > sizeof(iframe->raw_sbuf)) {
6086
0
              busy = 1;
6087
0
              iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
6088
0
              break;
6089
0
            }
6090
6091
0
            busy = 1;
6092
6093
0
            iframe->state = NGHTTP2_IB_READ_NBYTE;
6094
0
            inbound_frame_set_mark(iframe, iframe->payloadleft);
6095
6096
0
            break;
6097
0
          default:
6098
            /* Receiving too frequent unknown frames is suspicious. */
6099
0
            rv = session_update_glitch_ratelim(session);
6100
0
            if (rv != 0) {
6101
0
              return rv;
6102
0
            }
6103
6104
0
            if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6105
0
              return (nghttp2_ssize)inlen;
6106
0
            }
6107
6108
0
            busy = 1;
6109
6110
0
            iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
6111
6112
0
            break;
6113
0
          }
6114
0
        }
6115
0
      }
6116
6117
0
      if (!on_begin_frame_called) {
6118
0
        switch (iframe->state) {
6119
0
        case NGHTTP2_IB_IGN_HEADER_BLOCK:
6120
0
        case NGHTTP2_IB_IGN_PAYLOAD:
6121
0
        case NGHTTP2_IB_FRAME_SIZE_ERROR:
6122
0
        case NGHTTP2_IB_IGN_DATA:
6123
0
        case NGHTTP2_IB_IGN_ALL:
6124
0
          break;
6125
0
        default:
6126
0
          rv = session_call_on_begin_frame(session, &iframe->frame.hd);
6127
6128
0
          if (nghttp2_is_fatal(rv)) {
6129
0
            return rv;
6130
0
          }
6131
6132
0
          if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6133
0
            return (nghttp2_ssize)inlen;
6134
0
          }
6135
0
        }
6136
0
      }
6137
6138
0
      break;
6139
0
    }
6140
0
    case NGHTTP2_IB_READ_NBYTE:
6141
0
      DEBUGF("recv: [IB_READ_NBYTE]\n");
6142
6143
0
      readlen = inbound_frame_buf_read(iframe, in, last);
6144
0
      in += readlen;
6145
0
      iframe->payloadleft -= readlen;
6146
6147
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu, left=%zu\n", readlen,
6148
0
             iframe->payloadleft, nghttp2_buf_mark_avail(&iframe->sbuf));
6149
6150
0
      if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
6151
0
        return (nghttp2_ssize)(in - first);
6152
0
      }
6153
6154
0
      switch (iframe->frame.hd.type) {
6155
0
      case NGHTTP2_HEADERS:
6156
0
        if (iframe->padlen == 0 &&
6157
0
            (iframe->frame.hd.flags & NGHTTP2_FLAG_PADDED)) {
6158
0
          pri_fieldlen = nghttp2_frame_priority_len(iframe->frame.hd.flags);
6159
0
          padlen = inbound_frame_compute_pad(iframe);
6160
0
          if (padlen < 0 ||
6161
0
              (size_t)padlen + pri_fieldlen > 1 + iframe->payloadleft) {
6162
0
            rv = nghttp2_session_terminate_session_with_reason(
6163
0
              session, NGHTTP2_PROTOCOL_ERROR, "HEADERS: invalid padding");
6164
0
            if (nghttp2_is_fatal(rv)) {
6165
0
              return rv;
6166
0
            }
6167
0
            return (nghttp2_ssize)inlen;
6168
0
          }
6169
0
          iframe->frame.headers.padlen = (size_t)padlen;
6170
6171
0
          if (pri_fieldlen > 0) {
6172
0
            if (iframe->payloadleft < pri_fieldlen) {
6173
0
              busy = 1;
6174
0
              iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
6175
0
              break;
6176
0
            }
6177
0
            iframe->state = NGHTTP2_IB_READ_NBYTE;
6178
0
            inbound_frame_set_mark(iframe, pri_fieldlen);
6179
0
            break;
6180
0
          } else {
6181
            /* Truncate buffers used for padding spec */
6182
0
            inbound_frame_set_mark(iframe, 0);
6183
0
          }
6184
0
        }
6185
6186
0
        rv = session_process_headers_frame(session);
6187
0
        if (nghttp2_is_fatal(rv)) {
6188
0
          return rv;
6189
0
        }
6190
6191
0
        busy = 1;
6192
6193
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6194
0
          return (nghttp2_ssize)inlen;
6195
0
        }
6196
6197
0
        if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
6198
0
          rv = session_handle_invalid_stream2(
6199
0
            session, iframe->frame.hd.stream_id, NULL, NGHTTP2_ERR_INTERNAL);
6200
0
          if (nghttp2_is_fatal(rv)) {
6201
0
            return rv;
6202
0
          }
6203
0
          iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
6204
0
          break;
6205
0
        }
6206
6207
0
        if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) {
6208
0
          rv = session_update_glitch_ratelim(session);
6209
0
          if (rv != 0) {
6210
0
            return rv;
6211
0
          }
6212
6213
0
          if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6214
0
            return (nghttp2_ssize)inlen;
6215
0
          }
6216
6217
0
          iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
6218
0
          break;
6219
0
        }
6220
6221
0
        iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
6222
6223
0
        break;
6224
0
      case NGHTTP2_PRIORITY:
6225
0
        session_inbound_frame_reset(session);
6226
6227
0
        break;
6228
0
      case NGHTTP2_RST_STREAM:
6229
0
        rv = session_process_rst_stream_frame(session);
6230
0
        if (nghttp2_is_fatal(rv)) {
6231
0
          return rv;
6232
0
        }
6233
6234
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6235
0
          return (nghttp2_ssize)inlen;
6236
0
        }
6237
6238
0
        session_inbound_frame_reset(session);
6239
6240
0
        break;
6241
0
      case NGHTTP2_PUSH_PROMISE:
6242
0
        if (iframe->padlen == 0 &&
6243
0
            (iframe->frame.hd.flags & NGHTTP2_FLAG_PADDED)) {
6244
0
          padlen = inbound_frame_compute_pad(iframe);
6245
0
          if (padlen < 0 || (size_t)padlen + 4 /* promised stream id */
6246
0
                              > 1 + iframe->payloadleft) {
6247
0
            rv = nghttp2_session_terminate_session_with_reason(
6248
0
              session, NGHTTP2_PROTOCOL_ERROR, "PUSH_PROMISE: invalid padding");
6249
0
            if (nghttp2_is_fatal(rv)) {
6250
0
              return rv;
6251
0
            }
6252
0
            return (nghttp2_ssize)inlen;
6253
0
          }
6254
6255
0
          iframe->frame.push_promise.padlen = (size_t)padlen;
6256
6257
0
          if (iframe->payloadleft < 4) {
6258
0
            busy = 1;
6259
0
            iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
6260
0
            break;
6261
0
          }
6262
6263
0
          iframe->state = NGHTTP2_IB_READ_NBYTE;
6264
6265
0
          inbound_frame_set_mark(iframe, 4);
6266
6267
0
          break;
6268
0
        }
6269
6270
0
        rv = session_process_push_promise_frame(session);
6271
0
        if (nghttp2_is_fatal(rv)) {
6272
0
          return rv;
6273
0
        }
6274
6275
0
        busy = 1;
6276
6277
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6278
0
          return (nghttp2_ssize)inlen;
6279
0
        }
6280
6281
0
        if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
6282
0
          rv = session_handle_invalid_stream2(
6283
0
            session, iframe->frame.push_promise.promised_stream_id, NULL,
6284
0
            NGHTTP2_ERR_INTERNAL);
6285
0
          if (nghttp2_is_fatal(rv)) {
6286
0
            return rv;
6287
0
          }
6288
0
          iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
6289
0
          break;
6290
0
        }
6291
6292
0
        if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) {
6293
0
          iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
6294
0
          break;
6295
0
        }
6296
6297
0
        iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
6298
6299
0
        break;
6300
0
      case NGHTTP2_PING:
6301
0
        rv = session_process_ping_frame(session);
6302
0
        if (nghttp2_is_fatal(rv)) {
6303
0
          return rv;
6304
0
        }
6305
6306
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6307
0
          return (nghttp2_ssize)inlen;
6308
0
        }
6309
6310
0
        session_inbound_frame_reset(session);
6311
6312
0
        break;
6313
0
      case NGHTTP2_GOAWAY: {
6314
0
        size_t debuglen;
6315
6316
        /* 8 is Last-stream-ID + Error Code */
6317
0
        debuglen = iframe->frame.hd.length - 8;
6318
6319
0
        if (debuglen > 0) {
6320
0
          iframe->raw_lbuf = nghttp2_mem_malloc(mem, debuglen);
6321
6322
0
          if (iframe->raw_lbuf == NULL) {
6323
0
            return NGHTTP2_ERR_NOMEM;
6324
0
          }
6325
6326
0
          nghttp2_buf_wrap_init(&iframe->lbuf, iframe->raw_lbuf, debuglen);
6327
0
        }
6328
6329
0
        busy = 1;
6330
6331
0
        iframe->state = NGHTTP2_IB_READ_GOAWAY_DEBUG;
6332
6333
0
        break;
6334
0
      }
6335
0
      case NGHTTP2_WINDOW_UPDATE:
6336
0
        rv = session_process_window_update_frame(session);
6337
0
        if (nghttp2_is_fatal(rv)) {
6338
0
          return rv;
6339
0
        }
6340
6341
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6342
0
          return (nghttp2_ssize)inlen;
6343
0
        }
6344
6345
0
        session_inbound_frame_reset(session);
6346
6347
0
        break;
6348
0
      case NGHTTP2_ALTSVC: {
6349
0
        size_t origin_len;
6350
6351
0
        origin_len = nghttp2_get_uint16(iframe->sbuf.pos);
6352
6353
0
        DEBUGF("recv: origin_len=%zu\n", origin_len);
6354
6355
0
        if (origin_len > iframe->payloadleft) {
6356
0
          busy = 1;
6357
0
          iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
6358
0
          break;
6359
0
        }
6360
6361
0
        if (iframe->frame.hd.length > 2) {
6362
0
          iframe->raw_lbuf =
6363
0
            nghttp2_mem_malloc(mem, iframe->frame.hd.length - 2);
6364
6365
0
          if (iframe->raw_lbuf == NULL) {
6366
0
            return NGHTTP2_ERR_NOMEM;
6367
0
          }
6368
6369
0
          nghttp2_buf_wrap_init(&iframe->lbuf, iframe->raw_lbuf,
6370
0
                                iframe->frame.hd.length);
6371
0
        }
6372
6373
0
        busy = 1;
6374
6375
0
        iframe->state = NGHTTP2_IB_READ_ALTSVC_PAYLOAD;
6376
6377
0
        break;
6378
0
      case NGHTTP2_PRIORITY_UPDATE:
6379
0
        DEBUGF("recv: prioritized_stream_id=%d\n",
6380
0
               nghttp2_get_uint32(iframe->sbuf.pos) & NGHTTP2_STREAM_ID_MASK);
6381
6382
0
        rv = session_process_priority_update_frame(session);
6383
0
        if (nghttp2_is_fatal(rv)) {
6384
0
          return rv;
6385
0
        }
6386
6387
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6388
0
          return (nghttp2_ssize)inlen;
6389
0
        }
6390
6391
0
        session_inbound_frame_reset(session);
6392
6393
0
        break;
6394
0
      }
6395
0
      default:
6396
        /* This is unknown frame */
6397
0
        session_inbound_frame_reset(session);
6398
6399
0
        break;
6400
0
      }
6401
0
      break;
6402
0
    case NGHTTP2_IB_READ_HEADER_BLOCK:
6403
0
    case NGHTTP2_IB_IGN_HEADER_BLOCK: {
6404
0
      nghttp2_ssize data_readlen;
6405
0
      size_t trail_padlen;
6406
0
      int final;
6407
#ifdef DEBUGBUILD
6408
      if (iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK) {
6409
        DEBUGF("recv: [IB_READ_HEADER_BLOCK]\n");
6410
      } else {
6411
        DEBUGF("recv: [IB_IGN_HEADER_BLOCK]\n");
6412
      }
6413
#endif /* defined(DEBUGBUILD) */
6414
6415
0
      readlen = inbound_frame_payload_readlen(iframe, in, last);
6416
6417
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
6418
0
             iframe->payloadleft - readlen);
6419
6420
0
      data_readlen = inbound_frame_effective_readlen(
6421
0
        iframe, iframe->payloadleft - readlen, readlen);
6422
6423
0
      if (data_readlen == -1) {
6424
        /* everything is padding */
6425
0
        data_readlen = 0;
6426
0
      }
6427
6428
0
      trail_padlen = nghttp2_frame_trail_padlen(&iframe->frame, iframe->padlen);
6429
6430
0
      final = (iframe->frame.hd.flags & NGHTTP2_FLAG_END_HEADERS) &&
6431
0
              iframe->payloadleft - (size_t)data_readlen == trail_padlen;
6432
6433
0
      if (data_readlen > 0 || (data_readlen == 0 && final)) {
6434
0
        size_t hd_proclen = 0;
6435
6436
0
        DEBUGF("recv: block final=%d\n", final);
6437
6438
0
        rv =
6439
0
          inflate_header_block(session, &iframe->frame, &hd_proclen,
6440
0
                               (uint8_t *)in, (size_t)data_readlen, final,
6441
0
                               iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK);
6442
6443
0
        if (nghttp2_is_fatal(rv)) {
6444
0
          return rv;
6445
0
        }
6446
6447
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6448
0
          return (nghttp2_ssize)inlen;
6449
0
        }
6450
6451
0
        if (rv == NGHTTP2_ERR_PAUSE) {
6452
0
          in += hd_proclen;
6453
0
          iframe->payloadleft -= hd_proclen;
6454
6455
0
          return (nghttp2_ssize)(in - first);
6456
0
        }
6457
6458
0
        if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
6459
          /* The application says no more headers. We decompress the
6460
             rest of the header block but not invoke on_header_callback
6461
             and on_frame_recv_callback. */
6462
0
          in += hd_proclen;
6463
0
          iframe->payloadleft -= hd_proclen;
6464
6465
          /* Use promised stream ID for PUSH_PROMISE */
6466
0
          rv = session_handle_invalid_stream2(
6467
0
            session,
6468
0
            iframe->frame.hd.type == NGHTTP2_PUSH_PROMISE
6469
0
              ? iframe->frame.push_promise.promised_stream_id
6470
0
              : iframe->frame.hd.stream_id,
6471
0
            NULL, NGHTTP2_ERR_INTERNAL);
6472
0
          if (nghttp2_is_fatal(rv)) {
6473
0
            return rv;
6474
0
          }
6475
0
          busy = 1;
6476
0
          iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
6477
0
          break;
6478
0
        }
6479
6480
0
        in += readlen;
6481
0
        iframe->payloadleft -= readlen;
6482
6483
0
        if (rv == NGHTTP2_ERR_HEADER_COMP) {
6484
          /* GOAWAY is already issued */
6485
0
          if (iframe->payloadleft == 0) {
6486
0
            session_inbound_frame_reset(session);
6487
0
          } else {
6488
0
            busy = 1;
6489
0
            iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
6490
0
          }
6491
0
          break;
6492
0
        }
6493
0
      } else {
6494
0
        in += readlen;
6495
0
        iframe->payloadleft -= readlen;
6496
0
      }
6497
6498
0
      if (iframe->payloadleft) {
6499
0
        break;
6500
0
      }
6501
6502
0
      if ((iframe->frame.hd.flags & NGHTTP2_FLAG_END_HEADERS) == 0) {
6503
0
        inbound_frame_set_mark(iframe, NGHTTP2_FRAME_HDLEN);
6504
6505
0
        iframe->padlen = 0;
6506
6507
0
        if (iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK) {
6508
0
          iframe->state = NGHTTP2_IB_EXPECT_CONTINUATION;
6509
0
        } else {
6510
0
          iframe->state = NGHTTP2_IB_IGN_CONTINUATION;
6511
0
        }
6512
0
      } else {
6513
0
        if (iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK) {
6514
0
          rv = session_after_header_block_received(session);
6515
0
          if (nghttp2_is_fatal(rv)) {
6516
0
            return rv;
6517
0
          }
6518
6519
0
          if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6520
0
            return (nghttp2_ssize)inlen;
6521
0
          }
6522
0
        }
6523
0
        session_inbound_frame_reset(session);
6524
6525
0
        session->num_continuations = 0;
6526
0
      }
6527
0
      break;
6528
0
    }
6529
0
    case NGHTTP2_IB_IGN_PAYLOAD:
6530
0
      DEBUGF("recv: [IB_IGN_PAYLOAD]\n");
6531
6532
0
      readlen = inbound_frame_payload_readlen(iframe, in, last);
6533
0
      iframe->payloadleft -= readlen;
6534
0
      in += readlen;
6535
6536
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
6537
0
             iframe->payloadleft);
6538
6539
0
      if (iframe->payloadleft) {
6540
0
        break;
6541
0
      }
6542
6543
0
      switch (iframe->frame.hd.type) {
6544
0
      case NGHTTP2_HEADERS:
6545
0
      case NGHTTP2_PUSH_PROMISE:
6546
0
      case NGHTTP2_CONTINUATION:
6547
        /* Mark inflater bad so that we won't perform further decoding */
6548
0
        session->hd_inflater.ctx.bad = 1;
6549
0
        break;
6550
0
      default:
6551
0
        break;
6552
0
      }
6553
6554
0
      session_inbound_frame_reset(session);
6555
6556
0
      break;
6557
0
    case NGHTTP2_IB_FRAME_SIZE_ERROR:
6558
0
      DEBUGF("recv: [IB_FRAME_SIZE_ERROR]\n");
6559
6560
0
      rv = session_handle_frame_size_error(session);
6561
0
      if (nghttp2_is_fatal(rv)) {
6562
0
        return rv;
6563
0
      }
6564
6565
0
      assert(iframe->state == NGHTTP2_IB_IGN_ALL);
6566
6567
0
      return (nghttp2_ssize)inlen;
6568
0
    case NGHTTP2_IB_READ_SETTINGS:
6569
0
      DEBUGF("recv: [IB_READ_SETTINGS]\n");
6570
6571
0
      readlen = inbound_frame_buf_read(iframe, in, last);
6572
0
      iframe->payloadleft -= readlen;
6573
0
      in += readlen;
6574
6575
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
6576
0
             iframe->payloadleft);
6577
6578
0
      if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
6579
0
        break;
6580
0
      }
6581
6582
0
      if (readlen > 0) {
6583
0
        inbound_frame_set_settings_entry(iframe);
6584
0
      }
6585
0
      if (iframe->payloadleft) {
6586
0
        inbound_frame_set_mark(iframe, NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH);
6587
0
        break;
6588
0
      }
6589
6590
0
      rv = session_process_settings_frame(session);
6591
6592
0
      if (nghttp2_is_fatal(rv)) {
6593
0
        return rv;
6594
0
      }
6595
6596
0
      if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6597
0
        return (nghttp2_ssize)inlen;
6598
0
      }
6599
6600
0
      session_inbound_frame_reset(session);
6601
6602
0
      break;
6603
0
    case NGHTTP2_IB_READ_GOAWAY_DEBUG:
6604
0
      DEBUGF("recv: [IB_READ_GOAWAY_DEBUG]\n");
6605
6606
0
      readlen = inbound_frame_payload_readlen(iframe, in, last);
6607
6608
0
      if (readlen > 0) {
6609
0
        iframe->lbuf.last = nghttp2_cpymem(iframe->lbuf.last, in, readlen);
6610
6611
0
        iframe->payloadleft -= readlen;
6612
0
        in += readlen;
6613
0
      }
6614
6615
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
6616
0
             iframe->payloadleft);
6617
6618
0
      if (iframe->payloadleft) {
6619
0
        assert(nghttp2_buf_avail(&iframe->lbuf) > 0);
6620
6621
0
        break;
6622
0
      }
6623
6624
0
      rv = session_process_goaway_frame(session);
6625
6626
0
      if (nghttp2_is_fatal(rv)) {
6627
0
        return rv;
6628
0
      }
6629
6630
0
      if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6631
0
        return (nghttp2_ssize)inlen;
6632
0
      }
6633
6634
0
      session_inbound_frame_reset(session);
6635
6636
0
      break;
6637
0
    case NGHTTP2_IB_EXPECT_CONTINUATION:
6638
0
    case NGHTTP2_IB_IGN_CONTINUATION:
6639
#ifdef DEBUGBUILD
6640
      if (iframe->state == NGHTTP2_IB_EXPECT_CONTINUATION) {
6641
        fprintf(stderr, "recv: [IB_EXPECT_CONTINUATION]\n");
6642
      } else {
6643
        fprintf(stderr, "recv: [IB_IGN_CONTINUATION]\n");
6644
      }
6645
#endif /* defined(DEBUGBUILD) */
6646
6647
0
      readlen = inbound_frame_buf_read(iframe, in, last);
6648
0
      in += readlen;
6649
6650
0
      if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
6651
0
        return (nghttp2_ssize)(in - first);
6652
0
      }
6653
6654
0
      nghttp2_frame_unpack_frame_hd(&cont_hd, iframe->sbuf.pos);
6655
0
      iframe->payloadleft = cont_hd.length;
6656
6657
0
      DEBUGF("recv: payloadlen=%zu, type=%u, flags=0x%02x, stream_id=%d\n",
6658
0
             cont_hd.length, cont_hd.type, cont_hd.flags, cont_hd.stream_id);
6659
6660
0
      if (cont_hd.type != NGHTTP2_CONTINUATION ||
6661
0
          cont_hd.stream_id != iframe->frame.hd.stream_id) {
6662
0
        DEBUGF("recv: expected stream_id=%d, type=%d, but got stream_id=%d, "
6663
0
               "type=%u\n",
6664
0
               iframe->frame.hd.stream_id, NGHTTP2_CONTINUATION,
6665
0
               cont_hd.stream_id, cont_hd.type);
6666
0
        rv = nghttp2_session_terminate_session_with_reason(
6667
0
          session, NGHTTP2_PROTOCOL_ERROR,
6668
0
          "unexpected non-CONTINUATION frame or stream_id is invalid");
6669
0
        if (nghttp2_is_fatal(rv)) {
6670
0
          return rv;
6671
0
        }
6672
6673
0
        return (nghttp2_ssize)inlen;
6674
0
      }
6675
6676
0
      if (++session->num_continuations > session->max_continuations) {
6677
0
        return NGHTTP2_ERR_TOO_MANY_CONTINUATIONS;
6678
0
      }
6679
6680
      /* CONTINUATION won't bear NGHTTP2_PADDED flag */
6681
6682
0
      iframe->frame.hd.flags =
6683
0
        (uint8_t)(iframe->frame.hd.flags |
6684
0
                  (cont_hd.flags & NGHTTP2_FLAG_END_HEADERS));
6685
0
      iframe->frame.hd.length += cont_hd.length;
6686
6687
0
      busy = 1;
6688
6689
0
      if (iframe->state == NGHTTP2_IB_EXPECT_CONTINUATION) {
6690
0
        iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
6691
6692
0
        rv = session_call_on_begin_frame(session, &cont_hd);
6693
6694
0
        if (nghttp2_is_fatal(rv)) {
6695
0
          return rv;
6696
0
        }
6697
6698
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6699
0
          return (nghttp2_ssize)inlen;
6700
0
        }
6701
0
      } else {
6702
0
        iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
6703
0
      }
6704
6705
0
      break;
6706
0
    case NGHTTP2_IB_READ_PAD_DATA:
6707
0
      DEBUGF("recv: [IB_READ_PAD_DATA]\n");
6708
6709
0
      readlen = inbound_frame_buf_read(iframe, in, last);
6710
0
      in += readlen;
6711
0
      iframe->payloadleft -= readlen;
6712
6713
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu, left=%zu\n", readlen,
6714
0
             iframe->payloadleft, nghttp2_buf_mark_avail(&iframe->sbuf));
6715
6716
0
      if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
6717
0
        return (nghttp2_ssize)(in - first);
6718
0
      }
6719
6720
      /* Pad Length field is subject to flow control */
6721
0
      rv = nghttp2_session_update_recv_connection_window_size(session, readlen);
6722
0
      if (nghttp2_is_fatal(rv)) {
6723
0
        return rv;
6724
0
      }
6725
6726
0
      if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6727
0
        return (nghttp2_ssize)inlen;
6728
0
      }
6729
6730
      /* Pad Length field is consumed immediately */
6731
0
      rv =
6732
0
        nghttp2_session_consume(session, iframe->frame.hd.stream_id, readlen);
6733
6734
0
      if (nghttp2_is_fatal(rv)) {
6735
0
        return rv;
6736
0
      }
6737
6738
0
      if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6739
0
        return (nghttp2_ssize)inlen;
6740
0
      }
6741
6742
0
      stream = nghttp2_session_get_stream(session, iframe->frame.hd.stream_id);
6743
0
      if (stream) {
6744
0
        rv = nghttp2_session_update_recv_stream_window_size(
6745
0
          session, stream, readlen,
6746
0
          iframe->payloadleft ||
6747
0
            (iframe->frame.hd.flags & NGHTTP2_FLAG_END_STREAM) == 0);
6748
0
        if (nghttp2_is_fatal(rv)) {
6749
0
          return rv;
6750
0
        }
6751
6752
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6753
0
          return (nghttp2_ssize)inlen;
6754
0
        }
6755
0
      }
6756
6757
0
      busy = 1;
6758
6759
0
      padlen = inbound_frame_compute_pad(iframe);
6760
0
      if (padlen < 0) {
6761
0
        rv = nghttp2_session_terminate_session_with_reason(
6762
0
          session, NGHTTP2_PROTOCOL_ERROR, "DATA: invalid padding");
6763
0
        if (nghttp2_is_fatal(rv)) {
6764
0
          return rv;
6765
0
        }
6766
0
        return (nghttp2_ssize)inlen;
6767
0
      }
6768
6769
0
      iframe->frame.data.padlen = (size_t)padlen;
6770
6771
      /* Empty DATA frame without END_STREAM flag set is
6772
         suspicious. */
6773
0
      if (iframe->payloadleft == 0 &&
6774
0
          (iframe->frame.hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
6775
0
        rv = session_update_glitch_ratelim(session);
6776
0
        if (rv != 0) {
6777
0
          return rv;
6778
0
        }
6779
6780
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6781
0
          return (nghttp2_ssize)inlen;
6782
0
        }
6783
0
      }
6784
6785
0
      iframe->state = NGHTTP2_IB_READ_DATA;
6786
6787
0
      break;
6788
0
    case NGHTTP2_IB_READ_DATA:
6789
0
      stream = nghttp2_session_get_stream(session, iframe->frame.hd.stream_id);
6790
6791
0
      if (!stream) {
6792
0
        busy = 1;
6793
0
        iframe->state = NGHTTP2_IB_IGN_DATA;
6794
0
        break;
6795
0
      }
6796
6797
0
      DEBUGF("recv: [IB_READ_DATA]\n");
6798
6799
0
      readlen = inbound_frame_payload_readlen(iframe, in, last);
6800
0
      iframe->payloadleft -= readlen;
6801
0
      in += readlen;
6802
6803
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
6804
0
             iframe->payloadleft);
6805
6806
0
      if (readlen > 0) {
6807
0
        nghttp2_ssize data_readlen;
6808
6809
0
        rv =
6810
0
          nghttp2_session_update_recv_connection_window_size(session, readlen);
6811
0
        if (nghttp2_is_fatal(rv)) {
6812
0
          return rv;
6813
0
        }
6814
6815
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6816
0
          return (nghttp2_ssize)inlen;
6817
0
        }
6818
6819
0
        rv = nghttp2_session_update_recv_stream_window_size(
6820
0
          session, stream, readlen,
6821
0
          iframe->payloadleft ||
6822
0
            (iframe->frame.hd.flags & NGHTTP2_FLAG_END_STREAM) == 0);
6823
0
        if (nghttp2_is_fatal(rv)) {
6824
0
          return rv;
6825
0
        }
6826
6827
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6828
0
          return (nghttp2_ssize)inlen;
6829
0
        }
6830
6831
0
        data_readlen =
6832
0
          inbound_frame_effective_readlen(iframe, iframe->payloadleft, readlen);
6833
6834
0
        if (data_readlen == -1) {
6835
          /* everything is padding */
6836
0
          data_readlen = 0;
6837
0
        }
6838
6839
0
        padlen = (nghttp2_ssize)readlen - data_readlen;
6840
6841
0
        if (padlen > 0) {
6842
          /* Padding is considered as "consumed" immediately */
6843
0
          rv = nghttp2_session_consume(session, iframe->frame.hd.stream_id,
6844
0
                                       (size_t)padlen);
6845
6846
0
          if (nghttp2_is_fatal(rv)) {
6847
0
            return rv;
6848
0
          }
6849
6850
0
          if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6851
0
            return (nghttp2_ssize)inlen;
6852
0
          }
6853
0
        }
6854
6855
0
        DEBUGF("recv: data_readlen=%td\n", data_readlen);
6856
6857
0
        if (data_readlen > 0) {
6858
0
          if (session_enforce_http_messaging(session)) {
6859
0
            if (nghttp2_http_on_data_chunk(stream, (size_t)data_readlen) != 0) {
6860
0
              if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
6861
                /* Consume all data for connection immediately here */
6862
0
                rv = session_update_connection_consumed_size(
6863
0
                  session, (size_t)data_readlen);
6864
6865
0
                if (nghttp2_is_fatal(rv)) {
6866
0
                  return rv;
6867
0
                }
6868
6869
0
                if (iframe->state == NGHTTP2_IB_IGN_DATA) {
6870
0
                  return (nghttp2_ssize)inlen;
6871
0
                }
6872
0
              }
6873
6874
0
              rv = session_handle_invalid_stream2(
6875
0
                session, iframe->frame.hd.stream_id, &iframe->frame,
6876
0
                NGHTTP2_ERR_PROTO);
6877
0
              if (nghttp2_is_fatal(rv)) {
6878
0
                return rv;
6879
0
              }
6880
6881
0
              rv = session_update_glitch_ratelim(session);
6882
0
              if (rv != 0) {
6883
0
                return rv;
6884
0
              }
6885
6886
0
              if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6887
0
                return (nghttp2_ssize)inlen;
6888
0
              }
6889
6890
0
              busy = 1;
6891
0
              iframe->state = NGHTTP2_IB_IGN_DATA;
6892
6893
0
              break;
6894
0
            }
6895
0
          }
6896
0
          if (session->callbacks.on_data_chunk_recv_callback) {
6897
0
            rv = session->callbacks.on_data_chunk_recv_callback(
6898
0
              session, iframe->frame.hd.flags, iframe->frame.hd.stream_id,
6899
0
              in - readlen, (size_t)data_readlen, session->user_data);
6900
0
            if (nghttp2_is_fatal(rv)) {
6901
0
              return NGHTTP2_ERR_CALLBACK_FAILURE;
6902
0
            }
6903
6904
0
            if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6905
0
              return (nghttp2_ssize)inlen;
6906
0
            }
6907
6908
0
            if (rv == NGHTTP2_ERR_PAUSE) {
6909
0
              return (nghttp2_ssize)(in - first);
6910
0
            }
6911
0
          }
6912
0
        }
6913
0
      }
6914
6915
0
      if (iframe->payloadleft) {
6916
0
        break;
6917
0
      }
6918
6919
0
      rv = session_process_data_frame(session);
6920
0
      if (nghttp2_is_fatal(rv)) {
6921
0
        return rv;
6922
0
      }
6923
6924
0
      if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6925
0
        return (nghttp2_ssize)inlen;
6926
0
      }
6927
6928
0
      session_inbound_frame_reset(session);
6929
6930
0
      break;
6931
0
    case NGHTTP2_IB_IGN_DATA:
6932
0
      DEBUGF("recv: [IB_IGN_DATA]\n");
6933
6934
0
      readlen = inbound_frame_payload_readlen(iframe, in, last);
6935
0
      iframe->payloadleft -= readlen;
6936
0
      in += readlen;
6937
6938
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
6939
0
             iframe->payloadleft);
6940
6941
0
      if (readlen > 0) {
6942
        /* Update connection-level flow control window for ignored
6943
           DATA frame too */
6944
0
        rv =
6945
0
          nghttp2_session_update_recv_connection_window_size(session, readlen);
6946
0
        if (nghttp2_is_fatal(rv)) {
6947
0
          return rv;
6948
0
        }
6949
6950
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6951
0
          return (nghttp2_ssize)inlen;
6952
0
        }
6953
6954
0
        if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
6955
          /* Ignored DATA is considered as "consumed" immediately. */
6956
0
          rv = session_update_connection_consumed_size(session, readlen);
6957
6958
0
          if (nghttp2_is_fatal(rv)) {
6959
0
            return rv;
6960
0
          }
6961
6962
0
          if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6963
0
            return (nghttp2_ssize)inlen;
6964
0
          }
6965
0
        }
6966
0
      }
6967
6968
0
      if (iframe->payloadleft) {
6969
0
        break;
6970
0
      }
6971
6972
0
      session_inbound_frame_reset(session);
6973
6974
0
      break;
6975
0
    case NGHTTP2_IB_IGN_ALL:
6976
0
      return (nghttp2_ssize)inlen;
6977
0
    case NGHTTP2_IB_READ_EXTENSION_PAYLOAD:
6978
0
      DEBUGF("recv: [IB_READ_EXTENSION_PAYLOAD]\n");
6979
6980
0
      readlen = inbound_frame_payload_readlen(iframe, in, last);
6981
0
      iframe->payloadleft -= readlen;
6982
0
      in += readlen;
6983
6984
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
6985
0
             iframe->payloadleft);
6986
6987
0
      if (readlen > 0) {
6988
0
        rv = session_call_on_extension_chunk_recv_callback(
6989
0
          session, in - readlen, readlen);
6990
0
        if (nghttp2_is_fatal(rv)) {
6991
0
          return rv;
6992
0
        }
6993
6994
0
        if (iframe->state == NGHTTP2_IB_IGN_ALL) {
6995
0
          return (nghttp2_ssize)inlen;
6996
0
        }
6997
6998
0
        if (rv != 0) {
6999
0
          busy = 1;
7000
7001
0
          iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
7002
7003
0
          break;
7004
0
        }
7005
0
      }
7006
7007
0
      if (iframe->payloadleft > 0) {
7008
0
        break;
7009
0
      }
7010
7011
0
      rv = session_process_extension_frame(session);
7012
0
      if (nghttp2_is_fatal(rv)) {
7013
0
        return rv;
7014
0
      }
7015
7016
0
      if (iframe->state == NGHTTP2_IB_IGN_ALL) {
7017
0
        return (nghttp2_ssize)inlen;
7018
0
      }
7019
7020
0
      session_inbound_frame_reset(session);
7021
7022
0
      break;
7023
0
    case NGHTTP2_IB_READ_ALTSVC_PAYLOAD:
7024
0
      DEBUGF("recv: [IB_READ_ALTSVC_PAYLOAD]\n");
7025
7026
0
      readlen = inbound_frame_payload_readlen(iframe, in, last);
7027
0
      if (readlen > 0) {
7028
0
        iframe->lbuf.last = nghttp2_cpymem(iframe->lbuf.last, in, readlen);
7029
7030
0
        iframe->payloadleft -= readlen;
7031
0
        in += readlen;
7032
0
      }
7033
7034
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
7035
0
             iframe->payloadleft);
7036
7037
0
      if (iframe->payloadleft) {
7038
0
        assert(nghttp2_buf_avail(&iframe->lbuf) > 0);
7039
7040
0
        break;
7041
0
      }
7042
7043
0
      rv = session_process_altsvc_frame(session);
7044
0
      if (nghttp2_is_fatal(rv)) {
7045
0
        return rv;
7046
0
      }
7047
7048
0
      if (iframe->state == NGHTTP2_IB_IGN_ALL) {
7049
0
        return (nghttp2_ssize)inlen;
7050
0
      }
7051
7052
0
      session_inbound_frame_reset(session);
7053
7054
0
      break;
7055
0
    case NGHTTP2_IB_READ_ORIGIN_PAYLOAD:
7056
0
      DEBUGF("recv: [IB_READ_ORIGIN_PAYLOAD]\n");
7057
7058
0
      readlen = inbound_frame_payload_readlen(iframe, in, last);
7059
7060
0
      if (readlen > 0) {
7061
0
        iframe->lbuf.last = nghttp2_cpymem(iframe->lbuf.last, in, readlen);
7062
7063
0
        iframe->payloadleft -= readlen;
7064
0
        in += readlen;
7065
0
      }
7066
7067
0
      DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
7068
0
             iframe->payloadleft);
7069
7070
0
      if (iframe->payloadleft) {
7071
0
        assert(nghttp2_buf_avail(&iframe->lbuf) > 0);
7072
7073
0
        break;
7074
0
      }
7075
7076
0
      rv = session_process_origin_frame(session);
7077
7078
0
      if (nghttp2_is_fatal(rv)) {
7079
0
        return rv;
7080
0
      }
7081
7082
0
      if (iframe->state == NGHTTP2_IB_IGN_ALL) {
7083
0
        return (nghttp2_ssize)inlen;
7084
0
      }
7085
7086
0
      session_inbound_frame_reset(session);
7087
7088
0
      break;
7089
0
    }
7090
7091
0
    if (!busy && in == last) {
7092
0
      break;
7093
0
    }
7094
7095
0
    busy = 0;
7096
0
  }
7097
7098
0
  assert(in == last);
7099
7100
0
  return (nghttp2_ssize)(in - first);
7101
0
}
7102
7103
nghttp2_ssize nghttp2_session_mem_recv2(nghttp2_session *session,
7104
0
                                        const uint8_t *in, size_t inlen) {
7105
0
  nghttp2_ssize nread;
7106
7107
0
  nread = session_mem_recv(session, in, inlen);
7108
0
  if (nread < 0 && nghttp2_is_fatal((int)nread)) {
7109
0
    return nread;
7110
0
  }
7111
7112
0
  if (nghttp2_session_get_outbound_queue_size(session) >
7113
0
      session->max_outbound_queue_size) {
7114
0
    return NGHTTP2_ERR_FLOODED;
7115
0
  }
7116
7117
0
  return nread;
7118
0
}
7119
7120
0
int nghttp2_session_recv(nghttp2_session *session) {
7121
0
  uint8_t buf[NGHTTP2_INBOUND_BUFFER_LENGTH];
7122
0
  while (1) {
7123
0
    nghttp2_ssize readlen;
7124
0
    readlen = session_recv(session, buf, sizeof(buf));
7125
0
    if (readlen > 0) {
7126
0
      nghttp2_ssize proclen =
7127
0
        nghttp2_session_mem_recv2(session, buf, (size_t)readlen);
7128
0
      if (proclen < 0) {
7129
0
        return (int)proclen;
7130
0
      }
7131
0
      assert(proclen == readlen);
7132
0
    } else if (readlen == 0 || readlen == NGHTTP2_ERR_WOULDBLOCK) {
7133
0
      return 0;
7134
0
    } else if (readlen == NGHTTP2_ERR_EOF) {
7135
0
      return NGHTTP2_ERR_EOF;
7136
0
    } else if (readlen < 0) {
7137
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
7138
0
    }
7139
0
  }
7140
0
}
7141
7142
/*
7143
 * Returns the number of active streams, which includes streams in
7144
 * reserved state.
7145
 */
7146
0
static size_t session_get_num_active_streams(nghttp2_session *session) {
7147
0
  return nghttp2_map_size(&session->streams) - session->num_closed_streams -
7148
0
         session->num_idle_streams;
7149
0
}
7150
7151
0
int nghttp2_session_want_read(nghttp2_session *session) {
7152
0
  size_t num_active_streams;
7153
7154
  /* If this flag is set, we don't want to read. The application
7155
     should drop the connection. */
7156
0
  if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_SENT) {
7157
0
    return 0;
7158
0
  }
7159
7160
0
  num_active_streams = session_get_num_active_streams(session);
7161
7162
  /* Unless termination GOAWAY is sent or received, we always want to
7163
     read incoming frames. */
7164
7165
0
  if (num_active_streams > 0) {
7166
0
    return 1;
7167
0
  }
7168
7169
  /* If there is no active streams and GOAWAY has been sent or
7170
     received, we are done with this session. */
7171
0
  return (session->goaway_flags &
7172
0
          (NGHTTP2_GOAWAY_SENT | NGHTTP2_GOAWAY_RECV)) == 0;
7173
0
}
7174
7175
0
int nghttp2_session_want_write(nghttp2_session *session) {
7176
  /* If these flag is set, we don't want to write any data. The
7177
     application should drop the connection. */
7178
0
  if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_SENT) {
7179
0
    return 0;
7180
0
  }
7181
7182
  /*
7183
   * Unless termination GOAWAY is sent or received, we want to write
7184
   * frames if there is pending ones. If pending frame is request/push
7185
   * response HEADERS and concurrent stream limit is reached, we don't
7186
   * want to write them.
7187
   */
7188
0
  return session->aob.item || nghttp2_outbound_queue_top(&session->ob_urgent) ||
7189
0
         nghttp2_outbound_queue_top(&session->ob_reg) ||
7190
0
         (!session_sched_empty(session) && session->remote_window_size > 0) ||
7191
0
         (nghttp2_outbound_queue_top(&session->ob_syn) &&
7192
0
          !session_is_outgoing_concurrent_streams_max(session));
7193
0
}
7194
7195
int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags,
7196
0
                             const uint8_t *opaque_data) {
7197
0
  int rv;
7198
0
  nghttp2_outbound_item *item;
7199
0
  nghttp2_frame *frame;
7200
0
  nghttp2_mem *mem;
7201
7202
0
  mem = &session->mem;
7203
7204
0
  if ((flags & NGHTTP2_FLAG_ACK) &&
7205
0
      session->obq_flood_counter_ >= session->max_outbound_ack) {
7206
0
    return NGHTTP2_ERR_FLOODED;
7207
0
  }
7208
7209
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
7210
0
  if (item == NULL) {
7211
0
    return NGHTTP2_ERR_NOMEM;
7212
0
  }
7213
7214
0
  nghttp2_outbound_item_init(item);
7215
7216
0
  frame = &item->frame;
7217
7218
0
  nghttp2_frame_ping_init(&frame->ping, flags, opaque_data);
7219
7220
0
  rv = nghttp2_session_add_item(session, item);
7221
7222
0
  if (rv != 0) {
7223
0
    nghttp2_frame_ping_free(&frame->ping);
7224
0
    nghttp2_mem_free(mem, item);
7225
0
    return rv;
7226
0
  }
7227
7228
0
  if (flags & NGHTTP2_FLAG_ACK) {
7229
0
    ++session->obq_flood_counter_;
7230
0
  }
7231
7232
0
  return 0;
7233
0
}
7234
7235
int nghttp2_session_add_goaway(nghttp2_session *session, int32_t last_stream_id,
7236
                               uint32_t error_code, const uint8_t *opaque_data,
7237
0
                               size_t opaque_data_len, uint8_t aux_flags) {
7238
0
  int rv;
7239
0
  nghttp2_outbound_item *item;
7240
0
  nghttp2_frame *frame;
7241
0
  uint8_t *opaque_data_copy = NULL;
7242
0
  nghttp2_goaway_aux_data *aux_data;
7243
0
  nghttp2_mem *mem;
7244
7245
0
  mem = &session->mem;
7246
7247
0
  if (nghttp2_session_is_my_stream_id(session, last_stream_id)) {
7248
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
7249
0
  }
7250
7251
0
  if (opaque_data_len) {
7252
0
    if (opaque_data_len + 8 > NGHTTP2_MAX_PAYLOADLEN) {
7253
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
7254
0
    }
7255
0
    opaque_data_copy = nghttp2_mem_malloc(mem, opaque_data_len);
7256
0
    if (opaque_data_copy == NULL) {
7257
0
      return NGHTTP2_ERR_NOMEM;
7258
0
    }
7259
0
    memcpy(opaque_data_copy, opaque_data, opaque_data_len);
7260
0
  }
7261
7262
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
7263
0
  if (item == NULL) {
7264
0
    nghttp2_mem_free(mem, opaque_data_copy);
7265
0
    return NGHTTP2_ERR_NOMEM;
7266
0
  }
7267
7268
0
  nghttp2_outbound_item_init(item);
7269
7270
0
  frame = &item->frame;
7271
7272
  /* last_stream_id must not be increased from the value previously
7273
     sent */
7274
0
  last_stream_id =
7275
0
    nghttp2_min_int32(last_stream_id, session->local_last_stream_id);
7276
7277
0
  nghttp2_frame_goaway_init(&frame->goaway, last_stream_id, error_code,
7278
0
                            opaque_data_copy, opaque_data_len);
7279
7280
0
  aux_data = &item->aux_data.goaway;
7281
0
  aux_data->flags = aux_flags;
7282
7283
0
  rv = nghttp2_session_add_item(session, item);
7284
0
  if (rv != 0) {
7285
0
    nghttp2_frame_goaway_free(&frame->goaway, mem);
7286
0
    nghttp2_mem_free(mem, item);
7287
0
    return rv;
7288
0
  }
7289
7290
0
  session->goaway_flags |= NGHTTP2_GOAWAY_SUBMITTED;
7291
7292
0
  return 0;
7293
0
}
7294
7295
int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags,
7296
                                      int32_t stream_id,
7297
0
                                      int32_t window_size_increment) {
7298
0
  int rv;
7299
0
  nghttp2_outbound_item *item;
7300
0
  nghttp2_frame *frame;
7301
0
  nghttp2_mem *mem;
7302
7303
0
  mem = &session->mem;
7304
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
7305
0
  if (item == NULL) {
7306
0
    return NGHTTP2_ERR_NOMEM;
7307
0
  }
7308
7309
0
  nghttp2_outbound_item_init(item);
7310
7311
0
  frame = &item->frame;
7312
7313
0
  nghttp2_frame_window_update_init(&frame->window_update, flags, stream_id,
7314
0
                                   window_size_increment);
7315
7316
0
  rv = nghttp2_session_add_item(session, item);
7317
7318
0
  if (rv != 0) {
7319
0
    nghttp2_frame_window_update_free(&frame->window_update);
7320
0
    nghttp2_mem_free(mem, item);
7321
0
    return rv;
7322
0
  }
7323
0
  return 0;
7324
0
}
7325
7326
static void
7327
session_append_inflight_settings(nghttp2_session *session,
7328
0
                                 nghttp2_inflight_settings *settings) {
7329
0
  nghttp2_inflight_settings **i;
7330
7331
0
  for (i = &session->inflight_settings_head; *i; i = &(*i)->next)
7332
0
    ;
7333
7334
0
  *i = settings;
7335
0
}
7336
7337
int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
7338
0
                                 const nghttp2_settings_entry *iv, size_t niv) {
7339
0
  nghttp2_outbound_item *item;
7340
0
  nghttp2_frame *frame;
7341
0
  nghttp2_settings_entry *iv_copy;
7342
0
  size_t i;
7343
0
  int rv;
7344
0
  nghttp2_mem *mem;
7345
0
  nghttp2_inflight_settings *inflight_settings = NULL;
7346
0
  uint8_t no_rfc7540_pri = session->pending_no_rfc7540_priorities;
7347
7348
0
  mem = &session->mem;
7349
7350
0
  if (flags & NGHTTP2_FLAG_ACK) {
7351
0
    if (niv != 0) {
7352
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
7353
0
    }
7354
7355
0
    if (session->obq_flood_counter_ >= session->max_outbound_ack) {
7356
0
      return NGHTTP2_ERR_FLOODED;
7357
0
    }
7358
0
  }
7359
7360
0
  if (!nghttp2_iv_check(iv, niv)) {
7361
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
7362
0
  }
7363
7364
0
  for (i = 0; i < niv; ++i) {
7365
0
    if (iv[i].settings_id != NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES) {
7366
0
      continue;
7367
0
    }
7368
7369
0
    if (no_rfc7540_pri == UINT8_MAX) {
7370
0
      no_rfc7540_pri = (uint8_t)iv[i].value;
7371
0
      continue;
7372
0
    }
7373
7374
0
    if (iv[i].value != (uint32_t)no_rfc7540_pri) {
7375
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
7376
0
    }
7377
0
  }
7378
7379
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
7380
0
  if (item == NULL) {
7381
0
    return NGHTTP2_ERR_NOMEM;
7382
0
  }
7383
7384
0
  if (niv > 0) {
7385
0
    iv_copy = nghttp2_frame_iv_copy(iv, niv, mem);
7386
0
    if (iv_copy == NULL) {
7387
0
      nghttp2_mem_free(mem, item);
7388
0
      return NGHTTP2_ERR_NOMEM;
7389
0
    }
7390
0
  } else {
7391
0
    iv_copy = NULL;
7392
0
  }
7393
7394
0
  if ((flags & NGHTTP2_FLAG_ACK) == 0) {
7395
0
    rv = inflight_settings_new(&inflight_settings, iv, niv, mem);
7396
0
    if (rv != 0) {
7397
0
      assert(nghttp2_is_fatal(rv));
7398
0
      nghttp2_mem_free(mem, iv_copy);
7399
0
      nghttp2_mem_free(mem, item);
7400
0
      return rv;
7401
0
    }
7402
0
  }
7403
7404
0
  nghttp2_outbound_item_init(item);
7405
7406
0
  frame = &item->frame;
7407
7408
0
  nghttp2_frame_settings_init(&frame->settings, flags, iv_copy, niv);
7409
0
  rv = nghttp2_session_add_item(session, item);
7410
0
  if (rv != 0) {
7411
    /* The only expected error is fatal one */
7412
0
    assert(nghttp2_is_fatal(rv));
7413
7414
0
    inflight_settings_del(inflight_settings, mem);
7415
7416
0
    nghttp2_frame_settings_free(&frame->settings, mem);
7417
0
    nghttp2_mem_free(mem, item);
7418
7419
0
    return rv;
7420
0
  }
7421
7422
0
  if (flags & NGHTTP2_FLAG_ACK) {
7423
0
    ++session->obq_flood_counter_;
7424
0
  } else {
7425
0
    session_append_inflight_settings(session, inflight_settings);
7426
0
  }
7427
7428
  /* Extract NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS and ENABLE_PUSH
7429
     here.  We use it to refuse the incoming stream and PUSH_PROMISE
7430
     with RST_STREAM. */
7431
7432
0
  for (i = niv; i > 0; --i) {
7433
0
    if (iv[i - 1].settings_id == NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS) {
7434
0
      session->pending_local_max_concurrent_stream = iv[i - 1].value;
7435
0
      break;
7436
0
    }
7437
0
  }
7438
7439
0
  for (i = niv; i > 0; --i) {
7440
0
    if (iv[i - 1].settings_id == NGHTTP2_SETTINGS_ENABLE_PUSH) {
7441
0
      session->pending_enable_push = (uint8_t)iv[i - 1].value;
7442
0
      break;
7443
0
    }
7444
0
  }
7445
7446
0
  for (i = niv; i > 0; --i) {
7447
0
    if (iv[i - 1].settings_id == NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL) {
7448
0
      session->pending_enable_connect_protocol = (uint8_t)iv[i - 1].value;
7449
0
      break;
7450
0
    }
7451
0
  }
7452
7453
0
  if (no_rfc7540_pri == UINT8_MAX) {
7454
0
    session->pending_no_rfc7540_priorities = 0;
7455
0
  } else {
7456
0
    session->pending_no_rfc7540_priorities = no_rfc7540_pri;
7457
0
  }
7458
7459
0
  return 0;
7460
0
}
7461
7462
int nghttp2_session_pack_data(nghttp2_session *session, nghttp2_bufs *bufs,
7463
                              size_t datamax, nghttp2_frame *frame,
7464
                              nghttp2_data_aux_data *aux_data,
7465
0
                              nghttp2_stream *stream) {
7466
0
  int rv;
7467
0
  uint32_t data_flags;
7468
0
  nghttp2_ssize payloadlen;
7469
0
  nghttp2_ssize padded_payloadlen;
7470
0
  nghttp2_buf *buf;
7471
0
  size_t max_payloadlen;
7472
7473
0
  assert(bufs->head == bufs->cur);
7474
7475
0
  buf = &bufs->cur->buf;
7476
7477
0
  if (session->callbacks.read_length_callback2 ||
7478
0
      session->callbacks.read_length_callback) {
7479
0
    if (session->callbacks.read_length_callback2) {
7480
0
      payloadlen = session->callbacks.read_length_callback2(
7481
0
        session, frame->hd.type, stream->stream_id, session->remote_window_size,
7482
0
        stream->remote_window_size, session->remote_settings.max_frame_size,
7483
0
        session->user_data);
7484
0
    } else {
7485
0
      payloadlen = (nghttp2_ssize)session->callbacks.read_length_callback(
7486
0
        session, frame->hd.type, stream->stream_id, session->remote_window_size,
7487
0
        stream->remote_window_size, session->remote_settings.max_frame_size,
7488
0
        session->user_data);
7489
0
    }
7490
7491
0
    DEBUGF("send: read_length_callback=%td\n", payloadlen);
7492
7493
0
    payloadlen =
7494
0
      nghttp2_session_enforce_flow_control_limits(session, stream, payloadlen);
7495
7496
0
    DEBUGF("send: read_length_callback after flow control=%td\n", payloadlen);
7497
7498
0
    if (payloadlen <= 0) {
7499
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
7500
0
    }
7501
7502
0
    if ((size_t)payloadlen > nghttp2_buf_avail(buf)) {
7503
      /* Resize the current buffer(s).  The reason why we do +1 for
7504
         buffer size is for possible padding field. */
7505
0
      rv = nghttp2_bufs_realloc(&session->aob.framebufs,
7506
0
                                (size_t)(NGHTTP2_FRAME_HDLEN + 1 + payloadlen));
7507
7508
0
      if (rv != 0) {
7509
0
        DEBUGF("send: realloc buffer failed rv=%d", rv);
7510
        /* If reallocation failed, old buffers are still in tact.  So
7511
           use safe limit. */
7512
0
        payloadlen = (nghttp2_ssize)datamax;
7513
7514
0
        DEBUGF("send: use safe limit payloadlen=%td", payloadlen);
7515
0
      } else {
7516
0
        assert(&session->aob.framebufs == bufs);
7517
7518
0
        buf = &bufs->cur->buf;
7519
0
      }
7520
0
    }
7521
0
    datamax = (size_t)payloadlen;
7522
0
  }
7523
7524
  /* Current max DATA length is less then buffer chunk size */
7525
0
  assert(nghttp2_buf_avail(buf) >= datamax);
7526
7527
0
  data_flags = NGHTTP2_DATA_FLAG_NONE;
7528
0
  switch (aux_data->dpw.version) {
7529
0
  case NGHTTP2_DATA_PROVIDER_V1:
7530
0
    payloadlen = (nghttp2_ssize)aux_data->dpw.data_prd.v1.read_callback(
7531
0
      session, frame->hd.stream_id, buf->pos, datamax, &data_flags,
7532
0
      &aux_data->dpw.data_prd.v1.source, session->user_data);
7533
7534
0
    break;
7535
0
  case NGHTTP2_DATA_PROVIDER_V2:
7536
0
    payloadlen = aux_data->dpw.data_prd.v2.read_callback(
7537
0
      session, frame->hd.stream_id, buf->pos, datamax, &data_flags,
7538
0
      &aux_data->dpw.data_prd.v2.source, session->user_data);
7539
7540
0
    break;
7541
0
  default:
7542
0
    assert(0);
7543
0
    abort();
7544
0
  }
7545
7546
0
  if (payloadlen == NGHTTP2_ERR_DEFERRED ||
7547
0
      payloadlen == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE ||
7548
0
      payloadlen == NGHTTP2_ERR_PAUSE) {
7549
0
    DEBUGF("send: DATA postponed due to %s\n",
7550
0
           nghttp2_strerror((int)payloadlen));
7551
7552
0
    return (int)payloadlen;
7553
0
  }
7554
7555
0
  if (payloadlen < 0 || datamax < (size_t)payloadlen) {
7556
    /* This is the error code when callback is failed. */
7557
0
    return NGHTTP2_ERR_CALLBACK_FAILURE;
7558
0
  }
7559
7560
0
  buf->last = buf->pos + payloadlen;
7561
0
  buf->pos -= NGHTTP2_FRAME_HDLEN;
7562
7563
  /* Clear flags, because this may contain previous flags of previous
7564
     DATA */
7565
0
  frame->hd.flags = NGHTTP2_FLAG_NONE;
7566
7567
0
  if (data_flags & NGHTTP2_DATA_FLAG_EOF) {
7568
0
    aux_data->eof = 1;
7569
    /* If NGHTTP2_DATA_FLAG_NO_END_STREAM is set, don't set
7570
       NGHTTP2_FLAG_END_STREAM */
7571
0
    if ((aux_data->flags & NGHTTP2_FLAG_END_STREAM) &&
7572
0
        (data_flags & NGHTTP2_DATA_FLAG_NO_END_STREAM) == 0) {
7573
0
      frame->hd.flags |= NGHTTP2_FLAG_END_STREAM;
7574
0
    }
7575
0
  }
7576
7577
0
  if (data_flags & NGHTTP2_DATA_FLAG_NO_COPY) {
7578
0
    if (session->callbacks.send_data_callback == NULL) {
7579
0
      DEBUGF("NGHTTP2_DATA_FLAG_NO_COPY requires send_data_callback set\n");
7580
7581
0
      return NGHTTP2_ERR_CALLBACK_FAILURE;
7582
0
    }
7583
0
    aux_data->no_copy = 1;
7584
0
  }
7585
7586
0
  frame->hd.length = (size_t)payloadlen;
7587
0
  frame->data.padlen = 0;
7588
7589
0
  max_payloadlen =
7590
0
    nghttp2_min_size(datamax, frame->hd.length + NGHTTP2_MAX_PADLEN);
7591
7592
0
  padded_payloadlen =
7593
0
    session_call_select_padding(session, frame, max_payloadlen);
7594
7595
0
  if (nghttp2_is_fatal((int)padded_payloadlen)) {
7596
0
    return (int)padded_payloadlen;
7597
0
  }
7598
7599
0
  frame->data.padlen = (size_t)(padded_payloadlen - payloadlen);
7600
7601
0
  nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
7602
7603
0
  nghttp2_frame_add_pad(bufs, &frame->hd, frame->data.padlen,
7604
0
                        aux_data->no_copy);
7605
7606
0
  session_reschedule_stream(session, stream);
7607
7608
0
  if (frame->hd.length == 0 && (data_flags & NGHTTP2_DATA_FLAG_EOF) &&
7609
0
      (data_flags & NGHTTP2_DATA_FLAG_NO_END_STREAM)) {
7610
    /* DATA payload length is 0, and DATA frame does not bear
7611
       END_STREAM.  In this case, there is no point to send 0 length
7612
       DATA frame. */
7613
0
    return NGHTTP2_ERR_CANCEL;
7614
0
  }
7615
7616
0
  return 0;
7617
0
}
7618
7619
void *nghttp2_session_get_stream_user_data(nghttp2_session *session,
7620
0
                                           int32_t stream_id) {
7621
0
  nghttp2_stream *stream;
7622
0
  stream = nghttp2_session_get_stream(session, stream_id);
7623
0
  if (stream) {
7624
0
    return stream->stream_user_data;
7625
0
  } else {
7626
0
    return NULL;
7627
0
  }
7628
0
}
7629
7630
int nghttp2_session_set_stream_user_data(nghttp2_session *session,
7631
                                         int32_t stream_id,
7632
0
                                         void *stream_user_data) {
7633
0
  nghttp2_stream *stream;
7634
0
  nghttp2_frame *frame;
7635
0
  nghttp2_outbound_item *item;
7636
7637
0
  stream = nghttp2_session_get_stream(session, stream_id);
7638
0
  if (stream) {
7639
0
    stream->stream_user_data = stream_user_data;
7640
0
    return 0;
7641
0
  }
7642
7643
0
  if (session->server || !nghttp2_session_is_my_stream_id(session, stream_id) ||
7644
0
      !nghttp2_outbound_queue_top(&session->ob_syn)) {
7645
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
7646
0
  }
7647
7648
0
  frame = &nghttp2_outbound_queue_top(&session->ob_syn)->frame;
7649
0
  assert(frame->hd.type == NGHTTP2_HEADERS);
7650
7651
0
  if (frame->hd.stream_id > stream_id ||
7652
0
      (uint32_t)stream_id >= session->next_stream_id) {
7653
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
7654
0
  }
7655
7656
0
  for (item = session->ob_syn.head; item; item = item->qnext) {
7657
0
    if (item->frame.hd.stream_id < stream_id) {
7658
0
      continue;
7659
0
    }
7660
7661
0
    if (item->frame.hd.stream_id > stream_id) {
7662
0
      break;
7663
0
    }
7664
7665
0
    item->aux_data.headers.stream_user_data = stream_user_data;
7666
0
    return 0;
7667
0
  }
7668
7669
0
  return NGHTTP2_ERR_INVALID_ARGUMENT;
7670
0
}
7671
7672
0
int nghttp2_session_resume_data(nghttp2_session *session, int32_t stream_id) {
7673
0
  int rv;
7674
0
  nghttp2_stream *stream;
7675
0
  stream = nghttp2_session_get_stream(session, stream_id);
7676
0
  if (stream == NULL || !nghttp2_stream_check_deferred_item(stream)) {
7677
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
7678
0
  }
7679
7680
0
  rv = session_resume_deferred_stream_item(session, stream,
7681
0
                                           NGHTTP2_STREAM_FLAG_DEFERRED_USER);
7682
7683
0
  if (nghttp2_is_fatal(rv)) {
7684
0
    return rv;
7685
0
  }
7686
7687
0
  return 0;
7688
0
}
7689
7690
0
size_t nghttp2_session_get_outbound_queue_size(nghttp2_session *session) {
7691
0
  return nghttp2_outbound_queue_size(&session->ob_urgent) +
7692
0
         nghttp2_outbound_queue_size(&session->ob_reg) +
7693
0
         nghttp2_outbound_queue_size(&session->ob_syn);
7694
  /* TODO account for item attached to stream */
7695
0
}
7696
7697
int32_t
7698
nghttp2_session_get_stream_effective_recv_data_length(nghttp2_session *session,
7699
0
                                                      int32_t stream_id) {
7700
0
  nghttp2_stream *stream;
7701
0
  stream = nghttp2_session_get_stream(session, stream_id);
7702
0
  if (stream == NULL) {
7703
0
    return -1;
7704
0
  }
7705
0
  return stream->recv_window_size < 0 ? 0 : stream->recv_window_size;
7706
0
}
7707
7708
int32_t
7709
nghttp2_session_get_stream_effective_local_window_size(nghttp2_session *session,
7710
0
                                                       int32_t stream_id) {
7711
0
  nghttp2_stream *stream;
7712
0
  stream = nghttp2_session_get_stream(session, stream_id);
7713
0
  if (stream == NULL) {
7714
0
    return -1;
7715
0
  }
7716
0
  return stream->local_window_size;
7717
0
}
7718
7719
int32_t nghttp2_session_get_stream_local_window_size(nghttp2_session *session,
7720
0
                                                     int32_t stream_id) {
7721
0
  nghttp2_stream *stream;
7722
0
  int32_t size;
7723
0
  stream = nghttp2_session_get_stream(session, stream_id);
7724
0
  if (stream == NULL) {
7725
0
    return -1;
7726
0
  }
7727
7728
0
  size = stream->local_window_size - stream->recv_window_size;
7729
7730
  /* size could be negative if local endpoint reduced
7731
     SETTINGS_INITIAL_WINDOW_SIZE */
7732
0
  if (size < 0) {
7733
0
    return 0;
7734
0
  }
7735
7736
0
  return size;
7737
0
}
7738
7739
int32_t
7740
0
nghttp2_session_get_effective_recv_data_length(nghttp2_session *session) {
7741
0
  return session->recv_window_size < 0 ? 0 : session->recv_window_size;
7742
0
}
7743
7744
int32_t
7745
0
nghttp2_session_get_effective_local_window_size(nghttp2_session *session) {
7746
0
  return session->local_window_size;
7747
0
}
7748
7749
0
int32_t nghttp2_session_get_local_window_size(nghttp2_session *session) {
7750
0
  return session->local_window_size - session->recv_window_size;
7751
0
}
7752
7753
int32_t nghttp2_session_get_stream_remote_window_size(nghttp2_session *session,
7754
0
                                                      int32_t stream_id) {
7755
0
  nghttp2_stream *stream;
7756
7757
0
  stream = nghttp2_session_get_stream(session, stream_id);
7758
0
  if (stream == NULL) {
7759
0
    return -1;
7760
0
  }
7761
7762
  /* stream->remote_window_size can be negative when
7763
     SETTINGS_INITIAL_WINDOW_SIZE is changed. */
7764
0
  return nghttp2_max_int32(0, stream->remote_window_size);
7765
0
}
7766
7767
0
int32_t nghttp2_session_get_remote_window_size(nghttp2_session *session) {
7768
0
  return session->remote_window_size;
7769
0
}
7770
7771
uint32_t nghttp2_session_get_remote_settings(nghttp2_session *session,
7772
0
                                             nghttp2_settings_id id) {
7773
0
  switch (id) {
7774
0
  case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
7775
0
    return session->remote_settings.header_table_size;
7776
0
  case NGHTTP2_SETTINGS_ENABLE_PUSH:
7777
0
    return session->remote_settings.enable_push;
7778
0
  case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
7779
0
    return session->remote_settings.max_concurrent_streams;
7780
0
  case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
7781
0
    return session->remote_settings.initial_window_size;
7782
0
  case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
7783
0
    return session->remote_settings.max_frame_size;
7784
0
  case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
7785
0
    return session->remote_settings.max_header_list_size;
7786
0
  case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
7787
0
    return session->remote_settings.enable_connect_protocol;
7788
0
  case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
7789
0
    return session->remote_settings.no_rfc7540_priorities;
7790
0
  }
7791
7792
0
  assert(0);
7793
0
  abort(); /* if NDEBUG is set */
7794
0
}
7795
7796
uint32_t nghttp2_session_get_local_settings(nghttp2_session *session,
7797
0
                                            nghttp2_settings_id id) {
7798
0
  switch (id) {
7799
0
  case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
7800
0
    return session->local_settings.header_table_size;
7801
0
  case NGHTTP2_SETTINGS_ENABLE_PUSH:
7802
0
    return session->local_settings.enable_push;
7803
0
  case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
7804
0
    return session->local_settings.max_concurrent_streams;
7805
0
  case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
7806
0
    return session->local_settings.initial_window_size;
7807
0
  case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
7808
0
    return session->local_settings.max_frame_size;
7809
0
  case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
7810
0
    return session->local_settings.max_header_list_size;
7811
0
  case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
7812
0
    return session->local_settings.enable_connect_protocol;
7813
0
  case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
7814
0
    return session->local_settings.no_rfc7540_priorities;
7815
0
  }
7816
7817
0
  assert(0);
7818
0
  abort(); /* if NDEBUG is set */
7819
0
}
7820
7821
static int nghttp2_session_upgrade_internal(nghttp2_session *session,
7822
                                            const uint8_t *settings_payload,
7823
                                            size_t settings_payloadlen,
7824
0
                                            void *stream_user_data) {
7825
0
  nghttp2_stream *stream;
7826
0
  nghttp2_frame frame;
7827
0
  nghttp2_settings_entry *iv;
7828
0
  size_t niv;
7829
0
  int rv;
7830
0
  nghttp2_mem *mem;
7831
7832
0
  mem = &session->mem;
7833
7834
0
  if ((!session->server && session->next_stream_id != 1) ||
7835
0
      (session->server && session->last_recv_stream_id >= 1)) {
7836
0
    return NGHTTP2_ERR_PROTO;
7837
0
  }
7838
0
  if (settings_payloadlen % NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
7839
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
7840
0
  }
7841
  /* SETTINGS frame contains too many settings */
7842
0
  if (settings_payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH >
7843
0
      session->max_settings) {
7844
0
    return NGHTTP2_ERR_TOO_MANY_SETTINGS;
7845
0
  }
7846
0
  rv = nghttp2_frame_unpack_settings_payload2(&iv, &niv, settings_payload,
7847
0
                                              settings_payloadlen, mem);
7848
0
  if (rv != 0) {
7849
0
    return rv;
7850
0
  }
7851
7852
0
  if (session->server) {
7853
0
    nghttp2_frame_hd_init(&frame.hd, settings_payloadlen, NGHTTP2_SETTINGS,
7854
0
                          NGHTTP2_FLAG_NONE, 0);
7855
0
    frame.settings.iv = iv;
7856
0
    frame.settings.niv = niv;
7857
0
    rv = nghttp2_session_on_settings_received(session, &frame, 1 /* No ACK */);
7858
0
  } else {
7859
0
    rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
7860
0
  }
7861
0
  nghttp2_mem_free(mem, iv);
7862
0
  if (rv != 0) {
7863
0
    return rv;
7864
0
  }
7865
7866
0
  stream = nghttp2_session_open_stream(
7867
0
    session, 1, NGHTTP2_STREAM_FLAG_NONE, NGHTTP2_STREAM_OPENING,
7868
0
    session->server ? NULL : stream_user_data);
7869
0
  if (stream == NULL) {
7870
0
    return NGHTTP2_ERR_NOMEM;
7871
0
  }
7872
7873
0
  if (session->server) {
7874
0
    nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
7875
0
    session->last_recv_stream_id = 1;
7876
0
    session->last_proc_stream_id = 1;
7877
0
  } else {
7878
0
    nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
7879
0
    session->last_sent_stream_id = 1;
7880
0
    session->next_stream_id += 2;
7881
0
  }
7882
0
  return 0;
7883
0
}
7884
7885
int nghttp2_session_upgrade(nghttp2_session *session,
7886
                            const uint8_t *settings_payload,
7887
                            size_t settings_payloadlen,
7888
0
                            void *stream_user_data) {
7889
0
  int rv;
7890
0
  nghttp2_stream *stream;
7891
7892
0
  rv = nghttp2_session_upgrade_internal(session, settings_payload,
7893
0
                                        settings_payloadlen, stream_user_data);
7894
0
  if (rv != 0) {
7895
0
    return rv;
7896
0
  }
7897
7898
0
  stream = nghttp2_session_get_stream(session, 1);
7899
0
  assert(stream);
7900
7901
  /* We have no information about request header fields when Upgrade
7902
     was happened.  So we don't know the request method here.  If
7903
     request method is HEAD, we have a trouble because we may have
7904
     nonzero content-length header field in response headers, and we
7905
     will going to check it against the actual DATA frames, but we may
7906
     get mismatch because HEAD response body must be empty.  Because
7907
     of this reason, nghttp2_session_upgrade() was deprecated in favor
7908
     of nghttp2_session_upgrade2(), which has |head_request| parameter
7909
     to indicate that request method is HEAD or not. */
7910
0
  stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_UPGRADE_WORKAROUND;
7911
0
  return 0;
7912
0
}
7913
7914
int nghttp2_session_upgrade2(nghttp2_session *session,
7915
                             const uint8_t *settings_payload,
7916
                             size_t settings_payloadlen, int head_request,
7917
0
                             void *stream_user_data) {
7918
0
  int rv;
7919
0
  nghttp2_stream *stream;
7920
7921
0
  rv = nghttp2_session_upgrade_internal(session, settings_payload,
7922
0
                                        settings_payloadlen, stream_user_data);
7923
0
  if (rv != 0) {
7924
0
    return rv;
7925
0
  }
7926
7927
0
  stream = nghttp2_session_get_stream(session, 1);
7928
0
  assert(stream);
7929
7930
0
  if (head_request) {
7931
0
    stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_HEAD;
7932
0
  }
7933
7934
0
  return 0;
7935
0
}
7936
7937
int nghttp2_session_get_stream_local_close(nghttp2_session *session,
7938
0
                                           int32_t stream_id) {
7939
0
  nghttp2_stream *stream;
7940
7941
0
  stream = nghttp2_session_get_stream(session, stream_id);
7942
7943
0
  if (!stream) {
7944
0
    return -1;
7945
0
  }
7946
7947
0
  return (stream->shut_flags & NGHTTP2_SHUT_WR) != 0;
7948
0
}
7949
7950
int nghttp2_session_get_stream_remote_close(nghttp2_session *session,
7951
0
                                            int32_t stream_id) {
7952
0
  nghttp2_stream *stream;
7953
7954
0
  stream = nghttp2_session_get_stream(session, stream_id);
7955
7956
0
  if (!stream) {
7957
0
    return -1;
7958
0
  }
7959
7960
0
  return (stream->shut_flags & NGHTTP2_SHUT_RD) != 0;
7961
0
}
7962
7963
int nghttp2_session_consume(nghttp2_session *session, int32_t stream_id,
7964
0
                            size_t size) {
7965
0
  int rv;
7966
0
  nghttp2_stream *stream;
7967
7968
0
  if (stream_id == 0) {
7969
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
7970
0
  }
7971
7972
0
  if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE)) {
7973
0
    return NGHTTP2_ERR_INVALID_STATE;
7974
0
  }
7975
7976
0
  rv = session_update_connection_consumed_size(session, size);
7977
7978
0
  if (nghttp2_is_fatal(rv)) {
7979
0
    return rv;
7980
0
  }
7981
7982
0
  stream = nghttp2_session_get_stream(session, stream_id);
7983
7984
0
  if (!stream) {
7985
0
    return 0;
7986
0
  }
7987
7988
0
  rv = session_update_stream_consumed_size(session, stream, size);
7989
7990
0
  if (nghttp2_is_fatal(rv)) {
7991
0
    return rv;
7992
0
  }
7993
7994
0
  return 0;
7995
0
}
7996
7997
0
int nghttp2_session_consume_connection(nghttp2_session *session, size_t size) {
7998
0
  int rv;
7999
8000
0
  if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE)) {
8001
0
    return NGHTTP2_ERR_INVALID_STATE;
8002
0
  }
8003
8004
0
  rv = session_update_connection_consumed_size(session, size);
8005
8006
0
  if (nghttp2_is_fatal(rv)) {
8007
0
    return rv;
8008
0
  }
8009
8010
0
  return 0;
8011
0
}
8012
8013
int nghttp2_session_consume_stream(nghttp2_session *session, int32_t stream_id,
8014
0
                                   size_t size) {
8015
0
  int rv;
8016
0
  nghttp2_stream *stream;
8017
8018
0
  if (stream_id == 0) {
8019
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
8020
0
  }
8021
8022
0
  if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE)) {
8023
0
    return NGHTTP2_ERR_INVALID_STATE;
8024
0
  }
8025
8026
0
  stream = nghttp2_session_get_stream(session, stream_id);
8027
8028
0
  if (!stream) {
8029
0
    return 0;
8030
0
  }
8031
8032
0
  rv = session_update_stream_consumed_size(session, stream, size);
8033
8034
0
  if (nghttp2_is_fatal(rv)) {
8035
0
    return rv;
8036
0
  }
8037
8038
0
  return 0;
8039
0
}
8040
8041
int nghttp2_session_set_next_stream_id(nghttp2_session *session,
8042
0
                                       int32_t next_stream_id) {
8043
0
  if (next_stream_id <= 0 ||
8044
0
      session->next_stream_id > (uint32_t)next_stream_id) {
8045
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
8046
0
  }
8047
8048
0
  if (session->server) {
8049
0
    if (next_stream_id % 2) {
8050
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
8051
0
    }
8052
0
  } else if (next_stream_id % 2 == 0) {
8053
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
8054
0
  }
8055
8056
0
  session->next_stream_id = (uint32_t)next_stream_id;
8057
0
  return 0;
8058
0
}
8059
8060
0
uint32_t nghttp2_session_get_next_stream_id(nghttp2_session *session) {
8061
0
  return session->next_stream_id;
8062
0
}
8063
8064
0
int32_t nghttp2_session_get_last_proc_stream_id(nghttp2_session *session) {
8065
0
  return session->last_proc_stream_id;
8066
0
}
8067
8068
nghttp2_stream *nghttp2_session_find_stream(nghttp2_session *session,
8069
0
                                            int32_t stream_id) {
8070
0
  if (stream_id == 0) {
8071
0
    return &nghttp2_stream_root;
8072
0
  }
8073
8074
0
  return nghttp2_session_get_stream_raw(session, stream_id);
8075
0
}
8076
8077
0
nghttp2_stream *nghttp2_session_get_root_stream(nghttp2_session *session) {
8078
0
  (void)session;
8079
8080
0
  return &nghttp2_stream_root;
8081
0
}
8082
8083
0
int nghttp2_session_check_server_session(nghttp2_session *session) {
8084
0
  return session->server;
8085
0
}
8086
8087
int nghttp2_session_change_stream_priority(
8088
  nghttp2_session *session, int32_t stream_id,
8089
0
  const nghttp2_priority_spec *pri_spec) {
8090
0
  (void)session;
8091
0
  (void)stream_id;
8092
0
  (void)pri_spec;
8093
8094
0
  return 0;
8095
0
}
8096
8097
int nghttp2_session_create_idle_stream(nghttp2_session *session,
8098
                                       int32_t stream_id,
8099
0
                                       const nghttp2_priority_spec *pri_spec) {
8100
0
  (void)session;
8101
0
  (void)stream_id;
8102
0
  (void)pri_spec;
8103
8104
0
  return 0;
8105
0
}
8106
8107
size_t
8108
0
nghttp2_session_get_hd_inflate_dynamic_table_size(nghttp2_session *session) {
8109
0
  return nghttp2_hd_inflate_get_dynamic_table_size(&session->hd_inflater);
8110
0
}
8111
8112
size_t
8113
0
nghttp2_session_get_hd_deflate_dynamic_table_size(nghttp2_session *session) {
8114
0
  return nghttp2_hd_deflate_get_dynamic_table_size(&session->hd_deflater);
8115
0
}
8116
8117
0
void nghttp2_session_set_user_data(nghttp2_session *session, void *user_data) {
8118
0
  session->user_data = user_data;
8119
0
}
8120
8121
int nghttp2_session_change_extpri_stream_priority(
8122
  nghttp2_session *session, int32_t stream_id, const nghttp2_extpri *extpri_in,
8123
0
  int ignore_client_signal) {
8124
0
  nghttp2_stream *stream;
8125
0
  nghttp2_extpri extpri = *extpri_in;
8126
8127
0
  if (!session->server) {
8128
0
    return NGHTTP2_ERR_INVALID_STATE;
8129
0
  }
8130
8131
0
  if (session->pending_no_rfc7540_priorities != 1) {
8132
0
    return 0;
8133
0
  }
8134
8135
0
  if (stream_id == 0) {
8136
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
8137
0
  }
8138
8139
0
  stream = nghttp2_session_get_stream_raw(session, stream_id);
8140
0
  if (!stream) {
8141
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
8142
0
  }
8143
8144
0
  if (extpri.urgency > NGHTTP2_EXTPRI_URGENCY_LOW) {
8145
0
    extpri.urgency = NGHTTP2_EXTPRI_URGENCY_LOW;
8146
0
  }
8147
8148
0
  if (ignore_client_signal) {
8149
0
    stream->flags |= NGHTTP2_STREAM_FLAG_IGNORE_CLIENT_PRIORITIES;
8150
0
  }
8151
8152
0
  return session_update_stream_priority(session, stream,
8153
0
                                        nghttp2_extpri_to_uint8(&extpri));
8154
0
}
8155
8156
int nghttp2_session_get_extpri_stream_priority(nghttp2_session *session,
8157
                                               nghttp2_extpri *extpri,
8158
0
                                               int32_t stream_id) {
8159
0
  nghttp2_stream *stream;
8160
8161
0
  if (!session->server) {
8162
0
    return NGHTTP2_ERR_INVALID_STATE;
8163
0
  }
8164
8165
0
  if (session->pending_no_rfc7540_priorities != 1) {
8166
0
    return 0;
8167
0
  }
8168
8169
0
  if (stream_id == 0) {
8170
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
8171
0
  }
8172
8173
0
  stream = nghttp2_session_get_stream_raw(session, stream_id);
8174
0
  if (!stream) {
8175
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
8176
0
  }
8177
8178
0
  nghttp2_extpri_from_uint8(extpri, stream->extpri);
8179
8180
0
  return 0;
8181
0
}