1 /*
2 * nghttp3
3 *
4 * Copyright (c) 2019 nghttp3 contributors
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 "nghttp3_test_helper.h"
26
27 #include <string.h>
28 #include <assert.h>
29
30 #include "nghttp3_str.h"
31 #include "nghttp3_conv.h"
32
nghttp3_write_frame(nghttp3_buf * dest,nghttp3_frame * fr)33 void nghttp3_write_frame(nghttp3_buf *dest, nghttp3_frame *fr) {
34 switch (fr->hd.type) {
35 case NGHTTP3_FRAME_SETTINGS:
36 nghttp3_frame_write_settings_len(&fr->hd.length, &fr->settings);
37 dest->last = nghttp3_frame_write_settings(dest->last, &fr->settings);
38 break;
39 case NGHTTP3_FRAME_GOAWAY:
40 nghttp3_frame_write_goaway_len(&fr->hd.length, &fr->goaway);
41 dest->last = nghttp3_frame_write_goaway(dest->last, &fr->goaway);
42 break;
43 case NGHTTP3_FRAME_PRIORITY_UPDATE:
44 case NGHTTP3_FRAME_PRIORITY_UPDATE_PUSH_ID:
45 nghttp3_frame_write_priority_update_len(&fr->hd.length,
46 &fr->priority_update);
47 dest->last =
48 nghttp3_frame_write_priority_update(dest->last, &fr->priority_update);
49 break;
50 default:
51 assert(0);
52 }
53 }
54
nghttp3_write_frame_qpack(nghttp3_buf * dest,nghttp3_qpack_encoder * qenc,int64_t stream_id,nghttp3_frame * fr)55 void nghttp3_write_frame_qpack(nghttp3_buf *dest, nghttp3_qpack_encoder *qenc,
56 int64_t stream_id, nghttp3_frame *fr) {
57 int rv;
58 const nghttp3_nv *nva;
59 size_t nvlen;
60 nghttp3_buf pbuf, rbuf, ebuf;
61 const nghttp3_mem *mem = nghttp3_mem_default();
62 (void)rv;
63
64 switch (fr->hd.type) {
65 case NGHTTP3_FRAME_HEADERS:
66 nva = fr->headers.nva;
67 nvlen = fr->headers.nvlen;
68 break;
69 default:
70 assert(0);
71 abort();
72 }
73
74 nghttp3_buf_init(&pbuf);
75 nghttp3_buf_init(&rbuf);
76 nghttp3_buf_init(&ebuf);
77
78 rv = nghttp3_qpack_encoder_encode(qenc, &pbuf, &rbuf, &ebuf, stream_id, nva,
79 nvlen);
80 assert(0 == rv);
81 assert(0 == nghttp3_buf_len(&ebuf));
82
83 fr->hd.length = (int64_t)(nghttp3_buf_len(&pbuf) + nghttp3_buf_len(&rbuf));
84
85 dest->last = nghttp3_frame_write_hd(dest->last, &fr->hd);
86 dest->last = nghttp3_cpymem(dest->last, pbuf.pos, nghttp3_buf_len(&pbuf));
87 dest->last = nghttp3_cpymem(dest->last, rbuf.pos, nghttp3_buf_len(&rbuf));
88
89 nghttp3_buf_free(&rbuf, mem);
90 nghttp3_buf_free(&pbuf, mem);
91 }
92
nghttp3_write_frame_qpack_dyn(nghttp3_buf * dest,nghttp3_buf * ebuf,nghttp3_qpack_encoder * qenc,int64_t stream_id,nghttp3_frame * fr)93 void nghttp3_write_frame_qpack_dyn(nghttp3_buf *dest, nghttp3_buf *ebuf,
94 nghttp3_qpack_encoder *qenc,
95 int64_t stream_id, nghttp3_frame *fr) {
96 int rv;
97 const nghttp3_nv *nva;
98 size_t nvlen;
99 nghttp3_buf pbuf, rbuf;
100 const nghttp3_mem *mem = nghttp3_mem_default();
101 (void)rv;
102
103 switch (fr->hd.type) {
104 case NGHTTP3_FRAME_HEADERS:
105 nva = fr->headers.nva;
106 nvlen = fr->headers.nvlen;
107 break;
108 default:
109 assert(0);
110 abort();
111 }
112
113 nghttp3_buf_init(&pbuf);
114 nghttp3_buf_init(&rbuf);
115
116 rv = nghttp3_qpack_encoder_encode(qenc, &pbuf, &rbuf, ebuf, stream_id, nva,
117 nvlen);
118 assert(0 == rv);
119
120 fr->hd.length = (int64_t)(nghttp3_buf_len(&pbuf) + nghttp3_buf_len(&rbuf));
121
122 dest->last = nghttp3_frame_write_hd(dest->last, &fr->hd);
123 dest->last = nghttp3_cpymem(dest->last, pbuf.pos, nghttp3_buf_len(&pbuf));
124 dest->last = nghttp3_cpymem(dest->last, rbuf.pos, nghttp3_buf_len(&rbuf));
125
126 nghttp3_buf_free(&rbuf, mem);
127 nghttp3_buf_free(&pbuf, mem);
128 }
129
nghttp3_write_frame_data(nghttp3_buf * dest,size_t len)130 void nghttp3_write_frame_data(nghttp3_buf *dest, size_t len) {
131 nghttp3_frame_data fr;
132
133 fr.hd.type = NGHTTP3_FRAME_DATA;
134 fr.hd.length = (int64_t)len;
135
136 dest->last = nghttp3_frame_write_hd(dest->last, &fr.hd);
137 memset(dest->last, 0, len);
138 dest->last += len;
139 }
140