ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
Revision: 243
Committed: 2004-09-11T21:27:10-07:00 (20 years, 9 months ago) by Douglas Thrift
File size: 4663 byte(s)
Log Message:
Meep!

File Contents

# User Rev Content
1 Douglas Thrift 191 // Feeping Creaturism
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7 Douglas Thrift 194 #include "Environment.hpp"
8     #include "Matcher.hpp"
9 Douglas Thrift 196 #include "Jargon.hpp"
10 Douglas Thrift 191
11 Douglas Thrift 194 extern "C"
12     {
13     #include <sys/types.h>
14     #include <sys/stat.h>
15     #include <fts.h>
16     }
17    
18 Douglas Thrift 243 #include <menes-api/exename.hpp>
19     #include <menes-app/application.hpp>
20    
21     struct FeepingCreaturismCommand : public app::Application
22 Douglas Thrift 191 {
23 Douglas Thrift 243 virtual int Run(const app::ArgumentList& args)
24     {
25     FeepingCreaturism::program = api::GetExecutableName();
26 Douglas Thrift 201
27 Douglas Thrift 243 FeepingCreaturism creaturism;
28 Douglas Thrift 191
29 Douglas Thrift 243 return 0;
30     }
31     } creaturism;
32 Douglas Thrift 194
33     FeepingCreaturism::FeepingCreaturism()
34     {
35     initialize();
36 Douglas Thrift 203 parse();
37 Douglas Thrift 194
38 Douglas Thrift 203 ext::String path(env.get("PATH_INFO"));
39 Douglas Thrift 194 Matcher matcher;
40    
41 Douglas Thrift 211 if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$"))
42 Douglas Thrift 194 {
43 Douglas Thrift 211 daily(matcher.size() > 1 ? matcher[1] : "");
44 Douglas Thrift 194 }
45 Douglas Thrift 211 else if (path == matcher("^/random/(\\d+)?$"))
46     {
47     random(matcher.size() > 1 ? matcher[1] : "");
48     }
49 Douglas Thrift 194 else if (path == matcher("^/(" + this->matcher + ")$"))
50     {
51     select(matcher[1], true);
52     }
53 Douglas Thrift 198 else
54     {
55 Douglas Thrift 203 api::Cout << "Location: http://" << env.get("HTTP_HOST")
56 Douglas Thrift 202 << env.get("SCRIPT_NAME") << "/daily/\r\n\r\n";
57 Douglas Thrift 198 }
58 Douglas Thrift 194 }
59    
60 Douglas Thrift 203 ext::String FeepingCreaturism::program;
61 Douglas Thrift 201
62 Douglas Thrift 206 bool FeepingCreaturism::CaseLess::operator()(const std::string& one,
63     const std::string& two)
64     {
65     std::string one_(one), two_(two);
66    
67     // XXX: should be std::tolower except g++34 doesn't believe it
68     std::transform(one.begin(), one.end(), one_.begin(), ::tolower);
69     std::transform(two.begin(), two.end(), two_.begin(), ::tolower);
70    
71     return one_ < two_;
72     }
73    
74 Douglas Thrift 194 void FeepingCreaturism::initialize()
75     {
76 Douglas Thrift 201 ext::Handle<xml::Document> document(xml::Parse("jargon.xml"));
77     ext::Handle<xml::Node> node(*document/"feepingcreaturism");
78 Douglas Thrift 194
79 Douglas Thrift 201 this->path = *node/"jargon";
80     this->matcher = *node/"matcher";
81 Douglas Thrift 194
82 Douglas Thrift 236 char* path[] = { new char[this->path.GetData().GetSize()] };
83 Douglas Thrift 194
84 Douglas Thrift 236 std::strcpy(path[0], this->path.NullTerminate());
85 Douglas Thrift 194
86     ::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL));
87 Douglas Thrift 203 Matcher matcher("^" + this->path + "/(" + this->matcher + ")$");
88 Douglas Thrift 194
89 Douglas Thrift 196 if (traversal == NULL)
90     {
91 Douglas Thrift 203 api::Cerr << program << ": Horrible Failure!\n";
92 Douglas Thrift 196
93     std::exit(1);
94     }
95    
96 Douglas Thrift 194 for (::FTSENT* entity(::fts_read(traversal)); entity != NULL;
97     entity = ::fts_read(traversal))
98     {
99     if (entity->fts_info == FTS_F && entity->fts_path == matcher)
100     {
101     jargon.insert(matcher[1]);
102     }
103     }
104    
105     ::fts_close(traversal);
106    
107     delete [] path[0];
108     }
109    
110 Douglas Thrift 203 void FeepingCreaturism::parse()
111     {
112     std::stringstream query(env.get("QUERY_STRING"));
113    
114     if (env.get("REQUEST_METHOD") == "POST")
115     {
116     std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_"
117     "TYPE")));
118     char* content(new char[length]);
119    
120     api::Cin.Read(content, length);
121     query.write(content, length);
122    
123     delete [] content;
124     }
125    
126     if (query.str().empty()) return;
127    
128     do
129     {
130     std::string name, value;
131    
132     std::getline(query, name, '=');
133     std::getline(query, value, '&');
134    
135     cgi.insert(_P(name, value));
136     }
137     while (query.good());
138     }
139    
140 Douglas Thrift 211 void FeepingCreaturism::daily(const ext::String& date)
141 Douglas Thrift 194 {
142     std::time_t when(std::time(NULL));
143 Douglas Thrift 211 std::tm* day(std::localtime(&when));
144 Douglas Thrift 194
145 Douglas Thrift 211 day->tm_sec = 0;
146     day->tm_min = 0;
147     day->tm_hour = 0;
148 Douglas Thrift 194
149 Douglas Thrift 211 if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day);
150    
151     std::time_t difference(mktime(day) / 86400);
152 Douglas Thrift 203 std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
153     ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : "");
154 Douglas Thrift 194
155     select(entry);
156     }
157    
158 Douglas Thrift 211 void FeepingCreaturism::random(const ext::String& number)
159 Douglas Thrift 194 {
160     ::srandomdev();
161    
162 Douglas Thrift 203 std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
163 Douglas Thrift 211 std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() :
164     lexical_cast<std::vector<ext::String>::size_type>(number));
165 Douglas Thrift 194
166 Douglas Thrift 211 assert(random >= 0);
167     assert(random % jargon.size() < jargon.size());
168    
169     ext::String entry(jargon.size() ? jargon[random % jargon.size()] : "");
170    
171 Douglas Thrift 194 select(entry);
172     }
173    
174 Douglas Thrift 203 void FeepingCreaturism::select(const ext::String& selection, bool validate)
175 Douglas Thrift 194 {
176     if (!validate || jargon.find(selection) != jargon.end())
177     {
178 Douglas Thrift 203 api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
179 Douglas Thrift 196
180 Douglas Thrift 230 Jargon jargon(path, selection, cgi.find("include") != cgi.end()
181 Douglas Thrift 229 && lexical_cast<bool>(ext::String(cgi.find("include")->second)),
182     cgi.find("relative") != cgi.end()
183     ? ext::String(cgi.find("relative")->second) : ext::String());
184 Douglas Thrift 196
185 Douglas Thrift 225 api::Cout << jargon;
186 Douglas Thrift 194 }
187 Douglas Thrift 195 else
188     {
189 Douglas Thrift 203 api::Cout << "Status: 404\r\n"
190     << "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n"
191     << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
192 Douglas Thrift 195 << "<html><head>\n<title>404 Not Found</title>\n</head><body>\n"
193     << "<h1>Not Found</h1>\n<p>The requested URL "
194     << env.get("PATH_INFO") << " was not found on this server.</p>\n"
195     << "<hr />\n" << env.get("SERVER_SIGNATURE") << "</body></html>\n";
196     }
197 Douglas Thrift 194 }

Properties

Name Value
svn:eol-style native
svn:keywords Id