[core] Replace Boost.Spirit with std::regex in CacheControl::parse()

This commit is contained in:
Bruno de Oliveira Abinader
2018-07-28 14:56:26 +03:00
parent 83bd4c2135
commit 990b3b11b9
2 changed files with 16 additions and 21 deletions

View File

@@ -2,34 +2,20 @@
#include <mbgl/util/string.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wshadow"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
#pragma clang diagnostic ignored "-Wunknown-warning-option"
#pragma clang diagnostic ignored "-Wtautological-constant-compare"
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#pragma clang diagnostic pop
#pragma GCC diagnostic pop
#include <regex>
namespace mbgl {
namespace http {
CacheControl CacheControl::parse(const std::string& value) {
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
std::regex maxAgeRegex(R"((?:[^"]*"[^"]*[^\\]")*[^"]*max-age[\s]*=[\s]*([\d]+).*)");
std::smatch maxAgeMatch;
CacheControl result;
qi::phrase_parse(value.begin(), value.end(), (
(qi::lit("must-revalidate") [ phoenix::ref(result.mustRevalidate) = true ]) |
(qi::lit("max-age") >> '=' >> qi::ulong_long [ phoenix::ref(result.maxAge) = qi::_1 ]) |
(*(('"' >> *(('\\' >> qi::char_) | (qi::char_ - '"')) >> '"') | (qi::char_ - '"' - ',')))
) % ',', qi::ascii::space);
result.mustRevalidate = value.find("must-revalidate") != std::string::npos;
if (std::regex_match(value, maxAgeMatch, maxAgeRegex) && maxAgeMatch.size() == 2) {
result.maxAge = ::atoll(maxAgeMatch[1].str().c_str());
}
return result;
}

View File

@@ -10,6 +10,10 @@ TEST(HTTPHeader, Parsing) {
ASSERT_FALSE(bool(cc.maxAge));
EXPECT_FALSE(cc.mustRevalidate);
cc = http::CacheControl::parse(R"#("max-age=34)#");
ASSERT_FALSE(bool(cc.maxAge));
EXPECT_FALSE(cc.mustRevalidate);
cc = http::CacheControl::parse(R"#(max-age =34)#");
ASSERT_TRUE(bool(cc.maxAge));
EXPECT_EQ(34u, *cc.maxAge);
@@ -38,6 +42,11 @@ TEST(HTTPHeader, Parsing) {
EXPECT_EQ(3u, *cc.maxAge);
EXPECT_FALSE(cc.mustRevalidate);
cc = http::CacheControl::parse(R"#(max-age=3,max-age=""34)#");
ASSERT_TRUE(bool(cc.maxAge));
EXPECT_EQ(3u, *cc.maxAge);
EXPECT_FALSE(cc.mustRevalidate);
cc = http::CacheControl::parse(R"#(max-age="\",max-age=4,")#");
ASSERT_FALSE(bool(cc.maxAge));
EXPECT_FALSE(cc.mustRevalidate);