cookies.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef CPR_COOKIES_H
  2. #define CPR_COOKIES_H
  3. #include "cpr/curlholder.h"
  4. #include <chrono>
  5. #include <initializer_list>
  6. #include <sstream>
  7. #include <string>
  8. #include <vector>
  9. namespace cpr {
  10. /**
  11. * EXPIRES_STRING_SIZE is an explicitly static and const variable that could be only accessed within the same namespace and is immutable.
  12. * To be used for "std::array", the expression must have a constant value, so EXPIRES_STRING_SIZE must be a const value.
  13. **/
  14. static const std::size_t EXPIRES_STRING_SIZE = 100;
  15. class Cookie {
  16. public:
  17. Cookie() = default;
  18. /**
  19. * Some notes for the default value used by expires:
  20. * std::chrono::system_clock::time_point::min() won't work on Windows due to the min, max clash there.
  21. * So we fall back to std::chrono::system_clock::from_time_t(0) for the minimum value here.
  22. **/
  23. Cookie(const std::string& name, const std::string& value, const std::string& domain = "", bool p_isIncludingSubdomains = false, const std::string& path = "/", bool p_isHttpsOnly = false, std::chrono::system_clock::time_point expires = std::chrono::system_clock::from_time_t(0)) : name_{name}, value_{value}, domain_{domain}, includeSubdomains_{p_isIncludingSubdomains}, path_{path}, httpsOnly_{p_isHttpsOnly}, expires_{expires} {};
  24. const std::string GetDomain() const;
  25. bool IsIncludingSubdomains() const;
  26. const std::string GetPath() const;
  27. bool IsHttpsOnly() const;
  28. const std::chrono::system_clock::time_point GetExpires() const;
  29. const std::string GetExpiresString() const;
  30. const std::string GetName() const;
  31. const std::string GetValue() const;
  32. private:
  33. std::string name_;
  34. std::string value_;
  35. std::string domain_;
  36. bool includeSubdomains_{};
  37. std::string path_;
  38. bool httpsOnly_{};
  39. /**
  40. * TODO: Update the implementation using `std::chrono::utc_clock` of C++20
  41. **/
  42. std::chrono::system_clock::time_point expires_{};
  43. };
  44. class Cookies {
  45. public:
  46. /**
  47. * Should we URL-encode cookies when making a request.
  48. * Based on RFC6265, it is recommended but not mandatory to encode cookies.
  49. *
  50. * -------
  51. * To maximize compatibility with user agents, servers that wish to
  52. * store arbitrary data in a cookie-value SHOULD encode that data, for
  53. * example, using Base64 [RFC4648].
  54. * -------
  55. * Source: RFC6265 (https://www.ietf.org/rfc/rfc6265.txt)
  56. **/
  57. bool encode{true};
  58. // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
  59. Cookies(bool p_encode = true) : encode{p_encode} {};
  60. Cookies(const std::initializer_list<cpr::Cookie>& cookies, bool p_encode = true) : encode{p_encode}, cookies_{cookies} {};
  61. // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
  62. Cookies(const cpr::Cookie& cookie, bool p_encode = true) : encode{p_encode}, cookies_{cookie} {};
  63. cpr::Cookie& operator[](size_t pos);
  64. const std::string GetEncoded(const CurlHolder& holder) const;
  65. using iterator = std::vector<cpr::Cookie>::iterator;
  66. using const_iterator = std::vector<cpr::Cookie>::const_iterator;
  67. iterator begin();
  68. iterator end();
  69. const_iterator begin() const;
  70. const_iterator end() const;
  71. const_iterator cbegin() const;
  72. const_iterator cend() const;
  73. void emplace_back(const Cookie& str);
  74. void push_back(const Cookie& str);
  75. void pop_back();
  76. private:
  77. std::vector<cpr::Cookie> cookies_;
  78. };
  79. } // namespace cpr
  80. #endif