summaryrefslogtreecommitdiff
path: root/project2/json/couchSession.cpp
blob: ee7bad28f9ac026e338356ed1d8f18e85364c3f3 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <pch.hpp>
#include "curlHelper.h"
#include "safeMapFind.h"
#include "exceptions.h"
#include "../libmisc/buffer.h"
#include "../libmisc/curlsup.h"
#include <scriptLoader.h>
#include <sessionContainer.h>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include "jsonpp.h"
#include "safeMapFind.h"
#include "conversion.h"
#include "options.h"

class CouchDBFailure : public std::exception { };

class CouchSessionContainer : public SessionContainer {
	public:
		CouchSessionContainer() {
		}
		virtual SessionPtr getSession(const boost::uuids::uuid & uuid) const {
			try {
				json::Object obj = getSessionFromServer(uuid);
				if (boost::get<json::Number>(*safeMapLookup<Session::VariableNotFound>(obj, ExpiryKey)) > time(NULL)) {
					SessionPtr s = new Session(uuid);
					BOOST_FOREACH(const json::Object::value_type & v, obj) {
						s->SetValue(v.first, boost::apply_visitor(JsonToProject2(), *v.second));
					}
					return s;
				}
			}
			catch (...) {
			}
			return NULL;
		}

		virtual void SaveSession(SessionPtr s) const {
			CurlPtr c = new Curl();
			c->setopt(CURLOPT_UPLOAD, 1L);
			c->setopt(CURLOPT_FAILONERROR, 1);
			json::Object obj;
			s->ForeachValue(boost::bind(&CouchSessionContainer::addToObject, &obj, _1, _2));
			obj[ExpiryKey] = json::ValuePtr(new json::Value((json::Number)s->ExpiryTime()));
			Glib::ustring out = json::serializeObject(obj, "utf-8");
			c->setopt(CURLOPT_INFILESIZE_LARGE, (curl_off_t)out.size());
			unsigned int off = 0;
			BOOST_FOREACH(const std::string & b, baseUrls) {
				c->setopt(CURLOPT_URL, (b + boost::lexical_cast<std::string>(s->ID())).c_str());
				try {
					c->performSend(boost::bind(send, &out, &off, _1, _2));
					return;
				}
				catch (...) {
				}
			}
			throw CouchDBFailure();
		}

		json::Object getSessionFromServer(const boost::uuids::uuid & uuid) const {
			CurlPtr c = new Curl();
			c->setopt(CURLOPT_FAILONERROR, 1);
			Glib::ustring msg;
			BOOST_FOREACH(const std::string & b, baseUrls) {
				try {
					c->setopt(CURLOPT_URL, (b + boost::lexical_cast<std::string>(uuid)).c_str());
					c->performRead(boost::bind(append, &msg, _1, _2));
					json::Object o = json::parseObject(msg);
					return o;
				}
				catch (...) {
				}
			}
			throw CouchDBFailure();
		}

	private:
		static void addToObject(json::Object * obj, const Glib::ustring & name, const VariableType & value) {
			(*obj)[name] = json::ValuePtr(new json::Value(boost::apply_visitor(Project2ToJson(), value)));
		}

		static size_t send(Glib::ustring * buf, unsigned int * off, char * str, size_t l) {
			size_t len = std::min(buf->size() - *off, l);
			memcpy(str, buf->c_str() + *off, len);
			return len;
		}

		static size_t append(Glib::ustring * buf, const char * str, size_t l) {
			buf->append(str, l);
			return l;
		}

		void setopt_s(CurlHandle::Ptr c, CURLoption o, const char * v) {
			c->setopt(o, v);
		}

		void setopt_l(CurlHandle::Ptr c, CURLoption o, int64_t v) {
			c->setopt(o, (long)v);
		}

		static std::vector<std::string> baseUrls;
		static const Glib::ustring ExpiryKey;
		friend class CustomCouchSessionLoader;
};
std::vector<std::string> CouchSessionContainer::baseUrls;
const Glib::ustring CouchSessionContainer::ExpiryKey("project2:expires");

class CustomCouchSessionLoader : public SessionContainerLoader::For<CouchSessionContainer> {
	public:
		void onPeriodic() {
			deleteSessions();
			compactDB();
		}

		INITOPTIONS;
	private:
		static size_t discard(size_t l) {
			return l;
		}

		void compactDB() {
			CurlPtr c = new Curl();
			c->setopt(CURLOPT_POST, 1);
			c->appendHeader("Content-Type: application/json");
			BOOST_FOREACH(const std::string & b, CouchSessionContainer::baseUrls) {
				c->setopt(CURLOPT_URL, (b + "_compact").c_str());
				c->performRead(boost::bind(discard, _2));
			}
		}
		void deleteSessions() {
			// Create the server side search map
			json::Object map;
			Buffer mapBuf;
			mapBuf.appendf("function(doc) { var exp = doc['%s']; if (exp < %u) { emit(exp, doc._rev); } }",
					CouchSessionContainer::ExpiryKey.c_str(), (unsigned int)time(NULL));
			map["map"] = json::ValuePtr(new json::Value(mapBuf.str()));
			Glib::ustring mapStr(json::serializeObject(map, "utf-8"));
			// Create the CURL handle
			CurlPtr c = new Curl();
			c->setopt(CURLOPT_FAILONERROR, 1);
			c->appendHeader("Content-Type: application/json");
			c->setopt(CURLOPT_POST, 1);
			c->setopt(CURLOPT_POSTFIELDS, mapStr.c_str());
			c->setopt(CURLOPT_POSTFIELDSIZE, mapStr.bytes());
			BOOST_FOREACH(const std::string & b, CouchSessionContainer::baseUrls) {
				Glib::ustring msg;
				try {
					c->setopt(CURLOPT_URL, (b + "_temp_view").c_str());
					c->performRead(boost::bind(CouchSessionContainer::append, &msg, _1, _2));
					json::Object o = json::parseObject(msg);
					BOOST_FOREACH(const json::Array::value_type & v, boost::get<json::Array>(*safeMapLookup<ParamNotFound>(o, "rows"))) {
						json::Object rec = boost::get<json::Object>(*v);
						boost::uuids::uuid u = boost::uuids::string_generator()(boost::get<json::String>(*safeMapLookup<ParamNotFound>(rec, "id")).raw());
						Glib::ustring & rev = boost::get<json::String>(*safeMapLookup<ParamNotFound>(rec, "value"));
						deleteSession(u, rev);
					}
					return;
				}
				catch (...) {
				}
			}
		}

		void deleteSession(const boost::uuids::uuid & sid, const Glib::ustring & rev) const {
			CurlPtr c = new Curl();
			c->setopt(CURLOPT_CUSTOMREQUEST, "DELETE");
			BOOST_FOREACH(const std::string & b, CouchSessionContainer::baseUrls) {
				c->setopt(CURLOPT_URL, (b + boost::lexical_cast<std::string>(sid) + "?rev=" + rev).c_str());
				c->performRead(boost::bind(discard, _2));
				return;
			}
		}
};
DECLARE_CUSTOM_COMPONENT_LOADER("couchsession", CouchSessionContainer, CustomCouchSessionLoader, SessionContainerLoader);

DECLARE_OPTIONS(CustomCouchSessionLoader, "Session CouchDB options")
("session.couchdb.baseUrl", Options::functions([](const VariableType & v) { CouchSessionContainer::baseUrls.push_back(v); }, boost::bind(&std::vector<std::string>::clear, &CouchSessionContainer::baseUrls)),
 "Base URL to store sessions in")
END_OPTIONS(CustomCouchSessionLoader);