Skip to content

Commit 65f6a12

Browse files
authored
Pluggable robot rule parser (#1987)
* Don't trigger Mvn workflow for changes to the docs Signed-off-by: Julien Nioche <julien@digitalpebble.com> * Pluggable Robots Rule Parser Signed-off-by: Julien Nioche <julien@digitalpebble.com> * Format fix Signed-off-by: Julien Nioche <julien@digitalpebble.com> * Revert "Don't trigger Mvn workflow for changes to the docs" This reverts commit 9152167. * Added config to documentation Signed-off-by: Julien Nioche <julien@digitalpebble.com> --------- Signed-off-by: Julien Nioche <julien@digitalpebble.com>
1 parent 4a41fa9 commit 65f6a12

5 files changed

Lines changed: 30 additions & 3 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public abstract class AbstractHttpProtocol implements Protocol {
5858
private static final DateTimeFormatter ISO_INSTANT_FORMATTER =
5959
DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.of(ZoneOffset.UTC.toString()));
6060

61-
private org.apache.stormcrawler.protocol.HttpRobotRulesParser robots;
61+
private org.apache.stormcrawler.protocol.RobotRulesParser robots;
6262

6363
protected boolean skipRobots = false;
6464

@@ -121,7 +121,15 @@ public void configure(Config conf) {
121121
customHeaders.add(KeyValue.build(h));
122122
}
123123

124-
robots = new HttpRobotRulesParser(conf);
124+
String robotsParserImplementation =
125+
ConfUtils.getString(
126+
conf,
127+
"http.robots.parser.class",
128+
"org.apache.stormcrawler.protocol.HttpRobotRulesParser");
129+
robots =
130+
InitialisationUtil.initializeFromQualifiedName(
131+
robotsParserImplementation, RobotRulesParser.class);
132+
robots.setConf(conf);
125133
protocolMetadataPrefix =
126134
ConfUtils.getString(
127135
conf, ProtocolResponse.PROTOCOL_MD_PREFIX_PARAM, protocolMetadataPrefix);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class HttpRobotRulesParser extends RobotRulesParser {
4848

4949
private static final int MAX_NUM_REDIRECTS = 5;
5050

51-
HttpRobotRulesParser() {}
51+
public HttpRobotRulesParser() {}
5252

5353
public HttpRobotRulesParser(Config conf) {
5454
setConf(conf);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ public RobotRules(BaseRobotRules base) {
3535
this.base = base;
3636
}
3737

38+
/**
39+
* Returns the {@link BaseRobotRules} wrapped by this instance. {@link HttpRobotRulesParser}
40+
* always wraps the rules returned by {@link RobotRulesParser#parseRules} in a plain {@code
41+
* RobotRules} for content-length tracking (see {@link #getContentLengthFetched()}), so a custom
42+
* {@link RobotRulesParser} returning its own {@code BaseRobotRules} subclass (e.g. to expose
43+
* additional robots.txt directives) needs this accessor to unwrap it again.
44+
*/
45+
public BaseRobotRules getWrapped() {
46+
return base;
47+
}
48+
3849
@Override
3950
public boolean isAllowed(String url) {
4051
return base.isAllowed(url);

core/src/main/resources/crawler-default.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ config:
182182
# http.robots.content.limit: 524288 # 512 kiB
183183
http.robots.content.limit: -1 # default same as http.content.limit
184184

185+
# Implementation of RobotRulesParser used by the HTTP protocol implementations
186+
# to fetch and parse robots.txt. Override to plug in custom robots.txt
187+
# directive handling (e.g. parsing extensions not supported by crawler-commons).
188+
# The class must extend org.apache.stormcrawler.protocol.RobotRulesParser and
189+
# have a public no-arg constructor.
190+
http.robots.parser.class: "org.apache.stormcrawler.protocol.HttpRobotRulesParser"
191+
185192
# Guava caches used for the robots.txt directives
186193
robots.cache.spec: "maximumSize=10000,expireAfterWrite=6h"
187194
robots.error.cache.spec: "maximumSize=10000,expireAfterWrite=1h"

docs/src/main/asciidoc/configuration.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ implementation.
245245
| file.encoding | UTF-8 | Encoding for FileProtocol.
246246
| protocol.instances.num | 1 | Number of instances per protocol implementation.
247247
| http.protocol.versions | - | HTTP protocol versions in order of preference (h2, http/1.1, http/1.0, h2c). If empty, uses implementation defaults.
248+
| http.robots.parser.class | org.apache.stormcrawler.protocol.HttpRobotRulesParser | Implementation to use for parsing robots.txt files.
248249
| robots.cache.spec | maximumSize=10000,expireAfterWrite=6h | CacheBuilder configuration for robots cache.
249250
| robots.error.cache.spec | maximumSize=10000,expireAfterWrite=1h | CacheBuilder configuration for error cache.
250251
| okhttp.protocol.connection.pool.max.idle.connections | 5 | OkHttp maximum number of idle connections.

0 commit comments

Comments
 (0)