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