Skip to content

Commit d193e6c

Browse files
authored
#2026 - http.agent.name is dropped from the robots.txt match set when http.robots.agents is set (#2027)
* #2026 - http.agent.name is dropped from the robots.txt match set when http.robots.agents is set * #2026 - Always match http.agent.name against robots.txt, even when http.robots.agents is set
1 parent 725781f commit d193e6c

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

core/src/main/java/org/apache/stormcrawler/protocol/RobotRulesParser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ public void setConf(Config conf) {
141141
agentName);
142142
this.agentNames.add(agentName.toLowerCase(Locale.ROOT));
143143
} else {
144+
// our agent-string is always the first one we match against robots.txt
145+
agentNames.add(agentName);
144146
int index = 0;
145147
if ((agents.get(0)).equalsIgnoreCase(agentName)) {
146148
index++;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.stormcrawler.protocol;
19+
20+
import static java.nio.charset.StandardCharsets.UTF_8;
21+
22+
import crawlercommons.robots.BaseRobotRules;
23+
import java.util.Arrays;
24+
import java.util.List;
25+
import org.apache.storm.Config;
26+
import org.junit.jupiter.api.Assertions;
27+
import org.junit.jupiter.api.Test;
28+
29+
/**
30+
* Checks that the agent name advertised via {@code http.agent.name} is always part of the set of
31+
* names used to select the matching groups in a robots.txt, regardless of the content of {@code
32+
* http.robots.agents}.
33+
*/
34+
class RobotRulesParserAgentNamesTest {
35+
36+
private static final String ROBOTS_TXT =
37+
"User-agent: mybot\n"
38+
+ "Disallow: /restricted/\n"
39+
+ "\n"
40+
+ "User-agent: *\n"
41+
+ "Disallow: /\n";
42+
43+
private static RobotRulesParser parserWith(String agentName, String robotsAgents) {
44+
Config conf = new Config();
45+
conf.put("http.agent.name", agentName);
46+
if (robotsAgents != null) {
47+
conf.put("http.robots.agents", robotsAgents);
48+
}
49+
RobotRulesParser parser = new HttpRobotRulesParser();
50+
parser.setConf(conf);
51+
return parser;
52+
}
53+
54+
@Test
55+
void agentNameUsedWhenNoRobotsAgentsConfigured() {
56+
RobotRulesParser parser = parserWith("mybot", null);
57+
Assertions.assertEquals(List.of("mybot"), List.copyOf(parser.agentNames));
58+
}
59+
60+
@Test
61+
void agentNameKeptWhenListedFirst() {
62+
RobotRulesParser parser = parserWith("mybot", "mybot,mybot-uk");
63+
Assertions.assertEquals(
64+
Arrays.asList("mybot", "mybot-uk"),
65+
List.copyOf(parser.agentNames),
66+
"http.agent.name must be the first name matched against robots.txt");
67+
}
68+
69+
@Test
70+
void agentNameAddedWhenNotListedFirst() {
71+
RobotRulesParser parser = parserWith("mybot", "mybot-uk,other-bot");
72+
Assertions.assertTrue(
73+
parser.agentNames.contains("mybot"),
74+
"http.agent.name must be matched against robots.txt even when not listed first, but got "
75+
+ parser.agentNames);
76+
}
77+
78+
@Test
79+
void groupAddressedToAdvertisedAgentIsSelected() {
80+
RobotRulesParser parser = parserWith("mybot", "mybot,mybot-uk");
81+
BaseRobotRules rules =
82+
parser.parseRules(
83+
"http://example.com/robots.txt",
84+
ROBOTS_TXT.getBytes(UTF_8),
85+
"text/plain",
86+
parser.agentNames);
87+
// the "User-agent: mybot" group only disallows /restricted/, the wildcard group
88+
// disallows everything - if the advertised name is dropped we fall back to "*"
89+
Assertions.assertTrue(
90+
rules.isAllowed("http://example.com/index.html"),
91+
"fell back to the wildcard group instead of the group for 'mybot'");
92+
Assertions.assertFalse(rules.isAllowed("http://example.com/restricted/index.html"));
93+
}
94+
}

0 commit comments

Comments
 (0)