Skip to content

Commit

Permalink
Update path format in request-redirect.js (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanpf authored Dec 19, 2023
1 parent 0226b43 commit 000a814
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
53 changes: 39 additions & 14 deletions pjs/filter/request-redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
)
)
)
Expand Down
5 changes: 4 additions & 1 deletion tests/shpec/config/request-redirect_shpec/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@
"RequestRedirect": {
"Scheme": "https",
"Hostname": "",
"Path": "/abc",
"Path": {
"Type": "ReplacePrefixMatch",
"ReplacePrefixMatch": "/abc"
},
"Port": 8443,
"StatusCode": 301
}
Expand Down

0 comments on commit 000a814

Please sign in to comment.