OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 const String _DART_SESSION_ID = "DARTSESSID"; | |
6 | |
7 // A _HttpSession is a node in a double-linked list, with _next and _prev being | |
8 // the previous and next pointers. | |
9 class _HttpSession implements HttpSession { | |
10 _HttpSession(_HttpSessionManager this._sessionManager, String this.id) | |
11 : _lastSeen = new Date.now(); | |
12 | |
13 void destroy() { | |
14 _destroyed = true; | |
15 _sessionManager._removeFromTimeoutQueue(this); | |
16 _sessionManager._sessions.remove(id); | |
17 } | |
18 | |
19 // Mark the session as seen. This will reset the timeout and move the node to | |
20 // the end of the timeout queue. | |
21 void _markSeen() { | |
22 _lastSeen = new Date.now(); | |
23 _sessionManager._bumpToEnd(this); | |
24 } | |
25 | |
26 Dynamic data; | |
27 | |
28 Date get lastSeen => _lastSeen; | |
29 | |
30 final String id; | |
31 | |
32 void set onTimeout(void callback()) { | |
33 _timeoutCallback = callback; | |
34 } | |
35 | |
36 // Destroyed marked. Used by the http connection to see if a session is valid. | |
37 bool _destroyed = false; | |
38 Date _lastSeen; | |
39 Function _timeoutCallback; | |
40 _HttpSessionManager _sessionManager; | |
41 // Pointers in timeout queue. | |
42 _HttpSession _prev; | |
43 _HttpSession _next; | |
44 } | |
45 | |
46 // Private class used to manage all the active sessions. The sessions are stored | |
47 // in two ways: | |
48 // | |
49 // * In a map, mapping from ID to HttpSession. | |
50 // * In a linked list, used as a timeout queue. | |
51 class _HttpSessionManager { | |
52 _HttpSessionManager() : _sessions = {}; | |
53 | |
54 String createSessionId() { | |
55 const int _KEY_LENGTH = 16; // 128 bits. | |
56 var data = _getRandomBytes(_KEY_LENGTH); | |
57 return CryptoUtils.bytesToHex(data); | |
58 } | |
59 | |
60 _HttpSession getSession(String id) { | |
61 return _sessions[id]; | |
62 } | |
63 | |
64 _HttpSession createSession(init(HttpSession session)) { | |
65 var id = createSessionId(); | |
66 // TODO(ajohnsen): Consider adding a limit and throwing an exception. | |
67 // Should be very unlikely however. | |
68 while (_sessions.containsKey(id)) { | |
69 id = createSessionId(); | |
70 } | |
71 var session = _sessions[id] = new _HttpSession(this, id); | |
72 if (init != null) init(session); | |
73 _addToTimeoutQueue(session); | |
74 return session; | |
75 } | |
76 | |
77 void set sessionTimeout(int timeout) { | |
78 _sessionTimeout = timeout; | |
79 _stopTimer(); | |
80 _startTimer(); | |
81 } | |
82 | |
83 void close() { | |
84 _stopTimer(); | |
85 } | |
86 | |
87 void _bumpToEnd(_HttpSession session) { | |
88 _removeFromTimeoutQueue(session); | |
89 _addToTimeoutQueue(session); | |
90 } | |
91 | |
92 void _addToTimeoutQueue(_HttpSession session) { | |
93 if (_head == null) { | |
94 assert(_tail == null); | |
95 _tail = _head = session; | |
96 _startTimer(); | |
97 } else { | |
98 assert(_timer != null); | |
99 assert(_tail != null); | |
100 // Add to end. | |
101 _tail._next = session; | |
102 session._prev = _tail; | |
103 _tail = session; | |
104 } | |
105 } | |
106 | |
107 void _removeFromTimeoutQueue(_HttpSession session) { | |
108 if (session._next != null) { | |
109 session._next._prev = session._prev; | |
110 } | |
111 if (session._prev != null) { | |
112 session._prev._next = session._next; | |
113 } | |
114 if (_head == session) { | |
115 // We removed the head element, start new timer. | |
116 _head = session._next; | |
117 _stopTimer(); | |
118 _startTimer(); | |
119 } | |
120 if (_tail == session) { | |
121 _tail = session._prev; | |
122 } | |
123 session._next = session._prev = null; | |
124 } | |
125 | |
126 void _timerTimeout(_) { | |
127 _stopTimer(); // Clear timer. | |
128 assert(_head != null); | |
129 var session = _head; | |
130 session.destroy(); // Will remove the session from timeout queue and map. | |
131 if (session._timeoutCallback != null) { | |
132 session._timeoutCallback(); | |
133 } | |
134 } | |
135 | |
136 void _startTimer() { | |
137 assert(_timer == null); | |
138 if (_head != null) { | |
139 int seconds = new Date.now().difference(_head.lastSeen).inSeconds; | |
140 _timer = new Timer((_sessionTimeout - seconds) * 1000, _timerTimeout); | |
141 } | |
142 } | |
143 | |
144 void _stopTimer() { | |
145 if (_timer != null) { | |
146 _timer.cancel(); | |
147 _timer = null; | |
148 } | |
149 } | |
150 | |
151 Map<String, _HttpSession> _sessions; | |
152 int _sessionTimeout = 20 * 60; // 20 mins. | |
153 _HttpSession _head; | |
154 _HttpSession _tail; | |
155 Timer _timer; | |
156 | |
157 static Uint8List _getRandomBytes(int count) | |
158 native "Crypto_GetRandomBytes"; | |
159 } | |
160 | |
OLD | NEW |