platform/lib/angel_user_agent.dart

25 lines
790 B
Dart
Raw Normal View History

2016-12-12 04:32:26 +00:00
import 'package:angel_framework/angel_framework.dart';
2017-01-14 19:14:20 +00:00
import 'package:user_agent/user_agent.dart';
export 'package:user_agent/user_agent.dart';
2016-12-12 04:32:26 +00:00
2017-01-14 19:14:20 +00:00
/// Injects a [UserAgent] into requests.
///
/// If [strict] is `true`, then requests without a user agent will be rejected.
2016-12-12 04:32:26 +00:00
RequestMiddleware parseUserAgent({bool strict: true}) {
2017-01-14 19:14:20 +00:00
return (RequestContext req, res) async {
var agentString = req.headers.value('user-agent');
if (agentString == null) {
throw new AngelHttpException.badRequest(
message: 'User-Agent header is required.');
} else if (agentString != null) {
Map<String, List<String>> map = {};
req.headers.forEach((k, v) => map[k] = v);
req.inject(UserAgent, new UserAgent(agentString, headers: map));
2016-12-12 04:32:26 +00:00
}
2017-01-14 19:14:20 +00:00
2016-12-12 04:32:26 +00:00
return true;
};
}