Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(443)

Side by Side Diff: pkg/shelf/lib/src/message.dart

Issue 227563010: pkg/shelf: case-insensitive headers, cleaner Request ctor, a lot more tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fixing dependent code, changelog Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/shelf/lib/src/handlers/logger.dart ('k') | pkg/shelf/lib/src/request.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library shelf.message; 5 library shelf.message;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:collection';
8 import 'dart:convert'; 9 import 'dart:convert';
9 10
10 import 'package:collection/wrappers.dart'; 11 // TODO(kevmoo): use UnmodifiableMapView from SDK once 1.4 ships
12 import 'package:collection/wrappers.dart' as pc;
11 import 'package:http_parser/http_parser.dart'; 13 import 'package:http_parser/http_parser.dart';
12 import 'package:stack_trace/stack_trace.dart'; 14 import 'package:stack_trace/stack_trace.dart';
13 15
14 /// Represents logic shared between [Request] and [Response]. 16 /// Represents logic shared between [Request] and [Response].
15 abstract class Message { 17 abstract class Message {
16 /// The HTTP headers. 18 /// The HTTP headers.
17 /// 19 ///
18 /// The value is immutable. 20 /// The value is immutable.
19 final Map<String, String> headers; 21 final Map<String, String> headers;
20 22
21 /// The streaming body of the message. 23 /// The streaming body of the message.
22 /// 24 ///
23 /// This can be read via [read] or [readAsString]. 25 /// This can be read via [read] or [readAsString].
24 final Stream<List<int>> _body; 26 final Stream<List<int>> _body;
25 27
26 Message(UnmodifiableMapView<String, String> headers, this._body) 28 /// Creates a new [Message].
27 : this.headers = headers; 29 ///
30 /// If [headers] is `null`, it is treated as empty.
31 Message(this._body, {Map<String, String> headers})
32 : this.headers = _getIgnoreCaseMapView(headers);
28 33
29 /// The contents of the content-length field in [headers]. 34 /// The contents of the content-length field in [headers].
30 /// 35 ///
31 /// If not set, `null`. 36 /// If not set, `null`.
32 int get contentLength { 37 int get contentLength {
33 if (_contentLengthCache != null) return _contentLengthCache; 38 if (_contentLengthCache != null) return _contentLengthCache;
34 if (!headers.containsKey('content-length')) return null; 39 if (!headers.containsKey('content-length')) return null;
35 _contentLengthCache = int.parse(headers['content-length']); 40 _contentLengthCache = int.parse(headers['content-length']);
36 return _contentLengthCache; 41 return _contentLengthCache;
37 } 42 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 /// Otherwise the encoding is taken from the Content-Type header. If that 90 /// Otherwise the encoding is taken from the Content-Type header. If that
86 /// doesn't exist or doesn't have a "charset" parameter, UTF-8 is used. 91 /// doesn't exist or doesn't have a "charset" parameter, UTF-8 is used.
87 /// 92 ///
88 /// This calls [read] internally, which can only be called once. 93 /// This calls [read] internally, which can only be called once.
89 Future<String> readAsString([Encoding encoding]) { 94 Future<String> readAsString([Encoding encoding]) {
90 if (encoding == null) encoding = this.encoding; 95 if (encoding == null) encoding = this.encoding;
91 if (encoding == null) encoding = UTF8; 96 if (encoding == null) encoding = UTF8;
92 return Chain.track(encoding.decodeStream(read())); 97 return Chain.track(encoding.decodeStream(read()));
93 } 98 }
94 } 99 }
100
101 /// Creates on an unmodifiable map of [headers] with case-insensitive access.
102 Map<String, String> _getIgnoreCaseMapView(Map<String, String> headers) {
103 if (headers == null) return const {};
104 // TODO(kevmoo) generalize this model with a 'canonical map' to align with
105 // similiar implementation in http pkg [BaseRequest].
106 var map = new LinkedHashMap<String, String>(
107 equals: (key1, key2) => key1.toLowerCase() == key2.toLowerCase(),
108 hashCode: (key) => key.toLowerCase().hashCode);
109
110 map.addAll(headers);
111
112 return new pc.UnmodifiableMapView<String, String>(map);
113 }
OLDNEW
« no previous file with comments | « pkg/shelf/lib/src/handlers/logger.dart ('k') | pkg/shelf/lib/src/request.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698