Skip to content

Commit 156125d

Browse files
authored
feat: add internalRewriteLocationHeader option (#298)
* feat: add disableInternalRewriteLocationHeader option * fix: revert an unnecessary line change in README * refactor: use the positive option instead of the negative option
1 parent 23a202f commit 156125d

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ configuration passed to the route.
163163

164164
Object with [reply options](https://github.com/fastify/fastify-reply-from#replyfromsource-opts) for `@fastify/reply-from`.
165165

166+
### `internalRewriteLocationHeader`
167+
By default, `@fastify/http-proxy` will rewrite the `location` header when a request redirects to a relative path.
168+
In other words, the [prefix](https://github.com/fastify/fastify-http-proxy#prefix) will be added to the relative path.
169+
170+
If you want to preserve the original path, this option will disable this internal operation. Default: `true`.
171+
172+
Note that the [rewriteHeaders](https://github.com/fastify/fastify-reply-from#rewriteheadersheaders-request) option of [`@fastify/reply-from`](http://npm.im/fastify-reply-from) will retrieve headers modified (reminder: only `location` is updated among all headers) in parameter but with this option, the headers are unchanged.
173+
166174
### `httpMethods`
167175
An array that contains the types of the methods. Default: `['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']`.
168176

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ async function fastifyHttpProxy (fastify, opts) {
234234
fromOpts.base = opts.upstream
235235
fromOpts.prefix = undefined
236236

237+
const internalRewriteLocationHeader = opts.internalRewriteLocationHeader ?? true
237238
const oldRewriteHeaders = (opts.replyOptions || {}).rewriteHeaders
238239
const replyOpts = Object.assign({}, opts.replyOptions, {
239240
rewriteHeaders
@@ -249,7 +250,7 @@ async function fastifyHttpProxy (fastify, opts) {
249250

250251
function rewriteHeaders (headers, req) {
251252
const location = headers.location
252-
if (location && !isExternalUrl(location)) {
253+
if (location && !isExternalUrl(location) && internalRewriteLocationHeader) {
253254
headers.location = location.replace(rewritePrefix, fastify.prefix)
254255
}
255256
if (oldRewriteHeaders) {

test/test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ async function run () {
2929
throw new Error('kaboom')
3030
})
3131

32+
origin.post('/redirect-to-relative-url', async (request, reply) => {
33+
reply.header('location', '/relative-url')
34+
return { status: 'ok' }
35+
})
36+
3237
origin.get('/api2/a', async (request, reply) => {
3338
return 'this is /api2/a'
3439
})
@@ -567,6 +572,32 @@ async function run () {
567572
t.equal(location, '/api/something')
568573
})
569574

575+
test('location headers is preserved when internalRewriteLocationHeader option is false', async t => {
576+
const proxyServer = Fastify()
577+
578+
proxyServer.register(proxy, {
579+
upstream: `http://localhost:${origin.server.address().port}`,
580+
prefix: '/my-prefix',
581+
internalRewriteLocationHeader: false
582+
})
583+
584+
await proxyServer.listen({ port: 0 })
585+
586+
t.teardown(() => {
587+
proxyServer.close()
588+
})
589+
590+
const {
591+
headers: { location }
592+
} = await got(
593+
`http://localhost:${proxyServer.server.address().port}/my-prefix/redirect-to-relative-url`,
594+
{
595+
method: 'POST'
596+
}
597+
)
598+
t.equal(location, '/relative-url')
599+
})
600+
570601
test('passes onResponse option to reply.from() calls', async t => {
571602
const proxyServer = Fastify()
572603

0 commit comments

Comments
 (0)