Skip to content

Commit

Permalink
Rewriting cookie handling for IE.
Browse files Browse the repository at this point in the history
The IE driver should now be able to return full information for most
cookies, including expiration date and secure status. Additionally,
the driver will now return HTTP-only cookies from IE. Note that some
specific cookie information will still be incomplete. In particular,
session cookies that may have been set with domain, path, or secure
tokens in the cookie string will not have those tokens set in the
returned cookie, as that information is not available via any API. Also
note that this change introduces additional use of Windows APIs that
are not supported when run from a service process, making that more
completely unsupported. Fixes issue SeleniumHQ#391.
  • Loading branch information
jimevans committed Jun 23, 2015
1 parent b5728fc commit 43ec621
Show file tree
Hide file tree
Showing 21 changed files with 8,726 additions and 8,543 deletions.
53 changes: 53 additions & 0 deletions cpp/iedriver/BrowserCookie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "BrowserCookie.h"
#include "json.h"

namespace webdriver {

BrowserCookie::BrowserCookie(void) {
this->name_ = "";
this->value_ = "";
this->domain_ = "";
this->path_ = "";
this->expiration_time_ = 0L;
this->is_secure_ = false;
this->is_httponly_ = false;
}

BrowserCookie::~BrowserCookie(void) {
}

Json::Value BrowserCookie::ToJson() {
Json::Value cookie;
cookie["name"] = this->name_;
cookie["value"] = this->value_;
cookie["secure"] = this->is_secure_;
cookie["httpOnly"] = this->is_httponly_;
if (this->domain_.size() > 0) {
cookie["domain"] = this->domain_;
}
if (this->path_.size() > 0) {
cookie["path"] = this->path_;
}
if (this->expiration_time_ > 0) {
cookie["expiry"] = this->expiration_time_;
}
return cookie;
}

}
71 changes: 71 additions & 0 deletions cpp/iedriver/BrowserCookie.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef WEBDRIVER_IE_BROWSERCOOKIE_H_
#define WEBDRIVER_IE_BROWSERCOOKIE_H_

namespace Json {
class Value;
}

namespace webdriver {

class BrowserCookie
{
public:
BrowserCookie(void);
virtual ~BrowserCookie(void);

Json::Value ToJson(void);

std::string name(void) const { return this->name_; }
void set_name(const std::string& name) { this->name_ = name; }

std::string value(void) const { return this->value_; }
void set_value(const std::string& value) { this->value_ = value; }

std::string domain(void) const { return this->domain_; }
void set_domain(const std::string& domain) { this->domain_ = domain; }

std::string path(void) const { return this->path_; }
void set_path(const std::string& path) { this->path_ = path; }

bool is_secure(void) const { return this->is_secure_; }
void set_is_secure(const bool is_secure) { this->is_secure_ = is_secure; }

bool is_httponly(void) const { return this->is_httponly_; }
void set_is_httponly(const bool is_httponly) {
this->is_httponly_ = is_httponly;
}

long expiration_time(void) const { return this->expiration_time_; }
void set_expiration_time(const long expiration_time) {
this->expiration_time_ = expiration_time;
}

private:
std::string name_;
std::string value_;
std::string domain_;
std::string path_;
long expiration_time_;
bool is_secure_;
bool is_httponly_;
};

} // namespace webdriver

#endif // WEBDRIVER_IE_BROWSERCOOKIE_H_
6 changes: 4 additions & 2 deletions cpp/iedriver/CommandHandlers/AddCookieCommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ class AddCookieCommandHandler : public IECommandHandler {
cookie_value.removeMember("expiry");
if (expiry.isNumeric()) {
time_t expiration_time = static_cast<time_t>(expiry.asDouble());
char raw_formatted_time[30];
LOG(INFO) << "Received expiration time: " << expiration_time;
std::vector<char> raw_formatted_time(30);
tm time_info;
gmtime_s(&time_info, &expiration_time);
std::string month = this->GetMonthName(time_info.tm_mon);
std::string weekday = this->GetWeekdayName(time_info.tm_wday);
std::string format_string = weekday + ", %d " + month + " %Y %H:%M:%S GMT";
strftime(raw_formatted_time, 30 , format_string.c_str(), &time_info);
strftime(&raw_formatted_time[0], 30 , format_string.c_str(), &time_info);
std::string formatted_time(&raw_formatted_time[0]);
LOG(INFO) << "Formated expiration time: " << formatted_time;
cookie_string += "expires=" + formatted_time + "; ";
}

Expand Down
7 changes: 4 additions & 3 deletions cpp/iedriver/CommandHandlers/DeleteAllCookiesCommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define WEBDRIVER_IE_DELETEALLCOOKIESCOMMANDHANDLER_H_

#include "../Browser.h"
#include "../BrowserCookie.h"
#include "../IECommandHandler.h"
#include "../IECommandExecutor.h"

Expand All @@ -42,11 +43,11 @@ class DeleteAllCookiesCommandHandler : public IECommandHandler {
return;
}

std::map<std::string, std::string> cookies;
std::vector<BrowserCookie> cookies;
browser_wrapper->GetCookies(&cookies);
std::map<std::string, std::string>::const_iterator it = cookies.begin();
std::vector<BrowserCookie>::const_iterator it = cookies.begin();
for (; it != cookies.end(); ++it) {
std::string cookie_name = it->first;
std::string cookie_name = it->name();
status_code = browser_wrapper->DeleteCookie(cookie_name);
if (status_code != WD_SUCCESS) {
response->SetErrorResponse(status_code,
Expand Down
11 changes: 4 additions & 7 deletions cpp/iedriver/CommandHandlers/GetAllCookiesCommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "../Browser.h"
#include "../IECommandHandler.h"
#include "../IECommandExecutor.h"
#include "../BrowserCookie.h"

namespace webdriver {

Expand All @@ -43,15 +44,11 @@ class GetAllCookiesCommandHandler : public IECommandHandler {
return;
}

std::map<std::string, std::string> cookies;
std::vector<BrowserCookie> cookies;
browser_wrapper->GetCookies(&cookies);
std::map<std::string, std::string>::const_iterator it = cookies.begin();
std::vector<BrowserCookie>::iterator it = cookies.begin();
for (; it != cookies.end(); ++it) {
Json::Value cookie;
cookie["name"] = it->first;
cookie["value"] = it->second;
cookie["secure"] = false;
response_value.append(cookie);
response_value.append(it->ToJson());
}

response->SetSuccessResponse(response_value);
Expand Down
Loading

0 comments on commit 43ec621

Please sign in to comment.