OLD | NEW |
1 // Copyright (c) 2012, 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 const String _DART_SESSION_ID = "DARTSESSID"; | 7 const String _DART_SESSION_ID = "DARTSESSID"; |
8 | 8 |
9 // A _HttpSession is a node in a double-linked list, with _next and _prev being | 9 // A _HttpSession is a node in a double-linked list, with _next and _prev being |
10 // the previous and next pointers. | 10 // the previous and next pointers. |
11 class _HttpSession implements HttpSession { | 11 class _HttpSession implements HttpSession { |
| 12 // Destroyed marked. Used by the http connection to see if a session is valid. |
| 13 bool _destroyed = false; |
| 14 bool _isNew = true; |
| 15 DateTime _lastSeen; |
| 16 Function _timeoutCallback; |
| 17 _HttpSessionManager _sessionManager; |
| 18 // Pointers in timeout queue. |
| 19 _HttpSession _prev; |
| 20 _HttpSession _next; |
| 21 |
| 22 final Map _data = new Map(); |
| 23 |
12 _HttpSession(_HttpSessionManager this._sessionManager, String this.id) | 24 _HttpSession(_HttpSessionManager this._sessionManager, String this.id) |
13 : _lastSeen = new DateTime.now(); | 25 : _lastSeen = new DateTime.now(); |
14 | 26 |
15 void destroy() { | 27 void destroy() { |
16 _destroyed = true; | 28 _destroyed = true; |
17 _sessionManager._removeFromTimeoutQueue(this); | 29 _sessionManager._removeFromTimeoutQueue(this); |
18 _sessionManager._sessions.remove(id); | 30 _sessionManager._sessions.remove(id); |
19 } | 31 } |
20 | 32 |
21 // Mark the session as seen. This will reset the timeout and move the node to | 33 // Mark the session as seen. This will reset the timeout and move the node to |
22 // the end of the timeout queue. | 34 // the end of the timeout queue. |
23 void _markSeen() { | 35 void _markSeen() { |
24 _lastSeen = new DateTime.now(); | 36 _lastSeen = new DateTime.now(); |
25 _sessionManager._bumpToEnd(this); | 37 _sessionManager._bumpToEnd(this); |
26 } | 38 } |
27 | 39 |
28 dynamic data; | 40 DateTime get lastSeen => _lastSeen; |
29 | 41 |
30 DateTime get lastSeen => _lastSeen; | 42 bool get isNew => _isNew; |
31 | 43 |
32 final String id; | 44 final String id; |
33 | 45 |
34 void set onTimeout(void callback()) { | 46 void set onTimeout(void callback()) { |
35 _timeoutCallback = callback; | 47 _timeoutCallback = callback; |
36 } | 48 } |
37 | 49 |
38 // Destroyed marked. Used by the http connection to see if a session is valid. | 50 // Map implementation: |
39 bool _destroyed = false; | 51 bool containsValue(value) => _data.containsValue(value); |
40 DateTime _lastSeen; | 52 bool containsKey(key) => _data.containsKey(key); |
41 Function _timeoutCallback; | 53 operator [](key) => _data[key]; |
42 _HttpSessionManager _sessionManager; | 54 void operator []=(key, value) { _data[key] = value; } |
43 // Pointers in timeout queue. | 55 putIfAbsent(key, ifAbsent) => _data.putIfAbsent(key, ifAbsent); |
44 _HttpSession _prev; | 56 remove(key) => _data.remove(key); |
45 _HttpSession _next; | 57 void clear() => _data.clear(); |
| 58 void forEach(void f(key, value)) => _data.forEach(f); |
| 59 Iterable get keys => _data.keys; |
| 60 Iterable get values => _data.values; |
| 61 int get length => _data.length; |
| 62 bool get isEmpty => _data.isEmpty; |
46 } | 63 } |
47 | 64 |
48 // Private class used to manage all the active sessions. The sessions are stored | 65 // Private class used to manage all the active sessions. The sessions are stored |
49 // in two ways: | 66 // in two ways: |
50 // | 67 // |
51 // * In a map, mapping from ID to HttpSession. | 68 // * In a map, mapping from ID to HttpSession. |
52 // * In a linked list, used as a timeout queue. | 69 // * In a linked list, used as a timeout queue. |
53 class _HttpSessionManager { | 70 class _HttpSessionManager { |
54 _HttpSessionManager() : _sessions = {}; | 71 _HttpSessionManager() : _sessions = {}; |
55 | 72 |
56 String createSessionId() { | 73 String createSessionId() { |
57 const int _KEY_LENGTH = 16; // 128 bits. | 74 const int _KEY_LENGTH = 16; // 128 bits. |
58 var data = _getRandomBytes(_KEY_LENGTH); | 75 var data = _getRandomBytes(_KEY_LENGTH); |
59 return CryptoUtils.bytesToHex(data); | 76 return CryptoUtils.bytesToHex(data); |
60 } | 77 } |
61 | 78 |
62 _HttpSession getSession(String id) { | 79 _HttpSession getSession(String id) { |
63 return _sessions[id]; | 80 return _sessions[id]; |
64 } | 81 } |
65 | 82 |
66 _HttpSession createSession(init(HttpSession session)) { | 83 _HttpSession createSession() { |
67 var id = createSessionId(); | 84 var id = createSessionId(); |
68 // TODO(ajohnsen): Consider adding a limit and throwing an exception. | 85 // TODO(ajohnsen): Consider adding a limit and throwing an exception. |
69 // Should be very unlikely however. | 86 // Should be very unlikely however. |
70 while (_sessions.containsKey(id)) { | 87 while (_sessions.containsKey(id)) { |
71 id = createSessionId(); | 88 id = createSessionId(); |
72 } | 89 } |
73 var session = _sessions[id] = new _HttpSession(this, id); | 90 var session = _sessions[id] = new _HttpSession(this, id); |
74 if (init != null) init(session); | |
75 _addToTimeoutQueue(session); | 91 _addToTimeoutQueue(session); |
76 return session; | 92 return session; |
77 } | 93 } |
78 | 94 |
79 void set sessionTimeout(int timeout) { | 95 void set sessionTimeout(int timeout) { |
80 _sessionTimeout = timeout; | 96 _sessionTimeout = timeout; |
81 _stopTimer(); | 97 _stopTimer(); |
82 _startTimer(); | 98 _startTimer(); |
83 } | 99 } |
84 | 100 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 169 |
154 Map<String, _HttpSession> _sessions; | 170 Map<String, _HttpSession> _sessions; |
155 int _sessionTimeout = 20 * 60; // 20 mins. | 171 int _sessionTimeout = 20 * 60; // 20 mins. |
156 _HttpSession _head; | 172 _HttpSession _head; |
157 _HttpSession _tail; | 173 _HttpSession _tail; |
158 Timer _timer; | 174 Timer _timer; |
159 | 175 |
160 external static Uint8List _getRandomBytes(int count); | 176 external static Uint8List _getRandomBytes(int count); |
161 } | 177 } |
162 | 178 |
OLD | NEW |