Skip to content

Commit

Permalink
Use the original URL when requests are forwarded (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgelbg authored Sep 14, 2020
1 parent 0e73dfc commit 1c68a71
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
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

0 comments on commit 1c68a71

Please sign in to comment.