Skip to content

Commit

Permalink
Implement redirection handling after bug report
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis Fedoruk-Betschki committed Oct 13, 2024
1 parent 2766962 commit c821941
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ const proxy = httpProxy.createProxyServer({
secure: false,
changeOrigin: true,
selfHandleResponse: true,
// followRedirects: true,
// proxyTimeout: 30000,
});

proxy.on('proxyReq', function (proxyReq, req, res, options) {
Expand All @@ -133,6 +135,11 @@ proxy.on('proxyReq', function (proxyReq, req, res, options) {
proxyReq.setHeader('x-forwarded-for', originalIp as string);
proxyReq.setHeader('x-real-ip', originalIp as string);

// Preserve original headers
// Object.keys(req.headers).forEach(function (key) {
// proxyReq.setHeader(key, req.headers[key] as string);
// });

// Send the raw body to Ghost for legitimate requests
if ((req as any).rawBody && (req as any).rawBody.length > 0) {
proxyReq.setHeader(
Expand All @@ -149,6 +156,16 @@ proxy.on('proxyRes', (proxyRes, req, res) => {
console.log('Headers:', proxyRes.headers);
}

// Handle redirects (status codes 301, 302, 303, 307, 308)
if (proxyRes.statusCode && proxyRes.statusCode >= 300 && proxyRes.statusCode < 400 && proxyRes.headers.location) {
console.log(`Handling redirect: ${proxyRes.statusCode} to ${proxyRes.headers.location}`);
res.writeHead(proxyRes.statusCode, {
'Location': proxyRes.headers.location
});
res.end();
return;
}

// Ensure that we set the headers before streaming the response
res.writeHead(proxyRes.statusCode || 200, proxyRes.headers);

Expand Down

0 comments on commit c821941

Please sign in to comment.