ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Bender/Bender.cpp
Revision: 397
Committed: 2004-12-26T18:51:15-08:00 (20 years, 5 months ago) by douglas
File size: 2904 byte(s)
Log Message:
Generalized!

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 397 #include <menes-xml/document.hpp>
16     #include <menes-xml/nodeset.hpp>
17     #include <menes-xml/parse.hpp>
18 Douglas Thrift 113
19 douglas 350 class Bender
20     {
21     private:
22     void bend(const ext::String& path, const ext::String& agent);
23     void bend(const ext::String& path);
24     void pass(const ext::String& path);
25     public:
26     Bender();
27     };
28    
29 douglas 339 struct Environment
30 Douglas Thrift 109 {
31 douglas 340 ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } }
32 Douglas Thrift 268 } env;
33 Douglas Thrift 119
34 Douglas Thrift 268 int Main(const app::Options& options)
35     {
36     Bender bender;
37    
38 Douglas Thrift 109 return 0;
39     }
40    
41     Bender::Bender()
42     {
43 Douglas Thrift 268 ext::String path(env.get("PATH_TRANSLATED"));
44     Matcher matcher("^" + env.get("SCRIPT_NAME"));
45 Douglas Thrift 111
46 Douglas Thrift 268 if (!path.IsEmpty() && env.get("REQUEST_URI") != matcher)
47 Douglas Thrift 111 {
48 Douglas Thrift 266 try
49 Douglas Thrift 119 {
50 Douglas Thrift 266 api::FileReader file(path);
51 Douglas Thrift 119
52 Douglas Thrift 268 bend(path, env.get("HTTP_USER_AGENT"));
53 Douglas Thrift 119 }
54 douglas 376 catch (ext::Exception)
55 Douglas Thrift 121 {
56 Douglas Thrift 266 api::Cout << "Status: 404\r\n"
57     << "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n"
58 Douglas Thrift 121 << "<!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 Douglas Thrift 268 << "<p>The requested URL " << env.get("PATH_INFO")
64 Douglas Thrift 266 << " was not found on this server.</p>\n"
65 Douglas Thrift 121 << "<hr />\n"
66 Douglas Thrift 268 << env.get("SERVER_SIGNATURE") << "</body></html>\n";
67 Douglas Thrift 121 }
68 Douglas Thrift 111 }
69 douglas 340 else api::Cout << "Location: http://computers.douglasthrift.net/bender.xml\r\n\r\n";
70 Douglas Thrift 109 }
71 Douglas Thrift 114
72 Douglas Thrift 266 void Bender::bend(const ext::String& path, const ext::String& agent)
73 Douglas Thrift 114 {
74     Matcher matcher;
75    
76 douglas 340 if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)")) bend(path); else if (agent == matcher("rv:(\\d+)\\.(\\d+).*\\) Gecko"))
77 Douglas Thrift 114 {
78 Douglas Thrift 266 int major_(lexical_cast<int>(matcher[1])), minor_(lexical_cast<int>(matcher[2]));
79 Douglas Thrift 117
80 douglas 340 if (major_ > 1 || (major_ == 1 && minor_ >= 5)) pass(path); else bend(path);
81 Douglas Thrift 114 }
82 Douglas Thrift 266 else bend(path);
83 Douglas Thrift 114 }
84    
85 Douglas Thrift 266 void Bender::bend(const ext::String& path)
86 Douglas Thrift 114 {
87 douglas 397 _H<xml::Document> document(xml::Parse("bender.xml"));
88     _H<xml::Node> node(*document/"bender");
89     ext::String program(*node/"program");
90     _L<ext::String> args;
91 Douglas Thrift 272
92 douglas 397 _foreach (xml::NodeSet, arg, *node/"argument") args.InsertLast(**arg);
93    
94 Douglas Thrift 272 args.InsertLast(path);
95    
96 douglas 397 _S<api::Process> xslt(program, args);
97     _S<ios::String> output;
98 Douglas Thrift 116
99 Douglas Thrift 266 ios::ReadToWrite(*xslt.GetReader(), output);
100    
101     if (!output.IsEmpty())
102 Douglas Thrift 116 {
103 Douglas Thrift 266 ext::String type("text/xml");
104 douglas 335 Matcher matcher("http-equiv=\"Content-Type\" content=\"(.*)\"", PCRE_MULTILINE);
105 Douglas Thrift 116
106 Douglas Thrift 266 if (matcher == output) type = matcher[1];
107 Douglas Thrift 119
108 Douglas Thrift 266 api::Cout << "Content-Type: " << type << "\r\n\r\n" << output;
109 Douglas Thrift 116 }
110 Douglas Thrift 266 else pass(path);
111 Douglas Thrift 114 }
112 Douglas Thrift 116
113 Douglas Thrift 266 void Bender::pass(const ext::String& path)
114 Douglas Thrift 116 {
115 Douglas Thrift 266 api::Cout << "Content-Type: text/xml\r\n\r\n";
116 Douglas Thrift 116
117 Douglas Thrift 266 api::FileReader file(path);
118 Douglas Thrift 116
119 Douglas Thrift 266 ios::ReadToWrite(file, api::Cout);
120 Douglas Thrift 116 }

Properties

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