This commit is contained in:
thosakwe 2016-12-21 12:43:42 -05:00
parent d6d1153774
commit cd1c910a13
3 changed files with 18 additions and 26 deletions

View file

@ -1,6 +1,6 @@
# cors
![1.0.0-dev+1](https://img.shields.io/badge/version-1.0.0--dev+1-red.svg)
![1.0.0-dev+2](https://img.shields.io/badge/version-1.0.0--dev+2-red.svg)
![build status](https://travis-ci.org/angel-dart/cors.svg)
Angel CORS middleware.

View file

@ -26,61 +26,53 @@ bool _isOriginAllowed(String origin, allowedOrigin) {
RequestMiddleware cors([CorsOptions options]) {
final opts = options ?? new CorsOptions();
/*
print(opts.credentials);
print(opts.allowedHeaders);
print(opts.methods);
print(opts.exposedHeaders);
print(opts.maxAge);
print(opts.origin);
*/
return (RequestContext req, ResponseContext res) async {
// Access-Control-Allow-Credentials
if (opts.credentials == true) {
res.header('Access-Control-Allow-Credentials', 'true');
res.heades['Access-Control-Allow-Credentials'] = 'true';
}
// Access-Control-Allow-Headers
if (req.method == 'OPTIONS' && opts.allowedHeaders.isNotEmpty) {
res.header('Access-Control-Allow-Headers', opts.allowedHeaders.join(','));
res.headers['Access-Control-Allow-Headers'] =
opts.allowedHeaders.join(',');
} else if (req.method == 'OPTIONS') {
res.header('Access-Control-Allow-Headers',
req.headers.value('Access-Control-Allow-Headers'));
res.headers['Access-Control-Allow-Headers'] =
req.headers.value('Access-Control-Allow-Headers');
}
// Access-Control-Expose-Headers
if (opts.exposedHeaders.isNotEmpty) {
res.header(
'Access-Control-Expose-Headers', opts.exposedHeaders.join(','));
res.headers['Access-Control-Expose-Headers'] =
opts.exposedHeaders.join(',');
}
// Access-Control-Allow-Methods
if (req.method == 'OPTIONS' && opts.methods.isNotEmpty) {
res.header('Access-Control-Allow-Methods', opts.methods.join(','));
res.headers['Access-Control-Allow-Methods'] = opts.methods.join(',');
}
// Access-Control-Max-Age
if (req.method == 'OPTIONS' && opts.maxAge != null) {
res.header('Access-Control-Max-Age', opts.maxAge.toString());
res.headers['Access-Control-Max-Age'] = opts.maxAge.toString();
}
// Access-Control-Allow-Origin
if (opts.origin == false || opts.origin == '*') {
res.header('Access-Control-Allow-Origin', '*');
res.headers['Access-Control-Allow-Origin'] = '*';
} else if (opts.origin is String) {
res
..header('Access-Control-Allow-Origin', opts.origin)
..header('Vary', 'Origin');
..headers['Access-Control-Allow-Origin'] = opts.origin
..headers['Vary'] = 'Origin';
} else {
bool isAllowed =
_isOriginAllowed(req.headers.value('Origin'), opts.origin);
res.header('Access-Control-Allow-Origin',
isAllowed ? req.headers.value('Origin') : false.toString());
res.headers['Access-Control-Allow-Origin'] =
isAllowed ? req.headers.value('Origin') : false.toString();
if (isAllowed) {
res.header('Vary', 'Origin');
res.headers['Vary'] = 'Origin';
}
}

View file

@ -4,9 +4,9 @@ environment:
sdk: ">=1.19.0"
homepage: "https://github.com/angel-dart/cors.git"
name: "angel_cors"
version: "1.0.0-dev+1"
version: "1.0.0-dev+2"
dependencies:
angel_framework: "^1.0.0-dev.28"
angel_framework: "^1.0.0-dev"
dev_dependencies:
http: "^0.11.3+9"
test: "^0.12.17"