Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/BrowserCookie.cpp
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 "BrowserCookie.h"
18
#include <ctime>
19
#include "json.h"
20
21
namespace webdriver {
22
23
BrowserCookie::BrowserCookie(void) {
24
this->name_ = "";
25
this->value_ = "";
26
this->domain_ = "";
27
this->path_ = "";
28
this->expiration_time_ = 0;
29
this->is_secure_ = false;
30
this->is_httponly_ = false;
31
}
32
33
BrowserCookie::~BrowserCookie(void) {
34
}
35
36
BrowserCookie BrowserCookie::FromJson(const Json::Value& json_cookie) {
37
BrowserCookie cookie;
38
cookie.name_ = json_cookie["name"].asString();
39
cookie.value_ = json_cookie["value"].asString();
40
cookie.is_secure_ = json_cookie["secure"].asBool();
41
42
Json::Value expiry = json_cookie.get("expiry", Json::Value::null);
43
if (!expiry.isNull()) {
44
if (expiry.isNumeric()) {
45
cookie.expiration_time_ = expiry.asUInt64();
46
}
47
}
48
49
Json::Value domain = json_cookie.get("domain", Json::Value::null);
50
if (!domain.isNull() && domain.isString() && domain.asString() != "") {
51
cookie.domain_ = domain.asString();
52
}
53
54
Json::Value path = json_cookie.get("path", Json::Value::null);
55
if (!path.isNull() && path.isString() && path.asString() != "") {
56
cookie.path_ = path.asString();
57
}
58
return cookie;
59
}
60
61
std::string BrowserCookie::ToString() const {
62
std::string cookie_string(this->name_ +
63
"=" +
64
this->value_ +
65
"; ");
66
67
if (this->is_secure_) {
68
cookie_string += "secure; ";
69
}
70
71
if (this->expiration_time_ > 0) {
72
time_t expiration_time = static_cast<time_t>(this->expiration_time_);
73
time_t current_time;
74
time(&current_time);
75
long long expiration_seconds = expiration_time - current_time;
76
cookie_string += "max-age=" + std::to_string(expiration_seconds) + ";";
77
}
78
79
if (this->domain_.size() > 0) {
80
cookie_string += "domain=" + this->domain_ + "; ";
81
}
82
if (this->path_.size() > 0) {
83
cookie_string += "path=" + this->path_ + "; ";
84
}
85
return cookie_string;
86
}
87
88
Json::Value BrowserCookie::ToJson() {
89
Json::Value cookie;
90
cookie["name"] = this->name_;
91
cookie["value"] = this->value_;
92
cookie["secure"] = this->is_secure_;
93
cookie["httpOnly"] = this->is_httponly_;
94
if (this->domain_.size() > 0) {
95
cookie["domain"] = this->domain_;
96
}
97
if (this->path_.size() > 0) {
98
cookie["path"] = this->path_;
99
}
100
if (this->expiration_time_ > 0) {
101
cookie["expiry"] = this->expiration_time_;
102
}
103
return cookie;
104
}
105
106
BrowserCookie BrowserCookie::Copy(void) const {
107
BrowserCookie destination_cookie;
108
destination_cookie.set_name(this->name_);
109
destination_cookie.set_value(this->value_);
110
destination_cookie.set_domain(this->domain_);
111
destination_cookie.set_path(this->path_);
112
destination_cookie.set_is_secure(this->is_secure_);
113
destination_cookie.set_is_httponly(this->is_httponly_);
114
destination_cookie.set_expiration_time(this->expiration_time_);
115
return destination_cookie;
116
}
117
118
}
119
120