PageRenderTime 232ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llplugin/llplugincookiestore.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 120 lines | 60 code | 24 blank | 36 comment | 0 complexity | 7e4cf7a68aab15e7d4623d72f320d48e MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llplugincookiestore.h
  3. * @brief LLPluginCookieStore provides central storage for http cookies used by plugins
  4. *
  5. * @cond
  6. * $LicenseInfo:firstyear=2010&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. * @endcond
  27. */
  28. #ifndef LL_LLPLUGINCOOKIESTORE_H
  29. #define LL_LLPLUGINCOOKIESTORE_H
  30. #include "lldate.h"
  31. #include <map>
  32. #include <string>
  33. #include <iostream>
  34. class LLPluginCookieStore
  35. {
  36. LOG_CLASS(LLPluginCookieStore);
  37. public:
  38. LLPluginCookieStore();
  39. ~LLPluginCookieStore();
  40. // gets all cookies currently in storage -- use when initializing a plugin
  41. std::string getAllCookies();
  42. void writeAllCookies(std::ostream& s);
  43. // gets only persistent cookies (i.e. not session cookies) -- use when writing cookies to a file
  44. std::string getPersistentCookies();
  45. void writePersistentCookies(std::ostream& s);
  46. // gets cookies which are marked as "changed" -- use when sending periodic updates to plugins
  47. std::string getChangedCookies(bool clear_changed = true);
  48. void writeChangedCookies(std::ostream& s, bool clear_changed = true);
  49. // (re)initializes internal data structures and bulk-sets cookies -- use when reading cookies from a file
  50. void setAllCookies(const std::string &cookies, bool mark_changed = false);
  51. void readAllCookies(std::istream& s, bool mark_changed = false);
  52. // sets one or more cookies (without reinitializing anything) -- use when receiving cookies from a plugin
  53. void setCookies(const std::string &cookies, bool mark_changed = true);
  54. void readCookies(std::istream& s, bool mark_changed = true);
  55. // sets one or more cookies (without reinitializing anything), supplying a hostname the cookies came from -- use when setting a cookie manually
  56. void setCookiesFromHost(const std::string &cookies, const std::string &host, bool mark_changed = true);
  57. // quote or unquote a string as per the definition of 'quoted-string' in rfc2616
  58. static std::string quoteString(const std::string &s);
  59. static std::string unquoteString(const std::string &s);
  60. private:
  61. void setOneCookie(const std::string &s, std::string::size_type cookie_start, std::string::size_type cookie_end, bool mark_changed, const std::string &host = LLStringUtil::null);
  62. class Cookie
  63. {
  64. public:
  65. static Cookie *createFromString(const std::string &s, std::string::size_type cookie_start = 0, std::string::size_type cookie_end = std::string::npos, const std::string &host = LLStringUtil::null);
  66. // Construct a string from the cookie that uniquely represents it, to be used as a key in a std::map.
  67. std::string getKey() const;
  68. const std::string &getCookie() const { return mCookie; };
  69. bool isSessionCookie() const { return mDate.isNull(); };
  70. bool isDead() const { return mDead; };
  71. void setDead(bool dead) { mDead = dead; };
  72. bool isChanged() const { return mChanged; };
  73. void setChanged(bool changed) { mChanged = changed; };
  74. const LLDate &getDate() const { return mDate; };
  75. private:
  76. Cookie(const std::string &s, std::string::size_type cookie_start = 0, std::string::size_type cookie_end = std::string::npos);
  77. bool parse(const std::string &host);
  78. std::string::size_type findFieldEnd(std::string::size_type start = 0, std::string::size_type end = std::string::npos);
  79. bool matchName(std::string::size_type start, std::string::size_type end, const char *name);
  80. std::string mCookie; // The full cookie, in RFC 2109 string format
  81. LLDate mDate; // The expiration date of the cookie. For session cookies, this will be a null date (mDate.isNull() is true).
  82. // Start/end indices of various parts of the cookie string. Stored as indices into the string to save space and time.
  83. std::string::size_type mNameStart, mNameEnd;
  84. std::string::size_type mValueStart, mValueEnd;
  85. std::string::size_type mDomainStart, mDomainEnd;
  86. std::string::size_type mPathStart, mPathEnd;
  87. bool mDead;
  88. bool mChanged;
  89. };
  90. typedef std::map<std::string, Cookie*> cookie_map_t;
  91. cookie_map_t mCookies;
  92. bool mHasChangedCookies;
  93. void clearCookies();
  94. void removeCookie(const std::string &key);
  95. };
  96. #endif // LL_LLPLUGINCOOKIESTORE_H