Coverage Report

Created: 2023-03-26 06:11

/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 *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
    rv = detect_self_dependency(session, stream_id, pri_spec);
201
0
    if (rv != 0) {
202
0
      return rv;
203
0
    }
204
205
0
    flags |= NGHTTP2_FLAG_PRIORITY;
206
0
  } else {
207
0
    pri_spec = NULL;
208
0
  }
209
210
0
  return submit_headers_shared_nva(session, flags, stream_id, pri_spec, nva,
211
0
                                   nvlen, NULL, stream_user_data);
212
0
}
213
214
int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
215
0
                        const uint8_t *opaque_data) {
216
0
  flags &= NGHTTP2_FLAG_ACK;
217
0
  return nghttp2_session_add_ping(session, flags, opaque_data);
218
0
}
219
220
int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
221
                            int32_t stream_id,
222
0
                            const nghttp2_priority_spec *pri_spec) {
223
0
  int rv;
224
0
  nghttp2_outbound_item *item;
225
0
  nghttp2_frame *frame;
226
0
  nghttp2_priority_spec copy_pri_spec;
227
0
  nghttp2_mem *mem;
228
0
  (void)flags;
229
230
0
  mem = &session->mem;
231
232
0
  if (stream_id == 0 || pri_spec == NULL) {
233
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
234
0
  }
235
236
0
  if (stream_id == pri_spec->stream_id) {
237
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
238
0
  }
239
240
0
  copy_pri_spec = *pri_spec;
241
242
0
  nghttp2_priority_spec_normalize_weight(&copy_pri_spec);
243
244
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
245
246
0
  if (item == NULL) {
247
0
    return NGHTTP2_ERR_NOMEM;
248
0
  }
249
250
0
  nghttp2_outbound_item_init(item);
251
252
0
  frame = &item->frame;
253
254
0
  nghttp2_frame_priority_init(&frame->priority, stream_id, &copy_pri_spec);
255
256
0
  rv = nghttp2_session_add_item(session, item);
257
258
0
  if (rv != 0) {
259
0
    nghttp2_frame_priority_free(&frame->priority);
260
0
    nghttp2_mem_free(mem, item);
261
262
0
    return rv;
263
0
  }
264
265
0
  return 0;
266
0
}
267
268
int nghttp2_submit_rst_stream(nghttp2_session *session, uint8_t flags,
269
0
                              int32_t stream_id, uint32_t error_code) {
270
0
  (void)flags;
271
272
0
  if (stream_id == 0) {
273
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
274
0
  }
275
276
0
  return nghttp2_session_add_rst_stream(session, stream_id, error_code);
277
0
}
278
279
int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags,
280
                          int32_t last_stream_id, uint32_t error_code,
281
0
                          const uint8_t *opaque_data, size_t opaque_data_len) {
282
0
  (void)flags;
283
284
0
  if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
285
0
    return 0;
286
0
  }
287
0
  return nghttp2_session_add_goaway(session, last_stream_id, error_code,
288
0
                                    opaque_data, opaque_data_len,
289
0
                                    NGHTTP2_GOAWAY_AUX_NONE);
290
0
}
291
292
0
int nghttp2_submit_shutdown_notice(nghttp2_session *session) {
293
0
  if (!session->server) {
294
0
    return NGHTTP2_ERR_INVALID_STATE;
295
0
  }
296
0
  if (session->goaway_flags) {
297
0
    return 0;
298
0
  }
299
0
  return nghttp2_session_add_goaway(session, (1u << 31) - 1, NGHTTP2_NO_ERROR,
300
0
                                    NULL, 0,
301
0
                                    NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE);
302
0
}
303
304
int nghttp2_submit_settings(nghttp2_session *session, uint8_t flags,
305
0
                            const nghttp2_settings_entry *iv, size_t niv) {
306
0
  (void)flags;
307
0
  return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
308
0
}
309
310
int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags,
311
                                    int32_t stream_id, const nghttp2_nv *nva,
312
                                    size_t nvlen,
