PageRenderTime 275ms CodeModel.GetById 140ms app.highlight 11ms RepoModel.GetById 120ms 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
  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
 29#ifndef LL_LLPLUGINCOOKIESTORE_H
 30#define LL_LLPLUGINCOOKIESTORE_H
 31
 32#include "lldate.h"
 33#include <map>
 34#include <string>
 35#include <iostream>
 36
 37class LLPluginCookieStore
 38{
 39	LOG_CLASS(LLPluginCookieStore);
 40public:
 41	LLPluginCookieStore();
 42	~LLPluginCookieStore();
 43
 44	// gets all cookies currently in storage -- use when initializing a plugin
 45	std::string getAllCookies();
 46	void writeAllCookies(std::ostream& s);
 47	
 48	// gets only persistent cookies (i.e. not session cookies) -- use when writing cookies to a file
 49	std::string getPersistentCookies();
 50	void writePersistentCookies(std::ostream& s);
 51	
 52	// gets cookies which are marked as "changed" -- use when sending periodic updates to plugins
 53	std::string getChangedCookies(bool clear_changed = true);
 54	void writeChangedCookies(std::ostream& s, bool clear_changed = true);
 55	
 56	// (re)initializes internal data structures and bulk-sets cookies -- use when reading cookies from a file
 57	void setAllCookies(const std::string &cookies, bool mark_changed = false);
 58	void readAllCookies(std::istream& s, bool mark_changed = false);
 59	
 60	// sets one or more cookies (without reinitializing anything) -- use when receiving cookies from a plugin
 61	void setCookies(const std::string &cookies, bool mark_changed = true);
 62	void readCookies(std::istream& s, bool mark_changed = true);
 63
 64	// sets one or more cookies (without reinitializing anything), supplying a hostname the cookies came from -- use when setting a cookie manually
 65	void setCookiesFromHost(const std::string &cookies, const std::string &host, bool mark_changed = true);
 66
 67	// quote or unquote a string as per the definition of 'quoted-string' in rfc2616
 68	static std::string quoteString(const std::string &s);
 69	static std::string unquoteString(const std::string &s);
 70	
 71private:
 72
 73	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);
 74
 75	class Cookie
 76	{
 77	public:
 78		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);
 79		
 80		// Construct a string from the cookie that uniquely represents it, to be used as a key in a std::map.
 81		std::string getKey() const;
 82		
 83		const std::string &getCookie() const { return mCookie; };
 84		bool isSessionCookie() const { return mDate.isNull(); };
 85
 86		bool isDead() const { return mDead; };
 87		void setDead(bool dead) { mDead = dead; };
 88		
 89		bool isChanged() const { return mChanged; };
 90		void setChanged(bool changed) { mChanged = changed; };
 91
 92		const LLDate &getDate() const { return mDate; };
 93		
 94	private:
 95		Cookie(const std::string &s, std::string::size_type cookie_start = 0, std::string::size_type cookie_end = std::string::npos);
 96		bool parse(const std::string &host);
 97		std::string::size_type findFieldEnd(std::string::size_type start = 0, std::string::size_type end = std::string::npos);
 98		bool matchName(std::string::size_type start, std::string::size_type end, const char *name);
 99		
100		std::string mCookie;	// The full cookie, in RFC 2109 string format
101		LLDate mDate;			// The expiration date of the cookie.  For session cookies, this will be a null date (mDate.isNull() is true).
102		// Start/end indices of various parts of the cookie string.  Stored as indices into the string to save space and time.
103		std::string::size_type mNameStart, mNameEnd;
104		std::string::size_type mValueStart, mValueEnd;
105		std::string::size_type mDomainStart, mDomainEnd;
106		std::string::size_type mPathStart, mPathEnd;
107		bool mDead;
108		bool mChanged;
109	};
110	
111	typedef std::map<std::string, Cookie*> cookie_map_t;
112	
113	cookie_map_t mCookies;
114	bool mHasChangedCookies;
115	
116	void clearCookies();
117	void removeCookie(const std::string &key);
118};
119
120#endif // LL_LLPLUGINCOOKIESTORE_H