1 |
Douglas Thrift |
109 |
// Bender |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include "Bender.hpp" |
8 |
douglas |
320 |
#include "Matcher/Matcher.hpp" |
9 |
Douglas Thrift |
109 |
|
10 |
Douglas Thrift |
266 |
#include <menes-api/exename.hpp> |
11 |
|
|
#include <menes-api/files.hpp> |
12 |
|
|
#include <menes-api/process.hpp> |
13 |
|
|
#include <menes-app/simple.hpp> |
14 |
Douglas Thrift |
113 |
|
15 |
Douglas Thrift |
271 |
struct |
16 |
Douglas Thrift |
109 |
{ |
17 |
Douglas Thrift |
268 |
ext::String get(const ext::String& name) |
18 |
Douglas Thrift |
266 |
{ |
19 |
Douglas Thrift |
268 |
try |
20 |
|
|
{ |
21 |
|
|
return api::TheEnvironment.Get(name); |
22 |
|
|
} |
23 |
|
|
catch (ext::Exception) |
24 |
|
|
{ |
25 |
|
|
return ext::String(); |
26 |
|
|
} |
27 |
Douglas Thrift |
266 |
} |
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 Thrift |
121 |
else |
66 |
|
|
{ |
67 |
Douglas Thrift |
266 |
api::Cout << "Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n"; |
68 |
Douglas Thrift |
121 |
} |
69 |
Douglas Thrift |
109 |
} |
70 |
Douglas Thrift |
114 |
|
71 |
Douglas Thrift |
266 |
void Bender::bend(const ext::String& path, const ext::String& agent) |
72 |
Douglas Thrift |
114 |
{ |
73 |
|
|
Matcher matcher; |
74 |
|
|
|
75 |
Douglas Thrift |
116 |
if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)")) |
76 |
Douglas Thrift |
114 |
{ |
77 |
Douglas Thrift |
116 |
bend(path); |
78 |
Douglas Thrift |
114 |
} |
79 |
Douglas Thrift |
266 |
else if (agent == matcher("rv:(\\d+)\\.(\\d+).*\\) Gecko")) |
80 |
Douglas Thrift |
114 |
{ |
81 |
Douglas Thrift |
266 |
int major_(lexical_cast<int>(matcher[1])), minor_(lexical_cast<int>(matcher[2])); |
82 |
Douglas Thrift |
117 |
|
83 |
Douglas Thrift |
266 |
if (major_ > 1 || (major_ == 1 && minor_ >= 5)) |
84 |
Douglas Thrift |
117 |
{ |
85 |
|
|
pass(path); |
86 |
|
|
} |
87 |
Douglas Thrift |
266 |
else bend(path); |
88 |
Douglas Thrift |
114 |
} |
89 |
Douglas Thrift |
266 |
else bend(path); |
90 |
Douglas Thrift |
114 |
} |
91 |
|
|
|
92 |
Douglas Thrift |
266 |
void Bender::bend(const ext::String& path) |
93 |
Douglas Thrift |
114 |
{ |
94 |
Douglas Thrift |
273 |
_L<ext::String> args(1, "-a"); |
95 |
Douglas Thrift |
272 |
|
96 |
|
|
args.InsertLast(path); |
97 |
|
|
|
98 |
douglas |
311 |
#if defined(__FreeBSD__) |
99 |
Douglas Thrift |
273 |
_S<api::Process> xslt("/usr/local/bin/Xalan", args); |
100 |
douglas |
311 |
#elif defined(_WIN32) |
101 |
|
|
_S<api::Process> xslt("Xalan.exe", args); |
102 |
|
|
#else |
103 |
|
|
_S<api::Process> xslt("Xalan", args); |
104 |
|
|
#endif |
105 |
Douglas Thrift |
266 |
ios::String output; |
106 |
Douglas Thrift |
116 |
|
107 |
Douglas Thrift |
266 |
ios::ReadToWrite(*xslt.GetReader(), output); |
108 |
|
|
|
109 |
|
|
if (!output.IsEmpty()) |
110 |
Douglas Thrift |
116 |
{ |
111 |
Douglas Thrift |
266 |
ext::String type("text/xml"); |
112 |
Douglas Thrift |
123 |
Matcher matcher("http-equiv=\"Content-Type\" content=\"(.*)\""); |
113 |
Douglas Thrift |
116 |
|
114 |
Douglas Thrift |
266 |
if (matcher == output) type = matcher[1]; |
115 |
Douglas Thrift |
119 |
|
116 |
Douglas Thrift |
266 |
api::Cout << "Content-Type: " << type << "\r\n\r\n" << output; |
117 |
Douglas Thrift |
116 |
} |
118 |
Douglas Thrift |
266 |
else pass(path); |
119 |
Douglas Thrift |
271 |
|
120 |
|
|
xslt.Join(); |
121 |
Douglas Thrift |
114 |
} |
122 |
Douglas Thrift |
116 |
|
123 |
Douglas Thrift |
266 |
void Bender::pass(const ext::String& path) |
124 |
Douglas Thrift |
116 |
{ |
125 |
Douglas Thrift |
266 |
api::Cout << "Content-Type: text/xml\r\n\r\n"; |
126 |
Douglas Thrift |
116 |
|
127 |
Douglas Thrift |
266 |
api::FileReader file(path); |
128 |
Douglas Thrift |
116 |
|
129 |
Douglas Thrift |
266 |
ios::ReadToWrite(file, api::Cout); |
130 |
Douglas Thrift |
116 |
} |