Kylin/HttpProxy/UrlRouterPrivate.h
2023-07-21 15:28:59 +08:00

64 lines
2.0 KiB
C++

#ifndef __URLROUTERPRIVATE_H__
#define __URLROUTERPRIVATE_H__
#include "TemplateSegmentRule.h"
#include <boost/url/segments_encoded_view.hpp>
#include <boost/url/string_view.hpp>
#include <vector>
class AnyResource {
public:
virtual ~AnyResource() = default;
virtual void const *get() const noexcept = 0;
};
class ChildIndexVector {
static constexpr std::size_t N = 5;
public:
bool empty() const;
std::size_t size() const;
std::size_t *begin();
const std::size_t *begin() const;
std::size_t *end();
const std::size_t *end() const;
void erase(std::size_t *it);
void push_back(std::size_t v);
private:
std::size_t m_capcity{0};
std::size_t m_size{0};
std::size_t *m_childIndexes{nullptr};
std::size_t m_staticChildIndexes[N]{};
};
class SegementNode {
public:
static constexpr std::size_t npos{std::size_t(-1)};
std::size_t parentIndex{npos};
ChildIndexVector childIndexes;
TemplateSegment segment;
std::string templatePath;
std::shared_ptr<AnyResource> resource;
};
class UrlRouterPrivate {
public:
UrlRouterPrivate();
void insertImpl(boost::urls::string_view pattern, const std::shared_ptr<AnyResource> &resource);
const AnyResource *findImpl(boost::urls::segments_encoded_view path, boost::urls::string_view *&matches,
boost::urls::string_view *&ids) const noexcept;
protected:
SegementNode const *tryMatch(boost::urls::segments_encoded_view::const_iterator it,
boost::urls::segments_encoded_view::const_iterator end, const SegementNode *current,
int level, boost::urls::string_view *&matches, boost::urls::string_view *&ids) const;
static SegementNode const *findOptionalResource(const SegementNode *root, std::vector<SegementNode> const &ns,
boost::urls::string_view *&matches, boost::urls::string_view *&ids);
private:
std::vector<SegementNode> m_nodes;
};
#endif // __URLROUTERPRIVATE_H__