Update angel_user_agent.dart

This commit is contained in:
Tobe O 2016-12-12 20:05:20 -05:00 committed by GitHub
parent 211bab2136
commit 1d42e8b0eb

View file

@ -1,20 +1,20 @@
import 'package:angel_framework/angel_framework.dart';
import 'package:user_agent/user_agent.dart' as ua;
import 'package:ua_parser/ua_parser.dart' as ua;
/// Injects a [ua.UserAgent] into requests.
/// Injects a [ua.Client] and [ua.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(ua.UserAgent, ua.parse(req.headers.value('User-Agent')));
final client = ua.parse(req.headers.value('User-Agent'));
req
..inject(ua.Client, client)
..inject(ua.UserAgent, client.userAgent);
} catch (e) {
if (e is ua.UserAgentException && strict) {
throw new AngelHttpException.BadRequest(message: 'Invalid user agent.');
} else {
rethrow;
}
throw strict ?
new AngelHttpException.BadRequest(message: 'Invalid user agent.') : e;
}
return true;