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:
commit
a5f5661650
7 changed files with 114 additions and 0 deletions
30
packages/user_agent/.gitignore
vendored
Normal file
30
packages/user_agent/.gitignore
vendored
Normal 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
|
2
packages/user_agent/CHANGELOG.md
Normal file
2
packages/user_agent/CHANGELOG.md
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
# 2.0.0
|
||||||
|
* Angel 2 + Dart 2 updates
|
21
packages/user_agent/LICENSE
Normal file
21
packages/user_agent/LICENSE
Normal 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.
|
3
packages/user_agent/README.md
Normal file
3
packages/user_agent/README.md
Normal 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.
|
25
packages/user_agent/example/example.dart
Normal file
25
packages/user_agent/example/example.dart
Normal 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}');
|
||||||
|
}
|
23
packages/user_agent/lib/angel_user_agent.dart
Normal file
23
packages/user_agent/lib/angel_user_agent.dart
Normal 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;
|
||||||
|
}
|
10
packages/user_agent/pubspec.yaml
Normal file
10
packages/user_agent/pubspec.yaml
Normal 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
|
Loading…
Reference in a new issue