1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/**
@file
@author Stefan Frings
*/
#include "httpcookie.h"
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) {
this->name=name;
this->value=value;
this->maxAge=maxAge;
this->path=path;
this->comment=comment;
this->domain=domain;
this->secure=secure;
this->version=1;
}
HttpCookie::HttpCookie(const QByteArray source) {
version=1;
maxAge=0;
secure=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=="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");
}
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;
}
QByteArray HttpCookie::getName() const {
return name;
}
QByteArray HttpCookie::getValue() const {
return value;
}
QByteArray HttpCookie::getComment() const {
return comment;
}
QByteArray HttpCookie::getDomain() const {
return domain;
}
int HttpCookie::getMaxAge() const {
return maxAge;
}
QByteArray HttpCookie::getPath() const {
return path;
}
bool HttpCookie::getSecure() const {
return secure;
}
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;
}
|