Ready use the JSON-RPC 2.0 based stateless proxy
Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled

This commit is contained in:
Namhyeon Go 2024-11-24 03:15:37 +09:00
parent b5bc2bd851
commit f2044bcfbf
3 changed files with 51 additions and 23 deletions

View File

@ -1,6 +1,6 @@
{ {
"description": "WelsonJS test profile (test-misc.json)", "description": "WelsonJS test profile (test-misc.json)",
"updated_on": "2024-11-15", "updated_on": "2024-11-24",
"dependencies": { "dependencies": {
"welsonjs": "0.2.7" "welsonjs": "0.2.7"
}, },
@ -93,12 +93,17 @@
}, },
{ {
"id": "proxy_custom_provider", "id": "proxy_custom_provider",
"description": "HTTP proxy with an web proxy provider", "description": "HTTP request with an web proxy provider",
"tags": ["Network", "HTTP"] "tags": ["Network", "HTTP"]
}, },
{ {
"id": "proxy_serp", "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"] "tags": ["Network", "HTTP"]
}, },
{ {

View File

@ -50,13 +50,13 @@ var AVAILABLE_PROXIES = [
}, },
{ {
"type": "stateless-jsonrpc2", "type": "stateless-jsonrpc2",
"provider": "gnh1201/caterpillar", "provider": "github.com/gnh1201/caterpillar",
"url": "http://localhost:8080", "url": "http://localhost:8080",
"documentation": "https://github.com/gnh1201/caterpillar" "documentation": "https://github.com/gnh1201/caterpillar"
}, },
{ {
"type": "stateful", "type": "stateful",
"provider": "gnh1201/caterpillar", "provider": "github.com/gnh1201/caterpillar",
"url": "http://localhost:5555", "url": "http://localhost:5555",
"documentation": "https://github.com/gnh1201/caterpillar" "documentation": "https://github.com/gnh1201/caterpillar"
}, },
@ -109,7 +109,7 @@ var HTTPObject = function(engine) {
this.parameters = {}; this.parameters = {};
this.dataType = null; this.dataType = null;
this.userAgent = DEFAULT_USER_AGENT; this.userAgent = DEFAULT_USER_AGENT;
this.isAsynchronous = false; this.isAsync = false;
this.proxy = { this.proxy = {
"enabled": false, "enabled": false,
"type": "stateful", "type": "stateful",
@ -120,7 +120,7 @@ var HTTPObject = function(engine) {
"credential": null, // e.g. { username: "user", password: "pass" } "credential": null, // e.g. { username: "user", password: "pass" }
"method": null, // for stateless only. e.g. GET, POST "method": null, // for stateless only. e.g. GET, POST
"url": null, // for stateless only. e.g. http://localhost:8080 "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 "rpcMethod": "relay_fetch_url" // for stateless proxy
}; };
this.engine = (typeof(engine) !== "undefined" ? engine : "MSXML"); this.engine = (typeof(engine) !== "undefined" ? engine : "MSXML");
@ -228,7 +228,7 @@ var HTTPObject = function(engine) {
data: null, data: null,
contentType: this.contentType, contentType: this.contentType,
success: callback, success: callback,
async: this.isAsynchronous, async: this.isAsync,
error: onError // f(request, status, error) error: onError // f(request, status, error)
}; };
@ -311,7 +311,7 @@ var HTTPObject = function(engine) {
}; };
this.setMethod = function(method) { this.setMethod = function(method) {
this.method = method; this.method = method.toUpperCase();
return this; return this;
}; };
@ -465,7 +465,7 @@ var HTTPObject = function(engine) {
}; };
this.setIsAsynchronous = function(flag) { this.setIsAsynchronous = function(flag) {
this.isAsynchronous = flag; this.isAsync = flag;
return this; return this;
} }
@ -492,7 +492,7 @@ var HTTPObject = function(engine) {
if (this.proxy.type == "stateless-jsonrpc2") { if (this.proxy.type == "stateless-jsonrpc2") {
data = JSON.stringify( data = JSON.stringify(
JsonRpc2.wrap(this.proxy.rpcMethod, { JsonRpc2.wrap(this.proxy.rpcMethod, {
"method": this.proxy.method, "method": this.method,
"url": url, "url": url,
"headers": this.getRequestHeaders(function(x) { "headers": this.getRequestHeaders(function(x) {
return Object.keys(x).reduce(function(a, k) { return Object.keys(x).reduce(function(a, k) {
@ -504,7 +504,7 @@ var HTTPObject = function(engine) {
}, "") }, "")
); );
} }
return data; return data;
}; };
@ -602,7 +602,7 @@ var HTTPObject = function(engine) {
this.open = function(method, url) { this.open = function(method, url) {
var url = this.serializeParameters(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.pushState(null, null, url); // push state
this.setHeader("User-Agent", this.evaluate(this.getUserAgent())); // user agent this.setHeader("User-Agent", this.evaluate(this.getUserAgent())); // user agent
@ -612,17 +612,17 @@ var HTTPObject = function(engine) {
url = this.getProxiedUrl(url); url = this.getProxiedUrl(url);
// Open the URL // Open the URL
switch (this.method) { switch (this.getMethod()) {
case "POST": case "POST":
this._interface.open(method, url, this.isAsynchronous); this._interface.open("POST", url, this.isAsync);
break; break;
case "GET": case "GET":
this._interface.open(method, url, this.isAsynchronous); this._interface.open("GET", url, this.isAsync);
break; break;
default: 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"); this.setEngine("CURL");
console.log("Use the engine:", this.engine); console.log("Use the engine:", this.engine);
} }
@ -648,7 +648,8 @@ var HTTPObject = function(engine) {
// get opened URL from last states // get opened URL from last states
var state = this.states[this.states.length - 1]; 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 // [lib/http] cURL error with non-escaped ampersand on Command Prompt #103
var replaceAndExcludeCaretAnd = function(inputString) { var replaceAndExcludeCaretAnd = function(inputString) {
@ -690,7 +691,7 @@ var HTTPObject = function(engine) {
break; break;
default: default:
this._interface.send(this.serialize(url)); this._interface.send(this.serialize(target_url));
} }
// Waiting a response // Waiting a response
@ -763,7 +764,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(url))); cmd.push(replaceAndExcludeCaretAnd(this.serialize(target_url)));
} }
// Add proxy: <[protocol://][user:password@]proxyhost[:port]> // Add proxy: <[protocol://][user:password@]proxyhost[:port]>
@ -885,9 +886,14 @@ var HTTPObject = function(engine) {
if (this.isJSONResponse()) { if (this.isJSONResponse()) {
try { 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) { } catch (e) {
console.error("JSON parse error: " + e.message); console.error("JSON parse error:", e.message);
this.setResponseBody({}); this.setResponseBody({});
} }
} else { } else {
@ -1295,7 +1301,7 @@ exports.parseURL = parseURL;
exports.DEFAULT_USER_AGENT = DEFAULT_USER_AGENT; exports.DEFAULT_USER_AGENT = DEFAULT_USER_AGENT;
exports.defaultUserAgent = DEFAULT_USER_AGENT; // compatible 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.AUTHOR = "abuse@catswords.net";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;

View File

@ -1045,6 +1045,23 @@ var test_implements = {
console.log("responseBody:", response.responseBody); 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() { "numbers_test": function() {
// Riemann integrals // Riemann integrals
console.log("Test Riemann integrals (with 200 subdivisions)"); console.log("Test Riemann integrals (with 200 subdivisions)");