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