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