1 |
// Feeping Creaturism |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <cxx/standard.hh> |
8 |
|
9 |
#include <api/environment.hpp> |
10 |
#include <api/exename.hpp> |
11 |
#include <api/pcre/regex.hpp> |
12 |
#include <api/random.hpp> |
13 |
#include <app/simple.hpp> |
14 |
#include <cse/traits.hpp> |
15 |
|
16 |
#include <ctime> |
17 |
|
18 |
#include "Jargon.hpp" |
19 |
|
20 |
template <typename Environment> |
21 |
cse::String Get(const Environment &environment, const cse::String &name) |
22 |
{ |
23 |
try { return environment.Get(name); } |
24 |
catch (ext::NotFoundException) { return cse::EmptyString; } |
25 |
} |
26 |
|
27 |
int Main(const app::Options &options) |
28 |
{ |
29 |
try |
30 |
{ |
31 |
FeepingCreaturism creaturism; |
32 |
|
33 |
return 0; |
34 |
} |
35 |
catch (const ext::Exception &exception) |
36 |
{ |
37 |
api::Cout << _B("Content-Type: text/plain\r\n\r\n") << exception << ios::NewLine; |
38 |
|
39 |
return 1; |
40 |
} |
41 |
} |
42 |
|
43 |
FeepingCreaturism::FeepingCreaturism() |
44 |
{ |
45 |
if (Initialize()) |
46 |
return; |
47 |
|
48 |
Parse(); |
49 |
|
50 |
cse::String path(Get(api::TheEnvironment, _B("PATH_INFO"))); |
51 |
api::Pcre::RegEx::Match match; |
52 |
|
53 |
if (match = api::Pcre::RegEx(_B("^/daily/(\\d{4}-\\d{2}-\\d{2})?$"))(path)) |
54 |
Daily(match[1]); |
55 |
else if (match = api::Pcre::RegEx(_B("^/random/(\\d+)?$"))(path)) |
56 |
Random(match[1]); |
57 |
else if (match = api::Pcre::RegEx(_S<ios::String>() << _B("^/(") << matcher << _B(")$"))(path)) |
58 |
Select(match[1], true); |
59 |
else |
60 |
api::Cout << _B("Location: http://") << Get(api::TheEnvironment, "HTTP_HOST") << Get(api::TheEnvironment, "SCRIPT_NAME") << _B("/daily/\r\n\r\n"); |
61 |
} |
62 |
|
63 |
bool FeepingCreaturism::CaseLess::Execute(const cse::String &one, const cse::String &two) |
64 |
{ |
65 |
cse::String one_(cse::ToLower(one)), two_(cse::ToLower(two)); |
66 |
|
67 |
if (one_ == two_) |
68 |
return ext::Compare(one.GetData().Begin(), two.GetData().Begin(), one.GetData().GetSize() < two.GetData().GetSize() ? one.GetData().GetSize() : two.GetData().GetSize()) < 0; |
69 |
|
70 |
return ext::Compare(one_.GetData().Begin(), two_.GetData().Begin(), one_.GetData().GetSize() < two_.GetData().GetSize() ? one_.GetData().GetSize() : two_.GetData().GetSize()) < 0; |
71 |
} |
72 |
|
73 |
bool FeepingCreaturism::Initialize() |
74 |
{ |
75 |
_R<xml::Document> document(xml::Parse(_B("jargon.xml"))); |
76 |
_R<xml::Node> node(*document/_B("feepingcreaturism")); |
77 |
|
78 |
path = *node/_B("jargon"); |
79 |
matcher = *node/_B("matcher"); |
80 |
|
81 |
if (Get(api::TheEnvironment, _B("GATEWAY_INTERFACE")).IsEmpty()) |
82 |
{ |
83 |
_L<cse::String> args(1, path); |
84 |
|
85 |
args.InsertLast(_B("-type")); |
86 |
args.InsertLast(_B("f")); |
87 |
|
88 |
_S<api::Process> find(_B("/usr/bin/find"), args); |
89 |
api::Pcre::RegEx matcher_(_S<ios::String>() << _B("^") << path << _B("/(") << matcher << _B(")$")); |
90 |
ext::Buffer path; |
91 |
|
92 |
while (ios::ReadLine(*find.GetReader(), path)) |
93 |
if (api::Pcre::RegEx::Match match = matcher_(path)) |
94 |
jargon.Insert(match[1]); |
95 |
|
96 |
_S<api::FileWriter> writer(_B("jargon.dat")); |
97 |
|
98 |
_foreach (Set, path, jargon) |
99 |
_S<ios::FormatWriter>(writer) << *path << ios::NewLine; |
100 |
|
101 |
return true; |
102 |
} |
103 |
else |
104 |
{ |
105 |
_S<api::FileReader> reader(_B("jargon.dat")); |
106 |
ext::Buffer path; |
107 |
|
108 |
while (ios::ReadLine(reader, path)) |
109 |
jargon.Insert(path); |
110 |
} |
111 |
|
112 |
return false; |
113 |
} |
114 |
|
115 |
void FeepingCreaturism::Parse() |
116 |
{ |
117 |
_S<ios::Buffer> query(Get(api::TheEnvironment, _B("QUERY_STRING"))); |
118 |
|
119 |
if (Get(api::TheEnvironment, _B("REQUEST_METHOD")) == _B("POST")) |
120 |
{ |
121 |
query.Clear(); |
122 |
|
123 |
ios::ReadToWriteFully(api::Cin, query, lexical_cast<size_t>(Get(api::TheEnvironment, _B("CONTENT_LENGTH")))); |
124 |
} |
125 |
|
126 |
if (query.IsEmpty()) |
127 |
return; |
128 |
|
129 |
_forever |
130 |
try |
131 |
{ |
132 |
cse::String name(ios::ReadUntil(query, '=')); |
133 |
|
134 |
try |
135 |
{ |
136 |
// XXX: ios::ReadUntil() will throw an exception if it doesn't find '&' |
137 |
cse::String value(ios::ReadUntil(query, '&')); |
138 |
|
139 |
cgi[name].InsertLast(value); |
140 |
} |
141 |
catch (ext::EosException) |
142 |
{ |
143 |
cgi[name].InsertLast(_B("PWND!")); |
144 |
|
145 |
break; |
146 |
} |
147 |
} |
148 |
catch (ext::EosException) { break; } |
149 |
} |
150 |
|
151 |
void FeepingCreaturism::Daily(const cse::String &date) |
152 |
{ |
153 |
std::time_t when(std::time(NULL)); |
154 |
std::tm* day(std::localtime(&when)); |
155 |
|
156 |
day->tm_sec = 0; |
157 |
day->tm_min = 0; |
158 |
day->tm_hour = 0; |
159 |
|
160 |
if (!date.IsEmpty()) |
161 |
::strptime(date.NullTerminate(), "%Y-%m-%d", day); |
162 |
|
163 |
std::time_t difference(mktime(day) / 86400); |
164 |
_L<cse::String> jargon_(jargon.Begin(), jargon.End()); |
165 |
cse::String entry(jargon_.GetSize() ? jargon_[difference % jargon_.GetSize()] : cse::EmptyString); |
166 |
|
167 |
Select(entry); |
168 |
} |
169 |
|
170 |
void FeepingCreaturism::Random(const cse::String &number) |
171 |
{ |
172 |
_L<cse::String> jargon_(jargon.Begin(), jargon.End()); |
173 |
size_t random; |
174 |
|
175 |
if (number.IsEmpty()) |
176 |
api::WeakRandom.Read(reinterpret_cast<byte_t *>(&random), sizeof (size_t)); |
177 |
else |
178 |
random = lexical_cast<size_t>(number); |
179 |
|
180 |
_assert(random >= 0); |
181 |
_assert(random % jargon_.GetSize() < jargon_.GetSize()); |
182 |
|
183 |
cse::String entry(jargon_.GetSize() ? jargon_[random % jargon_.GetSize()] : cse::EmptyString); |
184 |
|
185 |
Select(entry); |
186 |
} |
187 |
|
188 |
void FeepingCreaturism::Select(const cse::String &selection, bool validate) |
189 |
{ |
190 |
if (!validate || jargon.Contains(selection)) |
191 |
{ |
192 |
api::Cout << _B("Content-Type: text/html; charset=UTF-8\r\n\r\n"); |
193 |
|
194 |
Jargon jargon(path, selection, cgi.Contains(_B("include")) && lexical_cast<bool>(cgi[_B("include")].First()), cgi.Contains(_B("relative")) ? cgi[_B("relative")].First() : cse::EmptyString); |
195 |
|
196 |
api::Cout << jargon; |
197 |
} |
198 |
else |
199 |
api::Cout << _B("Status: 404\r\nContent-Type: text/html; charset=ISO-8859-1\r\n\r\n<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested URL ") << Get(api::TheEnvironment, _B("PATH_INFO")) << _B(" was not found on this server.</p>\n<hr />\n") << Get(api::TheEnvironment, _B("SERVER_SIGNATURE")) << _B("</body></html>\n"); |
200 |
} |