Skip to content

feat: add internalRewriteLocationHeader option #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ configuration passed to the route.

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

### `internalRewriteLocationHeader`
By default, `@fastify/http-proxy` will rewrite the `location` header when a request redirects to a relative path.
In other words, the [prefix](https://github.com/fastify/fastify-http-proxy#prefix) will be added to the relative path.

If you want to preserve the original path, this option will disable this internal operation. Default: `true`.

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.

### `httpMethods`
An array that contains the types of the methods. Default: `['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']`.

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ async function fastifyHttpProxy (fastify, opts) {
fromOpts.base = opts.upstream
fromOpts.prefix = undefined

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

function rewriteHeaders (headers, req) {
const location = headers.location
if (location && !isExternalUrl(location)) {
if (location && !isExternalUrl(location) && internalRewriteLocationHeader) {
headers.location = location.replace(rewritePrefix, fastify.prefix)
}
if (oldRewriteHeaders) {
Expand Down
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ async function run () {
throw new Error('kaboom')
})

origin.post('/redirect-to-relative-url', async (request, reply) => {
reply.header('location', '/relative-url')
return { status: 'ok' }
})

origin.get('/api2/a', async (request, reply) => {
return 'this is /api2/a'
})
Expand Down Expand Up @@ -567,6 +572,32 @@ async function run () {
t.equal(location, '/api/something')
})

test('location headers is preserved when internalRewriteLocationHeader option is false', async t => {
const proxyServer = Fastify()

proxyServer.register(proxy, {
upstream: `http://localhost:${origin.server.address().port}`,
prefix: '/my-prefix',
internalRewriteLocationHeader: false
})

await proxyServer.listen({ port: 0 })

t.teardown(() => {
proxyServer.close()
})

const {
headers: { location }
} = await got(
`http://localhost:${proxyServer.server.address().port}/my-prefix/redirect-to-relative-url`,
{
method: 'POST'
}
)
t.equal(location, '/relative-url')
})

test('passes onResponse option to reply.from() calls', async t => {
const proxyServer = Fastify()

Expand Down