2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
28 #include "nghttp2_config.h"
32 #endif // HAVE_UNISTD_H
36 #endif // HAVE_NETDB_H
50 #include "url-parser/url_parser.h"
54 #include "allocator.h"
58 constexpr auto NGHTTP2_H2_ALPN = StringRef::from_lit("\x2h2");
59 constexpr auto NGHTTP2_H2 = StringRef::from_lit("h2");
61 // The additional HTTP/2 protocol ALPN protocol identifier we also
62 // supports for our applications to make smooth migration into final
64 constexpr auto NGHTTP2_H2_16_ALPN = StringRef::from_lit("\x5h2-16");
65 constexpr auto NGHTTP2_H2_16 = StringRef::from_lit("h2-16");
67 constexpr auto NGHTTP2_H2_14_ALPN = StringRef::from_lit("\x5h2-14");
68 constexpr auto NGHTTP2_H2_14 = StringRef::from_lit("h2-14");
70 constexpr auto NGHTTP2_H1_1_ALPN = StringRef::from_lit("\x8http/1.1");
71 constexpr auto NGHTTP2_H1_1 = StringRef::from_lit("http/1.1");
73 constexpr size_t NGHTTP2_MAX_UINT64_DIGITS = str_size("18446744073709551615");
77 inline bool is_alpha(const char c) {
78 return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
81 inline bool is_digit(const char c) { return '0' <= c && c <= '9'; }
83 inline bool is_hex_digit(const char c) {
84 return is_digit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
87 // Returns true if |s| is hex string.
88 bool is_hex_string(const StringRef &s);
90 bool in_rfc3986_unreserved_chars(const char c);
92 bool in_rfc3986_sub_delims(const char c);
94 // Returns true if |c| is in token (HTTP-p1, Section 3.2.6)
95 bool in_token(char c);
97 bool in_attr_char(char c);
99 // Returns integer corresponding to hex notation |c|. If
100 // is_hex_digit(c) is false, it returns 256.
101 uint32_t hex_to_uint(char c);
103 std::string percent_encode(const unsigned char *target, size_t len);
105 std::string percent_encode(const std::string &target);
107 // percent-encode path component of URI |s|.
108 std::string percent_encode_path(const std::string &s);
110 template <typename InputIt>
111 std::string percent_decode(InputIt first, InputIt last) {
113 result.resize(last - first);
114 auto p = std::begin(result);
115 for (; first != last; ++first) {
121 if (first + 1 != last && first + 2 != last && is_hex_digit(*(first + 1)) &&
122 is_hex_digit(*(first + 2))) {
123 *p++ = (hex_to_uint(*(first + 1)) << 4) + hex_to_uint(*(first + 2));
130 result.resize(p - std::begin(result));
134 StringRef percent_decode(BlockAllocator &balloc, const StringRef &src);
136 // Percent encode |target| if character is not in token or '%'.
137 StringRef percent_encode_token(BlockAllocator &balloc, const StringRef &target);
139 // Returns quotedString version of |target|. Currently, this function
140 // just replace '"' with '\"'.
141 StringRef quote_string(BlockAllocator &balloc, const StringRef &target);
143 std::string format_hex(const unsigned char *s, size_t len);
145 template <size_t N> std::string format_hex(const unsigned char (&s)[N]) {
146 return format_hex(s, N);
149 template <size_t N> std::string format_hex(const std::array<uint8_t, N> &s) {
150 return format_hex(s.data(), s.size());
153 StringRef format_hex(BlockAllocator &balloc, const StringRef &s);
155 static constexpr char LOWER_XDIGITS[] = "0123456789abcdef";
157 template <typename OutputIt>
158 OutputIt format_hex(OutputIt it, const StringRef &s) {
161 *it++ = LOWER_XDIGITS[c >> 4];
162 *it++ = LOWER_XDIGITS[c & 0xf];
168 // decode_hex decodes hex string |s|, returns the decoded byte string.
169 // This function assumes |s| is hex string, that is is_hex_string(s)
171 StringRef decode_hex(BlockAllocator &balloc, const StringRef &s);
173 // Returns given time |t| from epoch in HTTP Date format (e.g., Mon,
174 // 10 Oct 2016 10:25:58 GMT).
175 std::string http_date(time_t t);
176 // Writes given time |t| from epoch in HTTP Date format into the
177 // buffer pointed by |res|. The buffer must be at least 29 bytes
178 // long. This function returns the one beyond the last position.
179 char *http_date(char *res, time_t t);
181 // Returns given time |t| from epoch in Common Log format (e.g.,
182 // 03/Jul/2014:00:19:38 +0900)
183 std::string common_log_date(time_t t);
184 // Writes given time |t| from epoch in Common Log format into the
185 // buffer pointed by |res|. The buffer must be at least 26 bytes
186 // long. This function returns the one beyond the last position.
187 char *common_log_date(char *res, time_t t);
189 // Returns given millisecond |ms| from epoch in ISO 8601 format (e.g.,
190 // 2014-11-15T12:58:24.741Z or 2014-11-15T12:58:24.741+09:00)
191 std::string iso8601_date(int64_t ms);
192 // Writes given time |t| from epoch in ISO 8601 format into the buffer
193 // pointed by |res|. The buffer must be at least 29 bytes long. This
194 // function returns the one beyond the last position.
195 char *iso8601_date(char *res, int64_t ms);
197 time_t parse_http_date(const StringRef &s);
199 // Parses time formatted as "MMM DD HH:MM:SS YYYY [GMT]" (e.g., Feb 3
200 // 00:55:52 2015 GMT), which is specifically used by OpenSSL
201 // ASN1_TIME_print().
202 time_t parse_openssl_asn1_time_print(const StringRef &s);
206 inline char lowcase(char c) {
207 constexpr static unsigned char tbl[] = {
208 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
209 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
210 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
211 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
212 60, 61, 62, 63, 64, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
213 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
214 'z', 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
215 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
216 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
217 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
218 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
219 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
220 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
221 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
222 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224,
223 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
224 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254,
227 return tbl[static_cast<unsigned char>(c)];
230 template <typename InputIterator1, typename InputIterator2>
231 bool starts_with(InputIterator1 first1, InputIterator1 last1,
232 InputIterator2 first2, InputIterator2 last2) {
233 if (last1 - first1 < last2 - first2) {
236 return std::equal(first2, last2, first1);
239 template <typename S, typename T> bool starts_with(const S &a, const T &b) {
240 return starts_with(a.begin(), a.end(), b.begin(), b.end());
244 bool operator()(char lhs, char rhs) const {
245 return lowcase(lhs) == lowcase(rhs);
249 template <typename InputIterator1, typename InputIterator2>
250 bool istarts_with(InputIterator1 first1, InputIterator1 last1,
251 InputIterator2 first2, InputIterator2 last2) {
252 if (last1 - first1 < last2 - first2) {
255 return std::equal(first2, last2, first1, CaseCmp());
258 template <typename S, typename T> bool istarts_with(const S &a, const T &b) {
259 return istarts_with(a.begin(), a.end(), b.begin(), b.end());
262 template <typename T, typename CharT, size_t N>
263 bool istarts_with_l(const T &a, const CharT (&b)[N]) {
264 return istarts_with(a.begin(), a.end(), b, b + N - 1);
267 template <typename InputIterator1, typename InputIterator2>
268 bool ends_with(InputIterator1 first1, InputIterator1 last1,
269 InputIterator2 first2, InputIterator2 last2) {
270 if (last1 - first1 < last2 - first2) {
273 return std::equal(first2, last2, last1 - (last2 - first2));
276 template <typename T, typename S> bool ends_with(const T &a, const S &b) {
277 return ends_with(a.begin(), a.end(), b.begin(), b.end());
280 template <typename T, typename CharT, size_t N>
281 bool ends_with_l(const T &a, const CharT (&b)[N]) {
282 return ends_with(a.begin(), a.end(), b, b + N - 1);
285 template <typename InputIterator1, typename InputIterator2>
286 bool iends_with(InputIterator1 first1, InputIterator1 last1,
287 InputIterator2 first2, InputIterator2 last2) {
288 if (last1 - first1 < last2 - first2) {
291 return std::equal(first2, last2, last1 - (last2 - first2), CaseCmp());
294 template <typename T, typename S> bool iends_with(const T &a, const S &b) {
295 return iends_with(a.begin(), a.end(), b.begin(), b.end());
298 template <typename T, typename CharT, size_t N>
299 bool iends_with_l(const T &a, const CharT (&b)[N]) {
300 return iends_with(a.begin(), a.end(), b, b + N - 1);
303 template <typename InputIt1, typename InputIt2>
304 bool strieq(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) {
305 if (std::distance(first1, last1) != std::distance(first2, last2)) {
309 return std::equal(first1, last1, first2, CaseCmp());
312 template <typename T, typename S> bool strieq(const T &a, const S &b) {
313 return strieq(a.begin(), a.end(), b.begin(), b.end());
316 template <typename CharT, typename InputIt, size_t N>
317 bool strieq_l(const CharT (&a)[N], InputIt b, size_t blen) {
318 return strieq(a, a + (N - 1), b, b + blen);
321 template <typename CharT, size_t N, typename T>
322 bool strieq_l(const CharT (&a)[N], const T &b) {
323 return strieq(a, a + (N - 1), b.begin(), b.end());
326 template <typename InputIt1, typename InputIt2>
327 bool streq(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) {
328 if (std::distance(first1, last1) != std::distance(first2, last2)) {
331 return std::equal(first1, last1, first2);
334 template <typename T, typename S> bool streq(const T &a, const S &b) {
335 return streq(a.begin(), a.end(), b.begin(), b.end());
338 template <typename CharT, typename InputIt, size_t N>
339 bool streq_l(const CharT (&a)[N], InputIt b, size_t blen) {
340 return streq(a, a + (N - 1), b, b + blen);
343 template <typename CharT, size_t N, typename T>
344 bool streq_l(const CharT (&a)[N], const T &b) {
345 return streq(a, a + (N - 1), b.begin(), b.end());
348 // Returns true if |a| contains |b|. If both |a| and |b| are empty,
349 // this function returns false.
350 template <typename S, typename T> bool strifind(const S &a, const T &b) {
351 return std::search(a.begin(), a.end(), b.begin(), b.end(), CaseCmp()) !=
355 template <typename InputIt> void inp_strlower(InputIt first, InputIt last) {
356 std::transform(first, last, first, lowcase);
359 // Lowercase |s| in place.
360 inline void inp_strlower(std::string &s) {
361 inp_strlower(std::begin(s), std::end(s));
364 // Returns string representation of |n| with 2 fractional digits.
365 std::string dtos(double n);
367 template <typename T> std::string utos(T n) {
374 for (auto t = n; t; t /= 10, ++nlen)
378 res[--nlen] = (n % 10) + '0';
383 template <typename T, typename OutputIt> OutputIt utos(OutputIt dst, T n) {
389 for (auto t = n; t; t /= 10, ++nlen)
394 *--p = (n % 10) + '0';
399 template <typename T>
400 StringRef make_string_ref_uint(BlockAllocator &balloc, T n) {
401 auto iov = make_byte_ref(balloc, NGHTTP2_MAX_UINT64_DIGITS + 1);
403 p = util::utos(p, n);
405 return StringRef{iov.base, p};
408 template <typename T> std::string utos_unit(T n) {
410 if (n >= (1 << 30)) {
413 } else if (n >= (1 << 20)) {
416 } else if (n >= (1 << 10)) {
426 // Like utos_unit(), but 2 digits fraction part is followed.
427 template <typename T> std::string utos_funit(T n) {
430 if (n >= (1 << 30)) {
433 } else if (n >= (1 << 20)) {
436 } else if (n >= (1 << 10)) {
443 return dtos(static_cast<double>(n) / (1 << b)) + u;
446 extern const char UPPER_XDIGITS[];
448 template <typename T> std::string utox(T n) {
456 for (; t; t /= 16, ++i)
460 for (; n; --i, n /= 16) {
461 res[i] = UPPER_XDIGITS[(n & 0x0f)];
466 void to_token68(std::string &base64str);
468 StringRef to_base64(BlockAllocator &balloc, const StringRef &token68str);
470 void show_candidates(const char *unkopt, const option *options);
472 bool has_uri_field(const http_parser_url &u, http_parser_url_fields field);
474 bool fieldeq(const char *uri1, const http_parser_url &u1, const char *uri2,
475 const http_parser_url &u2, http_parser_url_fields field);
477 bool fieldeq(const char *uri, const http_parser_url &u,
478 http_parser_url_fields field, const char *t);
480 bool fieldeq(const char *uri, const http_parser_url &u,
481 http_parser_url_fields field, const StringRef &t);
483 StringRef get_uri_field(const char *uri, const http_parser_url &u,
484 http_parser_url_fields field);
486 uint16_t get_default_port(const char *uri, const http_parser_url &u);
488 bool porteq(const char *uri1, const http_parser_url &u1, const char *uri2,
489 const http_parser_url &u2);
491 void write_uri_field(std::ostream &o, const char *uri, const http_parser_url &u,
492 http_parser_url_fields field);
494 bool numeric_host(const char *hostname);
496 bool numeric_host(const char *hostname, int family);
498 // Returns numeric address string of |addr|. If getnameinfo() is
499 // failed, "unknown" is returned.
500 std::string numeric_name(const struct sockaddr *sa, socklen_t salen);
502 // Returns string representation of numeric address and port of
503 // |addr|. If address family is AF_UNIX, this return path to UNIX
504 // domain socket. Otherwise, the format is like <HOST>:<PORT>. For
505 // IPv6 address, address is enclosed by square brackets ([]).
506 std::string to_numeric_addr(const Address *addr);
508 // Sets |port| to |addr|.
509 void set_port(Address &addr, uint16_t port);
511 // Returns ASCII dump of |data| of length |len|. Only ASCII printable
512 // characters are preserved. Other characters are replaced with ".".
513 std::string ascii_dump(const uint8_t *data, size_t len);
515 // Returns absolute path of executable path. If argc == 0 or |cwd| is
516 // nullptr, this function returns nullptr. If argv[0] starts with
517 // '/', this function returns argv[0]. Oterwise return cwd + "/" +
518 // argv[0]. If non-null is returned, it is NULL-terminated string and
519 // dynamically allocated by malloc. The caller is responsible to free
521 char *get_exec_path(int argc, char **const argv, const char *cwd);
523 // Validates path so that it does not contain directory traversal
524 // vector. Returns true if path is safe. The |path| must start with
525 // "/" otherwise returns false. This function should be called after
526 // percent-decode was performed.
527 bool check_path(const std::string &path);
529 // Returns the |tv| value as 64 bit integer using a microsecond as an
531 int64_t to_time64(const timeval &tv);
533 // Returns true if ALPN ID |proto| is supported HTTP/2 protocol
535 bool check_h2_is_selected(const StringRef &proto);
537 // Selects h2 protocol ALPN ID if one of supported h2 versions are
538 // present in |in| of length inlen. Returns true if h2 version is
540 bool select_h2(const unsigned char **out, unsigned char *outlen,
541 const unsigned char *in, unsigned int inlen);
543 // Selects protocol ALPN ID if one of identifiers contained in |protolist| is
544 // present in |in| of length inlen. Returns true if identifier is
546 bool select_protocol(const unsigned char **out, unsigned char *outlen,
547 const unsigned char *in, unsigned int inlen,
548 std::vector<std::string> proto_list);
550 // Returns default ALPN protocol list, which only contains supported
551 // HTTP/2 protocol identifier.
552 std::vector<unsigned char> get_default_alpn();
554 // Parses delimited strings in |s| and returns the array of substring,
555 // delimited by |delim|. The any white spaces around substring are
556 // treated as a part of substring.
557 std::vector<std::string> parse_config_str_list(const StringRef &s,
560 // Parses delimited strings in |s| and returns Substrings in |s|
561 // delimited by |delim|. The any white spaces around substring are
562 // treated as a part of substring.
563 std::vector<StringRef> split_str(const StringRef &s, char delim);
565 // Writes given time |tp| in Common Log format (e.g.,
566 // 03/Jul/2014:00:19:38 +0900) in buffer pointed by |out|. The buffer
567 // must be at least 27 bytes, including terminal NULL byte. Expected
568 // type of |tp| is std::chrono::time_point. This function returns
569 // StringRef wrapping the buffer pointed by |out|, and this string is
570 // terminated by NULL.
571 template <typename T> StringRef format_common_log(char *out, const T &tp) {
573 std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch());
574 auto p = common_log_date(out, t.count());
576 return StringRef{out, p};
579 // Returns given time |tp| in ISO 8601 format (e.g.,
580 // 2014-11-15T12:58:24.741Z or 2014-11-15T12:58:24.741+09:00).
581 // Expected type of |tp| is std::chrono::time_point
582 template <typename T> std::string format_iso8601(const T &tp) {
583 auto t = std::chrono::duration_cast<std::chrono::milliseconds>(
584 tp.time_since_epoch());
585 return iso8601_date(t.count());
588 // Writes given time |tp| in ISO 8601 format (e.g.,
589 // 2014-11-15T12:58:24.741Z or 2014-11-15T12:58:24.741+09:00) in
590 // buffer pointed by |out|. The buffer must be at least 30 bytes,
591 // including terminal NULL byte. Expected type of |tp| is
592 // std::chrono::time_point. This function returns StringRef wrapping
593 // the buffer pointed by |out|, and this string is terminated by NULL.
594 template <typename T> StringRef format_iso8601(char *out, const T &tp) {
595 auto t = std::chrono::duration_cast<std::chrono::milliseconds>(
596 tp.time_since_epoch());
597 auto p = iso8601_date(out, t.count());
599 return StringRef{out, p};
602 // Writes given time |tp| in HTTP Date format (e.g., Mon, 10 Oct 2016
603 // 10:25:58 GMT) in buffer pointed by |out|. The buffer must be at
604 // least 30 bytes, including terminal NULL byte. Expected type of
605 // |tp| is std::chrono::time_point. This function returns StringRef
606 // wrapping the buffer pointed by |out|, and this string is terminated
608 template <typename T> StringRef format_http_date(char *out, const T &tp) {
610 std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch());
611 auto p = http_date(out, t.count());
613 return StringRef{out, p};
616 // Return the system precision of the template parameter |Clock| as
617 // a nanosecond value of type |Rep|
618 template <typename Clock, typename Rep> Rep clock_precision() {
619 std::chrono::duration<Rep, std::nano> duration = typename Clock::duration(1);
621 return duration.count();
624 int make_socket_closeonexec(int fd);
625 int make_socket_nonblocking(int fd);
626 int make_socket_nodelay(int fd);
628 int create_nonblock_socket(int family);
630 bool check_socket_connected(int fd);
632 // Returns the error code (errno) by inspecting SO_ERROR of given
633 // |fd|. This function returns the error code if it succeeds, or -1.
634 // Returning 0 means no error.
635 int get_socket_error(int fd);
637 // Returns true if |host| is IPv6 numeric address (e.g., ::1)
638 bool ipv6_numeric_addr(const char *host);
640 // Parses NULL terminated string |s| as unsigned integer and returns
641 // the parsed integer. Additionally, if |s| ends with 'k', 'm', 'g'
642 // and its upper case characters, multiply the integer by 1024, 1024 *
643 // 1024 and 1024 * 1024 respectively. If there is an error, returns
645 int64_t parse_uint_with_unit(const char *s);
646 // The following overload does not require |s| is NULL terminated.
647 int64_t parse_uint_with_unit(const uint8_t *s, size_t len);
648 int64_t parse_uint_with_unit(const StringRef &s);
650 // Parses NULL terminated string |s| as unsigned integer and returns
651 // the parsed integer. If there is an error, returns -1.
652 int64_t parse_uint(const char *s);
653 // The following overload does not require |s| is NULL terminated.
654 int64_t parse_uint(const uint8_t *s, size_t len);
655 int64_t parse_uint(const std::string &s);
656 int64_t parse_uint(const StringRef &s);
658 // Parses NULL terminated string |s| as unsigned integer and returns
659 // the parsed integer casted to double. If |s| ends with "s", the
660 // parsed value's unit is a second. If |s| ends with "ms", the unit
661 // is millisecond. Similarly, it also supports 'm' and 'h' for
662 // minutes and hours respectively. If none of them are given, the
663 // unit is second. This function returns
664 // std::numeric_limits<double>::infinity() if error occurs.
665 double parse_duration_with_unit(const char *s);
666 // The following overload does not require |s| is NULL terminated.
667 double parse_duration_with_unit(const uint8_t *s, size_t len);
668 double parse_duration_with_unit(const StringRef &s);
670 // Returns string representation of time duration |t|. If t has
671 // fractional part (at least more than or equal to 1e-3), |t| is
672 // multiplied by 1000 and the unit "ms" is appended. Otherwise, |t|
673 // is left as is and "s" is appended.
674 std::string duration_str(double t);
676 // Returns string representation of time duration |t|. It appends
677 // unit after the formatting. The available units are s, ms and us.
678 // The unit which is equal to or less than |t| is used and 2
679 // fractional digits follow.
680 std::string format_duration(const std::chrono::microseconds &u);
682 // Just like above, but this takes |t| as seconds.
683 std::string format_duration(double t);
685 // Creates "host:port" string using given |host| and |port|. If
686 // |host| is numeric IPv6 address (e.g., ::1), it is enclosed by "["
687 // and "]". If |port| is 80 or 443, port part is omitted.
688 StringRef make_http_hostport(BlockAllocator &balloc, const StringRef &host,
691 // Just like make_http_hostport(), but doesn't treat 80 and 443
693 std::string make_hostport(const StringRef &host, uint16_t port);
695 StringRef make_hostport(BlockAllocator &balloc, const StringRef &host,
698 // Dumps |src| of length |len| in the format similar to `hexdump -C`.
699 void hexdump(FILE *out, const uint8_t *src, size_t len);
701 // Copies 2 byte unsigned integer |n| in host byte order to |buf| in
702 // network byte order.
703 void put_uint16be(uint8_t *buf, uint16_t n);
705 // Copies 4 byte unsigned integer |n| in host byte order to |buf| in
706 // network byte order.
707 void put_uint32be(uint8_t *buf, uint32_t n);
709 // Retrieves 2 byte unsigned integer stored in |data| in network byte
710 // order and returns it in host byte order.
711 uint16_t get_uint16(const uint8_t *data);
713 // Retrieves 4 byte unsigned integer stored in |data| in network byte
714 // order and returns it in host byte order.
715 uint32_t get_uint32(const uint8_t *data);
717 // Retrieves 8 byte unsigned integer stored in |data| in network byte
718 // order and returns it in host byte order.
719 uint64_t get_uint64(const uint8_t *data);
721 // Reads mime types file (see /etc/mime.types), and stores extension
722 // -> MIME type map in |res|. This function returns 0 if it succeeds,
724 int read_mime_types(std::map<std::string, std::string> &res,
725 const char *filename);
727 // Fills random alpha and digit byte to the range [|first|, |last|).
728 // Returns the one beyond the |last|.
729 template <typename OutputIt, typename Generator>
730 OutputIt random_alpha_digit(OutputIt first, OutputIt last, Generator &gen) {
731 // If we use uint8_t instead char, gcc 6.2.0 complains by shouting
732 // char-array initialized from wide string.
733 static constexpr char s[] =
734 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
735 std::uniform_int_distribution<> dis(0, 26 * 2 + 10 - 1);
736 for (; first != last; ++first) {
737 *first = s[dis(gen)];
742 // Fills random bytes to the range [|first|, |last|).
743 template <typename OutputIt, typename Generator>
744 void random_bytes(OutputIt first, OutputIt last, Generator &gen) {
745 std::uniform_int_distribution<> dis(0, 255);
746 std::generate(first, last, [&dis, &gen]() { return dis(gen); });
749 template <typename OutputIterator, typename CharT, size_t N>
750 OutputIterator copy_lit(OutputIterator it, CharT (&s)[N]) {
751 return std::copy_n(s, N - 1, it);
755 double int_pow(double x, size_t y);
757 uint32_t hash32(const StringRef &s);
759 // Computes SHA-256 of |s|, and stores it in |buf|. This function
760 // returns 0 if it succeeds, or -1.
761 int sha256(uint8_t *buf, const StringRef &s);
763 // Computes SHA-1 of |s|, and stores it in |buf|. This function
764 // returns 0 if it succeeds, or -1.
765 int sha1(uint8_t *buf, const StringRef &s);
767 // Returns host from |hostport|. If host cannot be found in
768 // |hostport|, returns empty string. The returned string might not be
770 StringRef extract_host(const StringRef &hostport);
772 // split_hostport splits host and port in |hostport|. Unlike
773 // extract_host, square brackets enclosing host name is stripped. If
774 // port is not available, it returns empty string in the second
775 // string. The returned string might not be NULL-terminated. On any
776 // error, it returns a pair which has empty strings.
777 std::pair<StringRef, StringRef> split_hostport(const StringRef &hostport);
779 // Returns new std::mt19937 object.
780 std::mt19937 make_mt19937();
782 // daemonize calls daemon(3). If __APPLE__ is defined, it implements
783 // daemon() using fork().
784 int daemonize(int nochdir, int noclose);
788 } // namespace nghttp2