Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 835 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 846 InputStream get inputStream { | 846 InputStream get inputStream { |
| 847 if (_inputStream == null) { | 847 if (_inputStream == null) { |
| 848 _inputStream = new _HttpInputStream(this); | 848 _inputStream = new _HttpInputStream(this); |
| 849 _inputStream._streamMarkedClosed = _dataEndCalled; | 849 _inputStream._streamMarkedClosed = _dataEndCalled; |
| 850 } | 850 } |
| 851 return _inputStream; | 851 return _inputStream; |
| 852 } | 852 } |
| 853 | 853 |
| 854 String get protocolVersion => _protocolVersion; | 854 String get protocolVersion => _protocolVersion; |
| 855 | 855 |
| 856 HttpSession session([init(HttpSession session)]) { | |
| 857 if (_session != null) { | |
| 858 // It's already mapped, use it. | |
| 859 return _session; | |
| 860 } | |
| 861 // Create session, store it in connection, and return. | |
| 862 var sessionManager = _httpConnection._server._sessionManager; | |
| 863 return _session = sessionManager.createSession(init); | |
| 864 } | |
| 865 | |
| 856 void _onRequestStart(String method, String uri, String version) { | 866 void _onRequestStart(String method, String uri, String version) { |
| 857 _method = method; | 867 _method = method; |
| 858 _uri = uri; | 868 _uri = uri; |
| 859 _parseRequestUri(uri); | 869 _parseRequestUri(uri); |
| 860 } | 870 } |
| 861 | 871 |
| 862 void _onHeaderReceived(String name, String value) { | 872 void _onHeaderReceived(String name, String value) { |
| 863 _headers.add(name, value); | 873 _headers.add(name, value); |
| 864 } | 874 } |
| 865 | 875 |
| 866 void _onHeadersComplete() { | 876 void _onHeadersComplete() { |
| 877 if (_httpConnection._server._sessionManagerInstance != null) { | |
|
Søren Gjesse
2012/10/22 12:56:29
Nice.
| |
| 878 // Map to session if exists. | |
| 879 var sessionId = cookies.reduce(null, (last, cookie) { | |
| 880 if (last != null) return last; | |
| 881 return cookie.name.toUpperCase() == _DART_SESSION_ID ? | |
| 882 cookie.value : null; | |
| 883 }); | |
| 884 if (sessionId != null) { | |
| 885 var sessionManager = _httpConnection._server._sessionManager; | |
| 886 _session = sessionManager.getSession(sessionId); | |
| 887 if (_session != null) { | |
| 888 _session._markSeen(); | |
| 889 } | |
| 890 } | |
| 891 } | |
| 892 | |
| 867 _headers._mutable = false; | 893 _headers._mutable = false; |
| 868 // Prepare for receiving data. | 894 // Prepare for receiving data. |
| 869 _buffer = new _BufferList(); | 895 _buffer = new _BufferList(); |
| 870 } | 896 } |
| 871 | 897 |
| 872 void _onDataReceived(List<int> data) { | 898 void _onDataReceived(List<int> data) { |
| 873 _buffer.add(data); | 899 _buffer.add(data); |
| 874 if (_inputStream != null) _inputStream._dataReceived(); | 900 if (_inputStream != null) _inputStream._dataReceived(); |
| 875 } | 901 } |
| 876 | 902 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 914 | 940 |
| 915 String _method; | 941 String _method; |
| 916 String _uri; | 942 String _uri; |
| 917 String _path; | 943 String _path; |
| 918 String _queryString; | 944 String _queryString; |
| 919 Map<String, String> _queryParameters; | 945 Map<String, String> _queryParameters; |
| 920 _HttpInputStream _inputStream; | 946 _HttpInputStream _inputStream; |
| 921 _BufferList _buffer; | 947 _BufferList _buffer; |
| 922 bool _dataEndCalled = false; | 948 bool _dataEndCalled = false; |
| 923 Function _streamErrorHandler; | 949 Function _streamErrorHandler; |
| 950 _HttpSession _session; | |
| 924 } | 951 } |
| 925 | 952 |
| 926 | 953 |
| 927 // HTTP response object for sending a HTTP response. | 954 // HTTP response object for sending a HTTP response. |
| 928 class _HttpResponse extends _HttpRequestResponseBase implements HttpResponse { | 955 class _HttpResponse extends _HttpRequestResponseBase implements HttpResponse { |
| 929 _HttpResponse(_HttpConnection httpConnection) | 956 _HttpResponse(_HttpConnection httpConnection) |
| 930 : super(httpConnection), | 957 : super(httpConnection), |
| 931 _statusCode = HttpStatus.OK; | 958 _statusCode = HttpStatus.OK; |
| 932 | 959 |
| 933 void set contentLength(int contentLength) { | 960 void set contentLength(int contentLength) { |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1091 _writeCRLF(); | 1118 _writeCRLF(); |
| 1092 | 1119 |
| 1093 // Determine the value of the "Transfer-Encoding" header based on | 1120 // Determine the value of the "Transfer-Encoding" header based on |
| 1094 // whether the content length is known. | 1121 // whether the content length is known. |
| 1095 if (_contentLength >= 0) { | 1122 if (_contentLength >= 0) { |
| 1096 _headers.set(HttpHeaders.CONTENT_LENGTH, _contentLength.toString()); | 1123 _headers.set(HttpHeaders.CONTENT_LENGTH, _contentLength.toString()); |
| 1097 } else if (_contentLength < 0) { | 1124 } else if (_contentLength < 0) { |
| 1098 _headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked"); | 1125 _headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked"); |
| 1099 } | 1126 } |
| 1100 | 1127 |
| 1128 var session = _httpConnection._request._session; | |
| 1129 if (session != null && !session._destroyed) { | |
| 1130 // Make sure we only send the current session id. | |
| 1131 bool found = false; | |
| 1132 for (int i = 0; i < cookies.length; i++) { | |
| 1133 if (cookies[i].name.toUpperCase() == _DART_SESSION_ID) { | |
| 1134 cookie.value = session.id; | |
| 1135 found = true; | |
| 1136 break; | |
| 1137 } | |
| 1138 } | |
| 1139 if (!found) { | |
| 1140 cookies.add(new Cookie(_DART_SESSION_ID, session.id)); | |
| 1141 } | |
| 1142 } | |
| 1101 // Add all the cookies set to the headers. | 1143 // Add all the cookies set to the headers. |
| 1102 if (_cookies != null) { | 1144 if (_cookies != null) { |
| 1103 _cookies.forEach((cookie) { | 1145 _cookies.forEach((cookie) { |
| 1104 _headers.add("set-cookie", cookie); | 1146 _headers.add("set-cookie", cookie); |
| 1105 }); | 1147 }); |
| 1106 } | 1148 } |
| 1107 | 1149 |
| 1108 // Write headers. | 1150 // Write headers. |
| 1109 bool allWritten = _writeHeaders(); | 1151 bool allWritten = _writeHeaders(); |
| 1110 _state = HEADER_SENT; | 1152 _state = HEADER_SENT; |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1434 Function onError; | 1476 Function onError; |
| 1435 } | 1477 } |
| 1436 | 1478 |
| 1437 | 1479 |
| 1438 class _RequestHandlerRegistration { | 1480 class _RequestHandlerRegistration { |
| 1439 _RequestHandlerRegistration(Function this._matcher, Function this._handler); | 1481 _RequestHandlerRegistration(Function this._matcher, Function this._handler); |
| 1440 Function _matcher; | 1482 Function _matcher; |
| 1441 Function _handler; | 1483 Function _handler; |
| 1442 } | 1484 } |
| 1443 | 1485 |
| 1444 | |
| 1445 // HTTP server waiting for socket connections. The connections are | 1486 // HTTP server waiting for socket connections. The connections are |
| 1446 // managed by the server and as requests are received the request. | 1487 // managed by the server and as requests are received the request. |
| 1447 class _HttpServer implements HttpServer { | 1488 class _HttpServer implements HttpServer { |
| 1448 _HttpServer() : _connections = new Set<_HttpConnection>(), | 1489 _HttpServer() : _connections = new Set<_HttpConnection>(), |
| 1449 _handlers = new List<_RequestHandlerRegistration>(); | 1490 _handlers = new List<_RequestHandlerRegistration>(); |
| 1450 | 1491 |
| 1451 void listen(String host, int port, {int backlog: 128}) { | 1492 void listen(String host, int port, {int backlog: 128}) { |
| 1452 listenOn(new ServerSocket(host, port, backlog)); | 1493 listenOn(new ServerSocket(host, port, backlog)); |
| 1453 _closeServer = true; | 1494 _closeServer = true; |
| 1454 } | 1495 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 1480 void handler(HttpRequest request, HttpResponse response)) { | 1521 void handler(HttpRequest request, HttpResponse response)) { |
| 1481 _handlers.add(new _RequestHandlerRegistration(matcher, handler)); | 1522 _handlers.add(new _RequestHandlerRegistration(matcher, handler)); |
| 1482 } | 1523 } |
| 1483 | 1524 |
| 1484 void set defaultRequestHandler( | 1525 void set defaultRequestHandler( |
| 1485 void handler(HttpRequest request, HttpResponse response)) { | 1526 void handler(HttpRequest request, HttpResponse response)) { |
| 1486 _defaultHandler = handler; | 1527 _defaultHandler = handler; |
| 1487 } | 1528 } |
| 1488 | 1529 |
| 1489 void close() { | 1530 void close() { |
| 1490 if (_server !== null && _closeServer) { | 1531 if (_sessionManagerInstance != null) { |
| 1532 _sessionManagerInstance.close(); | |
| 1533 _sessionManagerInstance = null; | |
| 1534 } | |
| 1535 if (_server != null && _closeServer) { | |
| 1491 _server.close(); | 1536 _server.close(); |
| 1492 } | 1537 } |
| 1493 _server = null; | 1538 _server = null; |
| 1494 for (_HttpConnection connection in _connections) { | 1539 for (_HttpConnection connection in _connections) { |
| 1495 connection._destroy(); | 1540 connection._destroy(); |
| 1496 } | 1541 } |
| 1497 _connections.clear(); | 1542 _connections.clear(); |
| 1498 } | 1543 } |
| 1499 | 1544 |
| 1500 int get port { | 1545 int get port { |
| 1501 if (_server === null) { | 1546 if (_server === null) { |
| 1502 throw new HttpException("The HttpServer is not listening on a port."); | 1547 throw new HttpException("The HttpServer is not listening on a port."); |
| 1503 } | 1548 } |
| 1504 return _server.port; | 1549 return _server.port; |
| 1505 } | 1550 } |
| 1506 | 1551 |
| 1507 void set onError(void callback(e)) { | 1552 void set onError(void callback(e)) { |
| 1508 _onError = callback; | 1553 _onError = callback; |
| 1509 } | 1554 } |
| 1510 | 1555 |
| 1556 int set sessionTimeout(int timeout) { | |
| 1557 _sessionManager.sessionTimeout = timeout; | |
| 1558 } | |
| 1559 | |
| 1511 void _handleRequest(HttpRequest request, HttpResponse response) { | 1560 void _handleRequest(HttpRequest request, HttpResponse response) { |
| 1512 for (int i = 0; i < _handlers.length; i++) { | 1561 for (int i = 0; i < _handlers.length; i++) { |
| 1513 if (_handlers[i]._matcher(request)) { | 1562 if (_handlers[i]._matcher(request)) { |
| 1514 Function handler = _handlers[i]._handler; | 1563 Function handler = _handlers[i]._handler; |
| 1515 try { | 1564 try { |
| 1516 handler(request, response); | 1565 handler(request, response); |
| 1517 } catch (e) { | 1566 } catch (e) { |
| 1518 if (_onError != null) { | 1567 if (_onError != null) { |
| 1519 _onError(e); | 1568 _onError(e); |
| 1520 } else { | 1569 } else { |
| 1521 throw e; | 1570 throw e; |
| 1522 } | 1571 } |
| 1523 } | 1572 } |
| 1524 return; | 1573 return; |
| 1525 } | 1574 } |
| 1526 } | 1575 } |
| 1527 | 1576 |
| 1528 if (_defaultHandler != null) { | 1577 if (_defaultHandler != null) { |
| 1529 _defaultHandler(request, response); | 1578 _defaultHandler(request, response); |
| 1530 } else { | 1579 } else { |
| 1531 response.statusCode = HttpStatus.NOT_FOUND; | 1580 response.statusCode = HttpStatus.NOT_FOUND; |
| 1532 response.contentLength = 0; | 1581 response.contentLength = 0; |
| 1533 response.outputStream.close(); | 1582 response.outputStream.close(); |
| 1534 } | 1583 } |
| 1535 } | 1584 } |
| 1536 | 1585 |
| 1586 _HttpSessionManager get _sessionManager { | |
| 1587 // Lazy init. | |
| 1588 if (_sessionManagerInstance == null) { | |
| 1589 _sessionManagerInstance = new _HttpSessionManager(); | |
| 1590 } | |
| 1591 return _sessionManagerInstance; | |
| 1592 } | |
| 1593 | |
| 1537 | 1594 |
| 1538 ServerSocket _server; // The server listen socket. | 1595 ServerSocket _server; // The server listen socket. |
| 1539 bool _closeServer = false; | 1596 bool _closeServer = false; |
| 1540 Set<_HttpConnection> _connections; // Set of currently connected clients. | 1597 Set<_HttpConnection> _connections; // Set of currently connected clients. |
| 1541 List<_RequestHandlerRegistration> _handlers; | 1598 List<_RequestHandlerRegistration> _handlers; |
| 1542 Object _defaultHandler; | 1599 Object _defaultHandler; |
| 1543 Function _onError; | 1600 Function _onError; |
| 1601 _HttpSessionManager _sessionManagerInstance; | |
| 1544 } | 1602 } |
| 1545 | 1603 |
| 1546 | 1604 |
| 1547 class _HttpClientRequest | 1605 class _HttpClientRequest |
| 1548 extends _HttpRequestResponseBase implements HttpClientRequest { | 1606 extends _HttpRequestResponseBase implements HttpClientRequest { |
| 1549 _HttpClientRequest(String this._method, | 1607 _HttpClientRequest(String this._method, |
| 1550 Uri this._uri, | 1608 Uri this._uri, |
| 1551 _HttpClientConnection connection) | 1609 _HttpClientConnection connection) |
| 1552 : super(connection) { | 1610 : super(connection) { |
| 1553 _connection = connection; | 1611 _connection = connection; |
| (...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2291 | 2349 |
| 2292 | 2350 |
| 2293 class _RedirectInfo implements RedirectInfo { | 2351 class _RedirectInfo implements RedirectInfo { |
| 2294 const _RedirectInfo(int this.statusCode, | 2352 const _RedirectInfo(int this.statusCode, |
| 2295 String this.method, | 2353 String this.method, |
| 2296 Uri this.location); | 2354 Uri this.location); |
| 2297 final int statusCode; | 2355 final int statusCode; |
| 2298 final String method; | 2356 final String method; |
| 2299 final Uri location; | 2357 final Uri location; |
| 2300 } | 2358 } |
| OLD | NEW |