welsonjs/lib/http.js

999 lines
31 KiB
JavaScript
Raw Normal View History

2023-12-11 07:54:21 +00:00
// http.js
// https://github.com/gnh1201/welsonjs
2022-05-30 11:09:09 +00:00
var SYS = require("lib/system");
2020-12-07 08:33:12 +00:00
var FILE = require("lib/file");
2022-01-13 07:21:20 +00:00
var SHELL = require("lib/shell");
2022-01-15 10:38:45 +00:00
var RAND = require("lib/rand");
2022-01-17 15:44:54 +00:00
var BASE64 = require("lib/base64");
2023-10-30 18:13:02 +00:00
var PipeIPC = require("lib/pipe-ipc");
2022-01-13 07:21:20 +00:00
2022-05-30 09:18:32 +00:00
var OS_NAME = SYS.getOS();
var OS_ARCH = SYS.getArch();
var DEVICE_UUID = SYS.getUUID();
2023-12-11 07:54:21 +00:00
var PROCESS_VERSION = SYS.getProcessVersion();
2022-05-30 09:18:32 +00:00
2022-01-13 07:21:20 +00:00
var HTTPObject = function(engine) {
2020-11-10 09:13:41 +00:00
this.interface = null;
2020-11-25 06:10:02 +00:00
this.contentType = "application/x-www-form-urlencoded";
2020-11-10 09:13:41 +00:00
this.requestBody = "";
this.responseBody = null;
2020-11-26 08:13:13 +00:00
this.method = "GET";
2020-11-10 09:13:41 +00:00
this.headers = {};
2020-11-25 06:10:02 +00:00
this.parameters = {};
2020-11-26 08:13:13 +00:00
this.dataType = null;
2023-12-11 07:54:21 +00:00
this.userAgent = "WelsonJS/0.2.7 (" + OS_NAME + "; " + OS_ARCH + "; " + PROCESS_VERSION + "; " + DEVICE_UUID + "; abuse@catswords.net)";
2022-06-09 03:03:23 +00:00
this.isAsynchronous = false;
2022-01-17 09:58:52 +00:00
this.proxy = {
"enabled": false,
"protocol": "http",
"host": "127.0.0.1",
"port": 80,
2022-05-30 11:19:46 +00:00
"credential": null // { username: "user", password: "pass" }
2022-01-17 09:58:52 +00:00
};
2022-01-13 07:21:20 +00:00
this.engine = (typeof(engine) !== "undefined" ? engine : "MSXML");
this.cookie = null;
2023-09-21 06:07:22 +00:00
this.storedCookie = PipeIPC.create("volatile");
2022-01-13 07:21:20 +00:00
this.states = [];
2022-01-17 15:44:54 +00:00
this.variables = {
"uuidv4": RAND.uuidv4,
2022-05-30 11:19:46 +00:00
"base64json": function(v) { // e.g. {base64json VARIABLE_NAME}
2022-01-17 15:44:54 +00:00
return BASE64.encode(JSON.stringify(v));
2022-04-04 06:11:07 +00:00
},
2022-05-30 11:19:46 +00:00
"unixnow": function(diff) { // e.g. {unixnow -300} (seconds)
2022-05-06 07:52:39 +00:00
var t = parseInt(diff);
return Math.floor(new Date().getTime() / 1000) - t;
2022-04-04 06:11:07 +00:00
},
2022-05-30 11:19:46 +00:00
"unixnowms": function(diff) { // e.g. {unixnowms -300000} (milliseconds)
2022-05-06 07:52:39 +00:00
var t = parseInt(diff);
2022-05-06 08:33:50 +00:00
return Math.floor(new Date().getTime()) - t;
2022-06-09 02:35:47 +00:00
},
"isotime": function() {
2022-06-09 02:35:59 +00:00
return new Date().toISOString();
2022-01-17 15:44:54 +00:00
}
};
2022-02-18 07:50:24 +00:00
this.connectTimeout = 0;
2022-09-22 05:34:28 +00:00
this.maxTime = 0;
2022-02-18 07:50:24 +00:00
this.isDebugging = false;
this.credential = {
2022-04-14 08:46:15 +00:00
method: "",
username: "",
password: ""
2022-02-18 07:50:24 +00:00
};
2022-04-14 08:46:15 +00:00
this.isFollowRedirect = true;
2022-05-02 14:56:14 +00:00
this.saveTo = null;
this.isLoggingCookie = false;
2022-05-04 06:52:15 +00:00
this.debuggingText = '';
2020-12-07 03:48:37 +00:00
2022-05-30 11:19:46 +00:00
this.curlOptions = [];
2022-05-30 11:09:09 +00:00
this.charset = FILE.CdoCharset.CdoUTF_8;
2022-09-22 05:34:28 +00:00
this.isUseCharsetDetector = false;
2020-11-10 09:13:41 +00:00
this.create = function() {
2022-01-13 07:21:20 +00:00
if (this.engine == "MSXML") {
this.interface = CreateObject([
"Microsoft.XMLHTTP",
"WinHttp.WinHttpRequest.5.1",
"Msxml3.XMLHTTP",
"Msxml2.XMLHTTP",
"Msxml2.XMLHTTP.7.0",
"Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.5.O",
"Msxml2.XMLHTTP.4.O",
"Msxml2.XMLHTTP.3.O",
"Msxml2.XMLHTTP.2.6",
"Msxml2.ServerXMLHTTP",
"Msxml2.ServerXMLHTTP.6.0",
"Msxml2.ServerXMLHTTP.5.0",
"Msxml2.ServerXMLHTTP.4.0",
"Msxml2.ServerXMLHTTP.3.0"
]);
} else if (this.engine == "CURL") {
this.interface = SHELL.create();
}
2020-11-10 09:13:41 +00:00
return this;
2020-11-26 08:13:13 +00:00
};
this.jqEnabled = function() {
return (typeof(window) !== "undefined" && typeof(window.jQuery) !== "undefined");
};
2022-06-09 02:54:32 +00:00
this.jqAjax = function(url, callback, onError) {
2022-06-09 05:00:59 +00:00
var options = {
2022-06-09 02:54:32 +00:00
type: this.method,
headers: this.headers,
url: this.serializeParameters(url),
2022-06-09 05:00:59 +00:00
//data: this.requestBody,
2022-06-09 02:54:32 +00:00
contentType: this.contentType,
success: callback,
2022-06-09 03:03:23 +00:00
async: this.isAsynchronous,
2022-06-09 02:54:32 +00:00
error: onError // (request, status, error)
2022-06-09 05:00:59 +00:00
};
if (["POST", "PUT", "PATCH"].indexOf(this.method) > -1) {
options['data'] = this.requestBody;
}
this.setResponseBody(window.jQuery.ajax(options).responseText);
2020-11-26 08:13:13 +00:00
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.isJSONRequest = function() {
return (this.contentType === "application/json");
};
this.isJSONResponse = function() {
2020-12-07 08:33:12 +00:00
try {
if (this.dataType === "json") {
return true;
2022-01-13 07:21:20 +00:00
} else if (this.engine == "MSXML") {
2020-12-07 08:33:12 +00:00
var headers = this.getHeaders();
for (var key in headers) {
var _k = key.toLowerCase();
var _v = headers[key].toLowerCase();
if (_k === "content-type" && _v === "application/json") {
this.dataType = "json";
return true;
}
2020-11-10 09:13:41 +00:00
}
}
2020-12-07 08:33:12 +00:00
} catch (e) {
console.error("HTTPObject.isJSONResponse() -> ", e.message);
2020-11-10 09:13:41 +00:00
}
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
return false;
};
2020-06-28 14:22:57 +00:00
2022-01-13 07:21:20 +00:00
this.setEngine = function(engine) {
if (typeof(engine) == "string") {
this.engine = engine.toUpperCase();
} else {
this.engine = engine;
}
return this;
};
2022-05-30 11:19:46 +00:00
2022-01-17 10:02:42 +00:00
this.setProxy = function(proxy) {
2022-01-17 10:07:08 +00:00
for (var k in proxy) {
if (k in this.proxy) {
this.proxy[k] = proxy[k];
}
}
2022-01-17 10:02:42 +00:00
return this;
};
2022-01-13 07:21:20 +00:00
2020-11-10 09:13:41 +00:00
this.setMethod = function(method) {
this.method = method;
2022-01-13 07:21:20 +00:00
return this;
2020-11-10 09:13:41 +00:00
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.setDataType = function(type) {
this.dataType = type;
return this;
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.setContentType = function(type) {
this.contentType = type;
return this;
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.setRequestBody = function(data) {
this.requestBody = data;
return this;
2020-06-28 14:22:57 +00:00
}
2020-11-10 09:13:41 +00:00
this.setResponseBody = function(data) {
this.responseBody = data;
return this;
2020-11-05 05:09:20 +00:00
}
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.setHeader = function(key, value) {
2020-11-12 03:16:31 +00:00
this.headers[key] = value;
2020-11-10 09:13:41 +00:00
return this;
};
2022-05-30 11:19:46 +00:00
2022-01-13 07:21:20 +00:00
this.setCookie = function(cookie) {
this.cookie = cookie;
};
2020-11-10 09:13:41 +00:00
this.setHeaders = function(headers) {
2020-12-07 08:33:12 +00:00
try {
var headers = (typeof(headers) !== "undefined") ? headers : {};
for (var key in headers) {
var value = headers[key];
2022-01-13 07:21:20 +00:00
switch (key.toUpperCase()) {
case "CONTENT-TYPE":
2022-06-08 09:15:05 +00:00
console.warn("Will be forget the previous CONTENT-TYPE");
2022-01-13 07:21:20 +00:00
this.setContentType(value.toLowerCase());
break;
case "COOKIE":
2022-06-08 09:15:05 +00:00
console.warn("Will be forget the previous COOKIE");
2022-01-13 07:21:20 +00:00
this.setCookie(value);
break;
default:
this.setHeader(key, value);
2020-12-07 08:33:12 +00:00
}
2020-11-10 09:13:41 +00:00
}
2020-12-07 08:33:12 +00:00
} catch (e) {
console.error("HTTPObject.setHeaders() -> ", e.message);
2020-11-10 09:13:41 +00:00
}
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
return this;
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.getHeader = function(key) {
2020-11-25 02:46:20 +00:00
try {
return this.interface.getResponseHeader(key);
} catch (e) {
2020-11-25 06:10:02 +00:00
console.error("HTTPObject.getHeader() -> ", e.message);
2020-11-25 02:46:20 +00:00
}
2020-11-10 09:13:41 +00:00
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.getHeaders = function() {
2020-11-25 02:46:20 +00:00
try {
var raw = this.interface.getAllResponseHeaders();
return raw.split(/[\r\n]+/).filter(function(s) {
return s.trim().length > 0;
}).map(function(s) {
return s.trim().split(": ");
}).reduce(function(acc, c) {
acc[c[0].trim()] = c[1].trim();
return acc;
}, {});
} catch (e) {
2020-11-25 06:10:02 +00:00
console.error("HTTPObject.getHeaders() -> ", e.message);
2020-11-25 02:46:20 +00:00
}
2020-11-10 09:13:41 +00:00
};
2020-11-05 05:32:27 +00:00
2020-11-25 06:10:02 +00:00
this.setParameter = function(key, value) {
this.parameters[key] = value;
return this;
};
this.getParameter = function(key) {
return this.parameters[key];
};
this.setParameters = function(params) {
2020-12-07 08:33:12 +00:00
try {
var params = (typeof(params) !== "undefined") ? params : {};
for (var key in params) {
var value = params[key];
this.setParameter(key, value);
}
} catch (e) {
console.error("HTTPObject.setParameters() -> ", e.message);
2020-11-25 06:10:02 +00:00
}
return this;
};
this.getParameters = function() {
return this.parameters;
};
2020-11-12 03:16:31 +00:00
this.setBearerAuth = function(token) {
this.setHeader("Authorization", "Bearer " + token);
return this;
};
this.setUseCache = function(flag) {
if (flag === false) {
this.setHeaders({
//"Pragma": "no-cache",
//"Cache-Control": "no-cache",
"If-Modified-Since": "Sat, 1 Jan 2000 00:00:00 GMT"
});
}
return this;
};
2022-06-09 03:03:23 +00:00
this.setIsAsynchronous = function(flag) {
this.isAsynchronous = flag;
2020-11-26 08:13:13 +00:00
return this;
}
2020-11-12 06:39:50 +00:00
this.setUserAgent = function(agent) {
2022-05-30 11:19:46 +00:00
this.userAgent = agent;
2022-01-13 07:21:20 +00:00
return this;
2020-11-12 06:39:50 +00:00
};
2020-11-25 06:10:02 +00:00
this.serialize = function() {
if (this.isJSONRequest() && typeof(this.requestBody) === "object") {
return JSON.stringify(this.requestBody);
2022-05-12 16:55:39 +00:00
} else if (typeof(this.requestBody) === "object") {
return this.serializeURL(this.evaluate(this.requestBody));
2020-11-25 06:10:02 +00:00
} else {
2022-05-12 16:51:55 +00:00
return this.evaluate(this.requestBody);
2020-11-25 06:10:02 +00:00
}
};
2022-01-17 09:44:31 +00:00
this.serializeURL = function(parameters) {
var s = [];
for (var k in parameters) {
if (parameters.hasOwnProperty(k)) {
2022-05-12 16:51:55 +00:00
s.push(encodeURIComponent(k) + "=" + encodeURIComponent(this.evaluate(parameters[k])));
2022-01-03 04:17:00 +00:00
}
2020-11-25 06:10:02 +00:00
}
2022-01-17 09:44:31 +00:00
return s.join("&");
2020-11-25 06:10:02 +00:00
};
2022-01-03 04:17:00 +00:00
// Type 1: http://domain?a=1&b=2&c=3
// Type 2: http://domain/:a/:b/:c
2020-11-25 06:10:02 +00:00
this.serializeParameters = function(url) {
console.log(Object.keys(this.parameters).join(","));
2022-01-13 07:21:20 +00:00
// Bind parameters
2020-11-25 06:10:02 +00:00
if (Object.keys(this.parameters).length > 0) {
2022-01-03 04:17:00 +00:00
// Type 2
var parameters = {};
for (var k in this.parameters) {
if (url.indexOf(':' + k) > -1) {
2022-01-17 15:44:54 +00:00
url = url.replace(':' + k, this.evaluate(this.parameters[k]));
2022-01-13 07:59:30 +00:00
} else {
2022-01-17 15:44:54 +00:00
parameters[k] = this.evaluate(this.parameters[k]);
2022-01-03 04:17:00 +00:00
}
}
// Type 1
if (Object.keys(parameters).length > 0) {
if (url.indexOf('?') > -1) {
url += '&' + this.serializeURL(parameters);
} else {
url += '?' + this.serializeURL(parameters);
}
2020-11-25 06:10:02 +00:00
}
}
console.log("Requested URL: " + url);
return url;
2020-11-25 06:10:02 +00:00
};
2020-11-26 08:13:13 +00:00
this.open = function(method, url) {
2022-01-17 09:45:33 +00:00
var url = this.serializeParameters(url);
2022-05-30 11:19:46 +00:00
this.setMethod(method.toUpperCase()); // set method
this.pushState(null, null, url); // push state
this.setHeader("User-Agent", (this.userAgent != null ? this.evaluate(this.userAgent) : '')); // user agent
2020-06-28 14:22:57 +00:00
2022-01-13 07:21:20 +00:00
try {
if (this.engine == "MSXML") {
2022-04-04 06:11:07 +00:00
// Open
2022-01-13 07:21:20 +00:00
switch (this.method) {
case "POST":
2022-06-09 03:03:23 +00:00
this.interface.open(method, url, this.isAsynchronous);
2022-01-13 07:21:20 +00:00
break;
case "GET":
2022-06-09 03:03:23 +00:00
this.interface.open(method, url, this.isAsynchronous);
2022-01-13 07:21:20 +00:00
break;
default:
console.warn("Switching the engine to cURL. Not supported method in MSXML: " + method);
this.setEngine("CURL");
console.log("Opened engine:", this.engine);
2022-01-13 07:21:20 +00:00
}
} else {
2022-01-13 08:50:25 +00:00
console.log("Opened engine:", this.engine);
2020-11-25 02:46:20 +00:00
}
} catch (e) {
2022-01-13 08:50:25 +00:00
console.error("HTTPObject.open() ->", e.message);
2020-11-10 09:13:41 +00:00
}
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
return this;
};
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
this.send = function(callback) {
2022-01-13 07:21:20 +00:00
var responseText = null;
2022-09-22 05:34:28 +00:00
var debuggingText = null;
2022-01-13 07:21:20 +00:00
// [lib/http] cURL error with non-escaped ampersand on Command Prompt #103
var replaceAndExcludeCaretAnd = function(inputString) {
var result = "";
var i = 0;
while (i < inputString.length) {
// If the found position is ^&, do not modify and add it as is to the result
if (i < inputString.length - 1 && inputString.slice(i, i + 2) === "^&") {
result += inputString.slice(i, i + 2);
i += 2;
} else {
// Replace & with ^&
if (inputString.charAt(i) === "&") {
result += "^&";
} else {
result += inputString.charAt(i);
}
i++;
}
}
return result;
};
2022-01-13 07:21:20 +00:00
if (this.contentType != null) {
this.setHeader("Content-Type", this.contentType);
}
2020-06-28 14:22:57 +00:00
2020-11-25 02:46:20 +00:00
try {
2022-01-13 07:21:20 +00:00
if (this.engine == "MSXML") {
for (var key in this.headers) {
2022-01-17 15:44:54 +00:00
this.interface.setRequestHeader(key, this.evaluate(this.headers[key]));
2022-01-13 07:21:20 +00:00
}
2022-05-30 11:19:46 +00:00
2022-01-13 07:21:20 +00:00
switch (this.method) {
case "GET":
this.interface.send();
break;
default:
2022-05-12 16:55:39 +00:00
this.interface.send(this.serialize());
2022-01-13 07:21:20 +00:00
}
2020-11-10 09:13:41 +00:00
2022-01-13 07:21:20 +00:00
// Waiting a response
while (this.interface.readyState < 4) sleep(100);
// Get response text
responseText = this.interface.responseText;
} else if (this.engine == "CURL") {
if (this.states.length > 0) {
// Make CURL context
var state = this.states[this.states.length - 1];
2022-02-18 07:50:24 +00:00
var cmd = ["bin\\curl"];
2022-01-13 07:59:30 +00:00
var url = state.url;
2022-01-13 07:21:20 +00:00
2022-02-18 07:50:24 +00:00
if (this.isDebugging) {
cmd.push("-v");
}
2022-05-30 11:19:46 +00:00
2022-04-14 08:46:15 +00:00
if (this.isFollowRedirect) {
cmd.push("-L");
}
2022-02-18 07:50:24 +00:00
cmd.push("-X");
cmd.push(this.method);
2022-01-13 07:29:01 +00:00
if (Object.keys(this.headers).length > 0) {
2022-01-13 07:21:20 +00:00
for (var key in this.headers) {
2022-01-17 15:44:54 +00:00
var value = this.evaluate(this.headers[key]);
2022-01-13 07:21:20 +00:00
if (value != null) {
cmd.push("-H");
cmd.push(key + ": " + value);
}
}
}
2022-01-17 15:44:54 +00:00
2022-01-13 07:21:20 +00:00
if (this.cookie != null) {
cmd.push("-b");
2022-01-17 15:44:54 +00:00
cmd.push(this.evaluate(this.cookie));
2022-01-13 07:21:20 +00:00
}
2022-01-13 08:46:57 +00:00
2022-05-02 14:56:14 +00:00
if (this.isLoggingCookie) {
cmd.push("-c");
2023-09-21 06:07:22 +00:00
cmd.push(this.storedCookie.path);
2022-05-02 14:56:14 +00:00
}
2022-01-13 08:47:20 +00:00
cmd.push("-A");
2022-05-20 09:44:41 +00:00
cmd.push((this.userAgent != null ? this.evaluate(this.userAgent) : ''));
2022-05-30 11:19:46 +00:00
2022-02-18 07:50:24 +00:00
// --connect-timeout
if (this.connectTimeout > 0) {
cmd.push("--connect-timeout");
cmd.push(this.connectTimeout);
}
2022-09-22 05:34:28 +00:00
// --max-time
if (this.maxTime > 0) {
cmd.push("--max-time");
cmd.push(this.maxTime);
}
2022-02-18 07:50:24 +00:00
2022-04-04 06:11:07 +00:00
// Add the credential parameters
2022-02-18 07:50:24 +00:00
switch (this.credential.method.toUpperCase()) {
case "BASIC":
cmd.push("-u");
cmd.push(this.credential.username + ":" + this.credential.password);
break;
}
2022-01-13 07:21:20 +00:00
2022-01-17 09:44:31 +00:00
// Add the request body if this is not GET method
if (this.method !== "GET") {
2022-01-13 07:59:30 +00:00
cmd.push("-d");
cmd.push(replaceAndExcludeCaretAnd(this.serialize()));
2022-01-13 07:59:30 +00:00
}
2022-01-17 15:44:54 +00:00
2022-01-17 09:58:52 +00:00
// Add proxy: <[protocol://][user:password@]proxyhost[:port]>
if (this.proxy != null && this.proxy.enabled) {
cmd.push("-x");
if (this.proxy.credential != null) {
cmd.push([
this.proxy.protocol,
"://",
this.proxy.credential.username,
":",
this.proxy.credential.password,
"@",
this.proxy.host,
":",
this.proxy.port
].join(""));
} else {
cmd.push([
this.proxy.protocol,
"://",
this.proxy.host,
":",
this.proxy.port
].join(""));
}
}
2022-01-17 09:44:31 +00:00
2022-04-20 05:02:11 +00:00
// if it is download
2022-05-02 14:56:14 +00:00
if (this.saveTo != null) {
cmd.push("-o");
cmd.push(this.saveTo);
}
2022-05-30 11:19:46 +00:00
2022-05-30 11:09:09 +00:00
// if the count of this.curlOptions greater than 0
if (this.curlOptions.length > 0) {
cmd = cmd.concat(this.curlOptions);
}
2022-04-20 05:02:11 +00:00
// set the URL
2022-01-17 09:24:33 +00:00
cmd.push(state.url);
2022-01-13 07:21:20 +00:00
// Get response text
2022-09-22 05:34:28 +00:00
responseText = this.interface.setCharset(this.charset).exec(cmd);
2023-09-21 06:07:22 +00:00
// Reload a cookie in the pipe
if (this.isLoggingCookie) {
this.storedCookie.reload();
}
2022-09-22 05:34:28 +00:00
// If enabled the charset(text encoding) detector
if (this.isUseCharsetDetector) {
var detectedCharset = this.detectCharset(responseText);
console.log("Detected charset:", detectedCharset);
if (detectedCharset != null && this.charset != detectedCharset) {
var _interface = SHELL.create();
responseText = _interface.setCharset(detectedCharset).exec(cmd);
2023-09-21 01:59:51 +00:00
debuggingText = _interface.stderr.read();
2022-09-22 05:34:28 +00:00
}
}
2022-05-04 06:52:15 +00:00
// Get debuging text
2023-09-21 01:59:51 +00:00
debuggingText = this.interface.stderr.read();
2022-01-13 07:21:20 +00:00
}
2020-12-07 03:48:37 +00:00
}
2022-04-04 06:11:07 +00:00
if (typeof responseText === "string") {
console.log("Received", responseText.length, "bytes");
} else {
console.log("No received anything");
}
2022-09-22 05:34:28 +00:00
if (this.isDebugging && typeof debuggingText === "string") {
this.debuggingText = debuggingText;
console.log("Created debugging text", debuggingText.length, "bytes");
2022-05-30 12:29:38 +00:00
} else {
console.log("No debugging text");
}
2020-11-25 02:46:20 +00:00
if (this.isJSONResponse()) {
2020-12-07 08:33:12 +00:00
if (typeof(WScript) !== "undefined") {
JSON = {};
FILE.includeFile("app/assets/js/json2.js");
}
2022-01-13 07:21:20 +00:00
this.setResponseBody(JSON.parse(responseText));
2020-11-25 02:46:20 +00:00
} else {
2022-01-13 07:21:20 +00:00
this.setResponseBody(responseText);
2020-11-25 02:46:20 +00:00
}
2020-11-10 09:13:41 +00:00
2020-11-25 02:46:20 +00:00
if (typeof(callback) === "function") {
2020-12-07 08:33:12 +00:00
try {
callback(this.responseBody);
} catch (e) {
2022-02-18 07:50:24 +00:00
console.log("callback of HTTPObject.send() ->", e.message);
2020-12-07 08:33:12 +00:00
}
2020-11-25 02:46:20 +00:00
}
} catch (e) {
2022-02-18 07:50:24 +00:00
console.error("HTTPObject.send() ->", e.message);
2020-11-10 09:13:41 +00:00
}
2020-06-28 14:22:57 +00:00
2020-11-10 09:13:41 +00:00
return this;
};
2022-06-09 02:57:02 +00:00
this.get = function(url, callback, onError) {
2020-11-26 08:13:13 +00:00
try {
2022-06-09 02:54:32 +00:00
if (this.jqEnabled()) {
2022-06-09 02:57:02 +00:00
this.setMethod("GET");
2022-06-09 02:54:32 +00:00
this.jqAjax(url, callback, onError);
2022-06-09 03:14:24 +00:00
return this;
2022-06-09 02:54:32 +00:00
} else {
2022-06-09 02:57:02 +00:00
return this.open("GET", url).send(callback);
2022-06-09 02:54:32 +00:00
}
2020-11-26 08:13:13 +00:00
} catch (e) {
2022-06-09 02:54:32 +00:00
if (typeof onError === "function") onError(this, null, e);
2020-11-26 08:13:13 +00:00
}
2020-11-10 09:13:41 +00:00
};
2022-06-09 02:57:02 +00:00
this.post = function(url, callback, onError) {
2020-11-26 08:13:13 +00:00
try {
2022-01-03 04:17:00 +00:00
if (this.jqEnabled()) {
2022-06-09 02:57:02 +00:00
this.setMethod("POST");
2022-06-09 02:54:32 +00:00
this.jqAjax(url, callback, onError);
2022-06-09 03:14:24 +00:00
return this;
2022-01-03 04:17:00 +00:00
} else {
2022-06-09 02:57:02 +00:00
return this.open("POST", url).send(callback);
2022-01-03 04:17:00 +00:00
}
2020-11-26 08:13:13 +00:00
} catch (e) {
2022-06-09 02:54:32 +00:00
if (typeof onError === "function") onError(this, null, e);
2020-11-26 08:13:13 +00:00
}
2020-11-10 09:13:41 +00:00
};
2022-06-09 02:54:32 +00:00
this.patch = function(url, callback, onError) {
2020-11-25 02:46:20 +00:00
try {
2020-11-26 08:13:13 +00:00
if (this.jqEnabled()) {
2022-06-09 02:54:32 +00:00
this.setMethod("PATCH");
this.jqAjax(url, callback, onError);
2022-06-09 03:14:24 +00:00
return this;
2020-11-12 04:36:16 +00:00
} else {
2022-06-09 02:54:32 +00:00
return this.open("PATCH", url).send(callback);
2020-11-12 04:36:16 +00:00
}
2020-11-25 02:46:20 +00:00
} catch (e) {
2022-06-09 02:54:32 +00:00
if (typeof onError === "function") onError(this, null, e);
2022-06-09 02:57:02 +00:00
}
};
this.put = function(url, callback, onError) {
try {
if (this.jqEnabled()) {
this.setMethod("PUT");
this.jqAjax(url, callback, onError);
2022-06-09 03:14:24 +00:00
return this;
2022-06-09 02:57:02 +00:00
} else {
return this.open("PUT", url).send(callback);
}
} catch (e) {
if (typeof onError === "function") onError(this, null, e);
}
};
2022-06-09 05:06:54 +00:00
this._delete = function(url, callback, onError) {
2022-06-09 02:57:02 +00:00
try {
if (this.jqEnabled()) {
this.setMethod("DELETE");
this.jqAjax(url, callback, onError);
2022-06-09 03:14:24 +00:00
return this;
2022-06-09 02:57:02 +00:00
} else {
return this.open("DELETE", url).send(callback);
}
} catch (e) {
if (typeof onError === "function") onError(this, null, e);
2020-11-25 02:46:20 +00:00
}
2020-11-12 04:36:16 +00:00
};
2022-05-30 11:19:46 +00:00
2022-01-13 07:21:20 +00:00
this.pushState = function(state, title, url) {
this.states.push({
"state": state,
"title": title,
"url": url
});
};
this.popState = function() {
return this.states.pop();
};
2022-05-30 11:19:46 +00:00
2022-01-17 15:44:54 +00:00
this.setVariable = function(k, v) {
this.variables[k] = v;
};
2022-05-30 11:19:46 +00:00
2022-01-17 15:44:54 +00:00
this.setVariables = function(variables) {
try {
var variables = (typeof(variables) !== "undefined") ? variables : {};
2022-01-17 15:46:47 +00:00
for (var k in variables)
2022-05-30 11:19:46 +00:00
this.setVariable(k, variables[k]);
2022-01-17 15:44:54 +00:00
} catch (e) {
2022-02-18 07:50:24 +00:00
console.error("HTTPObject.setVariables() ->", e.message);
2022-01-17 15:44:54 +00:00
}
return this;
};
this.evaluate = function(str) {
2022-01-17 15:46:47 +00:00
var str = String(str);
2022-01-17 15:44:54 +00:00
var Lpos = str.indexOf('{');
var Rpos = str.indexOf('}', Lpos + 1);
2022-05-30 11:19:46 +00:00
var s0 = '',
s1 = [],
s2 = null,
s3, s4;
2022-01-17 15:44:54 +00:00
while (Lpos > -1 && Rpos > -1) {
2022-01-17 16:43:47 +00:00
s0 = str.substring(Lpos + 1, Rpos);
2022-05-03 04:18:39 +00:00
s2 = '';
s1 = s0.split(' ');
while (s1.length > 0) {
s3 = s1.pop();
if (s3 in this.variables) {
switch (typeof(this.variables[s3])) {
case "function":
s2 = this.variables[s3](s2);
break;
2022-01-17 15:44:54 +00:00
2022-05-03 04:18:39 +00:00
case "object":
s4 = this.variables[s3];
2022-05-06 07:52:39 +00:00
for (var k in s4) {
s4[k] = this.evaluate(s4[k]);
}
2022-05-03 04:18:39 +00:00
s2 = s4;
break;
default:
s2 = this.variables[s3];
2022-01-17 15:44:54 +00:00
}
2022-05-06 07:52:39 +00:00
} else {
s2 = s3;
2022-01-17 15:44:54 +00:00
}
}
2022-01-17 15:46:47 +00:00
str = str.substring(0, Lpos) + s2 + str.substring(Rpos + 1);
2022-01-17 15:44:54 +00:00
Lpos = str.indexOf('{');
Rpos = str.indexOf('}', Lpos + 1);
}
return str;
};
2020-11-12 04:36:16 +00:00
2022-02-18 07:50:24 +00:00
this.setConnectTimeout = function(seconds) {
this.connectTimeout = seconds;
return this;
};
2022-05-30 11:19:46 +00:00
2022-09-22 05:34:28 +00:00
this.setMaxTime = function(seconds) {
this.maxTime = seconds;
return this;
};
2022-02-18 07:50:24 +00:00
this.setIsDebugging = function(flag) {
this.isDebugging = flag;
return this;
};
2022-05-30 11:19:46 +00:00
2022-02-18 07:50:24 +00:00
this.setCredential = function(cred) {
this.credential = cred;
return this;
};
2022-05-30 11:19:46 +00:00
2022-05-03 04:13:52 +00:00
this.setIsFollowRedirect = function(flag) {
this.isFollowRedirect = flag;
2022-05-04 06:57:23 +00:00
return this;
2022-05-03 04:13:52 +00:00
};
2022-02-18 07:50:24 +00:00
2022-04-20 05:02:11 +00:00
this.setSaveTo = function(filename) {
this.saveTo = filename;
};
2022-04-25 08:49:10 +00:00
this.parseScripts = function() {
var scripts = [];
if (typeof this.responseBody !== "string")
return scripts;
var tagName = "script";
var a = this.responseBody.indexOf('<' + tagName + ' ');
var b = a < 0 ? -1 : this.responseBody.indexOf('</' + tagName + '>', a);
2022-05-30 11:19:46 +00:00
2022-04-25 08:49:10 +00:00
while (a > -1 && b > -1) {
var outerHTML = this.responseBody.substring(a, b + tagName.length + 3);
var innerHTML = this.responseBody.substring(this.responseBody.indexOf('>', a), b);
scripts.push({
'outerHTML': outerHTML,
'innerHTML': innerHTML
});
a = this.responseBody.indexOf('<' + tagName + ' ', b + tagName.length + 3);
b = a < 0 ? -1 : this.responseBody.indexOf('</' + tagName + '>', a);
}
return scripts;
};
2022-05-02 14:56:14 +00:00
this.setIsLoggingCookie = function(flag) {
this.isLoggingCookie = flag;
return this;
};
this.getAllCookies = function() {
var data = {};
2023-09-21 06:07:22 +00:00
var rows = splitLn(this.storedCookie.read());
2022-05-02 14:56:14 +00:00
for (var i = 0; i < rows.length; i++) {
var cols = rows[i].split("\t");
if (cols.length == 7) {
data[cols[5]] = cols[6];
}
}
return data;
};
2022-05-17 09:29:39 +00:00
this.getFrameURLs = function() {
if (typeof this.responseBody !== "string") {
return [];
2022-05-30 09:18:32 +00:00
}
2022-05-30 11:19:46 +00:00
2022-05-17 09:29:39 +00:00
var urls = [];
2022-05-30 09:18:32 +00:00
var response = this.responseBody;
var pos = response.indexOf('<iframe ');
2022-05-17 09:29:39 +00:00
while (pos > -1) {
2022-05-30 09:18:32 +00:00
var end = response.indexOf('</iframe>', pos);
2022-05-17 09:29:39 +00:00
2022-09-22 05:34:28 +00:00
if (end < 0) {
pos = response.indexOf('<iframe ', pos + 8);
2022-05-17 09:29:39 +00:00
continue;
2022-05-30 09:18:32 +00:00
}
2022-05-17 09:29:39 +00:00
var a = response.indexOf('src="', pos);
var b = response.indexOf('"', a + 5);
if (a > 0 && b > 0) {
urls.push(response.substring(a + 5, b));
}
pos = response.indexOf('<iframe ', pos + end);
}
return urls;
};
2022-05-30 09:18:32 +00:00
this.attachDebugger = function(_debugger) {
var _debugger = _debugger.toUpperCase();
console.warn("Existing proxy settings will be reset.");
2022-05-30 11:19:46 +00:00
switch (_debugger) {
2022-05-30 15:29:07 +00:00
case "FIDDLER": // Fiddler Classic
2022-05-30 11:09:09 +00:00
this.proxy = {
"enabled": true,
"protocol": "http",
"host": "127.0.0.1",
"port": 8888,
"credential": null
};
2022-05-30 11:19:15 +00:00
this.curlOptions.push("-k");
2022-05-30 11:09:09 +00:00
this.curlOptions.push("--ssl-no-revoke");
break;
2022-05-30 15:29:07 +00:00
case "FIDDLER2": // Fiddler Everywhere
this.proxy = {
"enabled": true,
"protocol": "http",
"host": "127.0.0.1",
"port": 8866,
"credential": null
};
2022-05-30 11:09:09 +00:00
this.curlOptions.push("-k");
this.curlOptions.push("--ssl-no-revoke");
2022-05-30 09:18:32 +00:00
break;
2022-05-30 11:09:09 +00:00
2022-05-30 15:29:07 +00:00
case "MITMPROXY": // mitmproxy
case "BURPSUITE": // Burp Suite
case "ZAP": // OWASP ZAP Zed Attack Proxy
2022-05-30 11:09:09 +00:00
this.proxy = {
"enabled": true,
"protocol": "http",
"host": "127.0.0.1",
"port": 8080,
"credential": null
};
2022-05-30 15:27:32 +00:00
this.curlOptions.push("-k");
this.curlOptions.push("--ssl-no-revoke");
2022-05-30 11:09:09 +00:00
break;
2022-05-30 09:18:32 +00:00
default:
this.proxy = {
"enabled": false,
"protocol": "http",
"host": "127.0.0.1",
"port": 80,
"credential": null
};
2022-05-30 09:18:32 +00:00
console.error("Not specified debugger");
}
return this;
};
2022-09-22 05:34:28 +00:00
this.setCharset = function(charset) {
this.charset = charset;
return this;
};
this.setIsUseCharsetDetector = function(flag) {
this.isUseCharsetDetector = flag;
return this;
};
this.detectCharset = function(content) {
var charset = null;
try {
var s = "charset=";
var pos = content.toLowerCase().indexOf(s);
if (pos > -1) {
var end = [
content.indexOf('"', pos + s.length),
content.indexOf('\'', pos + s.length),
content.indexOf(';', pos + s.length)
].reduce(function(a, x) {
if (a < 0 || x > 0 && x < a) a = x;
return a;
}, -1);
if (end > -1) {
var detectedCharset = content.substring(pos + s.length, end);
if (detectedCharset.length > 0 && detectedCharset.length < 16) {
charset = detectedCharset.toLowerCase();
}
}
}
} catch (e) {
charset = null;
}
return charset;
};
2023-03-09 02:38:56 +00:00
this.detectSSL = function() {
return (this.debuggingText.indexOf("certificate") > -1);
};
this.detectSSLCompleted = function() {
2023-03-09 02:42:55 +00:00
return this.detectSSL() && (this.debuggingText.indexOf("certificate problem") < 0);
2023-03-09 02:38:56 +00:00
};
2020-11-10 09:13:41 +00:00
this.create();
2020-06-28 14:22:57 +00:00
};
2022-06-09 05:06:54 +00:00
function create(engine) {
return new HTTPObject(engine);
}
2020-11-10 09:13:41 +00:00
2022-06-09 05:06:54 +00:00
function get(url, params, headers) {
return create().setHeaders(headers).setParameters(params).setUseCache(false).get(url).responseBody;
}
2020-11-12 05:02:25 +00:00
2022-06-09 05:06:54 +00:00
function post(url, data, headers, params) {
return create().setHeaders(headers).setRequestBody(data).setParameters(params).post(url).responseBody;
}
function patch(url, data, headers) {
return create().setHeaders(headers).setParameters(data).patch(url).responseBody;
}
function put(url, data, headers) {
return create().setHeaders(headers).setParameters(data).put(url).responseBody;
}
function _delete(url, params, headers) {
return create().setHeaders(headers).setParameters(params).setUseCache(false)._delete(url).responseBody;
}
2022-08-28 21:04:53 +00:00
exports.create = create;
2022-06-09 05:06:54 +00:00
exports.get = get;
exports.post = post;
exports.patch = patch;
exports.put = put;
exports._delete = _delete;
2020-11-12 05:02:25 +00:00
exports.VERSIONINFO = "HTTP request module (http.js) version 0.7.12";
2022-11-25 14:11:37 +00:00
exports.AUTHOR = "abuse@catswords.net";
2020-11-10 09:13:41 +00:00
exports.global = global;
2022-05-30 11:14:24 +00:00
exports.require = global.require;