Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/webdriver-server/server.h
2867 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
// Defines the server to respond to WebDriver JSON wire protocol commands.
18
// Subclasses are expected to provide their own initialization mechanism.
19
20
#ifndef WEBDRIVER_SERVER_SERVER_H_
21
#define WEBDRIVER_SERVER_SERVER_H_
22
23
#include <map>
24
#include <string>
25
#include <vector>
26
#if defined(_WIN32)
27
#include <memory>
28
#else
29
#include <tr1/memory>
30
#endif
31
#include "civetweb.h"
32
#include "command_types.h"
33
#include "response.h"
34
35
namespace webdriver {
36
37
class UriInfo;
38
class Session;
39
40
typedef std::shared_ptr<Session> SessionHandle;
41
42
class Server {
43
public:
44
explicit Server(const int port);
45
Server(const int port, const std::string& host);
46
Server(const int port, const std::string& host, const std::string& log_level, const std::string& log_file);
47
Server(const int port, const std::string& host, const std::string& log_level, const std::string& log_file, const std::string& acl);
48
virtual ~Server(void);
49
50
static int OnNewHttpRequest(struct mg_connection* conn);
51
52
bool Start(void);
53
void Stop(void);
54
int ProcessRequest(struct mg_connection* conn,
55
const struct mg_request_info* request_info);
56
57
int port(void) const { return this->port_; }
58
59
int session_count(void) const {
60
return static_cast<int>(this->sessions_.size());
61
}
62
63
protected:
64
virtual SessionHandle InitializeSession(void) = 0;
65
virtual std::string GetStatus(void) = 0;
66
virtual void ShutDown(void) = 0;
67
void AddCommand(const std::string& url,
68
const std::string& http_verb,
69
const std::string& command_name);
70
71
private:
72
typedef std::map<std::string, SessionHandle> SessionMap;
73
typedef std::map<std::string, std::shared_ptr<UriInfo> > UrlMap;
74
75
void Initialize(const int port,
76
const std::string& host,
77
const std::string& log_level,
78
const std::string& log_file,
79
const std::string& acl);
80
81
void ProcessWhitelist(const std::string& whitelist);
82
std::string GetListeningPorts(const bool use_ipv6);
83
std::string GetAccessControlList(void);
84
void GenerateOptionsList(std::vector<const char*>* options);
85
86
std::string ListSessions(void);
87
std::string LookupCommand(const std::string& uri,
88
const std::string& http_verb,
89
std::string* session_id,
90
std::string* locator);
91
std::string DispatchCommand(const std::string& url,
92
const std::string& http_verb,
93
const std::string& command_body);
94
void ShutDownSession(const std::string& session_id);
95
std::string ReadRequestBody(struct mg_connection* conn,
96
const struct mg_request_info* request_info);
97
bool LookupSession(const std::string& session_id,
98
SessionHandle* session_handle);
99
int SendResponseToClient(struct mg_connection* conn,
100
const struct mg_request_info* request_info,
101
const std::string& serialized_response);
102
void PopulateCommandRepository(void);
103
std::string ConstructLocatorParameterJson(std::vector<std::string> locator_param_names,
104
std::vector<std::string> locator_param_values,
105
std::string* session_id);
106
void SendHttpOk(mg_connection* connection,
107
const mg_request_info* request_info,
108
const std::string& body,
109
const std::string& content_type);
110
void SendHttpBadRequest(mg_connection* connection,
111
const mg_request_info* request_info,
112
const std::string& body);
113
void SendHttpInternalError(mg_connection* connection,
114
const mg_request_info* request_info,
115
const std::string& body);
116
void SendHttpMethodNotAllowed(mg_connection* connection,
117
const mg_request_info* request_info,
118
const std::string& allowed_methods,
119
const std::string& body);
120
void SendHttpNotFound(mg_connection* connection,
121
const mg_request_info* request_info,
122
const std::string& body);
123
void SendHttpTimeout(mg_connection* connection,
124
const mg_request_info* request_info,
125
const std::string& body);
126
void SendHttpNotImplemented(mg_connection* connection,
127
const mg_request_info* request_info,
128
const std::string& body);
129
void SendHttpSeeOther(mg_connection* connection,
130
const mg_request_info* request_info,
131
const std::string& location);
132
133
// The port used for communicating with this server.
134
int port_;
135
// The host IP address to which the server should bind.
136
std::string host_;
137
// List of whitelisted IPv4 addresses allowed to connect
138
// to this server.
139
std::vector<std::string> whitelist_;
140
// Map of options for the HTTP server
141
std::map<std::string, std::string> options_;
142
// The map of all command URIs (URL and HTTP verb), and
143
// the corresponding numerical value of the command.
144
UrlMap commands_;
145
// The map of all sessions currently active in this server.
146
SessionMap sessions_;
147
// The Mongoose context for this server.
148
struct mg_context* context_;
149
150
DISALLOW_COPY_AND_ASSIGN(Server);
151
};
152
153
} // namespace WebDriver
154
155
#endif // WEBDRIVER_SERVER_SERVER_H_
156
157