|
| 1 | +// Copyright (c) 2017, 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 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | + |
| 8 | +import 'package:async/async.dart'; |
| 9 | +import 'package:collection/collection.dart'; |
| 10 | + |
| 11 | +/// The body of a request or response. |
| 12 | +/// |
| 13 | +/// This tracks whether the body has been read. It's separate from [Message] |
| 14 | +/// because the message may be changed with [Message.change], but each instance |
| 15 | +/// should share a notion of whether the body was read. |
| 16 | +class Body { |
| 17 | + /// The contents of the message body. |
| 18 | + /// |
| 19 | + /// This will be `null` after [read] is called. |
| 20 | + Stream<List<int>> _stream; |
| 21 | + |
| 22 | + /// The encoding used to encode the stream returned by [read], or `null` if no |
| 23 | + /// encoding was used. |
| 24 | + final Encoding encoding; |
| 25 | + |
| 26 | + /// The length of the stream returned by [read], or `null` if that can't be |
| 27 | + /// determined efficiently. |
| 28 | + final int contentLength; |
| 29 | + |
| 30 | + Body._(this._stream, this.encoding, this.contentLength); |
| 31 | + |
| 32 | + /// Converts [body] to a byte stream and wraps it in a [Body]. |
| 33 | + /// |
| 34 | + /// [body] may be either a [Body], a [String], a [List<int>], a |
| 35 | + /// [Stream<List<int>>], or `null`. If it's a [String], [encoding] will be |
| 36 | + /// used to convert it to a [Stream<List<int>>]. |
| 37 | + factory Body(body, [Encoding encoding]) { |
| 38 | + if (body is Body) return body; |
| 39 | + |
| 40 | + Stream<List<int>> stream; |
| 41 | + int contentLength; |
| 42 | + if (body == null) { |
| 43 | + contentLength = 0; |
| 44 | + stream = new Stream.fromIterable([]); |
| 45 | + } else if (body is String) { |
| 46 | + if (encoding == null) { |
| 47 | + var encoded = UTF8.encode(body); |
| 48 | + // If the text is plain ASCII, don't modify the encoding. This means |
| 49 | + // that an encoding of "text/plain" will stay put. |
| 50 | + if (!_isPlainAscii(encoded, body.length)) encoding = UTF8; |
| 51 | + contentLength = encoded.length; |
| 52 | + stream = new Stream.fromIterable([encoded]); |
| 53 | + } else { |
| 54 | + var encoded = encoding.encode(body); |
| 55 | + contentLength = encoded.length; |
| 56 | + stream = new Stream.fromIterable([encoded]); |
| 57 | + } |
| 58 | + } else if (body is List) { |
| 59 | + contentLength = body.length; |
| 60 | + stream = new Stream.fromIterable([DelegatingList.typed(body)]); |
| 61 | + } else if (body is Stream) { |
| 62 | + stream = DelegatingStream.typed(body); |
| 63 | + } else { |
| 64 | + throw new ArgumentError('Response body "$body" must be a String or a ' |
| 65 | + 'Stream.'); |
| 66 | + } |
| 67 | + |
| 68 | + return new Body._(stream, encoding, contentLength); |
| 69 | + } |
| 70 | + |
| 71 | + /// Returns whether [bytes] is plain ASCII. |
| 72 | + /// |
| 73 | + /// [codeUnits] is the number of code units in the original string. |
| 74 | + static bool _isPlainAscii(List<int> bytes, int codeUnits) { |
| 75 | + // Most non-ASCII code units will produce multiple bytes and make the text |
| 76 | + // longer. |
| 77 | + if (bytes.length != codeUnits) return false; |
| 78 | + |
| 79 | + // Non-ASCII code units between U+0080 and U+009F produce 8-bit characters |
| 80 | + // with the high bit set. |
| 81 | + return bytes.every((byte) => byte & 0x80 == 0); |
| 82 | + } |
| 83 | + |
| 84 | + /// Returns a [Stream] representing the body. |
| 85 | + /// |
| 86 | + /// Can only be called once. |
| 87 | + Stream<List<int>> read() { |
| 88 | + if (_stream == null) { |
| 89 | + throw new StateError("The 'read' method can only be called once on a " |
| 90 | + "http.Request/http.Response object."); |
| 91 | + } |
| 92 | + var stream = _stream; |
| 93 | + _stream = null; |
| 94 | + return stream; |
| 95 | + } |
| 96 | +} |
0 commit comments