Path: blob/trunk/cpp/iedriver/CommandHandlers/AddCookieCommandHandler.cpp
2868 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516#include "AddCookieCommandHandler.h"17#include <ctime>18#include "errorcodes.h"19#include "../Browser.h"20#include "../BrowserCookie.h"21#include "../CookieManager.h"22#include "../IECommandExecutor.h"2324#define MAX_EXPIRATION_SECONDS 21474836472526namespace webdriver {2728AddCookieCommandHandler::AddCookieCommandHandler(void) {29}3031AddCookieCommandHandler::~AddCookieCommandHandler(void) {32}3334void AddCookieCommandHandler::ExecuteInternal(35const IECommandExecutor& executor,36const ParametersMap& command_parameters,37Response* response) {38ParametersMap::const_iterator cookie_parameter_iterator = command_parameters.find("cookie");39if (cookie_parameter_iterator == command_parameters.end()) {40response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: cookie");41return;42}4344Json::Value cookie_value = cookie_parameter_iterator->second;45BrowserCookie cookie = BrowserCookie::FromJson(cookie_value);4647if (cookie.expiration_time() > MAX_EXPIRATION_SECONDS) {48time_t current_time;49time(¤t_time);50time_t max_time = current_time + MAX_EXPIRATION_SECONDS;51std::vector<char> raw_formatted_time(30);52tm time_info;53gmtime_s(&time_info, &max_time);54std::string format_string = "%a, %d %b %Y %H:%M:%S GMT";55strftime(&raw_formatted_time[0], 30, format_string.c_str(), &time_info);56std::string formatted_time(&raw_formatted_time[0]);5758std::string error_message = "Internet Explorer does not allow cookies to ";59error_message.append("be set more than ");60error_message.append(std::to_string(MAX_EXPIRATION_SECONDS)).append(" ");61error_message.append("(2 ^ 32 - 1) seconds into the future, or ");62error_message.append(formatted_time).append(". This ia a limitaton of ");63error_message.append("the browser, not the driver.");64response->SetErrorResponse(ERROR_UNABLE_TO_SET_COOKIE, error_message);65return;66}6768BrowserHandle browser_wrapper;69int status_code = executor.GetCurrentBrowser(&browser_wrapper);70if (status_code != WD_SUCCESS) {71response->SetErrorResponse(status_code, "Unable to get current browser");72return;73}7475size_t last_path_slash_index = cookie.path().find_last_of("/");76if (last_path_slash_index != std::string::npos) {77std::string last_path_segment = cookie.path().substr(last_path_slash_index);78if (last_path_segment.size() > 1 &&79last_path_segment.find(".") != std::string::npos) {80// This algorithm is far from perfect. If the "path" property of the81// cookie includes the document name, the cookie won't be properly set,82// as IE's cookie handling expects a directory for path, not a file83// or document name. Strip the last segment of the path property (if84// if the path segment doesn't already end in a slash, and contains85// a period).86cookie.set_path(cookie.path().substr(0, last_path_slash_index));87}88}8990status_code = browser_wrapper->cookie_manager()->SetCookie(91browser_wrapper->GetCurrentUrl(),92cookie);9394if (status_code == EUNHANDLEDERROR) {95std::string error = "Could not set cookie. The most common cause ";96error.append("of this error is a mismatch in the bitness between the ");97error.append("driver and browser. In particular, be sure you are not ");98error.append("attempting to use a 64-bit IEDriverServer.exe against ");99error.append("IE 10 or 11, even on 64-bit Windows.");100response->SetErrorResponse(status_code, error);101return;102}103else if (status_code != WD_SUCCESS) {104response->SetErrorResponse(status_code, "Unable to add cookie to page");105return;106}107108response->SetSuccessResponse(Json::Value::null);109}110111} // namespace webdriver112113114