1/*
2 * Copyright (C) 2009 Joseph Pecoraro. All rights reserved.
3 * Copyright (C) 2017-2018 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <wtf/URL.h>
30#include <wtf/text/StringHash.h>
31#include <wtf/text/WTFString.h>
32
33#ifdef __OBJC__
34#include <objc/objc.h>
35#endif
36
37#if USE(SOUP)
38typedef struct _SoupCookie SoupCookie;
39#endif
40
41namespace WebCore {
42
43struct Cookie {
44 Cookie() = default;
45 Cookie(WTF::HashTableDeletedValueType)
46 : name(WTF::HashTableDeletedValue)
47 {
48 }
49
50 template<class Encoder> void encode(Encoder&) const;
51 template<class Decoder> static Optional<Cookie> decode(Decoder&);
52
53 WEBCORE_EXPORT bool operator==(const Cookie&) const;
54 WEBCORE_EXPORT unsigned hash() const;
55
56#ifdef __OBJC__
57 WEBCORE_EXPORT Cookie(NSHTTPCookie *);
58 WEBCORE_EXPORT operator NSHTTPCookie *() const;
59#elif USE(SOUP)
60 explicit Cookie(SoupCookie*);
61 SoupCookie* toSoupCookie() const;
62#endif
63
64 bool isNull() const
65 {
66 return name.isNull()
67 && value.isNull()
68 && domain.isNull()
69 && path.isNull()
70 && !created
71 && !expires
72 && !httpOnly
73 && !secure
74 && !session
75 && comment.isNull()
76 && commentURL.isNull();
77 }
78
79 bool isKeyEqual(const Cookie& otherCookie) const
80 {
81 return name == otherCookie.name
82 && domain == otherCookie.domain
83 && path == otherCookie.path;
84 }
85
86 String name;
87 String value;
88 String domain;
89 String path;
90 // Creation and expiration dates are expressed as milliseconds since the UNIX epoch.
91 double created { 0 };
92 double expires { 0 };
93 bool httpOnly { false };
94 bool secure { false };
95 bool session { false };
96 String comment;
97 URL commentURL;
98 Vector<uint16_t> ports;
99
100 enum class SameSitePolicy { None, Lax, Strict };
101 SameSitePolicy sameSite { SameSitePolicy::None };
102};
103
104struct CookieHash {
105 static unsigned hash(const Cookie& key)
106 {
107 return key.hash();
108 }
109
110 static bool equal(const Cookie& a, const Cookie& b)
111 {
112 return a == b;
113 }
114 static const bool safeToCompareToEmptyOrDeleted = false;
115};
116
117template<class Encoder>
118void Cookie::encode(Encoder& encoder) const
119{
120 encoder << name;
121 encoder << value;
122 encoder << domain;
123 encoder << path;
124 encoder << created;
125 encoder << expires;
126 encoder << httpOnly;
127 encoder << secure;
128 encoder << session;
129 encoder << comment;
130 encoder << commentURL;
131 encoder << ports;
132 encoder << sameSite;
133}
134
135template<class Decoder>
136Optional<Cookie> Cookie::decode(Decoder& decoder)
137{
138 Cookie cookie;
139 if (!decoder.decode(cookie.name))
140 return WTF::nullopt;
141 if (!decoder.decode(cookie.value))
142 return WTF::nullopt;
143 if (!decoder.decode(cookie.domain))
144 return WTF::nullopt;
145 if (!decoder.decode(cookie.path))
146 return WTF::nullopt;
147 if (!decoder.decode(cookie.created))
148 return WTF::nullopt;
149 if (!decoder.decode(cookie.expires))
150 return WTF::nullopt;
151 if (!decoder.decode(cookie.httpOnly))
152 return WTF::nullopt;
153 if (!decoder.decode(cookie.secure))
154 return WTF::nullopt;
155 if (!decoder.decode(cookie.session))
156 return WTF::nullopt;
157 if (!decoder.decode(cookie.comment))
158 return WTF::nullopt;
159 if (!decoder.decode(cookie.commentURL))
160 return WTF::nullopt;
161 if (!decoder.decode(cookie.ports))
162 return WTF::nullopt;
163 if (!decoder.decode(cookie.sameSite))
164 return WTF::nullopt;
165 return cookie;
166}
167
168}
169
170namespace WTF {
171 template<typename T> struct DefaultHash;
172 template<> struct DefaultHash<WebCore::Cookie> {
173 typedef WebCore::CookieHash Hash;
174 };
175 template<> struct HashTraits<WebCore::Cookie> : GenericHashTraits<WebCore::Cookie> {
176 static WebCore::Cookie emptyValue() { return { }; }
177 static void constructDeletedValue(WebCore::Cookie& slot) { slot = WebCore::Cookie(WTF::HashTableDeletedValue); }
178 static bool isDeletedValue(const WebCore::Cookie& slot) { return slot.name.isHashTableDeletedValue(); }
179
180 static const bool hasIsEmptyValueFunction = true;
181 static bool isEmptyValue(const WebCore::Cookie& slot) { return slot.isNull(); }
182 };
183 template<> struct EnumTraits<WebCore::Cookie::SameSitePolicy> {
184 using values = EnumValues<
185 WebCore::Cookie::SameSitePolicy,
186 WebCore::Cookie::SameSitePolicy::None,
187 WebCore::Cookie::SameSitePolicy::Lax,
188 WebCore::Cookie::SameSitePolicy::Strict
189 >;
190};
191}
192