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 121 by Douglas Thrift, 2004-03-18T23:26:40-08:00 vs.
Revision 268 by Douglas Thrift, 2004-10-27T11:55:27-07:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines