Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Unified Diff: pkg/oauth2/lib/src/utils.dart

Issue 11420025: Add a package for authenticating via OAuth2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Misc fixes Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/oauth2/lib/src/utils.dart
diff --git a/pkg/oauth2/lib/src/utils.dart b/pkg/oauth2/lib/src/utils.dart
new file mode 100644
index 0000000000000000000000000000000000000000..3c8547b5d96e7499c03cc776b3bc95ee3b20ed15
--- /dev/null
+++ b/pkg/oauth2/lib/src/utils.dart
@@ -0,0 +1,73 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library utils;
+
+import 'dart:uri';
+import 'dart:isolate';
+import 'dart:crypto';
+
+/// Adds additional query parameters to `url`, overwriting the original
+/// parameters if a name conflict occurs.
+Uri addQueryParameters(Uri url, Map<String, String> parameters) {
+ var queryMap = queryToMap(url.query);
+ mapAddAll(queryMap, parameters);
+ return url.resolve("?${mapToQuery(queryMap)}");
+}
+
+/// Convert a URL query string (or `application/x-www-form-urlencoded` body)
+/// into a [Map] from parameter names to values.
+Map<String, String> queryToMap(String queryList) {
+ var map = <String>{};
+ for (var pair in queryList.split("&")) {
+ var split = split1(pair, "=");
+ if (split.isEmpty) continue;
+ var key = urlDecode(split[0]);
+ var value = urlDecode(split.length > 1 ? split[1] : "");
Bob Nystrom 2012/11/16 19:53:30 var value = split.length > 1 ? urlDecode(split[1])
nweiz 2012/11/17 01:06:27 Done.
+ map[key] = value;
+ }
+ return map;
+}
+
+/// Convert a [Map] from parameter names to values to a URL query string.
+String mapToQuery(Map<String, String> map) {
+ var pairs = <List<String>>[];
+ map.forEach((key, value) {
+ key = encodeUriComponent(key);
+ value = (value == null || value.isEmpty) ? null : encodeUriComponent(value);
+ pairs.add([key, value]);
+ });
+ return Strings.join(pairs.map((pair) {
+ if (pair[1] == null) return pair[0];
+ return "${pair[0]}=${pair[1]}";
+ }), "&");
Bob Nystrom 2012/11/16 19:53:30 Indent the preceding three lines - 2, like in the
nweiz 2012/11/17 01:06:27 Done.
+}
+
+/// Add all key/value pairs from [source] to [destination], overwriting any
+/// pre-existing values.
+void mapAddAll(Map destination, Map source) =>
+ source.forEach((key, value) => destination[key] = value);
+
+/// Decode a URL-encoded string. Unlike [decodeUriComponent], this includes
+/// replacing `+` with ` `.
+String urlDecode(String encoded) =>
+ decodeUriComponent(encoded.replaceAll("+", " "));
+
+/// Like [String.split], but only splits on the first occurrence of the pattern.
+/// This will always return an array of two elements or fewer.
Bob Nystrom 2012/11/16 19:53:30 "array" -> "list".
nweiz 2012/11/17 01:06:27 Done.
+List<String> split1(String toSplit, String pattern) {
+ if (toSplit.isEmpty) return <String>[];
+
+ var index = toSplit.indexOf(pattern);
+ if (index == -1) return [toSplit];
+ return [toSplit.substring(0, index),
+ toSplit.substring(index + pattern.length)];
Bob Nystrom 2012/11/16 19:53:30 Indent +2.
nweiz 2012/11/17 01:06:27 Done.
+}
+
+/// Returns a [Future] that asynchronously completes to `null`.
+Future get async {
+ var completer = new Completer();
+ new Timer(0, (_) => completer.complete(null));
+ return completer.future;
+}

Powered by Google App Engine
This is Rietveld 408576698