mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-14 13:41:05 +00:00
Update http.js
This commit is contained in:
parent
8c6bd8985f
commit
6615efbd19
57
lib/http.js
57
lib/http.js
|
@ -34,6 +34,11 @@ var HTTPObject = function(engine) {
|
||||||
return BASE64.encode(JSON.stringify(v));
|
return BASE64.encode(JSON.stringify(v));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
this.connectTimeout = 0;
|
||||||
|
this.isDebugging = false;
|
||||||
|
this.credential = {
|
||||||
|
method: ""
|
||||||
|
};
|
||||||
|
|
||||||
this.create = function() {
|
this.create = function() {
|
||||||
if (this.engine == "MSXML") {
|
if (this.engine == "MSXML") {
|
||||||
|
@ -361,9 +366,16 @@ var HTTPObject = function(engine) {
|
||||||
if (this.states.length > 0) {
|
if (this.states.length > 0) {
|
||||||
// Make CURL context
|
// Make CURL context
|
||||||
var state = this.states[this.states.length - 1];
|
var state = this.states[this.states.length - 1];
|
||||||
var cmd = ["bin\\curl", "-X", this.method];
|
var cmd = ["bin\\curl"];
|
||||||
var url = state.url;
|
var url = state.url;
|
||||||
|
|
||||||
|
if (this.isDebugging) {
|
||||||
|
cmd.push("-v");
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.push("-X");
|
||||||
|
cmd.push(this.method);
|
||||||
|
|
||||||
if (Object.keys(this.headers).length > 0) {
|
if (Object.keys(this.headers).length > 0) {
|
||||||
for (var key in this.headers) {
|
for (var key in this.headers) {
|
||||||
var value = this.evaluate(this.headers[key]);
|
var value = this.evaluate(this.headers[key]);
|
||||||
|
@ -381,6 +393,20 @@ var HTTPObject = function(engine) {
|
||||||
|
|
||||||
cmd.push("-A");
|
cmd.push("-A");
|
||||||
cmd.push((this.userAgent != null ? this.userAgent : ''));
|
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
|
// Add the request body if this is not GET method
|
||||||
if (this.method !== "GET") {
|
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 (this.isJSONResponse()) {
|
||||||
if (typeof(WScript) !== "undefined") {
|
if (typeof(WScript) !== "undefined") {
|
||||||
|
@ -437,11 +463,11 @@ var HTTPObject = function(engine) {
|
||||||
try {
|
try {
|
||||||
callback(this.responseBody);
|
callback(this.responseBody);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("callback of HTTPObject.send() -> ", e.message);
|
console.log("callback of HTTPObject.send() ->", e.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("HTTPObject.send() -> ", e.message);
|
console.error("HTTPObject.send() ->", e.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
@ -451,7 +477,7 @@ var HTTPObject = function(engine) {
|
||||||
try {
|
try {
|
||||||
return this.open("POST", url).send(callback);
|
return this.open("POST", url).send(callback);
|
||||||
} catch (e) {
|
} 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);
|
return this.open("GET", url).send(callback);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} 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");
|
throw Error("PATCH does not supported on GUI mode");
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} 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])
|
this.setVariable(k, variables[k])
|
||||||
;
|
;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("HTTPObject.setVariables() -> ", e.message);
|
console.error("HTTPObject.setVariables() ->", e.message);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
@ -573,6 +599,21 @@ var HTTPObject = function(engine) {
|
||||||
return str;
|
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();
|
this.create();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user