ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
(Generate patch)

Comparing FeepingCreaturism/FeepingCreaturism.cpp (file contents):
Revision 261 by Douglas Thrift, 2004-10-07T22:20:16-07:00 vs.
Revision 794 by douglas, 2006-06-02T02:21:45-07:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 < #include "Environment.hpp"
8 < #include "Matcher.hpp"
7 > #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   #include "Jargon.hpp"
19  
20 < extern "C"
20 > template <typename Environment>
21 > cse::String Get(const Environment &environment, const cse::String &name)
22   {
23 < #include <sys/types.h>
24 < #include <sys/stat.h>
15 < #include <fts.h>
23 >        try { return environment.Get(name); }
24 >        catch (ext::NotFoundException) { return cse::EmptyString; }
25   }
26  
27 < #include <menes-api/exename.hpp>
19 < #include <menes-app/simple.hpp>
20 <
21 < int Main(const app::Options& options)
27 > int Main(const app::Options &options)
28   {
29 <        FeepingCreaturism::program = api::GetExecutablePath().GetName();
29 >        try
30 >        {
31 >                FeepingCreaturism creaturism;
32  
33 <        FeepingCreaturism creaturism;
33 >                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  
39 <        return 0;
39 >                return 1;
40 >        }
41   }
42  
43   FeepingCreaturism::FeepingCreaturism()
44   {
45 <        initialize();
46 <        parse();
45 >        Initialize();
46 >        Parse();
47  
48 <        ext::String path(env.get("PATH_INFO"));
49 <        Matcher matcher;
48 >        cse::String path(Get(api::TheEnvironment, _B("PATH_INFO")));
49 >        api::Pcre::RegEx::Match match;
50  
51 <        if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$"))
52 <        {
53 <                daily(matcher.size() > 1 ? matcher[1] : "");
54 <        }
55 <        else if (path == matcher("^/random/(\\d+)?$"))
56 <        {
44 <                random(matcher.size() > 1 ? matcher[1] : "");
45 <        }
46 <        else if (path == matcher("^/(" + this->matcher + ")$"))
47 <        {
48 <                select(matcher[1], true);
49 <        }
51 >        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          else
58 <        {
52 <                api::Cout << "Location: http://" << env.get("HTTP_HOST")
53 <                        << env.get("SCRIPT_NAME") << "/daily/\r\n\r\n";
54 <        }
58 >                api::Cout << _B("Location: http://") << Get(api::TheEnvironment, "HTTP_HOST") << Get(api::TheEnvironment, "SCRIPT_NAME") << _B("/daily/\r\n\r\n");
59   }
60  
61 < ext::String FeepingCreaturism::program;
58 <
59 < bool FeepingCreaturism::CaseLess::operator()(const std::string& one,
60 <        const std::string& two)
61 > bool FeepingCreaturism::CaseLess::Execute(const cse::String &one, const cse::String &two)
62   {
63 <        std::string one_(one), two_(two);
63 >        cse::String one_(cse::ToLower(one)), two_(cse::ToLower(two));
64  
65 <        // XXX: should be std::tolower except g++34 doesn't believe it
66 <        std::transform(one.begin(), one.end(), one_.begin(), ::tolower);
67 <        std::transform(two.begin(), two.end(), two_.begin(), ::tolower);
67 <
68 <        if (one_ == two_) return one < two;
65 >        // XXX: oops!
66 >        if (one_ == two_)
67 >                return one < two;
68  
69          return one_ < two_;
70   }
71  
72 < void FeepingCreaturism::initialize()
72 > void FeepingCreaturism::Initialize()
73   {
74 <        ext::Handle<xml::Document> document(xml::Parse("jargon.xml"));
75 <        ext::Handle<xml::Node> node(*document/"feepingcreaturism");
74 >        _R<xml::Document> document(xml::Parse(_B("jargon.xml")));
75 >        _R<xml::Node> node(*document/_B("feepingcreaturism"));
76  
77 <        this->path = *node/"jargon";
78 <        this->matcher = *node/"matcher";
77 >        path = *node/_B("jargon");
78 >        matcher = *node/_B("matcher");
79  
80 <        char* path[] = { new char[this->path.GetData().GetSize()] };
80 >        _L<cse::String> args(1, path);
81  
82 <        std::strcpy(path[0], this->path.NullTerminate());
82 >        args.InsertLast(_B("-type"));
83 >        args.InsertLast(_B("f"));
84  
85 <        ::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL));
86 <        Matcher matcher("^" + this->path + "/(" + this->matcher + ")$");
85 >        _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  
89 <        if (traversal == NULL)
90 <        {
91 <                api::Cerr << program << ": Horrible Failure!\n";
91 <
92 <                std::exit(1);
93 <        }
94 <
95 <        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];
89 >        while (ios::ReadLine(*find.GetReader(), path))
90 >                if (api::Pcre::RegEx::Match match = matcher_(path))
91 >                        jargon.Insert(match[1]);
92   }
93  
94 < void FeepingCreaturism::parse()
94 > void FeepingCreaturism::Parse()
95   {
96 <        std::stringstream query(env.get("QUERY_STRING"));
96 >        _S<ios::Buffer> query(Get(api::TheEnvironment, _B("QUERY_STRING")));
97  
98 <        if (env.get("REQUEST_METHOD") == "POST")
98 >        if (Get(api::TheEnvironment, _B("REQUEST_METHOD")) == _B("POST"))
99          {
100 <                std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_"
116 <                        "TYPE")));
117 <                char* content(new char[length]);
100 >                query.Clear();
101  
102 <                api::Cin.Read(content, length);
120 <                query.write(content, length);
121 <
122 <                delete [] content;
102 >                ios::ReadToWriteFully(api::Cin, query, lexical_cast<size_t>(Get(api::TheEnvironment, _B("CONTENT_LENGTH"))));
103          }
104  
105 <        if (query.str().empty()) return;
105 >        if (query.IsEmpty())
106 >                return;
107  
108 <        do
109 <        {
110 <                std::string name, value;
108 >        _forever
109 >                try
110 >                {
111 >                        cse::String name(ios::ReadUntil(query, '='));
112  
113 <                std::getline(query, name, '=');
114 <                std::getline(query, value, '&');
113 >                        try
114 >                        {
115 >                                cse::String value(ios::ReadUntil(query, '&'));
116 >
117 >                                cgi[name].InsertLast(value);
118 >                        }
119 >                        catch (ext::EosException)
120 >                        {
121 >                                cgi[name].InsertLast();
122  
123 <                cgi.insert(_P(name, value));
124 <        }
125 <        while (query.good());
123 >                                break;
124 >                        }
125 >                }
126 >                catch (ext::EosException) { break; }
127   }
128  
129 < void FeepingCreaturism::daily(const ext::String& date)
129 > void FeepingCreaturism::Daily(const cse::String &date)
130   {
131          std::time_t when(std::time(NULL));
132          std::tm* day(std::localtime(&when));
# Line 145 | Line 135 | void FeepingCreaturism::daily(const ext:
135          day->tm_min = 0;
136          day->tm_hour = 0;
137  
138 <        if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day);
138 >        if (!date.IsEmpty())
139 >                ::strptime(date.NullTerminate(), "%Y-%m-%d", day);
140  
141          std::time_t difference(mktime(day) / 86400);
142 <        std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
143 <        ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : "");
142 >        _L<cse::String> jargon_(jargon.Begin(), jargon.End());
143 >        cse::String entry(jargon_.GetSize() ? jargon_[difference % jargon_.GetSize()] : cse::EmptyString);
144  
145 <        select(entry);
145 >        Select(entry);
146   }
147  
148 < void FeepingCreaturism::random(const ext::String& number)
148 > void FeepingCreaturism::Random(const cse::String &number)
149   {
150 <        ::srandomdev();
151 <
152 <        std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
153 <        std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() :
154 <                lexical_cast<std::vector<ext::String>::size_type>(number));
150 >        _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  
158 <        assert(random >= 0);
159 <        assert(random % jargon.size() < jargon.size());
158 >        _assert(random >= 0);
159 >        _assert(random % jargon_.GetSize() < jargon_.GetSize());
160  
161 <        ext::String entry(jargon.size() ? jargon[random % jargon.size()] : "");
161 >        cse::String entry(jargon_.GetSize() ? jargon_[random % jargon_.GetSize()] : cse::EmptyString);
162  
163 <        select(entry);
163 >        Select(entry);
164   }
165  
166 < void FeepingCreaturism::select(const ext::String& selection, bool validate)
166 > void FeepingCreaturism::Select(const cse::String &selection, bool validate)
167   {
168 <        if (!validate || jargon.find(selection) != jargon.end())
168 >        if (!validate || jargon.Contains(selection))
169          {
170 <                api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
170 >                api::Cout << _B("Content-Type: text/html; charset=UTF-8\r\n\r\n");
171  
172 <                Jargon jargon(path, selection, cgi.find("include") != cgi.end()
180 <                        && lexical_cast<bool>(ext::String(cgi.find("include")->second)),
181 <                        cgi.find("relative") != cgi.end()
182 <                        ? ext::String(cgi.find("relative")->second) : ext::String());
172 >                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  
174                  api::Cout << jargon;
175          }
176          else
177 <        {
188 <                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 <                        << "<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 <        }
177 >                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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines