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

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

Issue 10173024: Change the error handling in dart:io (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 | « runtime/bin/http.dart ('k') | runtime/bin/input_stream.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];
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 362
363 List<int> _streamRead(int bytesToRead) { 363 List<int> _streamRead(int bytesToRead) {
364 return _buffer.readBytes(bytesToRead); 364 return _buffer.readBytes(bytesToRead);
365 } 365 }
366 366
367 int _streamReadInto(List<int> buffer, int offset, int len) { 367 int _streamReadInto(List<int> buffer, int offset, int len) {
368 List<int> data = _buffer.readBytes(len); 368 List<int> data = _buffer.readBytes(len);
369 buffer.setRange(offset, data.length, data); 369 buffer.setRange(offset, data.length, data);
370 } 370 }
371 371
372 void _streamSetErrorHandler(callback(Exception e)) { 372 void _streamSetErrorHandler(callback(e)) {
373 _streamErrorHandler = callback; 373 _streamErrorHandler = callback;
374 } 374 }
375 375
376 String _method; 376 String _method;
377 String _uri; 377 String _uri;
378 String _path; 378 String _path;
379 String _queryString; 379 String _queryString;
380 Map<String, String> _queryParameters; 380 Map<String, String> _queryParameters;
381 _HttpInputStream _inputStream; 381 _HttpInputStream _inputStream;
382 _BufferList _buffer; 382 _BufferList _buffer;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 void _streamSetNoPendingWriteHandler(callback()) { 450 void _streamSetNoPendingWriteHandler(callback()) {
451 if (_state != DONE) { 451 if (_state != DONE) {
452 _httpConnection._onNoPendingWrites = callback; 452 _httpConnection._onNoPendingWrites = callback;
453 } 453 }
454 } 454 }
455 455
456 void _streamSetCloseHandler(callback()) { 456 void _streamSetCloseHandler(callback()) {
457 // TODO(sgjesse): Handle this. 457 // TODO(sgjesse): Handle this.
458 } 458 }
459 459
460 void _streamSetErrorHandler(callback(Exception e)) { 460 void _streamSetErrorHandler(callback(e)) {
461 _streamErrorHandler = callback; 461 _streamErrorHandler = callback;
462 } 462 }
463 463
464 String _findReasonPhrase(int statusCode) { 464 String _findReasonPhrase(int statusCode) {
465 if (_reasonPhrase != null) { 465 if (_reasonPhrase != null) {
466 return _reasonPhrase; 466 return _reasonPhrase;
467 } 467 }
468 468
469 switch (statusCode) { 469 switch (statusCode) {
470 case HttpStatus.CONTINUE: return "Continue"; 470 case HttpStatus.CONTINUE: return "Continue";
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 void pipe(OutputStream output, [bool close = true]) { 576 void pipe(OutputStream output, [bool close = true]) {
577 _pipe(this, output, close: close); 577 _pipe(this, output, close: close);
578 } 578 }
579 579
580 List<int> _read(int bytesToRead) { 580 List<int> _read(int bytesToRead) {
581 List<int> result = _requestOrResponse._streamRead(bytesToRead); 581 List<int> result = _requestOrResponse._streamRead(bytesToRead);
582 _checkScheduleCallbacks(); 582 _checkScheduleCallbacks();
583 return result; 583 return result;
584 } 584 }
585 585
586 void set onError(void callback(Exception e)) { 586 void set onError(void callback(e)) {
587 _requestOrResponse._streamSetErrorHandler(callback); 587 _requestOrResponse._streamSetErrorHandler(callback);
588 } 588 }
589 589
590 int _readInto(List<int> buffer, int offset, int len) { 590 int _readInto(List<int> buffer, int offset, int len) {
591 int result = _requestOrResponse._streamReadInto(buffer, offset, len); 591 int result = _requestOrResponse._streamReadInto(buffer, offset, len);
592 _checkScheduleCallbacks(); 592 _checkScheduleCallbacks();
593 return result; 593 return result;
594 } 594 }
595 595
596 void _close() { 596 void _close() {
(...skipping 28 matching lines...) Expand all
625 } 625 }
626 626
627 void set onNoPendingWrites(void callback()) { 627 void set onNoPendingWrites(void callback()) {
628 _requestOrResponse._streamSetNoPendingWriteHandler(callback); 628 _requestOrResponse._streamSetNoPendingWriteHandler(callback);
629 } 629 }
630 630
631 void set onClosed(void callback()) { 631 void set onClosed(void callback()) {
632 _requestOrResponse._streamSetCloseHandler(callback); 632 _requestOrResponse._streamSetCloseHandler(callback);
633 } 633 }
634 634
635 void set onError(void callback(Exception e)) { 635 void set onError(void callback(e)) {
636 _requestOrResponse._streamSetErrorHandler(callback); 636 _requestOrResponse._streamSetErrorHandler(callback);
637 } 637 }
638 638
639 _HttpRequestResponseBase _requestOrResponse; 639 _HttpRequestResponseBase _requestOrResponse;
640 } 640 }
641 641
642 642
643 class _HttpConnectionBase implements Hashable { 643 class _HttpConnectionBase implements Hashable {
644 _HttpConnectionBase() : _sendBuffers = new Queue(), 644 _HttpConnectionBase() : _sendBuffers = new Queue(),
645 _httpParser = new _HttpParser(); 645 _httpParser = new _HttpParser();
646 646
647 void _connectionEstablished(Socket socket) { 647 void _connectionEstablished(Socket socket) {
648 _socket = socket; 648 _socket = socket;
649 // Register handler for socket events. 649 // Register handler for socket events.
650 _socket.onData = _onData; 650 _socket.onData = _onData;
651 _socket.onClosed = _onClosed; 651 _socket.onClosed = _onClosed;
652 _socket.onError = _onError; 652 _socket.onError = _onError;
653 // Ignore errors in the socket output stream as this is getting
654 // the same errors as the socket itself.
655 _socket.outputStream.onError = (e) => null;
653 } 656 }
654 657
655 bool _write(List<int> data, [bool copyBuffer = false]) { 658 bool _write(List<int> data, [bool copyBuffer = false]) {
656 if (!_error && !_closing) { 659 if (!_error && !_closing) {
657 return _socket.outputStream.write(data, copyBuffer); 660 return _socket.outputStream.write(data, copyBuffer);
658 } 661 }
659 } 662 }
660 663
661 bool _writeFrom(List<int> buffer, [int offset, int len]) { 664 bool _writeFrom(List<int> buffer, [int offset, int len]) {
662 if (!_error && !_closing) { 665 if (!_error && !_closing) {
(...skipping 21 matching lines...) Expand all
684 _close(); 687 _close();
685 } 688 }
686 } 689 }
687 } 690 }
688 691
689 void _onClosed() { 692 void _onClosed() {
690 _closing = true; 693 _closing = true;
691 _onConnectionClosed(null); 694 _onConnectionClosed(null);
692 } 695 }
693 696
694 void _onError(Exception e) { 697 void _onError(e) {
695 // If an error occurs, make sure to close the socket if one is associated. 698 // If an error occurs, make sure to close the socket if one is associated.
696 _error = true; 699 _error = true;
697 if (_socket != null) { 700 if (_socket != null) {
698 _socket.close(); 701 _socket.close();
699 } 702 }
700 _onConnectionClosed(e); 703 _onConnectionClosed(e);
701 } 704 }
702 705
703 abstract void _onConnectionClosed(Exception e); 706 abstract void _onConnectionClosed(e);
704 abstract void _responseDone(); 707 abstract void _responseDone();
705 708
706 void set _onNoPendingWrites(void callback()) { 709 void set _onNoPendingWrites(void callback()) {
707 if (!_error) { 710 if (!_error) {
708 _socket.outputStream.onNoPendingWrites = callback; 711 _socket.outputStream.onNoPendingWrites = callback;
709 } 712 }
710 } 713 }
711 714
712 int hashCode() => _socket.hashCode(); 715 int hashCode() => _socket.hashCode();
713 716
(...skipping 16 matching lines...) Expand all
730 (statusCode, reasonPhrase, version) => 733 (statusCode, reasonPhrase, version) =>
731 _onResponseStart(statusCode, reasonPhrase, version); 734 _onResponseStart(statusCode, reasonPhrase, version);
732 _httpParser.headerReceived = 735 _httpParser.headerReceived =
733 (name, value) => _onHeaderReceived(name, value); 736 (name, value) => _onHeaderReceived(name, value);
734 _httpParser.headersComplete = () => _onHeadersComplete(); 737 _httpParser.headersComplete = () => _onHeadersComplete();
735 _httpParser.dataReceived = (data) => _onDataReceived(data); 738 _httpParser.dataReceived = (data) => _onDataReceived(data);
736 _httpParser.dataEnd = (close) => _onDataEnd(close); 739 _httpParser.dataEnd = (close) => _onDataEnd(close);
737 _httpParser.error = (e) => _onError(e); 740 _httpParser.error = (e) => _onError(e);
738 } 741 }
739 742
740 void _onConnectionClosed(Exception e) { 743 void _onConnectionClosed(e) {
741 if (e != null && onError != null) { 744 // Don't report errors when HTTP parser is in idle state. Clients
745 // can close the connection and cause a connection reset by peer
746 // error which is OK.
747 if (e != null && onError != null && !_httpParser.isIdle) {
742 onError(e); 748 onError(e);
743 // Propagate the error to the streams. 749 // Propagate the error to the streams.
744 if (_request != null && _request._streamErrorHandler != null) { 750 if (_request != null && _request._streamErrorHandler != null) {
745 _request._streamErrorHandler(e); 751 _request._streamErrorHandler(e);
746 } 752 }
747 if (_response != null && _response._streamErrorHandler != null) { 753 if (_response != null && _response._streamErrorHandler != null) {
748 _response._streamErrorHandler(e); 754 _response._streamErrorHandler(e);
749 } 755 }
750 } 756 }
751 757
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 855
850 void listenOn(ServerSocket serverSocket) { 856 void listenOn(ServerSocket serverSocket) {
851 void onConnection(Socket socket) { 857 void onConnection(Socket socket) {
852 // Accept the client connection. 858 // Accept the client connection.
853 _HttpConnection connection = new _HttpConnection(this); 859 _HttpConnection connection = new _HttpConnection(this);
854 connection._connectionEstablished(socket); 860 connection._connectionEstablished(socket);
855 connection.onRequestReceived = _handleRequest; 861 connection.onRequestReceived = _handleRequest;
856 connection.onClosed = () => _connections.remove(connection); 862 connection.onClosed = () => _connections.remove(connection);
857 connection.onError = (e) { 863 connection.onError = (e) {
858 _connections.remove(connection); 864 _connections.remove(connection);
859 if (_onError != null) _onError(e); 865 if (_onError != null) {
866 _onError(e);
867 } else {
868 throw(e);
869 }
860 }; 870 };
861 connection._connectionEstablished(socket); 871 connection._connectionEstablished(socket);
862 _connections.add(connection); 872 _connections.add(connection);
863 } 873 }
864 serverSocket.onConnection = onConnection; 874 serverSocket.onConnection = onConnection;
865 _server = serverSocket; 875 _server = serverSocket;
866 _closeServer = false; 876 _closeServer = false;
867 } 877 }
868 878
869 addRequestHandler(bool matcher(String path), Object handler) { 879 addRequestHandler(bool matcher(String path), Object handler) {
(...skipping 18 matching lines...) Expand all
888 _connections.clear(); 898 _connections.clear();
889 } 899 }
890 900
891 int get port() { 901 int get port() {
892 if (_server === null) { 902 if (_server === null) {
893 throw new HttpException("The HttpServer is not listening on a port."); 903 throw new HttpException("The HttpServer is not listening on a port.");
894 } 904 }
895 return _server.port; 905 return _server.port;
896 } 906 }
897 907
898 void set onError(void callback(Exception e)) { 908 void set onError(void callback(e)) {
899 _onError = callback; 909 _onError = callback;
900 } 910 }
901 911
902 void _handleRequest(HttpRequest request, HttpResponse response) { 912 void _handleRequest(HttpRequest request, HttpResponse response) {
903 for (int i = 0; i < _handlers.length; i++) { 913 for (int i = 0; i < _handlers.length; i++) {
904 if (_handlers[i]._matcher(request)) { 914 if (_handlers[i]._matcher(request)) {
905 var handler = _handlers[i]._handler; 915 var handler = _handlers[i]._handler;
906 try { 916 try {
907 handler.onRequest(request, response); 917 handler.onRequest(request, response);
908 } catch (var e) { 918 } catch (var e) {
909 if (_onError != null) { 919 if (_onError != null) {
910 _onError(e); 920 _onError(e);
921 } else {
922 throw e;
911 } 923 }
912 } 924 }
913 return; 925 return;
914 } 926 }
915 } 927 }
916 928
917 if (_defaultHandler != null) { 929 if (_defaultHandler != null) {
918 if (_defaultHandler is Function) { 930 if (_defaultHandler is Function) {
919 _defaultHandler(request, response); 931 _defaultHandler(request, response);
920 } else { 932 } else {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
988 void _streamSetNoPendingWriteHandler(callback()) { 1000 void _streamSetNoPendingWriteHandler(callback()) {
989 if (_state != DONE) { 1001 if (_state != DONE) {
990 _httpConnection._onNoPendingWrites = callback; 1002 _httpConnection._onNoPendingWrites = callback;
991 } 1003 }
992 } 1004 }
993 1005
994 void _streamSetCloseHandler(callback()) { 1006 void _streamSetCloseHandler(callback()) {
995 // TODO(sgjesse): Handle this. 1007 // TODO(sgjesse): Handle this.
996 } 1008 }
997 1009
998 void _streamSetErrorHandler(callback(Exception e)) { 1010 void _streamSetErrorHandler(callback(e)) {
999 _streamErrorHandler = callback; 1011 _streamErrorHandler = callback;
1000 } 1012 }
1001 1013
1002 void _writeHeader() { 1014 void _writeHeader() {
1003 List<int> data; 1015 List<int> data;
1004 1016
1005 // Write request line. 1017 // Write request line.
1006 data = _method.toString().charCodes(); 1018 data = _method.toString().charCodes();
1007 _httpConnection._write(data); 1019 _httpConnection._write(data);
1008 _writeSP(); 1020 _writeSP();
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 List<int> _streamRead(int bytesToRead) { 1101 List<int> _streamRead(int bytesToRead) {
1090 return _buffer.readBytes(bytesToRead); 1102 return _buffer.readBytes(bytesToRead);
1091 } 1103 }
1092 1104
1093 int _streamReadInto(List<int> buffer, int offset, int len) { 1105 int _streamReadInto(List<int> buffer, int offset, int len) {
1094 List<int> data = _buffer.readBytes(len); 1106 List<int> data = _buffer.readBytes(len);
1095 buffer.setRange(offset, data.length, data); 1107 buffer.setRange(offset, data.length, data);
1096 return data.length; 1108 return data.length;
1097 } 1109 }
1098 1110
1099 void _streamSetErrorHandler(callback(Exception e)) { 1111 void _streamSetErrorHandler(callback(e)) {
1100 _streamErrorHandler = callback; 1112 _streamErrorHandler = callback;
1101 } 1113 }
1102 1114
1103 int _statusCode; 1115 int _statusCode;
1104 String _reasonPhrase; 1116 String _reasonPhrase;
1105 1117
1106 _HttpClientConnection _connection; 1118 _HttpClientConnection _connection;
1107 _HttpInputStream _inputStream; 1119 _HttpInputStream _inputStream;
1108 _BufferList _buffer; 1120 _BufferList _buffer;
1109 Function _streamErrorHandler; 1121 Function _streamErrorHandler;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1145 _socketConn = null; 1157 _socketConn = null;
1146 } 1158 }
1147 1159
1148 HttpClientRequest open(String method, String uri) { 1160 HttpClientRequest open(String method, String uri) {
1149 _method = method; 1161 _method = method;
1150 _request = new _HttpClientRequest(method, uri, this); 1162 _request = new _HttpClientRequest(method, uri, this);
1151 _response = new _HttpClientResponse(this); 1163 _response = new _HttpClientResponse(this);
1152 return _request; 1164 return _request;
1153 } 1165 }
1154 1166
1155 void _onConnectionClosed(Exception e) { 1167 void _onConnectionClosed(e) {
1156 // Socket is closed either due to an error or due to normal socket close. 1168 // Socket is closed either due to an error or due to normal socket close.
1157 if (e != null) { 1169 if (e != null) {
1158 if (_onErrorCallback != null) { 1170 if (_onErrorCallback != null) {
1159 _onErrorCallback(e); 1171 _onErrorCallback(e);
1160 } 1172 }
1161 } 1173 }
1162 _closing = true; 1174 _closing = true;
1163 if (e != null) { 1175 if (e != null) {
1164 // Propagate the error to the streams. 1176 // Propagate the error to the streams.
1165 if (_response != null && _response._streamErrorHandler != null) { 1177 if (_response != null && _response._streamErrorHandler != null) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1200 } 1212 }
1201 1213
1202 void set onRequest(void handler(HttpClientRequest request)) { 1214 void set onRequest(void handler(HttpClientRequest request)) {
1203 _onRequest = handler; 1215 _onRequest = handler;
1204 } 1216 }
1205 1217
1206 void set onResponse(void handler(HttpClientResponse response)) { 1218 void set onResponse(void handler(HttpClientResponse response)) {
1207 _onResponse = handler; 1219 _onResponse = handler;
1208 } 1220 }
1209 1221
1210 void set onError(void callback(Exception e)) { 1222 void set onError(void callback(e)) {
1211 _onErrorCallback = callback; 1223 _onErrorCallback = callback;
1212 } 1224 }
1213 1225
1214 Function _onRequest; 1226 Function _onRequest;
1215 Function _onResponse; 1227 Function _onResponse;
1216 Function _onErrorCallback; 1228 Function _onErrorCallback;
1217 1229
1218 _HttpClient _client; 1230 _HttpClient _client;
1219 _SocketConnection _socketConn; 1231 _SocketConnection _socketConn;
1220 HttpClientRequest _request; 1232 HttpClientRequest _request;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1336 _HttpClientConnection connection = new _HttpClientConnection(this); 1348 _HttpClientConnection connection = new _HttpClientConnection(this);
1337 1349
1338 // If there are active connections for this key get the first one 1350 // If there are active connections for this key get the first one
1339 // otherwise create a new one. 1351 // otherwise create a new one.
1340 Queue socketConnections = _openSockets[_connectionKey(host, port)]; 1352 Queue socketConnections = _openSockets[_connectionKey(host, port)];
1341 if (socketConnections == null || socketConnections.isEmpty()) { 1353 if (socketConnections == null || socketConnections.isEmpty()) {
1342 Socket socket = new Socket(host, port); 1354 Socket socket = new Socket(host, port);
1343 // Until the connection is established handle connection errors 1355 // Until the connection is established handle connection errors
1344 // here as the HttpClientConnection object is not yet associated 1356 // here as the HttpClientConnection object is not yet associated
1345 // with the socket. 1357 // with the socket.
1346 socket.onError = (Exception e) { 1358 socket.onError = (e) {
1347 // Report the error through the HttpClientConnection object to 1359 // Report the error through the HttpClientConnection object to
1348 // the client. 1360 // the client.
1349 connection._onError(e); 1361 connection._onError(e);
1350 }; 1362 };
1351 socket.onConnect = () { 1363 socket.onConnect = () {
1352 // When the connection is established, clear the error 1364 // When the connection is established, clear the error
1353 // callback as it will now be handled by the 1365 // callback as it will now be handled by the
1354 // HttpClientConnection object which will be associated with 1366 // HttpClientConnection object which will be associated with
1355 // the connected socket. 1367 // the connected socket.
1356 socket.onError = null; 1368 socket.onError = null;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1420 _activeSockets.remove(socketConn); 1432 _activeSockets.remove(socketConn);
1421 sockets.addFirst(socketConn); 1433 sockets.addFirst(socketConn);
1422 } 1434 }
1423 1435
1424 Function _onOpen; 1436 Function _onOpen;
1425 Map<String, Queue<_SocketConnection>> _openSockets; 1437 Map<String, Queue<_SocketConnection>> _openSockets;
1426 Set<_SocketConnection> _activeSockets; 1438 Set<_SocketConnection> _activeSockets;
1427 Timer _evictionTimer; 1439 Timer _evictionTimer;
1428 bool _shutdown; // Has this HTTP client been shutdown? 1440 bool _shutdown; // Has this HTTP client been shutdown?
1429 } 1441 }
OLDNEW
« no previous file with comments | « runtime/bin/http.dart ('k') | runtime/bin/input_stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698