ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
Revision: 261
Committed: 2004-10-07T22:20:16-07:00 (20 years, 8 months ago) by Douglas Thrift
File size: 4617 byte(s)
Log Message:
Huzzah!

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

Properties

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