From 81a4081441e3b17dd2fab2bad5db4cf091836515 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 14 Jan 2017 14:14:20 -0500 Subject: [PATCH] Go --- README.md | 1 + example/basic.dart | 16 ++++++++++++++++ lib/angel_user_agent.dart | 32 +++++++++++++++++--------------- pubspec.yaml | 3 ++- 4 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 example/basic.dart diff --git a/README.md b/README.md index d1125f23..c6753785 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # user_agent Middleware to inject a User Agent object into requests. +For convenience's sake, it also exports the `user_agent` library. \ No newline at end of file diff --git a/example/basic.dart b/example/basic.dart new file mode 100644 index 00000000..1210ca2e --- /dev/null +++ b/example/basic.dart @@ -0,0 +1,16 @@ +import 'dart:io'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_user_agent/angel_user_agent.dart'; + +main() async { + var app = new Angel()..before.add(parseUserAgent(strict: true)); + + app.get( + '/', + (UserAgent ua) => ua.isChrome + ? 'Woohoo! You are running Chrome.' + : 'Sorry, we only support Google Chrome.'); + + var server = await app.startServer(InternetAddress.ANY_IP_V4, 3000); + print('Listening at http://${server.address.address}:${server.port}'); +} diff --git a/lib/angel_user_agent.dart b/lib/angel_user_agent.dart index c4dfb7bf..a2964ba8 100644 --- a/lib/angel_user_agent.dart +++ b/lib/angel_user_agent.dart @@ -1,22 +1,24 @@ import 'package:angel_framework/angel_framework.dart'; -import 'package:ua_parser/ua_parser.dart' as ua; +import 'package:user_agent/user_agent.dart'; +export 'package:user_agent/user_agent.dart'; -/// 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`. +/// Injects a [UserAgent] into requests. +/// +/// If [strict] is `true`, then requests without a user agent will be rejected. RequestMiddleware parseUserAgent({bool strict: true}) { - return (req, res) async { - try { - final client = ua.parse(req.headers.value('User-Agent')); - req - ..inject(ua.Client, client) - ..inject(ua.UserAgent, client.userAgent); - } catch (e) { - throw strict ? - new AngelHttpException.BadRequest(message: 'Invalid user agent.') : e; + 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> map = {}; + req.headers.forEach((k, v) => map[k] = v); + + req.inject(UserAgent, new UserAgent(agentString, headers: map)); } - + return true; }; } diff --git a/pubspec.yaml b/pubspec.yaml index 8c223246..b3bd6641 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -6,4 +6,5 @@ homepage: https://github.com/angel-dart/user_agent environment: sdk: ">=1.19.0" dependencies: - ua_parser: ^1.0.0 + angel_framework: ^1.0.0-dev + user_agent: ^1.0.0-beta