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

Use the original URL when requests are forwarded #7

Merged
merged 3 commits into from
Sep 14, 2020
Merged

Conversation

jorgelbg
Copy link
Owner

When the request is forwarded to the edge worker instead of running as a transparent proxy (via the routes) the original URL gets lost. This PR defines a new custom header x-original-url for passing the original URL around. If the request is forwarded and the header is found it will be used to extract the domain, path, query for the logging of the visit.

Since the new request should be initialized using the original request, all headers should be passed down to the edge worker, so the referer parsing should still continue to work and not be lost in the process. The new header needs to be manually added to the outgoing request:

let req = new Request('https://dashflare.test.workers.dev', origReq);
req.headers.append('x-original-url', origReq.url)

This is especially relevant for integrations with Cloudflare's Worker Sites because it is not possible to set multiple workers on the same route.

FYI @mre

When the request is forwarded to the edge worker instead of running as a
transparent proxy (via the routes) the original URL gets lost. This PR
defines a new custom header `x-original-url` for passing the original
URL around. If the request is forwarded and the header is found it will
be used to extract the domain, path, query, etc. for the logging of the
visit.
We rely on the header any way as the source of the original URL.
src/handler.ts Outdated
response = new Response('ok', { status: 200 })
url = request.headers.get('x-original-url') || request.url
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait a sec, so request.headers.get returns false if the header doesn't exist? If so, why not only use url = request.headers.get('x-original-url') || request.url and skip the if (request.headers.get('x-original-url') != null) { line altogether? Only in the case where x-original-url is set will it be overwritten. Or am I misunderstanding the logic here?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request.headers.get('x-original-url') returns null|string but it can be used in this short or syntax. In any case I can definitively move the url initialization up like you mentioned:

let url = request.headers.get('x-original-url') || request.url

but we still need the conditional for creating the new Response instance. On the plus side it makes easier the code.

@jorgelbg jorgelbg merged commit 1c68a71 into master Sep 14, 2020
@jorgelbg jorgelbg deleted the x-original-url branch September 14, 2020 18:47
@jorgelbg jorgelbg mentioned this pull request Feb 8, 2021
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants