ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Bender/Bender.cpp
Revision: 340
Committed: 2004-12-14T15:49:53-08:00 (20 years, 6 months ago) by douglas
File size: 2545 byte(s)
Log Message:
Cheese!

File Contents

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

Properties

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