httpcookie.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. @file
  3. @author Stefan Frings
  4. */
  5. #include "httpcookie.h"
  6. using namespace stefanfrings;
  7. HttpCookie::HttpCookie()
  8. {
  9. version=1;
  10. maxAge=0;
  11. secure=false;
  12. }
  13. HttpCookie::HttpCookie(const QByteArray name, const QByteArray value, const int maxAge, const QByteArray path,
  14. const QByteArray comment, const QByteArray domain, const bool secure, const bool httpOnly,
  15. const QByteArray sameSite)
  16. {
  17. this->name=name;
  18. this->value=value;
  19. this->maxAge=maxAge;
  20. this->path=path;
  21. this->comment=comment;
  22. this->domain=domain;
  23. this->secure=secure;
  24. this->httpOnly=httpOnly;
  25. this->sameSite=sameSite;
  26. this->version=1;
  27. }
  28. HttpCookie::HttpCookie(const QByteArray source)
  29. {
  30. version=1;
  31. maxAge=0;
  32. secure=false;
  33. httpOnly=false;
  34. QList<QByteArray> list=splitCSV(source);
  35. foreach(QByteArray part, list)
  36. {
  37. // Split the part into name and value
  38. QByteArray name;
  39. QByteArray value;
  40. int posi=part.indexOf('=');
  41. if (posi)
  42. {
  43. name=part.left(posi).trimmed();
  44. value=part.mid(posi+1).trimmed();
  45. }
  46. else
  47. {
  48. name=part.trimmed();
  49. value="";
  50. }
  51. // Set fields
  52. if (name=="Comment")
  53. {
  54. comment=value;
  55. }
  56. else if (name=="Domain")
  57. {
  58. domain=value;
  59. }
  60. else if (name=="Max-Age")
  61. {
  62. maxAge=value.toInt();
  63. }
  64. else if (name=="Path")
  65. {
  66. path=value;
  67. }
  68. else if (name=="Secure")
  69. {
  70. secure=true;
  71. }
  72. else if (name=="HttpOnly")
  73. {
  74. httpOnly=true;
  75. }
  76. else if (name=="SameSite")
  77. {
  78. sameSite=value;
  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: Ignoring unknown %s=%s",name.data(),value.data());
  93. }
  94. }
  95. }
  96. }
  97. QByteArray HttpCookie::toByteArray() const
  98. {
  99. QByteArray buffer(name);
  100. buffer.append('=');
  101. buffer.append(value);
  102. if (!comment.isEmpty())
  103. {
  104. buffer.append("; Comment=");
  105. buffer.append(comment);
  106. }
  107. if (!domain.isEmpty())
  108. {
  109. buffer.append("; Domain=");
  110. buffer.append(domain);
  111. }
  112. if (maxAge!=0)
  113. {
  114. buffer.append("; Max-Age=");
  115. buffer.append(QByteArray::number(maxAge));
  116. }
  117. if (!path.isEmpty())
  118. {
  119. buffer.append("; Path=");
  120. buffer.append(path);
  121. }
  122. if (secure) {
  123. buffer.append("; Secure");
  124. }
  125. if (httpOnly) {
  126. buffer.append("; HttpOnly");
  127. }
  128. if (!sameSite.isEmpty()) {
  129. buffer.append("; SameSite=");
  130. buffer.append(sameSite);
  131. }
  132. buffer.append("; Version=");
  133. buffer.append(QByteArray::number(version));
  134. return buffer;
  135. }
  136. void HttpCookie::setName(const QByteArray name)
  137. {
  138. this->name=name;
  139. }
  140. void HttpCookie::setValue(const QByteArray value)
  141. {
  142. this->value=value;
  143. }
  144. void HttpCookie::setComment(const QByteArray comment)
  145. {
  146. this->comment=comment;
  147. }
  148. void HttpCookie::setDomain(const QByteArray domain)
  149. {
  150. this->domain=domain;
  151. }
  152. void HttpCookie::setMaxAge(const int maxAge)
  153. {
  154. this->maxAge=maxAge;
  155. }
  156. void HttpCookie::setPath(const QByteArray path)
  157. {
  158. this->path=path;
  159. }
  160. void HttpCookie::setSecure(const bool secure)
  161. {
  162. this->secure=secure;
  163. }
  164. void HttpCookie::setHttpOnly(const bool httpOnly)
  165. {
  166. this->httpOnly=httpOnly;
  167. }
  168. void HttpCookie::setSameSite(const QByteArray sameSite)
  169. {
  170. this->sameSite=sameSite;
  171. }
  172. const QByteArray& HttpCookie::getName() const
  173. {
  174. return name;
  175. }
  176. const QByteArray& HttpCookie::getValue() const
  177. {
  178. return value;
  179. }
  180. const QByteArray& HttpCookie::getComment() const
  181. {
  182. return comment;
  183. }
  184. const QByteArray& HttpCookie::getDomain() const
  185. {
  186. return domain;
  187. }
  188. int HttpCookie::getMaxAge() const
  189. {
  190. return maxAge;
  191. }
  192. const QByteArray& HttpCookie::getPath() const
  193. {
  194. return path;
  195. }
  196. bool HttpCookie::getSecure() const
  197. {
  198. return secure;
  199. }
  200. bool HttpCookie::getHttpOnly() const
  201. {
  202. return httpOnly;
  203. }
  204. const QByteArray& HttpCookie::getSameSite() const
  205. {
  206. return sameSite;
  207. }
  208. int HttpCookie::getVersion() const
  209. {
  210. return version;
  211. }
  212. QList<QByteArray> HttpCookie::splitCSV(const QByteArray source)
  213. {
  214. bool inString=false;
  215. QList<QByteArray> list;
  216. QByteArray buffer;
  217. for (int i=0; i<source.size(); ++i)
  218. {
  219. char c=source.at(i);
  220. if (inString==false)
  221. {
  222. if (c=='\"')
  223. {
  224. inString=true;
  225. }
  226. else if (c==';')
  227. {
  228. QByteArray trimmed=buffer.trimmed();
  229. if (!trimmed.isEmpty())
  230. {
  231. list.append(trimmed);
  232. }
  233. buffer.clear();
  234. }
  235. else
  236. {
  237. buffer.append(c);
  238. }
  239. }
  240. else
  241. {
  242. if (c=='\"')
  243. {
  244. inString=false;
  245. }
  246. else {
  247. buffer.append(c);
  248. }
  249. }
  250. }
  251. QByteArray trimmed=buffer.trimmed();
  252. if (!trimmed.isEmpty())
  253. {
  254. list.append(trimmed);
  255. }
  256. return list;
  257. }