Skip to content

Commit 995729d

Browse files
committed
Add support of HTTP_PROXY and http_proxy
1 parent 3cf159a commit 995729d

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

lib/W3CValidator.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var W3CValidator = function(url, java_home) {
3535
this.invalid = 0;
3636
this.JAVA_HOME = java_home;
3737
this.logData = "";
38+
this.httpProxy = false;
3839

3940
this.uri = new URL(url);
4041
this.crawler = new Crawler(this.uri.host);
@@ -54,6 +55,20 @@ var W3CValidator = function(url, java_home) {
5455
this.uri.set("protocol", "http:");
5556
}
5657

58+
// Get proxy
59+
if (process.env.HTTP_PROXY !== undefined) {
60+
this.httpProxy = URL(process.env.HTTP_PROXY);
61+
} else if (process.env.http_proxy !== undefined) {
62+
this.httpProxy = URL(process.env.http_proxy);
63+
}
64+
65+
// Set Crawler proxy
66+
if (this.httpProxy !== false) {
67+
this.crawler.useProxy = true;
68+
this.crawler.proxyHostname = this.httpProxy.hostname;
69+
this.crawler.proxyPort = this.httpProxy.port;
70+
}
71+
5772
this.crawler.initialProtocol = this.uri.protocol.replace(":", "");
5873
this.crawler.userAgent = "Node/W3CValidator";
5974

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

7691
W3CValidator.prototype.checkURL = function() {
7792
var url = this.chunk.pop();
93+
var javaOpts = ""
94+
95+
// Set JAVA Proxy
96+
if (this.httpProxy !== false) {
97+
javaOpts += `-Dhttp.proxyHost=${this.httpProxy.hostname} -Dhttp.proxyPort=${this.httpProxy.port}`
98+
}
7899

79100
var spinner = new Spinner(` ${url} %s`);
80101
spinner.start();
81102

103+
104+
82105
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) => {
106+
var child = exec(`java ${javaOpts} -jar ${vnuPath} --format json ${url}`, { env: { JAVA_HOME: this.JAVA_HOME }}, (error, stdout, stderr) => {
84107
var errors = JSON.parse(stderr);
85108

86109
spinner.stop(true);

0 commit comments

Comments
 (0)