QtWebApp
httpcookie.cpp
Go to the documentation of this file.
1 
6 #include "httpcookie.h"
7 
8 using namespace stefanfrings;
9 
11 {
12  version=1;
13  maxAge=0;
14  secure=false;
15 }
16 
17 HttpCookie::HttpCookie(const QByteArray name, const QByteArray value, const int maxAge, const QByteArray path,
18  const QByteArray comment, const QByteArray domain, const bool secure, const bool httpOnly,
19  const QByteArray sameSite)
20 {
21  this->name=name;
22  this->value=value;
23  this->maxAge=maxAge;
24  this->path=path;
25  this->comment=comment;
26  this->domain=domain;
27  this->secure=secure;
28  this->httpOnly=httpOnly;
29  this->sameSite=sameSite;
30  this->version=1;
31 }
32 
33 HttpCookie::HttpCookie(const QByteArray source)
34 {
35  version=1;
36  maxAge=0;
37  secure=false;
38  httpOnly=false;
39  QList<QByteArray> list=splitCSV(source);
40  foreach(QByteArray part, list)
41  {
42 
43  // Split the part into name and value
44  QByteArray name;
45  QByteArray value;
46  int posi=part.indexOf('=');
47  if (posi)
48  {
49  name=part.left(posi).trimmed();
50  value=part.mid(posi+1).trimmed();
51  }
52  else
53  {
54  name=part.trimmed();
55  value="";
56  }
57 
58  // Set fields
59  if (name=="Comment")
60  {
61  comment=value;
62  }
63  else if (name=="Domain")
64  {
65  domain=value;
66  }
67  else if (name=="Max-Age")
68  {
69  maxAge=value.toInt();
70  }
71  else if (name=="Path")
72  {
73  path=value;
74  }
75  else if (name=="Secure")
76  {
77  secure=true;
78  }
79  else if (name=="HttpOnly")
80  {
81  httpOnly=true;
82  }
83  else if (name=="SameSite")
84  {
85  sameSite=value;
86  }
87  else if (name=="Version")
88  {
89  version=value.toInt();
90  }
91  else {
92  if (this->name.isEmpty())
93  {
94  this->name=name;
95  this->value=value;
96  }
97  else
98  {
99  qWarning("HttpCookie: Ignoring unknown %s=%s",name.data(),value.data());
100  }
101  }
102  }
103 }
104 
105 QByteArray HttpCookie::toByteArray() const
106 {
107  QByteArray buffer(name);
108  buffer.append('=');
109  buffer.append(value);
110  if (!comment.isEmpty())
111  {
112  buffer.append("; Comment=");
113  buffer.append(comment);
114  }
115  if (!domain.isEmpty())
116  {
117  buffer.append("; Domain=");
118  buffer.append(domain);
119  }
120  if (maxAge!=0)
121  {
122  buffer.append("; Max-Age=");
123  buffer.append(QByteArray::number(maxAge));
124  }
125  if (!path.isEmpty())
126  {
127  buffer.append("; Path=");
128  buffer.append(path);
129  }
130  if (secure) {
131  buffer.append("; Secure");
132  }
133  if (httpOnly) {
134  buffer.append("; HttpOnly");
135  }
136  if (!sameSite.isEmpty()) {
137  buffer.append("; SameSite=");
138  buffer.append(sameSite);
139  }
140  buffer.append("; Version=");
141  buffer.append(QByteArray::number(version));
142  return buffer;
143 }
144 
145 void HttpCookie::setName(const QByteArray name)
146 {
147  this->name=name;
148 }
149 
150 void HttpCookie::setValue(const QByteArray value)
151 {
152  this->value=value;
153 }
154 
155 void HttpCookie::setComment(const QByteArray comment)
156 {
157  this->comment=comment;
158 }
159 
160 void HttpCookie::setDomain(const QByteArray domain)
161 {
162  this->domain=domain;
163 }
164 
165 void HttpCookie::setMaxAge(const int maxAge)
166 {
167  this->maxAge=maxAge;
168 }
169 
170 void HttpCookie::setPath(const QByteArray path)
171 {
172  this->path=path;
173 }
174 
175 void HttpCookie::setSecure(const bool secure)
176 {
177  this->secure=secure;
178 }
179 
180 void HttpCookie::setHttpOnly(const bool httpOnly)
181 {
182  this->httpOnly=httpOnly;
183 }
184 
185 void HttpCookie::setSameSite(const QByteArray sameSite)
186 {
187  this->sameSite=sameSite;
188 }
189 
190 QByteArray HttpCookie::getName() const
191 {
192  return name;
193 }
194 
195 QByteArray HttpCookie::getValue() const
196 {
197  return value;
198 }
199 
200 QByteArray HttpCookie::getComment() const
201 {
202  return comment;
203 }
204 
205 QByteArray HttpCookie::getDomain() const
206 {
207  return domain;
208 }
209 
211 {
212  return maxAge;
213 }
214 
215 QByteArray HttpCookie::getPath() const
216 {
217  return path;
218 }
219 
221 {
222  return secure;
223 }
224 
226 {
227  return httpOnly;
228 }
229 
230 QByteArray HttpCookie::getSameSite() const
231 {
232  return sameSite;
233 }
234 
236 {
237  return version;
238 }
239 
240 QList<QByteArray> HttpCookie::splitCSV(const QByteArray source)
241 {
242  bool inString=false;
243  QList<QByteArray> list;
244  QByteArray buffer;
245  for (int i=0; i<source.size(); ++i)
246  {
247  char c=source.at(i);
248  if (inString==false)
249  {
250  if (c=='\"')
251  {
252  inString=true;
253  }
254  else if (c==';')
255  {
256  QByteArray trimmed=buffer.trimmed();
257  if (!trimmed.isEmpty())
258  {
259  list.append(trimmed);
260  }
261  buffer.clear();
262  }
263  else
264  {
265  buffer.append(c);
266  }
267  }
268  else
269  {
270  if (c=='\"')
271  {
272  inString=false;
273  }
274  else {
275  buffer.append(c);
276  }
277  }
278  }
279  QByteArray trimmed=buffer.trimmed();
280  if (!trimmed.isEmpty())
281  {
282  list.append(trimmed);
283  }
284  return list;
285 }
QByteArray getValue() const
Get the value of this cookie.
Definition: httpcookie.cpp:195
void setName(const QByteArray name)
Set the name of this cookie.
Definition: httpcookie.cpp:145
bool getSecure() const
Get the secure flag of this cookie.
Definition: httpcookie.cpp:220
void setSameSite(const QByteArray sameSite)
Set same-site mode, so that the browser does not allow other web sites to access the cookie.
Definition: httpcookie.cpp:185
QByteArray getPath() const
Set the path of this cookie.
Definition: httpcookie.cpp:215
int getMaxAge() const
Get the maximum age of this cookie in seconds.
Definition: httpcookie.cpp:210
void setComment(const QByteArray comment)
Set the comment of this cookie.
Definition: httpcookie.cpp:155
void setValue(const QByteArray value)
Set the value of this cookie.
Definition: httpcookie.cpp:150
void setDomain(const QByteArray domain)
Set the domain of this cookie.
Definition: httpcookie.cpp:160
static QList< QByteArray > splitCSV(const QByteArray source)
Split a string list into parts, where each part is delimited by semicolon.
Definition: httpcookie.cpp:240
void setSecure(const bool secure)
Set secure mode, so that the cookie will be sent by the browser to the server only on secure connecti...
Definition: httpcookie.cpp:175
QByteArray getSameSite() const
Get the same-site flag of this cookie.
Definition: httpcookie.cpp:230
void setPath(const QByteArray path)
Set the path for that the cookie will be sent, default="/" which means the whole domain.
Definition: httpcookie.cpp:170
bool getHttpOnly() const
Get the HTTP-only flag of this cookie.
Definition: httpcookie.cpp:225
QByteArray getName() const
Get the name of this cookie.
Definition: httpcookie.cpp:190
int getVersion() const
Returns always 1.
Definition: httpcookie.cpp:235
HttpCookie()
Creates an empty cookie.
Definition: httpcookie.cpp:10
QByteArray getComment() const
Get the comment of this cookie.
Definition: httpcookie.cpp:200
void setMaxAge(const int maxAge)
Set the maximum age of this cookie in seconds.
Definition: httpcookie.cpp:165
void setHttpOnly(const bool httpOnly)
Set HTTP-only mode, so that the browser does not allow client-side scripts to access the cookie.
Definition: httpcookie.cpp:180
QByteArray getDomain() const
Get the domain of this cookie.
Definition: httpcookie.cpp:205
QByteArray toByteArray() const
Convert this cookie to a string that may be used in a Set-Cookie header.
Definition: httpcookie.cpp:105