Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/webdriver-server/uri_info.cc
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
#include "uri_info.h"
18
19
namespace webdriver {
20
21
UriInfo::UriInfo(const std::string& uri,
22
const std::string& verb,
23
const std::string& command_name) {
24
ParseUri(uri, &this->fragments_, &this->parameter_indexes_);
25
this->AddHttpVerb(verb, command_name);
26
}
27
28
void UriInfo::ParseUri(const std::string& uri,
29
std::vector<std::string>* fragments,
30
std::vector<size_t>* parameter_indexes) {
31
size_t current_position = 0;
32
size_t token_end_position = 0;
33
size_t current_token_index = 0;
34
do {
35
token_end_position = uri.find("/", current_position);
36
std::string token = uri.substr(current_position,
37
token_end_position - current_position);
38
if (parameter_indexes != NULL && token.find(":") == 0) {
39
parameter_indexes->push_back(current_token_index);
40
}
41
fragments->push_back(token);
42
++current_token_index;
43
current_position = token_end_position + 1;
44
} while (token_end_position != std::string::npos);
45
}
46
47
48
bool UriInfo::IsUriMatch(const std::vector<std::string>& uri_fragments,
49
std::vector<std::string>* uri_param_names,
50
std::vector<std::string>* uri_param_values) {
51
if (uri_fragments.size() != this->fragments_.size()) {
52
return false;
53
}
54
55
// Loop through the fragments of the URI, and if any of them don't match,
56
// then the passed in URI isn't a match. However, if the current fragment
57
// is a template parameter (e.g., ":sessionid"), ignore the fact that the
58
// values of the fragment don't match, and move to the next parameter index.
59
size_t next_parameter_index = 0;
60
for (size_t index = 0; index < this->fragments_.size(); ++index) {
61
if (uri_fragments[index] != this->fragments_[index]) {
62
if (this->fragments_[index].find(":") != 0) {
63
return false;
64
}
65
if (next_parameter_index < this->parameter_indexes_.size() &&
66
this->parameter_indexes_[next_parameter_index] != index) {
67
return false;
68
} else {
69
if (!this->IsValidParameterValue(
70
this->fragments_[this->parameter_indexes_[next_parameter_index]],
71
uri_fragments[this->parameter_indexes_[next_parameter_index]])) {
72
return false;
73
}
74
++next_parameter_index;
75
}
76
}
77
}
78
79
// At this point, we know the URI is a match. Now we just need to copy the
80
// parameter names and values back to the caller. Note that the parameter
81
// name has a colon in front of it which must be stripped.
82
for (size_t index = 0; index < this->parameter_indexes_.size(); ++index) {
83
std::string param_name = this->fragments_[this->parameter_indexes_[index]];
84
uri_param_names->push_back(param_name.substr(1));
85
uri_param_values->push_back(uri_fragments[this->parameter_indexes_[index]]);
86
}
87
88
return true;
89
}
90
91
bool UriInfo::HasHttpVerb(const std::string& http_verb,
92
std::string* command_name) {
93
HttpVerbMap::const_iterator verb_iterator = this->verb_map_.find(http_verb);
94
if (verb_iterator != this->verb_map_.end()) {
95
*command_name = verb_iterator->second;
96
return true;
97
}
98
return false;
99
}
100
101
void UriInfo::AddHttpVerb(const std::string& http_verb,
102
const std::string& command_name) {
103
this->verb_map_[http_verb] = command_name;
104
}
105
106
bool UriInfo::IsValidParameterValue(const std::string& param_name,
107
const std::string& param_value) {
108
// TODO(JimEvans): Create an extensible mechanism for this.
109
if (param_name == ":id" && param_value == "active") {
110
return false;
111
}
112
return true;
113
}
114
115
116
std::string UriInfo::GetSupportedVerbs(void) {
117
std::string supported_verbs = "";
118
HttpVerbMap::const_iterator verb_iterator = this->verb_map_.begin();
119
for (; verb_iterator != this->verb_map_.end(); ++verb_iterator) {
120
if (supported_verbs.size() != 0) {
121
supported_verbs.append(",");
122
}
123
supported_verbs.append(verb_iterator->first);
124
}
125
return supported_verbs;
126
}
127
128
} // namespace WebDriver
129
130