ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
Revision: 336
Committed: 2004-12-13T21:23:11-08:00 (20 years, 6 months ago) by douglas
File size: 4222 byte(s)
Log Message:
Cheese!

File Contents

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

Properties

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