mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-28 20:47:01 +00:00
Update http.js
This commit is contained in:
parent
92c57c3e94
commit
4bd7beb5ca
82
lib/http.js
82
lib/http.js
|
@ -52,6 +52,7 @@ var HTTPObject = function(engine) {
|
|||
}
|
||||
};
|
||||
this.connectTimeout = 0;
|
||||
this.maxTime = 0;
|
||||
this.isDebugging = false;
|
||||
this.credential = {
|
||||
method: "",
|
||||
|
@ -66,6 +67,9 @@ var HTTPObject = function(engine) {
|
|||
|
||||
this.curlOptions = [];
|
||||
|
||||
this.charset = "utf-8";
|
||||
this.isUseCharsetDetector = false;
|
||||
|
||||
this.create = function() {
|
||||
if (this.engine == "MSXML") {
|
||||
this.interface = CreateObject([
|
||||
|
@ -381,6 +385,7 @@ var HTTPObject = function(engine) {
|
|||
|
||||
this.send = function(callback) {
|
||||
var responseText = null;
|
||||
var debuggingText = null;
|
||||
|
||||
if (this.contentType != null) {
|
||||
this.setHeader("Content-Type", this.contentType);
|
||||
|
@ -452,6 +457,12 @@ var HTTPObject = function(engine) {
|
|||
cmd.push("--connect-timeout");
|
||||
cmd.push(this.connectTimeout);
|
||||
}
|
||||
|
||||
// --max-time
|
||||
if (this.maxTime > 0) {
|
||||
cmd.push("--max-time");
|
||||
cmd.push(this.maxTime);
|
||||
}
|
||||
|
||||
// Add the credential parameters
|
||||
switch (this.credential.method.toUpperCase()) {
|
||||
|
@ -508,10 +519,22 @@ var HTTPObject = function(engine) {
|
|||
cmd.push(state.url);
|
||||
|
||||
// Get response text
|
||||
responseText = this.interface.exec(cmd);
|
||||
responseText = this.interface.setCharset(this.charset).exec(cmd);
|
||||
|
||||
// If enabled the charset(text encoding) detector
|
||||
if (this.isUseCharsetDetector) {
|
||||
var detectedCharset = this.detectCharset(responseText);
|
||||
console.log("Detected charset:", detectedCharset);
|
||||
|
||||
if (detectedCharset != null && this.charset != detectedCharset) {
|
||||
var _interface = SHELL.create();
|
||||
responseText = _interface.setCharset(detectedCharset).exec(cmd);
|
||||
debuggingText = _interface.stderr;
|
||||
}
|
||||
}
|
||||
|
||||
// Get debuging text
|
||||
this.debuggingText = this.interface.stderr;
|
||||
debuggingText = this.interface.stderr;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -521,8 +544,9 @@ var HTTPObject = function(engine) {
|
|||
console.log("No received anything");
|
||||
}
|
||||
|
||||
if (this.isDebugging && typeof this.debuggingText === "string") {
|
||||
console.log("Created debugging text.", this.debuggingText.length, "bytes");
|
||||
if (this.isDebugging && typeof debuggingText === "string") {
|
||||
this.debuggingText = debuggingText;
|
||||
console.log("Created debugging text", debuggingText.length, "bytes");
|
||||
} else {
|
||||
console.log("No debugging text");
|
||||
}
|
||||
|
@ -698,6 +722,11 @@ var HTTPObject = function(engine) {
|
|||
return this;
|
||||
};
|
||||
|
||||
this.setMaxTime = function(seconds) {
|
||||
this.maxTime = seconds;
|
||||
return this;
|
||||
};
|
||||
|
||||
this.setIsDebugging = function(flag) {
|
||||
this.isDebugging = flag;
|
||||
return this;
|
||||
|
@ -774,7 +803,8 @@ var HTTPObject = function(engine) {
|
|||
while (pos > -1) {
|
||||
var end = response.indexOf('</iframe>', pos);
|
||||
|
||||
if (response.indexOf('</iframe>', pos) < 0) {
|
||||
if (end < 0) {
|
||||
pos = response.indexOf('<iframe ', pos + 8);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -848,6 +878,46 @@ var HTTPObject = function(engine) {
|
|||
return this;
|
||||
};
|
||||
|
||||
this.setCharset = function(charset) {
|
||||
this.charset = charset;
|
||||
return this;
|
||||
};
|
||||
|
||||
this.setIsUseCharsetDetector = function(flag) {
|
||||
this.isUseCharsetDetector = flag;
|
||||
return this;
|
||||
};
|
||||
|
||||
this.detectCharset = function(content) {
|
||||
var charset = null;
|
||||
|
||||
try {
|
||||
var s = "charset=";
|
||||
var pos = content.toLowerCase().indexOf(s);
|
||||
if (pos > -1) {
|
||||
var end = [
|
||||
content.indexOf('"', pos + s.length),
|
||||
content.indexOf('\'', pos + s.length),
|
||||
content.indexOf(';', pos + s.length)
|
||||
].reduce(function(a, x) {
|
||||
if (a < 0 || x > 0 && x < a) a = x;
|
||||
return a;
|
||||
}, -1);
|
||||
|
||||
if (end > -1) {
|
||||
var detectedCharset = content.substring(pos + s.length, end);
|
||||
if (detectedCharset.length > 0 && detectedCharset.length < 16) {
|
||||
charset = detectedCharset.toLowerCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
charset = null;
|
||||
}
|
||||
|
||||
return charset;
|
||||
};
|
||||
|
||||
this.create();
|
||||
};
|
||||
|
||||
|
@ -882,6 +952,6 @@ exports.patch = patch;
|
|||
exports.put = put;
|
||||
exports._delete = _delete;
|
||||
|
||||
exports.VERSIONINFO = "HTTP Lib (http.js) version 0.7.4";
|
||||
exports.VERSIONINFO = "HTTP Lib (http.js) version 0.7.5";
|
||||
exports.global = global;
|
||||
exports.require = global.require;
|
||||
|
|
Loading…
Reference in New Issue
Block a user