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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,34 +132,38 @@ async function handleRequest(event: FetchEvent): Promise<Response> {
}

let response: Response
if (request.url.includes('forward=true') == true) {
let url = request.headers.get('x-original-url') || request.url
// If the request contains a 'x-original-url' header we understand that this request is forwarded
// to the worker and that therefor the upstream should not be fetched. We use a custom header to
// change as little as possible from the original request.
if (request.headers.get('x-original-url') != null) {
response = new Response('ok', { status: 200 })
} else {
// fetch the original request
console.log(`Fetching origin ${request.url}`)
response = await fetch(request.url, request)
}

if (EXCLUDE.js && JAVASCRIPT_REGEX.test(request.url)) {
if (EXCLUDE.js && JAVASCRIPT_REGEX.test(url)) {
return response
}

if (EXCLUDE.css && CSS_REGEX.test(request.url)) {
if (EXCLUDE.css && CSS_REGEX.test(url)) {
return response
}

if (EXCLUDE.images && IMAGE_REGEX.test(request.url)) {
if (EXCLUDE.images && IMAGE_REGEX.test(url)) {
return response
}

let parsed = new URL(request.url)
let parsed = new URL(url)
let userAgent = request.headers.get('user-agent')

parser.setUA(`${userAgent}`)

let labels = {
method: request.method,
url: request.url,
url: url,
status: response.status,
referer: request.headers.get('referer'),
user_agent: userAgent,
Expand Down
36 changes: 35 additions & 1 deletion test/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ describe('request handler', () => {

it('sends payload to storage', async () => {
const headers: HeadersInit = new Headers({
host: 'some.google.host',
'x-forwarded-proto': 'https',
'cf-ipcountry': 'US',
'cf-connecting-ip': '17.110.220.180',
host: 'some.google.host',
'user-agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9',
})
Expand All @@ -38,6 +38,40 @@ describe('request handler', () => {
)
expect(body).to.not.include('17.110.220.180')
})

it('avoid fetching upstream when the URL is forwarded', async () => {
const headers: HeadersInit = new Headers({
host: 'dashflare.test.workers.dev',
'x-forwarded-proto': 'https',
'cf-ipcountry': 'US',
'cf-connecting-ip': '17.110.220.180',
'user-agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9',
'x-original-url': 'https://example.com',
referer: 'https://t.co/gJV9DEbJVy',
})

const event = new FetchEvent('fetch', {
request: new Request('https://dashflare.test.workers.dev', {
headers,
}),
})

const res = await handleRequest(event)
expect(await res.text()).to.equal('ok')

let body = fetchMock.calls('http://loki:3100/api/prom/push')[1][1].body
expect(body).to.satisfy((string) =>
[
'status=200',
// domain is extracted from the x-original-url header
'domain=example.com',
// the referer of the original request is detected and parsed
'network=twitter',
'type=social',
].every((bit) => string.includes(bit)),
)
})
})

describe('preprocessing', () => {
Expand Down