From f3628b98873f6c80c5f96f10e2e20022b0760998 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Tue, 18 Jan 2022 00:44:54 +0900 Subject: [PATCH] Update http.js --- lib/http.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/lib/http.js b/lib/http.js index 382a887..6a027ee 100644 --- a/lib/http.js +++ b/lib/http.js @@ -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(); };