Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.io; | |
| 6 | |
| 7 | |
| 8 /** | |
| 9 * [HttpBodyHandler] is a helper class for parsing and collecting HTTP message | |
| 10 * data in an easy-to-use [HttpBody] object. The content body is parsed, | |
| 11 * depending on the 'Content-Type' header field. | |
| 12 * | |
| 13 * To use with the [HttpServer] for request messages, [HttpBodyHandler] can be | |
| 14 * used as either a [StreamTransformer] or as a per-request handler (see | |
| 15 * [processRequest]). | |
| 16 * | |
| 17 * 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.
| |
| 18 * server.transform(new HttpBodyHandler()) | |
| 19 * .listen((HttpRequestBody body) { | |
| 20 * ... | |
| 21 * }); | |
| 22 * | |
| 23 * To use with the [HttpClient] for response messages, [HttpBodyHandler] can be | |
| 24 * used as a per-request handler (see [processResponse]). | |
| 25 * | |
| 26 * HttpClient client = ... | |
| 27 * client.get(...) | |
| 28 * .then((HttpClientRequest response) => response.close()) | |
| 29 * .then(HttpBodyHandler.processResponse) | |
| 30 * .then((HttpClientResponseBody body) { | |
| 31 * ... | |
| 32 * }); | |
| 33 * | |
| 34 * The following mime-types will be handled specially: | |
| 35 * - text/\* | |
| 36 * - application/json | |
| 37 * | |
| 38 * All other mime-types will be returned as [List<int>]. | |
| 39 */ | |
| 40 class HttpBodyHandler | |
| 41 implements StreamTransformer<HttpRequest, HttpRequestBody> { | |
| 42 factory HttpBodyHandler() => new _HttpBodyHandler(); | |
| 43 | |
| 44 /** | |
| 45 * Process and parse an incoming [HttpRequest]. The returned [HttpRequestBody] | |
| 46 * contains a [response] field for accessing the [HttpResponse]. | |
| 47 */ | |
| 48 static Future<HttpRequestBody> processRequest(HttpRequest request) { | |
| 49 return _HttpBodyHandler.processRequest(request); | |
| 50 } | |
| 51 | |
| 52 /** | |
| 53 * Process and parse an incoming [HttpClientResponse]. | |
| 54 */ | |
| 55 static Future<HttpClientResponseBody> processResponse( | |
| 56 HttpClientResponse response) { | |
| 57 return _HttpBodyHandler.processResponse(response); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 | |
| 62 /** | |
| 63 * The type of [HttpBody]. | |
| 64 */ | |
| 65 class HttpBodyType { | |
| 66 static const HttpBodyType BINARY = const HttpBodyType("binary"); | |
| 67 static const HttpBodyType TEXT = const HttpBodyType("text"); | |
| 68 static const HttpBodyType JSON = const HttpBodyType("json"); | |
| 69 | |
| 70 final String name; | |
|
Mads Ager (google)
2013/04/10 13:01:07
_name
Anders Johnsen
2013/04/10 13:46:06
Done.
| |
| 71 | |
| 72 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.
| |
| 73 | |
| 74 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.
| |
| 75 } | |
| 76 | |
| 77 | |
| 78 /** | |
| 79 * A HTTP content body produced by [HttpBodyHandler] for either [HttpRequest] | |
| 80 * or [HttpClientResponse]. | |
| 81 */ | |
| 82 abstract class HttpBody { | |
| 83 /** | |
| 84 * The content type e.g. application/json, application/octet-stream, | |
| 85 * application/x-www-form-urlencoded, text/plain. | |
| 86 */ | |
| 87 String get mimeType; | |
| 88 | |
| 89 /** | |
| 90 * A high-level type value, that reflects how the body was parsed, e.g. | |
| 91 * [HttpBodyType.BINARY], [HttpBodyType.TEXT] and [HttpBodyType.JSON]. | |
| 92 */ | |
| 93 HttpBodyType get type; | |
| 94 | |
| 95 /** | |
| 96 * The actual body. The type depends on [type]. | |
| 97 */ | |
| 98 dynamic get body; | |
| 99 } | |
| 100 | |
| 101 | |
| 102 /** | |
| 103 * The [HttpBody] of a [HttpClientResponse] will be of type | |
| 104 * [HttpClientResponseBody]. It contains the [HttpClientResponse] object | |
| 105 * for access to the headers. | |
| 106 */ | |
| 107 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.
| |
| 108 String get method; | |
| 109 | |
| 110 Uri get uri; | |
| 111 | |
| 112 HttpHeaders get headers; | |
| 113 | |
| 114 /** | |
| 115 * The [HttpClientResponse] of the HTTP body. | |
| 116 */ | |
| 117 HttpClientResponse get response; | |
| 118 } | |
| 119 | |
| 120 | |
| 121 /** | |
| 122 * The [HttpBody] of a [HttpRequest] will be of type [HttpRequestBody]. It | |
| 123 * contains the fields used to read all request header information and | |
| 124 * responsing to the client. | |
|
Mads Ager (google)
2013/04/10 13:01:07
responding
Anders Johnsen
2013/04/10 13:46:06
Done.
| |
| 125 */ | |
| 126 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.
| |
| 127 int get statusCode; | |
| 128 String get reasonPhrase; | |
| 129 HttpHeaders get headers; | |
| 130 | |
| 131 /** | |
| 132 * The [HttpResponse] used for responding to the client. | |
| 133 */ | |
| 134 HttpResponse get response; | |
| 135 } | |
| OLD | NEW |