Coverage Report

Created: 2025-01-09 06:16

/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
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 (dpw != NULL && dpw->data_prd.read_callback != NULL) {
91
0
    item->aux_data.headers.dpw = *dpw;
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_wrap *dpw,
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, dpw, 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(
178
0
    session, NGHTTP2_FLAG_END_STREAM, stream_id, NULL, nva, nvlen, NULL, NULL);
179
0
}
180
181
int32_t nghttp2_submit_headers(nghttp2_session *session, uint8_t flags,
182
                               int32_t stream_id,
183
                               const nghttp2_priority_spec *pri_spec,
184
                               const nghttp2_nv *nva, size_t nvlen,
185
0
                               void *stream_user_data) {
186
0
  int rv;
187
188
0
  if (stream_id == -1) {
189
0
    if (session->server) {
190
0
      return NGHTTP2_ERR_PROTO;
191
0
    }
192
0
  } else if (stream_id <= 0) {
193
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
194
0
  }
195
196
0
  flags &= NGHTTP2_FLAG_END_STREAM;
197
198
0
  if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec) &&
199
0
      session->remote_settings.no_rfc7540_priorities != 1) {
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 (session->remote_settings.no_rfc7540_priorities == 1) {
233
0
    return 0;
234
0
  }
235
236
0
  if (stream_id == 0 || pri_spec == NULL) {
237
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
238
0
  }
239
240
0
  if (stream_id == pri_spec->stream_id) {
241
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
242
0
  }
243
244
0
  copy_pri_spec = *pri_spec;
245
246
0
  nghttp2_priority_spec_normalize_weight(&copy_pri_spec);
247
248
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
249
250
0
  if (item == NULL) {
251
0
    return NGHTTP2_ERR_NOMEM;
252
0
  }
253
254
0
  nghttp2_outbound_item_init(item);
255
256
0
  frame = &item->frame;
257
258
0
  nghttp2_frame_priority_init(&frame->priority, stream_id, &copy_pri_spec);
259
260
0
  rv = nghttp2_session_add_item(session, item);
261
262
0
  if (rv != 0) {
263
0
    nghttp2_frame_priority_free(&frame->priority);
264
0
    nghttp2_mem_free(mem, item);
265
266
0
    return rv;
267
0
  }
268
269
0
  return 0;
270
0
}
271
272
int nghttp2_submit_rst_stream(nghttp2_session *session, uint8_t flags,
273
0
                              int32_t stream_id, uint32_t error_code) {
274
0
  (void)flags;
275
276
0
  if (stream_id == 0) {
277
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
278
0
  }
279
280
0
  return nghttp2_session_add_rst_stream(session, stream_id, error_code);
281
0
}
282
283
int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags,
284
                          int32_t last_stream_id, uint32_t error_code,
285
0
                          const uint8_t *opaque_data, size_t opaque_data_len) {
286
0
  (void)flags;
287
288
0
  if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
289
0
    return 0;
290
0
  }
291
0
  return nghttp2_session_add_goaway(session, last_stream_id, error_code,
292
0
                                    opaque_data, opaque_data_len,
293
0
                                    NGHTTP2_GOAWAY_AUX_NONE);
294
0
}
295
296
0
int nghttp2_submit_shutdown_notice(nghttp2_session *session) {
297
0
  if (!session->server) {
298
0
    return NGHTTP2_ERR_INVALID_STATE;
299
0
  }
300
0
  if (session->goaway_flags) {
301
0
    return 0;
302
0
  }
303
0
  return nghttp2_session_add_goaway(session, (1u << 31) - 1, NGHTTP2_NO_ERROR,
304
0
                                    NULL, 0,
305
0
                                    NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE);
306
0
}
307
308
int nghttp2_submit_settings(nghttp2_session *session, uint8_t flags,
309
23.1k
                            const nghttp2_settings_entry *iv, size_t niv) {
310
23.1k
  (void)flags;
311
23.1k
  return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
312
23.1k
}
313
314
int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags,
315
                                    int32_t stream_id, const nghttp2_nv *nva,
