Kylin/HttpProxy/TemplateSegmentRule.h

63 lines
1.8 KiB
C
Raw Normal View History

2023-07-21 15:28:59 +08:00
#ifndef __TEMPLATESEGMENTRULE_H__
#define __TEMPLATESEGMENTRULE_H__
#include <boost/url/error_types.hpp>
#include <boost/url/grammar/delim_rule.hpp>
#include <boost/url/grammar/optional_rule.hpp>
#include <boost/url/grammar/range_rule.hpp>
#include <boost/url/grammar/tuple_rule.hpp>
#include <boost/url/pct_string_view.hpp>
class TemplateSegment {
friend class TemplateSegmentRule;
public:
enum class Modifier {
None,
Optional, // {id?}
Star, // {id*}
Plus // {id+}
};
public:
bool isLiteral() const;
bool isStar() const;
bool isPlus() const;
bool isOptional() const;
bool hasModifier() const;
boost::urls::string_view id() const;
boost::urls::string_view string() const;
bool match(boost::urls::pct_string_view segement) const;
bool operator==(const TemplateSegment &other) const;
// segments have precedence:
// - literal
// - unique
// - optional
// - plus
// - star
bool operator<(const TemplateSegment &other) const;
private:
Modifier modifier = Modifier::None;
std::string m_string;
bool m_isLiteral = true;
};
class TemplateSegmentRule {
public:
using value_type = TemplateSegment;
boost::urls::result<value_type> parse(char const *&iterator, char const *end) const noexcept;
};
constexpr auto templateSegmentRule = TemplateSegmentRule{};
constexpr auto templatePathRule = boost::urls::grammar::tuple_rule(
boost::urls::grammar::squelch(boost::urls::grammar::optional_rule(boost::urls::grammar::delim_rule('/'))),
boost::urls::grammar::range_rule(
templateSegmentRule,
boost::urls::grammar::tuple_rule(boost::urls::grammar::squelch(boost::urls::grammar::delim_rule('/')),
templateSegmentRule)));
#endif // __TEMPLATESEGMENTRULE_H__