This repository has been archived by the owner on May 30, 2023. It is now read-only.
/
session_manager_request_handler.js
180 lines (150 loc) · 6.46 KB
/
session_manager_request_handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
This file is part of the GhostDriver by Ivan De Marino <http://ivandemarino.me>.
Copyright (c) 2012, Ivan De Marino <http://ivandemarino.me>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var ghostdriver = ghostdriver || {};
ghostdriver.SessionManagerReqHand = function() {
// private:
var
_protoParent = ghostdriver.SessionManagerReqHand.prototype,
_sessions = {}, //< will store key/value pairs like 'SESSION_ID : SESSION_OBJECT'
_sessionRHs = {},
_errors = _protoParent.errors,
_CLEANUP_WINDOWLESS_SESSIONS_TIMEOUT = 60000,
_handle = function(req, res) {
_protoParent.handle.call(this, req, res);
if (req.urlParsed.file === "session" && req.method === "POST") {
_postNewSessionCommand(req, res);
return;
} else if (req.urlParsed.file === "sessions" && req.method === "GET") {
_getActiveSessionsCommand(req, res);
return;
} else if (req.urlParsed.directory === "/session/") {
if (req.method === "GET") {
_getSessionCapabilitiesCommand(req, res);
} else if (req.method === "DELETE") {
_deleteSessionCommand(req, res);
}
return;
}
throw _errors.createInvalidReqInvalidCommandMethodEH(req);
},
_postNewSessionCommand = function(req, res) {
var newSession,
postObj = JSON.parse(req.post);
if (typeof(postObj) === "object") {
// Create and store a new Session
newSession = new ghostdriver.Session(postObj.desiredCapabilities);
_sessions[newSession.getId()] = newSession;
// console.log("New Session Created: " + newSession.getId());
// Redirect to the newly created Session
res.statusCode = 303; //< "303 See Other"
res.setHeader("Location", "http://" + req.headers.Host + "/wd/hub/session/"+newSession.getId());
res.closeGracefully();
return;
}
throw _errors.createInvalidReqMissingCommandParameterEH(req);
},
_getActiveSessionsCommand = function(req, res) {
var activeSessions = [],
sessionId;
// Create array of format '[{ "id" : SESSION_ID, "capabilities" : SESSION_CAPABILITIES_OBJECT }]'
for (sessionId in _sessions) {
activeSessions.push({
"id" : sessionId,
"capabilities" : _sessions[sessionId].getCapabilities()
});
}
res.success(null, activeSessions);
},
_deleteSession = function(sessionId) {
if (typeof(_sessions[sessionId]) !== "undefined") {
// Prepare the session to be deleted
_sessions[sessionId].aboutToDelete();
// Delete the session and the handler
delete _sessions[sessionId];
delete _sessionRHs[sessionId];
}
},
_deleteSessionCommand = function(req, res) {
var sId = req.urlParsed.file;
if (sId === "")
throw _errors.createInvalidReqMissingCommandParameterEH(req);
if (typeof(_sessions[sId]) !== "undefined") {
_deleteSession(sId);
res.success(sId);
} else {
throw _errors.createInvalidReqVariableResourceNotFoundEH(req);
}
},
_getSessionCapabilitiesCommand = function(req, res) {
var sId = req.urlParsed.file,
session;
if (sId === "")
throw _errors.createInvalidReqMissingCommandParameterEH(req);
session = _getSession(sId);
if (session !== null) {
res.success(sId, _sessions[sId].getCapabilities());
} else {
throw _errors.createInvalidReqVariableResourceNotFoundEH(req);
}
},
_getSession = function(sessionId) {
if (typeof(_sessions[sessionId]) !== "undefined") {
return _sessions[sessionId];
}
return null;
},
_getSessionReqHand = function(sessionId) {
if (_getSession(sessionId) !== null) {
// The session exists: what about the relative Session Request Handler?
if (typeof(_sessionRHs[sessionId]) === "undefined") {
_sessionRHs[sessionId] = new ghostdriver.SessionReqHand(_getSession(sessionId));
}
return _sessionRHs[sessionId];
}
return null;
},
_cleanupWindowlessSessions = function() {
var sId;
// Do this cleanup only if there are sessions
if (Object.keys(_sessions).length > 0) {
console.log("Asynchronous Sessions cleanup phase starting NOW");
for (sId in _sessions) {
if (_sessions[sId].getWindowsCount() === 0) {
console.log("About to delete Session '"+sId+"', because windowless...");
_deleteSession(sId);
console.log("... deleted!");
}
}
}
};
// Regularly cleanup un-used sessions
setInterval(_cleanupWindowlessSessions, _CLEANUP_WINDOWLESS_SESSIONS_TIMEOUT); //< every 60s
// public:
return {
handle : _handle,
getSession : _getSession,
getSessionReqHand : _getSessionReqHand
};
};
// prototype inheritance:
ghostdriver.SessionManagerReqHand.prototype = new ghostdriver.RequestHandler();