316
                                    size_t nvlen,
317
0
                                    void *promised_stream_user_data) {
318
0
  nghttp2_outbound_item *item;
319
0
  nghttp2_frame *frame;
320
0
  nghttp2_nv *nva_copy;
321
0
  uint8_t flags_copy;
322
0
  int32_t promised_stream_id;
323
0
  int rv;
324
0
  nghttp2_mem *mem;
325
0
  (void)flags;
326
327
0
  mem = &session->mem;
328
329
0
  if (stream_id <= 0 || nghttp2_session_is_my_stream_id(session, stream_id)) {
330
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
331
0
  }
332
333
0
  if (!session->server) {
334
0
    return NGHTTP2_ERR_PROTO;
335
0
  }
336
337
  /* All 32bit signed stream IDs are spent. */
338
0
  if (session->next_stream_id > INT32_MAX) {
339
0
    return NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
340
0
  }
341
342
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
343
0
  if (item == NULL) {
344
0
    return NGHTTP2_ERR_NOMEM;
345
0
  }
346
347
0
  nghttp2_outbound_item_init(item);
348
349
0
  item->aux_data.headers.stream_user_data = promised_stream_user_data;
350
351
0
  frame = &item->frame;
352
353
0
  rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
354
0
  if (rv < 0) {
355
0
    nghttp2_mem_free(mem, item);
356
0
    return rv;
357
0
  }
358
359
0
  flags_copy = NGHTTP2_FLAG_END_HEADERS;
360
361
0
  promised_stream_id = (int32_t)session->next_stream_id;
362
0
  session->next_stream_id += 2;
363
364
0
  nghttp2_frame_push_promise_init(&frame->push_promise, flags_copy, stream_id,
365
0
                                  promised_stream_id, nva_copy, nvlen);
366
367
0
  rv = nghttp2_session_add_item(session, item);
368
369
0
  if (rv != 0) {
370
0
    nghttp2_frame_push_promise_free(&frame->push_promise, mem);
371
0
    nghttp2_mem_free(mem, item);
372
373
0
    return rv;
374
0
  }
375
376
0
  return promised_stream_id;
377
0
}
378
379
int nghttp2_submit_window_update(nghttp2_session *session, uint8_t flags,
380
                                 int32_t stream_id,
381
0
                                 int32_t window_size_increment) {
382
0
  int rv;
383
0
  nghttp2_stream *stream = 0;
384
0
  (void)flags;
385
386
0
  if (window_size_increment == 0) {
387
0
    return 0;
388
0
  }
389
0
  if (stream_id == 0) {
390
0
    rv = nghttp2_adjust_local_window_size(
391
0
      &session->local_window_size, &session->recv_window_size,
392
0
      &session->recv_reduction, &window_size_increment);
393
0
    if (rv != 0) {
394
0
      return rv;
395
0
    }
396
0
  } else {
397
0
    stream = nghttp2_session_get_stream(session, stream_id);
398
0
    if (!stream) {
399
0
      return 0;
400
0
    }
401
402
0
    rv = nghttp2_adjust_local_window_size(
403
0
      &stream->local_window_size, &stream->recv_window_size,
404
0
      &stream->recv_reduction, &window_size_increment);
405
0
    if (rv != 0) {
406
0
      return rv;
407
0
    }
408
0
  }
409
410
0
  if (window_size_increment > 0) {
411
0
    if (stream_id == 0) {
412
0
      session->consumed_size =
413
0
        nghttp2_max_int32(0, session->consumed_size - window_size_increment);
414
0
    } else {
415
0
      stream->consumed_size =
416
0
        nghttp2_max_int32(0, stream->consumed_size - window_size_increment);
417
0
    }
418
419
0
    return nghttp2_session_add_window_update(session, 0, stream_id,
420
0
                                             window_size_increment);
421
0
  }
422
0
  return 0;
423
0
}
424
425
int nghttp2_session_set_local_window_size(nghttp2_session *session,
426
                                          uint8_t flags, int32_t stream_id,
427
0
                                          int32_t window_size) {
428
0
  int32_t window_size_increment;
429
0
  nghttp2_stream *stream;
430
0
  int rv;
431
0
  (void)flags;
432
433
0
  if (window_size < 0) {
434
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
435
0
  }
436
437
0
  if (stream_id == 0) {
438
0
    window_size_increment = window_size - session->local_window_size;
439
440
0
    if (window_size_increment == 0) {
441
0
      return 0;
442
0
    }
443
444
0
    if (window_size_increment < 0) {
445
0
      return nghttp2_adjust_local_window_size(
446
0
        &session->local_window_size, &session->recv_window_size,
447
0
        &session->recv_reduction, &window_size_increment);
448
0
    }
449
450
0
    rv = nghttp2_increase_local_window_size(
451
0
      &session->local_window_size, &session->recv_window_size,
452
0
      &session->recv_reduction, &window_size_increment);
453
454
0
    if (rv != 0) {
455
0
      return rv;
456
0
    }
457
458
0
    if (window_size_increment > 0) {
459
0
      return nghttp2_session_add_window_update(session, 0, stream_id,
460
0
                                               window_size_increment);
461
0
    }
462
463
0
    return nghttp2_session_update_recv_connection_window_size(session, 0);
464
0
  } else {
465
0
    stream = nghttp2_session_get_stream(session, stream_id);
466
467
0
    if (stream == NULL) {
468
0
      return 0;
469
0
    }
470
471
0
    window_size_increment = window_size - stream->local_window_size;
472
473
0
    if (window_size_increment == 0) {
474
0
      return 0;
475
0
    }
476
477
0
    if (window_size_increment < 0) {
478
0
      return nghttp2_adjust_local_window_size(
479
0
        &stream->local_window_size, &stream->recv_window_size,
480
0
        &stream->recv_reduction, &window_size_increment);
481
0
    }
482
483
0
    rv = nghttp2_increase_local_window_size(
484
0
      &stream->local_window_size, &stream->recv_window_size,
485
0
      &stream->recv_reduction, &window_size_increment);
486
487
0
    if (rv != 0) {
488
0
      return rv;
489
0
    }
490
491
0
    if (window_size_increment > 0) {
492
0
      return nghttp2_session_add_window_update(session, 0, stream_id,
493
0
                                               window_size_increment);
494
0
    }
495
496
0
    return nghttp2_session_update_recv_stream_window_size(session, stream, 0,
497
0
                                                          1);
498
0
  }
499
0
}
500
501
int nghttp2_submit_altsvc(nghttp2_session *session, uint8_t flags,
502
                          int32_t stream_id, const uint8_t *origin,
503
                          size_t origin_len, const uint8_t *field_value,
504
0
                          size_t field_value_len) {
505
0
  nghttp2_mem *mem;
506
0
  uint8_t *buf, *p;
507
0
  uint8_t *origin_copy;
508
0
  uint8_t *field_value_copy;
509
0
  nghttp2_outbound_item *item;
510
0
  nghttp2_frame *frame;
511
0
  nghttp2_ext_altsvc *altsvc;
512
0
  int rv;
513
0
  (void)flags;
514
515
0
  mem = &session->mem;
516
517
0
  if (!session->server) {
518
0
    return NGHTTP2_ERR_INVALID_STATE;
519
0
  }
520
521
0
  if (2 + origin_len + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
522
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
523
0
  }
524
525
0
  if (stream_id == 0) {
526
0
    if (origin_len == 0) {
527
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
528
0
    }
529
0
  } else if (origin_len != 0) {
530
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
531
0
  }
532
533
0
  buf = nghttp2_mem_malloc(mem, origin_len + field_value_len + 2);
534
0
  if (buf == NULL) {
535
0
    return NGHTTP2_ERR_NOMEM;
536
0
  }
537
538
0
  p = buf;
539
540
0
  origin_copy = p;
541
0
  if (origin_len) {
542
0
    p = nghttp2_cpymem(p, origin, origin_len);
543
0
  }
544
0
  *p++ = '\0';
545
546
0
  field_value_copy = p;
547
0
  if (field_value_len) {
548
0
    p = nghttp2_cpymem(p, field_value, field_value_len);
549
0
  }
550
0
  *p++ = '\0';
551
552
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
553
0
  if (item == NULL) {
554
0
    rv = NGHTTP2_ERR_NOMEM;
555
0
    goto fail_item_malloc;
556
0
  }
557
558
0
  nghttp2_outbound_item_init(item);
559
560
0
  item->aux_data.ext.builtin = 1;
561
562
0
  altsvc = &item->ext_frame_payload.altsvc;
563
564
0
  frame = &item->frame;
565
0
  frame->ext.payload = altsvc;
566
567
0
  nghttp2_frame_altsvc_init(&frame->ext, stream_id, origin_copy, origin_len,
568
0
                            field_value_copy, field_value_len);
569
570
0
  rv = nghttp2_session_add_item(session, item);
571
0
  if (rv != 0) {
572
0
    nghttp2_frame_altsvc_free(&frame->ext, mem);
573
0
    nghttp2_mem_free(mem, item);
574
575
0
    return rv;
576
0
  }
577
578
0
  return 0;
579
580
0
fail_item_malloc:
581
0
  free(buf);
582
583
0
  return rv;
584
0
}
585
586
int nghttp2_submit_origin(nghttp2_session *session, uint8_t flags,
587
0
                          const nghttp2_origin_entry *ov, size_t nov) {
588
0
  nghttp2_mem *mem;
589
0
  uint8_t *p;
590
0
  nghttp2_outbound_item *item;
591
0
  nghttp2_frame *frame;
592
0
  nghttp2_ext_origin *origin;
593
0
  nghttp2_origin_entry *ov_copy;
594
0
  size_t len = 0;
595
0
  size_t i;
596
0
  int rv;
597
0
  (void)flags;
598
599
0
  mem = &session->mem;
600
601
0
  if (!session->server) {
602
0
    return NGHTTP2_ERR_INVALID_STATE;
603
0
  }
604
605
0
  if (nov) {
606
0
    for (i = 0; i < nov; ++i) {
607
0
      len += ov[i].origin_len;
608
0
    }
609
610
0
    if (2 * nov + len > NGHTTP2_MAX_PAYLOADLEN) {
611
0
      return NGHTTP2_ERR_INVALID_ARGUMENT;
612
0
    }
613
614
    /* The last nov is added for terminal NULL character. */
615
0
    ov_copy =
616
0
      nghttp2_mem_malloc(mem, nov * sizeof(nghttp2_origin_entry) + len + nov);
617
0
    if (ov_copy == NULL) {
618
0
      return NGHTTP2_ERR_NOMEM;
619
0
    }
620
621
0
    p = (uint8_t *)ov_copy + nov * sizeof(nghttp2_origin_entry);
622
623
0
    for (i = 0; i < nov; ++i) {
624
0
      ov_copy[i].origin = p;
625
0
      ov_copy[i].origin_len = ov[i].origin_len;
626
0
      p = nghttp2_cpymem(p, ov[i].origin, ov[i].origin_len);
627
0
      *p++ = '\0';
628
0
    }
629
630
0
    assert((size_t)(p - (uint8_t *)ov_copy) ==
631
0
           nov * sizeof(nghttp2_origin_entry) + len + nov);
632
0
  } else {
633
0
    ov_copy = NULL;
634
0
  }
635
636
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
637
0
  if (item == NULL) {
638
0
    rv = NGHTTP2_ERR_NOMEM;
639
0
    goto fail_item_malloc;
640
0
  }
641
642
0
  nghttp2_outbound_item_init(item);
643
644
0
  item->aux_data.ext.builtin = 1;
645
646
0
  origin = &item->ext_frame_payload.origin;
647
648
0
  frame = &item->frame;
649
0
  frame->ext.payload = origin;
650
651
0
  nghttp2_frame_origin_init(&frame->ext, ov_copy, nov);
652
653
0
  rv = nghttp2_session_add_item(session, item);
654
0
  if (rv != 0) {
655
0
    nghttp2_frame_origin_free(&frame->ext, mem);
656
0
    nghttp2_mem_free(mem, item);
657
658
0
    return rv;
659
0
  }
660
661
0
  return 0;
662
663
0
fail_item_malloc:
664
0
  free(ov_copy);
665
666
0
  return rv;
667
0
}
668
669
int nghttp2_submit_priority_update(nghttp2_session *session, uint8_t flags,
670
                                   int32_t stream_id,
671
                                   const uint8_t *field_value,
672
0
                                   size_t field_value_len) {
673
0
  nghttp2_mem *mem;
674
0
  uint8_t *buf, *p;
675
0
  nghttp2_outbound_item *item;
676
0
  nghttp2_frame *frame;
677
0
  nghttp2_ext_priority_update *priority_update;
678
0
  int rv;
679
0
  (void)flags;
680
681
0
  mem = &session->mem;
682
683
0
  if (session->server) {
684
0
    return NGHTTP2_ERR_INVALID_STATE;
685
0
  }
686
687
0
  if (session->remote_settings.no_rfc7540_priorities == 0) {
688
0
    return 0;
689
0
  }
690
691
0
  if (stream_id == 0 || 4 + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
692
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
693
0
  }
694
695
0
  if (field_value_len) {
696
0
    buf = nghttp2_mem_malloc(mem, field_value_len + 1);
697
0
    if (buf == NULL) {
698
0
      return NGHTTP2_ERR_NOMEM;
699
0
    }
700
701
0
    p = nghttp2_cpymem(buf, field_value, field_value_len);
702
0
    *p = '\0';
703
0
  } else {
704
0
    buf = NULL;
705
0
  }
706
707
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
708
0
  if (item == NULL) {
709
0
    rv = NGHTTP2_ERR_NOMEM;
710
0
    goto fail_item_malloc;
711
0
  }
712
713
0
  nghttp2_outbound_item_init(item);
714
715
0
  item->aux_data.ext.builtin = 1;
716
717
0
  priority_update = &item->ext_frame_payload.priority_update;
718
719
0
  frame = &item->frame;
720
0
  frame->ext.payload = priority_update;
721
722
0
  nghttp2_frame_priority_update_init(&frame->ext, stream_id, buf,
723
0
                                     field_value_len);
724
725
0
  rv = nghttp2_session_add_item(session, item);
726
0
  if (rv != 0) {
727
0
    nghttp2_frame_priority_update_free(&frame->ext, mem);
728
0
    nghttp2_mem_free(mem, item);
729
730
0
    return rv;
731
0
  }
732
733
0
  return 0;
734
735
0
fail_item_malloc:
736
0
  free(buf);
737
738
0
  return rv;
739
0
}
740
741
static uint8_t set_request_flags(const nghttp2_priority_spec *pri_spec,
742
0
                                 const nghttp2_data_provider_wrap *dpw) {
743
0
  uint8_t flags = NGHTTP2_FLAG_NONE;
744
0
  if (dpw == NULL || dpw->data_prd.read_callback == NULL) {
745
0
    flags |= NGHTTP2_FLAG_END_STREAM;
746
0
  }
747
748
0
  if (pri_spec) {
749
0
    flags |= NGHTTP2_FLAG_PRIORITY;
750
0
  }
751
752
0
  return flags;
753
0
}
754
755
static int32_t submit_request_shared(nghttp2_session *session,
756
                                     const nghttp2_priority_spec *pri_spec,
757
                                     const nghttp2_nv *nva, size_t nvlen,
758
                                     const nghttp2_data_provider_wrap *dpw,
759
0
                                     void *stream_user_data) {
760
0
  uint8_t flags;
761
0
  int rv;
762
763
0
  if (session->server) {
764
0
    return NGHTTP2_ERR_PROTO;
765
0
  }
766
767
0
  if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec) &&
