4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
< |
#include "Environment.hpp" |
8 |
< |
#include "Matcher.hpp" |
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 |
< |
extern "C" |
20 |
> |
template <typename Environment> |
21 |
> |
cse::String Get(const Environment &environment, const cse::String &name) |
22 |
|
{ |
23 |
< |
#include <sys/types.h> |
24 |
< |
#include <sys/stat.h> |
15 |
< |
#include <fts.h> |
23 |
> |
try { return environment.Get(name); } |
24 |
> |
catch (ext::NotFoundException) { return cse::EmptyString; } |
25 |
|
} |
26 |
|
|
27 |
< |
int main(int argc, char* argv[]) |
27 |
> |
int Main(const app::Options &options) |
28 |
|
{ |
29 |
< |
FeepingCreaturism::program = argv[0]; |
29 |
> |
try |
30 |
> |
{ |
31 |
> |
FeepingCreaturism creaturism; |
32 |
|
|
33 |
< |
FeepingCreaturism creaturism; |
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 0; |
39 |
> |
return 1; |
40 |
> |
} |
41 |
|
} |
42 |
|
|
43 |
|
FeepingCreaturism::FeepingCreaturism() |
44 |
|
{ |
45 |
< |
initialize(); |
46 |
< |
parse(); |
45 |
> |
Initialize(); |
46 |
> |
Parse(); |
47 |
|
|
48 |
< |
ext::String path(env.get("PATH_INFO")); |
49 |
< |
Matcher matcher; |
48 |
> |
cse::String path(Get(api::TheEnvironment, _B("PATH_INFO"))); |
49 |
> |
api::Pcre::RegEx::Match match; |
50 |
|
|
51 |
< |
if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$")) |
52 |
< |
{ |
53 |
< |
daily(matcher.size() > 1 ? matcher[1] : ""); |
54 |
< |
} |
55 |
< |
else if (path == matcher("^/random/(\\d+)?$")) |
56 |
< |
{ |
41 |
< |
random(matcher.size() > 1 ? matcher[1] : ""); |
42 |
< |
} |
43 |
< |
else if (path == matcher("^/(" + this->matcher + ")$")) |
44 |
< |
{ |
45 |
< |
select(matcher[1], true); |
46 |
< |
} |
51 |
> |
if (match = api::Pcre::RegEx(_B("^/daily/(\\d{4}-\\d{2}-\\d{2})?$"))(path)) |
52 |
> |
Daily(match[1]); |
53 |
> |
else if (match = api::Pcre::RegEx(_B("^/random/(\\d+)?$"))(path)) |
54 |
> |
Random(match[1]); |
55 |
> |
else if (match = api::Pcre::RegEx(_S<ios::String>() << _B("^/(") << matcher << _B(")$"))(path)) |
56 |
> |
Select(match[1], true); |
57 |
|
else |
58 |
< |
{ |
49 |
< |
api::Cout << "Location: http://" << env.get("HTTP_HOST") |
50 |
< |
<< env.get("SCRIPT_NAME") << "/daily/\r\n\r\n"; |
51 |
< |
} |
58 |
> |
api::Cout << _B("Location: http://") << Get(api::TheEnvironment, "HTTP_HOST") << Get(api::TheEnvironment, "SCRIPT_NAME") << _B("/daily/\r\n\r\n"); |
59 |
|
} |
60 |
|
|
61 |
< |
ext::String FeepingCreaturism::program; |
55 |
< |
|
56 |
< |
bool FeepingCreaturism::CaseLess::operator()(const std::string& one, |
57 |
< |
const std::string& two) |
61 |
> |
bool FeepingCreaturism::CaseLess::Execute(const cse::String &one, const cse::String &two) |
62 |
|
{ |
63 |
< |
std::string one_(one), two_(two); |
63 |
> |
cse::String one_(cse::ToLower(one)), two_(cse::ToLower(two)); |
64 |
|
|
65 |
< |
// XXX: should be std::tolower except g++34 doesn't believe it |
66 |
< |
std::transform(one.begin(), one.end(), one_.begin(), ::tolower); |
63 |
< |
std::transform(two.begin(), two.end(), two_.begin(), ::tolower); |
64 |
< |
|
65 |
< |
if (one_ == two_) return one < two; |
65 |
> |
if (one_ == two_) |
66 |
> |
return one < two; |
67 |
|
|
68 |
|
return one_ < two_; |
69 |
|
} |
70 |
|
|
71 |
< |
void FeepingCreaturism::initialize() |
71 |
> |
void FeepingCreaturism::Initialize() |
72 |
|
{ |
73 |
< |
ext::Handle<xml::Document> document(xml::Parse("jargon.xml")); |
74 |
< |
ext::Handle<xml::Node> node(*document/"feepingcreaturism"); |
74 |
< |
|
75 |
< |
this->path = *node/"jargon"; |
76 |
< |
this->matcher = *node/"matcher"; |
73 |
> |
_R<xml::Document> document(xml::Parse(_B("jargon.xml"))); |
74 |
> |
_R<xml::Node> node(*document/_B("feepingcreaturism")); |
75 |
|
|
76 |
< |
char* path[] = { new char[this->path.GetData().GetSize()] }; |
76 |
> |
path = *node/_B("jargon"); |
77 |
> |
matcher = *node/_B("matcher"); |
78 |
|
|
79 |
< |
std::strcpy(path[0], this->path.NullTerminate()); |
79 |
> |
_L<cse::String> args(1, path); |
80 |
|
|
81 |
< |
::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL)); |
82 |
< |
Matcher matcher("^" + this->path + "/(" + this->matcher + ")$"); |
81 |
> |
args.InsertLast(_B("-type")); |
82 |
> |
args.InsertLast(_B("f")); |
83 |
|
|
84 |
< |
if (traversal == NULL) |
85 |
< |
{ |
86 |
< |
api::Cerr << program << ": Horrible Failure!\n"; |
88 |
< |
|
89 |
< |
std::exit(1); |
90 |
< |
} |
91 |
< |
|
92 |
< |
for (::FTSENT* entity(::fts_read(traversal)); entity != NULL; |
93 |
< |
entity = ::fts_read(traversal)) |
94 |
< |
{ |
95 |
< |
if (entity->fts_info == FTS_F && entity->fts_path == matcher) |
96 |
< |
{ |
97 |
< |
jargon.insert(matcher[1]); |
98 |
< |
} |
99 |
< |
} |
100 |
< |
|
101 |
< |
::fts_close(traversal); |
84 |
> |
_S<api::Process> find(_B("/usr/bin/find"), args); |
85 |
> |
api::Pcre::RegEx matcher_(_S<ios::String>() << _B("^") << path << _B("/(") << matcher << _B(")$")); |
86 |
> |
ext::Buffer path; |
87 |
|
|
88 |
< |
delete [] path[0]; |
88 |
> |
while (ios::ReadLine(*find.GetReader(), path)) |
89 |
> |
if (api::Pcre::RegEx::Match match = matcher_(path)) |
90 |
> |
jargon.Insert(match[1]); |
91 |
|
} |
92 |
|
|
93 |
< |
void FeepingCreaturism::parse() |
93 |
> |
void FeepingCreaturism::Parse() |
94 |
|
{ |
95 |
< |
std::stringstream query(env.get("QUERY_STRING")); |
95 |
> |
_S<ios::Buffer> query(Get(api::TheEnvironment, _B("QUERY_STRING"))); |
96 |
|
|
97 |
< |
if (env.get("REQUEST_METHOD") == "POST") |
97 |
> |
if (Get(api::TheEnvironment, _B("REQUEST_METHOD")) == _B("POST")) |
98 |
|
{ |
99 |
< |
std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_" |
113 |
< |
"TYPE"))); |
114 |
< |
char* content(new char[length]); |
99 |
> |
query.Clear(); |
100 |
|
|
101 |
< |
api::Cin.Read(content, length); |
117 |
< |
query.write(content, length); |
118 |
< |
|
119 |
< |
delete [] content; |
101 |
> |
ios::ReadToWriteFully(api::Cin, query, lexical_cast<size_t>(Get(api::TheEnvironment, _B("CONTENT_LENGTH")))); |
102 |
|
} |
103 |
|
|
104 |
< |
if (query.str().empty()) return; |
104 |
> |
if (query.IsEmpty()) |
105 |
> |
return; |
106 |
|
|
107 |
< |
do |
108 |
< |
{ |
109 |
< |
std::string name, value; |
107 |
> |
_forever |
108 |
> |
try |
109 |
> |
{ |
110 |
> |
cse::String name(ios::ReadUntil(query, '=')); |
111 |
|
|
112 |
< |
std::getline(query, name, '='); |
113 |
< |
std::getline(query, value, '&'); |
112 |
> |
try |
113 |
> |
{ |
114 |
> |
cse::String value(ios::ReadUntil(query, '&')); |
115 |
> |
|
116 |
> |
cgi[name].InsertLast(value); |
117 |
> |
} |
118 |
> |
catch (ext::EosException) |
119 |
> |
{ |
120 |
> |
cgi[name].InsertLast(); |
121 |
|
|
122 |
< |
cgi.insert(_P(name, value)); |
123 |
< |
} |
124 |
< |
while (query.good()); |
122 |
> |
break; |
123 |
> |
} |
124 |
> |
} |
125 |
> |
catch (ext::EosException) { break; } |
126 |
|
} |
127 |
|
|
128 |
< |
void FeepingCreaturism::daily(const ext::String& date) |
128 |
> |
void FeepingCreaturism::Daily(const cse::String &date) |
129 |
|
{ |
130 |
|
std::time_t when(std::time(NULL)); |
131 |
|
std::tm* day(std::localtime(&when)); |
134 |
|
day->tm_min = 0; |
135 |
|
day->tm_hour = 0; |
136 |
|
|
137 |
< |
if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day); |
137 |
> |
if (!date.IsEmpty()) |
138 |
> |
::strptime(date.NullTerminate(), "%Y-%m-%d", day); |
139 |
|
|
140 |
|
std::time_t difference(mktime(day) / 86400); |
141 |
< |
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
142 |
< |
ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : ""); |
141 |
> |
_L<cse::String> jargon_(jargon.Begin(), jargon.End()); |
142 |
> |
cse::String entry(jargon_.GetSize() ? jargon_[difference % jargon_.GetSize()] : cse::EmptyString); |
143 |
|
|
144 |
< |
select(entry); |
144 |
> |
Select(entry); |
145 |
|
} |
146 |
|
|
147 |
< |
void FeepingCreaturism::random(const ext::String& number) |
147 |
> |
void FeepingCreaturism::Random(const cse::String &number) |
148 |
|
{ |
149 |
< |
::srandomdev(); |
150 |
< |
|
151 |
< |
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
152 |
< |
std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() : |
153 |
< |
lexical_cast<std::vector<ext::String>::size_type>(number)); |
149 |
> |
_L<cse::String> jargon_(jargon.Begin(), jargon.End()); |
150 |
> |
size_t random; |
151 |
> |
|
152 |
> |
if (number.IsEmpty()) |
153 |
> |
api::WeakRandom.Read(reinterpret_cast<byte_t *>(&random), sizeof (size_t)); |
154 |
> |
else |
155 |
> |
random = lexical_cast<size_t>(number); |
156 |
|
|
157 |
< |
assert(random >= 0); |
158 |
< |
assert(random % jargon.size() < jargon.size()); |
157 |
> |
_assert(random >= 0); |
158 |
> |
_assert(random % jargon_.GetSize() < jargon_.GetSize()); |
159 |
|
|
160 |
< |
ext::String entry(jargon.size() ? jargon[random % jargon.size()] : ""); |
160 |
> |
cse::String entry(jargon_.GetSize() ? jargon_[random % jargon_.GetSize()] : cse::EmptyString); |
161 |
|
|
162 |
< |
select(entry); |
162 |
> |
Select(entry); |
163 |
|
} |
164 |
|
|
165 |
< |
void FeepingCreaturism::select(const ext::String& selection, bool validate) |
165 |
> |
void FeepingCreaturism::Select(const cse::String &selection, bool validate) |
166 |
|
{ |
167 |
< |
if (!validate || jargon.find(selection) != jargon.end()) |
167 |
> |
if (!validate || jargon.Contains(selection)) |
168 |
|
{ |
169 |
< |
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
169 |
> |
api::Cout << _B("Content-Type: text/html; charset=UTF-8\r\n\r\n"); |
170 |
|
|
171 |
< |
Jargon jargon(path, selection, cgi.find("include") != cgi.end() |
177 |
< |
&& lexical_cast<bool>(ext::String(cgi.find("include")->second)), |
178 |
< |
cgi.find("relative") != cgi.end() |
179 |
< |
? ext::String(cgi.find("relative")->second) : ext::String()); |
171 |
> |
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); |
172 |
|
|
173 |
|
api::Cout << jargon; |
174 |
|
} |
175 |
|
else |
176 |
< |
{ |
185 |
< |
api::Cout << "Status: 404\r\n" |
186 |
< |
<< "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n" |
187 |
< |
<< "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" |
188 |
< |
<< "<html><head>\n<title>404 Not Found</title>\n</head><body>\n" |
189 |
< |
<< "<h1>Not Found</h1>\n<p>The requested URL " |
190 |
< |
<< env.get("PATH_INFO") << " was not found on this server.</p>\n" |
191 |
< |
<< "<hr />\n" << env.get("SERVER_SIGNATURE") << "</body></html>\n"; |
192 |
< |
} |
176 |
> |
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"); |
177 |
|
} |