OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library utils; | 5 library utils; |
6 | 6 |
7 import 'dart:uri'; | 7 import 'dart:uri'; |
8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
9 import 'dart:crypto'; | 9 import 'dart:crypto'; |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 /// This will always return a list of two elements or fewer. | 58 /// This will always return a list of two elements or fewer. |
59 List<String> split1(String toSplit, String pattern) { | 59 List<String> split1(String toSplit, String pattern) { |
60 if (toSplit.isEmpty) return <String>[]; | 60 if (toSplit.isEmpty) return <String>[]; |
61 | 61 |
62 var index = toSplit.indexOf(pattern); | 62 var index = toSplit.indexOf(pattern); |
63 if (index == -1) return [toSplit]; | 63 if (index == -1) return [toSplit]; |
64 return [toSplit.substring(0, index), | 64 return [toSplit.substring(0, index), |
65 toSplit.substring(index + pattern.length)]; | 65 toSplit.substring(index + pattern.length)]; |
66 } | 66 } |
67 | 67 |
68 /** A pair of values. */ | |
Bob Nystrom
2012/12/04 01:41:48
If you're creating this type anyway, how about jus
nweiz
2012/12/04 01:51:35
Why would I create a specific type when I could ma
Bob Nystrom
2012/12/04 01:59:42
Why make types at all: They help clarify intent an
nweiz
2012/12/04 21:39:26
Done. I'm still not convinced this is better, but
| |
69 class Pair<E, F> { | |
70 E first; | |
71 F last; | |
72 | |
73 Pair(this.first, this.last); | |
74 | |
75 String toString() => '($first, $last)'; | |
76 | |
77 bool operator==(other) { | |
78 if (other is! Pair) return false; | |
79 return other.first == first && other.last == last; | |
80 } | |
81 } | |
82 | |
83 /// Parses a WWW-Authenticate header value, as per [RFC 2617][]. This returns a | |
84 /// pair where the first value is the authentication scheme, and the second is a | |
85 /// map of parameters. | |
86 /// | |
87 /// Throws a [FormatException] if the header has invalid formatting. | |
88 /// | |
89 /// [RFC 2617]: http://tools.ietf.org/html/rfc2617 | |
90 Pair<String, Map<String, String>> parseAuthenticateHeader(String header) { | |
91 var split = split1(header, ' '); | |
92 if (split.length == 0) { | |
93 throw new FormatException('Invalid WWW-Authenticate header: "$header"'); | |
94 } else if (split.length == 1 || split[1].trim().isEmpty) { | |
95 return new Pair(split[0].toLowerCase(), {}); | |
96 } | |
97 var scheme = split[0].toLowerCase(); | |
98 var paramString = split[1]; | |
99 | |
100 // From http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html. | |
101 var tokenChar = r'[^\0-\x1F()<>@,;:\\"/\[\]?={} \t\x7F]'; | |
102 var quotedStringChar = r'(?:[^\0-\x1F\x7F"]|\\.)'; | |
103 var regexp = new RegExp('^ *($tokenChar+)="($quotedStringChar*)" *(, *)?'); | |
104 | |
105 var params = {}; | |
106 var match; | |
107 do { | |
108 match = regexp.firstMatch(paramString); | |
Bob Nystrom
2012/12/04 01:41:48
Can you use allMatches() here?
nweiz
2012/12/04 01:51:35
No. I need to enforce that each match follows dire
Bob Nystrom
2012/12/04 01:59:42
OK, that's what I figured. Just checking.
| |
109 if (match == null) { | |
110 throw new FormatException('Invalid WWW-Authenticate header: "$header"'); | |
111 } | |
112 | |
113 paramString = paramString.substring(match.end); | |
114 params[match.group(1).toLowerCase()] = match.group(2); | |
115 } while (match.group(3) != null); | |
116 | |
117 if (!paramString.trim().isEmpty) { | |
118 throw new FormatException('Invalid WWW-Authenticate header: "$header"'); | |
119 } | |
120 | |
121 return new Pair<String, Map<String, String>>(scheme, params); | |
122 } | |
123 | |
68 /// Returns a [Future] that asynchronously completes to `null`. | 124 /// Returns a [Future] that asynchronously completes to `null`. |
69 Future get async { | 125 Future get async { |
70 var completer = new Completer(); | 126 var completer = new Completer(); |
71 new Timer(0, (_) => completer.complete(null)); | 127 new Timer(0, (_) => completer.complete(null)); |
72 return completer.future; | 128 return completer.future; |
73 } | 129 } |
OLD | NEW |