768
0
      session->remote_settings.no_rfc7540_priorities != 1) {
769
0
    rv = detect_self_dependency(session, -1, pri_spec);
770
0
    if (rv != 0) {
771
0
      return rv;
772
0
    }
773
0
  } else {
774
0
    pri_spec = NULL;
775
0
  }
776
777
0
  flags = set_request_flags(pri_spec, dpw);
778
779
0
  return submit_headers_shared_nva(session, flags, -1, pri_spec, nva, nvlen,
780
0
                                   dpw, stream_user_data);
781
0
}
782
783
int32_t nghttp2_submit_request(nghttp2_session *session,
784
                               const nghttp2_priority_spec *pri_spec,
785
                               const nghttp2_nv *nva, size_t nvlen,
786
                               const nghttp2_data_provider *data_prd,
787
0
                               void *stream_user_data) {
788
0
  nghttp2_data_provider_wrap dpw;
789
790
0
  return submit_request_shared(session, pri_spec, nva, nvlen,
791
0
                               nghttp2_data_provider_wrap_v1(&dpw, data_prd),
792
0
                               stream_user_data);
793
0
}
794
795
int32_t nghttp2_submit_request2(nghttp2_session *session,
796
                                const nghttp2_priority_spec *pri_spec,
797
                                const nghttp2_nv *nva, size_t nvlen,
798
                                const nghttp2_data_provider2 *data_prd,
799
0
                                void *stream_user_data) {
800
0
  nghttp2_data_provider_wrap dpw;
801
802
0
  return submit_request_shared(session, pri_spec, nva, nvlen,
803
0
                               nghttp2_data_provider_wrap_v2(&dpw, data_prd),
804
0
                               stream_user_data);
805
0
}
806
807
0
static uint8_t set_response_flags(const nghttp2_data_provider_wrap *dpw) {
808
0
  uint8_t flags = NGHTTP2_FLAG_NONE;
809
0
  if (dpw == NULL || dpw->data_prd.read_callback == NULL) {
810
0
    flags |= NGHTTP2_FLAG_END_STREAM;
811
0
  }
812
0
  return flags;
813
0
}
814
815
static int submit_response_shared(nghttp2_session *session, int32_t stream_id,
816
                                  const nghttp2_nv *nva, size_t nvlen,
817
0
                                  const nghttp2_data_provider_wrap *dpw) {
818
0
  uint8_t flags;
819
820
0
  if (stream_id <= 0) {
821
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
822
0
  }
823
824
0
  if (!session->server) {
825
0
    return NGHTTP2_ERR_PROTO;
826
0
  }
827
828
0
  flags = set_response_flags(dpw);
829
0
  return submit_headers_shared_nva(session, flags, stream_id, NULL, nva, nvlen,
830
0
                                   dpw, NULL);
831
0
}
832
833
int nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
834
                            const nghttp2_nv *nva, size_t nvlen,
