Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmnghttp2/lib/nghttp2_submit.c
Line
Count
Source
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 *data_prd,
72
0
                                     void *stream_user_data) {
73
0
  int rv;
74
0
  uint8_t flags_copy;
75
0
  nghttp2_outbound_item *item = NULL;
76
0
  nghttp2_frame *frame = NULL;
77
0
  nghttp2_headers_category hcat;
78
0
  nghttp2_mem *mem;
79
80
0
  mem = &session->mem;
81
82
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
83
0
  if (item == NULL) {
84
0
    rv = NGHTTP2_ERR_NOMEM;
85
0
    goto fail;
86
0
  }
87
88
0
  nghttp2_outbound_item_init(item);
89
90
0
  if (data_prd != NULL && data_prd->read_callback != NULL) {
91
0
    item->aux_data.headers.data_prd = *data_prd;
92
0
  }
93
94
0
  item->aux_data.headers.stream_user_data = stream_user_data;
95
96
0
  flags_copy =
97
0
      (uint8_t)((flags & (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PRIORITY)) |
98
0
                NGHTTP2_FLAG_END_HEADERS);
99
100
0
  if (stream_id == -1) {
101
0
    if (session->next_stream_id > INT32_MAX) {
102
0
      rv = NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
103
0
      goto fail;
104
0
    }
105
106
0
    stream_id = (int32_t)session->next_stream_id;
107
0
    session->next_stream_id += 2;
108
109
0
    hcat = NGHTTP2_HCAT_REQUEST;
110
0
  } else {
111
    /* More specific categorization will be done later. */
112
0
    hcat = NGHTTP2_HCAT_HEADERS;
113
0
  }
114
115
0
  frame = &item->frame;
116
117
0
  nghttp2_frame_headers_init(&frame->headers, flags_copy, stream_id, hcat,
118
0
                             pri_spec, nva_copy, nvlen);
119
120
0
  rv = nghttp2_session_add_item(session, item);
121
122
0
  if (rv != 0) {
123
0
    nghttp2_frame_headers_free(&frame->headers, mem);
124
0
    goto fail2;
125
0
  }
126
127
0
  if (hcat == NGHTTP2_HCAT_REQUEST) {
128
0
    return stream_id;
129
0
  }
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 *data_prd,
147
0
                                         void *stream_user_data) {
148
0
  int rv;
149
0
  nghttp2_nv *nva_copy;
150
0
  nghttp2_priority_spec copy_pri_spec;
151
0
  nghttp2_mem *mem;
152
153
0
  mem = &session->mem;
154
155
0
  if (pri_spec) {
156
0
    copy_pri_spec = *pri_spec;
157
0
    nghttp2_priority_spec_normalize_weight(&copy_pri_spec);
158
0
  } else {
159
0
    nghttp2_priority_spec_default_init(&copy_pri_spec);
160
0
  }
161
162
0
  rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
163
0
  if (rv < 0) {
164
0
    return rv;
165
0
  }
166
167
0
  return submit_headers_shared(session, flags, stream_id, &copy_pri_spec,
168
0
                               nva_copy, nvlen, data_prd, stream_user_data);
169
0
}
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
0
                              int32_t stream_id, uint32_t error_code) {
275
0
  (void)flags;
276
277
0
  if (stream_id == 0) {
278
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
279
0
  }
280
281
0
  return nghttp2_session_add_rst_stream(session, stream_id, error_code);
282
0
}
283
284
int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags,
285
                          int32_t last_stream_id, uint32_t error_code,
286
0
                          const uint8_t *opaque_data, size_t opaque_data_len) {
287
0
  (void)flags;
288
289
0
  if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
290
0
    return 0;
291
0
  }
292
0
  return nghttp2_session_add_goaway(session, last_stream_id, error_code,
293
0
                                    opaque_data, opaque_data_len,
294
0
                                    NGHTTP2_GOAWAY_AUX_NONE);
