1/*
2 * nghttp3
3 *
4 * Copyright (c) 2019 nghttp3 contributors
5 * Copyright (c) 2013 nghttp2 contributors
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26#include "nghttp3_frame.h"
27
28#include <string.h>
29#include <assert.h>
30
31#include "nghttp3_conv.h"
32#include "nghttp3_str.h"
33
34uint8_t *nghttp3_frame_write_hd(uint8_t *p, const nghttp3_frame_hd *hd) {
35  p = nghttp3_put_varint(p, hd->type);
36  p = nghttp3_put_varint(p, hd->length);
37  return p;
38}
39
40size_t nghttp3_frame_write_hd_len(const nghttp3_frame_hd *hd) {
41  return nghttp3_put_varint_len(hd->type) + nghttp3_put_varint_len(hd->length);
42}
43
44uint8_t *nghttp3_frame_write_settings(uint8_t *p,
45                                      const nghttp3_frame_settings *fr) {
46  size_t i;
47
48  p = nghttp3_frame_write_hd(p, &fr->hd);
49
50  for (i = 0; i < fr->niv; ++i) {
51    p = nghttp3_put_varint(p, (int64_t)fr->iv[i].id);
52    p = nghttp3_put_varint(p, (int64_t)fr->iv[i].value);
53  }
54
55  return p;
56}
57
58size_t nghttp3_frame_write_settings_len(int64_t *ppayloadlen,
59                                        const nghttp3_frame_settings *fr) {
60  size_t payloadlen = 0;
61  size_t i;
62
63  for (i = 0; i < fr->niv; ++i) {
64    payloadlen += nghttp3_put_varint_len((int64_t)fr->iv[i].id) +
65                  nghttp3_put_varint_len((int64_t)fr->iv[i].value);
66  }
67
68  *ppayloadlen = (int64_t)payloadlen;
69
70  return nghttp3_put_varint_len(NGHTTP3_FRAME_SETTINGS) +
71         nghttp3_put_varint_len((int64_t)payloadlen) + payloadlen;
72}
73
74uint8_t *nghttp3_frame_write_goaway(uint8_t *p,
75                                    const nghttp3_frame_goaway *fr) {
76  p = nghttp3_frame_write_hd(p, &fr->hd);
77  p = nghttp3_put_varint(p, fr->id);
78
79  return p;
80}
81
82size_t nghttp3_frame_write_goaway_len(int64_t *ppayloadlen,
83                                      const nghttp3_frame_goaway *fr) {
84  size_t payloadlen = nghttp3_put_varint_len(fr->id);
85
86  *ppayloadlen = (int64_t)payloadlen;
87
88  return nghttp3_put_varint_len(NGHTTP3_FRAME_GOAWAY) +
89         nghttp3_put_varint_len((int64_t)payloadlen) + payloadlen;
90}
91
92uint8_t *
93nghttp3_frame_write_priority_update(uint8_t *p,
94                                    const nghttp3_frame_priority_update *fr) {
95  p = nghttp3_frame_write_hd(p, &fr->hd);
96  p = nghttp3_put_varint(p, fr->pri_elem_id);
97
98  assert(fr->pri.urgency <= NGHTTP3_URGENCY_LOW);
99
100  *p++ = 'u';
101  *p++ = '=';
102  *p++ = (uint8_t)('0' + fr->pri.urgency);
103
104  if (fr->pri.inc) {
105#define NGHTTP3_PRIORITY_INCREMENTAL ", i"
106    p = nghttp3_cpymem(p, (const uint8_t *)NGHTTP3_PRIORITY_INCREMENTAL,
107                       sizeof(NGHTTP3_PRIORITY_INCREMENTAL) - 1);
108  }
109
110  return p;
111}
112
113size_t nghttp3_frame_write_priority_update_len(
114    int64_t *ppayloadlen, const nghttp3_frame_priority_update *fr) {
115  size_t payloadlen = nghttp3_put_varint_len(fr->pri_elem_id) + sizeof("u=U") -
116                      1 + (fr->pri.inc ? sizeof(", i") - 1 : 0);
117
118  *ppayloadlen = (int64_t)payloadlen;
119
120  return nghttp3_put_varint_len(fr->hd.type) +
121         nghttp3_put_varint_len((int64_t)payloadlen) + payloadlen;
122}
123
124int nghttp3_nva_copy(nghttp3_nv **pnva, const nghttp3_nv *nva, size_t nvlen,
125                     const nghttp3_mem *mem) {
126  size_t i;
127  uint8_t *data = NULL;
128  size_t buflen = 0;
129  nghttp3_nv *p;
130
131  if (nvlen == 0) {
132    *pnva = NULL;
133
134    return 0;
135  }
136
137  for (i = 0; i < nvlen; ++i) {
138    /* + 1 for null-termination */
139    if ((nva[i].flags & NGHTTP3_NV_FLAG_NO_COPY_NAME) == 0) {
140      buflen += nva[i].namelen + 1;
141    }
142    if ((nva[i].flags & NGHTTP3_NV_FLAG_NO_COPY_VALUE) == 0) {
143      buflen += nva[i].valuelen + 1;
144    }
145  }
146
147  buflen += sizeof(nghttp3_nv) * nvlen;
148
149  *pnva = nghttp3_mem_malloc(mem, buflen);
150
151  if (*pnva == NULL) {
152    return NGHTTP3_ERR_NOMEM;
153  }
154
155  p = *pnva;
156  data = (uint8_t *)(*pnva) + sizeof(nghttp3_nv) * nvlen;
157
158  for (i = 0; i < nvlen; ++i) {
159    p->flags = nva[i].flags;
160
161    if (nva[i].flags & NGHTTP3_NV_FLAG_NO_COPY_NAME) {
162      p->name = nva[i].name;
163      p->namelen = nva[i].namelen;
164    } else {
165      if (nva[i].namelen) {
166        memcpy(data, nva[i].name, nva[i].namelen);
167      }
168      p->name = data;
169      p->namelen = nva[i].namelen;
170      data[p->namelen] = '\0';
171      nghttp3_downcase(p->name, p->namelen);
172      data += nva[i].namelen + 1;
173    }
174
175    if (nva[i].flags & NGHTTP3_NV_FLAG_NO_COPY_VALUE) {
176      p->value = nva[i].value;
177      p->valuelen = nva[i].valuelen;
178    } else {
179      if (nva[i].valuelen) {
180        memcpy(data, nva[i].value, nva[i].valuelen);
181      }
182      p->value = data;
183      p->valuelen = nva[i].valuelen;
184      data[p->valuelen] = '\0';
185      data += nva[i].valuelen + 1;
186    }
187
188    ++p;
189  }
190  return 0;
191}
192
193void nghttp3_nva_del(nghttp3_nv *nva, const nghttp3_mem *mem) {
194  nghttp3_mem_free(mem, nva);
195}
196
197void nghttp3_frame_headers_free(nghttp3_frame_headers *fr,
198                                const nghttp3_mem *mem) {
199  if (fr == NULL) {
200    return;
201  }
202
203  nghttp3_nva_del(fr->nva, mem);
204}
205