Go to the documentation of this file.00001
00006 #include "httpcookie.h"
00007
00008 HttpCookie::HttpCookie() {
00009 version=1;
00010 maxAge=0;
00011 secure=false;
00012 }
00013
00014 HttpCookie::HttpCookie(const QByteArray name, const QByteArray value, const int maxAge, const QByteArray path, const QByteArray comment, const QByteArray domain, const bool secure) {
00015 this->name=name;
00016 this->value=value;
00017 this->maxAge=maxAge;
00018 this->path=path;
00019 this->comment=comment;
00020 this->domain=domain;
00021 this->secure=secure;
00022 this->version=1;
00023 }
00024
00025 HttpCookie::HttpCookie(const QByteArray source) {
00026 version=1;
00027 maxAge=0;
00028 secure=false;
00029 QList<QByteArray> list=splitCSV(source);
00030 foreach(QByteArray part, list) {
00031
00032
00033 QByteArray name;
00034 QByteArray value;
00035 int posi=part.indexOf('=');
00036 if (posi) {
00037 name=part.left(posi).trimmed();
00038 value=part.mid(posi+1).trimmed();
00039 }
00040 else {
00041 name=part.trimmed();
00042 value="";
00043 }
00044
00045
00046 if (name=="Comment") {
00047 comment=value;
00048 }
00049 else if (name=="Domain") {
00050 domain=value;
00051 }
00052 else if (name=="Max-Age") {
00053 maxAge=value.toInt();
00054 }
00055 else if (name=="Path") {
00056 path=value;
00057 }
00058 else if (name=="Secure") {
00059 secure=true;
00060 }
00061 else if (name=="Version") {
00062 version=value.toInt();
00063 }
00064 else {
00065 if (this->name.isEmpty()) {
00066 this->name=name;
00067 this->value=value;
00068 }
00069 else {
00070 qWarning("HttpCookie: Ignoring unknown %s=%s",name.data(),value.data());
00071 }
00072 }
00073 }
00074 }
00075
00076 QByteArray HttpCookie::toByteArray() const {
00077 QByteArray buffer(name);
00078 buffer.append('=');
00079 buffer.append(value);
00080 if (!comment.isEmpty()) {
00081 buffer.append("; Comment=");
00082 buffer.append(comment);
00083 }
00084 if (!domain.isEmpty()) {
00085 buffer.append("; Domain=");
00086 buffer.append(domain);
00087 }
00088 if (maxAge!=0) {
00089 buffer.append("; Max-Age=");
00090 buffer.append(QByteArray::number(maxAge));
00091 }
00092 if (!path.isEmpty()) {
00093 buffer.append("; Path=");
00094 buffer.append(path);
00095 }
00096 if (secure) {
00097 buffer.append("; Secure");
00098 }
00099 buffer.append("; Version=");
00100 buffer.append(QByteArray::number(version));
00101 return buffer;
00102 }
00103
00104 void HttpCookie::setName(const QByteArray name){
00105 this->name=name;
00106 }
00107
00108 void HttpCookie::setValue(const QByteArray value){
00109 this->value=value;
00110 }
00111
00112 void HttpCookie::setComment(const QByteArray comment){
00113 this->comment=comment;
00114 }
00115
00116 void HttpCookie::setDomain(const QByteArray domain){
00117 this->domain=domain;
00118 }
00119
00120 void HttpCookie::setMaxAge(const int maxAge){
00121 this->maxAge=maxAge;
00122 }
00123
00124 void HttpCookie::setPath(const QByteArray path){
00125 this->path=path;
00126 }
00127
00128 void HttpCookie::setSecure(const bool secure){
00129 this->secure=secure;
00130 }
00131
00132 QByteArray HttpCookie::getName() const {
00133 return name;
00134 }
00135
00136 QByteArray HttpCookie::getValue() const {
00137 return value;
00138 }
00139
00140 QByteArray HttpCookie::getComment() const {
00141 return comment;
00142 }
00143
00144 QByteArray HttpCookie::getDomain() const {
00145 return domain;
00146 }
00147
00148 int HttpCookie::getMaxAge() const {
00149 return maxAge;
00150 }
00151
00152 QByteArray HttpCookie::getPath() const {
00153 return path;
00154 }
00155
00156 bool HttpCookie::getSecure() const {
00157 return secure;
00158 }
00159
00160 int HttpCookie::getVersion() const {
00161 return version;
00162 }
00163
00164 QList<QByteArray> HttpCookie::splitCSV(const QByteArray source) {
00165 bool inString=false;
00166 QList<QByteArray> list;
00167 QByteArray buffer;
00168 for (int i=0; i<source.size(); ++i) {
00169 char c=source.at(i);
00170 if (inString==false) {
00171 if (c=='\"') {
00172 inString=true;
00173 }
00174 else if (c==';') {
00175 QByteArray trimmed=buffer.trimmed();
00176 if (!trimmed.isEmpty()) {
00177 list.append(trimmed);
00178 }
00179 buffer.clear();
00180 }
00181 else {
00182 buffer.append(c);
00183 }
00184 }
00185 else {
00186 if (c=='\"') {
00187 inString=false;
00188 }
00189 else {
00190 buffer.append(c);
00191 }
00192 }
00193 }
00194 QByteArray trimmed=buffer.trimmed();
00195 if (!trimmed.isEmpty()) {
00196 list.append(trimmed);
00197 }
00198 return list;
00199 }