SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
qtwebapp::HttpCookie Class Reference

#include <httpcookie.h>

Public Member Functions

 HttpCookie ()
 
 HttpCookie (const QByteArray name, const QByteArray value, const int maxAge, const QByteArray path="/", const QByteArray comment=QByteArray(), const QByteArray domain=QByteArray(), const bool secure=false, const bool httpOnly=false)
 
 HttpCookie (const QByteArray source)
 
QByteArray toByteArray () const
 
void setName (const QByteArray name)
 
void setValue (const QByteArray value)
 
void setComment (const QByteArray comment)
 
void setDomain (const QByteArray domain)
 
void setMaxAge (const int maxAge)
 
void setPath (const QByteArray path)
 
void setSecure (const bool secure)
 
void setHttpOnly (const bool httpOnly)
 
QByteArray getName () const
 
QByteArray getValue () const
 
QByteArray getComment () const
 
QByteArray getDomain () const
 
int getMaxAge () const
 
QByteArray getPath () const
 
bool getSecure () const
 
bool getHttpOnly () const
 
int getVersion () const
 

Static Public Member Functions

static QList< QByteArray > splitCSV (const QByteArray source)
 

Private Attributes

QByteArray name
 
QByteArray value
 
QByteArray comment
 
QByteArray domain
 
int maxAge
 
QByteArray path
 
bool secure
 
bool httpOnly
 
int version
 

Detailed Description

HTTP cookie as defined in RFC 2109. This class can also parse RFC 2965 cookies, but skips fields that are not defined in RFC 2109.

Definition at line 23 of file httpcookie.h.

Constructor & Destructor Documentation

◆ HttpCookie() [1/3]

HttpCookie::HttpCookie ( )

Creates an empty cookie

Definition at line 10 of file httpcookie.cpp.

References httpOnly, maxAge, secure, and version.

11 {
12  version=1;
13  maxAge=0;
14  secure=false;
15  httpOnly=false;
16 }

◆ HttpCookie() [2/3]

HttpCookie::HttpCookie ( const QByteArray  name,
const QByteArray  value,
const int  maxAge,
const QByteArray  path = "/",
const QByteArray  comment = QByteArray(),
const QByteArray  domain = QByteArray(),
const bool  secure = false,
const bool  httpOnly = false 
)

Create a cookie and set name/value pair.

Parameters
namename of the cookie
valuevalue of the cookie
maxAgemaximum age of the cookie in seconds. 0=discard immediately
pathPath for that the cookie will be sent, default="/" which means the whole domain
commentOptional comment, may be displayed by the web browser somewhere
domainOptional domain for that the cookie will be sent. Defaults to the current domain
secureIf true, the cookie will be sent by the browser to the server only on secure connections
httpOnlyIf true, the browser does not allow client-side scripts to access the cookie

Definition at line 18 of file httpcookie.cpp.

References comment, domain, httpOnly, maxAge, name, path, secure, value, and version.

19 {
20  this->name=name;
21  this->value=value;
22  this->maxAge=maxAge;
23  this->path=path;
24  this->comment=comment;
25  this->domain=domain;
26  this->secure=secure;
27  this->httpOnly=httpOnly;
28  this->version=1;
29 }
QByteArray comment
Definition: httpcookie.h:113

◆ HttpCookie() [3/3]

HttpCookie::HttpCookie ( const QByteArray  source)

Create a cookie from a string.

Parameters
sourceString as received in a HTTP Cookie2 header.

Definition at line 31 of file httpcookie.cpp.

References comment, domain, httpOnly, maxAge, name, path, secure, splitCSV(), value, and version.

32 {
33  version=1;
34  maxAge=0;
35  secure=false;
36  QList<QByteArray> list=splitCSV(source);
37  foreach(QByteArray part, list)
38  {
39 
40  // Split the part into name and value
41  QByteArray name;
42  QByteArray value;
43  int posi=part.indexOf('=');
44  if (posi)
45  {
46  name=part.left(posi).trimmed();
47  value=part.mid(posi+1).trimmed();
48  }
49  else
50  {
51  name=part.trimmed();
52  value="";
53  }
54 
55  // Set fields
56  if (name=="Comment")
57  {
58  comment=value;
59  }
60  else if (name=="Domain")
61  {
62  domain=value;
63  }
64  else if (name=="Max-Age")
65  {
66  maxAge=value.toInt();
67  }
68  else if (name=="Path")
69  {
70  path=value;
71  }
72  else if (name=="Secure")
73  {
74  secure=true;
75  }
76  else if (name=="HttpOnly")
77  {
78  httpOnly=true;
79  }
80  else if (name=="Version")
81  {
82  version=value.toInt();
83  }
84  else {
85  if (this->name.isEmpty())
86  {
87  this->name=name;
88  this->value=value;
89  }
90  else
91  {
92  qWarning("HttpCookie::HttpCookie: Ignoring unknown %s=%s",name.data(),value.data());
93  }
94  }
95  }
96 }
static QList< QByteArray > splitCSV(const QByteArray source)
Definition: httpcookie.cpp:219
QByteArray comment
Definition: httpcookie.h:113
+ Here is the call graph for this function:

