:)
This commit is contained in:
parent
d6d1153774
commit
cd1c910a13
3 changed files with 18 additions and 26 deletions
|
@ -1,6 +1,6 @@
|
||||||
# cors
|
# 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)
|
![build status](https://travis-ci.org/angel-dart/cors.svg)
|
||||||
|
|
||||||
Angel CORS middleware.
|
Angel CORS middleware.
|
||||||
|
|
|
@ -26,61 +26,53 @@ bool _isOriginAllowed(String origin, allowedOrigin) {
|
||||||
RequestMiddleware cors([CorsOptions options]) {
|
RequestMiddleware cors([CorsOptions options]) {
|
||||||
final opts = options ?? new CorsOptions();
|
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 {
|
return (RequestContext req, ResponseContext res) async {
|
||||||
// Access-Control-Allow-Credentials
|
// Access-Control-Allow-Credentials
|
||||||
if (opts.credentials == true) {
|
if (opts.credentials == true) {
|
||||||
res.header('Access-Control-Allow-Credentials', 'true');
|
res.heades['Access-Control-Allow-Credentials'] = 'true';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Access-Control-Allow-Headers
|
// Access-Control-Allow-Headers
|
||||||
if (req.method == 'OPTIONS' && opts.allowedHeaders.isNotEmpty) {
|
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') {
|
} else if (req.method == 'OPTIONS') {
|
||||||
res.header('Access-Control-Allow-Headers',
|
res.headers['Access-Control-Allow-Headers'] =
|
||||||
req.headers.value('Access-Control-Allow-Headers'));
|
req.headers.value('Access-Control-Allow-Headers');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Access-Control-Expose-Headers
|
// Access-Control-Expose-Headers
|
||||||
if (opts.exposedHeaders.isNotEmpty) {
|
if (opts.exposedHeaders.isNotEmpty) {
|
||||||
res.header(
|
res.headers['Access-Control-Expose-Headers'] =
|
||||||
'Access-Control-Expose-Headers', opts.exposedHeaders.join(','));
|
opts.exposedHeaders.join(',');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Access-Control-Allow-Methods
|
// Access-Control-Allow-Methods
|
||||||
if (req.method == 'OPTIONS' && opts.methods.isNotEmpty) {
|
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
|
// Access-Control-Max-Age
|
||||||
if (req.method == 'OPTIONS' && opts.maxAge != null) {
|
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
|
// Access-Control-Allow-Origin
|
||||||
if (opts.origin == false || opts.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) {
|
} else if (opts.origin is String) {
|
||||||
res
|
res
|
||||||
..header('Access-Control-Allow-Origin', opts.origin)
|
..headers['Access-Control-Allow-Origin'] = opts.origin
|
||||||
..header('Vary', 'Origin');
|
..headers['Vary'] = 'Origin';
|
||||||
} else {
|
} else {
|
||||||
bool isAllowed =
|
bool isAllowed =
|
||||||
_isOriginAllowed(req.headers.value('Origin'), opts.origin);
|
_isOriginAllowed(req.headers.value('Origin'), opts.origin);
|
||||||
|
|
||||||
res.header('Access-Control-Allow-Origin',
|
res.headers['Access-Control-Allow-Origin'] =
|
||||||
isAllowed ? req.headers.value('Origin') : false.toString());
|
isAllowed ? req.headers.value('Origin') : false.toString();
|
||||||
|
|
||||||
if (isAllowed) {
|
if (isAllowed) {
|
||||||
res.header('Vary', 'Origin');
|
res.headers['Vary'] = 'Origin';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,9 @@ environment:
|
||||||
sdk: ">=1.19.0"
|
sdk: ">=1.19.0"
|
||||||
homepage: "https://github.com/angel-dart/cors.git"
|
homepage: "https://github.com/angel-dart/cors.git"
|
||||||
name: "angel_cors"
|
name: "angel_cors"
|
||||||
version: "1.0.0-dev+1"
|
version: "1.0.0-dev+2"
|
||||||
dependencies:
|
dependencies:
|
||||||
angel_framework: "^1.0.0-dev.28"
|
angel_framework: "^1.0.0-dev"
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
http: "^0.11.3+9"
|
http: "^0.11.3+9"
|
||||||
test: "^0.12.17"
|
test: "^0.12.17"
|
||||||
|
|
Loading…
Reference in a new issue