1 |
Douglas Thrift |
191 |
// Feeping Creaturism |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
Douglas Thrift |
194 |
#include "Environment.hpp" |
8 |
douglas |
336 |
#include "Matcher/Matcher.hpp" |
9 |
Douglas Thrift |
196 |
#include "Jargon.hpp" |
10 |
Douglas Thrift |
191 |
|
11 |
Douglas Thrift |
261 |
#include <menes-api/exename.hpp> |
12 |
|
|
#include <menes-app/simple.hpp> |
13 |
|
|
|
14 |
|
|
int Main(const app::Options& options) |
15 |
Douglas Thrift |
191 |
{ |
16 |
Douglas Thrift |
261 |
FeepingCreaturism::program = api::GetExecutablePath().GetName(); |
17 |
Douglas Thrift |
201 |
|
18 |
Douglas Thrift |
251 |
FeepingCreaturism creaturism; |
19 |
Douglas Thrift |
191 |
|
20 |
Douglas Thrift |
251 |
return 0; |
21 |
|
|
} |
22 |
Douglas Thrift |
194 |
|
23 |
|
|
FeepingCreaturism::FeepingCreaturism() |
24 |
|
|
{ |
25 |
|
|
initialize(); |
26 |
Douglas Thrift |
203 |
parse(); |
27 |
Douglas Thrift |
194 |
|
28 |
Douglas Thrift |
203 |
ext::String path(env.get("PATH_INFO")); |
29 |
Douglas Thrift |
194 |
Matcher matcher; |
30 |
|
|
|
31 |
Douglas Thrift |
211 |
if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$")) |
32 |
Douglas Thrift |
194 |
{ |
33 |
Douglas Thrift |
211 |
daily(matcher.size() > 1 ? matcher[1] : ""); |
34 |
Douglas Thrift |
194 |
} |
35 |
Douglas Thrift |
211 |
else if (path == matcher("^/random/(\\d+)?$")) |
36 |
|
|
{ |
37 |
|
|
random(matcher.size() > 1 ? matcher[1] : ""); |
38 |
|
|
} |
39 |
Douglas Thrift |
194 |
else if (path == matcher("^/(" + this->matcher + ")$")) |
40 |
|
|
{ |
41 |
|
|
select(matcher[1], true); |
42 |
|
|
} |
43 |
Douglas Thrift |
198 |
else |
44 |
|
|
{ |
45 |
douglas |
307 |
api::Cout << "Location: http://" << env.get("HTTP_HOST") << env.get("SCRIPT_NAME") << "/daily/\r\n\r\n"; |
46 |
Douglas Thrift |
198 |
} |
47 |
Douglas Thrift |
194 |
} |
48 |
|
|
|
49 |
Douglas Thrift |
203 |
ext::String FeepingCreaturism::program; |
50 |
Douglas Thrift |
201 |
|
51 |
Douglas Thrift |
206 |
bool FeepingCreaturism::CaseLess::operator()(const std::string& one, |
52 |
|
|
const std::string& two) |
53 |
|
|
{ |
54 |
|
|
std::string one_(one), two_(two); |
55 |
|
|
|
56 |
|
|
// XXX: should be std::tolower except g++34 doesn't believe it |
57 |
|
|
std::transform(one.begin(), one.end(), one_.begin(), ::tolower); |
58 |
|
|
std::transform(two.begin(), two.end(), two_.begin(), ::tolower); |
59 |
|
|
|
60 |
Douglas Thrift |
255 |
if (one_ == two_) return one < two; |
61 |
|
|
|
62 |
Douglas Thrift |
206 |
return one_ < two_; |
63 |
|
|
} |
64 |
|
|
|
65 |
Douglas Thrift |
194 |
void FeepingCreaturism::initialize() |
66 |
|
|
{ |
67 |
Douglas Thrift |
265 |
_H<xml::Document> document(xml::Parse("jargon.xml")); |
68 |
|
|
_H<xml::Node> node(*document/"feepingcreaturism"); |
69 |
Douglas Thrift |
194 |
|
70 |
Douglas Thrift |
201 |
this->path = *node/"jargon"; |
71 |
|
|
this->matcher = *node/"matcher"; |
72 |
Douglas Thrift |
194 |
|
73 |
Douglas Thrift |
265 |
_L<ext::String> args(1, this->path); |
74 |
Douglas Thrift |
194 |
|
75 |
Douglas Thrift |
265 |
args.InsertLast("-type"); |
76 |
|
|
args.InsertLast("f"); |
77 |
Douglas Thrift |
194 |
|
78 |
Douglas Thrift |
265 |
_S<api::Process> find("/usr/bin/find", args); |
79 |
Douglas Thrift |
203 |
Matcher matcher("^" + this->path + "/(" + this->matcher + ")$"); |
80 |
Douglas Thrift |
265 |
ext::String path; |
81 |
Douglas Thrift |
194 |
|
82 |
Douglas Thrift |
265 |
while (ios::ReadLine(*find.GetReader(), path)) if (matcher == path) |
83 |
Douglas Thrift |
196 |
{ |
84 |
Douglas Thrift |
265 |
jargon.insert(matcher[1]); |
85 |
Douglas Thrift |
196 |
} |
86 |
Douglas Thrift |
194 |
} |
87 |
|
|
|
88 |
Douglas Thrift |
203 |
void FeepingCreaturism::parse() |
89 |
|
|
{ |
90 |
|
|
std::stringstream query(env.get("QUERY_STRING")); |
91 |
|
|
|
92 |
|
|
if (env.get("REQUEST_METHOD") == "POST") |
93 |
|
|
{ |
94 |
douglas |
307 |
std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_TYPE"))); |
95 |
Douglas Thrift |
203 |
char* content(new char[length]); |
96 |
|
|
|
97 |
|
|
api::Cin.Read(content, length); |
98 |
|
|
query.write(content, length); |
99 |
|
|
|
100 |
|
|
delete [] content; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
if (query.str().empty()) return; |
104 |
|
|
|
105 |
|
|
do |
106 |
|
|
{ |
107 |
|
|
std::string name, value; |
108 |
|
|
|
109 |
|
|
std::getline(query, name, '='); |
110 |
|
|
std::getline(query, value, '&'); |
111 |
|
|
|
112 |
|
|
cgi.insert(_P(name, value)); |
113 |
|
|
} |
114 |
|
|
while (query.good()); |
115 |
|
|
} |
116 |
|
|
|
117 |
Douglas Thrift |
211 |
void FeepingCreaturism::daily(const ext::String& date) |
118 |
Douglas Thrift |
194 |
{ |
119 |
|
|
std::time_t when(std::time(NULL)); |
120 |
Douglas Thrift |
211 |
std::tm* day(std::localtime(&when)); |
121 |
Douglas Thrift |
194 |
|
122 |
Douglas Thrift |
211 |
day->tm_sec = 0; |
123 |
|
|
day->tm_min = 0; |
124 |
|
|
day->tm_hour = 0; |
125 |
Douglas Thrift |
194 |
|
126 |
Douglas Thrift |
211 |
if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day); |
127 |
|
|
|
128 |
|
|
std::time_t difference(mktime(day) / 86400); |
129 |
douglas |
307 |
ext::Vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
130 |
|
|
ext::String entry(jargon.GetSize() ? jargon[difference % jargon.GetSize()] : ""); |
131 |
Douglas Thrift |
194 |
|
132 |
|
|
select(entry); |
133 |
|
|
} |
134 |
|
|
|
135 |
Douglas Thrift |
211 |
void FeepingCreaturism::random(const ext::String& number) |
136 |
Douglas Thrift |
194 |
{ |
137 |
|
|
::srandomdev(); |
138 |
|
|
|
139 |
douglas |
307 |
ext::Vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
140 |
|
|
size_t random(number.IsEmpty() ? ::random() : lexical_cast<size_t>(number)); |
141 |
Douglas Thrift |
194 |
|
142 |
Douglas Thrift |
211 |
assert(random >= 0); |
143 |
douglas |
307 |
assert(random % jargon.GetSize() < jargon.GetSize()); |
144 |
Douglas Thrift |
211 |
|
145 |
douglas |
307 |
ext::String entry(jargon.GetSize() ? jargon[random % jargon.GetSize()] : ""); |
146 |
Douglas Thrift |
211 |
|
147 |
Douglas Thrift |
194 |
select(entry); |
148 |
|
|
} |
149 |
|
|
|
150 |
Douglas Thrift |
203 |
void FeepingCreaturism::select(const ext::String& selection, bool validate) |
151 |
Douglas Thrift |
194 |
{ |
152 |
|
|
if (!validate || jargon.find(selection) != jargon.end()) |
153 |
|
|
{ |
154 |
Douglas Thrift |
203 |
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
155 |
Douglas Thrift |
196 |
|
156 |
douglas |
307 |
Jargon jargon(path, selection, cgi.find("include") != cgi.end() && lexical_cast<bool>(ext::String(cgi.find("include")->second)), cgi.find("relative") != cgi.end() ? ext::String(cgi.find("relative")->second) : ext::String()); |
157 |
Douglas Thrift |
196 |
|
158 |
Douglas Thrift |
225 |
api::Cout << jargon; |
159 |
Douglas Thrift |
194 |
} |
160 |
Douglas Thrift |
195 |
else |
161 |
|
|
{ |
162 |
Douglas Thrift |
203 |
api::Cout << "Status: 404\r\n" |
163 |
|
|
<< "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n" |
164 |
|
|
<< "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" |
165 |
Douglas Thrift |
195 |
<< "<html><head>\n<title>404 Not Found</title>\n</head><body>\n" |
166 |
|
|
<< "<h1>Not Found</h1>\n<p>The requested URL " |
167 |
|
|
<< env.get("PATH_INFO") << " was not found on this server.</p>\n" |
168 |
|
|
<< "<hr />\n" << env.get("SERVER_SIGNATURE") << "</body></html>\n"; |
169 |
|
|
} |
170 |
Douglas Thrift |
194 |
} |