1 |
Douglas Thrift |
109 |
// Bender |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
douglas |
320 |
#include "Matcher/Matcher.hpp" |
8 |
Douglas Thrift |
109 |
|
9 |
douglas |
350 |
#include <menes-api/environment.hpp> |
10 |
Douglas Thrift |
266 |
#include <menes-api/files.hpp> |
11 |
|
|
#include <menes-api/process.hpp> |
12 |
|
|
#include <menes-app/simple.hpp> |
13 |
douglas |
350 |
#include <menes-ios/helpers.hpp> |
14 |
Douglas Thrift |
113 |
|
15 |
douglas |
350 |
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 |
douglas |
339 |
struct Environment |
26 |
Douglas Thrift |
109 |
{ |
27 |
douglas |
340 |
ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } } |
28 |
Douglas Thrift |
268 |
} env; |
29 |
Douglas Thrift |
119 |
|
30 |
Douglas Thrift |
268 |
int Main(const app::Options& options) |
31 |
|
|
{ |
32 |
|
|
Bender bender; |
33 |
|
|
|
34 |
Douglas Thrift |
109 |
return 0; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
Bender::Bender() |
38 |
|
|
{ |
39 |
Douglas Thrift |
268 |
ext::String path(env.get("PATH_TRANSLATED")); |
40 |
|
|
Matcher matcher("^" + env.get("SCRIPT_NAME")); |
41 |
Douglas Thrift |
111 |
|
42 |
Douglas Thrift |
268 |
if (!path.IsEmpty() && env.get("REQUEST_URI") != matcher) |
43 |
Douglas Thrift |
111 |
{ |
44 |
Douglas Thrift |
266 |
try |
45 |
Douglas Thrift |
119 |
{ |
46 |
Douglas Thrift |
266 |
api::FileReader file(path); |
47 |
Douglas Thrift |
119 |
|
48 |
Douglas Thrift |
268 |
bend(path, env.get("HTTP_USER_AGENT")); |
49 |
Douglas Thrift |
119 |
} |
50 |
Douglas Thrift |
266 |
catch (ext::Exception e) |
51 |
Douglas Thrift |
121 |
{ |
52 |
Douglas Thrift |
266 |
api::Cout << "Status: 404\r\n" |
53 |
|
|
<< "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n" |
54 |
Douglas Thrift |
121 |
<< "<!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 |
Douglas Thrift |
268 |
<< "<p>The requested URL " << env.get("PATH_INFO") |
60 |
Douglas Thrift |
266 |
<< " was not found on this server.</p>\n" |
61 |
Douglas Thrift |
121 |
<< "<hr />\n" |
62 |
Douglas Thrift |
268 |
<< env.get("SERVER_SIGNATURE") << "</body></html>\n"; |
63 |
Douglas Thrift |
121 |
} |
64 |
Douglas Thrift |
111 |
} |
65 |
douglas |
340 |
else api::Cout << "Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n"; |
66 |
Douglas Thrift |
109 |
} |
67 |
Douglas Thrift |
114 |
|
68 |
Douglas Thrift |
266 |
void Bender::bend(const ext::String& path, const ext::String& agent) |
69 |
Douglas Thrift |
114 |
{ |
70 |
|
|
Matcher matcher; |
71 |
|
|
|
72 |
douglas |
340 |
if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)")) bend(path); else if (agent == matcher("rv:(\\d+)\\.(\\d+).*\\) Gecko")) |
73 |
Douglas Thrift |
114 |
{ |
74 |
Douglas Thrift |
266 |
int major_(lexical_cast<int>(matcher[1])), minor_(lexical_cast<int>(matcher[2])); |
75 |
Douglas Thrift |
117 |
|
76 |
douglas |
340 |
if (major_ > 1 || (major_ == 1 && minor_ >= 5)) pass(path); else bend(path); |
77 |
Douglas Thrift |
114 |
} |
78 |
Douglas Thrift |
266 |
else bend(path); |
79 |
Douglas Thrift |
114 |
} |
80 |
|
|
|
81 |
Douglas Thrift |
266 |
void Bender::bend(const ext::String& path) |
82 |
Douglas Thrift |
114 |
{ |
83 |
Douglas Thrift |
273 |
_L<ext::String> args(1, "-a"); |
84 |
Douglas Thrift |
272 |
|
85 |
|
|
args.InsertLast(path); |
86 |
|
|
|
87 |
douglas |
311 |
#if defined(__FreeBSD__) |
88 |
Douglas Thrift |
273 |
_S<api::Process> xslt("/usr/local/bin/Xalan", args); |
89 |
douglas |
311 |
#elif defined(_WIN32) |
90 |
|
|
_S<api::Process> xslt("Xalan.exe", args); |
91 |
|
|
#else |
92 |
|
|
_S<api::Process> xslt("Xalan", args); |
93 |
|
|
#endif |
94 |
Douglas Thrift |
266 |
ios::String output; |
95 |
Douglas Thrift |
116 |
|
96 |
Douglas Thrift |
266 |
ios::ReadToWrite(*xslt.GetReader(), output); |
97 |
|
|
|
98 |
|
|
if (!output.IsEmpty()) |
99 |
Douglas Thrift |
116 |
{ |
100 |
Douglas Thrift |
266 |
ext::String type("text/xml"); |
101 |
douglas |
335 |
Matcher matcher("http-equiv=\"Content-Type\" content=\"(.*)\"", PCRE_MULTILINE); |
102 |
Douglas Thrift |
116 |
|
103 |
Douglas Thrift |
266 |
if (matcher == output) type = matcher[1]; |
104 |
Douglas Thrift |
119 |
|
105 |
Douglas Thrift |
266 |
api::Cout << "Content-Type: " << type << "\r\n\r\n" << output; |
106 |
Douglas Thrift |
116 |
} |
107 |
Douglas Thrift |
266 |
else pass(path); |
108 |
Douglas Thrift |
271 |
|
109 |
|
|
xslt.Join(); |
110 |
Douglas Thrift |
114 |
} |
111 |
Douglas Thrift |
116 |
|
112 |
Douglas Thrift |
266 |
void Bender::pass(const ext::String& path) |
113 |
Douglas Thrift |
116 |
{ |
114 |
Douglas Thrift |
266 |
api::Cout << "Content-Type: text/xml\r\n\r\n"; |
115 |
Douglas Thrift |
116 |
|
116 |
Douglas Thrift |
266 |
api::FileReader file(path); |
117 |
Douglas Thrift |
116 |
|
118 |
Douglas Thrift |
266 |
ios::ReadToWrite(file, api::Cout); |
119 |
Douglas Thrift |
116 |
} |