example07.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * \file
  3. * Cookies.
  4. *
  5. */
  6. #include <string>
  7. #include <sstream>
  8. #include <vector>
  9. #include <iostream>
  10. #include <cstdlib>
  11. #include <ctime>
  12. #include <curlpp/cURLpp.hpp>
  13. #include <curlpp/Easy.hpp>
  14. #define CURLPP_ALLOW_NOT_AVAILABLE
  15. #include <curlpp/Infos.hpp>
  16. #include <curlpp/Options.hpp>
  17. class YesNo
  18. {
  19. public:
  20. explicit YesNo(bool yn) : yesno(yn) {}
  21. std::string operator()() const {
  22. return yesno ? "Yes" : "No";
  23. }
  24. friend std::ostream &operator<<(std::ostream &strm, const YesNo &yn) {
  25. strm << yn();
  26. return strm;
  27. }
  28. private:
  29. bool yesno;
  30. };
  31. struct MyCookie
  32. {
  33. std::string name;
  34. std::string value;
  35. std::string domain;
  36. std::string path;
  37. time_t expires;
  38. bool tail;
  39. bool secure;
  40. };
  41. std::ostream &
  42. operator<<(std::ostream &strm, const MyCookie &cook)
  43. {
  44. strm << "Cookie: '" << cook.name << "' (secure: " << YesNo(cook.secure) << ", tail: "
  45. << YesNo(cook.tail) << ") for domain: '" << cook.domain << "', "
  46. << "path: '" << cook.path << "'.\n";
  47. strm << "Value: '" << cook.value << "'.\n";
  48. strm << "Expires: '" << ctime(&cook.expires) << "'.\n";
  49. return strm;
  50. }
  51. std::vector<std::string> &
  52. split_cookie_str(const std::string &str, std::vector<std::string> &in)
  53. {
  54. std::string part;
  55. std::istringstream strm(str);
  56. while (getline(strm, part, '\t'))
  57. in.push_back(part);
  58. return in;
  59. }
  60. std::vector<std::string>
  61. splitCookieStr(const std::string &str)
  62. {
  63. std::vector<std::string> split;
  64. split_cookie_str(str, split);
  65. return split;
  66. }
  67. std::vector<std::string> &
  68. splitCookieStr(const std::string &str, std::vector<std::string> &in)
  69. {
  70. return split_cookie_str(str, in);
  71. }
  72. int StrToInt(const std::string &str)
  73. {
  74. std::istringstream strm(str);
  75. int i = 0;
  76. if (!(strm >> i)) {
  77. throw curlpp::RuntimeError("Unable to convert string '" + str + "' to integer!");
  78. }
  79. return i;
  80. }
  81. MyCookie
  82. MakeCookie(const std::string &str_cookie)
  83. {
  84. std::vector<std::string> vC = splitCookieStr(str_cookie);
  85. MyCookie cook;
  86. cook.domain = vC[0];
  87. cook.tail = vC[1] == "TRUE";
  88. cook.path = vC[2];
  89. cook.secure = vC[3] == "TRUE";
  90. cook.expires = StrToInt(vC[4]);
  91. cook.name = vC[5];
  92. cook.value = vC[6];
  93. return cook;
  94. }
  95. int
  96. main(void)
  97. {
  98. try
  99. {
  100. curlpp::Cleanup myCleanup;
  101. curlpp::Easy exEasy;
  102. std::vector<std::string> cookieList;
  103. // a cookie as in HTTP header
  104. cookieList.push_back("Set-Cookie: GMAIL_AT=EXPIRED;expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");
  105. // a Netscape style cookie with \t
  106. cookieList.push_back(".google.com\tTRUE\t/\tFALSE\t2147483647\tLSID\tI like you GOOGLE");
  107. // a Netscape style cookie with tabs in string
  108. cookieList.push_back(".yahoo.com TRUE / FALSE 0 YAHOO_COOKIE I like you yahoo, too");
  109. exEasy.setOpt(new curlpp::options::Url("http://www.google.com"));
  110. exEasy.setOpt(new curlpp::options::FileTime(true));
  111. exEasy.setOpt(new curlpp::options::Verbose(true));
  112. // loop throught the cookies and add one by one
  113. //
  114. for (std::vector<std::string>::iterator it = cookieList.begin();
  115. it != cookieList.end();
  116. ++it)
  117. {
  118. exEasy.setOpt(curlpp::options::CookieList(*it));
  119. }
  120. exEasy.perform();
  121. // see what cookies we got
  122. //
  123. std::cout << "\nCookies from cookie engine:" << std::endl;
  124. std::list<std::string> cookies;
  125. curlpp::infos::CookieList::get(exEasy, cookies);
  126. int i = 1;
  127. for (std::list<std::string>::const_iterator it = cookies.begin();
  128. it != cookies.end();
  129. ++it, i++)
  130. {
  131. std::cout << "[" << i << "]: " << MakeCookie(*it) << std::endl;
  132. }
  133. exit(EXIT_SUCCESS);
  134. }
  135. catch(curlpp::RuntimeError &e)
  136. {
  137. std::cerr << e.what() << std::endl;
  138. exit(EXIT_FAILURE);
  139. }
  140. catch(curlpp::LogicError &e)
  141. {
  142. std::cout << e.what() << std::endl;
  143. exit(EXIT_FAILURE);
  144. }
  145. }