diff --git a/lib/http.js b/lib/http.js index 66c0bd2..62aba6d 100644 --- a/lib/http.js +++ b/lib/http.js @@ -34,6 +34,11 @@ var HTTPObject = function(engine) { return BASE64.encode(JSON.stringify(v)); } }; + this.connectTimeout = 0; + this.isDebugging = false; + this.credential = { + method: "" + }; this.create = function() { if (this.engine == "MSXML") { @@ -361,9 +366,16 @@ var HTTPObject = function(engine) { if (this.states.length > 0) { // Make CURL context var state = this.states[this.states.length - 1]; - var cmd = ["bin\\curl", "-X", this.method]; + var cmd = ["bin\\curl"]; var url = state.url; + if (this.isDebugging) { + cmd.push("-v"); + } + + cmd.push("-X"); + cmd.push(this.method); + if (Object.keys(this.headers).length > 0) { for (var key in this.headers) { var value = this.evaluate(this.headers[key]); @@ -381,6 +393,20 @@ var HTTPObject = function(engine) { cmd.push("-A"); cmd.push((this.userAgent != null ? this.userAgent : '')); + + // --connect-timeout + if (this.connectTimeout > 0) { + cmd.push("--connect-timeout"); + cmd.push(this.connectTimeout); + } + + // Add the credential parameters + switch (this.credential.method.toUpperCase()) { + case "BASIC": + cmd.push("-u"); + cmd.push(this.credential.username + ":" + this.credential.password); + break; + } // Add the request body if this is not GET method if (this.method !== "GET") { @@ -421,7 +447,7 @@ var HTTPObject = function(engine) { } } - console.log("ResponseText: " + responseText); + console.log("Received", responseText.length, "bytes"); if (this.isJSONResponse()) { if (typeof(WScript) !== "undefined") { @@ -437,11 +463,11 @@ var HTTPObject = function(engine) { try { callback(this.responseBody); } catch (e) { - console.log("callback of HTTPObject.send() -> ", e.message); + console.log("callback of HTTPObject.send() ->", e.message); } } } catch (e) { - console.error("HTTPObject.send() -> ", e.message); + console.error("HTTPObject.send() ->", e.message); } return this; @@ -451,7 +477,7 @@ var HTTPObject = function(engine) { try { return this.open("POST", url).send(callback); } catch (e) { - console.error("HTTPObject.post() -> ", e.message); + console.error("HTTPObject.post() ->", e.message); } }; @@ -474,7 +500,7 @@ var HTTPObject = function(engine) { return this.open("GET", url).send(callback); } } catch (e) { - console.error("HTTPObject.get() -> ", e.message); + console.error("HTTPObject.get() ->", e.message); } }; @@ -497,7 +523,7 @@ var HTTPObject = function(engine) { throw Error("PATCH does not supported on GUI mode"); } } catch (e) { - console.error("HTTPObject.patch() -> ", e.message); + console.error("HTTPObject.patch() ->", e.message); } }; @@ -524,7 +550,7 @@ var HTTPObject = function(engine) { this.setVariable(k, variables[k]) ; } catch (e) { - console.error("HTTPObject.setVariables() -> ", e.message); + console.error("HTTPObject.setVariables() ->", e.message); } return this; }; @@ -573,6 +599,21 @@ var HTTPObject = function(engine) { return str; }; + this.setConnectTimeout = function(seconds) { + this.connectTimeout = seconds; + return this; + }; + + this.setIsDebugging = function(flag) { + this.isDebugging = flag; + return this; + }; + + this.setCredential = function(cred) { + this.credential = cred; + return this; + }; + this.create(); };