mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-09 05:06:04 +00:00
Update http.js
This commit is contained in:
parent
5a2bf30b38
commit
b93cac5e76
56
lib/http.js
56
lib/http.js
|
@ -27,7 +27,7 @@ var HTTPObject = function(engine) {
|
||||||
"protocol": "http",
|
"protocol": "http",
|
||||||
"host": "127.0.0.1",
|
"host": "127.0.0.1",
|
||||||
"port": 80,
|
"port": 80,
|
||||||
"credential": null // { username: "user", password: "pass" }
|
"credential": null // { username: "user", password: "pass" }
|
||||||
};
|
};
|
||||||
this.engine = (typeof(engine) !== "undefined" ? engine : "MSXML");
|
this.engine = (typeof(engine) !== "undefined" ? engine : "MSXML");
|
||||||
|
|
||||||
|
@ -35,14 +35,14 @@ var HTTPObject = function(engine) {
|
||||||
this.states = [];
|
this.states = [];
|
||||||
this.variables = {
|
this.variables = {
|
||||||
"uuidv4": RAND.uuidv4,
|
"uuidv4": RAND.uuidv4,
|
||||||
"base64json": function(v) { // e.g. {base64json VARIABLE_NAME}
|
"base64json": function(v) { // e.g. {base64json VARIABLE_NAME}
|
||||||
return BASE64.encode(JSON.stringify(v));
|
return BASE64.encode(JSON.stringify(v));
|
||||||
},
|
},
|
||||||
"unixnow": function(diff) { // e.g. {unixnow -300} (seconds)
|
"unixnow": function(diff) { // e.g. {unixnow -300} (seconds)
|
||||||
var t = parseInt(diff);
|
var t = parseInt(diff);
|
||||||
return Math.floor(new Date().getTime() / 1000) - t;
|
return Math.floor(new Date().getTime() / 1000) - t;
|
||||||
},
|
},
|
||||||
"unixnowms": function(diff) { // e.g. {unixnowms -300000} (milliseconds)
|
"unixnowms": function(diff) { // e.g. {unixnowms -300000} (milliseconds)
|
||||||
var t = parseInt(diff);
|
var t = parseInt(diff);
|
||||||
return Math.floor(new Date().getTime()) - t;
|
return Math.floor(new Date().getTime()) - t;
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ var HTTPObject = function(engine) {
|
||||||
this.isLoggingCookie = false;
|
this.isLoggingCookie = false;
|
||||||
this.debuggingText = '';
|
this.debuggingText = '';
|
||||||
|
|
||||||
this.curlOptions = [];
|
this.curlOptions = [];
|
||||||
|
|
||||||
this.create = function() {
|
this.create = function() {
|
||||||
if (this.engine == "MSXML") {
|
if (this.engine == "MSXML") {
|
||||||
|
@ -129,7 +129,7 @@ var HTTPObject = function(engine) {
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.setProxy = function(proxy) {
|
this.setProxy = function(proxy) {
|
||||||
for (var k in proxy) {
|
for (var k in proxy) {
|
||||||
if (k in this.proxy) {
|
if (k in this.proxy) {
|
||||||
|
@ -168,7 +168,7 @@ var HTTPObject = function(engine) {
|
||||||
this.headers[key] = value;
|
this.headers[key] = value;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.setCookie = function(cookie) {
|
this.setCookie = function(cookie) {
|
||||||
this.cookie = cookie;
|
this.cookie = cookie;
|
||||||
};
|
};
|
||||||
|
@ -270,7 +270,7 @@ var HTTPObject = function(engine) {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setUserAgent = function(agent) {
|
this.setUserAgent = function(agent) {
|
||||||
this.userAgent = agent;
|
this.userAgent = agent;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -329,9 +329,9 @@ var HTTPObject = function(engine) {
|
||||||
this.open = function(method, url) {
|
this.open = function(method, url) {
|
||||||
var url = this.serializeParameters(url);
|
var url = this.serializeParameters(url);
|
||||||
|
|
||||||
this.setMethod(method.toUpperCase()); // set method
|
this.setMethod(method.toUpperCase()); // set method
|
||||||
this.pushState(null, null, url); // push state
|
this.pushState(null, null, url); // push state
|
||||||
this.setHeader("User-Agent", (this.userAgent != null ? this.evaluate(this.userAgent) : '')); // user agent
|
this.setHeader("User-Agent", (this.userAgent != null ? this.evaluate(this.userAgent) : '')); // user agent
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (this.engine == "MSXML") {
|
if (this.engine == "MSXML") {
|
||||||
|
@ -370,7 +370,7 @@ var HTTPObject = function(engine) {
|
||||||
for (var key in this.headers) {
|
for (var key in this.headers) {
|
||||||
this.interface.setRequestHeader(key, this.evaluate(this.headers[key]));
|
this.interface.setRequestHeader(key, this.evaluate(this.headers[key]));
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (this.method) {
|
switch (this.method) {
|
||||||
case "GET":
|
case "GET":
|
||||||
this.interface.send();
|
this.interface.send();
|
||||||
|
@ -395,7 +395,7 @@ var HTTPObject = function(engine) {
|
||||||
if (this.isDebugging) {
|
if (this.isDebugging) {
|
||||||
cmd.push("-v");
|
cmd.push("-v");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isFollowRedirect) {
|
if (this.isFollowRedirect) {
|
||||||
cmd.push("-L");
|
cmd.push("-L");
|
||||||
}
|
}
|
||||||
|
@ -425,7 +425,7 @@ var HTTPObject = function(engine) {
|
||||||
|
|
||||||
cmd.push("-A");
|
cmd.push("-A");
|
||||||
cmd.push((this.userAgent != null ? this.evaluate(this.userAgent) : ''));
|
cmd.push((this.userAgent != null ? this.evaluate(this.userAgent) : ''));
|
||||||
|
|
||||||
// --connect-timeout
|
// --connect-timeout
|
||||||
if (this.connectTimeout > 0) {
|
if (this.connectTimeout > 0) {
|
||||||
cmd.push("--connect-timeout");
|
cmd.push("--connect-timeout");
|
||||||
|
@ -477,7 +477,7 @@ var HTTPObject = function(engine) {
|
||||||
cmd.push("-o");
|
cmd.push("-o");
|
||||||
cmd.push(this.saveTo);
|
cmd.push(this.saveTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the count of this.curlOptions greater than 0
|
// if the count of this.curlOptions greater than 0
|
||||||
if (this.curlOptions.length > 0) {
|
if (this.curlOptions.length > 0) {
|
||||||
cmd = cmd.concat(this.curlOptions);
|
cmd = cmd.concat(this.curlOptions);
|
||||||
|
@ -577,7 +577,7 @@ var HTTPObject = function(engine) {
|
||||||
console.error("HTTPObject.patch() ->", e.message);
|
console.error("HTTPObject.patch() ->", e.message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.pushState = function(state, title, url) {
|
this.pushState = function(state, title, url) {
|
||||||
this.states.push({
|
this.states.push({
|
||||||
"state": state,
|
"state": state,
|
||||||
|
@ -589,17 +589,16 @@ var HTTPObject = function(engine) {
|
||||||
this.popState = function() {
|
this.popState = function() {
|
||||||
return this.states.pop();
|
return this.states.pop();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.setVariable = function(k, v) {
|
this.setVariable = function(k, v) {
|
||||||
this.variables[k] = v;
|
this.variables[k] = v;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.setVariables = function(variables) {
|
this.setVariables = function(variables) {
|
||||||
try {
|
try {
|
||||||
var variables = (typeof(variables) !== "undefined") ? variables : {};
|
var variables = (typeof(variables) !== "undefined") ? variables : {};
|
||||||
for (var k in variables)
|
for (var k in variables)
|
||||||
this.setVariable(k, variables[k])
|
this.setVariable(k, variables[k]);
|
||||||
;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("HTTPObject.setVariables() ->", e.message);
|
console.error("HTTPObject.setVariables() ->", e.message);
|
||||||
}
|
}
|
||||||
|
@ -610,7 +609,10 @@ var HTTPObject = function(engine) {
|
||||||
var str = String(str);
|
var str = String(str);
|
||||||
var Lpos = str.indexOf('{');
|
var Lpos = str.indexOf('{');
|
||||||
var Rpos = str.indexOf('}', Lpos + 1);
|
var Rpos = str.indexOf('}', Lpos + 1);
|
||||||
var s0 = '', s1 = [], s2 = null, s3, s4;
|
var s0 = '',
|
||||||
|
s1 = [],
|
||||||
|
s2 = null,
|
||||||
|
s3, s4;
|
||||||
|
|
||||||
while (Lpos > -1 && Rpos > -1) {
|
while (Lpos > -1 && Rpos > -1) {
|
||||||
s0 = str.substring(Lpos + 1, Rpos);
|
s0 = str.substring(Lpos + 1, Rpos);
|
||||||
|
@ -652,17 +654,17 @@ var HTTPObject = function(engine) {
|
||||||
this.connectTimeout = seconds;
|
this.connectTimeout = seconds;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.setIsDebugging = function(flag) {
|
this.setIsDebugging = function(flag) {
|
||||||
this.isDebugging = flag;
|
this.isDebugging = flag;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.setCredential = function(cred) {
|
this.setCredential = function(cred) {
|
||||||
this.credential = cred;
|
this.credential = cred;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.setIsFollowRedirect = function(flag) {
|
this.setIsFollowRedirect = function(flag) {
|
||||||
this.isFollowRedirect = flag;
|
this.isFollowRedirect = flag;
|
||||||
return this;
|
return this;
|
||||||
|
@ -681,7 +683,7 @@ var HTTPObject = function(engine) {
|
||||||
var tagName = "script";
|
var tagName = "script";
|
||||||
var a = this.responseBody.indexOf('<' + tagName + ' ');
|
var a = this.responseBody.indexOf('<' + tagName + ' ');
|
||||||
var b = a < 0 ? -1 : this.responseBody.indexOf('</' + tagName + '>', a);
|
var b = a < 0 ? -1 : this.responseBody.indexOf('</' + tagName + '>', a);
|
||||||
|
|
||||||
while (a > -1 && b > -1) {
|
while (a > -1 && b > -1) {
|
||||||
var outerHTML = this.responseBody.substring(a, b + tagName.length + 3);
|
var outerHTML = this.responseBody.substring(a, b + tagName.length + 3);
|
||||||
var innerHTML = this.responseBody.substring(this.responseBody.indexOf('>', a), b);
|
var innerHTML = this.responseBody.substring(this.responseBody.indexOf('>', a), b);
|
||||||
|
@ -721,7 +723,7 @@ var HTTPObject = function(engine) {
|
||||||
if (typeof this.responseBody !== "string") {
|
if (typeof this.responseBody !== "string") {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
var urls = [];
|
var urls = [];
|
||||||
var response = this.responseBody;
|
var response = this.responseBody;
|
||||||
var pos = response.indexOf('<iframe ');
|
var pos = response.indexOf('<iframe ');
|
||||||
|
@ -750,7 +752,7 @@ var HTTPObject = function(engine) {
|
||||||
|
|
||||||
console.warn("Existing proxy settings will be reset.");
|
console.warn("Existing proxy settings will be reset.");
|
||||||
|
|
||||||
switch(_debugger) {
|
switch (_debugger) {
|
||||||
case "FIDDLER":
|
case "FIDDLER":
|
||||||
this.proxy = {
|
this.proxy = {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user