1 
2 /* Web Polygraph       http://www.web-polygraph.org/
3  * Copyright 2003-2011 The Measurement Factory
4  * Licensed under the Apache License, Version 2.0 */
5 
6 #ifndef POLYGRAPH__RUNTIME_HTTPCOOKIES_H
7 #define POLYGRAPH__RUNTIME_HTTPCOOKIES_H
8 
9 #include "xstd/String.h"
10 #include "runtime/Farm.h"
11 
12 #include <list>
13 #include <map>
14 
15 // manages a single HTTP Cookie
16 class HttpCookie {
17 	public:
name()18 		const String &name() const { return theName; }
data()19 		const String &data() const { return theData; }
20 
21 		void reset();
22 		void set(const String &aData, const int nameEnd);
23 		bool expired() const;
24 		bool discardable() const;
25 
26 		static void Configure();
27 		// cookies are destroyed with Put()
28 		static void Put(HttpCookie *const cookie);
29 		// cookies are created with Parse()
30 		static HttpCookie *Parse(const char *buf, const char *eoh);
31 
32 	private:
33 		static bool ParseParameter(const char *const buf, const char *const end, HttpCookie &cookie);
34 
35 		// protected to force Farm use
36 		HttpCookie();
~HttpCookie()37 		~HttpCookie() {};
38 
39 		HttpCookie(const HttpCookie &); // undefined
40 		HttpCookie &operator =(const HttpCookie &); // undefined
41 
discard()42 		void discard() { isDiscardable = true; }
expires(const Time & data)43 		void expires(const Time &data) { theExpires = data; }
44 		void maxAge(const int delta);
45 
46 		String theName;
47 		String theData;
48 		Time theExpires;
49 		bool isDiscardable;
50 
51 		friend class ObjFarm<HttpCookie>;
52 		static ObjFarm<HttpCookie> TheCookieFarm;
53 };
54 
55 // server cookies storage
56 class HttpCookies {
57 	public:
58 		HttpCookies();
59 		~HttpCookies();
60 
61 		void keepLimit(const unsigned limit);
62 
63 		void add(HttpCookie *const cookie);
64 
65 		int count() const;
66 
67 		// first/next fresh cookie in undefined order
68 		const HttpCookie *first();
69 		const HttpCookie *next();
70 
71 		void purgeDiscardable();
72 
73 		static Counter TheTotalCount;
74 		static Counter ThePurgedFreshCount;
75 		static Counter ThePurgedStaleCount;
76 		static Counter TheUpdatedCount;
77 
78 	private:
79 		typedef std::list<HttpCookie*> Cache;
80 
81 		void purgeAt(const Cache::iterator &i, const bool purgeName = true, const bool doReport = true);
82 		const HttpCookie *sync();
83 
84 		void reportCached(const HttpCookie &cookie) const;
85 		void reportUpdated(const HttpCookie &cookie) const;
86 		void reportPurged(const HttpCookie &cookie) const;
87 		void printMessage(const char *const message, const HttpCookie &cookie) const;
88 
89 		unsigned theLimit; // maximum number of cookies to store
90 		Cache theCookies;
91 		Cache::iterator theIter; // current cookie pointer, used for getting cookies
92 		typedef std::map<String, Cache::iterator> NameMap;
93 		NameMap theNameMap; // name:cookie in cache
94 };
95 
96 #endif
97