diff --git a/README.md b/README.md index 5d357b3..cacf757 100644 --- a/README.md +++ b/README.md @@ -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']`. diff --git a/index.js b/index.js index 4ff9d18..34aaffd 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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) { diff --git a/test/test.js b/test/test.js index 6935d7d..5d56c39 100644 --- a/test/test.js +++ b/test/test.js @@ -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' }) @@ -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()