Skip to content
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 multiple user-agent #29

Merged
merged 5 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/RobotsTxt.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,36 +126,49 @@ protected function getDisallowsPerUserAgent(string $content): array

$disallowsPerUserAgent = [];

$currentUserAgent = null;
$currentUserAgents = [];

$treatAllowDisallowLine = false;

foreach ($lines as $line) {
if ($this->isCommentLine($line)) {
if ($this->isCommentOrEmptyLine($line)) {
continue;
}

if ($this->isUserAgentLine($line)) {
if ($treatAllowDisallowLine) {
$treatAllowDisallowLine = false;
$currentUserAgents = [];
}
$disallowsPerUserAgent[$this->parseUserAgent($line)] = [];

$currentUserAgent = &$disallowsPerUserAgent[$this->parseUserAgent($line)];
$currentUserAgents[] = &$disallowsPerUserAgent[$this->parseUserAgent($line)];

continue;
}

if ($currentUserAgent === null) {
if ($this->isDisallowLine($line)) {
$treatAllowDisallowLine = true;
} elseif ($this->isAllowLine($line)) {
Kixell-NicolasJardillier marked this conversation as resolved.
Show resolved Hide resolved
$treatAllowDisallowLine = true;
continue;
} else {
continue;
}

$disallowUrl = $this->parseDisallow($line);

$currentUserAgent[$disallowUrl] = $disallowUrl;
foreach ($currentUserAgents as &$currentUserAgent) {
$currentUserAgent[$disallowUrl] = $disallowUrl;
}
}

return $disallowsPerUserAgent;
}

protected function isCommentLine(string $line): bool
protected function isCommentOrEmptyLine(string $line): bool
{
return strpos(trim($line), '#') === 0;
return strpos(trim($line), '#') === 0 || trim($line) === '';
Kixell-NicolasJardillier marked this conversation as resolved.
Show resolved Hide resolved
}

protected function isUserAgentLine(string $line): bool
Expand All @@ -173,6 +186,16 @@ protected function parseDisallow(string $line): string
return trim(substr_replace(strtolower(trim($line)), '', 0, 8), ': ');
}

protected function isDisallowLine(string $line): string
{
return trim(substr(str_replace(' ', '', strtolower(trim($line))), 0, 6), ': ') === 'disall';
Kixell-NicolasJardillier marked this conversation as resolved.
Show resolved Hide resolved
}

protected function isAllowLine(string $line): string
{
return trim(substr(str_replace(' ', '', strtolower(trim($line))), 0, 6), ': ') === 'allow';
}

/**
* @deprecated
*/
Expand Down
56 changes: 56 additions & 0 deletions tests/RobotsTxtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,60 @@ public function the_disallows_user_agent_check_is_case_insensitive()
$this->assertFalse($robots->allows('/no-agents', 'UserAgent007'));
$this->assertFalse($robots->allows('/no-agents', strtolower('UserAgent007')));
}

/** @test */
public function it_can_handle_multiple_user_agent()
Kixell-NicolasJardillier marked this conversation as resolved.
Show resolved Hide resolved
{
$robots = RobotsTxt::readFrom(__DIR__.'/data/robots.txt');

$this->assertFalse($robots->allows('/en/admin?print', 'UserAgent010'));
$this->assertFalse($robots->allows('/en/admin?print=true', 'UserAgent011'));
$this->assertTrue($robots->allows('/en/admin?print=true', 'UserAgent012'));
$this->assertTrue($robots->allows('/en/admin?print=true', 'UserAgent013'));
$this->assertTrue($robots->allows('/en/admin?print=true', 'UserAgent014'));
$this->assertFalse($robots->allows('/en/admin?print=true', 'UserAgent015'));

$this->assertTrue($robots->allows('/', 'UserAgent010'));
$this->assertTrue($robots->allows('/', 'UserAgent011'));
$this->assertTrue($robots->allows('/', 'UserAgent012'));
$this->assertTrue($robots->allows('/', 'UserAgent013'));
$this->assertTrue($robots->allows('/', 'UserAgent014'));
$this->assertFalse($robots->allows('/', 'UserAgent015'));

$this->assertTrue($robots->allows('/fr/ad', 'UserAgent010'));
$this->assertFalse($robots->allows('/fr/admin', 'UserAgent010'));
$this->assertTrue($robots->allows('/fr/admin/', 'UserAgent010'));
$this->assertTrue($robots->allows('/fr/admin?', 'UserAgent010'));
$this->assertTrue($robots->allows('/fr/admin?test', 'UserAgent010'));

$this->assertTrue($robots->allows('/fr/ad', 'UserAgent011'));
$this->assertFalse($robots->allows('/fr/admin', 'UserAgent011'));
$this->assertTrue($robots->allows('/fr/admin/', 'UserAgent011'));
$this->assertTrue($robots->allows('/fr/admin?', 'UserAgent011'));
$this->assertTrue($robots->allows('/fr/admin?test', 'UserAgent011'));

$this->assertTrue($robots->allows('/fr/ad', 'UserAgent012'));
$this->assertTrue($robots->allows('/fr/admin', 'UserAgent012'));
$this->assertTrue($robots->allows('/fr/admin/', 'UserAgent012'));
$this->assertTrue($robots->allows('/fr/admin?', 'UserAgent012'));
$this->assertTrue($robots->allows('/fr/admin?test', 'UserAgent012'));

$this->assertTrue($robots->allows('/fr/ad', 'UserAgent013'));
$this->assertTrue($robots->allows('/fr/admin', 'UserAgent013'));
$this->assertTrue($robots->allows('/fr/admin/', 'UserAgent013'));
$this->assertTrue($robots->allows('/fr/admin?', 'UserAgent013'));
$this->assertTrue($robots->allows('/fr/admin?test', 'UserAgent013'));

$this->assertTrue($robots->allows('/fr/ad', 'UserAgent014'));
$this->assertTrue($robots->allows('/fr/admin', 'UserAgent014'));
$this->assertTrue($robots->allows('/fr/admin/', 'UserAgent014'));
$this->assertTrue($robots->allows('/fr/admin?', 'UserAgent014'));
$this->assertTrue($robots->allows('/fr/admin?test', 'UserAgent014'));

$this->assertFalse($robots->allows('/fr/ad', 'UserAgent015'));
$this->assertFalse($robots->allows('/fr/admin', 'UserAgent015'));
$this->assertFalse($robots->allows('/fr/admin/', 'UserAgent015'));
$this->assertFalse($robots->allows('/fr/admin?', 'UserAgent015'));
$this->assertFalse($robots->allows('/fr/admin?test', 'UserAgent015'));
}
}
26 changes: 25 additions & 1 deletion tests/data/robots.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,28 @@ User-agent: google
Disallow: /
User-agent: UserAgent007

Disallow: /no-agents
Disallow: /no-agents

User-agent: UserAgent010
User-agent: UserAgent011
Disallow: /*?print
Disallow: /nl/admin/
Disallow: /en/admin/*
Disallow: /fr/admin$
Disallow: /es/admin-disallow/

User-agent: UserAgent012
User-agent: UserAgent013

User-agent: UserAgent014
Allow: /*?print
Disallow: /nl/admin/

Disallow: /en/admin/*
Allow: /fr/admin$

#comment
Disallow: /es/admin-disallow/

User-agent: UserAgent015
Disallow: /
2 changes: 1 addition & 1 deletion tests/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"author": "",
"license": "MIT",
"dependencies": {
"express": "^4.13.3"
"express": "^4.17.1"
}
}