Chromium Code Reviews| Index: sdk/lib/io/http_body.dart |
| diff --git a/sdk/lib/io/http_body.dart b/sdk/lib/io/http_body.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..61de0cb207ec176a4cf948002ad7690344e9576c |
| --- /dev/null |
| +++ b/sdk/lib/io/http_body.dart |
| @@ -0,0 +1,135 @@ |
| +// 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; |
| + |
| + |
| +/** |
| + * [HttpBodyHandler] is a helper class for parsing and collecting HTTP message |
| + * data in an easy-to-use [HttpBody] object. The content body is parsed, |
| + * depending on the 'Content-Type' header field. |
| + * |
| + * To use with the [HttpServer] for request messages, [HttpBodyHandler] can be |
| + * used as either a [StreamTransformer] or as a per-request handler (see |
| + * [processRequest]). |
| + * |
| + * HttpServer server = ... |
|
Søren Gjesse
2013/04/10 13:20:45
I think you need four space indent for dart doc to
Anders Johnsen
2013/04/10 13:46:06
Done.
|
| + * server.transform(new HttpBodyHandler()) |
| + * .listen((HttpRequestBody body) { |
| + * ... |
| + * }); |
| + * |
| + * To use with the [HttpClient] for response messages, [HttpBodyHandler] can be |
| + * used as a per-request handler (see [processResponse]). |
| + * |
| + * HttpClient client = ... |
| + * client.get(...) |
| + * .then((HttpClientRequest response) => response.close()) |
| + * .then(HttpBodyHandler.processResponse) |
| + * .then((HttpClientResponseBody body) { |
| + * ... |
| + * }); |
| + * |
| + * The following mime-types will be handled specially: |
| + * - text/\* |
| + * - application/json |
| + * |
| + * All other mime-types will be returned as [List<int>]. |
| + */ |
| +class HttpBodyHandler |
| + implements StreamTransformer<HttpRequest, HttpRequestBody> { |
| + factory HttpBodyHandler() => new _HttpBodyHandler(); |
| + |
| + /** |
| + * Process and parse an incoming [HttpRequest]. The returned [HttpRequestBody] |
| + * contains a [response] field for accessing the [HttpResponse]. |
| + */ |
| + static Future<HttpRequestBody> processRequest(HttpRequest request) { |
| + return _HttpBodyHandler.processRequest(request); |
| + } |
| + |
| + /** |
| + * Process and parse an incoming [HttpClientResponse]. |
| + */ |
| + static Future<HttpClientResponseBody> processResponse( |
| + HttpClientResponse response) { |
| + return _HttpBodyHandler.processResponse(response); |
| + } |
| +} |
| + |
| + |
| +/** |
| + * The type of [HttpBody]. |
| + */ |
| +class HttpBodyType { |
| + static const HttpBodyType BINARY = const HttpBodyType("binary"); |
| + static const HttpBodyType TEXT = const HttpBodyType("text"); |
| + static const HttpBodyType JSON = const HttpBodyType("json"); |
| + |
| + final String name; |
|
Mads Ager (google)
2013/04/10 13:01:07
_name
Anders Johnsen
2013/04/10 13:46:06
Done.
|
| + |
| + const HttpBodyType(String this.name); |
|
Mads Ager (google)
2013/04/10 13:01:07
HttpBodyType._(String this.name);
To make sure th
Anders Johnsen
2013/04/10 13:46:06
Done.
|
| + |
| + String toString() => name; |
|
Søren Gjesse
2013/04/10 13:20:45
Change toString to
String toString() => "HttpBody
Anders Johnsen
2013/04/10 13:46:06
Done.
|
| +} |
| + |
| + |
| +/** |
| + * A HTTP content body produced by [HttpBodyHandler] for either [HttpRequest] |
| + * or [HttpClientResponse]. |
| + */ |
| +abstract class HttpBody { |
| + /** |
| + * The content type e.g. application/json, application/octet-stream, |
| + * application/x-www-form-urlencoded, text/plain. |
| + */ |
| + String get mimeType; |
| + |
| + /** |
| + * A high-level type value, that reflects how the body was parsed, e.g. |
| + * [HttpBodyType.BINARY], [HttpBodyType.TEXT] and [HttpBodyType.JSON]. |
| + */ |
| + HttpBodyType get type; |
| + |
| + /** |
| + * The actual body. The type depends on [type]. |
| + */ |
| + dynamic get body; |
| +} |
| + |
| + |
| +/** |
| + * The [HttpBody] of a [HttpClientResponse] will be of type |
| + * [HttpClientResponseBody]. It contains the [HttpClientResponse] object |
| + * for access to the headers. |
| + */ |
| +abstract class HttpClientResponseBody extends HttpBody { |
|
Søren Gjesse
2013/04/10 13:20:45
Add dart doc comments to these getters.
Søren Gjesse
2013/04/10 13:20:45
Doesn't these getters belong to the HttpRequestBod
Anders Johnsen
2013/04/10 13:46:06
Done.
Anders Johnsen
2013/04/10 13:46:06
Done.
|
| + String get method; |
| + |
| + Uri get uri; |
| + |
| + HttpHeaders get headers; |
| + |
| + /** |
| + * The [HttpClientResponse] of the HTTP body. |
| + */ |
| + HttpClientResponse get response; |
| +} |
| + |
| + |
| +/** |
| + * The [HttpBody] of a [HttpRequest] will be of type [HttpRequestBody]. It |
| + * contains the fields used to read all request header information and |
| + * responsing to the client. |
|
Mads Ager (google)
2013/04/10 13:01:07
responding
Anders Johnsen
2013/04/10 13:46:06
Done.
|
| + */ |
| +abstract class HttpRequestBody extends HttpBody { |
|
Søren Gjesse
2013/04/10 13:20:45
Add dart doc comments to these getters.
Søren Gjesse
2013/04/10 13:20:45
Doesn't these getters belong to the HttpClientResp
Anders Johnsen
2013/04/10 13:46:06
Done.
Anders Johnsen
2013/04/10 13:46:06
Done.
|
| + int get statusCode; |
| + String get reasonPhrase; |
| + HttpHeaders get headers; |
| + |
| + /** |
| + * The [HttpResponse] used for responding to the client. |
| + */ |
| + HttpResponse get response; |
| +} |