-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpb.cpp
More file actions
161 lines (128 loc) · 3.65 KB
/
Copy pathpb.cpp
File metadata and controls
161 lines (128 loc) · 3.65 KB
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
//
// Mountain CMS
// Daniel A. White
//
// pb.cpp
// Builds the pages and caches them
//
#include "pb.h"
void mtn_cms_page_builder::getpage( mtn_cms_http_response_data* response,
mtn_cms_http_request_data* request,
CppSQLite3DB* db,
std::string* content)
{
_response = response;
_request = request;
_db = db;
mtn_cms_cache_item cache;
if (getcache(request->page, &cache))
{
time_t t = time(NULL);
if ( t <= (cache.ttl + cache.created) )
{
(*content) = cache.data;
response->status = MTN_CMS_HTTP_STATUS_OK;
response->length = cache.data.length();
response->content_type = MTN_CMS_MIME_TEXT_HTML;
return;
}
}
if ( buildpage(request->page, *content))
{
response->status = MTN_CMS_HTTP_STATUS_OK;
response->length = content->length();
response->content_type = MTN_CMS_MIME_TEXT_HTML;
return;
}
response->status = MTN_CMS_HTTP_STATUS_NOT_FOUND;
return;
}
std::string mtn_cms_page_builder::datestamp(const time_t& time, int ttl)
{
std::ostringstream ret;
ret << "<h1>Page last built at " << mtn_cms_http_time_to_gmt_string(time);
ret << " with a T.T.L. of " << ttl << " second(s) </h1>";
return ret.str();
}
bool mtn_cms_page_builder::getcache(const std::string& page, mtn_cms_cache_item* cache_item)
{
std::string _q = "SELECT * FROM `page_cache` WHERE page = \'" + page + "\';";
try
{
CppSQLite3Query q = _db->execQuery(_q.c_str());
while (!q.eof())
{
int len;
cache_item->data = q.getStringField("data");
cache_item->created = q.getIntField("created");
cache_item->ttl = q.getIntField("ttl");
cache_item->valid = true;
q.nextRow();
}
}
catch (CppSQLite3Exception ex)
{
cache_item->valid = false;
}
return cache_item->valid;
}
void mtn_cms_page_builder::storecache(const std::string& page, mtn_cms_cache_item* cache_item)
{
try
{
perror(cache_item->data.c_str());
//Mutex here
std::string q = "DELETE FROM `page_cache` WHERE page = \'" + page + "\';";
_db->execScalar(q.c_str());
}
catch (CppSQLite3Exception ex)
{
}
// try
// {
std::ostringstream q;
q << "INSERT INTO `page_cache` (`page`, `data`, `created`, `ttl`)";
q << " VALUES (\'" << page << "\', \'";
q << cache_item->data << "\', " << cache_item->created;
q << ", " << cache_item->ttl << ");";
_db->execScalar(q.str().c_str());
//Release here
// }
// catch (CppSQLite3Exception ex)
// {
// }
}
bool mtn_cms_page_builder::buildpage(std::string page, std::string& data)
{
try
{
mtn_cms_cache_item cache;
std::ostringstream s;
std::string head, nav, content, stamp, foot;
time_t now = time(NULL);
std::string _q;
_q = "SELECT * FROM `pages` WHERE page = \'" + page + "\';";
if ( _db->execQuery(_q.c_str()).eof() )
return false;
content = _db->execQuery(_q.c_str()).getStringField("data");
_q = "SELECT * FROM `pages` WHERE page = \'Navigation\';";
nav = _db->execQuery(_q.c_str()).getStringField("data");
_q = "SELECT * FROM `pages` WHERE page = \'Header\';";
head = _db->execQuery(_q.c_str()).getStringField("data");
_q = "SELECT * FROM `pages` WHERE page = \'Footer\';";
foot = _db->execQuery(_q.c_str()).getStringField("data");
// stamp = datestamp(now, 60);
// perror(stamp);
s << head << nav << content << datestamp(now, 60) << foot;
cache.data = s.str();
cache.ttl = 60;
cache.created = now;
data = s.str();
storecache(page, &cache);
return true;
}
catch (CppSQLite3Exception ex)
{
}
return false;
}