httpcookie.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. @file
  3. @author Stefan Frings
  4. */
  5. #ifndef HTTPCOOKIE_H
  6. #define HTTPCOOKIE_H
  7. #include <QList>
  8. #include <QByteArray>
  9. #include "httpglobal.h"
  10. namespace stefanfrings {
  11. /**
  12. HTTP cookie as defined in RFC 2109.
  13. Supports some additional attributes of RFC6265bis.
  14. */
  15. class DECLSPEC HttpCookie
  16. {
  17. public:
  18. /** Creates an empty cookie */
  19. HttpCookie();
  20. /**
  21. Create a cookie and set name/value pair.
  22. @param name name of the cookie
  23. @param value value of the cookie
  24. @param maxAge maximum age of the cookie in seconds. 0=discard immediately
  25. @param path Path for that the cookie will be sent, default="/" which means the whole domain
  26. @param comment Optional comment, may be displayed by the web browser somewhere
  27. @param domain Optional domain for that the cookie will be sent. Defaults to the current domain
  28. @param secure If true, the cookie will be sent by the browser to the server only on secure connections
  29. @param httpOnly If true, the browser does not allow client-side scripts to access the cookie
  30. @param sameSite Declare if the cookie can only be read by the same site, which is a stronger
  31. restriction than the domain. Allowed values: "Lax" and "Strict".
  32. */
  33. HttpCookie(const QByteArray name, const QByteArray value, const int maxAge,
  34. const QByteArray path="/", const QByteArray comment=QByteArray(),
  35. const QByteArray domain=QByteArray(), const bool secure=false,
  36. const bool httpOnly=false, const QByteArray sameSite=QByteArray());
  37. /**
  38. Create a cookie from a string.
  39. @param source String as received in a HTTP Cookie2 header.
  40. */
  41. HttpCookie(const QByteArray source);
  42. /** Convert this cookie to a string that may be used in a Set-Cookie header. */
  43. QByteArray toByteArray() const ;
  44. /**
  45. Split a string list into parts, where each part is delimited by semicolon.
  46. Semicolons within double quotes are skipped. Double quotes are removed.
  47. */
  48. static QList<QByteArray> splitCSV(const QByteArray source);
  49. /** Set the name of this cookie */
  50. void setName(const QByteArray name);
  51. /** Set the value of this cookie */
  52. void setValue(const QByteArray value);
  53. /** Set the comment of this cookie */
  54. void setComment(const QByteArray comment);
  55. /** Set the domain of this cookie */
  56. void setDomain(const QByteArray domain);
  57. /** Set the maximum age of this cookie in seconds. 0=discard immediately */
  58. void setMaxAge(const int maxAge);
  59. /** Set the path for that the cookie will be sent, default="/" which means the whole domain */
  60. void setPath(const QByteArray path);
  61. /** Set secure mode, so that the cookie will be sent by the browser to the server only on secure connections */
  62. void setSecure(const bool secure);
  63. /** Set HTTP-only mode, so that the browser does not allow client-side scripts to access the cookie */
  64. void setHttpOnly(const bool httpOnly);
  65. /**
  66. * Set same-site mode, so that the browser does not allow other web sites to access the cookie.
  67. * Allowed values: "Lax" and "Strict".
  68. */
  69. void setSameSite(const QByteArray sameSite);
  70. /** Get the name of this cookie */
  71. const QByteArray& getName() const;
  72. /** Get the value of this cookie */
  73. const QByteArray& getValue() const;
  74. /** Get the comment of this cookie */
  75. const QByteArray& getComment() const;
  76. /** Get the domain of this cookie */
  77. const QByteArray& getDomain() const;
  78. /** Get the maximum age of this cookie in seconds. */
  79. int getMaxAge() const;
  80. /** Set the path of this cookie */
  81. const QByteArray& getPath() const;
  82. /** Get the secure flag of this cookie */
  83. bool getSecure() const;
  84. /** Get the HTTP-only flag of this cookie */
  85. bool getHttpOnly() const;
  86. /** Get the same-site flag of this cookie */
  87. const QByteArray& getSameSite() const;
  88. /** Returns always 1 */
  89. int getVersion() const;
  90. private:
  91. QByteArray name;
  92. QByteArray value;
  93. QByteArray comment;
  94. QByteArray domain;
  95. int maxAge;
  96. QByteArray path;
  97. bool secure;
  98. bool httpOnly;
  99. QByteArray sameSite;
  100. int version;
  101. };
  102. } // end of namespace
  103. #endif // HTTPCOOKIE_H