ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Bender/Bender.cpp
Revision: 161
Committed: 2004-06-16T21:13:23-07:00 (21 years ago) by Douglas Thrift
File size: 2496 byte(s)
Log Message:
That might make more sense!

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

Properties

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