mirror of
https://github.com/gnh1201/welsonjs.git
synced 2024-11-26 15:31:42 +00:00
Ready use the JSON-RPC 2.0 based stateless proxy
This commit is contained in:
parent
8942a98431
commit
3fa72f86d6
123
lib/http.js
123
lib/http.js
|
@ -1,5 +1,5 @@
|
|||
// http.js
|
||||
// HTTP REST Client for WelsonJS framework
|
||||
// HTTP Client for WelsonJS framework
|
||||
// Namhyeon Go (Catswords Research) <abuse@catswords.net>
|
||||
// https://github.com/gnh1201/welsonjs
|
||||
var SYS = require("lib/system");
|
||||
|
@ -102,7 +102,7 @@ var HTTPObject = function(engine) {
|
|||
this._interface = null;
|
||||
|
||||
this.contentType = "application/x-www-form-urlencoded";
|
||||
this.requestBody = "";
|
||||
this.requestBody = null;
|
||||
this.responseBody = null;
|
||||
this.method = "GET";
|
||||
this.headers = {};
|
||||
|
@ -117,8 +117,11 @@ var HTTPObject = function(engine) {
|
|||
"protocol": "http",
|
||||
"host": "127.0.0.1",
|
||||
"port": 80,
|
||||
"credential": null, // { username: "user", password: "pass" }
|
||||
"url": null // stateless only
|
||||
"credential": null, // e.g. { username: "user", password: "pass" }
|
||||
"method": null, // for stateless only. e.g. GET, POST
|
||||
"url": null, // for stateless only. e.g. http://localhost:8080
|
||||
"userAgent": null, // for stateless only
|
||||
"rpcMethod": "relay_fetch_url" // for stateless proxy
|
||||
};
|
||||
this.engine = (typeof(engine) !== "undefined" ? engine : "MSXML");
|
||||
|
||||
|
@ -219,18 +222,18 @@ var HTTPObject = function(engine) {
|
|||
|
||||
this.jqAjax = function(url, callback, onError) {
|
||||
var options = {
|
||||
type: this.method,
|
||||
type: this.getMethod(),
|
||||
headers: this.headers,
|
||||
url: this.serializeParameters(url),
|
||||
//data: this.requestBody,
|
||||
data: null,
|
||||
contentType: this.contentType,
|
||||
success: callback,
|
||||
async: this.isAsynchronous,
|
||||
error: onError // (request, status, error)
|
||||
error: onError // f(request, status, error)
|
||||
};
|
||||
|
||||
if (["POST", "PUT", "PATCH"].indexOf(this.method) > -1) {
|
||||
options['data'] = this.requestBody;
|
||||
options['data'] = this.serialize();
|
||||
}
|
||||
|
||||
this.setResponseBody(window.jQuery.ajax(options).responseText);
|
||||
|
@ -245,11 +248,11 @@ var HTTPObject = function(engine) {
|
|||
if (this.dataType === "json") {
|
||||
return true;
|
||||
} else if (this.engine == "MSXML") {
|
||||
var headers = this.getHeaders();
|
||||
var headers = this.getResponseHeaders();
|
||||
for (var key in headers) {
|
||||
var _k = key.toLowerCase();
|
||||
var _v = headers[key].toLowerCase();
|
||||
if (_k === "content-type" && _v === "application/json") {
|
||||
var header_key = key.toLowerCase();
|
||||
var header_value = headers[key].toLowerCase();
|
||||
if (header_key === "content-type" && header_value === "application/json") {
|
||||
this.dataType = "json";
|
||||
return true;
|
||||
}
|
||||
|
@ -284,7 +287,7 @@ var HTTPObject = function(engine) {
|
|||
this.proxy.credential = availableProxy['credential'] || this.proxy.credential;
|
||||
this.proxy.url = availableProxy['url'] || this.proxy.url;
|
||||
|
||||
console.info("Please check documentation:", availableProxy.documentation);
|
||||
console.info("See the documentation:", availableProxy.documentation);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -296,6 +299,11 @@ var HTTPObject = function(engine) {
|
|||
this.proxy[k] = proxy[k];
|
||||
}
|
||||
|
||||
// When JSON-RPC 2.0 based stateless proxy
|
||||
if (this.proxy.type == "stateless-jsonrpc2") {
|
||||
this.proxy.method = "POST";
|
||||
}
|
||||
|
||||
// check proxy configuration
|
||||
console.info("Proxy Configuration:", JSON.stringify(this.proxy));
|
||||
|
||||
|
@ -306,6 +314,10 @@ var HTTPObject = function(engine) {
|
|||
this.method = method;
|
||||
return this;
|
||||
};
|
||||
|
||||
this.getMethod = function() {
|
||||
return (this.proxy.method != null ? this.proxy.method : this.method);
|
||||
};
|
||||
|
||||
this.setDataType = function(type) {
|
||||
this.dataType = type;
|
||||
|
@ -358,25 +370,24 @@ var HTTPObject = function(engine) {
|
|||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("HTTPObject.setHeaders() -> ", e.message);
|
||||
console.error("Exception on HTTPObject.setHeaders():", e.message);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
this.getHeader = function(key) {
|
||||
this.getResponseHeader = function(key) {
|
||||
try {
|
||||
return this._interface.getResponseHeader(key);
|
||||
} catch (e) {
|
||||
console.error("HTTPObject.getHeader() -> ", e.message);
|
||||
console.error("Exception on HTTPObject.getResponseHeader():", e.message);
|
||||
}
|
||||
};
|
||||
|
||||
this.getHeaders = function() {
|
||||
this.getResponseHeaders = function(callback) {
|
||||
try {
|
||||
var raw = this._interface.getAllResponseHeaders();
|
||||
|
||||
return raw.split(/[\r\n]+/).filter(function(s) {
|
||||
var text = this._interface.getAllResponseHeaders();
|
||||
var headers = text.split(/[\r\n]+/).filter(function(s) {
|
||||
return s.trim().length > 0;
|
||||
}).map(function(s) {
|
||||
return s.trim().split(": ");
|
||||
|
@ -384,11 +395,33 @@ var HTTPObject = function(engine) {
|
|||
acc[c[0].trim()] = c[1].trim();
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
if (typeof callback !== "function") {
|
||||
return headers;
|
||||
}
|
||||
|
||||
return callback(headers);
|
||||
} catch (e) {
|
||||
console.error("HTTPObject.getHeaders() -> ", e.message);
|
||||
console.error("Exception on HTTPObject.getResponseHeaders():", e.message);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
this.getRequestHeader = function(key) {
|
||||
return this.headers[key];
|
||||
}
|
||||
|
||||
this.getRequestHeaders = function(callback) {
|
||||
try {
|
||||
if (typeof callback !== "function") {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
return callback(this.headers);
|
||||
} catch (e) {
|
||||
console.error("Exception on HTTPObject.getRequestHeaders():", e.message);
|
||||
}
|
||||
};
|
||||
|
||||
this.setParameter = function(key, value) {
|
||||
this.parameters[key] = value;
|
||||
return this;
|
||||
|
@ -406,7 +439,7 @@ var HTTPObject = function(engine) {
|
|||
this.setParameter(key, value);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("HTTPObject.setParameters() -> ", e.message);
|
||||
console.error("Exception on HTTPObject.setParameters():", e.message);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
@ -440,22 +473,26 @@ var HTTPObject = function(engine) {
|
|||
this.userAgent = agent;
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
this.getUserAgent = function() {
|
||||
return (this.proxy.userAgent != null ? this.proxy.userAgent : this.userAgent);
|
||||
};
|
||||
|
||||
this.serialize = function() {
|
||||
if (this.isJSONRequest() && typeof(this.requestBody) === "object") {
|
||||
if (this.isJSONRequest() && typeof this.requestBody === "object") {
|
||||
return JSON.stringify(this.requestBody);
|
||||
} else if (typeof(this.requestBody) === "object") {
|
||||
return this.serializeURL(this.evaluate(this.requestBody));
|
||||
} else if (typeof this.requestBody === "object") {
|
||||
return this.serializeURL(this.requestBody);
|
||||
} else {
|
||||
return this.evaluate(this.requestBody);
|
||||
}
|
||||
};
|
||||
|
||||
this.serializeURL = function(parameters) {
|
||||
this.serializeURL = function(params) {
|
||||
var s = [];
|
||||
for (var k in parameters) {
|
||||
if (parameters.hasOwnProperty(k)) {
|
||||
s.push(encodeURIComponent(k) + "=" + encodeURIComponent(this.evaluate(parameters[k])));
|
||||
for (var k in params) {
|
||||
if (params.hasOwnProperty(k)) {
|
||||
s.push(encodeURIComponent(k) + "=" + encodeURIComponent(this.evaluate(params[k])));
|
||||
}
|
||||
}
|
||||
return s.join("&");
|
||||
|
@ -493,7 +530,7 @@ var HTTPObject = function(engine) {
|
|||
return url;
|
||||
};
|
||||
|
||||
this.getProxiedURL = function(url) {
|
||||
this.getProxiedUrl = function(url) {
|
||||
if (!this.proxy.enabled) return url;
|
||||
|
||||
if (this.proxy.type == "serp") {
|
||||
|
@ -547,12 +584,12 @@ var HTTPObject = function(engine) {
|
|||
|
||||
this.setMethod(method.toUpperCase()); // set method
|
||||
this.pushState(null, null, url); // push state
|
||||
this.setHeader("User-Agent", this.evaluate(this.userAgent)); // user agent
|
||||
this.setHeader("User-Agent", this.evaluate(this.getUserAgent())); // user agent
|
||||
|
||||
try {
|
||||
if (this.engine == "MSXML") {
|
||||
// Get the proxied URL
|
||||
url = this.getProxiedURL(url);
|
||||
url = this.getProxiedUrl(url);
|
||||
|
||||
// Open the URL
|
||||
switch (this.method) {
|
||||
|
@ -617,7 +654,7 @@ var HTTPObject = function(engine) {
|
|||
this._interface.setRequestHeader(key, this.evaluate(this.headers[key]));
|
||||
}
|
||||
|
||||
switch (this.method) {
|
||||
switch (this.getMethod()) {
|
||||
case "GET":
|
||||
this._interface.send();
|
||||
break;
|
||||
|
@ -636,7 +673,7 @@ var HTTPObject = function(engine) {
|
|||
// Make CURL context
|
||||
var state = this.states[this.states.length - 1];
|
||||
var cmd = [];
|
||||
var url = this.getProxiedURL(state.url);
|
||||
var url = this.getProxiedUrl(state.url);
|
||||
|
||||
if (this.isDebugging) {
|
||||
cmd.push("-v");
|
||||
|
@ -651,7 +688,7 @@ var HTTPObject = function(engine) {
|
|||
}
|
||||
|
||||
cmd.push("-X");
|
||||
cmd.push(this.method);
|
||||
cmd.push(this.getMethod());
|
||||
|
||||
if (Object.keys(this.headers).length > 0) {
|
||||
for (var key in this.headers) {
|
||||
|
@ -674,7 +711,7 @@ var HTTPObject = function(engine) {
|
|||
}
|
||||
|
||||
cmd.push("-A");
|
||||
cmd.push(this.evaluate(this.userAgent));
|
||||
cmd.push(this.evaluate(this.getUserAgent()));
|
||||
|
||||
// --connect-timeout
|
||||
if (this.connectTimeout > 0) {
|
||||
|
@ -697,7 +734,7 @@ var HTTPObject = function(engine) {
|
|||
}
|
||||
|
||||
// Add the request body if this is not GET method
|
||||
if (this.method !== "GET") {
|
||||
if (this.getMethod() !== "GET") {
|
||||
cmd.push("-d");
|
||||
cmd.push(replaceAndExcludeCaretAnd(this.serialize()));
|
||||
}
|
||||
|
@ -782,10 +819,10 @@ var HTTPObject = function(engine) {
|
|||
var job_priority = "normal";
|
||||
var state = this.states[this.states.length - 1];
|
||||
var cmd = ["/transfer", job_name];
|
||||
var url = this.getProxiedURL(state.url);
|
||||
var url = this.getProxiedUrl(state.url);
|
||||
var out = PipeIPC.connect("volatile");
|
||||
|
||||
if (this.method == "GET") {
|
||||
if (this.getMethod() == "GET") {
|
||||
cmd = cmd.concat(["/download", "/priority", job_priority, url, SYS.getCurrentScriptDirectory() + "\\" + out.path]); // build a BITS command
|
||||
this._interface.exec(cmd); // launch the download job
|
||||
out.reload(); // read the downloaded data
|
||||
|
@ -799,7 +836,7 @@ var HTTPObject = function(engine) {
|
|||
} else if (this.engine == "CERT") {
|
||||
var state = this.states[this.states.length - 1];
|
||||
var out = PipeIPC.connect("volatile");
|
||||
var url = this.getProxiedURL(state.url);
|
||||
var url = this.getProxiedUrl(state.url);
|
||||
var cmd = ["-urlcache", "-split", "-f", url, out.path];
|
||||
this._interface.exec(cmd);
|
||||
out.reload();
|
||||
|
@ -1232,7 +1269,7 @@ exports.parseURL = parseURL;
|
|||
exports.DEFAULT_USER_AGENT = DEFAULT_USER_AGENT;
|
||||
exports.defaultUserAgent = DEFAULT_USER_AGENT; // compatible
|
||||
|
||||
exports.VERSIONINFO = "WelsonJS HTTP Client (http.js) version 0.7.41";
|
||||
exports.VERSIONINFO = "HTTP Client for WelsonJS framework (http.js) version 0.7.41";
|
||||
exports.AUTHOR = "abuse@catswords.net";
|
||||
exports.global = global;
|
||||
exports.require = global.require;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
// https://github.com/gnh1201/welsonjs
|
||||
var HTTP = require("lib/http");
|
||||
|
||||
function jsonrpc2(url) {
|
||||
function JsonRpc2(url) {
|
||||
this.url = url;
|
||||
this.userAgent = "php-httpproxy/0.1.5 (Client; WelsonJS; abuse@catswords.net)";
|
||||
|
||||
|
@ -46,7 +46,7 @@ function wrap(method, params, id) {
|
|||
}
|
||||
|
||||
function create(url) {
|
||||
return new jsonrpc2(url);
|
||||
return new JsonRpc2(url);
|
||||
}
|
||||
|
||||
exports.wrap = wrap;
|
||||
|
|
Loading…
Reference in New Issue
Block a user