12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2021 Tatsuhiro Tsujikawa
52c593315Sopenharmony_ci *
62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the
82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
122c593315Sopenharmony_ci * the following conditions:
132c593315Sopenharmony_ci *
142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be
152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software.
162c593315Sopenharmony_ci *
172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242c593315Sopenharmony_ci */
252c593315Sopenharmony_ci#ifndef HTTP3_H
262c593315Sopenharmony_ci#define HTTP3_H
272c593315Sopenharmony_ci
282c593315Sopenharmony_ci#include "nghttp2_config.h"
292c593315Sopenharmony_ci
302c593315Sopenharmony_ci#include <cstring>
312c593315Sopenharmony_ci#include <string>
322c593315Sopenharmony_ci#include <vector>
332c593315Sopenharmony_ci
342c593315Sopenharmony_ci#include <nghttp3/nghttp3.h>
352c593315Sopenharmony_ci
362c593315Sopenharmony_ci#include "http2.h"
372c593315Sopenharmony_ci#include "template.h"
382c593315Sopenharmony_ci
392c593315Sopenharmony_cinamespace nghttp2 {
402c593315Sopenharmony_ci
412c593315Sopenharmony_cinamespace http3 {
422c593315Sopenharmony_ci
432c593315Sopenharmony_ci// Creates nghttp3_nv using |name| and |value| and returns it. The
442c593315Sopenharmony_ci// returned value only references the data pointer to name.c_str() and
452c593315Sopenharmony_ci// value.c_str().  If |no_index| is true, nghttp3_nv flags member has
462c593315Sopenharmony_ci// NGHTTP3_NV_FLAG_NEVER_INDEX flag set.
472c593315Sopenharmony_cinghttp3_nv make_nv(const std::string &name, const std::string &value,
482c593315Sopenharmony_ci                   bool never_index = false);
492c593315Sopenharmony_ci
502c593315Sopenharmony_cinghttp3_nv make_nv(const StringRef &name, const StringRef &value,
512c593315Sopenharmony_ci                   bool never_index = false);
522c593315Sopenharmony_ci
532c593315Sopenharmony_cinghttp3_nv make_nv_nocopy(const std::string &name, const std::string &value,
542c593315Sopenharmony_ci                          bool never_index = false);
552c593315Sopenharmony_ci
562c593315Sopenharmony_cinghttp3_nv make_nv_nocopy(const StringRef &name, const StringRef &value,
572c593315Sopenharmony_ci                          bool never_index = false);
582c593315Sopenharmony_ci
592c593315Sopenharmony_ci// Create nghttp3_nv from string literal |name| and |value|.
602c593315Sopenharmony_citemplate <size_t N, size_t M>
612c593315Sopenharmony_ciconstexpr nghttp3_nv make_nv_ll(const char (&name)[N], const char (&value)[M]) {
622c593315Sopenharmony_ci  return {(uint8_t *)name, (uint8_t *)value, N - 1, M - 1,
632c593315Sopenharmony_ci          NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE};
642c593315Sopenharmony_ci}
652c593315Sopenharmony_ci
662c593315Sopenharmony_ci// Create nghttp3_nv from string literal |name| and c-string |value|.
672c593315Sopenharmony_citemplate <size_t N>
682c593315Sopenharmony_cinghttp3_nv make_nv_lc(const char (&name)[N], const char *value) {
692c593315Sopenharmony_ci  return {(uint8_t *)name, (uint8_t *)value, N - 1, strlen(value),
702c593315Sopenharmony_ci          NGHTTP3_NV_FLAG_NO_COPY_NAME};
712c593315Sopenharmony_ci}
722c593315Sopenharmony_ci
732c593315Sopenharmony_citemplate <size_t N>
742c593315Sopenharmony_cinghttp3_nv make_nv_lc_nocopy(const char (&name)[N], const char *value) {
752c593315Sopenharmony_ci  return {(uint8_t *)name, (uint8_t *)value, N - 1, strlen(value),
762c593315Sopenharmony_ci          NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE};
772c593315Sopenharmony_ci}
782c593315Sopenharmony_ci
792c593315Sopenharmony_ci// Create nghttp3_nv from string literal |name| and std::string
802c593315Sopenharmony_ci// |value|.
812c593315Sopenharmony_citemplate <size_t N>
822c593315Sopenharmony_cinghttp3_nv make_nv_ls(const char (&name)[N], const std::string &value) {
832c593315Sopenharmony_ci  return {(uint8_t *)name, (uint8_t *)value.c_str(), N - 1, value.size(),
842c593315Sopenharmony_ci          NGHTTP3_NV_FLAG_NO_COPY_NAME};
852c593315Sopenharmony_ci}
862c593315Sopenharmony_ci
872c593315Sopenharmony_citemplate <size_t N>
882c593315Sopenharmony_cinghttp3_nv make_nv_ls_nocopy(const char (&name)[N], const std::string &value) {
892c593315Sopenharmony_ci  return {(uint8_t *)name, (uint8_t *)value.c_str(), N - 1, value.size(),
902c593315Sopenharmony_ci          NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE};
912c593315Sopenharmony_ci}
922c593315Sopenharmony_ci
932c593315Sopenharmony_citemplate <size_t N>
942c593315Sopenharmony_cinghttp3_nv make_nv_ls_nocopy(const char (&name)[N], const StringRef &value) {
952c593315Sopenharmony_ci  return {(uint8_t *)name, (uint8_t *)value.c_str(), N - 1, value.size(),
962c593315Sopenharmony_ci          NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE};
972c593315Sopenharmony_ci}
982c593315Sopenharmony_ci
992c593315Sopenharmony_ci// Appends headers in |headers| to |nv|.  |headers| must be indexed
1002c593315Sopenharmony_ci// before this call (its element's token field is assigned).  Certain
1012c593315Sopenharmony_ci// headers, including disallowed headers in HTTP/3 spec and headers
1022c593315Sopenharmony_ci// which require special handling (i.e. via), are not copied.  |flags|
1032c593315Sopenharmony_ci// is one or more of HeaderBuildOp flags.  They tell function that
1042c593315Sopenharmony_ci// certain header fields should not be added.
1052c593315Sopenharmony_civoid copy_headers_to_nva(std::vector<nghttp3_nv> &nva,
1062c593315Sopenharmony_ci                         const HeaderRefs &headers, uint32_t flags);
1072c593315Sopenharmony_ci
1082c593315Sopenharmony_ci// Just like copy_headers_to_nva(), but this adds
1092c593315Sopenharmony_ci// NGHTTP3_NV_FLAG_NO_COPY_NAME and NGHTTP3_NV_FLAG_NO_COPY_VALUE.
1102c593315Sopenharmony_civoid copy_headers_to_nva_nocopy(std::vector<nghttp3_nv> &nva,
1112c593315Sopenharmony_ci                                const HeaderRefs &headers, uint32_t flags);
1122c593315Sopenharmony_ci
1132c593315Sopenharmony_ci// Checks the header name/value pair using nghttp3_check_header_name()
1142c593315Sopenharmony_ci// and nghttp3_check_header_value(). If both function returns nonzero,
1152c593315Sopenharmony_ci// this function returns nonzero.
1162c593315Sopenharmony_ciint check_nv(const uint8_t *name, size_t namelen, const uint8_t *value,
1172c593315Sopenharmony_ci             size_t valuelen);
1182c593315Sopenharmony_ci
1192c593315Sopenharmony_ci} // namespace http3
1202c593315Sopenharmony_ci
1212c593315Sopenharmony_ci} // namespace nghttp2
1222c593315Sopenharmony_ci
1232c593315Sopenharmony_ci#endif // HTTP3_H
124