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

Comparing Bender/Bender.cpp (file contents):
Revision 120 by Douglas Thrift, 2004-03-17T22:01:55-08:00 vs.
Revision 397 by douglas, 2004-12-26T18:51:15-08:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 < #include "Bender.hpp"
8 < #include "Matcher.hpp"
7 > #include "Matcher/Matcher.hpp"
8  
9 < #include <xalanc/Include/PlatformDefinitions.hpp>
10 < #include <xercesc/util/PlatformUtils.hpp>
11 < #include <xalanc/XalanTransformer/XalanTransformer.hpp>
9 > #include <menes-api/console.hpp>
10 > #include <menes-api/environment.hpp>
11 > #include <menes-api/files.hpp>
12 > #include <menes-api/process.hpp>
13 > #include <menes-app/simple.hpp>
14 > #include <menes-ios/helpers.hpp>
15 > #include <menes-xml/document.hpp>
16 > #include <menes-xml/nodeset.hpp>
17 > #include <menes-xml/parse.hpp>
18  
19 < XALAN_USING_XERCES(XMLPlatformUtils)
20 < XALAN_USING_XALAN(XalanTransformer)
19 > class Bender
20 > {
21 > private:
22 >        void bend(const ext::String& path, const ext::String& agent);
23 >        void bend(const ext::String& path);
24 >        void pass(const ext::String& path);
25 > public:
26 >        Bender();
27 > };
28  
29 < int main(int argc, char* argv[])
29 > struct Environment
30   {
31 <        XMLPlatformUtils::Initialize();
32 <        XalanTransformer::initialize();
31 >        ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } }
32 > } env;
33  
34 + int Main(const app::Options& options)
35 + {
36          Bender bender;
37  
24        XalanTransformer::terminate();
25        XMLPlatformUtils::Terminate();
26        XalanTransformer::ICUCleanUp();
27
38          return 0;
39   }
40  
41   Bender::Bender()
42   {
43 <        string path = sgetenv("PATH_TRANSLATED");
43 >        ext::String path(env.get("PATH_TRANSLATED"));
44 >        Matcher matcher("^" + env.get("SCRIPT_NAME"));
45  
46 <
36 <        if (path != "")
46 >        if (!path.IsEmpty() && env.get("REQUEST_URI") != matcher)
47          {
48 <                ifstream file(path.c_str());
39 <
40 <                if (file.is_open())
48 >                try
49                  {
50 <                        file.close();
50 >                        api::FileReader file(path);
51  
52 <                        bend(path, sgetenv("HTTP_USER_AGENT"));
53 <
54 <                        return;
52 >                        bend(path, env.get("HTTP_USER_AGENT"));
53 >                }
54 >                catch (ext::Exception)
55 >                {
56 >                        api::Cout << "Status: 404\r\n"
57 >                                << "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n"
58 >                                << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
59 >                                << "<html><head>\n"
60 >                                << "<title>404 Not Found</title>\n"
61 >                                << "</head><body>\n"
62 >                                << "<h1>Not Found</h1>\n"
63 >                                << "<p>The requested URL " << env.get("PATH_INFO")
64 >                                << " was not found on this server.</p>\n"
65 >                                << "<hr />\n"
66 >                                << env.get("SERVER_SIGNATURE") << "</body></html>\n";
67                  }
68          }
69 <
50 <        cout << "Location: http://computers.douglasthrift.net/bender.xml\n\n";
69 >        else api::Cout << "Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n";
70   }
71  
72 < void Bender::bend(const string& path, const string& agent)
72 > void Bender::bend(const ext::String& path, const ext::String& agent)
73   {
74          Matcher matcher;
75  
76 <        if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)"))
76 >        if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)")) bend(path); else if (agent == matcher("rv:(\\d+)\\.(\\d+).*\\) Gecko"))
77          {
78 <                bend(path);
78 >                int major_(lexical_cast<int>(matcher[1])), minor_(lexical_cast<int>(matcher[2]));
79 >
80 >                if (major_ > 1 || (major_ == 1 && minor_ >= 5)) pass(path); else bend(path);
81          }
82 <        else if (agent == matcher(string("^Mozilla/4.0 \\(compatible; MSIE (\\d+)")
83 <                + "\\.(\\d+)(\\w+)?; .*\\)$"))
63 <        {
64 <                int version;
65 <                istringstream number(matcher[1]);
82 >        else bend(path);
83 > }
84  
85 <                number >> version;
85 > void Bender::bend(const ext::String& path)
86 > {
87 >        _H<xml::Document> document(xml::Parse("bender.xml"));
88 >        _H<xml::Node> node(*document/"bender");
89 >        ext::String program(*node/"program");
90 >        _L<ext::String> args;
91  
92 <                if (version >= 6)
70 <                {
71 <                        pass(path);
72 <                }
73 <                else
74 <                {
75 <                        bend(path);
76 <                }
77 <        }
78 <        else if (agent == matcher(string("^Mozilla/5.0 \\(.*; rv:(\\d+)\\.(\\d+)(")
79 <                + "\\.d+)?(\\w+)?\\) Gecko\\/.*"))
80 <        {
81 <                int major, minor;
82 <                istringstream number(matcher[1] + ' ' + matcher[2]);
92 >        _foreach (xml::NodeSet, arg, *node/"argument") args.InsertLast(**arg);
93  
94 <                number >> major;
85 <                number >> minor;
94 >        args.InsertLast(path);
95  
96 <                if (major > 1 || (major == 1 && minor >= 5))
97 <                {
89 <                        pass(path);
90 <                }
91 <                else
92 <                {
93 <                        bend(path);
94 <                }
95 <        }
96 <        else
97 <        {
98 <                bend(path);
99 <        }
100 < }
96 >        _S<api::Process> xslt(program, args);
97 >        _S<ios::String> output;
98  
99 < void Bender::bend(const string& path)
103 < {
104 <        ostringstream output;
105 <        XalanTransformer transformer;
99 >        ios::ReadToWrite(*xslt.GetReader(), output);
100  
101 <        if (transformer.transform(path.c_str(), output) == 0)
101 >        if (!output.IsEmpty())
102          {
103 <                string type = "text/html; charset=ISO-8859-1";
104 <                Matcher matcher("<META http-equiv=\"Content-Type\" content=\"(.*)\">");
103 >                ext::String type("text/xml");
104 >                Matcher matcher("http-equiv=\"Content-Type\" content=\"(.*)\"", PCRE_MULTILINE);
105  
106 <                if (matcher == output.str())
113 <                {
114 <                        type = matcher[1];
115 <                }
106 >                if (matcher == output) type = matcher[1];
107  
108 <                cout << "Content-Type: " << type << "\n\n" << output.str();
118 <        }
119 <        else
120 <        {
121 <                pass(path);
108 >                api::Cout << "Content-Type: " << type << "\r\n\r\n" << output;
109          }
110 +        else pass(path);
111   }
112  
113 < void Bender::pass(const string& path)
113 > void Bender::pass(const ext::String& path)
114   {
115 <        cout << "Content-Type: text/xml; charset=ISO-8859-1\n\n";
128 <
129 <        ifstream fin(path.c_str());
130 <        string line;
115 >        api::Cout << "Content-Type: text/xml\r\n\r\n";
116  
117 <        do
133 <        {
134 <                getline(fin, line);
117 >        api::FileReader file(path);
118  
119 <                cout << line << '\n';
137 <        }
138 <        while (fin.good());
119 >        ios::ReadToWrite(file, api::Cout);
120   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines