platform/lib/angel_user_agent.dart
2017-01-25 17:51:33 -05:00

25 lines
840 B
Dart

import 'package:angel_framework/angel_framework.dart';
import 'package:user_agent/user_agent.dart';
export 'package:user_agent/user_agent.dart';
/// Injects a [UserAgent] into requests.
///
/// If [strict] is `true`, then requests without a user agent will be rejected.
RequestMiddleware parseUserAgent({bool strict: true}) {
return (RequestContext req, res) async {
var agentString = req.headers.value('user-agent');
if (agentString == null && strict) {
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));
} else
req.inject(UserAgent, null);
return true;
};
}