| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 shelf.request; | 5 library shelf.request; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:http_parser/http_parser.dart'; | 9 import 'package:http_parser/http_parser.dart'; |
| 10 | 10 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 Stream<List<int>> body, Map<String, Object> context}) | 74 Stream<List<int>> body, Map<String, Object> context}) |
| 75 : this.requestedUri = requestedUri, | 75 : this.requestedUri = requestedUri, |
| 76 this.protocolVersion = protocolVersion == null ? | 76 this.protocolVersion = protocolVersion == null ? |
| 77 '1.1' : protocolVersion, | 77 '1.1' : protocolVersion, |
| 78 this.url = _computeUrl(requestedUri, url, scriptName), | 78 this.url = _computeUrl(requestedUri, url, scriptName), |
| 79 this.scriptName = _computeScriptName(requestedUri, url, scriptName), | 79 this.scriptName = _computeScriptName(requestedUri, url, scriptName), |
| 80 super(body == null ? new Stream.fromIterable([]) : body, | 80 super(body == null ? new Stream.fromIterable([]) : body, |
| 81 headers: headers, context: context) { | 81 headers: headers, context: context) { |
| 82 if (method.isEmpty) throw new ArgumentError('method cannot be empty.'); | 82 if (method.isEmpty) throw new ArgumentError('method cannot be empty.'); |
| 83 | 83 |
| 84 // TODO(kevmoo) use isAbsolute property on Uri once Issue 18053 is fixed | 84 if (!requestedUri.isAbsolute) { |
| 85 if (requestedUri.scheme.isEmpty) { | |
| 86 throw new ArgumentError('requstedUri must be an absolute URI.'); | 85 throw new ArgumentError('requstedUri must be an absolute URI.'); |
| 87 } | 86 } |
| 88 | 87 |
| 89 if (this.scriptName.isNotEmpty && !this.scriptName.startsWith('/')) { | 88 if (this.scriptName.isNotEmpty && !this.scriptName.startsWith('/')) { |
| 90 throw new ArgumentError('scriptName must be empty or start with "/".'); | 89 throw new ArgumentError('scriptName must be empty or start with "/".'); |
| 91 } | 90 } |
| 92 | 91 |
| 93 if (this.scriptName == '/') { | 92 if (this.scriptName == '/') { |
| 94 throw new ArgumentError( | 93 throw new ArgumentError( |
| 95 'scriptName can never be "/". It should be empty instead.'); | 94 'scriptName can never be "/". It should be empty instead.'); |
| 96 } | 95 } |
| 97 | 96 |
| 98 if (this.scriptName.endsWith('/')) { | 97 if (this.scriptName.endsWith('/')) { |
| 99 throw new ArgumentError('scriptName must not end with "/".'); | 98 throw new ArgumentError('scriptName must not end with "/".'); |
| 100 } | 99 } |
| 101 | 100 |
| 102 if (this.url.path.isNotEmpty && !this.url.path.startsWith('/')) { | 101 if (this.url.path.isNotEmpty && !this.url.path.startsWith('/')) { |
| 103 throw new ArgumentError('url must be empty or start with "/".'); | 102 throw new ArgumentError('url must be empty or start with "/".'); |
| 104 } | 103 } |
| 105 | 104 |
| 106 if (this.scriptName.isEmpty && this.url.path.isEmpty) { | 105 if (this.scriptName.isEmpty && this.url.path.isEmpty) { |
| 107 throw new ArgumentError('scriptName and url cannot both be empty.'); | 106 throw new ArgumentError('scriptName and url cannot both be empty.'); |
| 108 } | 107 } |
| 109 } | 108 } |
| 109 |
| 110 /// Creates a new [Request] by copying existing values and applying specified |
| 111 /// changes. |
| 112 /// |
| 113 /// New key-value pairs in [context] and [headers] will be added to the copied |
| 114 /// [Request]. |
| 115 /// |
| 116 /// If [context] or [headers] includes a key that already exists, the |
| 117 /// key-value pair will replace the corresponding entry in the copied |
| 118 /// [Request]. |
| 119 /// |
| 120 /// All other context and header values from the [Request] will be included |
| 121 /// in the copied [Request] unchanged. |
| 122 Request change({Map<String, String> headers, Map<String, Object> context}) { |
| 123 headers = updateMap(this.headers, headers); |
| 124 context = updateMap(this.context, context); |
| 125 |
| 126 return new Request(this.method, this.requestedUri, |
| 127 protocolVersion: this.protocolVersion, headers: headers, url: this.url, |
| 128 scriptName: this.scriptName, body: this.read(), context: context); |
| 129 } |
| 110 } | 130 } |
| 111 | 131 |
| 112 /// Computes `url` from the provided [Request] constructor arguments. | 132 /// Computes `url` from the provided [Request] constructor arguments. |
| 113 /// | 133 /// |
| 114 /// If [url] and [scriptName] are `null`, infer value from [requestedUrl], | 134 /// If [url] and [scriptName] are `null`, infer value from [requestedUrl], |
| 115 /// otherwise return [url]. | 135 /// otherwise return [url]. |
| 116 /// | 136 /// |
| 117 /// If [url] is provided, but [scriptName] is omitted, throws an | 137 /// If [url] is provided, but [scriptName] is omitted, throws an |
| 118 /// [ArgumentError]. | 138 /// [ArgumentError]. |
| 119 Uri _computeUrl(Uri requestedUri, Uri url, String scriptName) { | 139 Uri _computeUrl(Uri requestedUri, Uri url, String scriptName) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 144 return ''; | 164 return ''; |
| 145 } | 165 } |
| 146 | 166 |
| 147 if (url != null && scriptName != null) { | 167 if (url != null && scriptName != null) { |
| 148 return scriptName; | 168 return scriptName; |
| 149 } | 169 } |
| 150 | 170 |
| 151 throw new ArgumentError( | 171 throw new ArgumentError( |
| 152 'url and scriptName must both be null or both be set.'); | 172 'url and scriptName must both be null or both be set.'); |
| 153 } | 173 } |
| OLD | NEW |