HttpCookie.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "network/HttpCookie.h"
  22. #include "platform/CCFileUtils.h"
  23. #include <sstream>
  24. #include <stdio.h>
  25. void HttpCookie::readFile()
  26. {
  27. std::string inString = cocos2d::FileUtils::getInstance()->getStringFromFile(_cookieFileName);
  28. if(!inString.empty())
  29. {
  30. std::vector<std::string> cookiesVec;
  31. cookiesVec.clear();
  32. std::stringstream stream(inString);
  33. std::string item;
  34. while(std::getline(stream, item, '\n'))
  35. {
  36. cookiesVec.push_back(item);
  37. }
  38. if(cookiesVec.empty())
  39. return;
  40. _cookies.clear();
  41. for(auto& cookie : cookiesVec)
  42. {
  43. if(cookie.length() == 0)
  44. continue;
  45. if(cookie.find("#HttpOnly_") != std::string::npos)
  46. {
  47. cookie = cookie.substr(10);
  48. }
  49. if(cookie.at(0) == '#')
  50. continue;
  51. CookiesInfo co;
  52. std::stringstream streamInfo(cookie);
  53. std::vector<std::string> elems;
  54. std::string elemsItem;
  55. while (std::getline(streamInfo, elemsItem, '\t'))
  56. {
  57. elems.push_back(elemsItem);
  58. }
  59. co.domain = elems[0];
  60. if (co.domain.at(0) == '.')
  61. {
  62. co.domain = co.domain.substr(1);
  63. }
  64. co.tailmatch = strcmp("TRUE", elems[1].c_str())?true: false;
  65. co.path = elems[2];
  66. co.secure = strcmp("TRUE", elems[3].c_str())?true: false;
  67. co.expires = elems[4];
  68. co.name = elems[5];
  69. co.value = elems[6];
  70. _cookies.push_back(co);
  71. }
  72. }
  73. }
  74. const std::vector<CookiesInfo>* HttpCookie::getCookies() const
  75. {
  76. return &_cookies;
  77. }
  78. const CookiesInfo* HttpCookie::getMatchCookie(const std::string& url) const
  79. {
  80. for(auto& cookie : _cookies)
  81. {
  82. if(url.find(cookie.domain) != std::string::npos)
  83. return &cookie;
  84. }
  85. return nullptr;
  86. }
  87. void HttpCookie::updateOrAddCookie(CookiesInfo* cookie)
  88. {
  89. for(auto& _cookie : _cookies)
  90. {
  91. if(cookie->domain == _cookie.domain)
  92. {
  93. _cookie = *cookie;
  94. return;
  95. }
  96. }
  97. _cookies.push_back(*cookie);
  98. }
  99. void HttpCookie::writeFile()
  100. {
  101. FILE *out;
  102. out = fopen(_cookieFileName.c_str(), "w");
  103. fputs("# Netscape HTTP Cookie File\n"
  104. "# http://curl.haxx.se/docs/http-cookies.html\n"
  105. "# This file was generated by cocos2d-x! Edit at your own risk.\n"
  106. "# Test cocos2d-x cookie write.\n\n",
  107. out);
  108. std::string line;
  109. for(auto& cookie : _cookies)
  110. {
  111. line.clear();
  112. line.append(cookie.domain);
  113. line.append(1, '\t');
  114. cookie.tailmatch ? line.append("TRUE") : line.append("FALSE");
  115. line.append(1, '\t');
  116. line.append(cookie.path);
  117. line.append(1, '\t');
  118. cookie.secure ? line.append("TRUE") : line.append("FALSE");
  119. line.append(1, '\t');
  120. line.append(cookie.expires);
  121. line.append(1, '\t');
  122. line.append(cookie.name);
  123. line.append(1, '\t');
  124. line.append(cookie.value);
  125. //line.append(1, '\n');
  126. fprintf(out, "%s\n", line.c_str());
  127. }
  128. fclose(out);
  129. }
  130. void HttpCookie::setCookieFileName(const std::string& filename)
  131. {
  132. _cookieFileName = filename;
  133. }