Update http.js

This commit is contained in:
Namhyeon Go 2022-01-18 00:44:54 +09:00 committed by GitHub
parent ccc4b688b8
commit f3628b9887
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@
var FILE = require("lib/file");
var SHELL = require("lib/shell");
var RAND = require("lib/rand");
var BASE64 = require("lib/base64");
var HTTPObject = function(engine) {
this.interface = null;
@ -27,6 +28,12 @@ var HTTPObject = function(engine) {
this.cookie = null;
this.states = [];
this.variables = {
"uuidv4": RAND.uuidv4,
"base64json": function(v) {
return BASE64.encode(JSON.stringify(v));
}
};
this.create = function() {
if (this.engine == "MSXML") {
@ -271,11 +278,9 @@ var HTTPObject = function(engine) {
var parameters = {};
for (var k in this.parameters) {
if (url.indexOf(':' + k) > -1) {
url = url.replace(':' + k, this.parameters[k]);
} else if(this.parameters[k] == "{uuidv4}") {
parameters[k] = RAND.uuidv4(); // Generate UUID v4
url = url.replace(':' + k, this.evaluate(this.parameters[k]));
} else {
parameters[k] = this.parameters[k];
parameters[k] = this.evaluate(this.parameters[k]);
}
}
@ -335,7 +340,7 @@ var HTTPObject = function(engine) {
try {
if (this.engine == "MSXML") {
for (var key in this.headers) {
this.interface.setRequestHeader(key, this.headers[key]);
this.interface.setRequestHeader(key, this.evaluate(this.headers[key]));
}
switch (this.method) {
@ -361,17 +366,17 @@ var HTTPObject = function(engine) {
if (Object.keys(this.headers).length > 0) {
for (var key in this.headers) {
var value = this.headers[key];
var value = this.evaluate(this.headers[key]);
if (value != null) {
cmd.push("-H");
cmd.push(key + ": " + value);
}
}
}
if (this.cookie != null) {
cmd.push("-b");
cmd.push(this.cookie);
cmd.push(this.evaluate(this.cookie));
}
cmd.push("-A");
@ -382,7 +387,7 @@ var HTTPObject = function(engine) {
cmd.push("-d");
cmd.push(this.requestBody);
}
// Add proxy: <[protocol://][user:password@]proxyhost[:port]>
if (this.proxy != null && this.proxy.enabled) {
cmd.push("-x");
@ -507,6 +512,64 @@ var HTTPObject = function(engine) {
this.popState = function() {
return this.states.pop();
};
this.setVariable = function(k, v) {
this.variables[k] = v;
};
this.setVariables = function(variables) {
try {
var variables = (typeof(variables) !== "undefined") ? variables : {};
for (var k in variables) this.setVariable(k, variables[k]);
} catch (e) {
console.error("HTTPObject.setVariables() -> ", e.message);
}
return this;
};
this.evaluate = function(str) {
var str = String(str);
var Lpos = str.indexOf('{');
var Rpos = str.indexOf('}', Lpos + 1);
var s1 = [], s2 = null, s3, s4;
console.log("Before evaluated:", str);
while (Lpos > -1 && Rpos > -1) {
s1 = str.substring(Lpos + 1, Rpos).split(' ');
while (s1.length > 0) {
s3 = s1.pop();
console.log("*", s3);
if (s3 in this.variables) {
switch (typeof(this.variables[s3])) {
case "function":
s2 = this.variables[s3](s2);
break;
case "object":
s4 = this.variables[s3];
for (var k in s4) s4[k] = this.evaluate(s4[k]);
s2 = s4;
break;
default:
s2 = this.variables[s3];
}
}
}
str = str.substring(0, Lpos) + s2 + str.substring(Rpos + 1);
Lpos = str.indexOf('{');
Rpos = str.indexOf('}', Lpos + 1);
}
console.log("After evaluated:", str);
return str;
};
this.create();
};