// Bender // // Douglas Thrift // // $Id$ #include "Bender.hpp" #include "Matcher.hpp" #include #include #include XALAN_USING_XERCES(XMLPlatformUtils) XALAN_USING_XALAN(XalanTransformer) int main(int argc, char* argv[]) { XMLPlatformUtils::Initialize(); XalanTransformer::initialize(); Bender bender; XalanTransformer::terminate(); XMLPlatformUtils::Terminate(); XalanTransformer::ICUCleanUp(); return 0; } Bender::Bender() { string path = sgetenv("PATH_TRANSLATED"); if (path != "") { ifstream file(path.c_str()); if (file.is_open()) { file.close(); bend(path, sgetenv("HTTP_USER_AGENT")); } else { cout << "Status: 404\n" << "Content-Type: text/html; charset=ISO-8859-1\n\n" << "\n" << "\n" << "404 Not Found\n" << "\n" << "

Not Found

\n" << "

The requested URL " << sgetenv("PATH_INFO") << " was no" << "t found on this server.

\n" << "
\n" << sgetenv("SERVER_SIGNATURE") << "\n"; } } else { cout << "Location: http://computers.douglasthrift.net/bender.xml\n\n"; } } void Bender::bend(const string& path, const string& agent) { Matcher matcher; if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)")) { bend(path); } else if (agent == matcher(string("^Mozilla/4.0 \\(compatible; MSIE (\\d+)") + "\\.(\\d+)(\\w+)?; .*\\)$")) { int version; istringstream number(matcher[1]); number >> version; if (version >= 6) { pass(path); } else { bend(path); } } else if (agent == matcher(string("^Mozilla/5.0 \\(.*; rv:(\\d+)\\.(\\d+)(") + "\\.d+)?(\\w+)?\\) Gecko\\/.*")) { int major, minor; istringstream number(matcher[1] + ' ' + matcher[2]); number >> major; number >> minor; if (major > 1 || (major == 1 && minor >= 5)) { pass(path); } else { bend(path); } } else { bend(path); } } void Bender::bend(const string& path) { ostringstream output; XalanTransformer transformer; if (transformer.transform(path.c_str(), output) == 0) { string type = "text/html; charset=ISO-8859-1"; Matcher matcher(""); if (matcher == output.str()) { type = matcher[1]; } cout << "Content-Type: " << type << "\n\n" << output.str(); } else { pass(path); } } void Bender::pass(const string& path) { cout << "Content-Type: text/xml; charset=ISO-8859-1\n\n"; ifstream fin(path.c_str()); string line; do { getline(fin, line); cout << line << '\n'; } while (fin.good()); }