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