aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llplugin/llplugincookiestore.h
blob: 69f0cf130405e8e429f5256aa430350d8fa1a7f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/** 
 * @file llplugincookiestore.h
 * @brief LLPluginCookieStore provides central storage for http cookies used by plugins
 *
 * @cond
 * $LicenseInfo:firstyear=2010&license=viewergpl$
 * 
 * Copyright (c) 2010, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 * 
 * @endcond
 */

#ifndef LL_LLPLUGINCOOKIESTORE_H
#define LL_LLPLUGINCOOKIESTORE_H

#include "lldate.h"
#include <map>
#include <string>
#include <iostream>

class LLPluginCookieStore
{
	LOG_CLASS(LLPluginCookieStore);
public:
	LLPluginCookieStore();
	~LLPluginCookieStore();

	// gets all cookies currently in storage -- use when initializing a plugin
	std::string getAllCookies();
	void writeAllCookies(std::ostream& s);
	
	// gets only persistent cookies (i.e. not session cookies) -- use when writing cookies to a file
	std::string getPersistentCookies();
	void writePersistentCookies(std::ostream& s);
	
	// gets cookies which are marked as "changed" -- use when sending periodic updates to plugins
	std::string getChangedCookies(bool clear_changed = true);
	void writeChangedCookies(std::ostream& s, bool clear_changed = true);
	
	// (re)initializes internal data structures and bulk-sets cookies -- use when reading cookies from a file
	void setAllCookies(const std::string &cookies, bool mark_changed = false);
	void readAllCookies(std::istream& s, bool mark_changed = false);
	
	// sets one or more cookies (without reinitializing anything) -- use when receiving cookies from a plugin
	void setCookies(const std::string &cookies, bool mark_changed = true);
	void readCookies(std::istream& s, bool mark_changed = true);

	// sets one or more cookies (without reinitializing anything), supplying a hostname the cookies came from -- use when setting a cookie manually
	void setCookiesFromHost(const std::string &cookies, const std::string &host, bool mark_changed = true);

	// quote or unquote a string as per the definition of 'quoted-string' in rfc2616
	static std::string quoteString(const std::string &s);
	static std::string unquoteString(const std::string &s);
	
private:

	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);

	class Cookie
	{
	public:
		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);
		
		// Construct a string from the cookie that uniquely represents it, to be used as a key in a std::map.
		std::string getKey() const;
		
		const std::string &getCookie() const { return mCookie; };
		bool isSessionCookie() const { return mDate.isNull(); };

		bool isDead() const { return mDead; };
		void setDead(bool dead) { mDead = dead; };
		
		bool isChanged() const { return mChanged; };
		void setChanged(bool changed) { mChanged = changed; };

		const LLDate &getDate() const { return mDate; };
		
	private:
		Cookie(const std::string &s, std::string::size_type cookie_start = 0, std::string::size_type cookie_end = std::string::npos);
		bool parse(const std::string &host);
		std::string::size_type findFieldEnd(std::string::size_type start = 0, std::string::size_type end = std::string::npos);
		bool matchName(std::string::size_type start, std::string::size_type end, const char *name);
		
		std::string mCookie;	// The full cookie, in RFC 2109 string format
		LLDate mDate;			// The expiration date of the cookie.  For session cookies, this will be a null date (mDate.isNull() is true).
		// Start/end indices of various parts of the cookie string.  Stored as indices into the string to save space and time.
		std::string::size_type mNameStart, mNameEnd;
		std::string::size_type mValueStart, mValueEnd;
		std::string::size_type mDomainStart, mDomainEnd;
		std::string::size_type mPathStart, mPathEnd;
		bool mDead;
		bool mChanged;
	};
	
	typedef std::map<std::string, Cookie*> cookie_map_t;
	
	cookie_map_t mCookies;
	bool mHasChangedCookies;
	
	void clearCookies();
	void removeCookie(const std::string &key);
};

#endif // LL_LLPLUGINCOOKIESTORE_H