4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
< |
#include "Bender.hpp" |
8 |
< |
#include "Matcher/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 |
> |
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 |
29 |
> |
struct Environment |
30 |
|
{ |
31 |
< |
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 |
< |
} |
31 |
> |
cse::String get(const cse::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return cse::String(); } } |
32 |
|
} env; |
33 |
|
|
34 |
|
int Main(const app::Options& options) |
40 |
|
|
41 |
|
Bender::Bender() |
42 |
|
{ |
43 |
< |
ext::String path(env.get("PATH_TRANSLATED")); |
44 |
< |
Matcher matcher("^" + env.get("SCRIPT_NAME")); |
43 |
> |
cse::String path(env.get(_B("PATH_TRANSLATED"))); |
44 |
> |
api::Pcre::RegEx script(_S<ios::String>() << _B("^") << env.get(_B("SCRIPT_NAME"))); |
45 |
|
|
46 |
< |
if (!path.IsEmpty() && env.get("REQUEST_URI") != matcher) |
46 |
> |
if (!path.IsEmpty() && script(env.get(_B("REQUEST_URI")))) |
47 |
|
{ |
48 |
|
try |
49 |
|
{ |
50 |
< |
api::FileReader file(path); |
50 |
> |
_S<api::FileReader> file(path); |
51 |
|
|
52 |
< |
bend(path, env.get("HTTP_USER_AGENT")); |
52 |
> |
bend(path, env.get(_B("HTTP_USER_AGENT"))); |
53 |
|
} |
54 |
< |
catch (ext::Exception e) |
54 |
> |
catch (ext::Exception) |
55 |
|
{ |
56 |
< |
api::Cout << "Status: 404\r\n" |
57 |
< |
<< "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n" |
58 |
< |
<< "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" |
59 |
< |
<< "<html><head>\n" |
60 |
< |
<< "<title>404 Not Found</title>\n" |
61 |
< |
<< "</head><body>\n" |
62 |
< |
<< "<h1>Not Found</h1>\n" |
63 |
< |
<< "<p>The requested URL " << env.get("PATH_INFO") |
64 |
< |
<< " was not found on this server.</p>\n" |
65 |
< |
<< "<hr />\n" |
66 |
< |
<< env.get("SERVER_SIGNATURE") << "</body></html>\n"; |
56 |
> |
api::Cout << _B("Status: 404\r\n") |
57 |
> |
<< _B("Content-Type: text/html; charset=ISO-8859-1\r\n\r\n") |
58 |
> |
<< _B("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n") |
59 |
> |
<< _B("<html><head>\n") |
60 |
> |
<< _B("<title>404 Not Found</title>\n") |
61 |
> |
<< _B("</head><body>\n") |
62 |
> |
<< _B("<h1>Not Found</h1>\n") |
63 |
> |
<< _B("<p>The requested URL ") << env.get(_B("PATH_INFO")) |
64 |
> |
<< _B(" was not found on this server.</p>\n") |
65 |
> |
<< _B("<hr />\n") |
66 |
> |
<< env.get(_B("SERVER_SIGNATURE")) << _B("</body></html>\n"); |
67 |
|
} |
68 |
|
} |
69 |
< |
else |
66 |
< |
{ |
67 |
< |
api::Cout << "Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n"; |
68 |
< |
} |
69 |
> |
else api::Cout << _B("Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n"); |
70 |
|
} |
71 |
|
|
72 |
< |
void Bender::bend(const ext::String& path, const ext::String& agent) |
72 |
> |
void Bender::bend(const cse::String& path, const cse::String& agent) |
73 |
|
{ |
74 |
< |
Matcher matcher; |
74 |
> |
api::Pcre::RegEx opera(_B("Opera[ /]\\d+\\.\\d+")), gecko(_B("rv:(\\d+)\\.(\\d+).*\\) Gecko")); |
75 |
|
|
76 |
< |
if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)")) |
76 |
< |
{ |
76 |
> |
if (opera(agent)) |
77 |
|
bend(path); |
78 |
< |
} |
79 |
< |
else if (agent == matcher("rv:(\\d+)\\.(\\d+).*\\) Gecko")) |
78 |
> |
else if (api::Pcre::RegEx::Match match = gecko(agent)) |
79 |
|
{ |
80 |
< |
int major_(lexical_cast<int>(matcher[1])), minor_(lexical_cast<int>(matcher[2])); |
80 |
> |
int major_(lexical_cast<int>(match[1])), minor_(lexical_cast<int>(match[2])); |
81 |
|
|
82 |
|
if (major_ > 1 || (major_ == 1 && minor_ >= 5)) |
84 |
– |
{ |
83 |
|
pass(path); |
84 |
< |
} |
85 |
< |
else bend(path); |
84 |
> |
else |
85 |
> |
bend(path); |
86 |
|
} |
87 |
< |
else bend(path); |
87 |
> |
else |
88 |
> |
bend(path); |
89 |
|
} |
90 |
|
|
91 |
< |
void Bender::bend(const ext::String& path) |
91 |
> |
void Bender::bend(const cse::String& path) |
92 |
|
{ |
93 |
< |
_L<ext::String> args(1, "-a"); |
93 |
> |
_R<xml::Document> document(xml::Parse(_B("bender.xml"))); |
94 |
> |
_R<xml::Node> node(*document/_B("bender")); |
95 |
> |
cse::String program(*node/_B("program")); |
96 |
> |
_L<cse::String> args; |
97 |
> |
|
98 |
> |
_foreach (const xml::NodeSet, arg, *node/_B("argument")) |
99 |
> |
args.InsertLast(**arg); |
100 |
|
|
101 |
|
args.InsertLast(path); |
102 |
|
|
103 |
< |
#if defined(__FreeBSD__) |
104 |
< |
_S<api::Process> xslt("/usr/local/bin/Xalan", args); |
100 |
< |
#elif defined(_WIN32) |
101 |
< |
_S<api::Process> xslt("Xalan.exe", args); |
102 |
< |
#else |
103 |
< |
_S<api::Process> xslt("Xalan", args); |
104 |
< |
#endif |
105 |
< |
ios::String output; |
103 |
> |
_S<api::Process> xslt(program, args); |
104 |
> |
_S<ios::String> output; |
105 |
|
|
106 |
|
ios::ReadToWrite(*xslt.GetReader(), output); |
107 |
|
|
108 |
|
if (!output.IsEmpty()) |
109 |
|
{ |
110 |
< |
ext::String type("text/xml"); |
111 |
< |
Matcher matcher("http-equiv=\"Content-Type\" content=\"(.*)\""); |
110 |
> |
cse::String type(_B("text/xml")); |
111 |
> |
api::Pcre::RegEx content("http-equiv=\"Content-Type\" content=\"(.*)\""); |
112 |
|
|
113 |
< |
if (matcher == output) type = matcher[1]; |
113 |
> |
if (api::Pcre::RegEx::Match match = content(output)) |
114 |
> |
type = match[1]; |
115 |
|
|
116 |
|
api::Cout << "Content-Type: " << type << "\r\n\r\n" << output; |
117 |
|
} |
118 |
< |
else pass(path); |
119 |
< |
|
120 |
< |
xslt.Join(); |
118 |
> |
else |
119 |
> |
pass(path); |
120 |
|
} |
121 |
|
|
122 |
< |
void Bender::pass(const ext::String& path) |
122 |
> |
void Bender::pass(const cse::String& path) |
123 |
|
{ |
124 |
< |
api::Cout << "Content-Type: text/xml\r\n\r\n"; |
124 |
> |
api::Cout << _B("Content-Type: text/xml\r\n\r\n"); |
125 |
|
|
126 |
< |
api::FileReader file(path); |
126 |
> |
_S<api::FileReader> file(path); |
127 |
|
|
128 |
|
ios::ReadToWrite(file, api::Cout); |
129 |
|
} |