Coverage Report

Created: 2024-09-08 06:32

/src/nghttp2/lib/nghttp2_submit.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * nghttp2 - HTTP/2 C Library
3
 *
4
 * Copyright (c) 2012, 2013 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_submit.h"
26
27
#include <string.h>
28
#include <assert.h>
29
30
#include "nghttp2_session.h"
31
#include "nghttp2_frame.h"
32
#include "nghttp2_helper.h"
33
#include "nghttp2_priority_spec.h"
34
35
/*
36
 * Detects the dependency error, that is stream attempted to depend on
37
 * itself.  If |stream_id| is -1, we use session->next_stream_id as
38
 * stream ID.
39
 *
40
 * This function returns 0 if it succeeds, or one of the following
41
 * error codes:
42
 *
43
 * NGHTTP2_ERR_INVALID_ARGUMENT
44
 *   Stream attempted to depend on itself.
45
 */
46
static int detect_self_dependency(nghttp2_session *session, int32_t stream_id,
47
0
                                  const nghttp2_priority_spec *pri_spec) {
48
0
  assert(pri_spec);
49
50
0
  if (stream_id == -1) {
51
0
    if ((int32_t)session->next_stream_id == pri_spec->stream_id) {
52
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
53
0
    }
54
0
    return 0;
55
0
  }
56
57
0
  if (stream_id == pri_spec->stream_id) {
58
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
59
0
  }
60
61
0
  return 0;
62
0
}
63
64
/* This function takes ownership of |nva_copy|. Regardless of the
65
   return value, the caller must not free |nva_copy| after this
66
   function returns. */
67
static int32_t submit_headers_shared(nghttp2_session *session, uint8_t flags,
68
                                     int32_t stream_id,
69
                                     const nghttp2_priority_spec *pri_spec,
70
                                     nghttp2_nv *nva_copy, size_t nvlen,
71
                                     const nghttp2_data_provider_wrap *dpw,
72
8.72k
                                     void *stream_user_data) {
73
8.72k
  int rv;
74
8.72k
  uint8_t flags_copy;
75
8.72k
  nghttp2_outbound_item *item = NULL;
76
8.72k
  nghttp2_frame *frame = NULL;
77
8.72k
  nghttp2_headers_category hcat;
78
8.72k
  nghttp2_mem *mem;
79
80
8.72k
  mem = &session->mem;
81
82
8.72k
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
83
8.72k
  if (item == NULL) {
84
0
    rv = NGHTTP2_ERR_NOMEM;
85
0
    goto fail;
86
0
  }
87
88
8.72k
  nghttp2_outbound_item_init(item);
89
90
8.72k
  if (dpw != NULL && dpw->data_prd.read_callback != NULL) {
91
1.00k
    item->aux_data.headers.dpw = *dpw;
92
1.00k
  }
93
94
8.72k
  item->aux_data.headers.stream_user_data = stream_user_data;
95
96
8.72k
  flags_copy =
97
8.72k
      (uint8_t)((flags & (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PRIORITY)) |
98
8.72k
                NGHTTP2_FLAG_END_HEADERS);
99
100
8.72k
  if (stream_id == -1) {
101
8.72k
    if (session->next_stream_id > INT32_MAX) {
102
0
      rv = NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
103
0
      goto fail;
104
0
    }
105
106
8.72k
    stream_id = (int32_t)session->next_stream_id;
107
8.72k
    session->next_stream_id += 2;
108
109
8.72k
    hcat = NGHTTP2_HCAT_REQUEST;
110
8.72k
  } else {
111
    /* More specific categorization will be done later. */
112
0
    hcat = NGHTTP2_HCAT_HEADERS;
113
0
  }
114
115
8.72k
  frame = &item->frame;
116
117
8.72k
  nghttp2_frame_headers_init(&frame->headers, flags_copy, stream_id, hcat,
118
8.72k
                             pri_spec, nva_copy, nvlen);
119
120
8.72k
  rv = nghttp2_session_add_item(session, item);
121
122
8.72k
  if (rv != 0) {
123
0
    nghttp2_frame_headers_free(&frame->headers, mem);
124
0
    goto fail2;
125
0
  }
126
127
8.72k
  if (hcat == NGHTTP2_HCAT_REQUEST) {
128
8.72k
    return stream_id;
129
8.72k
  }
130
131
0
  return 0;
132
133
0
fail:
134
  /* nghttp2_frame_headers_init() takes ownership of nva_copy. */
135
0
  nghttp2_nv_array_del(nva_copy, mem);
136
0
fail2:
137
0
  nghttp2_mem_free(mem, item);
138
139
0
  return rv;
140
0
}
141
142
static int32_t submit_headers_shared_nva(nghttp2_session *session,
143
                                         uint8_t flags, int32_t stream_id,
144
                                         const nghttp2_priority_spec *pri_spec,
145
                                         const nghttp2_nv *nva, size_t nvlen,
146
                                         const nghttp2_data_provider_wrap *dpw,
147
8.72k
                                         void *stream_user_data) {
148
8.72k
  int rv;
149
8.72k
  nghttp2_nv *nva_copy;
150
8.72k
  nghttp2_priority_spec copy_pri_spec;
151
8.72k
  nghttp2_mem *mem;
152
153
8.72k
  mem = &session->mem;
154
155
8.72k
  if (pri_spec) {
156
0
    copy_pri_spec = *pri_spec;
157
0
    nghttp2_priority_spec_normalize_weight(&copy_pri_spec);
158
8.72k
  } else {
159
8.72k
    nghttp2_priority_spec_default_init(&copy_pri_spec);
160
8.72k
  }
161
162
8.72k
  rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
163
8.72k
  if (rv < 0) {
164
0
    return rv;
165
0
  }
166
167
8.72k
  return submit_headers_shared(session, flags, stream_id, &copy_pri_spec,
168
8.72k
                               nva_copy, nvlen, dpw, stream_user_data);
169
8.72k
}
170
171
int nghttp2_submit_trailer(nghttp2_session *session, int32_t stream_id,
172
0
                           const nghttp2_nv *nva, size_t nvlen) {
173
0
  if (stream_id <= 0) {
174
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
175
0
  }
176
177
0
  return (int)submit_headers_shared_nva(session, NGHTTP2_FLAG_END_STREAM,
178
0
                                        stream_id, NULL, nva, nvlen, NULL,
179
0
                                        NULL);
180
0
}
181
182
int32_t nghttp2_submit_headers(nghttp2_session *session, uint8_t flags,
183
                               int32_t stream_id,
184
                               const nghttp2_priority_spec *pri_spec,
185
                               const nghttp2_nv *nva, size_t nvlen,
186
0
                               void *stream_user_data) {
187
0
  int rv;
188
189
0
  if (stream_id == -1) {
190
0
    if (session->server) {
191
0
      return NGHTTP2_ERR_PROTO;
192
0
    }
193
0
  } else if (stream_id <= 0) {
194
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
195
0
  }
196
197
0
  flags &= NGHTTP2_FLAG_END_STREAM;
198
199
0
  if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec) &&
