diff --git a/data/test-misc.json b/data/test-misc.json index fc04e9e..33b46f7 100644 --- a/data/test-misc.json +++ b/data/test-misc.json @@ -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" }, @@ -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"] }, { diff --git a/lib/http.js b/lib/http.js index 12bc77d..c5dc6dd 100644 --- a/lib/http.js +++ b/lib/http.js @@ -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; abuse@catswords.net)", // 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 = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/testloader.js b/testloader.js index 9554bdb..e71425d 100644 --- a/testloader.js +++ b/testloader.js @@ -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)");