| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- /**
- @file
- @author Stefan Frings
- */
- #include "httpcookie.h"
- using namespace stefanfrings;
- HttpCookie::HttpCookie()
- {
- version=1;
- maxAge=0;
- secure=false;
- }
- HttpCookie::HttpCookie(const QByteArray name, const QByteArray value, const int maxAge, const QByteArray path,
- const QByteArray comment, const QByteArray domain, const bool secure, const bool httpOnly,
- const QByteArray sameSite)
- {
- this->name=name;
- this->value=value;
- this->maxAge=maxAge;
- this->path=path;
- this->comment=comment;
- this->domain=domain;
- this->secure=secure;
- this->httpOnly=httpOnly;
- this->sameSite=sameSite;
- this->version=1;
- }
- HttpCookie::HttpCookie(const QByteArray source)
- {
- version=1;
- maxAge=0;
- secure=false;
- httpOnly=false;
- QList<QByteArray> list=splitCSV(source);
- foreach(QByteArray part, list)
- {
- // Split the part into name and value
- QByteArray name;
- QByteArray value;
- int posi=part.indexOf('=');
- if (posi)
- {
- name=part.left(posi).trimmed();
- value=part.mid(posi+1).trimmed();
- }
- else
- {
- name=part.trimmed();
- value="";
- }
- // Set fields
- if (name=="Comment")
- {
- comment=value;
- }
- else if (name=="Domain")
- {
- domain=value;
- }
- else if (name=="Max-Age")
- {
- maxAge=value.toInt();
- }
- else if (name=="Path")
- {
- path=value;
- }
- else if (name=="Secure")
- {
- secure=true;
- }
- else if (name=="HttpOnly")
- {
- httpOnly=true;
- }
- else if (name=="SameSite")
- {
- sameSite=value;
- }
- else if (name=="Version")
- {
- version=value.toInt();
- }
- else {
- if (this->name.isEmpty())
- {
- this->name=name;
- this->value=value;
- }
- else
- {
- qWarning("HttpCookie: Ignoring unknown %s=%s",name.data(),value.data());
- }
- }
- }
- }
- QByteArray HttpCookie::toByteArray() const
- {
- QByteArray buffer(name);
- buffer.append('=');
- buffer.append(value);
- if (!comment.isEmpty())
- {
- buffer.append("; Comment=");
- buffer.append(comment);
- }
- if (!domain.isEmpty())
- {
- buffer.append("; Domain=");
- buffer.append(domain);
- }
- if (maxAge!=0)
- {
- buffer.append("; Max-Age=");
- buffer.append(QByteArray::number(maxAge));
- }
- if (!path.isEmpty())
- {
- buffer.append("; Path=");
- buffer.append(path);
- }
- if (secure) {
- buffer.append("; Secure");
- }
- if (httpOnly) {
- buffer.append("; HttpOnly");
- }
- if (!sameSite.isEmpty()) {
- buffer.append("; SameSite=");
- buffer.append(sameSite);
- }
- buffer.append("; Version=");
- buffer.append(QByteArray::number(version));
- return buffer;
- }
- void HttpCookie::setName(const QByteArray name)
- {
- this->name=name;
- }
- void HttpCookie::setValue(const QByteArray value)
- {
- this->value=value;
- }
- void HttpCookie::setComment(const QByteArray comment)
- {
- this->comment=comment;
- }
- void HttpCookie::setDomain(const QByteArray domain)
- {
- this->domain=domain;
- }
- void HttpCookie::setMaxAge(const int maxAge)
- {
- this->maxAge=maxAge;
- }
- void HttpCookie::setPath(const QByteArray path)
- {
- this->path=path;
- }
- void HttpCookie::setSecure(const bool secure)
- {
- this->secure=secure;
- }
- void HttpCookie::setHttpOnly(const bool httpOnly)
- {
- this->httpOnly=httpOnly;
- }
- void HttpCookie::setSameSite(const QByteArray sameSite)
- {
- this->sameSite=sameSite;
- }
- const QByteArray& HttpCookie::getName() const
- {
- return name;
- }
- const QByteArray& HttpCookie::getValue() const
- {
- return value;
- }
- const QByteArray& HttpCookie::getComment() const
- {
- return comment;
- }
- const QByteArray& HttpCookie::getDomain() const
- {
- return domain;
- }
- int HttpCookie::getMaxAge() const
- {
- return maxAge;
- }
- const QByteArray& HttpCookie::getPath() const
- {
- return path;
- }
- bool HttpCookie::getSecure() const
- {
- return secure;
- }
- bool HttpCookie::getHttpOnly() const
- {
- return httpOnly;
- }
- const QByteArray& HttpCookie::getSameSite() const
- {
- return sameSite;
- }
- int HttpCookie::getVersion() const
- {
- return version;
- }
- QList<QByteArray> HttpCookie::splitCSV(const QByteArray source)
- {
- bool inString=false;
- QList<QByteArray> list;
- QByteArray buffer;
- for (int i=0; i<source.size(); ++i)
- {
- char c=source.at(i);
- if (inString==false)
- {
- if (c=='\"')
- {
- inString=true;
- }
- else if (c==';')
- {
- QByteArray trimmed=buffer.trimmed();
- if (!trimmed.isEmpty())
- {
- list.append(trimmed);
- }
- buffer.clear();
- }
- else
- {
- buffer.append(c);
- }
- }
- else
- {
- if (c=='\"')
- {
- inString=false;
- }
- else {
- buffer.append(c);
- }
- }
- }
- QByteArray trimmed=buffer.trimmed();
- if (!trimmed.isEmpty())
- {
- list.append(trimmed);
- }
- return list;
- }
|