15 |
|
#include <fts.h> |
16 |
|
} |
17 |
|
|
18 |
< |
int main(int argc, char* argv[]) |
18 |
> |
#include <menes-api/exename.hpp> |
19 |
> |
#include <menes-app/simple.hpp> |
20 |
> |
|
21 |
> |
int Main(const app::Options& options) |
22 |
|
{ |
23 |
< |
FeepingCreaturism::program = argv[0]; |
23 |
> |
FeepingCreaturism::program = api::GetExecutablePath().GetName(); |
24 |
|
|
25 |
|
FeepingCreaturism creaturism; |
26 |
|
|
30 |
|
FeepingCreaturism::FeepingCreaturism() |
31 |
|
{ |
32 |
|
initialize(); |
33 |
+ |
parse(); |
34 |
|
|
35 |
< |
std::string path(env.get("PATH_INFO")); |
35 |
> |
ext::String path(env.get("PATH_INFO")); |
36 |
|
Matcher matcher; |
37 |
|
|
38 |
< |
if (path == "/daily/") daily(); else if (path == "/random/") |
38 |
> |
if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$")) |
39 |
> |
{ |
40 |
> |
daily(matcher.size() > 1 ? matcher[1] : ""); |
41 |
> |
} |
42 |
> |
else if (path == matcher("^/random/(\\d+)?$")) |
43 |
|
{ |
44 |
< |
random(); |
44 |
> |
random(matcher.size() > 1 ? matcher[1] : ""); |
45 |
|
} |
46 |
|
else if (path == matcher("^/(" + this->matcher + ")$")) |
47 |
|
{ |
49 |
|
} |
50 |
|
else |
51 |
|
{ |
52 |
< |
cout << "Status: 301\r\n"; |
52 |
> |
api::Cout << "Location: http://" << env.get("HTTP_HOST") |
53 |
> |
<< env.get("SCRIPT_NAME") << "/daily/\r\n\r\n"; |
54 |
|
} |
55 |
|
} |
56 |
|
|
57 |
< |
std::string FeepingCreaturism::program; |
57 |
> |
ext::String FeepingCreaturism::program; |
58 |
> |
|
59 |
> |
bool FeepingCreaturism::CaseLess::operator()(const std::string& one, |
60 |
> |
const std::string& two) |
61 |
> |
{ |
62 |
> |
std::string one_(one), two_(two); |
63 |
> |
|
64 |
> |
// XXX: should be std::tolower except g++34 doesn't believe it |
65 |
> |
std::transform(one.begin(), one.end(), one_.begin(), ::tolower); |
66 |
> |
std::transform(two.begin(), two.end(), two_.begin(), ::tolower); |
67 |
> |
|
68 |
> |
if (one_ == two_) return one < two; |
69 |
> |
|
70 |
> |
return one_ < two_; |
71 |
> |
} |
72 |
|
|
73 |
|
void FeepingCreaturism::initialize() |
74 |
|
{ |
78 |
|
this->path = *node/"jargon"; |
79 |
|
this->matcher = *node/"matcher"; |
80 |
|
|
81 |
< |
char* path[] = { new char[this->path.size()] }; |
81 |
> |
char* path[] = { new char[this->path.GetData().GetSize()] }; |
82 |
|
|
83 |
< |
std::strcpy(path[0], this->path.c_str()); |
83 |
> |
std::strcpy(path[0], this->path.NullTerminate()); |
84 |
|
|
85 |
|
::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL)); |
86 |
< |
Matcher matcher('^' + this->path + "/(" + this->matcher + ")$"); |
86 |
> |
Matcher matcher("^" + this->path + "/(" + this->matcher + ")$"); |
87 |
|
|
88 |
|
if (traversal == NULL) |
89 |
|
{ |
90 |
< |
cerr << program << ": Horrible Failure!\n"; |
90 |
> |
api::Cerr << program << ": Horrible Failure!\n"; |
91 |
|
|
92 |
|
std::exit(1); |
93 |
|
} |
106 |
|
delete [] path[0]; |
107 |
|
} |
108 |
|
|
109 |
< |
void FeepingCreaturism::daily() |
109 |
> |
void FeepingCreaturism::parse() |
110 |
> |
{ |
111 |
> |
std::stringstream query(env.get("QUERY_STRING")); |
112 |
> |
|
113 |
> |
if (env.get("REQUEST_METHOD") == "POST") |
114 |
> |
{ |
115 |
> |
std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_" |
116 |
> |
"TYPE"))); |
117 |
> |
char* content(new char[length]); |
118 |
> |
|
119 |
> |
api::Cin.Read(content, length); |
120 |
> |
query.write(content, length); |
121 |
> |
|
122 |
> |
delete [] content; |
123 |
> |
} |
124 |
> |
|
125 |
> |
if (query.str().empty()) return; |
126 |
> |
|
127 |
> |
do |
128 |
> |
{ |
129 |
> |
std::string name, value; |
130 |
> |
|
131 |
> |
std::getline(query, name, '='); |
132 |
> |
std::getline(query, value, '&'); |
133 |
> |
|
134 |
> |
cgi.insert(_P(name, value)); |
135 |
> |
} |
136 |
> |
while (query.good()); |
137 |
> |
} |
138 |
> |
|
139 |
> |
void FeepingCreaturism::daily(const ext::String& date) |
140 |
|
{ |
141 |
|
std::time_t when(std::time(NULL)); |
142 |
< |
std::tm* now(std::localtime(&when)); |
142 |
> |
std::tm* day(std::localtime(&when)); |
143 |
> |
|
144 |
> |
day->tm_sec = 0; |
145 |
> |
day->tm_min = 0; |
146 |
> |
day->tm_hour = 0; |
147 |
|
|
148 |
< |
now->tm_sec = 0; |
149 |
< |
now->tm_min = 0; |
150 |
< |
now->tm_hour = 0; |
151 |
< |
|
152 |
< |
std::time_t difference(mktime(now) / 86400); |
96 |
< |
std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end()); |
97 |
< |
std::string entry(jargon.size() ? jargon[difference % jargon.size()] : ""); |
148 |
> |
if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day); |
149 |
> |
|
150 |
> |
std::time_t difference(mktime(day) / 86400); |
151 |
> |
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
152 |
> |
ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : ""); |
153 |
|
|
154 |
|
select(entry); |
155 |
|
} |
156 |
|
|
157 |
< |
void FeepingCreaturism::random() |
157 |
> |
void FeepingCreaturism::random(const ext::String& number) |
158 |
|
{ |
159 |
|
::srandomdev(); |
160 |
|
|
161 |
< |
std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end()); |
162 |
< |
std::string entry(jargon.size() ? jargon[::random() % jargon.size()] : ""); |
161 |
> |
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
162 |
> |
std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() : |
163 |
> |
lexical_cast<std::vector<ext::String>::size_type>(number)); |
164 |
> |
|
165 |
> |
assert(random >= 0); |
166 |
> |
assert(random % jargon.size() < jargon.size()); |
167 |
> |
|
168 |
> |
ext::String entry(jargon.size() ? jargon[random % jargon.size()] : ""); |
169 |
|
|
170 |
|
select(entry); |
171 |
|
} |
172 |
|
|
173 |
< |
void FeepingCreaturism::select(const std::string& selection, bool validate) |
173 |
> |
void FeepingCreaturism::select(const ext::String& selection, bool validate) |
174 |
|
{ |
175 |
|
if (!validate || jargon.find(selection) != jargon.end()) |
176 |
|
{ |
177 |
< |
cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
177 |
> |
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
178 |
|
|
179 |
< |
Jargon jargon(path + '/' + selection); |
179 |
> |
Jargon jargon(path, selection, cgi.find("include") != cgi.end() |
180 |
> |
&& lexical_cast<bool>(ext::String(cgi.find("include")->second)), |
181 |
> |
cgi.find("relative") != cgi.end() |
182 |
> |
? ext::String(cgi.find("relative")->second) : ext::String()); |
183 |
|
|
184 |
< |
cout << jargon; |
184 |
> |
api::Cout << jargon; |
185 |
|
} |
186 |
|
else |
187 |
|
{ |
188 |
< |
cout << "Status: 404\r\nContent-Type: text/html; charset=ISO-8859-1\r\n" |
189 |
< |
<< "\r\n<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" |
188 |
> |
api::Cout << "Status: 404\r\n" |
189 |
> |
<< "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n" |
190 |
> |
<< "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" |
191 |
|
<< "<html><head>\n<title>404 Not Found</title>\n</head><body>\n" |
192 |
|
<< "<h1>Not Found</h1>\n<p>The requested URL " |
193 |
|
<< env.get("PATH_INFO") << " was not found on this server.</p>\n" |