// Bender // // Douglas Thrift // // $Id$ #include "Bender.hpp" #include "Matcher/Matcher.hpp" #include #include #include #include struct Environment { ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } } } env; int Main(const app::Options& options) { Bender bender; return 0; } Bender::Bender() { ext::String path(env.get("PATH_TRANSLATED")); Matcher matcher("^" + env.get("SCRIPT_NAME")); if (!path.IsEmpty() && env.get("REQUEST_URI") != matcher) { try { api::FileReader file(path); bend(path, env.get("HTTP_USER_AGENT")); } catch (ext::Exception e) { api::Cout << "Status: 404\r\n" << "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n" << "\n" << "\n" << "404 Not Found\n" << "\n" << "

Not Found

\n" << "

The requested URL " << env.get("PATH_INFO") << " was not found on this server.

\n" << "
\n" << env.get("SERVER_SIGNATURE") << "\n"; } } else api::Cout << "Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n"; } void Bender::bend(const ext::String& path, const ext::String& agent) { Matcher matcher; if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)")) bend(path); else if (agent == matcher("rv:(\\d+)\\.(\\d+).*\\) Gecko")) { int major_(lexical_cast(matcher[1])), minor_(lexical_cast(matcher[2])); if (major_ > 1 || (major_ == 1 && minor_ >= 5)) pass(path); else bend(path); } else bend(path); } void Bender::bend(const ext::String& path) { _L args(1, "-a"); args.InsertLast(path); #if defined(__FreeBSD__) _S xslt("/usr/local/bin/Xalan", args); #elif defined(_WIN32) _S xslt("Xalan.exe", args); #else _S xslt("Xalan", args); #endif ios::String output; ios::ReadToWrite(*xslt.GetReader(), output); if (!output.IsEmpty()) { ext::String type("text/xml"); Matcher matcher("http-equiv=\"Content-Type\" content=\"(.*)\"", PCRE_MULTILINE); if (matcher == output) type = matcher[1]; api::Cout << "Content-Type: " << type << "\r\n\r\n" << output; } else pass(path); xslt.Join(); } void Bender::pass(const ext::String& path) { api::Cout << "Content-Type: text/xml\r\n\r\n"; api::FileReader file(path); ios::ReadToWrite(file, api::Cout); }