Skip to content

Commit 90d674e

Browse files
authored
Merge pull request #32 from KnpLabs/change/manage-complete-url-redirection
Match the url from start for redirections
2 parents 8ded449 + f90bdc0 commit 90d674e

File tree

6 files changed

+34
-26
lines changed

6 files changed

+34
-26
lines changed

.env.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ MANAGER_HTTP_SERVER_HOST=0.0.0.0
1414
WORKER_ENABLED=1
1515
WORKER_RENDERER_AUTHORIZED_REQUEST_DOMAINS=*
1616
WORKER_RENDERER_AUTHORIZED_REQUEST_RESOURCES=*
17-
WORKER_RENDERER_REDIRECTED_DOMAINS=external-nginx|nginx
17+
WORKER_RENDERER_REDIRECTIONS=http://external-nginx|http://nginx

docker-compose.test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ services:
2424
- MANAGER_ENABLED=0
2525
- WORKER_ENABLED=1
2626
- QUEUE_REDIS_DSN=redis://redis:6379
27-
- WORKER_RENDERER_REDIRECTED_DOMAINS=external-nginx|nginx
27+
- WORKER_RENDERER_REDIRECTIONS=http://external-nginx|http://nginx
2828

2929
redis:
3030
image: redis:6.2.2-buster

src/configuration.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const isManagerConfigurationValid = T
3535

