2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2015 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"
43 // std::forward is constexpr since C++14
44 template <typename... T>
46 typename std::decay<typename std::common_type<T...>::type>::type,
48 make_array(T &&... t) {
50 typename std::decay<typename std::common_type<T...>::type>::type,
51 sizeof...(T)>{{std::forward<T>(t)...}};
54 template <typename T, size_t N> constexpr size_t array_size(T (&)[N]) {
58 template <typename T, size_t N> constexpr size_t str_size(T (&)[N]) {
62 // inspired by <http://blog.korfuri.fr/post/go-defer-in-cpp/>, but our
63 // template can take functions returning other than void.
64 template <typename F, typename... T> struct Defer {
65 Defer(F &&f, T &&... t)
66 : f(std::bind(std::forward<F>(f), std::forward<T>(t)...)) {}
67 Defer(Defer &&o) noexcept : f(std::move(o.f)) {}
70 using ResultType = typename std::result_of<typename std::decay<F>::type(
71 typename std::decay<T>::type...)>::type;
72 std::function<ResultType()> f;
75 template <typename F, typename... T> Defer<F, T...> defer(F &&f, T &&... t) {
76 return Defer<F, T...>(std::forward<F>(f), std::forward<T>(t)...);
79 template <typename T, typename F> bool test_flags(T t, F flags) {
80 return (t & flags) == flags;
83 // doubly linked list of element T*. T must have field T *dlprev and
84 // T *dlnext, which point to previous element and next element in the
86 template <typename T> struct DList {
87 DList() : head(nullptr), tail(nullptr), len(0) {}
89 DList(const DList &) = delete;
90 DList &operator=(const DList &) = delete;
92 DList(DList &&other) noexcept
93 : head{std::exchange(other.head, nullptr)},
94 tail{std::exchange(other.tail, nullptr)},
95 len{std::exchange(other.len, 0)} {}
97 DList &operator=(DList &&other) noexcept {
101 head = std::exchange(other.head, nullptr);
102 tail = std::exchange(other.tail, nullptr);
103 len = std::exchange(other.len, 0);
135 t->dlprev = t->dlnext = nullptr;
138 bool empty() const { return head == nullptr; }
140 size_t size() const { return len; }
146 template <typename T> void dlist_delete_all(DList<T> &dl) {
147 for (auto e = dl.head; e;) {
148 auto next = e->dlnext;
154 // User-defined literals for K, M, and G (powers of 1024)
156 constexpr unsigned long long operator"" _k(unsigned long long k) {
160 constexpr unsigned long long operator"" _m(unsigned long long m) {
161 return m * 1024 * 1024;
164 constexpr unsigned long long operator"" _g(unsigned long long g) {
165 return g * 1024 * 1024 * 1024;
168 // User-defined literals for time, converted into double in seconds
171 constexpr double operator"" _h(unsigned long long h) { return h * 60 * 60; }
174 constexpr double operator"" _min(unsigned long long min) { return min * 60; }
177 constexpr double operator"" _s(unsigned long long s) { return s; }
180 constexpr double operator"" _ms(unsigned long long ms) { return ms / 1000.; }
182 // Returns a copy of NULL-terminated string [first, last).
183 template <typename InputIt>
184 std::unique_ptr<char[]> strcopy(InputIt first, InputIt last) {
185 auto res = std::make_unique<char[]>(last - first + 1);
186 *std::copy(first, last, res.get()) = '\0';
190 // Returns a copy of NULL-terminated string |val|.
191 inline std::unique_ptr<char[]> strcopy(const char *val) {
192 return strcopy(val, val + strlen(val));
195 inline std::unique_ptr<char[]> strcopy(const char *val, size_t n) {
196 return strcopy(val, val + n);
199 // Returns a copy of val.c_str().
200 inline std::unique_ptr<char[]> strcopy(const std::string &val) {
201 return strcopy(std::begin(val), std::end(val));
204 inline std::unique_ptr<char[]> strcopy(const std::unique_ptr<char[]> &val) {
208 return strcopy(val.get());
211 inline std::unique_ptr<char[]> strcopy(const std::unique_ptr<char[]> &val,
216 return strcopy(val.get(), val.get() + n);
219 // ImmutableString represents string that is immutable unlike
220 // std::string. It has c_str() and size() functions to mimic
221 // std::string. It manages buffer by itself. Just like std::string,
222 // c_str() returns NULL-terminated string, but NULL character may
223 // appear before the final terminal NULL.
224 class ImmutableString {
226 using traits_type = std::char_traits<char>;
227 using value_type = traits_type::char_type;
228 using allocator_type = std::allocator<char>;
229 using size_type = std::allocator_traits<allocator_type>::size_type;
230 using difference_type =
231 std::allocator_traits<allocator_type>::difference_type;
232 using const_reference = const value_type &;
233 using const_pointer = const value_type *;
234 using const_iterator = const_pointer;
235 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
237 ImmutableString() : len(0), base("") {}
238 ImmutableString(const char *s, size_t slen)
239 : len(slen), base(copystr(s, s + len)) {}
240 explicit ImmutableString(const char *s)
241 : len(strlen(s)), base(copystr(s, s + len)) {}
242 explicit ImmutableString(const std::string &s)
243 : len(s.size()), base(copystr(std::begin(s), std::end(s))) {}
244 template <typename InputIt>
245 ImmutableString(InputIt first, InputIt last)
246 : len(std::distance(first, last)), base(copystr(first, last)) {}
247 ImmutableString(const ImmutableString &other)
248 : len(other.len), base(copystr(std::begin(other), std::end(other))) {}
249 ImmutableString(ImmutableString &&other) noexcept
250 : len{std::exchange(other.len, 0)}, base{std::exchange(other.base, "")} {}
257 ImmutableString &operator=(const ImmutableString &other) {
258 if (this == &other) {
265 base = copystr(std::begin(other), std::end(other));
268 ImmutableString &operator=(ImmutableString &&other) noexcept {
269 if (this == &other) {
275 len = std::exchange(other.len, 0);
276 base = std::exchange(other.base, "");
280 template <size_t N> static ImmutableString from_lit(const char (&s)[N]) {
281 return ImmutableString(s, N - 1);
284 const_iterator begin() const { return base; };
285 const_iterator cbegin() const { return base; };
287 const_iterator end() const { return base + len; };
288 const_iterator cend() const { return base + len; };
290 const_reverse_iterator rbegin() const {
291 return const_reverse_iterator{base + len};
293 const_reverse_iterator crbegin() const {
294 return const_reverse_iterator{base + len};
297 const_reverse_iterator rend() const { return const_reverse_iterator{base}; }
298 const_reverse_iterator crend() const { return const_reverse_iterator{base}; }
300 const char *c_str() const { return base; }
301 size_type size() const { return len; }
302 bool empty() const { return len == 0; }
303 const_reference operator[](size_type pos) const { return *(base + pos); }
306 template <typename InputIt> const char *copystr(InputIt first, InputIt last) {
310 auto res = new char[std::distance(first, last) + 1];
311 *std::copy(first, last, res) = '\0';
319 inline bool operator==(const ImmutableString &lhs, const ImmutableString &rhs) {
320 return lhs.size() == rhs.size() &&
321 std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
324 inline bool operator==(const ImmutableString &lhs, const std::string &rhs) {
325 return lhs.size() == rhs.size() &&
326 std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
329 inline bool operator==(const std::string &lhs, const ImmutableString &rhs) {
333 inline bool operator==(const ImmutableString &lhs, const char *rhs) {
334 return lhs.size() == strlen(rhs) &&
335 std::equal(std::begin(lhs), std::end(lhs), rhs);
338 inline bool operator==(const char *lhs, const ImmutableString &rhs) {
342 inline bool operator!=(const ImmutableString &lhs, const ImmutableString &rhs) {
343 return !(lhs == rhs);
346 inline bool operator!=(const ImmutableString &lhs, const std::string &rhs) {
347 return !(lhs == rhs);
350 inline bool operator!=(const std::string &lhs, const ImmutableString &rhs) {
351 return !(rhs == lhs);
354 inline bool operator!=(const ImmutableString &lhs, const char *rhs) {
355 return !(lhs == rhs);
358 inline bool operator!=(const char *lhs, const ImmutableString &rhs) {
359 return !(rhs == lhs);
362 inline std::ostream &operator<<(std::ostream &o, const ImmutableString &s) {
363 return o.write(s.c_str(), s.size());
366 inline std::string &operator+=(std::string &lhs, const ImmutableString &rhs) {
367 lhs.append(rhs.c_str(), rhs.size());
371 // StringRef is a reference to a string owned by something else. So
372 // it behaves like simple string, but it does not own pointer. When
373 // it is default constructed, it has empty string. You can freely
374 // copy or move around this struct, but never free its pointer. str()
375 // function can be used to export the content as std::string.
378 using traits_type = std::char_traits<char>;
379 using value_type = traits_type::char_type;
380 using allocator_type = std::allocator<char>;
381 using size_type = std::allocator_traits<allocator_type>::size_type;
382 using difference_type =
383 std::allocator_traits<allocator_type>::difference_type;
384 using const_reference = const value_type &;
385 using const_pointer = const value_type *;
386 using const_iterator = const_pointer;
387 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
389 constexpr StringRef() : base(""), len(0) {}
390 explicit StringRef(const std::string &s) : base(s.c_str()), len(s.size()) {}
391 explicit StringRef(const ImmutableString &s)
392 : base(s.c_str()), len(s.size()) {}
393 explicit StringRef(const char *s) : base(s), len(strlen(s)) {}
394 constexpr StringRef(const char *s, size_t n) : base(s), len(n) {}
395 template <typename CharT>
396 constexpr StringRef(const CharT *s, size_t n)
397 : base(reinterpret_cast<const char *>(s)), len(n) {}
398 template <typename InputIt>
399 StringRef(InputIt first, InputIt last)
400 : base(reinterpret_cast<const char *>(&*first)),
401 len(std::distance(first, last)) {}
402 template <typename InputIt>
403 StringRef(InputIt *first, InputIt *last)
404 : base(reinterpret_cast<const char *>(first)),
405 len(std::distance(first, last)) {}
406 template <typename CharT, size_t N>
407 constexpr static StringRef from_lit(const CharT (&s)[N]) {
408 return StringRef{s, N - 1};
410 static StringRef from_maybe_nullptr(const char *s) {
418 constexpr const_iterator begin() const { return base; };
419 constexpr const_iterator cbegin() const { return base; };
421 constexpr const_iterator end() const { return base + len; };
422 constexpr const_iterator cend() const { return base + len; };
424 const_reverse_iterator rbegin() const {
425 return const_reverse_iterator{base + len};
427 const_reverse_iterator crbegin() const {
428 return const_reverse_iterator{base + len};
431 const_reverse_iterator rend() const { return const_reverse_iterator{base}; }
432 const_reverse_iterator crend() const { return const_reverse_iterator{base}; }
434 constexpr const char *c_str() const { return base; }
435 constexpr size_type size() const { return len; }
436 constexpr bool empty() const { return len == 0; }
437 constexpr const_reference operator[](size_type pos) const {
438 return *(base + pos);
441 std::string str() const { return std::string(base, len); }
442 const uint8_t *byte() const {
443 return reinterpret_cast<const uint8_t *>(base);
451 inline bool operator==(const StringRef &lhs, const StringRef &rhs) {
452 return lhs.size() == rhs.size() &&
453 std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
456 inline bool operator==(const StringRef &lhs, const std::string &rhs) {
457 return lhs.size() == rhs.size() &&
458 std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
461 inline bool operator==(const std::string &lhs, const StringRef &rhs) {
465 inline bool operator==(const StringRef &lhs, const char *rhs) {
466 return lhs.size() == strlen(rhs) &&
467 std::equal(std::begin(lhs), std::end(lhs), rhs);
470 inline bool operator==(const StringRef &lhs, const ImmutableString &rhs) {
471 return lhs.size() == rhs.size() &&
472 std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
475 inline bool operator==(const ImmutableString &lhs, const StringRef &rhs) {
479 inline bool operator==(const char *lhs, const StringRef &rhs) {
483 inline bool operator!=(const StringRef &lhs, const StringRef &rhs) {
484 return !(lhs == rhs);
487 inline bool operator!=(const StringRef &lhs, const std::string &rhs) {
488 return !(lhs == rhs);
491 inline bool operator!=(const std::string &lhs, const StringRef &rhs) {
492 return !(rhs == lhs);
495 inline bool operator!=(const StringRef &lhs, const char *rhs) {
496 return !(lhs == rhs);
499 inline bool operator!=(const char *lhs, const StringRef &rhs) {
500 return !(rhs == lhs);
503 inline bool operator<(const StringRef &lhs, const StringRef &rhs) {
504 return std::lexicographical_compare(std::begin(lhs), std::end(lhs),
505 std::begin(rhs), std::end(rhs));
508 inline std::ostream &operator<<(std::ostream &o, const StringRef &s) {
509 return o.write(s.c_str(), s.size());
512 inline std::string &operator+=(std::string &lhs, const StringRef &rhs) {
513 lhs.append(rhs.c_str(), rhs.size());
517 inline int run_app(std::function<int(int, char **)> app, int argc,
520 return app(argc, argv);
521 } catch (const std::bad_alloc &) {
522 fputs("Out of memory\n", stderr);
523 } catch (const std::exception &x) {
524 fprintf(stderr, "Caught %s:\n%s\n", typeid(x).name(), x.what());
526 fputs("Unknown exception caught\n", stderr);
531 } // namespace nghttp2
534 template <> struct hash<nghttp2::StringRef> {
535 std::size_t operator()(const nghttp2::StringRef &s) const noexcept {
537 // https://tools.ietf.org/html/draft-eastlake-fnv-16#section-6.1.1
538 uint32_t h = 2166136261u;
540 h ^= static_cast<uint8_t>(c);
541 h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);