1 |
Douglas Thrift |
109 |
// Bender |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include "Bender.hpp" |
8 |
Douglas Thrift |
113 |
#include "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 |
266 |
int Main(const app::Options& options) |
16 |
Douglas Thrift |
109 |
{ |
17 |
Douglas Thrift |
266 |
try |
18 |
|
|
{ |
19 |
|
|
Bender bender; |
20 |
|
|
} |
21 |
|
|
catch (ext::Exception e) {} |
22 |
Douglas Thrift |
119 |
|
23 |
Douglas Thrift |
109 |
return 0; |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
Bender::Bender() |
27 |
|
|
{ |
28 |
Douglas Thrift |
266 |
ext::String path(api::TheEnvironment.Get("PATH_TRANSLATED")); |
29 |
|
|
Matcher matcher("^" + api::TheEnvironment.Get("SCRIPT_NAME")); |
30 |
Douglas Thrift |
111 |
|
31 |
Douglas Thrift |
266 |
if (!path.IsEmpty() && api::TheEnvironment.Get("REQUEST_URI") != matcher) |
32 |
Douglas Thrift |
111 |
{ |
33 |
Douglas Thrift |
266 |
try |
34 |
Douglas Thrift |
119 |
{ |
35 |
Douglas Thrift |
266 |
api::FileReader file(path); |
36 |
Douglas Thrift |
119 |
|
37 |
Douglas Thrift |
266 |
bend(path, api::TheEnvironment.Get("HTTP_USER_AGENT")); |
38 |
Douglas Thrift |
119 |
} |
39 |
Douglas Thrift |
266 |
catch (ext::Exception e) |
40 |
Douglas Thrift |
121 |
{ |
41 |
Douglas Thrift |
266 |
api::Cout << "Status: 404\r\n" |
42 |
|
|
<< "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n" |
43 |
Douglas Thrift |
121 |
<< "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" |
44 |
|
|
<< "<html><head>\n" |
45 |
|
|
<< "<title>404 Not Found</title>\n" |
46 |
|
|
<< "</head><body>\n" |
47 |
|
|
<< "<h1>Not Found</h1>\n" |
48 |
Douglas Thrift |
266 |
<< "<p>The requested URL " |
49 |
|
|
<< api::TheEnvironment.Get("PATH_INFO") |
50 |
|
|
<< " was not found on this server.</p>\n" |
51 |
Douglas Thrift |
121 |
<< "<hr />\n" |
52 |
Douglas Thrift |
266 |
<< api::TheEnvironment.Get("SERVER_SIGNATURE") |
53 |
Douglas Thrift |
121 |
<< "</body></html>\n"; |
54 |
|
|
} |
55 |
Douglas Thrift |
111 |
} |
56 |
Douglas Thrift |
121 |
else |
57 |
|
|
{ |
58 |
Douglas Thrift |
266 |
api::Cout << "Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n"; |
59 |
Douglas Thrift |
121 |
} |
60 |
Douglas Thrift |
109 |
} |
61 |
Douglas Thrift |
114 |
|
62 |
Douglas Thrift |
266 |
void Bender::bend(const ext::String& path, const ext::String& agent) |
63 |
Douglas Thrift |
114 |
{ |
64 |
|
|
Matcher matcher; |
65 |
|
|
|
66 |
Douglas Thrift |
116 |
if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)")) |
67 |
Douglas Thrift |
114 |
{ |
68 |
Douglas Thrift |
116 |
bend(path); |
69 |
Douglas Thrift |
114 |
} |
70 |
Douglas Thrift |
266 |
else if (agent == matcher("rv:(\\d+)\\.(\\d+).*\\) Gecko")) |
71 |
Douglas Thrift |
114 |
{ |
72 |
Douglas Thrift |
266 |
int major_(lexical_cast<int>(matcher[1])), minor_(lexical_cast<int>(matcher[2])); |
73 |
Douglas Thrift |
117 |
|
74 |
Douglas Thrift |
266 |
if (major_ > 1 || (major_ == 1 && minor_ >= 5)) |
75 |
Douglas Thrift |
117 |
{ |
76 |
|
|
pass(path); |
77 |
|
|
} |
78 |
Douglas Thrift |
266 |
else bend(path); |
79 |
Douglas Thrift |
114 |
} |
80 |
Douglas Thrift |
266 |
else bend(path); |
81 |
Douglas Thrift |
114 |
} |
82 |
|
|
|
83 |
Douglas Thrift |
266 |
void Bender::bend(const ext::String& path) |
84 |
Douglas Thrift |
114 |
{ |
85 |
Douglas Thrift |
266 |
_L<ext::String> args(1, path); |
86 |
|
|
_S<api::Process> xslt("/usr/local/bin/xsltproc", args); |
87 |
|
|
ios::String output; |
88 |
Douglas Thrift |
116 |
|
89 |
Douglas Thrift |
266 |
ios::ReadToWrite(*xslt.GetReader(), output); |
90 |
|
|
|
91 |
|
|
if (!output.IsEmpty()) |
92 |
Douglas Thrift |
116 |
{ |
93 |
Douglas Thrift |
266 |
ext::String type("text/xml"); |
94 |
Douglas Thrift |
123 |
Matcher matcher("http-equiv=\"Content-Type\" content=\"(.*)\""); |
95 |
Douglas Thrift |
116 |
|
96 |
Douglas Thrift |
266 |
if (matcher == output) type = matcher[1]; |
97 |
Douglas Thrift |
119 |
|
98 |
Douglas Thrift |
266 |
api::Cout << "Content-Type: " << type << "\r\n\r\n" << output; |
99 |
Douglas Thrift |
116 |
} |
100 |
Douglas Thrift |
266 |
else pass(path); |
101 |
Douglas Thrift |
114 |
} |
102 |
Douglas Thrift |
116 |
|
103 |
Douglas Thrift |
266 |
void Bender::pass(const ext::String& path) |
104 |
Douglas Thrift |
116 |
{ |
105 |
Douglas Thrift |
266 |
api::Cout << "Content-Type: text/xml\r\n\r\n"; |
106 |
Douglas Thrift |
116 |
|
107 |
Douglas Thrift |
266 |
api::FileReader file(path); |
108 |
Douglas Thrift |
116 |
|
109 |
Douglas Thrift |
266 |
ios::ReadToWrite(file, api::Cout); |
110 |
Douglas Thrift |
116 |
} |