ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Bender/Bender.cpp
Revision: 376
Committed: 2004-12-22T18:23:27-08:00 (20 years, 6 months ago) by douglas
File size: 2760 byte(s)
Log Message:
Who needs an e?

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id