200
0
      session->remote_settings.no_rfc7540_priorities != 1) {
201
0
    rv = detect_self_dependency(session, stream_id, pri_spec);
202
0
    if (rv != 0) {
203
0
      return rv;
204
0
    }
205
206
0
    flags |= NGHTTP2_FLAG_PRIORITY;
207
0
  } else {
208
0
    pri_spec = NULL;
209
0
  }
210
211
0
  return submit_headers_shared_nva(session, flags, stream_id, pri_spec, nva,
212
0
                                   nvlen, NULL, stream_user_data);
213
0
}
214
215
int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
216
0
                        const uint8_t *opaque_data) {
217
0
  flags &= NGHTTP2_FLAG_ACK;
218
0
  return nghttp2_session_add_ping(session, flags, opaque_data);
219
0
}
220
221
int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
222
                            int32_t stream_id,
223
0
                            const nghttp2_priority_spec *pri_spec) {
224
0
  int rv;
225
0
  nghttp2_outbound_item *item;
226
0
  nghttp2_frame *frame;
227
0
  nghttp2_priority_spec copy_pri_spec;
228
0
  nghttp2_mem *mem;
229
0
  (void)flags;
230
231
0
  mem = &session->mem;
232
233
0
  if (session->remote_settings.no_rfc7540_priorities == 1) {
234
0
    return 0;
235
0
  }
236
237
0
  if (stream_id == 0 || pri_spec == NULL) {
238
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
239
0
  }
240
241
0
  if (stream_id == pri_spec->stream_id) {
242
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
243
0
  }
244
245
0
  copy_pri_spec = *pri_spec;
246
247
0
  nghttp2_priority_spec_normalize_weight(&copy_pri_spec);
248
249
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
250
251
0
  if (item == NULL) {
252
0
    return NGHTTP2_ERR_NOMEM;
253
0
  }
254
255
0
  nghttp2_outbound_item_init(item);
256
257
0
  frame = &item->frame;
258
259
0
  nghttp2_frame_priority_init(&frame->priority, stream_id, &copy_pri_spec);
260
261
0
  rv = nghttp2_session_add_item(session, item);
262
263
0
  if (rv != 0) {
264
0
    nghttp2_frame_priority_free(&frame->priority);
265
0
    nghttp2_mem_free(mem, item);
266
267
0
    return rv;
268
0
  }
269
270
0
  return 0;
271
0
}
272
273
int nghttp2_submit_rst_stream(nghttp2_session *session, uint8_t flags,
274
6.73k
                              int32_t stream_id, uint32_t error_code) {
275
6.73k
  (void)flags;
276
277
6.73k
  if (stream_id == 0) {
278
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
279
0
  }
280
281
6.73k
  return nghttp2_session_add_rst_stream(session, stream_id, error_code);
282
6.73k
}
283
284
int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags,
285
                          int32_t last_stream_id, uint32_t error_code,
