diff --git a/lib/mapper.js b/lib/mapper.js index 0c4b19a..9ab1ff3 100644 --- a/lib/mapper.js +++ b/lib/mapper.js @@ -23,6 +23,7 @@ var updateFromJson = function (path, map) { host: keyToSession[key]["host"], port: parseInt(keyToSession[key]["port"]), requires_path_in_url: info?.requires_path_in_url, + requires_path_in_header: info?.requires_path_in_header, }, }; } @@ -66,6 +67,7 @@ var updateFromSqlite = function (path, map) { key_type: row["key_type"], token: row["token"], requires_path_in_url: info?.requires_path_in_url, + requires_path_in_header: info?.requires_path_in_header, }; }, finish, diff --git a/lib/proxy.js b/lib/proxy.js index 1532168..2565bc6 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -124,6 +124,15 @@ DynamicProxy.prototype.targetForRequest = function (request) { key = path_split[1]; token = path_split[2]; + const target_inject_header = this.sessionMap[key]?.requires_path_in_header; + if (target_inject_header) { + request.headers[target_inject_header] = [ + this.proxyPathPrefix, + key, + token, + ].join("/"); + } + if (!this.sessionMap[key]?.requires_path_in_url) { const target_url = "/" + path_split.slice(3).join("/"); request.url = target_url; diff --git a/test/test.js b/test/test.js index 2e5c464..8bc17a1 100644 --- a/test/test.js +++ b/test/test.js @@ -245,6 +245,46 @@ describe("DynamicProxy", function () { }); }); + describe("map based path forwarding to top-level path with entry point path in header", function () { + it( + 'should respect session map with requires_path_in_header="X-My-Header", strip entry point path from ' + + 'url and instead provide it in header "X-My-Header"', + async function () { + const sessionMap = { + coolkey: { + token: "cooltoken", + target: { + host: "localhost", + port: TEST_PORT, + }, + requires_path_in_header: "X-My-Header", + }, + }; + const proxy = new DynamicProxy({ + port: 5103, + verbose: true, + sessionMap: sessionMap, + proxyPathPrefix: "/interactivetool/ep", + }); + proxy.listen(); + const headers = { + host: "usegalaxy.org", + }; + + proxy.proxy.on("proxyReq", function (proxyReq) { + proxyReq + .getHeader("X-My-Header") + .should.equal("/interactivetool/ep/coolkey/cooltoken"); + }); + + const path = + "/interactivetool/ep/coolkey/cooltoken/test_data/extradir/README.md"; + await verifyProxyOnPort(5103, headers, path); + proxy.close(); + }, + ); + }); + describe("double proxying", function () { it("should proxy across two servers", async function () { const sessionMap = {