835
0
                            const nghttp2_data_provider *data_prd) {
836
0
  nghttp2_data_provider_wrap dpw;
837
838
0
  return submit_response_shared(session, stream_id, nva, nvlen,
839
0
                                nghttp2_data_provider_wrap_v1(&dpw, data_prd));
840
0
}
841
842
int nghttp2_submit_response2(nghttp2_session *session, int32_t stream_id,
843
                             const nghttp2_nv *nva, size_t nvlen,
844
0
                             const nghttp2_data_provider2 *data_prd) {
845
0
  nghttp2_data_provider_wrap dpw;
846
847
0
  return submit_response_shared(session, stream_id, nva, nvlen,
848
0
                                nghttp2_data_provider_wrap_v2(&dpw, data_prd));
849
0
}
850
851
int nghttp2_submit_data_shared(nghttp2_session *session, uint8_t flags,
852
                               int32_t stream_id,
853
0
                               const nghttp2_data_provider_wrap *dpw) {
854
0
  int rv;
855
0
  nghttp2_outbound_item *item;
856
0
  nghttp2_frame *frame;
857
0
  nghttp2_data_aux_data *aux_data;
858
0
  uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
859
0
  nghttp2_mem *mem;
860
861
0
  mem = &session->mem;
862
863
0
  if (stream_id == 0) {
864
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
865
0
  }
866
867
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
868
0
  if (item == NULL) {
869
0
    return NGHTTP2_ERR_NOMEM;
870
0
  }
871
872
0
  nghttp2_outbound_item_init(item);
873
874
0
  frame = &item->frame;
875
0
  aux_data = &item->aux_data.data;
876
0
  aux_data->dpw = *dpw;
877
0
  aux_data->eof = 0;
878
0
  aux_data->flags = nflags;
879
880
  /* flags are sent on transmission */
881
0
  nghttp2_frame_data_init(&frame->data, NGHTTP2_FLAG_NONE, stream_id);
882
883
0
  rv = nghttp2_session_add_item(session, item);
884
0
  if (rv != 0) {
885
0
    nghttp2_frame_data_free(&frame->data);
886
0
    nghttp2_mem_free(mem, item);
887
0
    return rv;
888
0
  }
889
0
  return 0;
890
0
}
891
892
int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
893
                        int32_t stream_id,