286
1.10k
                          const uint8_t *opaque_data, size_t opaque_data_len) {
287
1.10k
  (void)flags;
288
289
1.10k
  if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
290
478
    return 0;
291
478
  }
292
623
  return nghttp2_session_add_goaway(session, last_stream_id, error_code,
293
623
                                    opaque_data, opaque_data_len,
294
623
                                    NGHTTP2_GOAWAY_AUX_NONE);
295
1.10k
}
296
297
0
int nghttp2_submit_shutdown_notice(nghttp2_session *session) {
298
0
  if (!session->server) {
299
0
    return NGHTTP2_ERR_INVALID_STATE;
300
0
  }
301
0
  if (session->goaway_flags) {
302
0
    return 0;
303
0
  }
304
0
  return nghttp2_session_add_goaway(session, (1u << 31) - 1, NGHTTP2_NO_ERROR,
305
0
                                    NULL, 0,
306
0
                                    NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE);
307
0
}
308
309
int nghttp2_submit_settings(nghttp2_session *session, uint8_t flags,
310
8.35k
                            const nghttp2_settings_entry *iv, size_t niv) {
311
8.35k
  (void)flags;
312
8.35k
  return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
313
8.35k
}
314
315
int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags,
316
                                    int32_t stream_id, const nghttp2_nv *nva,
317
                                    size_t nvlen,
