4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
< |
#include "Bender.hpp" |
8 |
< |
#include "Matcher.hpp" |
7 |
> |
#include <cxx/standard.hh> |
8 |
|
|
9 |
< |
#include <menes-api/exename.hpp> |
10 |
< |
#include <menes-api/files.hpp> |
11 |
< |
#include <menes-api/process.hpp> |
12 |
< |
#include <menes-app/simple.hpp> |
9 |
> |
#include <api/pcre/regex.hpp> |
10 |
> |
#include <api/environment.hpp> |
11 |
> |
#include <api/files.hpp> |
12 |
> |
#include <api/process.hpp> |
13 |
> |
#include <app/simple.hpp> |
14 |
> |
#include <ios/helpers.hpp> |
15 |
> |
#include <xml/document.hpp> |
16 |
> |
#include <xml/nodeset.hpp> |
17 |
> |
#include <xml/parse.hpp> |
18 |
|
|
19 |
< |
int Main(const app::Options& options) |
19 |
> |
class Bender |
20 |
> |
{ |
21 |
> |
private: |
22 |
> |
void bend(const cse::String& path, const cse::String& agent); |
23 |
> |
void bend(const cse::String& path); |
24 |
> |
void pass(const cse::String& path); |
25 |
> |
public: |
26 |
> |
Bender(); |
27 |
> |
}; |
28 |
> |
|
29 |
> |
struct Environment |
30 |
|
{ |
31 |
< |
try |
31 |
> |
cse::String get(const cse::String& name) |
32 |
|
{ |
33 |
< |
Bender bender; |
33 |
> |
try |
34 |
> |
{ |
35 |
> |
return api::TheEnvironment.Get(name); |
36 |
> |
} |
37 |
> |
catch (ext::Exception) |
38 |
> |
{ |
39 |
> |
return cse::String(); |
40 |
> |
} |
41 |
|
} |
42 |
< |
catch (ext::Exception e) {} |
42 |
> |
} env; |
43 |
> |
|
44 |
> |
int Main(const app::Options& options) |
45 |
> |
{ |
46 |
> |
Bender bender; |
47 |
|
|
48 |
|
return 0; |
49 |
|
} |
50 |
|
|
51 |
|
Bender::Bender() |
52 |
|
{ |
53 |
< |
ext::String path(api::TheEnvironment.Get("PATH_TRANSLATED")); |
54 |
< |
Matcher matcher("^" + api::TheEnvironment.Get("SCRIPT_NAME")); |
53 |
> |
cse::String path(env.get(_B("PATH_TRANSLATED"))); |
54 |
> |
api::Pcre::RegEx script(_S<ios::String>() << _B("^") << env.get(_B("SCRIPT_NAME"))); |
55 |
|
|
56 |
< |
if (!path.IsEmpty() && api::TheEnvironment.Get("REQUEST_URI") != matcher) |
56 |
> |
if (!path.IsEmpty() && !script(env.get(_B("REQUEST_URI")))) |
57 |
|
{ |
58 |
|
try |
59 |
|
{ |
60 |
< |
api::FileReader file(path); |
60 |
> |
_S<api::FileReader> file(path); |
61 |
|
|
62 |
< |
bend(path, api::TheEnvironment.Get("HTTP_USER_AGENT")); |
62 |
> |
bend(path, env.get(_B("HTTP_USER_AGENT"))); |
63 |
|
} |
64 |
< |
catch (ext::Exception e) |
64 |
> |
catch (ext::Exception) |
65 |
|
{ |
66 |
< |
api::Cout << "Status: 404\r\n" |
67 |
< |
<< "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n" |
68 |
< |
<< "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" |
69 |
< |
<< "<html><head>\n" |
70 |
< |
<< "<title>404 Not Found</title>\n" |
71 |
< |
<< "</head><body>\n" |
72 |
< |
<< "<h1>Not Found</h1>\n" |
73 |
< |
<< "<p>The requested URL " |
74 |
< |
<< api::TheEnvironment.Get("PATH_INFO") |
75 |
< |
<< " was not found on this server.</p>\n" |
76 |
< |
<< "<hr />\n" |
52 |
< |
<< api::TheEnvironment.Get("SERVER_SIGNATURE") |
53 |
< |
<< "</body></html>\n"; |
66 |
> |
api::Cout << _B("Status: 404\r\n") |
67 |
> |
<< _B("Content-Type: text/html; charset=ISO-8859-1\r\n\r\n") |
68 |
> |
<< _B("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n") |
69 |
> |
<< _B("<html><head>\n") |
70 |
> |
<< _B("<title>404 Not Found</title>\n") |
71 |
> |
<< _B("</head><body>\n") |
72 |
> |
<< _B("<h1>Not Found</h1>\n") |
73 |
> |
<< _B("<p>The requested URL ") << env.get(_B("PATH_INFO")) |
74 |
> |
<< _B(" was not found on this server.</p>\n") |
75 |
> |
<< _B("<hr />\n") |
76 |
> |
<< env.get(_B("SERVER_SIGNATURE")) << _B("</body></html>\n"); |
77 |
|
} |
78 |
|
} |
79 |
|
else |
80 |
< |
{ |
58 |
< |
api::Cout << "Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n"; |
59 |
< |
} |
80 |
> |
api::Cout << _B("Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n"); |
81 |
|
} |
82 |
|
|
83 |
< |
void Bender::bend(const ext::String& path, const ext::String& agent) |
83 |
> |
void Bender::bend(const cse::String& path, const cse::String& agent) |
84 |
|
{ |
85 |
< |
Matcher matcher; |
85 |
> |
api::Pcre::RegEx opera(_B("Opera[ /]\\d+\\.\\d+")), gecko(_B("rv:(\\d+)\\.(\\d+).*\\) Gecko")); |
86 |
|
|
87 |
< |
if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)")) |
67 |
< |
{ |
87 |
> |
if (opera(agent)) |
88 |
|
bend(path); |
89 |
< |
} |
70 |
< |
else if (agent == matcher("rv:(\\d+)\\.(\\d+).*\\) Gecko")) |
89 |
> |
else if (api::Pcre::RegEx::Match match = gecko(agent)) |
90 |
|
{ |
91 |
< |
int major_(lexical_cast<int>(matcher[1])), minor_(lexical_cast<int>(matcher[2])); |
91 |
> |
int major_(lexical_cast<int>(match[1])), minor_(lexical_cast<int>(match[2])); |
92 |
|
|
93 |
|
if (major_ > 1 || (major_ == 1 && minor_ >= 5)) |
75 |
– |
{ |
94 |
|
pass(path); |
95 |
< |
} |
96 |
< |
else bend(path); |
95 |
> |
else |
96 |
> |
bend(path); |
97 |
|
} |
98 |
< |
else bend(path); |
98 |
> |
else |
99 |
> |
bend(path); |
100 |
|
} |
101 |
|
|
102 |
< |
void Bender::bend(const ext::String& path) |
102 |
> |
void Bender::bend(const cse::String& path) |
103 |
|
{ |
104 |
< |
_L<ext::String> args(1, path); |
105 |
< |
_S<api::Process> xslt("/usr/local/bin/xsltproc", args); |
106 |
< |
ios::String output; |
104 |
> |
_R<xml::Document> document(xml::Parse(_B("bender.xml"))); |
105 |
> |
_R<xml::Node> node(*document/_B("bender")); |
106 |
> |
cse::String program(*node/_B("program")); |
107 |
> |
_L<cse::String> args; |
108 |
|
|
109 |
< |
ios::ReadToWrite(*xslt.GetReader(), output); |
109 |
> |
_foreach (const xml::NodeSet, arg, *node/_B("argument")) |
110 |
> |
args.InsertLast(**arg); |
111 |
|
|
112 |
< |
if (!output.IsEmpty()) |
112 |
> |
args.InsertLast(path); |
113 |
> |
|
114 |
> |
_S<api::Process> xslt(program, args); |
115 |
> |
ext::Buffer line; |
116 |
> |
api::Pcre::RegEx content("http-equiv=\"Content-Type\" content=\"(.*)\""); |
117 |
> |
_S<ios::String> output; |
118 |
> |
cse::String type(_B("text/xml")); |
119 |
> |
|
120 |
> |
while (ios::ReadLine(*xslt.GetReader(), line)) |
121 |
|
{ |
122 |
< |
ext::String type("text/xml"); |
123 |
< |
Matcher matcher("http-equiv=\"Content-Type\" content=\"(.*)\""); |
122 |
> |
output << line << ios::NewLine; |
123 |
> |
|
124 |
> |
if (api::Pcre::RegEx::Match match = content(output)) |
125 |
> |
{ |
126 |
> |
type = match[1]; |
127 |
> |
|
128 |
> |
break; |
129 |
> |
} |
130 |
> |
} |
131 |
|
|
132 |
< |
if (matcher == output) type = matcher[1]; |
132 |
> |
ios::ReadToWrite(*xslt.GetReader(), output); |
133 |
|
|
134 |
+ |
if (!output.IsEmpty()) |
135 |
|
api::Cout << "Content-Type: " << type << "\r\n\r\n" << output; |
136 |
< |
} |
137 |
< |
else pass(path); |
136 |
> |
else |
137 |
> |
pass(path); |
138 |
|
} |
139 |
|
|
140 |
< |
void Bender::pass(const ext::String& path) |
140 |
> |
void Bender::pass(const cse::String& path) |
141 |
|
{ |
142 |
< |
api::Cout << "Content-Type: text/xml\r\n\r\n"; |
142 |
> |
api::Cout << _B("Content-Type: text/xml\r\n\r\n"); |
143 |
|
|
144 |
< |
api::FileReader file(path); |
144 |
> |
_S<api::FileReader> file(path); |
145 |
|
|
146 |
|
ios::ReadToWrite(file, api::Cout); |
147 |
|
} |