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

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

Issue 9500014: Add statusCode and reasonPhrase getters and setters to the HttpResponse interface (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 8 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
« no previous file with comments | « runtime/bin/http.dart ('k') | tests/standalone/src/HttpTest.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 // Global constants. 5 // Global constants.
6 class _Const { 6 class _Const {
7 // Bytes for "HTTP/1.0". 7 // Bytes for "HTTP/1.0".
8 static final HTTP10 = const [72, 84, 84, 80, 47, 49, 46, 48]; 8 static final HTTP10 = const [72, 84, 84, 80, 47, 49, 46, 48];
9 // Bytes for "HTTP/1.1". 9 // Bytes for "HTTP/1.1".
10 static final HTTP11 = const [72, 84, 84, 80, 47, 49, 46, 49]; 10 static final HTTP11 = const [72, 84, 84, 80, 47, 49, 46, 49];
(...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after
661 661
662 662
663 // HTTP response object for sending a HTTP response. 663 // HTTP response object for sending a HTTP response.
664 class _HttpResponse extends _HttpRequestResponseBase implements HttpResponse { 664 class _HttpResponse extends _HttpRequestResponseBase implements HttpResponse {
665 static final int START = 0; 665 static final int START = 0;
666 static final int HEADERS_SENT = 1; 666 static final int HEADERS_SENT = 1;
667 static final int DONE = 2; 667 static final int DONE = 2;
668 668
669 _HttpResponse(_HttpConnection httpConnection) 669 _HttpResponse(_HttpConnection httpConnection)
670 : super(httpConnection), 670 : super(httpConnection),
671 statusCode = HttpStatus.OK, 671 _statusCode = HttpStatus.OK,
672 _state = START; 672 _state = START;
673 673
674 void set contentLength(int contentLength) { 674 void set contentLength(int contentLength) {
675 if (_outputStream != null) return new HttpException("Header already sent"); 675 if (_outputStream != null) return new HttpException("Header already sent");
676 _contentLength = contentLength; 676 _contentLength = contentLength;
677 } 677 }
678
678 void set keepAlive(bool keepAlive) { 679 void set keepAlive(bool keepAlive) {
679 if (_outputStream != null) return new HttpException("Header already sent"); 680 if (_outputStream != null) return new HttpException("Header already sent");
680 _keepAlive = keepAlive; 681 _keepAlive = keepAlive;
681 } 682 }
682 683
684 int get statusCode() => _statusCode;
685 void set statusCode(int statusCode) {
686 if (_outputStream != null) return new HttpException("Header already sent");
687 _statusCode = statusCode;
688 }
689
690 String get reasonPhrase() => _findReasonPhrase(_statusCode);
691 void set reasonPhrase(String reasonPhrase) => _reasonPhrase = reasonPhrase;
692
683 // Set a header on the response. NOTE: If the same header is set 693 // Set a header on the response. NOTE: If the same header is set
684 // more than once only the last one will be part of the response. 694 // more than once only the last one will be part of the response.
685 void setHeader(String name, String value) { 695 void setHeader(String name, String value) {
686 if (_outputStream != null) return new HttpException("Header already sent"); 696 if (_outputStream != null) return new HttpException("Header already sent");
687 _setHeader(name, value); 697 _setHeader(name, value);
688 } 698 }
689 699
690 OutputStream get outputStream() { 700 OutputStream get outputStream() {
691 if (_state == DONE) throw new HttpException("Response closed"); 701 if (_state == DONE) throw new HttpException("Response closed");
692 if (_outputStream == null) { 702 if (_outputStream == null) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 745
736 void _streamSetCloseHandler(callback()) { 746 void _streamSetCloseHandler(callback()) {
737 // TODO(sgjesse): Handle this. 747 // TODO(sgjesse): Handle this.
738 } 748 }
739 749
740 void _streamSetErrorHandler(callback()) { 750 void _streamSetErrorHandler(callback()) {
741 // TODO(sgjesse): Handle this. 751 // TODO(sgjesse): Handle this.
742 } 752 }
743 753
744 String _findReasonPhrase(int statusCode) { 754 String _findReasonPhrase(int statusCode) {
745 if (reasonPhrase != null) { 755 if (_reasonPhrase != null) {
746 return reasonPhrase; 756 return _reasonPhrase;
747 } 757 }
748 758
749 switch (statusCode) { 759 switch (statusCode) {
750 case HttpStatus.CONTINUE: return "Continue"; 760 case HttpStatus.CONTINUE: return "Continue";
751 case HttpStatus.SWITCHING_PROTOCOLS: return "Switching Protocols"; 761 case HttpStatus.SWITCHING_PROTOCOLS: return "Switching Protocols";
752 case HttpStatus.OK: return "OK"; 762 case HttpStatus.OK: return "OK";
753 case HttpStatus.CREATED: return "Created"; 763 case HttpStatus.CREATED: return "Created";
754 case HttpStatus.ACCEPTED: return "Accepted"; 764 case HttpStatus.ACCEPTED: return "Accepted";
755 case HttpStatus.NON_AUTHORITATIVE_INFORMATION: 765 case HttpStatus.NON_AUTHORITATIVE_INFORMATION:
756 return "Non-Authoritative Information"; 766 return "Non-Authoritative Information";
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 } 806 }
797 } 807 }
798 808
799 bool _writeHeader() { 809 bool _writeHeader() {
800 List<int> data; 810 List<int> data;
801 OutputStream stream = _httpConnection.outputStream; 811 OutputStream stream = _httpConnection.outputStream;
802 812
803 // Write status line. 813 // Write status line.
804 stream.write(_Const.HTTP11); 814 stream.write(_Const.HTTP11);
805 _writeSP(); 815 _writeSP();
806 data = statusCode.toString().charCodes(); 816 data = _statusCode.toString().charCodes();
807 stream.write(data); 817 stream.write(data);
808 _writeSP(); 818 _writeSP();
809 data = _findReasonPhrase(statusCode).charCodes(); 819 data = reasonPhrase.charCodes();
810 stream.write(data); 820 stream.write(data);
811 _writeCRLF(); 821 _writeCRLF();
812 822
813 // Determine the value of the "Connection" header 823 // Determine the value of the "Connection" header
814 // based on the keep alive state. 824 // based on the keep alive state.
815 setHeader("Connection", keepAlive ? "keep-alive" : "close"); 825 setHeader("Connection", keepAlive ? "keep-alive" : "close");
816 // Determine the value of the "Transfer-Encoding" header based on 826 // Determine the value of the "Transfer-Encoding" header based on
817 // whether the content length is known. 827 // whether the content length is known.
818 if (_contentLength >= 0) { 828 if (_contentLength >= 0) {
819 setHeader("Content-Length", _contentLength.toString()); 829 setHeader("Content-Length", _contentLength.toString());
820 } else { 830 } else {
821 setHeader("Transfer-Encoding", "chunked"); 831 setHeader("Transfer-Encoding", "chunked");
822 } 832 }
823 833
824 // Write headers. 834 // Write headers.
825 bool allWritten = _writeHeaders(); 835 bool allWritten = _writeHeaders();
826 _state = HEADERS_SENT; 836 _state = HEADERS_SENT;
827 return allWritten; 837 return allWritten;
828 } 838 }
829 839
830 // Response status code. 840 // Response status code.
831 int statusCode; 841 int _statusCode;
832 String reasonPhrase; 842 String _reasonPhrase;
833 _HttpOutputStream _outputStream; 843 _HttpOutputStream _outputStream;
834 int _state; 844 int _state;
835 } 845 }
836 846
837 847
838 class _HttpInputStream extends _BaseDataInputStream implements InputStream { 848 class _HttpInputStream extends _BaseDataInputStream implements InputStream {
839 _HttpInputStream(_HttpRequestResponseBase this._requestOrResponse) { 849 _HttpInputStream(_HttpRequestResponseBase this._requestOrResponse) {
840 _checkScheduleCallbacks(); 850 _checkScheduleCallbacks();
841 } 851 }
842 852
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
1092 _state = START { 1102 _state = START {
1093 _connection = connection; 1103 _connection = connection;
1094 // Default GET requests to have no content. 1104 // Default GET requests to have no content.
1095 if (_method == "GET") { 1105 if (_method == "GET") {
1096 _contentLength = 0; 1106 _contentLength = 0;
1097 } 1107 }
1098 } 1108 }
1099 1109
1100 void set contentLength(int contentLength) => _contentLength = contentLength; 1110 void set contentLength(int contentLength) => _contentLength = contentLength;
1101 void set keepAlive(bool keepAlive) => _keepAlive = keepAlive; 1111 void set keepAlive(bool keepAlive) => _keepAlive = keepAlive;
1102 int get statusCode() { return _statusCode; }
1103 String get reasonPhrase() { return _reasonPhrase; }
1104 1112
1105 void setHeader(String name, String value) { 1113 void setHeader(String name, String value) {
1106 _setHeader(name, value); 1114 _setHeader(name, value);
1107 } 1115 }
1108 1116
1109 bool writeString(String string) { 1117 bool writeString(String string) {
1110 outputStream; 1118 outputStream;
1111 return _writeString(string); 1119 return _writeString(string);
1112 } 1120 }
1113 1121
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1197 } 1205 }
1198 1206
1199 1207
1200 class _HttpClientResponse 1208 class _HttpClientResponse
1201 extends _HttpRequestResponseBase implements HttpClientResponse { 1209 extends _HttpRequestResponseBase implements HttpClientResponse {
1202 _HttpClientResponse(_HttpClientConnection connection) 1210 _HttpClientResponse(_HttpClientConnection connection)
1203 : super(connection) { 1211 : super(connection) {
1204 _connection = connection; 1212 _connection = connection;
1205 } 1213 }
1206 1214
1207 int get statusCode() { return _statusCode; } 1215 int get statusCode() => _statusCode;
1208 int get reasonPhrase() { return _reasonPhrase; } 1216 String get reasonPhrase() => _reasonPhrase;
1209 Map get headers() => _headers; 1217 Map get headers() => _headers;
1210 1218
1211 InputStream get inputStream() { 1219 InputStream get inputStream() {
1212 if (_inputStream == null) { 1220 if (_inputStream == null) {
1213 _inputStream = new _HttpInputStream(this); 1221 _inputStream = new _HttpInputStream(this);
1214 } 1222 }
1215 return _inputStream; 1223 return _inputStream;
1216 } 1224 }
1217 1225
1218 void _requestStartHandler(String method, String uri) { 1226 void _requestStartHandler(String method, String uri) {
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
1560 } else { 1568 } else {
1561 value = queryString.substring(currentPosition, position); 1569 value = queryString.substring(currentPosition, position);
1562 currentPosition = position + 1; 1570 currentPosition = position + 1;
1563 } 1571 }
1564 result[HttpUtil.decodeUrlEncodedString(name)] = 1572 result[HttpUtil.decodeUrlEncodedString(name)] =
1565 HttpUtil.decodeUrlEncodedString(value); 1573 HttpUtil.decodeUrlEncodedString(value);
1566 } 1574 }
1567 return result; 1575 return result;
1568 } 1576 }
1569 } 1577 }
OLDNEW
« no previous file with comments | « runtime/bin/http.dart ('k') | tests/standalone/src/HttpTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698