Chromium Code Reviews| Index: sdk/lib/io/http_body_impl.dart |
| diff --git a/sdk/lib/io/http_body_impl.dart b/sdk/lib/io/http_body_impl.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..54a01450e8918607aba6019e2afb06ae1d2a5ee0 |
| --- /dev/null |
| +++ b/sdk/lib/io/http_body_impl.dart |
| @@ -0,0 +1,99 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of dart.io; |
| + |
| +class _HttpBodyHandler |
| + extends StreamEventTransformer<HttpRequest, HttpRequestBody> |
| + implements HttpBodyHandler { |
| + |
| + void handleData(HttpRequest request, EventSink<HttpRequestBody> sink) { |
|
Mads Ager (google)
2013/04/10 08:20:26
You need to make this private. It is not part of t
Anders Johnsen
2013/04/10 12:52:49
Done.
|
| + processRequest(request).then(sink.add, onError: sink.addError); |
| + } |
| + |
| + static Future<HttpRequestBody> processRequest(HttpRequest request) { |
| + return process(request, request.headers) |
| + .then((body) => new _HttpRequestBody(request.response, body), |
| + onError: (error) { |
| + // Try to send BAD_REQUEST response. |
| + request.response.statusCode = HttpStatus.BAD_REQUEST; |
| + request.response.close(); |
| + request.response.done.catchError((_) {}); |
| + throw error; |
| + }); |
| + } |
| + |
| + static Future<HttpBody> processResponse(HttpClientResponse response) { |
| + return process(response, response.headers); |
| + } |
| + |
| + static Future<HttpBody> process(Stream<List<int>> stream, |
|
Mads Ager (google)
2013/04/10 08:20:26
You need to make this private, it is not part of t
Anders Johnsen
2013/04/10 12:52:49
It's static, so it should be safe.
|
| + HttpHeaders headers) { |
|
Søren Gjesse
2013/04/10 07:09:11
Indentation.
Anders Johnsen
2013/04/10 12:52:49
Done.
|
| + return stream.fold( |
|
Søren Gjesse
2013/04/10 07:09:11
We could do a stream transformation to String here
Anders Johnsen
2013/04/10 12:52:49
It would complicate stuff, as we would have to fol
|
| + new _BufferList(), |
| + (buffer, data) { |
| + // TODO(ajohnsen): Add limit for POST data. |
| + buffer.add(data); |
| + return buffer; |
| + }) |
| + .then((list) { |
| + var content = list.readBytes(); |
| + int type = HttpBody.BINARY; |
| + var mimeType = headers.contentType.toString(); |
| + String asText(Encoding defaultEncoding) { |
| + var encoding; |
| + var charset = headers.contentType.charset; |
| + if (charset != null) encoding = Encoding.fromName(charset); |
| + if (encoding == null) encoding = defaultEncoding; |
| + return _decodeString(content, encoding); |
| + } |
| + switch (headers.contentType.primaryType) { |
| + case "text": |
| + type = HttpBody.TEXT; |
| + content = asText(Encoding.ASCII); |
| + break; |
| + |
| + case "application": |
| + switch (headers.contentType.subType) { |
| + case "json": |
| + content = JSON.parse(asText(Encoding.UTF_8)); |
| + type = HttpBody.JSON; |
| + break; |
| + |
| + default: |
| + break; |
| + } |
| + break; |
| + |
| + // TODO(ajohnsen): Add support for FORM data. |
|
Mads Ager (google)
2013/04/10 08:20:26
For this commit, remove the FORM type so people ar
Anders Johnsen
2013/04/10 12:52:49
Done.
|
| + |
| + default: |
| + break; |
| + } |
| + return new _HttpBody(mimeType, type, content); |
| + }); |
| + } |
| +} |
| + |
| +class _HttpBody implements HttpBody { |
| + final String mimeType; |
| + final int type; |
| + final dynamic body; |
| + |
| + _HttpBody(String this.mimeType, |
| + int this.type, |
| + dynamic this.body); |
| + |
| + Map get json => type == HttpBody.JSON ? body : null; |
| + List<int> get binary => type == HttpBody.BINARY ? body : null; |
| + Map get form => type == HttpBody.FORM ? body : null; |
|
Mads Ager (google)
2013/04/10 08:20:26
Remove until supported?
Anders Johnsen
2013/04/10 12:52:49
Done.
|
| + String get text => type == HttpBody.TEXT ? body : null; |
| +} |
| + |
| +class _HttpRequestBody extends _HttpBody { |
| + final HttpResponse response; |
| + |
| + _HttpRequestBody(HttpRequest this.response, HttpBody body) |
| + : super(body.mimeType, body.type, body.body); |
| +} |