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 117 by Douglas Thrift, 2004-03-17T02:02:43-08:00 vs.
Revision 340 by douglas, 2004-12-14T15:49:53-08:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines