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';
|
2016-12-12 04:32:26 +00:00
|
|
|
|
2018-09-11 21:21:06 +00:00
|
|
|
/// Injects a [UserAgent] factory into requests.
|
2017-01-14 19:14:20 +00:00
|
|
|
///
|
2018-09-11 21:21:06 +00:00
|
|
|
/// Because it is an injected factory, the user agent will not be
|
|
|
|
/// parsed until you request it via `req.container.make<UserAgent>()`.
|
|
|
|
bool parseUserAgent(RequestContext req, ResponseContext res) {
|
|
|
|
req.container.registerFactory<UserAgent>((container) {
|
2017-01-14 19:14:20 +00:00
|
|
|
var agentString = req.headers.value('user-agent');
|
|
|
|
|
2018-09-11 21:21:06 +00:00
|
|
|
if (agentString?.trim()?.isNotEmpty != true) {
|
2017-01-14 19:14:20 +00:00
|
|
|
throw new AngelHttpException.badRequest(
|
|
|
|
message: 'User-Agent header is required.');
|
|
|
|
} else if (agentString != null) {
|
2018-09-11 21:21:06 +00:00
|
|
|
var userAgent = new UserAgent(agentString);
|
|
|
|
container.registerSingleton<UserAgent>(userAgent);
|
|
|
|
return userAgent;
|
|
|
|
}
|
|
|
|
});
|
2017-01-14 19:14:20 +00:00
|
|
|
|
2018-09-11 21:21:06 +00:00
|
|
|
return true;
|
2016-12-12 04:32:26 +00:00
|
|
|
}
|