Skip to content

Commit 7456c77

Browse files
committed
Merge pull request #1 from llaumgui/proxy-patch
Add support of HTTP_PROXY and http_proxy
2 parents 3cf159a + 7bc2852 commit 7456c77

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

lib/W3CValidator.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var pkg = require("../package.json");
2020
program.version(pkg.version)
2121
.usage("[options] <url>")
2222
.option("-l, --log", "log errors in a text file")
23+
.option("-m, --max <n>", "max URLs to crawl")
2324
.option("-q, --query", "consider query string")
2425
.option("-v, --verbose", "show error details")
2526
.parse(process.argv);
@@ -35,6 +36,7 @@ var W3CValidator = function(url, java_home) {
3536
this.invalid = 0;
3637
this.JAVA_HOME = java_home;
3738
this.logData = "";
39+
this.httpProxy = false;
3840

3941
this.uri = new URL(url);
4042
this.crawler = new Crawler(this.uri.host);
@@ -54,6 +56,22 @@ var W3CValidator = function(url, java_home) {
5456
this.uri.set("protocol", "http:");
5557
}
5658

59+
// Get proxy
60+
if (["127.0.0.1", "localhost"].indexOf(this.crawler.host) === -1) {
61+
if (process.env.HTTP_PROXY !== undefined) {
62+
this.httpProxy = URL(process.env.HTTP_PROXY);
63+
} else if (process.env.http_proxy !== undefined) {
64+
this.httpProxy = URL(process.env.http_proxy);
65+
}
66+
}
67+
68+
// Set Crawler proxy
69+
if (this.httpProxy !== false) {
70+
this.crawler.useProxy = true;
71+
this.crawler.proxyHostname = this.httpProxy.hostname;
72+
this.crawler.proxyPort = this.httpProxy.port;
73+
}
74+
5775
this.crawler.initialProtocol = this.uri.protocol.replace(":", "");
5876
this.crawler.userAgent = "Node/W3CValidator";
5977

@@ -75,12 +93,20 @@ var W3CValidator = function(url, java_home) {
7593

7694
W3CValidator.prototype.checkURL = function() {
7795
var url = this.chunk.pop();
96+
var javaOpts = ""
97+
98+
// Set JAVA Proxy
99+
if (this.httpProxy !== false) {
100+
javaOpts += `-Dhttp.proxyHost=${this.httpProxy.hostname} -Dhttp.proxyPort=${this.httpProxy.port}`
101+
}
78102

79103
var spinner = new Spinner(` ${url} %s`);
80104
spinner.start();
81105

106+
107+
82108
var vnuPath = path.join(__dirname, "../vnu/vnu.jar").replace(/\s/g, "\\ ");
83-
var child = exec(`java -jar ${vnuPath} --format json ${url}`, { env: { JAVA_HOME: this.JAVA_HOME }}, (error, stdout, stderr) => {
109+
var child = exec(`java ${javaOpts} -jar ${vnuPath} --format json ${url}`, { env: { JAVA_HOME: this.JAVA_HOME }}, (error, stdout, stderr) => {
84110
var errors = JSON.parse(stderr);
85111

86112
spinner.stop(true);
@@ -135,6 +161,10 @@ W3CValidator.prototype.create = function() {
135161

136162
this.crawler.on("fetchcomplete", (item) => {
137163
this.chunk.push(item.url);
164+
if (program.max && this.chunk.length >= program.max) {
165+
this.crawler.emit("complete");
166+
this.crawler.stop();
167+
}
138168
});
139169

140170
this.crawler.on("complete", () => {

0 commit comments

Comments
 (0)