-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
86 lines (66 loc) · 2.4 KB
/
index.js
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
83
84
85
86
/**
* set route-level cors
* @author coverguo
*/
'use strict';
var CORS_HOST_LIST = {};
function isTopHost(origin, corsHost) {
var lastIndex = origin.lastIndexOf(corsHost);
var originLength = origin.length;
var corsHostLength = corsHost.length;
// console.log('lastIndex', lastIndex);
// console.log('originLength', originLength);
// console.log('corsHostLength', corsHostLength);
if (lastIndex === -1) {
return false;
}
if (originLength - lastIndex !== corsHostLength) {
return false;
}
return true;
}
function routerCors(request, reply) {
//has no origin ,just return;
if (!request.headers.origin) {
return reply.continue();
}
// depending on whether we have a boom or not,
// headers need to be set differently.
var response = request.response.isBoom ? request.response.output : request.response;
//get the route setting
// console.log(request.route.settings);
var settings = request.route.settings;
if (!settings.cors || !settings.cors.origin) {
return reply.continue();
}
var originList = settings.cors.origin;
// remove http:// or https://
var origin = request.headers.origin.replace(/http(s)?:\/\//, '');
originList.forEach(function(corsHost) {
if (origin === corsHost || corsHost === '*' || isTopHost(origin, corsHost)) {
response.headers['access-control-allow-origin'] = request.headers.origin;
response.headers['access-control-allow-credentials'] = 'true';
}
});
if (request.method !== 'options') {
return reply.continue();
}
response.statusCode = 200;
response.headers['access-control-expose-headers'] = 'content-type, content-length, etag';
response.headers['access-control-max-age'] = 60 * 10; // 10 minutes
// dynamically set allowed headers & method
if (request.headers['access-control-request-headers']) {
response.headers['access-control-allow-headers'] = request.headers['access-control-request-headers'];
}
if (request.headers['access-control-request-method']) {
response.headers['access-control-allow-methods'] = request.headers['access-control-request-method'];
}
reply.continue();
}
exports.register = function(server, options, next) {
server.ext('onPreResponse', routerCors);
next();
};
exports.register.attributes = {
pkg: require('./package.json')
};