ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Bender/Bender.cpp
Revision: 117
Committed: 2004-03-17T02:02:43-08:00 (21 years, 3 months ago) by Douglas Thrift
File size: 2636 byte(s)
Log Message:
Stuff happens!

File Contents

# User Rev Content
1 Douglas Thrift 109 // Bender
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Bender.hpp"
8 Douglas Thrift 113 #include "Matcher.hpp"
9 Douglas Thrift 109
10 Douglas Thrift 113 #include <xalanc/Include/PlatformDefinitions.hpp>
11     #include <xercesc/util/PlatformUtils.hpp>
12     #include <xalanc/XalanTransformer/XalanTransformer.hpp>
13    
14     XALAN_USING_XERCES(XMLPlatformUtils)
15     XALAN_USING_XALAN(XalanTransformer)
16    
17 Douglas Thrift 109 int main(int argc, char* argv[])
18     {
19     XMLPlatformUtils::Initialize();
20     XalanTransformer::initialize();
21    
22     Bender bender;
23    
24     XalanTransformer::terminate();
25     XMLPlatformUtils::Terminate();
26     XalanTransformer::ICUCleanUp();
27    
28     return 0;
29     }
30    
31     Bender::Bender()
32     {
33 Douglas Thrift 111 string path = sgetenv("PATH_TRANSLATED");
34 Douglas Thrift 114 ifstream file(path.c_str());
35 Douglas Thrift 111
36 Douglas Thrift 114 if (path != "" && file.is_open())
37 Douglas Thrift 111 {
38 Douglas Thrift 116 file.close();
39 Douglas Thrift 111
40 Douglas Thrift 116 bend(path, sgetenv("HTTP_USER_AGENT"));
41 Douglas Thrift 111 }
42 Douglas Thrift 114 else
43     {
44     cout << "Status: 404\n"
45     << "Content-Type: text/html; charset=ISO-8859-1\n\n"
46     << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
47     << "<html><head>\n"
48     << "<title>404 Not Found</title>\n"
49     << "</head><body>\n"
50     << "<h1>Not Found</h1>\n"
51     << "<p>The requested URL " << sgetenv("PATH_INFO") << " was not fo"
52     << "und on this server.</p>\n"
53     << "<hr />\n"
54     << sgetenv("SERVER_SIGNATURE")
55     << "</body></html>\n";
56     }
57 Douglas Thrift 109 }
58 Douglas Thrift 114
59 Douglas Thrift 116 void Bender::bend(const string& path, const string& agent)
60 Douglas Thrift 114 {
61     Matcher matcher;
62    
63 Douglas Thrift 116 if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)"))
64 Douglas Thrift 114 {
65 Douglas Thrift 116 bend(path);
66 Douglas Thrift 114 }
67 Douglas Thrift 116 else if (agent == matcher(string("^Mozilla/4.0 \\(compatible; MSIE (\\d+)")
68     + "\\.(\\d+)(\\w+)?; .*\\)$"))
69 Douglas Thrift 114 {
70 Douglas Thrift 117 int version;
71     istringstream number(matcher[1]);
72 Douglas Thrift 116
73 Douglas Thrift 117 number >> version;
74    
75     if (version >= 6)
76     {
77     pass(path);
78     }
79     else
80     {
81     bend(path);
82     }
83 Douglas Thrift 114 }
84 Douglas Thrift 116 else if (agent == matcher(string("^Mozilla/5.0 \\(.*; rv:(\\d+)\\.(\\d+)(")
85     + "\\.d+)?(\\w+)?\\) Gecko\\/.*"))
86 Douglas Thrift 114 {
87 Douglas Thrift 117 int major, minor;
88     istringstream number(matcher[1] + ' ' + matcher[2]);
89    
90     number >> major;
91     number >> minor;
92    
93     if (major > 1 || (major == 1 && minor >= 5))
94     {
95     pass(path);
96     }
97     else
98     {
99     bend(path);
100     }
101 Douglas Thrift 114 }
102     else
103     {
104 Douglas Thrift 116 bend(path);
105 Douglas Thrift 114 }
106     }
107    
108 Douglas Thrift 116 void Bender::bend(const string& path)
109 Douglas Thrift 114 {
110 Douglas Thrift 116 ostringstream output;
111     XalanTransformer transformer;
112    
113     if (transformer.transform(path.c_str(), output) == 0)
114     {
115     string type = "text/html; charset=ISO-8859-1";
116     Matcher matcher("<META http-equiv=\"Content-Type\" content=\"(.*)\">");
117    
118     if (matcher == output.str())
119     {
120     type = matcher[1];
121     }
122    
123     cout << "Content-Type: " << type << "\n\n" << output.str();
124     }
125     else
126     {
127     pass(path);
128     }
129 Douglas Thrift 114 }
130 Douglas Thrift 116
131     void Bender::pass(const string& path)
132     {
133     cout << "Content-Type: text/xml; charset=ISO-8859-1\n\n";
134    
135     ifstream fin(path.c_str());
136     string line;
137    
138     do
139     {
140     getline(fin, line);
141    
142     cout << line << '\n';
143     }
144     while (fin.good());
145     }

Properties

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