-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ready use the JSON-RPC 2.0 based stateless proxy
- Loading branch information
Showing
3 changed files
with
51 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,13 +50,13 @@ var AVAILABLE_PROXIES = [ | |
}, | ||
{ | ||
"type": "stateless-jsonrpc2", | ||
"provider": "gnh1201/caterpillar", | ||
"provider": "github.com/gnh1201/caterpillar", | ||
"url": "http://localhost:8080", | ||
"documentation": "https://github.com/gnh1201/caterpillar" | ||
}, | ||
{ | ||
"type": "stateful", | ||
"provider": "gnh1201/caterpillar", | ||
"provider": "github.com/gnh1201/caterpillar", | ||
"url": "http://localhost:5555", | ||
"documentation": "https://github.com/gnh1201/caterpillar" | ||
}, | ||
|
@@ -109,7 +109,7 @@ var HTTPObject = function(engine) { | |
this.parameters = {}; | ||
this.dataType = null; | ||
this.userAgent = DEFAULT_USER_AGENT; | ||
this.isAsynchronous = false; | ||
this.isAsync = false; | ||
this.proxy = { | ||
"enabled": false, | ||
"type": "stateful", | ||
|
@@ -120,7 +120,7 @@ var HTTPObject = function(engine) { | |
"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 | ||
"userAgent": "php-httpproxy/0.1.5 (Client; WelsonJS; [email protected])", // for stateless only | ||
"rpcMethod": "relay_fetch_url" // for stateless proxy | ||
}; | ||
this.engine = (typeof(engine) !== "undefined" ? engine : "MSXML"); | ||
|
@@ -228,7 +228,7 @@ var HTTPObject = function(engine) { | |
data: null, | ||
contentType: this.contentType, | ||
success: callback, | ||
async: this.isAsynchronous, | ||
async: this.isAsync, | ||
error: onError // f(request, status, error) | ||
}; | ||
|
||
|
@@ -311,7 +311,7 @@ var HTTPObject = function(engine) { | |
}; | ||
|
||
this.setMethod = function(method) { | ||
this.method = method; | ||
this.method = method.toUpperCase(); | ||
return this; | ||
}; | ||
|
||
|
@@ -465,7 +465,7 @@ var HTTPObject = function(engine) { | |
}; | ||
|
||
this.setIsAsynchronous = function(flag) { | ||
this.isAsynchronous = flag; | ||
this.isAsync = flag; | ||
return this; | ||
} | ||
|
||
|
@@ -492,7 +492,7 @@ var HTTPObject = function(engine) { | |
if (this.proxy.type == "stateless-jsonrpc2") { | ||
data = JSON.stringify( | ||
JsonRpc2.wrap(this.proxy.rpcMethod, { | ||
"method": this.proxy.method, | ||
"method": this.method, | ||
"url": url, | ||
"headers": this.getRequestHeaders(function(x) { | ||
return Object.keys(x).reduce(function(a, k) { | ||
|
@@ -504,7 +504,7 @@ var HTTPObject = function(engine) { | |
}, "") | ||
); | ||
} | ||
|
||
return data; | ||
}; | ||
|
||
|
@@ -602,7 +602,7 @@ var HTTPObject = function(engine) { | |
this.open = function(method, url) { | ||
var url = this.serializeParameters(url); | ||
|
||
this.setMethod(method.toUpperCase()); // set method | ||
this.setMethod(method); // set method | ||
this.pushState(null, null, url); // push state | ||
this.setHeader("User-Agent", this.evaluate(this.getUserAgent())); // user agent | ||
|
||
|
@@ -612,17 +612,17 @@ var HTTPObject = function(engine) { | |
url = this.getProxiedUrl(url); | ||
|
||
// Open the URL | ||
switch (this.method) { | ||
switch (this.getMethod()) { | ||
case "POST": | ||
this._interface.open(method, url, this.isAsynchronous); | ||
this._interface.open("POST", url, this.isAsync); | ||
break; | ||
|
||
case "GET": | ||
this._interface.open(method, url, this.isAsynchronous); | ||
this._interface.open("GET", url, this.isAsync); | ||
break; | ||
|
||
default: | ||
console.warn("Switching the engine to cURL. Not supported method in MSXML: " + method); | ||
console.warn(this.method, "method not supported. Retrying with cURL..."); | ||
this.setEngine("CURL"); | ||
console.log("Use the engine:", this.engine); | ||
} | ||
|
@@ -648,7 +648,8 @@ var HTTPObject = function(engine) { | |
|
||
// get opened URL from last states | ||
var state = this.states[this.states.length - 1]; | ||
var url = this.getProxiedUrl(state.url); | ||
var target_url = state.url; | ||
var url = this.getProxiedUrl(target_url); | ||
|
||
// [lib/http] cURL error with non-escaped ampersand on Command Prompt #103 | ||
var replaceAndExcludeCaretAnd = function(inputString) { | ||
|
@@ -690,7 +691,7 @@ var HTTPObject = function(engine) { | |
break; | ||
|
||
default: | ||
this._interface.send(this.serialize(url)); | ||
this._interface.send(this.serialize(target_url)); | ||
} | ||
|
||
// Waiting a response | ||
|
@@ -763,7 +764,7 @@ var HTTPObject = function(engine) { | |
// Add the request body if this is not GET method | ||
if (this.getMethod() !== "GET") { | ||
cmd.push("-d"); | ||
cmd.push(replaceAndExcludeCaretAnd(this.serialize(url))); | ||
cmd.push(replaceAndExcludeCaretAnd(this.serialize(target_url))); | ||
} | ||
|
||
// Add proxy: <[protocol://][user:password@]proxyhost[:port]> | ||
|
@@ -885,9 +886,14 @@ var HTTPObject = function(engine) { | |
|
||
if (this.isJSONResponse()) { | ||
try { | ||
this.setResponseBody(JSON.parse(responseText)); | ||
var res = JSON.parse(responseText); | ||
if (this.proxy.type == "stateless-jsonrpc2") { | ||
this.setResponseBody(res.result.data); | ||
} else { | ||
this.setResponseBody(res); | ||
} | ||
} catch (e) { | ||
console.error("JSON parse error: " + e.message); | ||
console.error("JSON parse error:", e.message); | ||
this.setResponseBody({}); | ||
} | ||
} else { | ||
|
@@ -1295,7 +1301,7 @@ exports.parseURL = parseURL; | |
exports.DEFAULT_USER_AGENT = DEFAULT_USER_AGENT; | ||
exports.defaultUserAgent = DEFAULT_USER_AGENT; // compatible | ||
|
||
exports.VERSIONINFO = "HTTP Client for WelsonJS framework (http.js) version 0.7.42"; | ||
exports.VERSIONINFO = "HTTP Client for WelsonJS framework (http.js) version 0.7.43"; | ||
exports.AUTHOR = "[email protected]"; | ||
exports.global = global; | ||
exports.require = global.require; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters