From 000a814cb69171a50b583b7174c2ed4b938a1689 Mon Sep 17 00:00:00 2001 From: pfans Date: Tue, 19 Dec 2023 21:13:36 +0800 Subject: [PATCH] Update path format in request-redirect.js (#104) --- pjs/filter/request-redirect.js | 53 ++++++++++++++----- .../config/request-redirect_shpec/config.json | 5 +- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/pjs/filter/request-redirect.js b/pjs/filter/request-redirect.js index de2a835e..8d0a3d7e 100644 --- a/pjs/filter/request-redirect.js +++ b/pjs/filter/request-redirect.js @@ -29,34 +29,58 @@ ).join('/') ), - makeRedirectHandler = cfg => ( + makePathHandle = (path, cfg) => ( + (cfg?.Path?.Type === 'ReplacePrefixMatch') ? ( + (cfg?.Path?.ReplacePrefixMatch !== undefined) && ( + head => ( + head?.path?.length > path.length ? ( + head.path = resolvPath(cfg.Path.ReplacePrefixMatch) + head.path.substring(path.length) + ) : ( + head.path = resolvPath(cfg.Path.ReplacePrefixMatch) + ) + ) + ) + ) : (cfg?.Path?.Type === 'ReplaceFullPath') && ( + (cfg?.Path?.ReplaceFullPath !== undefined) && ( + head => ( + ( + prefix = (head?.path || '').split('?')[0], + suffix = (head?.path || '').substring(prefix.length), + ) => ( + head.path = resolvPath(cfg.Path.ReplaceFullPath) + suffix + ) + )() + ) + ) + ), + + makeRedirectHandler = (path, cfg) => ( head => cfg?.StatusCode ? ( ( scheme = cfg?.Scheme || head?.scheme || 'http', hostname = cfg?.Hostname || head?.headers?.host, - path = resolvPath(cfg?.Path) || head?.path, + pathHandle = makePathHandle(path, cfg), port = cfg?.Port, ) => ( + pathHandle(head), port && hostname && ( hostname = hostname.split(':')[0] + ':' + port ), - hostname && path ? ( - new Message({ - status: cfg.StatusCode, - headers: { - Location: scheme + '://' + hostname + path - } - }) - ) : null + new Message({ + status: cfg.StatusCode, + headers: { + Location: scheme + '://' + hostname + head.path + } + }) ) )() : null ), - makeServiceRedirectHandler = svc => ( - (svc?.Filters || []).filter( + makeServiceRedirectHandler = (path, cfg) => ( + (cfg?.Filters || []).filter( e => e?.Type === 'RequestRedirect' ).map( - e => makeRedirectHandler(e.RequestRedirect) + e => makeRedirectHandler(path, e.RequestRedirect) ).filter( e => e )?.[0] @@ -66,11 +90,12 @@ route => ( ( config = route?.config, + path = config?.Path?.Path || '/', backendService = config?.BackendService, ) => ( new algo.Cache( service => ( - makeServiceRedirectHandler(backendService?.[service]) || makeServiceRedirectHandler(config) + makeServiceRedirectHandler(path, backendService?.[service]) || makeServiceRedirectHandler(path, config) ) ) ) diff --git a/tests/shpec/config/request-redirect_shpec/config.json b/tests/shpec/config/request-redirect_shpec/config.json index e6cbe7c7..4e3c14b5 100644 --- a/tests/shpec/config/request-redirect_shpec/config.json +++ b/tests/shpec/config/request-redirect_shpec/config.json @@ -92,7 +92,10 @@ "RequestRedirect": { "Scheme": "https", "Hostname": "", - "Path": "/abc", + "Path": { + "Type": "ReplacePrefixMatch", + "ReplacePrefixMatch": "/abc" + }, "Port": 8443, "StatusCode": 301 }