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 |
11 import 'message.dart'; | 11 import 'message.dart'; |
| 12 import 'util.dart'; |
12 | 13 |
13 /// Represents an HTTP request to be processed by a Shelf application. | 14 /// Represents an HTTP request to be processed by a Shelf application. |
14 class Request extends Message { | 15 class Request extends Message { |
15 /// The remainder of the [requestedUri] path and query designating the virtual | 16 /// The remainder of the [requestedUri] path and query designating the virtual |
16 /// "location" of the request's target within the handler. | 17 /// "location" of the request's target within the handler. |
17 /// | 18 /// |
18 /// [url] may be an empty, if [requestedUri]targets the handler | 19 /// [url] may be an empty, if [requestedUri]targets the handler |
19 /// root and does not have a trailing slash. | 20 /// root and does not have a trailing slash. |
20 /// | 21 /// |
21 /// [url] is never null. If it is not empty, it will start with `/`. | 22 /// [url] is never null. If it is not empty, it will start with `/`. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 Stream<List<int>> body, Map<String, Object> context}) | 75 Stream<List<int>> body, Map<String, Object> context}) |
75 : this.requestedUri = requestedUri, | 76 : this.requestedUri = requestedUri, |
76 this.protocolVersion = protocolVersion == null ? | 77 this.protocolVersion = protocolVersion == null ? |
77 '1.1' : protocolVersion, | 78 '1.1' : protocolVersion, |
78 this.url = _computeUrl(requestedUri, url, scriptName), | 79 this.url = _computeUrl(requestedUri, url, scriptName), |
79 this.scriptName = _computeScriptName(requestedUri, url, scriptName), | 80 this.scriptName = _computeScriptName(requestedUri, url, scriptName), |
80 super(body == null ? new Stream.fromIterable([]) : body, | 81 super(body == null ? new Stream.fromIterable([]) : body, |
81 headers: headers, context: context) { | 82 headers: headers, context: context) { |
82 if (method.isEmpty) throw new ArgumentError('method cannot be empty.'); | 83 if (method.isEmpty) throw new ArgumentError('method cannot be empty.'); |
83 | 84 |
84 // TODO(kevmoo) use isAbsolute property on Uri once Issue 18053 is fixed | 85 if (!requestedUri.isAbsolute) { |
85 if (requestedUri.scheme.isEmpty) { | |
86 throw new ArgumentError('requstedUri must be an absolute URI.'); | 86 throw new ArgumentError('requstedUri must be an absolute URI.'); |
87 } | 87 } |
88 | 88 |
89 if (this.scriptName.isNotEmpty && !this.scriptName.startsWith('/')) { | 89 if (this.scriptName.isNotEmpty && !this.scriptName.startsWith('/')) { |
90 throw new ArgumentError('scriptName must be empty or start with "/".'); | 90 throw new ArgumentError('scriptName must be empty or start with "/".'); |
91 } | 91 } |
92 | 92 |
93 if (this.scriptName == '/') { | 93 if (this.scriptName == '/') { |
94 throw new ArgumentError( | 94 throw new ArgumentError( |
95 'scriptName can never be "/". It should be empty instead.'); | 95 'scriptName can never be "/". It should be empty instead.'); |
96 } | 96 } |
97 | 97 |
98 if (this.scriptName.endsWith('/')) { | 98 if (this.scriptName.endsWith('/')) { |
99 throw new ArgumentError('scriptName must not end with "/".'); | 99 throw new ArgumentError('scriptName must not end with "/".'); |
100 } | 100 } |
101 | 101 |
102 if (this.url.path.isNotEmpty && !this.url.path.startsWith('/')) { | 102 if (this.url.path.isNotEmpty && !this.url.path.startsWith('/')) { |
103 throw new ArgumentError('url must be empty or start with "/".'); | 103 throw new ArgumentError('url must be empty or start with "/".'); |
104 } | 104 } |
105 | 105 |
106 if (this.scriptName.isEmpty && this.url.path.isEmpty) { | 106 if (this.scriptName.isEmpty && this.url.path.isEmpty) { |
107 throw new ArgumentError('scriptName and url cannot both be empty.'); | 107 throw new ArgumentError('scriptName and url cannot both be empty.'); |
108 } | 108 } |
109 } | 109 } |
| 110 |
| 111 /// Creates a new [Request] by copying existing values and applying specified |
| 112 /// changes. |
| 113 /// |
| 114 /// New key-value pairs in [context] and [headers] will be added to the copied |
| 115 /// [Request]. |
| 116 /// |
| 117 /// If [context] or [headers] includes a key that already exists, the |
| 118 /// key-value pair will replace the corresponding entry in the copied |
| 119 /// [Request]. |
| 120 /// |
| 121 /// All other context and header values from the [Request] will be included |
| 122 /// in the copied [Request] unchanged. |
| 123 Request change({Map<String, String> headers, Map<String, Object> context}) { |
| 124 headers = updateMap(this.headers, headers); |
| 125 context = updateMap(this.context, context); |
| 126 |
| 127 return new Request(this.method, this.requestedUri, |
| 128 protocolVersion: this.protocolVersion, headers: headers, url: this.url, |
| 129 scriptName: this.scriptName, body: this.read(), context: context); |
| 130 } |
110 } | 131 } |
111 | 132 |
112 /// Computes `url` from the provided [Request] constructor arguments. | 133 /// Computes `url` from the provided [Request] constructor arguments. |
113 /// | 134 /// |
114 /// If [url] and [scriptName] are `null`, infer value from [requestedUrl], | 135 /// If [url] and [scriptName] are `null`, infer value from [requestedUrl], |
115 /// otherwise return [url]. | 136 /// otherwise return [url]. |
116 /// | 137 /// |
117 /// If [url] is provided, but [scriptName] is omitted, throws an | 138 /// If [url] is provided, but [scriptName] is omitted, throws an |
118 /// [ArgumentError]. | 139 /// [ArgumentError]. |
119 Uri _computeUrl(Uri requestedUri, Uri url, String scriptName) { | 140 Uri _computeUrl(Uri requestedUri, Uri url, String scriptName) { |
120 if (url == null && scriptName == null) { | 141 if (url == null && scriptName == null) { |
121 return new Uri(path: requestedUri.path, query: requestedUri.query, | 142 return new Uri(path: requestedUri.path, query: requestedUri.query, |
122 fragment: requestedUri.fragment); | 143 fragment: requestedUri.fragment); |
123 } | 144 } |
124 | 145 |
125 if (url != null && scriptName != null) { | 146 if (url != null && scriptName != null) { |
126 // TODO(kevmoo) use isAbsolute property on Uri once Issue 18053 is fixed | |
127 if (url.scheme.isNotEmpty) throw new ArgumentError('url must be relative.'); | 147 if (url.scheme.isNotEmpty) throw new ArgumentError('url must be relative.'); |
128 return url; | 148 return url; |
129 } | 149 } |
130 | 150 |
131 throw new ArgumentError( | 151 throw new ArgumentError( |
132 'url and scriptName must both be null or both be set.'); | 152 'url and scriptName must both be null or both be set.'); |
133 } | 153 } |
134 | 154 |
135 /// Computes `scriptName` from the provided [Request] constructor arguments. | 155 /// Computes `scriptName` from the provided [Request] constructor arguments. |
136 /// | 156 /// |
137 /// If [url] and [scriptName] are `null` it returns an empty string, otherwise | 157 /// If [url] and [scriptName] are `null` it returns an empty string, otherwise |
138 /// [scriptName] is returned. | 158 /// [scriptName] is returned. |
139 /// | 159 /// |
140 /// If [script] is provided, but [url] is omitted, throws an | 160 /// If [script] is provided, but [url] is omitted, throws an |
141 /// [ArgumentError]. | 161 /// [ArgumentError]. |
142 String _computeScriptName(Uri requstedUri, Uri url, String scriptName) { | 162 String _computeScriptName(Uri requstedUri, Uri url, String scriptName) { |
143 if (url == null && scriptName == null) { | 163 if (url == null && scriptName == null) { |
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 |