318
0
                                    void *promised_stream_user_data) {
319
0
  nghttp2_outbound_item *item;
320
0
  nghttp2_frame *frame;
321
0
  nghttp2_nv *nva_copy;
322
0
  uint8_t flags_copy;
323
0
  int32_t promised_stream_id;
324
0
  int rv;
325
0
  nghttp2_mem *mem;
326
0
  (void)flags;
327
328
0
  mem = &session->mem;
329
330
0
  if (stream_id <= 0 || nghttp2_session_is_my_stream_id(session, stream_id)) {
331
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
332
0
  }
333
334
0
  if (!session->server) {
335
0
    return NGHTTP2_ERR_PROTO;
336
0
  }
337
338
  /* All 32bit signed stream IDs are spent. */
339
0
  if (session->next_stream_id > INT32_MAX) {
340
0
    return NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
341
0
  }
342
343
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
344
0
  if (item == NULL) {
345
0
    return NGHTTP2_ERR_NOMEM;
346
0
  }
347
348
0
  nghttp2_outbound_item_init(item);
349
350
0
  item->aux_data.headers.stream_user_data = promised_stream_user_data;
351
352
0
  frame = &item->frame;
353
354
0
  rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
355
0
  if (rv < 0) {
356
0
    nghttp2_mem_free(mem, item);
357
0
    return rv;
358
0
  }
359
360
0
  flags_copy = NGHTTP2_FLAG_END_HEADERS;
361
362
0
  promised_stream_id = (int32_t)session->next_stream_id;
363
0
  session->next_stream_id += 2;
364
365
0
  nghttp2_frame_push_promise_init(&frame->push_promise, flags_copy, stream_id,
366
0
                                  promised_stream_id, nva_copy, nvlen);
367
368
0
  rv = nghttp2_session_add_item(session, item);
369
370
0
  if (rv != 0) {
371
0
    nghttp2_frame_push_promise_free(&frame->push_promise, mem);
372
0
    nghttp2_mem_free(mem, item);
373
374
0
    return rv;
375
0
  }
376
377
0
  return promised_stream_id;
378
0
}
379
380
int nghttp2_submit_window_update(nghttp2_session *session, uint8_t flags,
381
                                 int32_t stream_id,
382
2.92k
                                 int32_t window_size_increment) {
383
2.92k
  int rv;
384
2.92k
  nghttp2_stream *stream = 0;
385
2.92k
  (void)flags;
386
387
2.92k
  if (window_size_increment == 0) {
388
0
    return 0;
389
0
  }
390
2.92k
  if (stream_id == 0) {
391
0
    rv = nghttp2_adjust_local_window_size(
392
0
        &session->local_window_size, &session->recv_window_size,
393
0
        &session->recv_reduction, &window_size_increment);
394
0
    if (rv != 0) {
395
0
      return rv;
396
0
    }
397
2.92k
  } else {
398
2.92k
    stream = nghttp2_session_get_stream(session, stream_id);
399
2.92k
    if (!stream) {
400
0
      return 0;
401
0
    }
402
403
2.92k
    rv = nghttp2_adjust_local_window_size(
404
2.92k
        &stream->local_window_size, &stream->recv_window_size,
405
2.92k
        &stream->recv_reduction, &window_size_increment);
406
2.92k
    if (rv != 0) {
407
0
      return rv;
408
0
    }
409
2.92k
  }
410
411
2.92k
  if (window_size_increment > 0) {
412
2.92k
    if (stream_id == 0) {
413
0
      session->consumed_size =
414
0
          nghttp2_max_int32(0, session->consumed_size - window_size_increment);
415
2.92k
    } else {
416
2.92k
      stream->consumed_size =
417
2.92k
          nghttp2_max_int32(0, stream->consumed_size - window_size_increment);
418
2.92k
    }
419
420
2.92k
    return nghttp2_session_add_window_update(session, 0, stream_id,
421
2.92k
                                             window_size_increment);
422
2.92k
  }
423
0
  return 0;
424
2.92k
}
425
426
int nghttp2_session_set_local_window_size(nghttp2_session *session,
427
                                          uint8_t flags, int32_t stream_id,
428
8.35k
                                          int32_t window_size) {
429
8.35k
  int32_t window_size_increment;
430
8.35k
  nghttp2_stream *stream;
431
8.35k
  int rv;
432
8.35k
  (void)flags;
433
434
8.35k
  if (window_size < 0) {
435
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
436
0
  }
437
438
8.35k
  if (stream_id == 0) {
439
8.35k
    window_size_increment = window_size - session->local_window_size;
440
441
8.35k
    if (window_size_increment == 0) {
442
0
      return 0;
443
0
    }
444
445
8.35k
    if (window_size_increment < 0) {
446
0
      return nghttp2_adjust_local_window_size(
447
0
          &session->local_window_size, &session->recv_window_size,
448
0
          &session->recv_reduction, &window_size_increment);
449
0
    }
450
451
8.35k
    rv = nghttp2_increase_local_window_size(
452
8.35k
        &session->local_window_size, &session->recv_window_size,
453
8.35k
        &session->recv_reduction, &window_size_increment);
454
455
8.35k
    if (rv != 0) {
456
0
      return rv;
457
0
    }
458
459
8.35k
    if (window_size_increment > 0) {
460
8.35k
      return nghttp2_session_add_window_update(session, 0, stream_id,
461
8.35k
                                               window_size_increment);
462
8.35k
    }
463
464
0
    return nghttp2_session_update_recv_connection_window_size(session, 0);
465
8.35k
  } else {
466
0
    stream = nghttp2_session_get_stream(session, stream_id);
467
468
0
    if (stream == NULL) {
469
0
      return 0;
470
0
    }
471
472
0
    window_size_increment = window_size - stream->local_window_size;
473
474
0
    if (window_size_increment == 0) {
475
0
      return 0;
476
0
    }
477
478
0
    if (window_size_increment < 0) {
479
0
      return nghttp2_adjust_local_window_size(
480
0
          &stream->local_window_size, &stream->recv_window_size,
481
0
          &stream->recv_reduction, &window_size_increment);
482
0
    }
483
484
0
    rv = nghttp2_increase_local_window_size(
485
0
        &stream->local_window_size, &stream->recv_window_size,
486
0
        &stream->recv_reduction, &window_size_increment);
487
488
0
    if (rv != 0) {
489
0
      return rv;
490
0
    }
491
492
0
    if (window_size_increment > 0) {
493
0
      return nghttp2_session_add_window_update(session, 0, stream_id,
494
0
                                               window_size_increment);
495
0
    }
496
497
0
    return nghttp2_session_update_recv_stream_window_size(session, stream, 0,
498
0
                                                          1);
499
0
  }
500
8.35k
}
501
502
int nghttp2_submit_altsvc(nghttp2_session *session, uint8_t flags,
503
                          int32_t stream_id, const uint8_t *origin,
504
                          size_t origin_len, const uint8_t *field_value,
505
0
                          size_t field_value_len) {
506
0
  nghttp2_mem *mem;
507
0
  uint8_t *buf, *p;
508
0
  uint8_t *origin_copy;
509
0
  uint8_t *field_value_copy;
510
0
  nghttp2_outbound_item *item;
511
0
  nghttp2_frame *frame;
512
0
  nghttp2_ext_altsvc *altsvc;
513
0
  int rv;
514
0
  (void)flags;
515
516
0
  mem = &session->mem;
517
518
0
  if (!session->server) {
519
0
    return NGHTTP2_ERR_INVALID_STATE;
520
0
  }
521
522
0
  if (2 + origin_len + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
523
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
524
0
  }
525
526
0
  if (stream_id == 0) {
527
0
    if (origin_len == 0) {
528
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
529
0
    }
530
0
  } else if (origin_len != 0) {
531
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
532
0
  }
533
534
0
  buf = nghttp2_mem_malloc(mem, origin_len + field_value_len + 2);
535
0
  if (buf == NULL) {
536
0
    return NGHTTP2_ERR_NOMEM;
537
0
  }
538
539
0
  p = buf;
540
541
0
  origin_copy = p;
542
0
  if (origin_len) {
543
0
    p = nghttp2_cpymem(p, origin, origin_len);
544
0
  }
545
0
  *p++ = '\0';
546
547
0
  field_value_copy = p;
548
0
  if (field_value_len) {
549
0
    p = nghttp2_cpymem(p, field_value, field_value_len);
550
0
  }
551
0
  *p++ = '\0';
552
553
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
554
0
  if (item == NULL) {
555
0
    rv = NGHTTP2_ERR_NOMEM;
556
0
    goto fail_item_malloc;
557
0
  }
558
559
0
  nghttp2_outbound_item_init(item);
560
561
0
  item->aux_data.ext.builtin = 1;
562
563
0
  altsvc = &item->ext_frame_payload.altsvc;
564
565
0
  frame = &item->frame;
566
0
  frame->ext.payload = altsvc;
567
568
0
  nghttp2_frame_altsvc_init(&frame->ext, stream_id, origin_copy, origin_len,
569
0
                            field_value_copy, field_value_len);
570
571
0
  rv = nghttp2_session_add_item(session, item);
572
0
  if (rv != 0) {
573
0
    nghttp2_frame_altsvc_free(&frame->ext, mem);
574
0
    nghttp2_mem_free(mem, item);
575
576
0
    return rv;
577
0
  }
578
579
0
  return 0;
580
581
0
fail_item_malloc:
582
0
  free(buf);
583
584
0
  return rv;
585
0
}
586
587
int nghttp2_submit_origin(nghttp2_session *session, uint8_t flags,
588
0
                          const nghttp2_origin_entry *ov, size_t nov) {
589
0
  nghttp2_mem *mem;
590
0
  uint8_t *p;
591
0
  nghttp2_outbound_item *item;
592
0
  nghttp2_frame *frame;
593
0
  nghttp2_ext_origin *origin;
594
0
  nghttp2_origin_entry *ov_copy;
595
0
  size_t len = 0;
596
0
  size_t i;
597
0
  int rv;
598
0
  (void)flags;
599
600
0
  mem = &session->mem;
601
602
0
  if (!session->server) {
603
0
    return NGHTTP2_ERR_INVALID_STATE;
604
0
  }
605
606
0
  if (nov) {
607
0
    for (i = 0; i < nov; ++i) {
608
0
      len += ov[i].origin_len;
609
0
    }
610
611
0
    if (2 * nov + len > NGHTTP2_MAX_PAYLOADLEN) {
612
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
613
0
    }
614
615
    /* The last nov is added for terminal NULL character. */
616
0
    ov_copy =
617
0
        nghttp2_mem_malloc(mem, nov * sizeof(nghttp2_origin_entry) + len + nov);
618
0
    if (ov_copy == NULL) {
619
0
      return NGHTTP2_ERR_NOMEM;
620
0
    }
621
622
0
    p = (uint8_t *)ov_copy + nov * sizeof(nghttp2_origin_entry);
623
624
0
    for (i = 0; i < nov; ++i) {
625
0
      ov_copy[i].origin = p;
626
0
      ov_copy[i].origin_len = ov[i].origin_len;
627
0
      p = nghttp2_cpymem(p, ov[i].origin, ov[i].origin_len);
628
0
      *p++ = '\0';
629
0
    }
630
631
0
    assert((size_t)(p - (uint8_t *)ov_copy) ==
632
0
           nov * sizeof(nghttp2_origin_entry) + len + nov);
633
0
  } else {
634
0
    ov_copy = NULL;
635
0
  }
636
637
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
638
0
  if (item == NULL) {
639
0
    rv = NGHTTP2_ERR_NOMEM;
640
0
    goto fail_item_malloc;
641
0
  }
642
643
0
  nghttp2_outbound_item_init(item);
644
645
0
  item->aux_data.ext.builtin = 1;
646
647
0
  origin = &item->ext_frame_payload.origin;
648
649
0
  frame = &item->frame;
650
0
  frame->ext.payload = origin;
651
652
0
  nghttp2_frame_origin_init(&frame->ext, ov_copy, nov);
653
654
0
  rv = nghttp2_session_add_item(session, item);
655
0
  if (rv != 0) {
656
0
    nghttp2_frame_origin_free(&frame->ext, mem);
657
0
    nghttp2_mem_free(mem, item);
658
659
0
    return rv;
660
0
  }
661
662
0
  return 0;
663
664
0
fail_item_malloc:
665
0
  free(ov_copy);
666
667
0
  return rv;
668
0
}
669
670
int nghttp2_submit_priority_update(nghttp2_session *session, uint8_t flags,
671
                                   int32_t stream_id,
672
                                   const uint8_t *field_value,
673
0
                                   size_t field_value_len) {
674
0
  nghttp2_mem *mem;
675
0
  uint8_t *buf, *p;
676
0
  nghttp2_outbound_item *item;
677
0
  nghttp2_frame *frame;
678
0
  nghttp2_ext_priority_update *priority_update;
679
0
  int rv;
680
0
  (void)flags;
681
682
0
  mem = &session->mem;
683
684
0
  if (session->server) {
685
0
    return NGHTTP2_ERR_INVALID_STATE;
686
0
  }
687
688
0
  if (session->remote_settings.no_rfc7540_priorities == 0) {
689
0
    return 0;
690
0
  }
691
692
0
  if (stream_id == 0 || 4 + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
693
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
694
0
  }
695
696
0
  if (field_value_len) {
697
0
    buf = nghttp2_mem_malloc(mem, field_value_len + 1);
698
0
    if (buf == NULL) {
699
0
      return NGHTTP2_ERR_NOMEM;
700
0
    }
701
702
0
    p = nghttp2_cpymem(buf, field_value, field_value_len);
703
0
    *p = '\0';
704
0
  } else {
705
0
    buf = NULL;
706
0
  }
707
708
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
709
0
  if (item == NULL) {
710
0
    rv = NGHTTP2_ERR_NOMEM;
711
0
    goto fail_item_malloc;
712
0
  }
713
714
0
  nghttp2_outbound_item_init(item);
715
716
0
  item->aux_data.ext.builtin = 1;
717
718
0
  priority_update = &item->ext_frame_payload.priority_update;
719
720
0
  frame = &item->frame;
721
0
  frame->ext.payload = priority_update;
722
723
0
  nghttp2_frame_priority_update_init(&frame->ext, stream_id, buf,
724
0
                                     field_value_len);
725
726
0
  rv = nghttp2_session_add_item(session, item);
727
0
  if (rv != 0) {
728
0
    nghttp2_frame_priority_update_free(&frame->ext, mem);
729
0
    nghttp2_mem_free(mem, item);
730
731
0
    return rv;
732
0
  }
733
734
0
  return 0;
735
736
0
fail_item_malloc:
737
0
  free(buf);
738
739
0
  return rv;
740
0
}
741
742
static uint8_t set_request_flags(const nghttp2_priority_spec *pri_spec,
743
8.72k
                                 const nghttp2_data_provider_wrap *dpw) {
744
8.72k
  uint8_t flags = NGHTTP2_FLAG_NONE;
745
8.72k
  if (dpw == NULL || dpw->data_prd.read_callback == NULL) {
746
7.72k
    flags |= NGHTTP2_FLAG_END_STREAM;
747
7.72k
  }
748
749
8.72k
  if (pri_spec) {
750
0
    flags |= NGHTTP2_FLAG_PRIORITY;
751
0
  }
752
753
8.72k
  return flags;
754
8.72k
}
755
756
static int32_t submit_request_shared(nghttp2_session *session,
757
                                     const nghttp2_priority_spec *pri_spec,
758
                                     const nghttp2_nv *nva, size_t nvlen,
759
                                     const nghttp2_data_provider_wrap *dpw,
760
8.72k
                                     void *stream_user_data) {
761
8.72k
  uint8_t flags;
762
8.72k
  int rv;
763
764
8.72k
  if (session->server) {
765
0
    return NGHTTP2_ERR_PROTO;
766
0
  }
767
768
8.72k
  if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec) &&
