ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
Revision: 270
Committed: 2004-10-27T23:44:48-07:00 (20 years, 7 months ago) by Douglas Thrift
File size: 4274 byte(s)
Log Message:
Join for now.

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

Properties

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