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