ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Bender/Bender.cpp
Revision: 121
Committed: 2004-03-18T23:26:40-08:00 (21 years, 3 months ago) by Douglas Thrift
File size: 2748 byte(s)
Log Message:
Damn, Apache 1.3.x sucks!

File Contents

# User Rev Content
1 Douglas Thrift 109 // Bender
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Bender.hpp"
8 Douglas Thrift 113 #include "Matcher.hpp"
9 Douglas Thrift 109
10 Douglas Thrift 113 #include <xalanc/Include/PlatformDefinitions.hpp>
11     #include <xercesc/util/PlatformUtils.hpp>
12     #include <xalanc/XalanTransformer/XalanTransformer.hpp>
13    
14     XALAN_USING_XERCES(XMLPlatformUtils)
15     XALAN_USING_XALAN(XalanTransformer)
16    
17 Douglas Thrift 109 int main(int argc, char* argv[])
18     {
19     XMLPlatformUtils::Initialize();
20     XalanTransformer::initialize();
21 Douglas Thrift 119
22 Douglas Thrift 109 Bender bender;
23    
24     XalanTransformer::terminate();
25     XMLPlatformUtils::Terminate();
26     XalanTransformer::ICUCleanUp();
27    
28     return 0;
29     }
30    
31     Bender::Bender()
32     {
33 Douglas Thrift 111 string path = sgetenv("PATH_TRANSLATED");
34    
35 Douglas Thrift 119
36     if (path != "")
37 Douglas Thrift 111 {
38 Douglas Thrift 119 ifstream file(path.c_str());
39    
40     if (file.is_open())
41     {
42     file.close();
43    
44     bend(path, sgetenv("HTTP_USER_AGENT"));
45     }
46 Douglas Thrift 121 else
47     {
48     cout << "Status: 404\n"
49     << "Content-Type: text/html; charset=ISO-8859-1\n\n"
50     << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
51     << "<html><head>\n"
52     << "<title>404 Not Found</title>\n"
53     << "</head><body>\n"
54     << "<h1>Not Found</h1>\n"
55     << "<p>The requested URL " << sgetenv("PATH_INFO") << " was no"
56     << "t found on this server.</p>\n"
57     << "<hr />\n"
58     << sgetenv("SERVER_SIGNATURE")
59     << "</body></html>\n";
60     }
61 Douglas Thrift 111 }
62 Douglas Thrift 121 else
63     {
64     cout << "Location: http://computers.douglasthrift.net/bender.xml\n\n";
65     }
66 Douglas Thrift 109 }
67 Douglas Thrift 114
68 Douglas Thrift 116 void Bender::bend(const string& path, const string& agent)
69 Douglas Thrift 114 {
70     Matcher matcher;
71    
72 Douglas Thrift 116 if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)"))
73 Douglas Thrift 114 {
74 Douglas Thrift 116 bend(path);
75 Douglas Thrift 114 }
76 Douglas Thrift 116 else if (agent == matcher(string("^Mozilla/4.0 \\(compatible; MSIE (\\d+)")
77     + "\\.(\\d+)(\\w+)?; .*\\)$"))
78 Douglas Thrift 114 {
79 Douglas Thrift 117 int version;
80     istringstream number(matcher[1]);
81 Douglas Thrift 116
82 Douglas Thrift 117 number >> version;
83    
84     if (version >= 6)
85     {
86     pass(path);
87     }
88     else
89     {
90     bend(path);
91     }
92 Douglas Thrift 114 }
93 Douglas Thrift 116 else if (agent == matcher(string("^Mozilla/5.0 \\(.*; rv:(\\d+)\\.(\\d+)(")
94     + "\\.d+)?(\\w+)?\\) Gecko\\/.*"))
95 Douglas Thrift 114 {
96 Douglas Thrift 117 int major, minor;
97     istringstream number(matcher[1] + ' ' + matcher[2]);
98    
99     number >> major;
100     number >> minor;
101    
102     if (major > 1 || (major == 1 && minor >= 5))
103     {
104     pass(path);
105     }
106     else
107     {
108     bend(path);
109     }
110 Douglas Thrift 114 }
111     else
112     {
113 Douglas Thrift 116 bend(path);
114 Douglas Thrift 114 }
115     }
116    
117 Douglas Thrift 116 void Bender::bend(const string& path)
118 Douglas Thrift 114 {
119 Douglas Thrift 116 ostringstream output;
120     XalanTransformer transformer;
121    
122     if (transformer.transform(path.c_str(), output) == 0)
123     {
124     string type = "text/html; charset=ISO-8859-1";
125     Matcher matcher("<META http-equiv=\"Content-Type\" content=\"(.*)\">");
126    
127     if (matcher == output.str())
128     {
129     type = matcher[1];
130     }
131 Douglas Thrift 119
132 Douglas Thrift 116 cout << "Content-Type: " << type << "\n\n" << output.str();
133     }
134     else
135     {
136     pass(path);
137     }
138 Douglas Thrift 114 }
139 Douglas Thrift 116
140     void Bender::pass(const string& path)
141     {
142     cout << "Content-Type: text/xml; charset=ISO-8859-1\n\n";
143    
144     ifstream fin(path.c_str());
145     string line;
146    
147     do
148     {
149     getline(fin, line);
150    
151     cout << line << '\n';
152     }
153     while (fin.good());
154     }

Properties

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