From 2bde1565478bf444b7ce11ac98b2e92478eb65ef Mon Sep 17 00:00:00 2001 From: Sergey Yagodkin Date: Sat, 5 Aug 2017 20:03:41 +0300 Subject: [PATCH] Fix bug with an escaped slash --- src/agent.cpp | 11 ++++++++++- test/test-robots.cpp | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/agent.cpp b/src/agent.cpp index b91cbf0..0e07e01 100644 --- a/src/agent.cpp +++ b/src/agent.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "url.h" @@ -82,6 +83,14 @@ namespace Rep std::string Agent::escape(const std::string& query) { - return Url::Url(query).defrag().escape().fullpath(); + std::regex escaped_slash ("%2[Ff]"); + std::regex escaped_newline ("\n"); + std::string result; + + result = std::regex_replace(query, escaped_slash, "\n"); + + std::string url = Url::Url(result).defrag().escape().fullpath(); + + return std::regex_replace(url, escaped_newline, "%2F"); } } diff --git a/test/test-robots.cpp b/test/test-robots.cpp index 6831967..7349b82 100644 --- a/test/test-robots.cpp +++ b/test/test-robots.cpp @@ -48,6 +48,15 @@ TEST(RobotsTest, HonorsDefaultAgent) EXPECT_TRUE(robot.allowed("/path", "agent")); } +TEST(RobotsTest, EscapedSlash) +{ + std::string content = + "User-agent: *\n" + "Disallow: /*%2F\n"; + Rep::Robots robot(content); + EXPECT_TRUE(robot.allowed("/tmp/", "one")); +} + TEST(RobotsTest, HonorsSpecificAgent) { std::string content =