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

Side by Side Diff: sdk/lib/io/http_headers.dart

Issue 12440002: Make instances of HeaderValue and ContentType immutable (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated pkg/http Created 7 years, 9 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 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 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 part of dart.io; 5 part of dart.io;
6 6
7 class _HttpHeaders implements HttpHeaders { 7 class _HttpHeaders implements HttpHeaders {
8 _HttpHeaders(String this.protocolVersion) 8 _HttpHeaders(String this.protocolVersion)
9 : _headers = new Map<String, List<String>>(); 9 : _headers = new Map<String, List<String>>();
10 10
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 // Format "Expires" header with date in Greenwich Mean Time (GMT). 191 // Format "Expires" header with date in Greenwich Mean Time (GMT).
192 String formatted = _HttpUtils.formatDate(expires.toUtc()); 192 String formatted = _HttpUtils.formatDate(expires.toUtc());
193 _set(HttpHeaders.EXPIRES, formatted); 193 _set(HttpHeaders.EXPIRES, formatted);
194 } 194 }
195 195
196 ContentType get contentType { 196 ContentType get contentType {
197 var values = _headers["content-type"]; 197 var values = _headers["content-type"];
198 if (values != null) { 198 if (values != null) {
199 return new ContentType.fromString(values[0]); 199 return new ContentType.fromString(values[0]);
200 } else { 200 } else {
201 return new ContentType(); 201 return null;
202 } 202 }
203 } 203 }
204 204
205 void set contentType(ContentType contentType) { 205 void set contentType(ContentType contentType) {
206 _checkMutable(); 206 _checkMutable();
207 _set(HttpHeaders.CONTENT_TYPE, contentType.toString()); 207 _set(HttpHeaders.CONTENT_TYPE, contentType.toString());
208 } 208 }
209 209
210 void _add(String name, Object value) { 210 void _add(String name, Object value) {
211 var lowerCaseName = name.toLowerCase(); 211 var lowerCaseName = name.toLowerCase();
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 } 270 }
271 } 271 }
272 } 272 }
273 _set(HttpHeaders.HOST, value); 273 _set(HttpHeaders.HOST, value);
274 } else { 274 } else {
275 throw new HttpException("Unexpected type for header named $name"); 275 throw new HttpException("Unexpected type for header named $name");
276 } 276 }
277 } else if (lowerCaseName == HttpHeaders.CONTENT_TYPE) { 277 } else if (lowerCaseName == HttpHeaders.CONTENT_TYPE) {
278 _set(HttpHeaders.CONTENT_TYPE, value); 278 _set(HttpHeaders.CONTENT_TYPE, value);
279 } else { 279 } else {
280 _addValue(lowerCaseName, value); 280 _addValue(lowerCaseName, value);
281 } 281 }
282 } 282 }
283 283
284 void _addValue(String name, Object value) { 284 void _addValue(String name, Object value) {
285 List<String> values = _headers[name]; 285 List<String> values = _headers[name];
286 if (values == null) { 286 if (values == null) {
287 values = new List<String>(); 287 values = new List<String>();
288 _headers[name] = values; 288 _headers[name] = values;
289 } 289 }
290 if (value is DateTime) { 290 if (value is DateTime) {
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 486
487 int _contentLength = -1; 487 int _contentLength = -1;
488 bool _chunkedTransferEncoding = false; 488 bool _chunkedTransferEncoding = false;
489 final String protocolVersion; 489 final String protocolVersion;
490 String _host; 490 String _host;
491 int _port; 491 int _port;
492 } 492 }
493 493
494 494
495 class _HeaderValue implements HeaderValue { 495 class _HeaderValue implements HeaderValue {
496 _HeaderValue([String this.value = ""]); 496 String _value;
497 Map<String, String> _parameters;
497 498
498 _HeaderValue.fromString(String value, {this.parameterSeparator: ";"}) { 499 _HeaderValue([String this._value = "", this._parameters]);
500
501 _HeaderValue.fromString(String value, {parameterSeparator: ";"}) {
499 // Parse the string. 502 // Parse the string.
500 _parse(value); 503 _parse(value, parameterSeparator);
501 } 504 }
502 505
506 String get value => _value;
507
503 Map<String, String> get parameters { 508 Map<String, String> get parameters {
504 if (_parameters == null) _parameters = new Map<String, String>(); 509 if (_parameters == null) _parameters = new Map<String, String>();
505 return _parameters; 510 return _parameters;
506 } 511 }
507 512
508 String toString() { 513 String toString() {
509 StringBuffer sb = new StringBuffer(); 514 StringBuffer sb = new StringBuffer();
510 sb.write(value); 515 sb.write(_value);
511 if (parameters != null && parameters.length > 0) { 516 if (parameters != null && parameters.length > 0) {
512 _parameters.forEach((String name, String value) { 517 _parameters.forEach((String name, String value) {
513 sb.write("; "); 518 sb.write("; ");
514 sb.write(name); 519 sb.write(name);
515 sb.write("="); 520 sb.write("=");
516 sb.write(value); 521 sb.write(value);
517 }); 522 });
518 } 523 }
519 return sb.toString(); 524 return sb.toString();
520 } 525 }
521 526
522 void _parse(String s) { 527 void _parse(String s, String parameterSeparator) {
523 int index = 0; 528 int index = 0;
524 529
525 bool done() => index == s.length; 530 bool done() => index == s.length;
526 531
527 void skipWS() { 532 void skipWS() {
528 while (!done()) { 533 while (!done()) {
529 if (s[index] != " " && s[index] != "\t") return; 534 if (s[index] != " " && s[index] != "\t") return;
530 index++; 535 index++;
531 } 536 }
532 } 537 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 skipWS(); 604 skipWS();
600 String value = parseParameterValue(); 605 String value = parseParameterValue();
601 _parameters[name] = value; 606 _parameters[name] = value;
602 skipWS(); 607 skipWS();
603 if (done()) return; 608 if (done()) return;
604 expect(parameterSeparator); 609 expect(parameterSeparator);
605 } 610 }
606 } 611 }
607 612
608 skipWS(); 613 skipWS();
609 value = parseValue(); 614 _value = parseValue();
610 skipWS(); 615 skipWS();
611 if (done()) return; 616 if (done()) return;
612 maybeExpect(parameterSeparator); 617 maybeExpect(parameterSeparator);
613 parseParameters(); 618 parseParameters();
614 } 619 }
615
616 String value;
617 String parameterSeparator;
618 Map<String, String> _parameters;
619 } 620 }
620 621
621 622
622 class _ContentType extends _HeaderValue implements ContentType { 623 class _ContentType extends _HeaderValue implements ContentType {
623 _ContentType(String primaryType, String subType) 624 _ContentType(String primaryType, String subType, String charset)
624 : _primaryType = primaryType, _subType = subType, super(""); 625 : _primaryType = primaryType, _subType = subType, super("") {
626 if (_primaryType == null) _primaryType = "";
627 if (_subType == null) _subType = "";
628 _value = "$_primaryType/$_subType";;
629 if (charset != null) {
630 parameters["charset"] = charset;
631 }
632 }
625 633
626 _ContentType.fromString(String value) : super.fromString(value); 634 _ContentType.fromString(String value) : super.fromString(value) {
627 635 int index = _value.indexOf("/");
628 String get value => "$_primaryType/$_subType"; 636 if (index == -1 || index == (_value.length - 1)) {
629 637 _primaryType = _value.trim().toLowerCase();
630 void set value(String s) { 638 _subType = "";
631 int index = s.indexOf("/");
632 if (index == -1 || index == (s.length - 1)) {
633 primaryType = s.trim().toLowerCase();
634 subType = "";
635 } else { 639 } else {
636 primaryType = s.substring(0, index).trim().toLowerCase(); 640 _primaryType = _value.substring(0, index).trim().toLowerCase();
637 subType = s.substring(index + 1).trim().toLowerCase(); 641 _subType = _value.substring(index + 1).trim().toLowerCase();
638 } 642 }
639 } 643 }
640 644
641 String get primaryType => _primaryType; 645 String get primaryType => _primaryType;
642 646
643 void set primaryType(String s) {
644 _primaryType = s;
645 }
646
647 String get subType => _subType; 647 String get subType => _subType;
648 648
649 void set subType(String s) {
650 _subType = s;
651 }
652
653 String get charset => parameters["charset"]; 649 String get charset => parameters["charset"];
654 650
655 void set charset(String s) {
656 parameters["charset"] = s;
657 }
658
659 String _primaryType = ""; 651 String _primaryType = "";
660 String _subType = ""; 652 String _subType = "";
661 } 653 }
662 654
663 655
664 class _Cookie implements Cookie { 656 class _Cookie implements Cookie {
665 _Cookie([String this.name, String this.value]); 657 _Cookie([String this.name, String this.value]);
666 658
667 _Cookie.fromSetCookieValue(String value) { 659 _Cookie.fromSetCookieValue(String value) {
668 // Parse the 'set-cookie' header value. 660 // Parse the 'set-cookie' header value.
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 775
784 String name; 776 String name;
785 String value; 777 String value;
786 DateTime expires; 778 DateTime expires;
787 int maxAge; 779 int maxAge;
788 String domain; 780 String domain;
789 String path; 781 String path;
790 bool httpOnly = false; 782 bool httpOnly = false;
791 bool secure = false; 783 bool secure = false;
792 } 784 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698