Create angel.dart
This commit is contained in:
parent
2d2d08ecd7
commit
0ae8e81913
1 changed files with 22 additions and 0 deletions
22
lib/angel.dart
Normal file
22
lib/angel.dart
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
|
import 'user_agent.dart';
|
||||||
|
|
||||||
|
/// Injects a [UserAgent] into requests.
|
||||||
|
///
|
||||||
|
/// If [strict] is `true`, then an invalid
|
||||||
|
/// `User-Agent` header will throw a `400 Bad Request`.
|
||||||
|
RequestMiddleware parseUserAgent({bool strict: true}) {
|
||||||
|
return (req, res) async {
|
||||||
|
try {
|
||||||
|
req.inject(UserAgent, parse(req.headers.value('User-Agent')));
|
||||||
|
} catch (e) {
|
||||||
|
if (e is UserAgentException && strict) {
|
||||||
|
throw new AngelHttpException.BadRequest(message: 'Invalid user agent.');
|
||||||
|
} else {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in a new issue