-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle leading wildcard in directive path #41
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
#include <algorithm> | ||
#include <iomanip> | ||
#include <sstream> | ||
|
||
#include "url.h" | ||
|
||
#include "agent.h" | ||
|
@@ -13,18 +12,32 @@ namespace | |
{ | ||
return url.defrag().escape().fullpath(); | ||
} | ||
|
||
std::string trim_front(const std::string& str, const char chr) | ||
{ | ||
auto itr = std::find_if(str.begin(), str.end(), | ||
[chr](const char c) {return c != chr;}); | ||
return std::string(itr, str.end()); | ||
} | ||
} | ||
|
||
namespace Rep | ||
{ | ||
Agent& Agent::allow(const std::string& query) | ||
{ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why a blank line here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no explanation for this blank line. |
||
Url::Url url(query); | ||
// ignore directives for external URLs | ||
if (is_external(url)) | ||
{ | ||
return *this; | ||
} | ||
// leading wildcard? | ||
if (query.front() == '*') | ||
{ | ||
Url::Url trimmed(trim_front(query, '*')); | ||
directives_.push_back(Directive(escape_url(trimmed), true)); | ||
} | ||
directives_.push_back(Directive(escape_url(url), true)); | ||
sorted_ = false; | ||
return *this; | ||
|
@@ -45,6 +58,12 @@ namespace Rep | |
{ | ||
return *this; | ||
} | ||
// leading wildcard? | ||
if (query.front() == '*') | ||
{ | ||
Url::Url trimmed(trim_front(query, '*')); | ||
directives_.push_back(Directive(escape_url(trimmed), false)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean for there to be an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that is the intent. There is an explanation in my PR comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. D'oh again! |
||
directives_.push_back(Directive(escape_url(url), false)); | ||
} | ||
sorted_ = false; | ||
|
@@ -55,9 +74,10 @@ namespace Rep | |
{ | ||
if (!sorted_) | ||
{ | ||
std::sort(directives_.begin(), directives_.end(), [](const Directive& a, const Directive& b) { | ||
return b.priority() < a.priority(); | ||
}); | ||
std::sort(directives_.begin(), directives_.end(), | ||
[](const Directive& a, const Directive& b) { | ||
return b.priority() < a.priority(); | ||
}); | ||
sorted_ = true; | ||
} | ||
return directives_; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -319,3 +319,39 @@ TEST(RobotsTest, NeverExternalAllowed) | |
Rep::Robots robot("", "http://a.com/robots.txt"); | ||
EXPECT_FALSE(robot.allowed("http://b.com/", "one")); | ||
} | ||
|
||
TEST(RobotsTest, LeadingWildcardAllow) | ||
{ | ||
std::string content = | ||
"User-agent: meow\n" | ||
"Disallow: /\n" | ||
"Allow: ****/cats\n" | ||
"Allow: */kangaroos\n"; | ||
Rep::Robots robot(content); | ||
|
||
EXPECT_FALSE(robot.allowed("/kangaroo/zebra/cat/page.html", "meow")); | ||
EXPECT_TRUE(robot.allowed("/cats.html", "meow")); | ||
EXPECT_TRUE(robot.allowed("/cats/page.html", "meow")); | ||
EXPECT_TRUE(robot.allowed("/get/more/cats/page.html", "meow")); | ||
EXPECT_TRUE(robot.allowed("/kangaroos/page.html", "meow")); | ||
EXPECT_TRUE(robot.allowed("/heaps/of/kangaroos/page.html", "meow")); | ||
EXPECT_TRUE(robot.allowed("/kangaroosandkoalas/page.html", "meow")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like a case with neither There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first line has neither
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy to add a specific test if you have one in mind? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. D'oh! Never mind. |
||
} | ||
|
||
TEST(RobotsTest, LeadingWildcardDisallow) | ||
{ | ||
std::string content = | ||
"User-agent: meow\n" | ||
"Allow: /\n" | ||
"Disallow: ****/cats\n" | ||
"Disallow: */kangaroos\n"; | ||
Rep::Robots robot(content); | ||
|
||
EXPECT_TRUE(robot.allowed("/kangaroo/zebra/cat/page.html", "meow")); | ||
EXPECT_FALSE(robot.allowed("/cats.html", "meow")); | ||
EXPECT_FALSE(robot.allowed("/cats/page.html", "meow")); | ||
EXPECT_FALSE(robot.allowed("/get/more/cats/page.html", "meow")); | ||
EXPECT_FALSE(robot.allowed("/kangaroos/page.html", "meow")); | ||
EXPECT_FALSE(robot.allowed("/heaps/of/kangaroos/page.html", "meow")); | ||
EXPECT_FALSE(robot.allowed("/kangaroosandkoalas/page.html", "meow")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this line here to separate system includes from the rest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea? I probably added or removed it by accident when adding
iostream
for debugging.