769
8.72k
      session->remote_settings.no_rfc7540_priorities != 1) {
770
0
    rv = detect_self_dependency(session, -1, pri_spec);
771
0
    if (rv != 0) {
772
0
      return rv;
773
0
    }
774
8.72k
  } else {
775
8.72k
    pri_spec = NULL;
776
8.72k
  }
777
778
8.72k
  flags = set_request_flags(pri_spec, dpw);
779
780
8.72k
  return submit_headers_shared_nva(session, flags, -1, pri_spec, nva, nvlen,
781
8.72k
                                   dpw, stream_user_data);
782
8.72k
}
783
784
int32_t nghttp2_submit_request(nghttp2_session *session,
785
                               const nghttp2_priority_spec *pri_spec,
786
                               const nghttp2_nv *nva, size_t nvlen,
787
                               const nghttp2_data_provider *data_prd,
788
8.72k
                               void *stream_user_data) {
789
8.72k
  nghttp2_data_provider_wrap dpw;
790
791
8.72k
  return submit_request_shared(session, pri_spec, nva, nvlen,
792
8.72k
                               nghttp2_data_provider_wrap_v1(&dpw, data_prd),
793
8.72k
                               stream_user_data);
794
8.72k
}
795
796
int32_t nghttp2_submit_request2(nghttp2_session *session,
797
                                const nghttp2_priority_spec *pri_spec,
798
                                const nghttp2_nv *nva, size_t nvlen,
799
                                const nghttp2_data_provider2 *data_prd,
800
0
                                void *stream_user_data) {
801
0
  nghttp2_data_provider_wrap dpw;
802
803
0
  return submit_request_shared(session, pri_spec, nva, nvlen,
804
0
                               nghttp2_data_provider_wrap_v2(&dpw, data_prd),
805
0
                               stream_user_data);
806
0
}
807
808
0
static uint8_t set_response_flags(const nghttp2_data_provider_wrap *dpw) {
809
0
  uint8_t flags = NGHTTP2_FLAG_NONE;
810
0
  if (dpw == NULL || dpw->data_prd.read_callback == NULL) {
811
0
    flags |= NGHTTP2_FLAG_END_STREAM;
812
0
  }
813
0
  return flags;
814
0
}
815
816
static int submit_response_shared(nghttp2_session *session, int32_t stream_id,
817
                                  const nghttp2_nv *nva, size_t nvlen,
818
0
                                  const nghttp2_data_provider_wrap *dpw) {
819
0
  uint8_t flags;
820
821
0
  if (stream_id <= 0) {
822
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
823
0
  }
824
825
0
  if (!session->server) {
826
0
    return NGHTTP2_ERR_PROTO;
827
0
  }
828
829
0
  flags = set_response_flags(dpw);
830
0
  return submit_headers_shared_nva(session, flags, stream_id, NULL, nva, nvlen,
831
0
                                   dpw, NULL);
832
0
}
833
834
int nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
835
                            const nghttp2_nv *nva, size_t nvlen,