313
0
                                    void *promised_stream_user_data) {
314
0
  nghttp2_outbound_item *item;
315
0
  nghttp2_frame *frame;
316
0
  nghttp2_nv *nva_copy;
317
0
  uint8_t flags_copy;
318
0
  int32_t promised_stream_id;
319
0
  int rv;
320
0
  nghttp2_mem *mem;
321
0
  (void)flags;
322
323
0
  mem = &session->mem;
324
325
0
  if (stream_id <= 0 || nghttp2_session_is_my_stream_id(session, stream_id)) {
326
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
327
0
  }
328
329
0
  if (!session->server) {
330
0
    return NGHTTP2_ERR_PROTO;
331
0
  }
332
333
  /* All 32bit signed stream IDs are spent. */
334
0
  if (session->next_stream_id > INT32_MAX) {
335
0
    return NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
336
0
  }
337
338
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
339
0
  if (item == NULL) {
340
0
    return NGHTTP2_ERR_NOMEM;
341
0
  }
342
343
0
  nghttp2_outbound_item_init(item);
344
345
0
  item->aux_data.headers.stream_user_data = promised_stream_user_data;
346
347
0
  frame = &item->frame;
348
349
0
  rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
350
0
  if (rv < 0) {
351
0
    nghttp2_mem_free(mem, item);
352
0
    return rv;
353
0
  }
354
355
0
  flags_copy = NGHTTP2_FLAG_END_HEADERS;
356
357
0
  promised_stream_id = (int32_t)session->next_stream_id;
358
0
  session->next_stream_id += 2;
359
360
0
  nghttp2_frame_push_promise_init(&frame->push_promise, flags_copy, stream_id,
361
0
                                  promised_stream_id, nva_copy, nvlen);
362
363
0
  rv = nghttp2_session_add_item(session, item);
364
365
0
  if (rv != 0) {
366
0
    nghttp2_frame_push_promise_free(&frame->push_promise, mem);
367
0
    nghttp2_mem_free(mem, item);
368
369
0
    return rv;
370
0
  }
371
372
0
  return promised_stream_id;
373
0
}
374
375
int nghttp2_submit_window_update(nghttp2_session *session, uint8_t flags,
376
                                 int32_t stream_id,
377
0
                                 int32_t window_size_increment) {
378
0
  int rv;
379
0
  nghttp2_stream *stream = 0;
380
0
  (void)flags;
381
382
0
  if (window_size_increment == 0) {
383
0
    return 0;
384
0
  }
385
0
  if (stream_id == 0) {
386
0
    rv = nghttp2_adjust_local_window_size(
387
0
        &session->local_window_size, &session->recv_window_size,
388
0
        &session->recv_reduction, &window_size_increment);
389
0
    if (rv != 0) {
390
0
      return rv;
391
0
    }
392
0
  } else {
393
0
    stream = nghttp2_session_get_stream(session, stream_id);
394
0
    if (!stream) {
395
0
      return 0;
396
0
    }
397
398
0
    rv = nghttp2_adjust_local_window_size(
399
0
        &stream->local_window_size, &stream->recv_window_size,
400
0
        &stream->recv_reduction, &window_size_increment);
401
0
    if (rv != 0) {
402
0
      return rv;
403
0
    }
404
0
  }
405
406
0
  if (window_size_increment > 0) {
407
0
    if (stream_id == 0) {
408
0
      session->consumed_size =
409
0
          nghttp2_max(0, session->consumed_size - window_size_increment);
410
0
    } else {
411
0
      stream->consumed_size =
412
0
          nghttp2_max(0, stream->consumed_size - window_size_increment);
413
0
    }
414
415
0
    return nghttp2_session_add_window_update(session, 0, stream_id,
416
0
                                             window_size_increment);
417
0
  }
418
0
  return 0;
419
0
}
420
421
int nghttp2_session_set_local_window_size(nghttp2_session *session,
422
                                          uint8_t flags, int32_t stream_id,
423
0
                                          int32_t window_size) {
424
0
  int32_t window_size_increment;
425
0
  nghttp2_stream *stream;
426
0
  int rv;
427
0
  (void)flags;
428
429
0
  if (window_size < 0) {
430
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
431
0
  }
432
433
0
  if (stream_id == 0) {
434
0
    window_size_increment = window_size - session->local_window_size;
435
436
0
    if (window_size_increment == 0) {
437
0
      return 0;
438
0
    }
439
440
0
    if (window_size_increment < 0) {
441
0
      return nghttp2_adjust_local_window_size(
442
0
          &session->local_window_size, &session->recv_window_size,
443
0
          &session->recv_reduction, &window_size_increment);
444
0
    }
445
446
0
    rv = nghttp2_increase_local_window_size(
447
0
        &session->local_window_size, &session->recv_window_size,
448
0
        &session->recv_reduction, &window_size_increment);
449
450
0
    if (rv != 0) {
451
0
      return rv;
452
0
    }
453
454
0
    if (window_size_increment > 0) {
455
0
      return nghttp2_session_add_window_update(session, 0, stream_id,
456
0
                                               window_size_increment);
457
0
    }
458
459
0
    return nghttp2_session_update_recv_connection_window_size(session, 0);
460
0
  } else {
461
0
    stream = nghttp2_session_get_stream(session, stream_id);
462
463
0
    if (stream == NULL) {
464
0
      return 0;
465
0
    }
466
467
0
    window_size_increment = window_size - stream->local_window_size;
468
469
0
    if (window_size_increment == 0) {
470
0
      return 0;
471
0
    }
472
473
0
    if (window_size_increment < 0) {
474
0
      return nghttp2_adjust_local_window_size(
475
0
          &stream->local_window_size, &stream->recv_window_size,
476
0
          &stream->recv_reduction, &window_size_increment);
477
0
    }
478
479
0
    rv = nghttp2_increase_local_window_size(
480
0
        &stream->local_window_size, &stream->recv_window_size,
481
0
        &stream->recv_reduction, &window_size_increment);
482
483
0
    if (rv != 0) {
484
0
      return rv;
485
0
    }
486
487
0
    if (window_size_increment > 0) {
488
0
      return nghttp2_session_add_window_update(session, 0, stream_id,
489
0
                                               window_size_increment);
490
0
    }
491
492
0
    return nghttp2_session_update_recv_stream_window_size(session, stream, 0,
493
0
                                                          1);
494
0
  }
495
496
0
  return 0;
497
0
}
498
499
int nghttp2_submit_altsvc(nghttp2_session *session, uint8_t flags,
500
                          int32_t stream_id, const uint8_t *origin,
501
                          size_t origin_len, const uint8_t *field_value,
502
0
                          size_t field_value_len) {
503
0
  nghttp2_mem *mem;
504
0
  uint8_t *buf, *p;
505
0
  uint8_t *origin_copy;
506
0
  uint8_t *field_value_copy;
507
0
  nghttp2_outbound_item *item;
508
0
  nghttp2_frame *frame;
509
0
  nghttp2_ext_altsvc *altsvc;
510
0
  int rv;
511
0
  (void)flags;
512
513
0
  mem = &session->mem;
514
515
0
  if (!session->server) {
516
0
    return NGHTTP2_ERR_INVALID_STATE;
517
0
  }
518
519
0
  if (2 + origin_len + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
520
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
521
0
  }
522
523
0
  if (stream_id == 0) {
524
0
    if (origin_len == 0) {
525
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
526
0
    }
527
0
  } else if (origin_len != 0) {
528
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
529
0
  }
530
531
0
  buf = nghttp2_mem_malloc(mem, origin_len + field_value_len + 2);
532
0
  if (buf == NULL) {
533
0
    return NGHTTP2_ERR_NOMEM;
534
0
  }
535
536
0
  p = buf;
537
538
0
  origin_copy = p;
539
0
  if (origin_len) {
540
0
    p = nghttp2_cpymem(p, origin, origin_len);
541
0
  }
542
0
  *p++ = '\0';
543
544
0
  field_value_copy = p;
545
0
  if (field_value_len) {
546
0
    p = nghttp2_cpymem(p, field_value, field_value_len);
547
0
  }
548
0
  *p++ = '\0';
549
550
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
551
0
  if (item == NULL) {
552
0
    rv = NGHTTP2_ERR_NOMEM;
553
0
    goto fail_item_malloc;
554
0
  }
555
556
0
  nghttp2_outbound_item_init(item);
557
558
0
  item->aux_data.ext.builtin = 1;
559
560
0
  altsvc = &item->ext_frame_payload.altsvc;
561
562
0
  frame = &item->frame;
563
0
  frame->ext.payload = altsvc;
564
565
0
  nghttp2_frame_altsvc_init(&frame->ext, stream_id, origin_copy, origin_len,
566
0
                            field_value_copy, field_value_len);
567
568
0
  rv = nghttp2_session_add_item(session, item);
569
0
  if (rv != 0) {
570
0
    nghttp2_frame_altsvc_free(&frame->ext, mem);
571
0
    nghttp2_mem_free(mem, item);
572
573
0
    return rv;
574
0
  }
575
576
0
  return 0;
577
578
0
fail_item_malloc:
579
0
  free(buf);
580
581
0
  return rv;
582
0
}
583
584
int nghttp2_submit_origin(nghttp2_session *session, uint8_t flags,
585
0
                          const nghttp2_origin_entry *ov, size_t nov) {
586
0
  nghttp2_mem *mem;
587
0
  uint8_t *p;
588
0
  nghttp2_outbound_item *item;
589
0
  nghttp2_frame *frame;
590
0
  nghttp2_ext_origin *origin;
591
0
  nghttp2_origin_entry *ov_copy;
592
0
  size_t len = 0;
593
0
  size_t i;
594
0
  int rv;
595
0
  (void)flags;
596
597
0
  mem = &session->mem;
598
599
0
  if (!session->server) {
600
0
    return NGHTTP2_ERR_INVALID_STATE;
601
0
  }
602
603
0
  if (nov) {
604
0
    for (i = 0; i < nov; ++i) {
605
0
      len += ov[i].origin_len;
606
0
    }
607
608
0
    if (2 * nov + len > NGHTTP2_MAX_PAYLOADLEN) {
609
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
610
0
    }
611
612
    /* The last nov is added for terminal NULL character. */
613
0
    ov_copy =
614
0
        nghttp2_mem_malloc(mem, nov * sizeof(nghttp2_origin_entry) + len + nov);
615
0
    if (ov_copy == NULL) {
616
0
      return NGHTTP2_ERR_NOMEM;
617
0
    }
618
619
0
    p = (uint8_t *)ov_copy + nov * sizeof(nghttp2_origin_entry);
620
621
0
    for (i = 0; i < nov; ++i) {
622
0
      ov_copy[i].origin = p;
623
0
      ov_copy[i].origin_len = ov[i].origin_len;
624
0
      p = nghttp2_cpymem(p, ov[i].origin, ov[i].origin_len);
625
0
      *p++ = '\0';
626
0
    }
627
628
0
    assert((size_t)(p - (uint8_t *)ov_copy) ==
629
0
           nov * sizeof(nghttp2_origin_entry) + len + nov);
630
0
  } else {
631
0
    ov_copy = NULL;
632
0
  }
633
634
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
635
0
  if (item == NULL) {
636
0
    rv = NGHTTP2_ERR_NOMEM;
637
0
    goto fail_item_malloc;
638
0
  }
639
640
0
  nghttp2_outbound_item_init(item);
641
642
0
  item->aux_data.ext.builtin = 1;
643
644
0
  origin = &item->ext_frame_payload.origin;
645
646
0
  frame = &item->frame;
647
0
  frame->ext.payload = origin;
648
649
0
  nghttp2_frame_origin_init(&frame->ext, ov_copy, nov);
650
651
0
  rv = nghttp2_session_add_item(session, item);
652
0
  if (rv != 0) {
653
0
    nghttp2_frame_origin_free(&frame->ext, mem);
654
0
    nghttp2_mem_free(mem, item);
655
656
0
    return rv;
657
0
  }
658
659
0
  return 0;
660
661
0
fail_item_malloc:
662
0
  free(ov_copy);
663
664
0
  return rv;
665
0
}
666
667
static uint8_t set_request_flags(const nghttp2_priority_spec *pri_spec,
668
0
                                 const nghttp2_data_provider *data_prd) {
669
0
  uint8_t flags = NGHTTP2_FLAG_NONE;
670
0
  if (data_prd == NULL || data_prd->read_callback == NULL) {
671
0
    flags |= NGHTTP2_FLAG_END_STREAM;
672
0
  }
673
674
0
  if (pri_spec) {
675
0
    flags |= NGHTTP2_FLAG_PRIORITY;
676
0
  }
677
678
0
  return flags;
679
0
}
680
681
int32_t nghttp2_submit_request(nghttp2_session *session,
682
                               const nghttp2_priority_spec *pri_spec,
683
                               const nghttp2_nv *nva, size_t nvlen,
684
                               const nghttp2_data_provider *data_prd,
685
0
                               void *stream_user_data) {
686
0
  uint8_t flags;
687
0
  int rv;
688
689
0
  if (session->server) {
690
0
    return NGHTTP2_ERR_PROTO;
691
0
  }
692
693
0
  if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec)) {
694
0
    rv = detect_self_dependency(session, -1, pri_spec);
695
0
    if (rv != 0) {
696
0
      return rv;
697
0
    }
698
0
  } else {
699
0
    pri_spec = NULL;
700
0
  }