894
0
                        const nghttp2_data_provider *data_prd) {
895
0
  nghttp2_data_provider_wrap dpw;
896
897
0
  assert(data_prd);
898
899
0
  return nghttp2_submit_data_shared(
900
0
    session, flags, stream_id, nghttp2_data_provider_wrap_v1(&dpw, data_prd));
901
0
}
902
903
int nghttp2_submit_data2(nghttp2_session *session, uint8_t flags,
904
                         int32_t stream_id,
905
0
                         const nghttp2_data_provider2 *data_prd) {
906
0
  nghttp2_data_provider_wrap dpw;
907
908
0
  assert(data_prd);
909
910
0
  return nghttp2_submit_data_shared(
911
0
    session, flags, stream_id, nghttp2_data_provider_wrap_v2(&dpw, data_prd));
912
0
}
913
914
ssize_t nghttp2_pack_settings_payload(uint8_t *buf, size_t buflen,
915
                                      const nghttp2_settings_entry *iv,
916
0
                                      size_t niv) {
917
0
  return (ssize_t)nghttp2_pack_settings_payload2(buf, buflen, iv, niv);
918
0
}
919
920
nghttp2_ssize nghttp2_pack_settings_payload2(uint8_t *buf, size_t buflen,
921
                                             const nghttp2_settings_entry *iv,
922
0
                                             size_t niv) {
923
0
  if (!nghttp2_iv_check(iv, niv)) {
924
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
925
0
  }
926
927
0
  if (buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
928
0
    return NGHTTP2_ERR_INSUFF_BUFSIZE;
929
0
  }
930
931
0
  return (nghttp2_ssize)nghttp2_frame_pack_settings_payload(buf, iv, niv);
932
0
}
933
934
int nghttp2_submit_extension(nghttp2_session *session, uint8_t type,
935
0
                             uint8_t flags, int32_t stream_id, void *payload) {
936
0
  int rv;
937
0
  nghttp2_outbound_item *item;
938
0
  nghttp2_frame *frame;
939
0
  nghttp2_mem *mem;
940
941
0
  mem = &session->mem;
942
943
0
  if (type <= NGHTTP2_CONTINUATION) {
944
0
    return NGHTTP2_ERR_INVALID_ARGUMENT;
945
0
  }
946
947
0
  if (!session->callbacks.pack_extension_callback2 &&
948
0
      !session->callbacks.pack_extension_callback) {
949
0
    return NGHTTP2_ERR_INVALID_STATE;
950
0
  }
951
952
0
  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
953
0
  if (item == NULL) {
954
0
    return NGHTTP2_ERR_NOMEM;
955
0
  }
956
957
0
  nghttp2_outbound_item_init(item);
958
959
0
  frame = &item->frame;
960
0
  nghttp2_frame_extension_init(&frame->ext, type, flags, stream_id, payload);
961
962
0
  rv = nghttp2_session_add_item(session, item);
963
0
  if (rv != 0) {
964
0
    nghttp2_frame_extension_free(&frame->ext);
965
0
    nghttp2_mem_free(mem, item);
966
0
    return rv;
967
0
  }
968
969
0
  return 0;
970
0
}