Skip to content
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

Proxy request method, headers and body with sendProxy() #224

Closed
P4sca1 opened this issue Nov 2, 2022 · 2 comments · Fixed by #226
Closed

Proxy request method, headers and body with sendProxy() #224

P4sca1 opened this issue Nov 2, 2022 · 2 comments · Fixed by #226
Labels
enhancement New feature or request

Comments

@P4sca1
Copy link

P4sca1 commented Nov 2, 2022

I would expect sendProxy to copy method, headers and body from the original request.

// Copy request method
const method = event.req.method

// Copy request headers
const ignoredHeaders = [
	// https://github.com/nodejs/undici/blob/cd78eb57203ce85a26312290c1bef4017e30cf74/lib/core/request.js#L196-L220
	'transfer-encoding',
	'connection',
	'keep-alive',
	'upgrade',
	'expect',
]
const headers: HeadersInit = {}
for (const [header, value] of Object.entries(getRequestHeaders(event))) {
	if (value === undefined || ignoredHeaders.includes(header)) continue
	headers[header] = value
}
// TODO: Set x-forwarder-* headers?

// Copy request body
const payloadMethods = ['PATCH', 'POST', 'PUT', 'DELETE']
const body = payloadMethods.includes(method)
	? await (readRawBody(event, false) as unknown as Promise<Buffer>) // https://github.com/unjs/h3/issues/223
	: undefined

sendProxy(event, target.toString(), {
	fetchOptions: { method, headers, body },
})
@pi0 pi0 changed the title Extend sendProxy functionality Proxy request method, headers and body with sendProxy() Nov 3, 2022
@pi0 pi0 added the enhancement New feature or request label Nov 3, 2022
@pi0
Copy link
Member

pi0 commented Nov 3, 2022

Normally sendProxy is an explicit method to send response as proxy. We can create few options like passHeaders: true or proxyHeaders: true (same for body and method) to extend functionality.

@pi0
Copy link
Member

pi0 commented Nov 3, 2022

Added a basic implementation in #226

Feel free to open a PR to improve options. I think both X-Forwarded-For support and making ignore configurable are good ideas!

@pi0 pi0 closed this as completed in #226 Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants