Skip to content

Commit

Permalink
Ready use the JSON-RPC 2.0 based stateless proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
gnh1201 committed Nov 23, 2024
1 parent b5bc2bd commit f2044bc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
11 changes: 8 additions & 3 deletions data/test-misc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"description": "WelsonJS test profile (test-misc.json)",
"updated_on": "2024-11-15",
"updated_on": "2024-11-24",
"dependencies": {
"welsonjs": "0.2.7"
},
Expand Down Expand Up @@ -93,12 +93,17 @@
},
{
"id": "proxy_custom_provider",
"description": "HTTP proxy with an web proxy provider",
"description": "HTTP request with an web proxy provider",
"tags": ["Network", "HTTP"]
},
{
"id": "proxy_serp",
"description": "HTTP proxy with a SERP provider",
"description": "HTTP request with a SERP provider",
"tags": ["Network", "HTTP"]
},
{
"id": "proxy_stateless_jsonrpc2",
"description": "HTTP request with a JSON-RPC 2.0 based stateless proxy",
"tags": ["Network", "HTTP"]
},
{
Expand Down
46 changes: 26 additions & 20 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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",
Expand All @@ -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; abuse@catswords.net)", // for stateless only
"rpcMethod": "relay_fetch_url" // for stateless proxy
};
this.engine = (typeof(engine) !== "undefined" ? engine : "MSXML");
Expand Down Expand Up @@ -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)
};

Expand Down Expand Up @@ -311,7 +311,7 @@ var HTTPObject = function(engine) {
};

this.setMethod = function(method) {
this.method = method;
this.method = method.toUpperCase();
return this;
};

Expand Down Expand Up @@ -465,7 +465,7 @@ var HTTPObject = function(engine) {
};

this.setIsAsynchronous = function(flag) {
this.isAsynchronous = flag;
this.isAsync = flag;
return this;
}

Expand All @@ -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) {
Expand All @@ -504,7 +504,7 @@ var HTTPObject = function(engine) {
}, "")
);
}

return data;
};

Expand Down Expand Up @@ -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

Expand All @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]>
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;
17 changes: 17 additions & 0 deletions testloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,23 @@ var test_implements = {
console.log("responseBody:", response.responseBody);
},

"proxy_stateless_jsonrpc2": function() {
var HTTP = require("lib/http");

var response = HTTP.create()
.setProxy({
"enabled": true,
"provider": "github.com/gnh1201/caterpillar",
"type": "stateless-jsonrpc2",
"url": "https://localhost:8080"
})
.setDataType("json")
.open("GET", "https://example.org")
.send();

console.log("responseBody:", response.responseBody);
},

"numbers_test": function() {
// Riemann integrals
console.log("Test Riemann integrals (with 200 subdivisions)");
Expand Down

0 comments on commit f2044bc

Please sign in to comment.