ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Bender/Bender.cpp
(Generate patch)

Comparing Bender/Bender.cpp (file contents):
Revision 113 by Douglas Thrift, 2004-03-16T12:31:22-08:00 vs.
Revision 124 by Douglas Thrift, 2004-03-21T23:03:36-08:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines