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 String name;
80 String value;
81 String domain;
82 String path;
83 // Creation and expiration dates are expressed as milliseconds since the UNIX epoch.
84 double created { 0 };
85 double expires { 0 };
86 bool httpOnly { false };
87 bool secure { false };
88 bool session { false };
89 String comment;
90 URL commentURL;
91 Vector<uint16_t> ports;
92
93 enum class SameSitePolicy { None, Lax, Strict };
94 SameSitePolicy sameSite { SameSitePolicy::None };
95};
96
97struct CookieHash {
98 static unsigned hash(const Cookie& key)
99 {
100 return key.hash();
101 }
102
103 static bool equal(const Cookie& a, const Cookie& b)
104 {
105 return a == b;
106 }
107 static const bool safeToCompareToEmptyOrDeleted = false;
108};
109
110template<class Encoder>
111void Cookie::encode(Encoder& encoder) const
112{
113 encoder << name;
114 encoder << value;
115 encoder << domain;
116 encoder << path;
117 encoder << created;
118 encoder << expires;
119 encoder << httpOnly;
120 encoder << secure;
121 encoder << session;
122 encoder << comment;
123 encoder << commentURL;
124 encoder << ports;
125 encoder << sameSite;
126}
127
128template<class Decoder>
129Optional<Cookie> Cookie::decode(Decoder& decoder)
130{
131 Cookie cookie;
132 if (!decoder.decode(cookie.name))
133 return WTF::nullopt;
134 if (!decoder.decode(cookie.value))
135 return WTF::nullopt;
136 if (!decoder.decode(cookie.domain))
137 return WTF::nullopt;
138 if (!decoder.decode(cookie.path))
139 return WTF::nullopt;
140 if (!decoder.decode(cookie.created))
141 return WTF::nullopt;
142 if (!decoder.decode(cookie.expires))
143 return WTF::nullopt;
144 if (!decoder.decode(cookie.httpOnly))
145 return WTF::nullopt;
146 if (!decoder.decode(cookie.secure))
147 return WTF::nullopt;
148 if (!decoder.decode(cookie.session))
149 return WTF::nullopt;
150 if (!decoder.decode(cookie.comment))
151 return WTF::nullopt;
152 if (!decoder.decode(cookie.commentURL))
153 return WTF::nullopt;
154 if (!decoder.decode(cookie.ports))
155 return WTF::nullopt;
156 if (!decoder.decode(cookie.sameSite))
157 return WTF::nullopt;
158 return cookie;
159}
160
161}
162
163namespace WTF {
164 template<typename T> struct DefaultHash;
165 template<> struct DefaultHash<WebCore::Cookie> {
166 typedef WebCore::CookieHash Hash;
167 };
168 template<> struct HashTraits<WebCore::Cookie> : GenericHashTraits<WebCore::Cookie> {
169 static WebCore::Cookie emptyValue() { return { }; }
170 static void constructDeletedValue(WebCore::Cookie& slot) { slot = WebCore::Cookie(WTF::HashTableDeletedValue); }
171 static bool isDeletedValue(const WebCore::Cookie& slot) { return slot.name.isHashTableDeletedValue(); }
172 };
173 template<> struct EnumTraits<WebCore::Cookie::SameSitePolicy> {
174 using values = EnumValues<
175 WebCore::Cookie::SameSitePolicy,
176 WebCore::Cookie::SameSitePolicy::None,
177 WebCore::Cookie::SameSitePolicy::Lax,
178 WebCore::Cookie::SameSitePolicy::Strict
179 >;
180};
181}
182