Skip to content

Commit

Permalink
fix(api): API calls with http Expect header fails
Browse files Browse the repository at this point in the history
Fixes #322.
  • Loading branch information
mountaindude committed Dec 16, 2021
1 parent f67a6db commit 3707f3e
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,41 +149,55 @@ async function build(opts = {}) {
// Configure X-HTTP-Method-Override handling
proxyRestServer.register(FastifyReplyFrom, {
base: `http://localhost:${globals.config.get('Butler.restServerConfig.backgroundServerPort')}`,
http: true,
});

proxyRestServer.get('/*', (request, reply) => {
const { url } = request.raw;
reply.from(url, {
rewriteRequestHeaders: (originalReq, headers) =>
Object.assign(headers, { remoteIP: originalReq.client.remoteAddress }),
rewriteRequestHeaders: (originalReq, headers) => {
Object.assign(headers, { remoteIP: originalReq.client.remoteAddress });
return headers;
},
});
});

proxyRestServer.put('/*', (request, reply) => {
const { url } = request.raw;
reply.from(url, {
rewriteRequestHeaders: (originalReq, headers) =>
Object.assign(headers, { remoteIP: originalReq.client.remoteAddress }),
rewriteRequestHeaders: (originalReq, headers) => {
Object.assign(headers, { remoteIP: originalReq.client.remoteAddress });
return headers;
},
});
});

proxyRestServer.delete('/*', (request, reply) => {
const { url } = request.raw;
reply.from(url, {
rewriteRequestHeaders: (originalReq, headers) =>
Object.assign(headers, { remoteIP: originalReq.client.remoteAddress }),
rewriteRequestHeaders: (originalReq, headers) => {
Object.assign(headers, { remoteIP: originalReq.client.remoteAddress });
return headers;
},
});
});

proxyRestServer.post('/*', (request, reply) => {
const { url } = request.raw;
const { 'x-http-method-override': method = 'POST' } = request.headers;
// eslint-disable-next-line no-param-reassign
reply.request.raw.method = method;
reply.from(url, {
rewriteRequestHeaders: (originalReq, headers) =>
Object.assign(headers, { remoteIP: originalReq.client.remoteAddress }),
});
try {
const { url } = request.raw;
const { 'x-http-method-override': method = 'POST' } = request.headers;

// eslint-disable-next-line no-param-reassign
reply.request.raw.method = method;
reply.from(url, {
rewriteRequestHeaders: (originalReq, headers) => {
Object.assign(headers, { remoteIP: originalReq.client.remoteAddress });
return headers;
},
});
} catch (err) {
globals.logger.error(`Error in POST handler: ${err}`);
}
});

// ---------------------------------------------------
Expand Down

0 comments on commit 3707f3e

Please sign in to comment.