ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Bender/Bender.cpp
Revision: 120
Committed: 2004-03-17T22:01:55-08:00 (21 years, 3 months ago) by Douglas Thrift
File size: 2279 byte(s)
Log Message:
If we don't have to handle 404, let's just redirect to a page about ourself.

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 Douglas Thrift 119
22 Douglas Thrift 109 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    
35 Douglas Thrift 119
36     if (path != "")
37 Douglas Thrift 111 {
38 Douglas Thrift 119 ifstream file(path.c_str());
39    
40     if (file.is_open())
41     {
42     file.close();
43    
44     bend(path, sgetenv("HTTP_USER_AGENT"));
45    
46     return;
47     }
48 Douglas Thrift 111 }
49 Douglas Thrift 119
50 Douglas Thrift 120 cout << "Location: http://computers.douglasthrift.net/bender.xml\n\n";
51 Douglas Thrift 109 }
52 Douglas Thrift 114
53 Douglas Thrift 116 void Bender::bend(const string& path, const string& agent)
54 Douglas Thrift 114 {
55     Matcher matcher;
56    
57 Douglas Thrift 116 if (agent == matcher("Opera( |\\/)(\\d+)\\.(\\d+)"))
58 Douglas Thrift 114 {
59 Douglas Thrift 116 bend(path);
60 Douglas Thrift 114 }
61 Douglas Thrift 116 else if (agent == matcher(string("^Mozilla/4.0 \\(compatible; MSIE (\\d+)")
62     + "\\.(\\d+)(\\w+)?; .*\\)$"))
63 Douglas Thrift 114 {
64 Douglas Thrift 117 int version;
65     istringstream number(matcher[1]);
66 Douglas Thrift 116
67 Douglas Thrift 117 number >> version;
68    
69     if (version >= 6)
70     {
71     pass(path);
72     }
73     else
74     {
75     bend(path);
76     }
77 Douglas Thrift 114 }
78 Douglas Thrift 116 else if (agent == matcher(string("^Mozilla/5.0 \\(.*; rv:(\\d+)\\.(\\d+)(")
79     + "\\.d+)?(\\w+)?\\) Gecko\\/.*"))
80 Douglas Thrift 114 {
81 Douglas Thrift 117 int major, minor;
82     istringstream number(matcher[1] + ' ' + matcher[2]);
83    
84     number >> major;
85     number >> minor;
86    
87     if (major > 1 || (major == 1 && minor >= 5))
88     {
89     pass(path);
90     }
91     else
92     {
93     bend(path);
94     }
95 Douglas Thrift 114 }
96     else
97     {
98 Douglas Thrift 116 bend(path);
99 Douglas Thrift 114 }
100     }
101    
102 Douglas Thrift 116 void Bender::bend(const string& path)
103 Douglas Thrift 114 {
104 Douglas Thrift 116 ostringstream output;
105     XalanTransformer transformer;
106    
107     if (transformer.transform(path.c_str(), output) == 0)
108     {
109     string type = "text/html; charset=ISO-8859-1";
110     Matcher matcher("<META http-equiv=\"Content-Type\" content=\"(.*)\">");
111    
112     if (matcher == output.str())
113     {
114     type = matcher[1];
115     }
116 Douglas Thrift 119
117 Douglas Thrift 116 cout << "Content-Type: " << type << "\n\n" << output.str();
118     }
119     else
120     {
121     pass(path);
122     }
123 Douglas Thrift 114 }
124 Douglas Thrift 116
125     void Bender::pass(const string& path)
126     {
127     cout << "Content-Type: text/xml; charset=ISO-8859-1\n\n";
128    
129     ifstream fin(path.c_str());
130     string line;
131    
132     do
133     {
134     getline(fin, line);
135    
136     cout << line << '\n';
137     }
138     while (fin.good());
139     }

Properties

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