701
702
0
  flags = set_request_flags(pri_spec, data_prd);
703
704
0
  return submit_headers_shared_nva(session, flags, -1, pri_spec, nva, nvlen,
705
0
                                   data_prd, stream_user_data);
706
0
}
707
708
0
static uint8_t set_response_flags(const nghttp2_data_provider *data_prd) {
709
0
  uint8_t flags = NGHTTP2_FLAG_NONE;
710
0
  if (data_prd == NULL || data_prd->read_callback == NULL) {
711
0
    flags |= NGHTTP2_FLAG_END_STREAM;
712
0
  }
713
0
  return flags;
714
0
}
715
716
int nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
717
                            const nghttp2_nv *nva, size_t nvlen,
718
0
                            const nghttp2_data_provider *data_prd) {
719
0
  uint8_t flags;
720
721
0
  if (stream_id <= 0) {
722
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
723
0
  }
724
725
0
  if (!session->server) {
726
0
    return NGHTTP2_ERR_PROTO;
727
0
  }
728
729
0
  flags = set_response_flags(data_prd);
730
0
  return submit_headers_shared_nva(session, flags, stream_id, NULL, nva, nvlen,
731
0
                                   data_prd, NULL);
732
0
}
733
734
int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
735
                        int32_t stream_id,
736
0
                        const nghttp2_data_provider *data_prd) {
737
0
  int rv;
738
0
  nghttp2_outbound_item *item;
739
0
  nghttp2_frame *frame;
740
0
  nghttp2_data_aux_data *aux_data;
741
0
  uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
742
0
  nghttp2_mem *mem;
743
744
0
  mem = &session->mem;
745
746
0
  if (stream_id == 0) {
747
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
748
0
  }
749
750
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
751
0
  if (item == NULL) {
752
0
    return NGHTTP2_ERR_NOMEM;
753
0
  }
754
755
0
  nghttp2_outbound_item_init(item);
756
757
0
  frame = &item->frame;
758
0
  aux_data = &item->aux_data.data;
759
0
  aux_data->data_prd = *data_prd;
760
0
  aux_data->eof = 0;
761
0
  aux_data->flags = nflags;
762
763
  /* flags are sent on transmission */
764
0
  nghttp2_frame_data_init(&frame->data, NGHTTP2_FLAG_NONE, stream_id);
765
766
0
  rv = nghttp2_session_add_item(session, item);
767
0
  if (rv != 0) {
768
0
    nghttp2_frame_data_free(&frame->data);
769
0
    nghttp2_mem_free(mem, item);
770
0
    return rv;
771
0
  }
772
0
  return 0;
773
0
}
774
775
ssize_t nghttp2_pack_settings_payload(uint8_t *buf, size_t buflen,
776
                                      const nghttp2_settings_entry *iv,
777
0
                                      size_t niv) {
778
0
  if (!nghttp2_iv_check(iv, niv)) {
779
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
780
0
  }
781
782
0
  if (buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
783
0
    return NGHTTP2_ERR_INSUFF_BUFSIZE;
784
0
  }
785
786
0
  return (ssize_t)nghttp2_frame_pack_settings_payload(buf, iv, niv);
787
0
}
788
789
int nghttp2_submit_extension(nghttp2_session *session, uint8_t type,
790
0
                             uint8_t flags, int32_t stream_id, void *payload) {
791
0
  int rv;
792
0
  nghttp2_outbound_item *item;
793
0
  nghttp2_frame *frame;
794
0
  nghttp2_mem *mem;
795
796
0
  mem = &session->mem;
797
798
0
  if (type <= NGHTTP2_CONTINUATION) {
799
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
800
0
  }
801
802
0
  if (!session->callbacks.pack_extension_callback) {
803
0
    return NGHTTP2_ERR_INVALID_STATE;
804
0
  }
805
806
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
807
0
  if (item == NULL) {
808
0
    return NGHTTP2_ERR_NOMEM;
809
0
  }
810
811
0
  nghttp2_outbound_item_init(item);
812
813
0
  frame = &item->frame;
814
0
  nghttp2_frame_extension_init(&frame->ext, type, flags, stream_id, payload);
815
816
0
  rv = nghttp2_session_add_item(session, item);
817
0
  if (rv != 0) {
818
0
    nghttp2_frame_extension_free(&frame->ext);
819
0
    nghttp2_mem_free(mem, item);
820
0
    return rv;
821
0
  }
822
823
0
  return 0;
824
0
}