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

Side by Side Diff: runtime/bin/http_impl.dart

Issue 9956175: Add test of the HTTP headers object (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 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 | « no previous file | tests/standalone/src/io/HttpHeadersTest.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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 class _HttpHeaders implements HttpHeaders { 5 class _HttpHeaders implements HttpHeaders {
6 _HttpHeaders() : _headers = new Map<String, List<String>>(); 6 _HttpHeaders() : _headers = new Map<String, List<String>>();
7 7
8 List<String> operator[](String name) { 8 List<String> operator[](String name) {
9 name = name.toLowerCase(); 9 name = name.toLowerCase();
10 return _headers[name]; 10 return _headers[name];
11 } 11 }
12 12
13 String value(String name) { 13 String value(String name) {
14 name = name.toLowerCase(); 14 name = name.toLowerCase();
15 List<String> values = _headers[name]; 15 List<String> values = _headers[name];
16 if (values == null) return null; 16 if (values == null) return null;
17 if (values.length > 1) { 17 if (values.length > 1) {
18 throw new HttpException("More than one value for header $name"); 18 throw new HttpException("More than one value for header $name");
19 } 19 }
20 return values[0]; 20 return values[0];
21 } 21 }
22 22
23 void add(String name, Object value) { 23 void add(String name, Object value) {
24 if (value is List) { 24 if (value is List) {
25 for (int i = 0; i < value.length; i++) { 25 for (int i = 0; i < value.length; i++) {
26 _add(name, vlaie[i]); 26 _add(name, value[i]);
27 } 27 }
28 } else { 28 } else {
29 _add(name, value); 29 _add(name, value);
30 } 30 }
31 } 31 }
32 32
33 void set(String name, Object value) { 33 void set(String name, Object value) {
34 removeAll(name); 34 removeAll(name);
35 add(name, value); 35 add(name, value);
36 } 36 }
(...skipping 19 matching lines...) Expand all
56 _host = host; 56 _host = host;
57 _updateHostHeader(); 57 _updateHostHeader();
58 } 58 }
59 59
60 int get port() => _port; 60 int get port() => _port;
61 void set port(int port) { 61 void set port(int port) {
62 _port = port; 62 _port = port;
63 _updateHostHeader(); 63 _updateHostHeader();
64 } 64 }
65 65
66 Date get expires() { 66 Date get expires() {
Anders Johnsen 2012/04/18 06:34:53 Did we remove expires, host and port from the othe
Søren Gjesse 2012/04/18 06:40:16 expires, host and port was removed from the other
67 if (_expires == null) { 67 List<String> values = _headers["expires"];
68 List<String> values = _headers["expires"]; 68 if (values != null) {
69 if (values != null) { 69 try {
70 _expires = _HttpUtils.parseDate(values[0]); 70 return _HttpUtils.parseDate(values[0]);
71 } catch (Exception e) {
72 return null;
71 } 73 }
72 } 74 }
73 return _expires;
74 } 75 }
75 76
76 void set expires(Date expires) { 77 void set expires(Date expires) {
77 _expires = expires;
78 // Format "Expires" header with date in Greenwich Mean Time (GMT). 78 // Format "Expires" header with date in Greenwich Mean Time (GMT).
79 String formatted = 79 String formatted =
80 _HttpUtils.formatDate(_expires.changeTimeZone(new TimeZone.utc())); 80 _HttpUtils.formatDate(expires.changeTimeZone(new TimeZone.utc()));
81 _set("expires", formatted); 81 _set("expires", formatted);
82 } 82 }
83 83
84 void _add(String name, Object value) { 84 void _add(String name, Object value) {
85 // TODO(sgjesse): Add immutable state throw HttpException is immutable. 85 // TODO(sgjesse): Add immutable state throw HttpException is immutable.
86 if (name.toLowerCase() == "expires") { 86 if (name.toLowerCase() == "expires") {
87 if (value is Date) { 87 if (value is Date) {
88 expires = value; 88 expires = value;
89 } else if (value is String) { 89 } else if (value is String) {
90 expires = _HttpUtils.parseDate(value); 90 _set("expires", value);
91 } else { 91 } else {
92 throw new HttpException("Unexpected type for header named $name"); 92 throw new HttpException("Unexpected type for header named $name");
93 } 93 }
94 } else if (name.toLowerCase() == "host") { 94 } else if (name.toLowerCase() == "host") {
95 int pos = value.indexOf(":"); 95 int pos = value.indexOf(":");
96 if (pos == -1) { 96 if (pos == -1) {
97 _host = value; 97 _host = value;
98 _port = HttpClient.DEFAULT_HTTP_PORT; 98 _port = HttpClient.DEFAULT_HTTP_PORT;
99 } else { 99 } else {
100 _host = value.substring(0, pos); 100 if (pos > 0) {
101 _host = value.substring(0, pos);
102 } else {
103 _host = null;
104 }
101 if (pos + 1 == value.length) { 105 if (pos + 1 == value.length) {
102 _port = HttpClient.DEFAULT_HTTP_PORT; 106 _port = HttpClient.DEFAULT_HTTP_PORT;
103 } else { 107 } else {
104 _port = Math.parseInt(value.substring(pos + 1)); 108 try {
109 _port = Math.parseInt(value.substring(pos + 1));
110 } catch (BadNumberFormatException e) {
111 _port = null;
112 }
105 } 113 }
114 _set("host", value);
106 } 115 }
107 _updateHostHeader();
108 } else { 116 } else {
109 name = name.toLowerCase(); 117 name = name.toLowerCase();
110 List<String> values = _headers[name]; 118 List<String> values = _headers[name];
111 if (values == null) { 119 if (values == null) {
112 values = new List<String>(); 120 values = new List<String>();
113 _headers[name] = values; 121 _headers[name] = values;
114 } 122 }
115 values.add(value.toString()); 123 values.add(value.toString());
116 } 124 }
117 } 125 }
118 126
119 void _set(String name, String value) { 127 void _set(String name, String value) {
120 name = name.toLowerCase(); 128 name = name.toLowerCase();
121 List<String> values = new List<String>(); 129 List<String> values = new List<String>();
122 _headers[name] = values; 130 _headers[name] = values;
123 values.add(value); 131 values.add(value);
124 } 132 }
125 133
126 _updateHostHeader() { 134 _updateHostHeader() {
127 String portPart = _port == HttpClient.DEFAULT_HTTP_PORT ? "" : ":$_port"; 135 bool defaultPort = _port == null || _port == HttpClient.DEFAULT_HTTP_PORT;
136 String portPart = defaultPort ? "" : ":$_port";
128 _set("host", "$host$portPart"); 137 _set("host", "$host$portPart");
129 } 138 }
130 139
131 _write(_HttpConnectionBase connection) { 140 _write(_HttpConnectionBase connection) {
132 final COLONSP = const [_CharCode.COLON, _CharCode.SP]; 141 final COLONSP = const [_CharCode.COLON, _CharCode.SP];
133 final COMMASP = const [_CharCode.COMMA, _CharCode.SP]; 142 final COMMASP = const [_CharCode.COMMA, _CharCode.SP];
134 final CRLF = const [_CharCode.CR, _CharCode.LF]; 143 final CRLF = const [_CharCode.CR, _CharCode.LF];
135 144
136 // Format headers. 145 // Format headers.
137 _headers.forEach((String name, List<String> values) { 146 _headers.forEach((String name, List<String> values) {
(...skipping 25 matching lines...) Expand all
163 } 172 }
164 sb.add("\n"); 173 sb.add("\n");
165 }); 174 });
166 return sb.toString(); 175 return sb.toString();
167 } 176 }
168 177
169 Map<String, List<String>> _headers; 178 Map<String, List<String>> _headers;
170 179
171 String _host; 180 String _host;
172 int _port; 181 int _port;
173 Date _expires;
174 } 182 }
175 183
176 184
177 class _HttpRequestResponseBase { 185 class _HttpRequestResponseBase {
178 _HttpRequestResponseBase(_HttpConnectionBase this._httpConnection) 186 _HttpRequestResponseBase(_HttpConnectionBase this._httpConnection)
179 : _contentLength = -1, 187 : _contentLength = -1,
180 _headers = new _HttpHeaders(); 188 _headers = new _HttpHeaders();
181 189
182 int get contentLength() => _contentLength; 190 int get contentLength() => _contentLength;
183 HttpHeaders get headers() => _headers; 191 HttpHeaders get headers() => _headers;
(...skipping 1146 matching lines...) Expand 10 before | Expand all | Expand 10 after
1330 sockets.addFirst(socketConn); 1338 sockets.addFirst(socketConn);
1331 socketConn._markReturned(); 1339 socketConn._markReturned();
1332 } 1340 }
1333 1341
1334 Function _onOpen; 1342 Function _onOpen;
1335 Map<String, Queue<_SocketConnection>> _openSockets; 1343 Map<String, Queue<_SocketConnection>> _openSockets;
1336 Set<_SocketConnection> _activeSockets; 1344 Set<_SocketConnection> _activeSockets;
1337 Timer _evictionTimer; 1345 Timer _evictionTimer;
1338 bool _shutdown; // Has this HTTP client been shutdown? 1346 bool _shutdown; // Has this HTTP client been shutdown?
1339 } 1347 }
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/src/io/HttpHeadersTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698