12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2015 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 SHRPX_ROUTER_H
262c593315Sopenharmony_ci#define SHRPX_ROUTER_H
272c593315Sopenharmony_ci
282c593315Sopenharmony_ci#include "shrpx.h"
292c593315Sopenharmony_ci
302c593315Sopenharmony_ci#include <vector>
312c593315Sopenharmony_ci#include <memory>
322c593315Sopenharmony_ci
332c593315Sopenharmony_ci#include "allocator.h"
342c593315Sopenharmony_ci
352c593315Sopenharmony_ciusing namespace nghttp2;
362c593315Sopenharmony_ci
372c593315Sopenharmony_cinamespace shrpx {
382c593315Sopenharmony_ci
392c593315Sopenharmony_cistruct RNode {
402c593315Sopenharmony_ci  RNode();
412c593315Sopenharmony_ci  RNode(const char *s, size_t len, ssize_t index, ssize_t wildcard_index);
422c593315Sopenharmony_ci  RNode(RNode &&) = default;
432c593315Sopenharmony_ci  RNode(const RNode &) = delete;
442c593315Sopenharmony_ci  RNode &operator=(RNode &&) = default;
452c593315Sopenharmony_ci  RNode &operator=(const RNode &) = delete;
462c593315Sopenharmony_ci
472c593315Sopenharmony_ci  // Next RNode, sorted by s[0].
482c593315Sopenharmony_ci  std::vector<std::unique_ptr<RNode>> next;
492c593315Sopenharmony_ci  // Stores pointer to the string this node represents.  Not
502c593315Sopenharmony_ci  // NULL-terminated.
512c593315Sopenharmony_ci  const char *s;
522c593315Sopenharmony_ci  // Length of |s|
532c593315Sopenharmony_ci  size_t len;
542c593315Sopenharmony_ci  // Index of pattern if match ends in this node.  Note that we don't
552c593315Sopenharmony_ci  // store duplicated pattern.
562c593315Sopenharmony_ci  ssize_t index;
572c593315Sopenharmony_ci  // Index of wildcard pattern if query includes this node as prefix
582c593315Sopenharmony_ci  // and it still has suffix to match.  Note that we don't store
592c593315Sopenharmony_ci  // duplicated pattern.
602c593315Sopenharmony_ci  ssize_t wildcard_index;
612c593315Sopenharmony_ci};
622c593315Sopenharmony_ci
632c593315Sopenharmony_ciclass Router {
642c593315Sopenharmony_cipublic:
652c593315Sopenharmony_ci  Router();
662c593315Sopenharmony_ci  ~Router();
672c593315Sopenharmony_ci  Router(Router &&) = default;
682c593315Sopenharmony_ci  Router(const Router &) = delete;
692c593315Sopenharmony_ci  Router &operator=(Router &&) = default;
702c593315Sopenharmony_ci  Router &operator=(const Router &) = delete;
712c593315Sopenharmony_ci
722c593315Sopenharmony_ci  // Adds route |pattern| with its |index|.  If same pattern has
732c593315Sopenharmony_ci  // already been added, the existing index is returned.  If
742c593315Sopenharmony_ci  // |wildcard| is true, |pattern| is considered as wildcard pattern,
752c593315Sopenharmony_ci  // and all paths which have the |pattern| as prefix and are strictly
762c593315Sopenharmony_ci  // longer than |pattern| match.  The wildcard pattern only works
772c593315Sopenharmony_ci  // with match(const StringRef&, const StringRef&).
782c593315Sopenharmony_ci  size_t add_route(const StringRef &pattern, size_t index,
792c593315Sopenharmony_ci                   bool wildcard = false);
802c593315Sopenharmony_ci  // Returns the matched index of pattern.  -1 if there is no match.
812c593315Sopenharmony_ci  ssize_t match(const StringRef &host, const StringRef &path) const;
822c593315Sopenharmony_ci  // Returns the matched index of pattern |s|.  -1 if there is no
832c593315Sopenharmony_ci  // match.
842c593315Sopenharmony_ci  ssize_t match(const StringRef &s) const;
852c593315Sopenharmony_ci  // Returns the matched index of pattern if a pattern is a suffix of
862c593315Sopenharmony_ci  // |s|, otherwise -1.  If |*last_node| is not nullptr, it specifies
872c593315Sopenharmony_ci  // the first node to start matching.  If it is nullptr, match will
882c593315Sopenharmony_ci  // start from scratch.  When the match was found (the return value
892c593315Sopenharmony_ci  // is not -1), |*nread| has the number of bytes matched in |s|, and
902c593315Sopenharmony_ci  // |*last_node| has the last matched node.  One can continue to
912c593315Sopenharmony_ci  // match the longer pattern using the returned |*last_node| to the
922c593315Sopenharmony_ci  // another invocation of this function until it returns -1.
932c593315Sopenharmony_ci  ssize_t match_prefix(size_t *nread, const RNode **last_node,
942c593315Sopenharmony_ci                       const StringRef &s) const;
952c593315Sopenharmony_ci
962c593315Sopenharmony_ci  void add_node(RNode *node, const char *pattern, size_t patlen, ssize_t index,
972c593315Sopenharmony_ci                ssize_t wildcard_index);
982c593315Sopenharmony_ci
992c593315Sopenharmony_ci  void dump() const;
1002c593315Sopenharmony_ci
1012c593315Sopenharmony_ciprivate:
1022c593315Sopenharmony_ci  BlockAllocator balloc_;
1032c593315Sopenharmony_ci  // The root node of Patricia tree.  This is special node and its s
1042c593315Sopenharmony_ci  // field is nulptr, and len field is 0.
1052c593315Sopenharmony_ci  RNode root_;
1062c593315Sopenharmony_ci};
1072c593315Sopenharmony_ci
1082c593315Sopenharmony_ci} // namespace shrpx
1092c593315Sopenharmony_ci
1102c593315Sopenharmony_ci#endif // SHRPX_ROUTER_H
111