HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_NetworkCookie.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_NetworkCookie.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_NETWORKCOOKIE_H__
13 #define __NET_NETWORKCOOKIE_H__
14 
15 #include "NET_API.h"
16 
17 #include "NET_ConvertToType.h"
18 #include "NET_Time.h"
19 
20 #include <UT/UT_Array.h>
21 #include <UT/UT_StringHolder.h>
22 #include <UT/UT_Url.h>
23 
24 #include <ctime>
25 
26 class NET_NetworkCookie;
28 
30 {
31 public:
33  {
35  Full
36  };
37 
38  enum SameSite
39  {
43  };
44 
47 
48  NET_NetworkCookie(const NET_NetworkCookie &cookie) = default;
49  NET_NetworkCookie &operator=(const NET_NetworkCookie &cookie) = default;
50 
51  NET_NetworkCookie(NET_NetworkCookie &&cookie) = default;
52  NET_NetworkCookie &operator=(NET_NetworkCookie &&cookie) = default;
53 
54  bool operator==(const NET_NetworkCookie &cookie) const;
55  bool operator!=(const NET_NetworkCookie &cookie) const;
56 
57  bool hasSameIdentifier(const NET_NetworkCookie &cookie) const;
58  UT_StringHolder toString(StringForm form = Full) const;
59  bool hasMatchingDomain(const NET_NetworkCookie &cookie) const;
60  bool hasMatchingDomain(const UT_Url &url) const;
61  bool hasMatchingPath(const UT_StringRef &path) const;
62  bool hasMatchingPath(const UT_Url &url) const;
63  bool hasMatchingPath(const NET_NetworkCookie &cookie) const;
64  bool isEquivalent(const NET_NetworkCookie &cookie) const
65  {
66  return myName == cookie.myName && myDomain == cookie.myDomain
67  && myPath == cookie.myPath;
68  }
69 
70  bool isValid() const;
71  bool hasExpired() const;
72 
73  void normalize(const UT_Url &url);
74 
75  bool parseCookie(const UT_StringRef& cookie);
76  static void parseCookies(const UT_StringArray &cookie_string,
77  NET_CookieList &cookies);
78 
79  const UT_StringHolder &name() const { return myName; }
80  const UT_StringHolder &domain() const { return myDomain; }
81  const NET_Time &creation() const { return myCreationDate; }
82  const NET_Time &expiration() const { return myExpirationDate; }
83  const UT_StringHolder &path() const { return myPath; }
84  const UT_StringHolder &value() const { return myValue; }
85  bool isHttpOnly() const { return myHttpOnly; }
86  bool isSecure() const { return myIsSecure; }
87  const NET_Time &lastAccess() const { return myLastAccessDate; }
88  bool isPersistent() const { return myIsPersistent; }
89  int maxAge() const { return myMaxAge; }
90  SameSite sameSite() const { return mySameSite; }
91 
92  void setName(const UT_StringHolder &name) { myName = name; }
93  void setDomain(const UT_StringHolder &domain) { myDomain = domain; }
94  void setCreation(const NET_Time &t) { myCreationDate = t; }
95  void setExpiration(const NET_Time &t)
96  {
97  myExpirationDate = t;
98  myHasExpires = !myExpirationDate.isNull();
99  setPersistent(myHasExpires);
100  }
101  bool setExpiration(const UT_StringRef& date)
102  {
103  bool result = NET_Time::fromUTCString(date, myExpirationDate);
104 
105  myHasExpires = result && !myExpirationDate.isNull();
106  setPersistent(myHasExpires);
107  return result;
108  }
109  void setPath(const UT_StringHolder &path) { myPath = path; }
110  void setValue(const UT_StringHolder &value) { myValue = value; }
111  void setHttpOnly(bool http_only) { myHttpOnly = http_only; }
112  void setSecure(bool is_secure) { myIsSecure = is_secure; }
113  void setLastAccess(const NET_Time &t) { myLastAccessDate = t; }
114  void setPersistent(bool is_persistent) { myIsPersistent = is_persistent; }
115  void setMaxAge(int max_age) { myMaxAge = max_age; }
116  void setSameSite(SameSite same_site) { mySameSite = same_site; }
117 
118  // Debug print the entire contents of the cookie
119  void dump() const;
120 private:
122 
123  UT_StringHolder myName;
124  UT_StringHolder myDomain;
125  NET_Time myCreationDate;
126  NET_Time myExpirationDate;
127  UT_StringHolder myPath;
128  UT_StringHolder myValue;
129  bool myHttpOnly;
130  bool myIsSecure;
131  bool myIsPersistent;
132  NET_Time myLastAccessDate;
133  bool myHasExpires;
134  int myMaxAge = 0;
135  SameSite mySameSite = SAME_SITE_NONE;
136  // These variables are not used. They are only used to interface properly
137  // with QtWebEngine
138  int myPriority;
139 };
140 
141 inline bool
143 {
144  return myName == cookie.myName && myDomain == cookie.myDomain &&
145  myExpirationDate == cookie.myExpirationDate &&
146  myPath == cookie.myPath && myValue == cookie.myValue &&
147  myHttpOnly == cookie.myHttpOnly && myIsSecure == cookie.myIsSecure &&
148  myIsPersistent == cookie.myIsPersistent &&
149  myHasExpires == cookie.myHasExpires && myMaxAge == cookie.myMaxAge &&
150  mySameSite == cookie.mySameSite;
151 }
152 
153 inline bool
155 {
156  return !(*this == cookie);
157 }
158 
159 // Serialize
160 template<>
162 {
163  if (!cookie.isValid())
164  return false;
165 
166  writer.jsonValue(cookie.toString());
167  return true;
168 }
169 
170 template <>
172 {
173  UT_StringHolder str_cookie;
174  if (!data.import(str_cookie))
175  return false;
176 
177  return cookie.parseCookie(str_cookie);
178 }
179 
180 #endif // __NET_NETWORKCOOKIE_H__
181 
GLboolean * data
Definition: glcorearb.h:131
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
**But if you need a result
Definition: thread.h:613
#define NET_API
Definition: NET_API.h:9
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
Definition: UT_Url.h:22
GLuint const GLchar * name
Definition: glcorearb.h:786
GLdouble t
Definition: glad.h:2397
LeafData & operator=(const LeafData &)=delete
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
Definition: core.h:1131
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
static bool fromUTCString(const UT_StringRef &time_string, NET_Time &time)
Definition: format.h:895
constexpr T normalize(UT_FixedVector< T, D > &a) noexcept