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

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

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

Powered by Google App Engine
This is Rietveld 408576698