| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of http_server; | 5 part of http_server; |
| 6 | 6 |
| 7 class _HttpBodyHandlerTransformer | 7 class _HttpBodyHandlerTransformer |
| 8 implements StreamTransformer<HttpRequest, HttpRequestBody> { | 8 implements StreamTransformer<HttpRequest, HttpRequestBody> { |
| 9 final Encoding _defaultEncoding; | 9 final Encoding _defaultEncoding; |
| 10 | 10 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } | 57 } |
| 58 | 58 |
| 59 static Future<HttpBody> process(Stream<List<int>> stream, | 59 static Future<HttpBody> process(Stream<List<int>> stream, |
| 60 HttpHeaders headers, | 60 HttpHeaders headers, |
| 61 Encoding defaultEncoding) { | 61 Encoding defaultEncoding) { |
| 62 ContentType contentType = headers.contentType; | 62 ContentType contentType = headers.contentType; |
| 63 | 63 |
| 64 Future<HttpBody> asBinary() { | 64 Future<HttpBody> asBinary() { |
| 65 return stream | 65 return stream |
| 66 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) | 66 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) |
| 67 .then((builder) => new _HttpBody(contentType, | 67 .then((builder) => new _HttpBody("binary", builder.takeBytes())); |
| 68 "binary", | |
| 69 builder.takeBytes())); | |
| 70 } | 68 } |
| 71 | 69 |
| 72 Future<HttpBody> asText(Encoding defaultEncoding) { | 70 Future<HttpBody> asText(Encoding defaultEncoding) { |
| 73 var encoding; | 71 var encoding; |
| 74 var charset = contentType.charset; | 72 var charset = contentType.charset; |
| 75 if (charset != null) encoding = Encoding.getByName(charset); | 73 if (charset != null) encoding = Encoding.getByName(charset); |
| 76 if (encoding == null) encoding = defaultEncoding; | 74 if (encoding == null) encoding = defaultEncoding; |
| 77 return stream | 75 return stream |
| 78 .transform(encoding.decoder) | 76 .transform(encoding.decoder) |
| 79 .fold(new StringBuffer(), (buffer, data) => buffer..write(data)) | 77 .fold(new StringBuffer(), (buffer, data) => buffer..write(data)) |
| 80 .then((buffer) => new _HttpBody(contentType, | 78 .then((buffer) => new _HttpBody("text", buffer.toString())); |
| 81 "text", | |
| 82 buffer.toString())); | |
| 83 } | 79 } |
| 84 | 80 |
| 85 Future<HttpBody> asFormData() { | 81 Future<HttpBody> asFormData() { |
| 86 return stream | 82 return stream |
| 87 .transform(new MimeMultipartTransformer( | 83 .transform(new MimeMultipartTransformer( |
| 88 contentType.parameters['boundary'])) | 84 contentType.parameters['boundary'])) |
| 89 .map((part) => HttpMultipartFormData.parse( | 85 .map((part) => HttpMultipartFormData.parse( |
| 90 part, defaultEncoding: defaultEncoding)) | 86 part, defaultEncoding: defaultEncoding)) |
| 91 .map((multipart) { | 87 .map((multipart) { |
| 92 var future; | 88 var future; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 110 return [multipart.contentDisposition.parameters['name'], data]; | 106 return [multipart.contentDisposition.parameters['name'], data]; |
| 111 }); | 107 }); |
| 112 }) | 108 }) |
| 113 .fold([], (l, f) => l..add(f)) | 109 .fold([], (l, f) => l..add(f)) |
| 114 .then(Future.wait) | 110 .then(Future.wait) |
| 115 .then((parts) { | 111 .then((parts) { |
| 116 Map<String, dynamic> map = new Map<String, dynamic>(); | 112 Map<String, dynamic> map = new Map<String, dynamic>(); |
| 117 for (var part in parts) { | 113 for (var part in parts) { |
| 118 map[part[0]] = part[1]; // Override existing entries. | 114 map[part[0]] = part[1]; // Override existing entries. |
| 119 } | 115 } |
| 120 return new _HttpBody(contentType, 'form', map); | 116 return new _HttpBody('form', map); |
| 121 }); | 117 }); |
| 122 } | 118 } |
| 123 | 119 |
| 124 if (contentType == null) { | 120 if (contentType == null) { |
| 125 return asBinary(); | 121 return asBinary(); |
| 126 } | 122 } |
| 127 | 123 |
| 128 switch (contentType.primaryType) { | 124 switch (contentType.primaryType) { |
| 129 case "text": | 125 case "text": |
| 130 return asText(defaultEncoding); | 126 return asText(defaultEncoding); |
| 131 | 127 |
| 132 case "application": | 128 case "application": |
| 133 switch (contentType.subType) { | 129 switch (contentType.subType) { |
| 134 case "json": | 130 case "json": |
| 135 return asText(UTF8) | 131 return asText(UTF8) |
| 136 .then((body) => new _HttpBody(contentType, | 132 .then((body) => new _HttpBody("json", JSON.decode(body.body))); |
| 137 "json", | |
| 138 JSON.decode(body.body))); | |
| 139 | 133 |
| 140 case "x-www-form-urlencoded": | 134 case "x-www-form-urlencoded": |
| 141 return asText(ASCII) | 135 return asText(ASCII) |
| 142 .then((body) { | 136 .then((body) { |
| 143 var map = Uri.splitQueryString(body.body, | 137 var map = Uri.splitQueryString(body.body, |
| 144 encoding: defaultEncoding); | 138 encoding: defaultEncoding); |
| 145 var result = {}; | 139 var result = {}; |
| 146 for (var key in map.keys) { | 140 for (var key in map.keys) { |
| 147 result[key] = map[key]; | 141 result[key] = map[key]; |
| 148 } | 142 } |
| 149 return new _HttpBody(contentType, "form", result); | 143 return new _HttpBody("form", result); |
| 150 }); | 144 }); |
| 151 | 145 |
| 152 default: | 146 default: |
| 153 break; | 147 break; |
| 154 } | 148 } |
| 155 break; | 149 break; |
| 156 | 150 |
| 157 case "multipart": | 151 case "multipart": |
| 158 switch (contentType.subType) { | 152 switch (contentType.subType) { |
| 159 case "form-data": | 153 case "form-data": |
| (...skipping 13 matching lines...) Expand all Loading... |
| 173 } | 167 } |
| 174 | 168 |
| 175 class _HttpBodyFileUpload implements HttpBodyFileUpload { | 169 class _HttpBodyFileUpload implements HttpBodyFileUpload { |
| 176 final ContentType contentType; | 170 final ContentType contentType; |
| 177 final String filename; | 171 final String filename; |
| 178 final dynamic content; | 172 final dynamic content; |
| 179 _HttpBodyFileUpload(this.contentType, this.filename, this.content); | 173 _HttpBodyFileUpload(this.contentType, this.filename, this.content); |
| 180 } | 174 } |
| 181 | 175 |
| 182 class _HttpBody implements HttpBody { | 176 class _HttpBody implements HttpBody { |
| 183 final ContentType contentType; | |
| 184 final String type; | 177 final String type; |
| 185 final dynamic body; | 178 final dynamic body; |
| 186 | 179 |
| 187 _HttpBody(ContentType this.contentType, | 180 _HttpBody(this.type, this.body); |
| 188 String this.type, | |
| 189 dynamic this.body); | |
| 190 } | 181 } |
| 191 | 182 |
| 192 class _HttpRequestBody extends _HttpBody implements HttpRequestBody { | 183 class _HttpRequestBody extends _HttpBody implements HttpRequestBody { |
| 193 final String method; | 184 final HttpRequest request; |
| 194 final Uri uri; | |
| 195 final HttpHeaders headers; | |
| 196 final HttpResponse response; | |
| 197 | 185 |
| 198 _HttpRequestBody(HttpRequest request, HttpBody body) | 186 _HttpRequestBody(this.request, HttpBody body) |
| 199 : super(body.contentType, body.type, body.body), | 187 : super(body.type, body.body); |
| 200 method = request.method, | |
| 201 uri = request.uri, | |
| 202 headers = request.headers, | |
| 203 response = request.response; | |
| 204 } | 188 } |
| 205 | 189 |
| 206 class _HttpClientResponseBody | 190 class _HttpClientResponseBody |
| 207 extends _HttpBody implements HttpClientResponseBody { | 191 extends _HttpBody implements HttpClientResponseBody { |
| 208 final HttpClientResponse response; | 192 final HttpClientResponse response; |
| 209 | 193 |
| 210 _HttpClientResponseBody(HttpClientResponse response, HttpBody body) | 194 _HttpClientResponseBody(this.response, HttpBody body) |
| 211 : super(body.contentType, body.type, body.body), | 195 : super(body.type, body.body); |
| 212 this.response = response; | |
| 213 | |
| 214 int get statusCode => response.statusCode; | |
| 215 | |
| 216 String get reasonPhrase => response.reasonPhrase; | |
| 217 | |
| 218 HttpHeaders get headers => response.headers; | |
| 219 } | 196 } |
| OLD | NEW |