836
0
                            const nghttp2_data_provider *data_prd) {
837
0
  nghttp2_data_provider_wrap dpw;
838
839
0
  return submit_response_shared(session, stream_id, nva, nvlen,
840
0
                                nghttp2_data_provider_wrap_v1(&dpw, data_prd));
841
0
}
842
843
int nghttp2_submit_response2(nghttp2_session *session, int32_t stream_id,
844
                             const nghttp2_nv *nva, size_t nvlen,
845
0
                             const nghttp2_data_provider2 *data_prd) {
846
0
  nghttp2_data_provider_wrap dpw;
847
848
0
  return submit_response_shared(session, stream_id, nva, nvlen,
849
0
                                nghttp2_data_provider_wrap_v2(&dpw, data_prd));
850
0
}
851
852
int nghttp2_submit_data_shared(nghttp2_session *session, uint8_t flags,
853
                               int32_t stream_id,
854
933
                               const nghttp2_data_provider_wrap *dpw) {
855
933
  int rv;
856
933
  nghttp2_outbound_item *item;
857
933
  nghttp2_frame *frame;
858
933
  nghttp2_data_aux_data *aux_data;
859
933
  uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
860
933
  nghttp2_mem *mem;
861
862
933
  mem = &session->mem;
863
864
933
  if (stream_id == 0) {
865
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
866
0
  }
867
868
933
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
869
933
  if (item == NULL) {
870
0
    return NGHTTP2_ERR_NOMEM;
871
0
  }
872
873
933
  nghttp2_outbound_item_init(item);
874
875
933
  frame = &item->frame;
876
933
  aux_data = &item->aux_data.data;
877
933
  aux_data->dpw = *dpw;
878
933
  aux_data->eof = 0;
879
933
  aux_data->flags = nflags;
880
881
  /* flags are sent on transmission */
882
933
  nghttp2_frame_data_init(&frame->data, NGHTTP2_FLAG_NONE, stream_id);
883
884
933
  rv = nghttp2_session_add_item(session, item);
885
933
  if (rv != 0) {
886
0
    nghttp2_frame_data_free(&frame->data);
887
0
    nghttp2_mem_free(mem, item);
888
0
    return rv;
889
0
  }
890
933
  return 0;
891
933
}
892
893
int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
894
                        int32_t stream_id,
