mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-15 06:01:04 +00:00
Ready use the JSON-RPC 2.0 based stateless proxy
This commit is contained in:
parent
3fa72f86d6
commit
43b5781cbd
46
lib/http.js
46
lib/http.js
|
@ -478,14 +478,22 @@ var HTTPObject = function(engine) {
|
||||||
return (this.proxy.userAgent != null ? this.proxy.userAgent : this.userAgent);
|
return (this.proxy.userAgent != null ? this.proxy.userAgent : this.userAgent);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.serialize = function() {
|
this.serialize = function(url) {
|
||||||
|
var data = "";
|
||||||
|
|
||||||
if (this.isJSONRequest() && typeof this.requestBody === "object") {
|
if (this.isJSONRequest() && typeof this.requestBody === "object") {
|
||||||
return JSON.stringify(this.requestBody);
|
data = JSON.stringify(this.requestBody);
|
||||||
} else if (typeof this.requestBody === "object") {
|
} else if (typeof this.requestBody === "object") {
|
||||||
return this.serializeURL(this.requestBody);
|
data = this.serializeURL(this.requestBody);
|
||||||
} else {
|
} else {
|
||||||
return this.evaluate(this.requestBody);
|
data = this.evaluate(this.requestBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.proxy.type == "stateless-jsonrpc2") {
|
||||||
|
// todo
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.serializeURL = function(params) {
|
this.serializeURL = function(params) {
|
||||||
|
@ -506,21 +514,21 @@ var HTTPObject = function(engine) {
|
||||||
// Bind parameters
|
// Bind parameters
|
||||||
if (Object.keys(this.parameters).length > 0) {
|
if (Object.keys(this.parameters).length > 0) {
|
||||||
// Type 2
|
// Type 2
|
||||||
var parameters = {};
|
var params = {};
|
||||||
for (var k in this.parameters) {
|
for (var k in this.parameters) {
|
||||||
if (url.indexOf(':' + k) > -1) {
|
if (url.indexOf(':' + k) > -1) {
|
||||||
url = url.replace(':' + k, this.evaluate(this.parameters[k]));
|
url = url.replace(':' + k, this.evaluate(this.parameters[k]));
|
||||||
} else {
|
} else {
|
||||||
parameters[k] = this.evaluate(this.parameters[k]);
|
params[k] = this.evaluate(this.parameters[k]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type 1
|
// Type 1
|
||||||
if (Object.keys(parameters).length > 0) {
|
if (Object.keys(params).length > 0) {
|
||||||
if (url.indexOf('?') > -1) {
|
if (url.indexOf('?') > -1) {
|
||||||
url += '&' + this.serializeURL(parameters);
|
url += '&' + this.serializeURL(params);
|
||||||
} else {
|
} else {
|
||||||
url += '?' + this.serializeURL(parameters);
|
url += '?' + this.serializeURL(params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -620,6 +628,16 @@ var HTTPObject = function(engine) {
|
||||||
var responseText = null;
|
var responseText = null;
|
||||||
var debuggingText = null;
|
var debuggingText = null;
|
||||||
|
|
||||||
|
// check exists the opened states
|
||||||
|
if (this.states.length == 0) {
|
||||||
|
console.error("No available states");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get opened URL from last states
|
||||||
|
var state = this.states[this.states.length - 1];
|
||||||
|
var url = this.getProxiedUrl(state.url);
|
||||||
|
|
||||||
// [lib/http] cURL error with non-escaped ampersand on Command Prompt #103
|
// [lib/http] cURL error with non-escaped ampersand on Command Prompt #103
|
||||||
var replaceAndExcludeCaretAnd = function(inputString) {
|
var replaceAndExcludeCaretAnd = function(inputString) {
|
||||||
var result = "";
|
var result = "";
|
||||||
|
@ -660,7 +678,7 @@ var HTTPObject = function(engine) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
this._interface.send(this.serialize());
|
this._interface.send(this.serialize(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Waiting a response
|
// Waiting a response
|
||||||
|
@ -669,11 +687,8 @@ var HTTPObject = function(engine) {
|
||||||
// Get response text
|
// Get response text
|
||||||
responseText = this._interface.responseText;
|
responseText = this._interface.responseText;
|
||||||
} else if (this.engine == "CURL") {
|
} else if (this.engine == "CURL") {
|
||||||
if (this.states.length > 0) {
|
// Build the cURL command line context
|
||||||
// Make CURL context
|
|
||||||
var state = this.states[this.states.length - 1];
|
|
||||||
var cmd = [];
|
var cmd = [];
|
||||||
var url = this.getProxiedUrl(state.url);
|
|
||||||
|
|
||||||
if (this.isDebugging) {
|
if (this.isDebugging) {
|
||||||
cmd.push("-v");
|
cmd.push("-v");
|
||||||
|
@ -736,7 +751,7 @@ var HTTPObject = function(engine) {
|
||||||
// Add the request body if this is not GET method
|
// Add the request body if this is not GET method
|
||||||
if (this.getMethod() !== "GET") {
|
if (this.getMethod() !== "GET") {
|
||||||
cmd.push("-d");
|
cmd.push("-d");
|
||||||
cmd.push(replaceAndExcludeCaretAnd(this.serialize()));
|
cmd.push(replaceAndExcludeCaretAnd(this.serialize(url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add proxy: <[protocol://][user:password@]proxyhost[:port]>
|
// Add proxy: <[protocol://][user:password@]proxyhost[:port]>
|
||||||
|
@ -810,7 +825,6 @@ var HTTPObject = function(engine) {
|
||||||
|
|
||||||
// Get debuging text
|
// Get debuging text
|
||||||
debuggingText = this._interface.stderr.read();
|
debuggingText = this._interface.stderr.read();
|
||||||
}
|
|
||||||
|
|
||||||
// clear manually
|
// clear manually
|
||||||
this._interface.clear();
|
this._interface.clear();
|
||||||
|
|
|
@ -11,6 +11,7 @@ function JsonRpc2(url) {
|
||||||
this.setUserAgent = function(agent) {
|
this.setUserAgent = function(agent) {
|
||||||
this.userAgent = agent;
|
this.userAgent = agent;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.invoke = function(method, params, id) {
|
this.invoke = function(method, params, id) {
|
||||||
var result;
|
var result;
|
||||||
var response = HTTP.create("MSXML")
|
var response = HTTP.create("MSXML")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user