Add 'packages/user_agent/' from commit '70b15207d5e746fca954c5c7107ad8e1be059078'

git-subtree-dir: packages/user_agent
git-subtree-mainline: ae8f7a77d2
git-subtree-split: 70b15207d5
This commit is contained in:
Tobe O 2020-02-15 18:29:14 -05:00
commit a5f5661650
7 changed files with 114 additions and 0 deletions

30
packages/user_agent/.gitignore vendored Normal file
View file

@ -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

View file

@ -0,0 +1,2 @@
# 2.0.0
* Angel 2 + Dart 2 updates

View file

@ -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.

View file

@ -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.

View file

@ -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<UserAgent>();
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}');
}

View file

@ -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<UserAgent>()`.
bool parseUserAgent(RequestContext req, ResponseContext res) {
req.container.registerFactory<UserAgent>((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>(userAgent);
return userAgent;
}
});
return true;
}

View file

@ -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 <thosakwe@gmail.com>
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