From 0ae8e81913b24f5b86d224b106079ffb0d066d16 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:32:26 -0500 Subject: [PATCH] Create angel.dart --- lib/angel.dart | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lib/angel.dart diff --git a/lib/angel.dart b/lib/angel.dart new file mode 100644 index 00000000..1a7de31e --- /dev/null +++ b/lib/angel.dart @@ -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; + }; +}