OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 library shelf.message; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 |
| 10 import 'package:collection/wrappers.dart'; |
| 11 import 'package:stack_trace/stack_trace.dart'; |
| 12 |
| 13 import 'media_type.dart'; |
| 14 |
| 15 /// Represents logic shared between [Request] and [Response]. |
| 16 abstract class Message { |
| 17 /// The HTTP headers. |
| 18 /// |
| 19 /// The value is immutable. |
| 20 final Map<String, String> headers; |
| 21 |
| 22 /// The streaming body of the message. |
| 23 /// |
| 24 /// This can be read via [read] or [readAsString]. |
| 25 final Stream<List<int>> _body; |
| 26 |
| 27 Message(UnmodifiableMapView<String, String> headers, this._body) |
| 28 : this.headers = headers; |
| 29 |
| 30 /// The contents of the content-length field in [headers]. |
| 31 /// |
| 32 /// If not set, `null`. |
| 33 int get contentLength { |
| 34 if (_contentLengthCache != null) return _contentLengthCache; |
| 35 if (!headers.containsKey('content-length')) return null; |
| 36 _contentLengthCache = int.parse(headers['content-length']); |
| 37 return _contentLengthCache; |
| 38 } |
| 39 int _contentLengthCache; |
| 40 |
| 41 /// The MIME type of the message. |
| 42 /// |
| 43 /// This is parsed from the Content-Type header in [headers]. It contains only |
| 44 /// the MIME type, without any Content-Type parameters. |
| 45 /// |
| 46 /// If [headers] doesn't have a Content-Type header, this will be `null`. |
| 47 String get mimeType { |
| 48 var contentType = _contentType; |
| 49 if (contentType == null) return null; |
| 50 return contentType.mimeType; |
| 51 } |
| 52 |
| 53 /// The encoding of the message body. |
| 54 /// |
| 55 /// This is parsed from the "charset" paramater of the Content-Type header in |
| 56 /// [headers]. |
| 57 /// |
| 58 /// If [headers] doesn't have a Content-Type header or it specifies an |
| 59 /// encoding that [dart:convert] doesn't support, this will be `null`. |
| 60 Encoding get encoding { |
| 61 var contentType = _contentType; |
| 62 if (contentType == null) return null; |
| 63 if (!contentType.parameters.containsKey('charset')) return null; |
| 64 return Encoding.getByName(contentType.parameters['charset']); |
| 65 } |
| 66 |
| 67 /// The parsed version of the Content-Type header in [headers]. |
| 68 /// |
| 69 /// This is cached for efficient access. |
| 70 MediaType get _contentType { |
| 71 if (_contentTypeCache != null) return _contentTypeCache; |
| 72 if (!headers.containsKey('content-type')) return null; |
| 73 _contentTypeCache = new MediaType.parse(headers['content-type']); |
| 74 return _contentTypeCache; |
| 75 } |
| 76 MediaType _contentTypeCache; |
| 77 |
| 78 /// Returns a [Stream] representing the body. |
| 79 /// |
| 80 /// Can only be called once. |
| 81 Stream<List<int>> read() => _body; |
| 82 |
| 83 /// Returns a [Future] containing the body as a String. |
| 84 /// |
| 85 /// If [encoding] is passed, that's used to decode the body. |
| 86 /// Otherwise the encoding is taken from the Content-Type header. If that |
| 87 /// doesn't exist or doesn't have a "charset" parameter, UTF-8 is used. |
| 88 /// |
| 89 /// This calls [read] internally, which can only be called once. |
| 90 Future<String> readAsString([Encoding encoding]) { |
| 91 if (encoding == null) encoding = this.encoding; |
| 92 if (encoding == null) encoding = UTF8; |
| 93 return Chain.track(encoding.decodeStream(read())); |
| 94 } |
| 95 } |
OLD | NEW |