ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
Revision: 794
Committed: 2006-06-02T02:21:45-07:00 (19 years ago) by douglas
File size: 4668 byte(s)
Log Message:
Oops!

File Contents

# User Rev Content
1 Douglas Thrift 191 // Feeping Creaturism
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7 douglas 792 #include <cxx/standard.hh>
8    
9     #include <api/environment.hpp>
10     #include <api/exename.hpp>
11     #include <api/pcre/regex.hpp>
12     #include <api/random.hpp>
13     #include <app/simple.hpp>
14     #include <cse/traits.hpp>
15    
16     #include <ctime>
17    
18 Douglas Thrift 196 #include "Jargon.hpp"
19 Douglas Thrift 191
20 douglas 792 template <typename Environment>
21     cse::String Get(const Environment &environment, const cse::String &name)
22     {
23     try { return environment.Get(name); }
24     catch (ext::NotFoundException) { return cse::EmptyString; }
25     }
26 Douglas Thrift 261
27 douglas 792 int Main(const app::Options &options)
28 Douglas Thrift 191 {
29 douglas 792 try
30     {
31     FeepingCreaturism creaturism;
32 Douglas Thrift 201
33 douglas 792 return 0;
34     }
35     catch (const ext::Exception &exception)
36     {
37     api::Cout << _B("Content-Type: text/plain\r\n\r\n") << exception << ios::NewLine;
38 Douglas Thrift 191
39 douglas 792 return 1;
40     }
41 Douglas Thrift 251 }
42 Douglas Thrift 194
43     FeepingCreaturism::FeepingCreaturism()
44     {
45 douglas 792 Initialize();
46     Parse();
47 Douglas Thrift 194
48 douglas 792 cse::String path(Get(api::TheEnvironment, _B("PATH_INFO")));
49     api::Pcre::RegEx::Match match;
50 Douglas Thrift 194
51 douglas 792 if (match = api::Pcre::RegEx(_B("^/daily/(\\d{4}-\\d{2}-\\d{2})?$"))(path))
52     Daily(match[1]);
53     else if (match = api::Pcre::RegEx(_B("^/random/(\\d+)?$"))(path))
54     Random(match[1]);
55     else if (match = api::Pcre::RegEx(_S<ios::String>() << _B("^/(") << matcher << _B(")$"))(path))
56     Select(match[1], true);
57 Douglas Thrift 198 else
58 douglas 792 api::Cout << _B("Location: http://") << Get(api::TheEnvironment, "HTTP_HOST") << Get(api::TheEnvironment, "SCRIPT_NAME") << _B("/daily/\r\n\r\n");
59 Douglas Thrift 194 }
60    
61 douglas 792 bool FeepingCreaturism::CaseLess::Execute(const cse::String &one, const cse::String &two)
62 Douglas Thrift 206 {
63 douglas 792 cse::String one_(cse::ToLower(one)), two_(cse::ToLower(two));
64 Douglas Thrift 206
65 douglas 794 // XXX: oops!
66 douglas 792 if (one_ == two_)
67     return one < two;
68 Douglas Thrift 206
69     return one_ < two_;
70     }
71    
72 douglas 792 void FeepingCreaturism::Initialize()
73 Douglas Thrift 194 {
74 douglas 792 _R<xml::Document> document(xml::Parse(_B("jargon.xml")));
75     _R<xml::Node> node(*document/_B("feepingcreaturism"));
76 Douglas Thrift 194
77 douglas 792 path = *node/_B("jargon");
78     matcher = *node/_B("matcher");
79 Douglas Thrift 194
80 douglas 792 _L<cse::String> args(1, path);
81 Douglas Thrift 194
82 douglas 792 args.InsertLast(_B("-type"));
83     args.InsertLast(_B("f"));
84 Douglas Thrift 194
85 douglas 792 _S<api::Process> find(_B("/usr/bin/find"), args);
86     api::Pcre::RegEx matcher_(_S<ios::String>() << _B("^") << path << _B("/(") << matcher << _B(")$"));
87     ext::Buffer path;
88 Douglas Thrift 194
89 douglas 792 while (ios::ReadLine(*find.GetReader(), path))
90     if (api::Pcre::RegEx::Match match = matcher_(path))
91     jargon.Insert(match[1]);
92 Douglas Thrift 194 }
93    
94 douglas 792 void FeepingCreaturism::Parse()
95 Douglas Thrift 203 {
96 douglas 792 _S<ios::Buffer> query(Get(api::TheEnvironment, _B("QUERY_STRING")));
97 Douglas Thrift 203
98 douglas 792 if (Get(api::TheEnvironment, _B("REQUEST_METHOD")) == _B("POST"))
99 Douglas Thrift 203 {
100 douglas 792 query.Clear();
101 Douglas Thrift 203
102 douglas 792 ios::ReadToWriteFully(api::Cin, query, lexical_cast<size_t>(Get(api::TheEnvironment, _B("CONTENT_LENGTH"))));
103 Douglas Thrift 203 }
104    
105 douglas 792 if (query.IsEmpty())
106     return;
107 Douglas Thrift 203
108 douglas 792 _forever
109     try
110     {
111     cse::String name(ios::ReadUntil(query, '='));
112 Douglas Thrift 203
113 douglas 792 try
114     {
115     cse::String value(ios::ReadUntil(query, '&'));
116 Douglas Thrift 203
117 douglas 792 cgi[name].InsertLast(value);
118     }
119     catch (ext::EosException)
120     {
121     cgi[name].InsertLast();
122    
123     break;
124     }
125     }
126     catch (ext::EosException) { break; }
127 Douglas Thrift 203 }
128    
129 douglas 792 void FeepingCreaturism::Daily(const cse::String &date)
130 Douglas Thrift 194 {
131     std::time_t when(std::time(NULL));
132 Douglas Thrift 211 std::tm* day(std::localtime(&when));
133 Douglas Thrift 194
134 Douglas Thrift 211 day->tm_sec = 0;
135     day->tm_min = 0;
136     day->tm_hour = 0;
137 Douglas Thrift 194
138 douglas 792 if (!date.IsEmpty())
139     ::strptime(date.NullTerminate(), "%Y-%m-%d", day);
140 Douglas Thrift 211
141     std::time_t difference(mktime(day) / 86400);
142 douglas 792 _L<cse::String> jargon_(jargon.Begin(), jargon.End());
143     cse::String entry(jargon_.GetSize() ? jargon_[difference % jargon_.GetSize()] : cse::EmptyString);
144 Douglas Thrift 194
145 douglas 792 Select(entry);
146 Douglas Thrift 194 }
147    
148 douglas 792 void FeepingCreaturism::Random(const cse::String &number)
149 Douglas Thrift 194 {
150 douglas 792 _L<cse::String> jargon_(jargon.Begin(), jargon.End());
151     size_t random;
152    
153     if (number.IsEmpty())
154     api::WeakRandom.Read(reinterpret_cast<byte_t *>(&random), sizeof (size_t));
155     else
156     random = lexical_cast<size_t>(number);
157 Douglas Thrift 194
158 douglas 792 _assert(random >= 0);
159     _assert(random % jargon_.GetSize() < jargon_.GetSize());
160 Douglas Thrift 194
161 douglas 792 cse::String entry(jargon_.GetSize() ? jargon_[random % jargon_.GetSize()] : cse::EmptyString);
162 Douglas Thrift 211
163 douglas 792 Select(entry);
164 Douglas Thrift 194 }
165    
166 douglas 792 void FeepingCreaturism::Select(const cse::String &selection, bool validate)
167 Douglas Thrift 194 {
168 douglas 792 if (!validate || jargon.Contains(selection))
169 Douglas Thrift 194 {
170 douglas 792 api::Cout << _B("Content-Type: text/html; charset=UTF-8\r\n\r\n");
171 Douglas Thrift 196
172 douglas 792 Jargon jargon(path, selection, cgi.Contains(_B("include")) && lexical_cast<bool>(cgi[_B("include")].First()), cgi.Contains(_B("relative")) ? cgi[_B("relative")].First() : cse::EmptyString);
173 Douglas Thrift 196
174 Douglas Thrift 225 api::Cout << jargon;
175 Douglas Thrift 194 }
176 Douglas Thrift 195 else
177 douglas 792 api::Cout << _B("Status: 404\r\nContent-Type: text/html; charset=ISO-8859-1\r\n\r\n<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested URL ") << Get(api::TheEnvironment, _B("PATH_INFO")) << _B(" was not found on this server.</p>\n<hr />\n") << Get(api::TheEnvironment, _B("SERVER_SIGNATURE")) << _B("</body></html>\n");
178 Douglas Thrift 194 }

Properties

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