Member Function Documentation

◆ getComment()

QByteArray HttpCookie::getComment ( ) const

Get the comment of this cookie

Definition at line 184 of file httpcookie.cpp.

References comment.

185 {
186  return comment;
187 }
QByteArray comment
Definition: httpcookie.h:113

◆ getDomain()

QByteArray HttpCookie::getDomain ( ) const

Get the domain of this cookie

Definition at line 189 of file httpcookie.cpp.

References domain.

190 {
191  return domain;
192 }

◆ getHttpOnly()

bool HttpCookie::getHttpOnly ( ) const

Get the HTTP-only flag of this cookie

Definition at line 209 of file httpcookie.cpp.

References httpOnly.

210 {
211  return httpOnly;
212 }

◆ getMaxAge()

int HttpCookie::getMaxAge ( ) const

Get the maximum age of this cookie in seconds.

Definition at line 194 of file httpcookie.cpp.

References maxAge.

195 {
196  return maxAge;
197 }

◆ getName()

QByteArray HttpCookie::getName ( ) const

Get the name of this cookie

Definition at line 174 of file httpcookie.cpp.

References name.

Referenced by qtwebapp::HttpResponse::setCookie().

175 {
176  return name;
177 }
+ Here is the caller graph for this function:

◆ getPath()

QByteArray HttpCookie::getPath ( ) const

Set the path of this cookie

Definition at line 199 of file httpcookie.cpp.

References path.

200 {
201  return path;
202 }

◆ getSecure()

bool HttpCookie::getSecure ( ) const

Get the secure flag of this cookie

Definition at line 204 of file httpcookie.cpp.

References secure.

205 {
206  return secure;
207 }

◆ getValue()

QByteArray HttpCookie::getValue ( ) const

Get the value of this cookie

Definition at line 179 of file httpcookie.cpp.

References value.

180 {
181  return value;
182 }

◆ getVersion()

int HttpCookie::getVersion ( ) const

Returns always 1

Definition at line 214 of file httpcookie.cpp.

References version.

215 {
216  return version;
217 }

◆ setComment()

void HttpCookie::setComment ( const QByteArray  comment)

Set the comment of this cookie

Definition at line 144 of file httpcookie.cpp.

References comment.

145 {
146  this->comment=comment;
147 }
QByteArray comment
Definition: httpcookie.h:113

◆ setDomain()

void HttpCookie::setDomain ( const QByteArray  domain)

Set the domain of this cookie

Definition at line 149 of file httpcookie.cpp.

References domain.

150 {
151  this->domain=domain;
152 }

◆ setHttpOnly()

void HttpCookie::setHttpOnly ( const bool  httpOnly)

Set HTTP-only mode, so that he browser does not allow client-side scripts to access the cookie

Definition at line 169 of file httpcookie.cpp.

References httpOnly.

170 {
171  this->httpOnly=httpOnly;
172 }

◆ setMaxAge()

void HttpCookie::setMaxAge ( const int  maxAge)

Set the maximum age of this cookie in seconds. 0=discard immediately

Definition at line 154 of file httpcookie.cpp.

References maxAge.

155 {
156  this->maxAge=maxAge;
157 }

◆ setName()

void HttpCookie::setName ( const QByteArray  name)

Set the name of this cookie

Definition at line 134 of file httpcookie.cpp.

References name.

135 {
136  this->name=name;
137 }

◆ setPath()

void HttpCookie::setPath ( const QByteArray  path)

Set the path for that the cookie will be sent, default="/" which means the whole domain

Definition at line 159 of file httpcookie.cpp.

References path.

160 {
161  this->path=path;
162 }

◆ setSecure()

void HttpCookie::setSecure ( const bool  secure)

Set secure mode, so that the cookie will be sent by the browser to the server only on secure connections

Definition at line 164 of file httpcookie.cpp.

References secure.

165 {
166  this->secure=secure;
167 }

◆ setValue()

void HttpCookie::setValue ( const QByteArray  value)

Set the value of this cookie

Definition at line 139 of file httpcookie.cpp.

References value.

