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 WWW-Authenticate header value, parsed as per [RFC 2617][]. |
| 69 /// |
| 70 /// [RFC 2617]: http://tools.ietf.org/html/rfc2617 |
| 71 class AuthenticateHeader { |
| 72 final String scheme; |
| 73 final Map<String, String> parameters; |
| 74 |
| 75 AuthenticateHeader(this.scheme, this.parameters); |
| 76 |
| 77 /// Parses a header string. Throws a [FormatException] if the header is |
| 78 /// invalid. |
| 79 factory AuthenticateHeader.parse(String header) { |
| 80 var split = split1(header, ' '); |
| 81 if (split.length == 0) { |
| 82 throw new FormatException('Invalid WWW-Authenticate header: "$header"'); |
| 83 } else if (split.length == 1 || split[1].trim().isEmpty) { |
| 84 return new AuthenticateHeader(split[0].toLowerCase(), {}); |
| 85 } |
| 86 var scheme = split[0].toLowerCase(); |
| 87 var paramString = split[1]; |
| 88 |
| 89 // From http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html. |
| 90 var tokenChar = r'[^\0-\x1F()<>@,;:\\"/\[\]?={} \t\x7F]'; |
| 91 var quotedStringChar = r'(?:[^\0-\x1F\x7F"]|\\.)'; |
| 92 var regexp = new RegExp('^ *($tokenChar+)="($quotedStringChar*)" *(, *)?'); |
| 93 |
| 94 var parameters = {}; |
| 95 var match; |
| 96 do { |
| 97 match = regexp.firstMatch(paramString); |
| 98 if (match == null) { |
| 99 throw new FormatException('Invalid WWW-Authenticate header: "$header"'); |
| 100 } |
| 101 |
| 102 paramString = paramString.substring(match.end); |
| 103 parameters[match.group(1).toLowerCase()] = match.group(2); |
| 104 } while (match.group(3) != null); |
| 105 |
| 106 if (!paramString.trim().isEmpty) { |
| 107 throw new FormatException('Invalid WWW-Authenticate header: "$header"'); |
| 108 } |
| 109 |
| 110 return new AuthenticateHeader(scheme, parameters); |
| 111 } |
| 112 } |
| 113 |
68 /// Returns a [Future] that asynchronously completes to `null`. | 114 /// Returns a [Future] that asynchronously completes to `null`. |
69 Future get async { | 115 Future get async { |
70 var completer = new Completer(); | 116 var completer = new Completer(); |
71 new Timer(0, (_) => completer.complete(null)); | 117 new Timer(0, (_) => completer.complete(null)); |
72 return completer.future; | 118 return completer.future; |
73 } | 119 } |
OLD | NEW |