3636
// isWorkerConfigurationValid :: Configuration -> Boolean
3737
const isWorkerConfigurationValid = pipe(
38-
path(['worker', 'renderer', 'domain_redirections']),
38+
path(['worker', 'renderer', 'redirections']),
3939
reduce(
4040
(acc, { from, to }) => unless(
4141
equals(false),
@@ -96,11 +96,11 @@ const generate = () => ({
9696
authorized_request_resources: commaSeparatedStringToArray(
9797
process.env.WORKER_RENDERER_AUTHORIZED_REQUEST_RESOURCES ?? '*',
9898
),
99-
domain_redirections: pipe(
99+
redirections: pipe(
100100
commaSeparatedStringToArray,
101101
map(pipeSeparatedStringToArray),
102102
map(([from, to]) => ({ from, to })),
103-
)(process.env.WORKER_RENDERER_REDIRECTED_DOMAINS ?? ''),
103+
)(process.env.WORKER_RENDERER_REDIRECTIONS ?? ''),
104104
},
105105
},
106106
})

src/configuration.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('configuration :: createConfiguration', () => {
4141
authorized_request_resources: [
4242
'*',
4343
],
44-
domain_redirections: [],
44+
redirections: [],
4545
},
4646
},
4747
})
@@ -77,7 +77,7 @@ describe('configuration :: createConfiguration', () => {
7777
authorized_request_resources: [
7878
'*',
7979
],
80-
domain_redirections: [],
80+
redirections: [],
8181
},
8282
},
8383
})
@@ -113,7 +113,7 @@ describe('configuration :: createConfiguration', () => {
113113
authorized_request_resources: [
114114
'*',
115115
],
116-
domain_redirections: [],
116+
redirections: [],
117117
},
118118
},
119119
})
@@ -129,7 +129,7 @@ describe('configuration :: createConfiguration', () => {
129129
process.env.WORKER_ENABLED = 1
130130
process.env.WORKER_RENDERER_AUTHORIZED_REQUEST_DOMAINS = 'localhost, nginx'
131131
process.env.WORKER_RENDERER_AUTHORIZED_REQUEST_RESOURCES = 'document, script'
132-
process.env.WORKER_RENDERER_REDIRECTED_DOMAINS = 'example.com|nginx'
132+
process.env.WORKER_RENDERER_REDIRECTIONS = 'http://example.com|http://nginx'
133133

134134
expect(createConfiguration()).toStrictEqual({
135135
log: {
@@ -159,9 +159,9 @@ describe('configuration :: createConfiguration', () => {
159159
'document',
160160
'script',
161161
],
162-
domain_redirections: [{
163-
from: 'example.com',
164-
to: 'nginx',
162+
redirections: [{
163+
from: 'http://example.com',
164+
to: 'http://nginx',
165165
}],
166166
},
167167
},
@@ -181,7 +181,7 @@ describe('configuration :: createConfiguration', () => {
181181

182182
it(`throws an exception when the worker configuration is invalid`, () => {
183183
process.env.QUEUE_REDIS_DSN = 'redis://redis:6379'
184-
process.env.WORKER_RENDERER_REDIRECTED_DOMAINS = 'example.com|'
184+
process.env.WORKER_RENDERER_REDIRECTIONS = 'http://example.com|'
185185

186186
expect(() => createConfiguration()).toThrow('Invalid configuration.')
187187
})

src/worker/renderers/chrome/browserRequestHandler.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { T, anyPass, complement, cond, equals, find, ifElse, isNil, pipe, propEq, test } from 'ramda'
1+
import { T, anyPass, complement, cond, equals, find, ifElse, isNil, pipe, replace, tap, test } from 'ramda'
22

33
// resolveRequestDomain :: Request -> String
44
const resolveRequestDomain = req => req.url().match(/^(https?:\/\/)?(?<host>[^/]+)/).groups.host
@@ -29,19 +29,23 @@ const isRequestResourceAuthorized = authorizedRequestResources => pipe(
2929
complement(isNil),
3030
)
3131

32-
// getDomainRedirection :: [Object] -> Request -> Object|Null
33-
const getDomainRedirection = domainRedirections => domain => find(propEq('from', domain), domainRedirections)
32+
// getRedirection :: [Object] -> Request -> Object|Null
33+
const getRedirection = redirections => req => find(
34+
({ from }) => test(new RegExp(`^${from}`, 'i'), req.url()),
35+
redirections,
36+
)
3437

35-
// allowRequest :: Request -> _
36-
const allowRequest = domainRedirections => req => pipe(
37-
resolveRequestDomain,
38-
getDomainRedirection(domainRedirections),
38+
// allowRequest :: (logger, [Object]) -> Request -> _
39+
const allowRequest = (logger, redirections) => req => pipe(
40+
getRedirection(redirections),
3941
ifElse(
4042
isNil,
4143
() => req.continue(),
42-
({ from, to }) => req.continue({
43-
url: req.url().replace(from, to),
44-
}),
44+
pipe(
45+
({ from, to }) => replace(new RegExp(`^${from}`, 'i'), to, req.url()),
46+
tap(redirectedUrl => logger.debug(`Redirecting "${req.url()}" to ${redirectedUrl}`)),
47+
redirectedUrl => req.continue({ url: redirectedUrl }),
48+
),
4549
),
4650
)(req)
4751

@@ -58,5 +62,5 @@ export default (configuration, logger) => cond([
5862
complement(isRequestResourceAuthorized(configuration.worker.renderer.authorized_request_resources)),
5963
blockRequest(logger, 'resource type'),
6064
],
61-
[T, allowRequest(configuration.worker.renderer.domain_redirections)],
65+
[T, allowRequest(logger, configuration.worker.renderer.redirections)],
6266
])

src/worker/renderers/chrome/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { apply } from 'ramda'
1+
import { apply, map, pipe } from 'ramda'
22
import browserRequestHandler from './browserRequestHandler'
33
import { formatException } from './../../../logger'
44
import getBrowserProvider from './browserProvider'
@@ -13,7 +13,11 @@ const renderPageContent = async (configuration, logger, browserInstance, url) =>
1313
page.on('error', error => logger.error(formatException(error)))
1414
page.on('pageerror', error => logger.error(formatException(error)))
1515
page.on('requestfailed', req => logger.debug(`Browser request failed. ${req.url()}.`))
16-
page.on('console', msg => apply(logger.debug, msg.args()))
16+
page.on('console', pipe(
17+
msg => msg.args(),
18+
map(arg => arg.toString()),
19+
apply(logger.debug),
20+
))
1721

1822
await page.goto(url, {
1923
waitUntil: 'networkidle0',

0 commit comments

Comments
 (0)