diff --git a/packages/user_agent/.gitignore b/packages/user_agent/.gitignore new file mode 100644 index 00000000..db7ee919 --- /dev/null +++ b/packages/user_agent/.gitignore @@ -0,0 +1,30 @@ +# See https://www.dartlang.org/tools/private-files.html + +# Files and directories created by pub +.buildlog +.packages +.project +.pub/ +.scripts-bin/ +build/ +**/packages/ + +# Files created by dart2js +# (Most Dart developers will use pub build to compile Dart, use/modify these +# rules if you intend to use dart2js directly +# Convention is to use extension '.dart.js' for Dart compiled to Javascript to +# differentiate from explicit Javascript files) +*.dart.js +*.part.js +*.js.deps +*.js.map +*.info.json + +# Directory created by dartdoc +doc/api/ + +# Don't commit pubspec lock file +# (Library packages only! Remove pattern if developing an application package) +pubspec.lock + +.dart_tool \ No newline at end of file diff --git a/packages/user_agent/CHANGELOG.md b/packages/user_agent/CHANGELOG.md new file mode 100644 index 00000000..313c73b7 --- /dev/null +++ b/packages/user_agent/CHANGELOG.md @@ -0,0 +1,2 @@ +# 2.0.0 +* Angel 2 + Dart 2 updates \ No newline at end of file diff --git a/packages/user_agent/LICENSE b/packages/user_agent/LICENSE new file mode 100644 index 00000000..15fe44bd --- /dev/null +++ b/packages/user_agent/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 The Angel Framework + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/user_agent/README.md b/packages/user_agent/README.md new file mode 100644 index 00000000..c6753785 --- /dev/null +++ b/packages/user_agent/README.md @@ -0,0 +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/packages/user_agent/example/example.dart b/packages/user_agent/example/example.dart new file mode 100644 index 00000000..4c98333a --- /dev/null +++ b/packages/user_agent/example/example.dart @@ -0,0 +1,25 @@ +import 'dart:io'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_user_agent/angel_user_agent.dart'; +import 'package:user_agent/user_agent.dart'; + +main() async { + var app = new Angel(); + var http = new AngelHttp(app); + + app.get( + '/', + waterfall([ + parseUserAgent, + (req, res) { + var ua = req.container.make(); + return ua.isChrome + ? 'Woohoo! You are running Chrome.' + : 'Sorry, we only support Google Chrome.'; + }, + ]), + ); + + var server = await http.startServer(InternetAddress.anyIPv4, 3000); + print('Listening at http://${server.address.address}:${server.port}'); +} diff --git a/packages/user_agent/lib/angel_user_agent.dart b/packages/user_agent/lib/angel_user_agent.dart new file mode 100644 index 00000000..1adbd231 --- /dev/null +++ b/packages/user_agent/lib/angel_user_agent.dart @@ -0,0 +1,23 @@ +import 'package:angel_framework/angel_framework.dart'; +import 'package:user_agent/user_agent.dart'; + +/// Injects a [UserAgent] factory into requests. +/// +/// Because it is an injected factory, the user agent will not be +/// parsed until you request it via `req.container.make()`. +bool parseUserAgent(RequestContext req, ResponseContext res) { + req.container.registerFactory((container) { + var agentString = req.headers.value('user-agent'); + + if (agentString?.trim()?.isNotEmpty != true) { + throw new AngelHttpException.badRequest( + message: 'User-Agent header is required.'); + } else if (agentString != null) { + var userAgent = new UserAgent(agentString); + container.registerSingleton(userAgent); + return userAgent; + } + }); + + return true; +} diff --git a/packages/user_agent/pubspec.yaml b/packages/user_agent/pubspec.yaml new file mode 100644 index 00000000..23815260 --- /dev/null +++ b/packages/user_agent/pubspec.yaml @@ -0,0 +1,10 @@ +name: angel_user_agent +version: 2.0.0 +description: Angel middleware to parse and inject a User Agent object into requests. +author: Tobe O +homepage: https://github.com/angel-dart/user_agent +environment: + sdk: ">=2.0.0-dev <3.0.0" +dependencies: + angel_framework: ^2.0.0-alpha + user_agent: ^2.0.0 \ No newline at end of file