1 |
// Bender |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Bender.hpp" |
8 |
#include "Matcher.hpp" |
9 |
|
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 |
struct |
16 |
{ |
17 |
ext::String get(const ext::String& name) |
18 |
{ |
19 |
try |
20 |
{ |
21 |
return api::TheEnvironment.Get(name); |
22 |
} |
23 |
catch (ext::Exception) |
24 |
{ |
25 |
return ext::String(); |
26 |
} |
27 |
} |
28 |
} env; |
29 |
|
30 |
int Main(const app::Options& options) |
31 |
{ |
32 |
Bender bender; |
33 |
|
34 |
return 0; |
35 |
} |
36 |
|
37 |
Bender::Bender() |
38 |
{ |
39 |
ext::String path(env.get("PATH_TRANSLATED")); |
40 |
Matcher matcher("^" + env.get("SCRIPT_NAME")); |
41 |
|
42 |
if (!path.IsEmpty() && env.get("REQUEST_URI") != matcher) |
43 |
{ |
44 |
try |
45 |
{ |
46 |
api::FileReader file(path); |
47 |
|
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 |
} |
64 |
} |
65 |
else |
66 |
{ |
67 |
api::Cout << "Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n"; |
68 |
} |
69 |
} |
70 |
|
71 |
void Bender::bend(const ext::String& path, const ext::String& agent) |
72 |
{ |
73 |
Matcher matcher; |
74 |
|
75 |
if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)")) |
76 |
{ |
77 |
bend(path); |
78 |
} |
79 |
else if (agent == matcher("rv:(\\d+)\\.(\\d+).*\\) Gecko")) |
80 |
{ |
81 |
int major_(lexical_cast<int>(matcher[1])), minor_(lexical_cast<int>(matcher[2])); |
82 |
|
83 |
if (major_ > 1 || (major_ == 1 && minor_ >= 5)) |
84 |
{ |
85 |
pass(path); |
86 |
} |
87 |
else bend(path); |
88 |
} |
89 |
else bend(path); |
90 |
} |
91 |
|
92 |
void Bender::bend(const ext::String& path) |
93 |
{ |
94 |
_L<ext::String> args(1, "-IN"); |
95 |
|
96 |
args.InsertLast(path); |
97 |
|
98 |
_S<api::Process> xslt("/usr/local/bin/xalan-j", args); |
99 |
ios::String output; |
100 |
|
101 |
ios::ReadToWrite(*xslt.GetReader(), output); |
102 |
|
103 |
if (!output.IsEmpty()) |
104 |
{ |
105 |
ext::String type("text/xml"); |
106 |
Matcher matcher("http-equiv=\"Content-Type\" content=\"(.*)\""); |
107 |
|
108 |
if (matcher == output) type = matcher[1]; |
109 |
|
110 |
api::Cout << "Content-Type: " << type << "\r\n\r\n" << output; |
111 |
} |
112 |
else pass(path); |
113 |
|
114 |
xslt.Join(); |
115 |
} |
116 |
|
117 |
void Bender::pass(const ext::String& path) |
118 |
{ |
119 |
api::Cout << "Content-Type: text/xml\r\n\r\n"; |
120 |
|
121 |
api::FileReader file(path); |
122 |
|
123 |
ios::ReadToWrite(file, api::Cout); |
124 |
} |