forked from marksteele/edge-rewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
83 lines (69 loc) · 1.8 KB
/
Copy pathhandler.js
File metadata and controls
83 lines (69 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
var rules = require('./api/rules.js');
let rewriteRules;
const applyRules = function(e) {
const req = e.Records[0].cf.request;
const uri = req.uri;
return rewriteRules.reduce((acc, rule) => {
if (acc.skip == true) {
return acc;
}
if (rule.host) {
if (!rule.host.test(req.headers.host[0].value)) {
return acc;
}
}
if (rule.hostRW) {
acc.res.headers.host[0].value = rule.hostRW;
}
var match = rule.regexp.test(req.uri);
// If not match
if (!match) {
// Inverted rewrite
if (rule.inverted) {
acc.res.uri = rule.replace;
acc.skip = rule.last;
return acc;
}
return acc;
}
// Gone
if (rule.gone) {
return {'res': {status: '410',statusDescription: 'Gone'},'skip': rule.last};
}
// Forbidden
if (rule.forbidden) {
return { 'res': { status: '403', statusDescription: 'Forbidden' }, 'skip': rule.last};
}
// Redirect
if (rule.redirect) {
return {
'res': {
status: rule.redirect || 301,
statusDescription: 'Found',
headers: {
location: [{
key: 'Location',
value: uri.replace(rule.regexp, rule.replace),
}],
},
}, 'skip': rule.last
};
}
// Rewrite
if (!rule.inverted) {
if (rule.replace !== '-') {
acc.res.uri = uri.replace(rule.regexp, rule.replace);
}
acc.skip = rule.last;
return acc;
}
}, { 'res': Object.assign({},e.Records[0].cf.request)});
}
module.exports.applyRules = applyRules;
module.exports.handler = (e, ctx, cb) => {
if (rewriteRules === undefined || process.env.IS_TEST) {
rewriteRules = rules.parseRules(rules.loadRules());
}
cb(null,applyRules(e).res);
};