From f3606d05fc0cd5c4d75fd28bb9dd63f9f2c02a20 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:19:55 -0500 Subject: [PATCH 01/17] Initial commit --- .gitignore | 27 +++++++++++++++++++++++++++ LICENSE | 21 +++++++++++++++++++++ README.md | 2 ++ 3 files changed, 50 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7c280441 --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +# See https://www.dartlang.org/tools/private-files.html + +# Files and directories created by pub +.buildlog +.packages +.project +.pub/ +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 diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..15fe44bd --- /dev/null +++ b/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/README.md b/README.md new file mode 100644 index 00000000..5ef0bad3 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# user_agent +User agent parser, with special Angel support. From bac307fcde212c84d98ea94b953aee2093713122 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:24:10 -0500 Subject: [PATCH 02/17] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7c280441..427e911b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .packages .project .pub/ +.scripts-bin/ build/ **/packages/ From 36311e3628d61ae26455bbc8de8e10ced149e8fb Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:25:43 -0500 Subject: [PATCH 03/17] Create user_agent.dart --- lib/user_agent.dart | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 lib/user_agent.dart diff --git a/lib/user_agent.dart b/lib/user_agent.dart new file mode 100644 index 00000000..a4f16d39 --- /dev/null +++ b/lib/user_agent.dart @@ -0,0 +1,6 @@ +library user_agent; + +import 'src/matchers.dart'; +import 'src/user_agent.dart'; + +UserAgent parse(String header) {} From 2d2d08ecd7858b5c7c82504695bb6bc7751e85e2 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:27:25 -0500 Subject: [PATCH 04/17] Update user_agent.dart --- lib/user_agent.dart | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/user_agent.dart b/lib/user_agent.dart index a4f16d39..2d4bc43f 100644 --- a/lib/user_agent.dart +++ b/lib/user_agent.dart @@ -3,4 +3,14 @@ library user_agent; import 'src/matchers.dart'; import 'src/user_agent.dart'; +/// Parses the given header into a user agent. UserAgent parse(String header) {} + +class UserAgentException implements Exception { + final String message; + + UserAgentException(this.message); + + @override + String toString() => 'User Agent exception: $message'; +} From 0ae8e81913b24f5b86d224b106079ffb0d066d16 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:32:26 -0500 Subject: [PATCH 05/17] 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; + }; +} From 794064219ab9b82a496770d270a3913d94279557 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:35:12 -0500 Subject: [PATCH 06/17] Update angel.dart --- lib/angel.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/angel.dart b/lib/angel.dart index 1a7de31e..800b9ac9 100644 --- a/lib/angel.dart +++ b/lib/angel.dart @@ -1,16 +1,16 @@ import 'package:angel_framework/angel_framework.dart'; -import 'user_agent.dart'; +import 'package:user_agent/user_agent.dart' as ua; -/// Injects a [UserAgent] into requests. +/// Injects a [ua.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'))); + req.inject(ua.UserAgent, ua.parse(req.headers.value('User-Agent'))); } catch (e) { - if (e is UserAgentException && strict) { + if (e is ua.UserAgentException && strict) { throw new AngelHttpException.BadRequest(message: 'Invalid user agent.'); } else { rethrow; From 8585a564a1c5e6199b141663e1ccccc7f683a2a6 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:35:20 -0500 Subject: [PATCH 07/17] Delete user_agent.dart --- lib/user_agent.dart | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 lib/user_agent.dart diff --git a/lib/user_agent.dart b/lib/user_agent.dart deleted file mode 100644 index 2d4bc43f..00000000 --- a/lib/user_agent.dart +++ /dev/null @@ -1,16 +0,0 @@ -library user_agent; - -import 'src/matchers.dart'; -import 'src/user_agent.dart'; - -/// Parses the given header into a user agent. -UserAgent parse(String header) {} - -class UserAgentException implements Exception { - final String message; - - UserAgentException(this.message); - - @override - String toString() => 'User Agent exception: $message'; -} From 33a591cbca2ab9084e7c1bc54137e88f50d5803f Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:35:31 -0500 Subject: [PATCH 08/17] Rename angel.dart to user_agent.dart --- lib/{angel.dart => user_agent.dart} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/{angel.dart => user_agent.dart} (100%) diff --git a/lib/angel.dart b/lib/user_agent.dart similarity index 100% rename from lib/angel.dart rename to lib/user_agent.dart From 70cbe72f928d622ec71f549971ce758e526a38b2 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:35:44 -0500 Subject: [PATCH 09/17] Rename user_agent.dart to angel_user_agent.dart --- lib/{user_agent.dart => angel_user_agent.dart} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/{user_agent.dart => angel_user_agent.dart} (100%) diff --git a/lib/user_agent.dart b/lib/angel_user_agent.dart similarity index 100% rename from lib/user_agent.dart rename to lib/angel_user_agent.dart From 6c934143787a64dfc27254e07aff3410bede7b93 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:37:32 -0500 Subject: [PATCH 10/17] Create pubspec.yaml --- pubspec.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 pubspec.yaml diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 00000000..ea733361 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,9 @@ +name: angel_user_agent +version: 1.0.0 +description: Middleware to inject a User Agent object into requests. +author: Tobe O +homepage: https://github.com/angel-dart/user_agent +environment: + sdk: ">=1.19.0" +dependencies: + user_agent: ^1.0.0 From b604b11c8312349f18d5177d99de1b878d215fd2 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 11 Dec 2016 23:37:48 -0500 Subject: [PATCH 11/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5ef0bad3..d1125f23 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # user_agent -User agent parser, with special Angel support. +Middleware to inject a User Agent object into requests. From 211bab2136ff0b0529455d5bb50c466dcf7651de Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 12 Dec 2016 20:03:26 -0500 Subject: [PATCH 12/17] Update pubspec.yaml --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index ea733361..8c223246 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -6,4 +6,4 @@ homepage: https://github.com/angel-dart/user_agent environment: sdk: ">=1.19.0" dependencies: - user_agent: ^1.0.0 + ua_parser: ^1.0.0 From 1d42e8b0ebc42054187eb80b5131a067349e3584 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Mon, 12 Dec 2016 20:05:20 -0500 Subject: [PATCH 13/17] Update angel_user_agent.dart --- lib/angel_user_agent.dart | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/angel_user_agent.dart b/lib/angel_user_agent.dart index 800b9ac9..c4dfb7bf 100644 --- a/lib/angel_user_agent.dart +++ b/lib/angel_user_agent.dart @@ -1,20 +1,20 @@ import 'package:angel_framework/angel_framework.dart'; -import 'package:user_agent/user_agent.dart' as ua; +import 'package:ua_parser/ua_parser.dart' as ua; -/// Injects a [ua.UserAgent] into requests. +/// 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`. RequestMiddleware parseUserAgent({bool strict: true}) { return (req, res) async { try { - req.inject(ua.UserAgent, ua.parse(req.headers.value('User-Agent'))); + final client = ua.parse(req.headers.value('User-Agent')); + req + ..inject(ua.Client, client) + ..inject(ua.UserAgent, client.userAgent); } catch (e) { - if (e is ua.UserAgentException && strict) { - throw new AngelHttpException.BadRequest(message: 'Invalid user agent.'); - } else { - rethrow; - } + throw strict ? + new AngelHttpException.BadRequest(message: 'Invalid user agent.') : e; } return true; From 81a4081441e3b17dd2fab2bad5db4cf091836515 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 14 Jan 2017 14:14:20 -0500 Subject: [PATCH 14/17] 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 From 84f84f9b3ba857531af0404e6d5d8587bcd3eb24 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Wed, 25 Jan 2017 17:51:33 -0500 Subject: [PATCH 15/17] Patch --- lib/angel_user_agent.dart | 5 +++-- pubspec.yaml | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/angel_user_agent.dart b/lib/angel_user_agent.dart index a2964ba8..8a54dfd6 100644 --- a/lib/angel_user_agent.dart +++ b/lib/angel_user_agent.dart @@ -9,7 +9,7 @@ RequestMiddleware parseUserAgent({bool strict: true}) { return (RequestContext req, res) async { var agentString = req.headers.value('user-agent'); - if (agentString == null) { + if (agentString == null && strict) { throw new AngelHttpException.badRequest( message: 'User-Agent header is required.'); } else if (agentString != null) { @@ -17,7 +17,8 @@ RequestMiddleware parseUserAgent({bool strict: true}) { req.headers.forEach((k, v) => map[k] = v); req.inject(UserAgent, new UserAgent(agentString, headers: map)); - } + } else + req.inject(UserAgent, null); return true; }; diff --git a/pubspec.yaml b/pubspec.yaml index b3bd6641..04df76f2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_user_agent -version: 1.0.0 +version: 1.0.1 description: Middleware to inject a User Agent object into requests. author: Tobe O homepage: https://github.com/angel-dart/user_agent From 9df3bbf4254a1ac8cd703a9b121a25b307c7049d Mon Sep 17 00:00:00 2001 From: Tobe O Date: Tue, 11 Sep 2018 17:21:06 -0400 Subject: [PATCH 16/17] Angel 2 updates --- .gitignore | 2 ++ example/basic.dart | 16 ---------------- example/example.dart | 25 +++++++++++++++++++++++++ lib/angel_user_agent.dart | 26 ++++++++++++-------------- pubspec.yaml | 10 +++++----- 5 files changed, 44 insertions(+), 35 deletions(-) delete mode 100644 example/basic.dart create mode 100644 example/example.dart diff --git a/.gitignore b/.gitignore index 427e911b..db7ee919 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ 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/example/basic.dart b/example/basic.dart deleted file mode 100644 index 1210ca2e..00000000 --- a/example/basic.dart +++ /dev/null @@ -1,16 +0,0 @@ -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/example/example.dart b/example/example.dart new file mode 100644 index 00000000..4c98333a --- /dev/null +++ b/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/lib/angel_user_agent.dart b/lib/angel_user_agent.dart index 8a54dfd6..1adbd231 100644 --- a/lib/angel_user_agent.dart +++ b/lib/angel_user_agent.dart @@ -1,25 +1,23 @@ import 'package:angel_framework/angel_framework.dart'; import 'package:user_agent/user_agent.dart'; -export 'package:user_agent/user_agent.dart'; -/// Injects a [UserAgent] into requests. +/// Injects a [UserAgent] factory into requests. /// -/// If [strict] is `true`, then requests without a user agent will be rejected. -RequestMiddleware parseUserAgent({bool strict: true}) { - return (RequestContext req, res) async { +/// 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 == null && strict) { + if (agentString?.trim()?.isNotEmpty != true) { throw new AngelHttpException.badRequest( message: 'User-Agent header is required.'); } else if (agentString != null) { - Map> map = {}; - req.headers.forEach((k, v) => map[k] = v); + var userAgent = new UserAgent(agentString); + container.registerSingleton(userAgent); + return userAgent; + } + }); - req.inject(UserAgent, new UserAgent(agentString, headers: map)); - } else - req.inject(UserAgent, null); - - return true; - }; + return true; } diff --git a/pubspec.yaml b/pubspec.yaml index 04df76f2..23815260 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,10 @@ name: angel_user_agent -version: 1.0.1 -description: Middleware to inject a User Agent object into requests. +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: ">=1.19.0" + sdk: ">=2.0.0-dev <3.0.0" dependencies: - angel_framework: ^1.0.0-dev - user_agent: ^1.0.0-beta + angel_framework: ^2.0.0-alpha + user_agent: ^2.0.0 \ No newline at end of file From 70b15207d5e746fca954c5c7107ad8e1be059078 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Tue, 11 Sep 2018 17:21:27 -0400 Subject: [PATCH 17/17] CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..313c73b7 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +# 2.0.0 +* Angel 2 + Dart 2 updates \ No newline at end of file