| 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 = ... |
| 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 * A HTTP content body produced by [HttpBodyHandler] for either [HttpRequest] |
| 64 * or [HttpClientResponse]. |
| 65 */ |
| 66 abstract class HttpBody { |
| 67 /** |
| 68 * The content type e.g. application/json, application/octet-stream, |
| 69 * application/x-www-form-urlencoded, text/plain. |
| 70 */ |
| 71 String get mimeType; |
| 72 |
| 73 /** |
| 74 * A high-level type value, that reflects how the body was parsed, e.g. |
| 75 * "text", "binary" and "json". |
| 76 */ |
| 77 String get type; |
| 78 |
| 79 /** |
| 80 * The actual body. The type depends on [type]. |
| 81 */ |
| 82 dynamic get body; |
| 83 } |
| 84 |
| 85 |
| 86 /** |
| 87 * The [HttpBody] of a [HttpClientResponse] will be of type |
| 88 * [HttpClientResponseBody]. It contains the [HttpClientResponse] object |
| 89 * for access to the headers. |
| 90 */ |
| 91 abstract class HttpClientResponseBody extends HttpBody { |
| 92 /** |
| 93 * Returns the status code. |
| 94 */ |
| 95 int get statusCode; |
| 96 |
| 97 /** |
| 98 * Returns the reason phrase associated with the status code. |
| 99 */ |
| 100 String get reasonPhrase; |
| 101 |
| 102 /** |
| 103 * Returns the response headers. |
| 104 */ |
| 105 HttpHeaders get headers; |
| 106 |
| 107 /** |
| 108 * The [HttpClientResponse] of the HTTP body. |
| 109 */ |
| 110 HttpClientResponse get response; |
| 111 } |
| 112 |
| 113 |
| 114 /** |
| 115 * The [HttpBody] of a [HttpRequest] will be of type [HttpRequestBody]. It |
| 116 * contains the fields used to read all request header information and |
| 117 * responding to the client. |
| 118 */ |
| 119 abstract class HttpRequestBody extends HttpBody { |
| 120 /** |
| 121 * Returns the method for the request. |
| 122 */ |
| 123 String get method; |
| 124 |
| 125 /** |
| 126 * Returns the URI for the request. |
| 127 */ |
| 128 Uri get uri; |
| 129 |
| 130 /** |
| 131 * Returns the request headers. |
| 132 */ |
| 133 HttpHeaders get headers; |
| 134 |
| 135 /** |
| 136 * The [HttpResponse] used for responding to the client. |
| 137 */ |
| 138 HttpResponse get response; |
| 139 } |
| OLD | NEW |