Update http.js

This commit is contained in:
Namhyeon Go 2022-01-13 16:21:20 +09:00 committed by GitHub
parent 66ad0e69af
commit 8f6859ceae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,8 +2,17 @@
// HTTP API // HTTP API
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
var FILE = require("lib/file"); var FILE = require("lib/file");
var SHELL = require("lib/shell");
var HTTPObject = function() { // UUID v4
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
var HTTPObject = function(engine) {
this.interface = null; this.interface = null;
this.contentType = "application/x-www-form-urlencoded"; this.contentType = "application/x-www-form-urlencoded";
this.requestBody = ""; this.requestBody = "";
@ -14,25 +23,33 @@ var HTTPObject = function() {
this.dataType = null; this.dataType = null;
this.userAgent = "WelsonJS/0.1.4-dev (https://github.com/gnh1201/welsonjs)"; this.userAgent = "WelsonJS/0.1.4-dev (https://github.com/gnh1201/welsonjs)";
this.isAsync = false; this.isAsync = false;
this.engine = (typeof(engine) !== "undefined" ? engine : "MSXML");
this.cookie = null;
this.states = [];
this.create = function() { this.create = function() {
this.interface = CreateObject([ if (this.engine == "MSXML") {
"Microsoft.XMLHTTP", this.interface = CreateObject([
"WinHttp.WinHttpRequest.5.1", "Microsoft.XMLHTTP",
"Msxml3.XMLHTTP", "WinHttp.WinHttpRequest.5.1",
"Msxml2.XMLHTTP", "Msxml3.XMLHTTP",
"Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP",
"Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.7.0",
"Msxml2.XMLHTTP.5.O", "Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.4.O", "Msxml2.XMLHTTP.5.O",
"Msxml2.XMLHTTP.3.O", "Msxml2.XMLHTTP.4.O",
"Msxml2.XMLHTTP.2.6", "Msxml2.XMLHTTP.3.O",
"Msxml2.ServerXMLHTTP", "Msxml2.XMLHTTP.2.6",
"Msxml2.ServerXMLHTTP.6.0", "Msxml2.ServerXMLHTTP",
"Msxml2.ServerXMLHTTP.5.0", "Msxml2.ServerXMLHTTP.6.0",
"Msxml2.ServerXMLHTTP.4.0", "Msxml2.ServerXMLHTTP.5.0",
"Msxml2.ServerXMLHTTP.3.0" "Msxml2.ServerXMLHTTP.4.0",
]); "Msxml2.ServerXMLHTTP.3.0"
]);
} else if (this.engine == "CURL") {
this.interface = SHELL.create();
}
return this; return this;
}; };
@ -52,7 +69,7 @@ var HTTPObject = function() {
try { try {
if (this.dataType === "json") { if (this.dataType === "json") {
return true; return true;
} else { } else if (this.engine == "MSXML") {
var headers = this.getHeaders(); var headers = this.getHeaders();
for (var key in headers) { for (var key in headers) {
var _k = key.toLowerCase(); var _k = key.toLowerCase();
@ -70,8 +87,18 @@ var HTTPObject = function() {
return false; return false;
}; };
this.setEngine = function(engine) {
if (typeof(engine) == "string") {
this.engine = engine.toUpperCase();
} else {
this.engine = engine;
}
return this;
};
this.setMethod = function(method) { this.setMethod = function(method) {
this.method = method; this.method = method;
return this;
}; };
this.setDataType = function(type) { this.setDataType = function(type) {
@ -99,14 +126,25 @@ var HTTPObject = function() {
return this; return this;
}; };
this.setCookie = function(cookie) {
this.cookie = cookie;
};
this.setHeaders = function(headers) { this.setHeaders = function(headers) {
try { try {
var headers = (typeof(headers) !== "undefined") ? headers : {}; var headers = (typeof(headers) !== "undefined") ? headers : {};
for (var key in headers) { for (var key in headers) {
var value = headers[key]; var value = headers[key];
this.setHeader(key, value);
if (key.toLowerCase() == "content-type") { switch (key.toUpperCase()) {
this.contentType = value.toLowerCase(); case "CONTENT-TYPE":
this.setContentType(value.toLowerCase());
break;
case "COOKIE":
this.setCookie(value);
break;
default:
this.setHeader(key, value);
} }
} }
} catch (e) { } catch (e) {
@ -191,6 +229,7 @@ var HTTPObject = function() {
this.setUserAgent = function(agent) { this.setUserAgent = function(agent) {
this.userAgent = agent; this.userAgent = agent;
this.setHeader("User-Agent", this.userAgent); this.setHeader("User-Agent", this.userAgent);
return this;
}; };
this.serialize = function() { this.serialize = function() {
@ -218,6 +257,12 @@ var HTTPObject = function() {
this.serializeParameters = function(url) { this.serializeParameters = function(url) {
console.log(Object.keys(this.parameters).join(",")); console.log(Object.keys(this.parameters).join(","));
// UUID v4
while (url.indexOf("{uuidv4}") > -1) {
url = url.replace("{uuidv4}", uuidv4());
}
// Bind parameters
if (Object.keys(this.parameters).length > 0) { if (Object.keys(this.parameters).length > 0) {
// Type 2 // Type 2
var parameters = {}; var parameters = {};
@ -245,23 +290,27 @@ var HTTPObject = function() {
}; };
this.open = function(method, url) { this.open = function(method, url) {
this.setMethod(method.toUpperCase()); var url = this.serializeParameters(url);
this.setMethod(method.toUpperCase()); // set method
this.pushState(null, null, url); // push state
try { try {
switch (this.method) { if (this.engine == "MSXML") {
case "POST": switch (this.method) {
this.interface.open(method, this.serializeParameters(url), this.isAsync); case "POST":
break; this.interface.open(method, url, this.isAsync);
break;
case "GET": case "GET":
this.interface.open(method, this.serializeParameters(url), this.isAsync); this.interface.open(method, url, this.isAsync);
break; break;
case "PATCH": default:
break; console.error("Not supported HTTP method: " + method);
}
default: } else {
console.error("Not supported HTTP method: " + method); console.log("Selected engine: " + this.engine);
} }
} catch (e) { } catch (e) {
console.error("HTTPObject.open() -> ", e.message); console.error("HTTPObject.open() -> ", e.message);
@ -271,37 +320,75 @@ var HTTPObject = function() {
}; };
this.send = function(callback) { this.send = function(callback) {
this.setHeader("Content-Type", this.contentType); var responseText = null;
if (this.contentType != null) {
this.setHeader("Content-Type", this.contentType);
}
try { try {
for (var key in this.headers) { if (this.engine == "MSXML") {
this.interface.setRequestHeader(key, this.headers[key]); for (var key in this.headers) {
this.interface.setRequestHeader(key, this.headers[key]);
}
switch (this.method) {
case "GET":
this.interface.send();
break;
default:
this.interface.send(this.serialize(this.requestBody));
}
// 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];
var cmd = ["bin\\curl", "-X", this.method];
if (this.headers.length > 0) {
for (var key in this.headers) {
var value = this.headers[key];
if (value != null) {
cmd.push("-H");
cmd.push(key + ": " + value);
}
}
}
if (this.cookie != null) {
cmd.push("-b");
cmd.push(this.cookie);
}
if (this.userAgent != null) {
cmd.push("-A");
cmd.push(this.userAgent);
}
cmd.push(state.url);
// Get response text
responseText = this.interface.exec(cmd);
}
} }
switch (this.method) { console.log("ResponseText: " + responseText);
case "GET":
this.interface.send();
break;
default:
this.interface.send(this.serialize(this.requestBody));
}
// Waiting a response
while (this.interface.readyState < 4) {
sleep(100);
}
console.log("ResponseText: " + this.interface.responseText);
if (this.isJSONResponse()) { if (this.isJSONResponse()) {
if (typeof(WScript) !== "undefined") { if (typeof(WScript) !== "undefined") {
JSON = {}; JSON = {};
FILE.includeFile("app/assets/js/json2.js"); FILE.includeFile("app/assets/js/json2.js");
} }
this.setResponseBody(JSON.parse(this.interface.responseText)); this.setResponseBody(JSON.parse(responseText));
} else { } else {
this.setResponseBody(this.interface.responseText); this.setResponseBody(responseText);
} }
if (typeof(callback) === "function") { if (typeof(callback) === "function") {
@ -372,12 +459,24 @@ var HTTPObject = function() {
} }
}; };
this.pushState = function(state, title, url) {
this.states.push({
"state": state,
"title": title,
"url": url
});
};
this.popState = function() {
return this.states.pop();
};
this.create(); this.create();
this.setUserAgent(this.userAgent); this.setUserAgent(this.userAgent);
}; };
exports.create = function() { exports.create = function(engine) {
return (new HTTPObject()); return (new HTTPObject(engine));
}; };
exports.post = function(url, data, headers, params) { exports.post = function(url, data, headers, params) {
@ -388,6 +487,6 @@ exports.get = function(url, data, headers) {
return (new HTTPObject()).setHeaders(headers).setParameters(data).setUseCache(false).get(url).responseBody; return (new HTTPObject()).setHeaders(headers).setParameters(data).setUseCache(false).get(url).responseBody;
}; };
exports.VERSIONINFO = "HTTP Lib (http.js) version 0.2"; exports.VERSIONINFO = "HTTP Lib (http.js) version 0.3";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;