140 {
141  this->value=value;
142 }

◆ splitCSV()

QList< QByteArray > HttpCookie::splitCSV ( const QByteArray  source)
static

Split a string list into parts, where each part is delimited by semicolon. Semicolons within double quotes are skipped. Double quotes are removed.

Definition at line 219 of file httpcookie.cpp.

References i.

Referenced by qtwebapp::HttpRequest::extractCookies(), and HttpCookie().

220 {
221  bool inString=false;
222  QList<QByteArray> list;
223  QByteArray buffer;
224  for (int i=0; i<source.size(); ++i)
225  {
226  char c=source.at(i);
227  if (inString==false)
228  {
229  if (c=='\"')
230  {
231  inString=true;
232  }
233  else if (c==';')
234  {
235  QByteArray trimmed=buffer.trimmed();
236  if (!trimmed.isEmpty())
237  {
238  list.append(trimmed);
239  }
240  buffer.clear();
241  }
242  else
243  {
244  buffer.append(c);
245  }
246  }
247  else
248  {
249  if (c=='\"')
250  {
251  inString=false;
252  }
253  else {
254  buffer.append(c);
255  }
256  }
257  }
258  QByteArray trimmed=buffer.trimmed();
259  if (!trimmed.isEmpty())
260  {
261  list.append(trimmed);
262  }
263  return list;
264 }
int32_t i
Definition: decimators.h:244
+ Here is the caller graph for this function:

◆ toByteArray()

QByteArray HttpCookie::toByteArray ( ) const

Convert this cookie to a string that may be used in a Set-Cookie header.

Definition at line 98 of file httpcookie.cpp.

References comment, domain, httpOnly, maxAge, name, path, secure, value, and version.

Referenced by qtwebapp::HttpResponse::writeHeaders().

99 {
100  QByteArray buffer(name);
101  buffer.append('=');
102  buffer.append(value);
103  if (!comment.isEmpty())
104  {
105  buffer.append("; Comment=");
106  buffer.append(comment);
107  }
108  if (!domain.isEmpty())
109  {
110  buffer.append("; Domain=");
111  buffer.append(domain);
112  }
113  if (maxAge!=0)
114  {
115  buffer.append("; Max-Age=");
116  buffer.append(QByteArray::number(maxAge));
117  }
118  if (!path.isEmpty())
119  {
120  buffer.append("; Path=");
121  buffer.append(path);
122  }
123  if (secure) {
124  buffer.append("; Secure");
125  }
126  if (httpOnly) {
127  buffer.append("; HttpOnly");
128  }
129  buffer.append("; Version=");
130  buffer.append(QByteArray::number(version));
131  return buffer;
132 }
QByteArray comment
Definition: httpcookie.h:113
+ Here is the caller graph for this function:

Member Data Documentation

◆ comment

QByteArray qtwebapp::HttpCookie::comment
private

Definition at line 113 of file httpcookie.h.

Referenced by getComment(), HttpCookie(), setComment(), and toByteArray().

◆ domain

QByteArray qtwebapp::HttpCookie::domain
private

Definition at line 114 of file httpcookie.h.

Referenced by getDomain(), HttpCookie(), setDomain(), and toByteArray().

◆ httpOnly

bool qtwebapp::HttpCookie::httpOnly
private

Definition at line 118 of file httpcookie.h.

Referenced by getHttpOnly(), HttpCookie(), setHttpOnly(), and toByteArray().

◆ maxAge

int qtwebapp::HttpCookie::maxAge
private

Definition at line 115 of file httpcookie.h.

Referenced by getMaxAge(), HttpCookie(), setMaxAge(), and toByteArray().

◆ name

QByteArray qtwebapp::HttpCookie::name
private

Definition at line 111 of file httpcookie.h.

Referenced by getName(), HttpCookie(), setName(), and toByteArray().

◆ path

QByteArray qtwebapp::HttpCookie::path
private

Definition at line 116 of file httpcookie.h.

Referenced by getPath(), HttpCookie(), setPath(), and toByteArray().

◆ secure

bool qtwebapp::HttpCookie::secure
private

Definition at line 117 of file httpcookie.h.

Referenced by getSecure(), HttpCookie(), setSecure(), and toByteArray().

◆ value

QByteArray qtwebapp::HttpCookie::value
private

Definition at line 112 of file httpcookie.h.

Referenced by getValue(), HttpCookie(), setValue(), and toByteArray().

◆ version

int qtwebapp::HttpCookie::version
private

Definition at line 119 of file httpcookie.h.

Referenced by getVersion(), HttpCookie(), and toByteArray().


The documentation for this class was generated from the following files: