Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/CommandHandlers/AddCookieCommandHandler.cpp
2868 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 "AddCookieCommandHandler.h"
18
#include <ctime>
19
#include "errorcodes.h"
20
#include "../Browser.h"
21
#include "../BrowserCookie.h"
22
#include "../CookieManager.h"
23
#include "../IECommandExecutor.h"
24
25
#define MAX_EXPIRATION_SECONDS 2147483647
26
27
namespace webdriver {
28
29
AddCookieCommandHandler::AddCookieCommandHandler(void) {
30
}
31
32
AddCookieCommandHandler::~AddCookieCommandHandler(void) {
33
}
34
35
void AddCookieCommandHandler::ExecuteInternal(
36
const IECommandExecutor& executor,
37
const ParametersMap& command_parameters,
38
Response* response) {
39
ParametersMap::const_iterator cookie_parameter_iterator = command_parameters.find("cookie");
40
if (cookie_parameter_iterator == command_parameters.end()) {
41
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: cookie");
42
return;
43
}
44
45
Json::Value cookie_value = cookie_parameter_iterator->second;
46
BrowserCookie cookie = BrowserCookie::FromJson(cookie_value);
47
48
if (cookie.expiration_time() > MAX_EXPIRATION_SECONDS) {
49
time_t current_time;
50
time(&current_time);
51
time_t max_time = current_time + MAX_EXPIRATION_SECONDS;
52
std::vector<char> raw_formatted_time(30);
53
tm time_info;
54
gmtime_s(&time_info, &max_time);
55
std::string format_string = "%a, %d %b %Y %H:%M:%S GMT";
56
strftime(&raw_formatted_time[0], 30, format_string.c_str(), &time_info);
57
std::string formatted_time(&raw_formatted_time[0]);
58
59
std::string error_message = "Internet Explorer does not allow cookies to ";
60
error_message.append("be set more than ");
61
error_message.append(std::to_string(MAX_EXPIRATION_SECONDS)).append(" ");
62
error_message.append("(2 ^ 32 - 1) seconds into the future, or ");
63
error_message.append(formatted_time).append(". This ia a limitaton of ");
64
error_message.append("the browser, not the driver.");
65
response->SetErrorResponse(ERROR_UNABLE_TO_SET_COOKIE, error_message);
66
return;
67
}
68
69
BrowserHandle browser_wrapper;
70
int status_code = executor.GetCurrentBrowser(&browser_wrapper);
71
if (status_code != WD_SUCCESS) {
72
response->SetErrorResponse(status_code, "Unable to get current browser");
73
return;
74
}
75
76
size_t last_path_slash_index = cookie.path().find_last_of("/");
77
if (last_path_slash_index != std::string::npos) {
78
std::string last_path_segment = cookie.path().substr(last_path_slash_index);
79
if (last_path_segment.size() > 1 &&
80
last_path_segment.find(".") != std::string::npos) {
81
// This algorithm is far from perfect. If the "path" property of the
82
// cookie includes the document name, the cookie won't be properly set,
83
// as IE's cookie handling expects a directory for path, not a file
84
// or document name. Strip the last segment of the path property (if
85
// if the path segment doesn't already end in a slash, and contains
86
// a period).
87
cookie.set_path(cookie.path().substr(0, last_path_slash_index));
88
}
89
}
90
91
status_code = browser_wrapper->cookie_manager()->SetCookie(
92
browser_wrapper->GetCurrentUrl(),
93
cookie);
94
95
if (status_code == EUNHANDLEDERROR) {
96
std::string error = "Could not set cookie. The most common cause ";
97
error.append("of this error is a mismatch in the bitness between the ");
98
error.append("driver and browser. In particular, be sure you are not ");
99
error.append("attempting to use a 64-bit IEDriverServer.exe against ");
100
error.append("IE 10 or 11, even on 64-bit Windows.");
101
response->SetErrorResponse(status_code, error);
102
return;
103
}
104
else if (status_code != WD_SUCCESS) {
105
response->SetErrorResponse(status_code, "Unable to add cookie to page");
106
return;
107
}
108
109
response->SetSuccessResponse(Json::Value::null);
110
}
111
112
} // namespace webdriver
113
114