895
0
                        const nghttp2_data_provider *data_prd) {
896
0
  nghttp2_data_provider_wrap dpw;
897
898
0
  assert(data_prd);
899
900
0
  return nghttp2_submit_data_shared(
901
0
      session, flags, stream_id, nghttp2_data_provider_wrap_v1(&dpw, data_prd));
902
0
}
903
904
int nghttp2_submit_data2(nghttp2_session *session, uint8_t flags,
905
                         int32_t stream_id,
906
0
                         const nghttp2_data_provider2 *data_prd) {
907
0
  nghttp2_data_provider_wrap dpw;
908
909
0
  assert(data_prd);
910
911
0
  return nghttp2_submit_data_shared(
912
0
      session, flags, stream_id, nghttp2_data_provider_wrap_v2(&dpw, data_prd));
913
0
}
914
915
ssize_t nghttp2_pack_settings_payload(uint8_t *buf, size_t buflen,
916
                                      const nghttp2_settings_entry *iv,
917
67
                                      size_t niv) {
918
67
  return (ssize_t)nghttp2_pack_settings_payload2(buf, buflen, iv, niv);
919
67
}
920
921
nghttp2_ssize nghttp2_pack_settings_payload2(uint8_t *buf, size_t buflen,
922
                                             const nghttp2_settings_entry *iv,
923
67
                                             size_t niv) {
924
67
  if (!nghttp2_iv_check(iv, niv)) {
925
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
926
0
  }
927
928
67
  if (buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
929
0
    return NGHTTP2_ERR_INSUFF_BUFSIZE;
930
0
  }
931
932
67
  return (nghttp2_ssize)nghttp2_frame_pack_settings_payload(buf, iv, niv);
933
67
}
934
935
int nghttp2_submit_extension(nghttp2_session *session, uint8_t type,
936
0
                             uint8_t flags, int32_t stream_id, void *payload) {
937
0
  int rv;
938
0
  nghttp2_outbound_item *item;
939
0
  nghttp2_frame *frame;
940
0
  nghttp2_mem *mem;
941
942
0
  mem = &session->mem;
943
944
0
  if (type <= NGHTTP2_CONTINUATION) {
945
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
946
0
  }
947
948
0
  if (!session->callbacks.pack_extension_callback2 &&
949
0
      !session->callbacks.pack_extension_callback) {
950
0
    return NGHTTP2_ERR_INVALID_STATE;
951
0
  }
952
953
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
954
0
  if (item == NULL) {
955
0
    return NGHTTP2_ERR_NOMEM;
956
0
  }
957
958
0
  nghttp2_outbound_item_init(item);
959
960
0
  frame = &item->frame;
961
0
  nghttp2_frame_extension_init(&frame->ext, type, flags, stream_id, payload);
962
963
0
  rv = nghttp2_session_add_item(session, item);
964
0
  if (rv != 0) {
965
0
    nghttp2_frame_extension_free(&frame->ext);
966
0
    nghttp2_mem_free(mem, item);
967
0
    return rv;
968
0
  }
969
970
0
  return 0;
971
0
}