295
0
}
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
0
                            const nghttp2_settings_entry *iv, size_t niv) {
311
0
  (void)flags;
312
0
  return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
313
0
}
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
0
                                 int32_t window_size_increment) {
383
0
  int rv;
384
0
  nghttp2_stream *stream = 0;
385
0
  (void)flags;
386
387
0
  if (window_size_increment == 0) {
388
0
    return 0;
389
0
  }
390
0
  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
0
  } else {
398
0
    stream = nghttp2_session_get_stream(session, stream_id);
399
0
    if (!stream) {
400
0
      return 0;
401
0
    }
402
403
0
    rv = nghttp2_adjust_local_window_size(
404
0
        &stream->local_window_size, &stream->recv_window_size,
405
0
        &stream->recv_reduction, &window_size_increment);
406
0
    if (rv != 0) {
407
0
      return rv;
408
0
    }
409
0
  }
410
411
0
  if (window_size_increment > 0) {
412
0
    if (stream_id == 0) {
413
0
      session->consumed_size =
414
0
          nghttp2_max(0, session->consumed_size - window_size_increment);
415
0
    } else {
416
0
      stream->consumed_size =
417
0
          nghttp2_max(0, stream->consumed_size - window_size_increment);
418
0
    }
419
420
0
    return nghttp2_session_add_window_update(session, 0, stream_id,
421
0
                                             window_size_increment);
422
0
  }
423
0
  return 0;
424
0
}
425
426
int nghttp2_session_set_local_window_size(nghttp2_session *session,
427
                                          uint8_t flags, int32_t stream_id,
428
0
                                          int32_t window_size) {
429
0
  int32_t window_size_increment;
430
0
  nghttp2_stream *stream;
431
0
  int rv;
432
0
  (void)flags;
433
434
0
  if (window_size < 0) {
435
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
436
0
  }
437
438
0
  if (stream_id == 0) {
439
0
    window_size_increment = window_size - session->local_window_size;
440
441
0
    if (window_size_increment == 0) {
442
0
      return 0;
443
0
    }
444
445
0
    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
0
    rv = nghttp2_increase_local_window_size(
452
0
        &session->local_window_size, &session->recv_window_size,
453
0
        &session->recv_reduction, &window_size_increment);
454
455
0
    if (rv != 0) {
456
0
      return rv;
457
0
    }
458
459
0
    if (window_size_increment > 0) {
460
0
      return nghttp2_session_add_window_update(session, 0, stream_id,
461
0
                                               window_size_increment);
462
0
    }
463
464
0
    return nghttp2_session_update_recv_connection_window_size(session, 0);
465
0
  } 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
0
}
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
0
                                 const nghttp2_data_provider *data_prd) {
744
0
  uint8_t flags = NGHTTP2_FLAG_NONE;
745
0
  if (data_prd == NULL || data_prd->read_callback == NULL) {
746
0
    flags |= NGHTTP2_FLAG_END_STREAM;
747
0
  }
748
749
0
  if (pri_spec) {
750
0
    flags |= NGHTTP2_FLAG_PRIORITY;
751
0
  }
752
753
0
  return flags;
754
0
}
755
756
int32_t nghttp2_submit_request(nghttp2_session *session,
757
                               const nghttp2_priority_spec *pri_spec,
758
                               const nghttp2_nv *nva, size_t nvlen,
759
                               const nghttp2_data_provider *data_prd,
760
0
                               void *stream_user_data) {
761
0
  uint8_t flags;
762
0
  int rv;
763
764
0
  if (session->server) {
765
0
    return NGHTTP2_ERR_PROTO;
766
0
  }
767
768
0
  if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec) &&
769
0
      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
0
  } else {
775
0
    pri_spec = NULL;
776
0
  }
777
778
0
  flags = set_request_flags(pri_spec, data_prd);
779
780
0
  return submit_headers_shared_nva(session, flags, -1, pri_spec, nva, nvlen,
781
0
                                   data_prd, stream_user_data);
