1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2021 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 #ifndef HTTP3_H
26 #define HTTP3_H
27
28 #include "nghttp2_config.h"
29
30 #include <cstring>
31 #include <string>
32 #include <vector>
33
34 #include <nghttp3/nghttp3.h>
35
36 #include "http2.h"
37 #include "template.h"
38
39 namespace nghttp2 {
40
41 namespace http3 {
42
43 // Creates nghttp3_nv using |name| and |value| and returns it. The
44 // returned value only references the data pointer to name.c_str() and
45 // value.c_str(). If |no_index| is true, nghttp3_nv flags member has
46 // NGHTTP3_NV_FLAG_NEVER_INDEX flag set.
47 nghttp3_nv make_nv(const std::string &name, const std::string &value,
48 bool never_index = false);
49
50 nghttp3_nv make_nv(const StringRef &name, const StringRef &value,
51 bool never_index = false);
52
53 nghttp3_nv make_nv_nocopy(const std::string &name, const std::string &value,
54 bool never_index = false);
55
56 nghttp3_nv make_nv_nocopy(const StringRef &name, const StringRef &value,
57 bool never_index = false);
58
59 // Create nghttp3_nv from string literal |name| and |value|.
60 template <size_t N, size_t M>
make_nv_ll(const char (& name)[N],const char (& value)[M])61 constexpr nghttp3_nv make_nv_ll(const char (&name)[N], const char (&value)[M]) {
62 return {(uint8_t *)name, (uint8_t *)value, N - 1, M - 1,
63 NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE};
64 }
65
66 // Create nghttp3_nv from string literal |name| and c-string |value|.
67 template <size_t N>
make_nv_lc(const char (& name)[N],const char * value)68 nghttp3_nv make_nv_lc(const char (&name)[N], const char *value) {
69 return {(uint8_t *)name, (uint8_t *)value, N - 1, strlen(value),
70 NGHTTP3_NV_FLAG_NO_COPY_NAME};
71 }
72
73 template <size_t N>
make_nv_lc_nocopy(const char (& name)[N],const char * value)74 nghttp3_nv make_nv_lc_nocopy(const char (&name)[N], const char *value) {
75 return {(uint8_t *)name, (uint8_t *)value, N - 1, strlen(value),
76 NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE};
77 }
78
79 // Create nghttp3_nv from string literal |name| and std::string
80 // |value|.
81 template <size_t N>
make_nv_ls(const char (& name)[N],const std::string & value)82 nghttp3_nv make_nv_ls(const char (&name)[N], const std::string &value) {
83 return {(uint8_t *)name, (uint8_t *)value.c_str(), N - 1, value.size(),
84 NGHTTP3_NV_FLAG_NO_COPY_NAME};
85 }
86
87 template <size_t N>
make_nv_ls_nocopy(const char (& name)[N],const std::string & value)88 nghttp3_nv make_nv_ls_nocopy(const char (&name)[N], const std::string &value) {
89 return {(uint8_t *)name, (uint8_t *)value.c_str(), N - 1, value.size(),
90 NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE};
91 }
92
93 template <size_t N>
make_nv_ls_nocopy(const char (& name)[N],const StringRef & value)94 nghttp3_nv make_nv_ls_nocopy(const char (&name)[N], const StringRef &value) {
95 return {(uint8_t *)name, (uint8_t *)value.c_str(), N - 1, value.size(),
96 NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE};
97 }
98
99 // Appends headers in |headers| to |nv|. |headers| must be indexed
100 // before this call (its element's token field is assigned). Certain
101 // headers, including disallowed headers in HTTP/3 spec and headers
102 // which require special handling (i.e. via), are not copied. |flags|
103 // is one or more of HeaderBuildOp flags. They tell function that
104 // certain header fields should not be added.
105 void copy_headers_to_nva(std::vector<nghttp3_nv> &nva,
106 const HeaderRefs &headers, uint32_t flags);
107
108 // Just like copy_headers_to_nva(), but this adds
109 // NGHTTP3_NV_FLAG_NO_COPY_NAME and NGHTTP3_NV_FLAG_NO_COPY_VALUE.
110 void copy_headers_to_nva_nocopy(std::vector<nghttp3_nv> &nva,
111 const HeaderRefs &headers, uint32_t flags);
112
113 // Checks the header name/value pair using nghttp3_check_header_name()
114 // and nghttp3_check_header_value(). If both function returns nonzero,
115 // this function returns nonzero.
116 int check_nv(const uint8_t *name, size_t namelen, const uint8_t *value,
117 size_t valuelen);
118
119 } // namespace http3
120
121 } // namespace nghttp2
122
123 #endif // HTTP3_H
124