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