782
0
}
783
784
0
static uint8_t set_response_flags(const nghttp2_data_provider *data_prd) {
785
0
  uint8_t flags = NGHTTP2_FLAG_NONE;
786
0
  if (data_prd == NULL || data_prd->read_callback == NULL) {
787
0
    flags |= NGHTTP2_FLAG_END_STREAM;
788
0
  }
789
0
  return flags;
790
0
}
791
792
int nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
793
                            const nghttp2_nv *nva, size_t nvlen,
794
0
                            const nghttp2_data_provider *data_prd) {
795
0
  uint8_t flags;
796
797
0
  if (stream_id <= 0) {
798
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
799
0
  }
800
801
0
  if (!session->server) {
802
0
    return NGHTTP2_ERR_PROTO;
803
0
  }
804
805
0
  flags = set_response_flags(data_prd);
806
0
  return submit_headers_shared_nva(session, flags, stream_id, NULL, nva, nvlen,
807
0
                                   data_prd, NULL);
808
0
}
809
810
int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
811
                        int32_t stream_id,
812
0
                        const nghttp2_data_provider *data_prd) {
813
0
  int rv;
814
0
  nghttp2_outbound_item *item;
815
0
  nghttp2_frame *frame;
816
0
  nghttp2_data_aux_data *aux_data;
817
0
  uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
818
0
  nghttp2_mem *mem;
819
820
0
  mem = &session->mem;
821
822
0
  if (stream_id == 0) {
823
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
824
0
  }
825
826
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
827
0
  if (item == NULL) {
828
0
    return NGHTTP2_ERR_NOMEM;
829
0
  }
830
831
0
  nghttp2_outbound_item_init(item);
832
833
0
  frame = &item->frame;
834
0
  aux_data = &item->aux_data.data;
835
0
  aux_data->data_prd = *data_prd;
836
0
  aux_data->eof = 0;
837
0
  aux_data->flags = nflags;
838
839
  /* flags are sent on transmission */
840
0
  nghttp2_frame_data_init(&frame->data, NGHTTP2_FLAG_NONE, stream_id);
841
842
0
  rv = nghttp2_session_add_item(session, item);
843
0
  if (rv != 0) {
844
0
    nghttp2_frame_data_free(&frame->data);
845
0
    nghttp2_mem_free(mem, item);
846
0
    return rv;
847
0
  }
848
0
  return 0;
849
0
}
850
851
ssize_t nghttp2_pack_settings_payload(uint8_t *buf, size_t buflen,
852
                                      const nghttp2_settings_entry *iv,
853
0
                                      size_t niv) {
854
0
  if (!nghttp2_iv_check(iv, niv)) {
855
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
856
0
  }
857
858
0
  if (buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
859
0
    return NGHTTP2_ERR_INSUFF_BUFSIZE;
860
0
  }
861
862
0
  return (ssize_t)nghttp2_frame_pack_settings_payload(buf, iv, niv);
863
0
}
864
865
int nghttp2_submit_extension(nghttp2_session *session, uint8_t type,
866
0
                             uint8_t flags, int32_t stream_id, void *payload) {
867
0
  int rv;
868
0
  nghttp2_outbound_item *item;
869
0
  nghttp2_frame *frame;
870
0
  nghttp2_mem *mem;
871
872
0
  mem = &session->mem;
873
874
0
  if (type <= NGHTTP2_CONTINUATION) {
875
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
876
0
  }
877
878
0
  if (!session->callbacks.pack_extension_callback) {
879
0
    return NGHTTP2_ERR_INVALID_STATE;
880
0
  }
881
882
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
883
0
  if (item == NULL) {
884
0
    return NGHTTP2_ERR_NOMEM;
885
0
  }
886
887
0
  nghttp2_outbound_item_init(item);
888
889
0
  frame = &item->frame;
890
0
  nghttp2_frame_extension_init(&frame->ext, type, flags, stream_id, payload);
891
892
0
  rv = nghttp2_session_add_item(session, item);
893
0
  if (rv != 0) {
894
0
    nghttp2_frame_extension_free(&frame->ext);
895
0
    nghttp2_mem_free(mem, item);
896
0
    return rv;
897
0
  }
898
899
0
  return 0;
900
0
}