ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Smersh.cpp
Revision: 252
Committed: 2004-09-13T17:52:44-07:00 (20 years, 9 months ago) by Douglas Thrift
File size: 3912 byte(s)
Log Message:
CGI no likey!

File Contents

# User Rev Content
1 Douglas Thrift 162 // Smersh
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Smersh.hpp"
8 Douglas Thrift 164 #include "Matcher.hpp"
9 Douglas Thrift 165 #include "Person.hpp"
10     #include "Daemon.hpp"
11     #include "Redirector.hpp"
12 Douglas Thrift 162
13 Douglas Thrift 241 #include <menes-api/exename.hpp>
14     #include <menes-app/application.hpp>
15    
16 Douglas Thrift 165 string program;
17     bool debug(false);
18    
19 Douglas Thrift 252 int main(int argc, char* argv[])
20 Douglas Thrift 162 {
21 Douglas Thrift 252 program = api::GetExecutableName();
22    
23     int port(54321);
24     bool fork(false), daemon(false), redirector(false);
25     string redirect, log("smersh.log");
26    
27     for (int index(1); index < argc; ++index)
28 Douglas Thrift 241 {
29 Douglas Thrift 252 string arg(argv[index]);
30     Matcher matcher;
31 Douglas Thrift 162
32 Douglas Thrift 252 if (arg == "-daemon")
33 Douglas Thrift 241 {
34 Douglas Thrift 252 if (!daemon) daemon = true;
35     }
36     else if (arg == matcher("^-redirector=(.+)$"))
37     {
38     if (!redirector) redirector = true;
39 Douglas Thrift 165
40 Douglas Thrift 252 redirect = matcher[1];
41 Douglas Thrift 165 }
42 Douglas Thrift 252 else if (arg == matcher("^-port=([0-9]+)$"))
43 Douglas Thrift 165 {
44 Douglas Thrift 252 port = lexical_cast<int>(matcher[1]);
45 Douglas Thrift 165 }
46 Douglas Thrift 252 else if (arg == "-fork")
47 Douglas Thrift 172 {
48 Douglas Thrift 252 if (!fork) fork = true;
49 Douglas Thrift 172 }
50 Douglas Thrift 252 else if (arg == matcher("^-log=(.+)$"))
51     {
52     log = matcher[1];
53     }
54     else if (arg == "-D")
55     {
56     if (!debug) debug = true;
57     }
58 Douglas Thrift 172 else
59     {
60 Douglas Thrift 252 cout << "Usage: " << program
61     << " [-daemon|-redirector=redirect] [-port=port] [-fork] "
62     << "[-log=log] [-D]\n";
63    
64     return 1;
65 Douglas Thrift 172 }
66 Douglas Thrift 252 }
67 Douglas Thrift 165
68 Douglas Thrift 252 if (daemon)
69     {
70     Daemon daemon(port, fork, log);
71 Douglas Thrift 165 }
72 Douglas Thrift 252 else if (redirector)
73     {
74     Redirector redirector(port, fork, log, redirect);
75     }
76     else
77     {
78     Smersh smersh;
79     }
80 Douglas Thrift 165
81 Douglas Thrift 252 return 0;
82     }
83    
84 Douglas Thrift 176 Smersh::Smersh(istream& sin, ostream& sout, const Environment& env)
85 Douglas Thrift 162 {
86 Douglas Thrift 173 parse(sin, env);
87 Douglas Thrift 182 smersh(sout, env);
88 Douglas Thrift 162 }
89    
90 Douglas Thrift 173 Environment Smersh::env;
91    
92 Douglas Thrift 176 void Smersh::parse(istream& sin, const Environment& env)
93 Douglas Thrift 162 {
94 Douglas Thrift 173 stringstream query(env.get("QUERY_STRING"));
95 Douglas Thrift 162
96 Douglas Thrift 173 if (env.get("REQUEST_METHOD") == "POST")
97 Douglas Thrift 172 {
98 Douglas Thrift 173 streamsize length(lexical_cast<streamsize>(env.get("CONTENT_LENGTH")));
99 Douglas Thrift 175 char* content(new char[length]);
100 Douglas Thrift 162
101 Douglas Thrift 172 sin.read(content, length);
102     query.write(content, length);
103 Douglas Thrift 162
104 Douglas Thrift 172 delete [] content;
105     }
106     if (query.str() == "") return;
107    
108 Douglas Thrift 162 do
109     {
110     string name, value;
111    
112 Douglas Thrift 172 getline(query, name, '=');
113     getline(query, value, '&');
114 Douglas Thrift 162
115     cgi.insert(pair<string, string>(name, value));
116     }
117 Douglas Thrift 172 while (query.good());
118 Douglas Thrift 162 }
119 Douglas Thrift 166
120 Douglas Thrift 182 void Smersh::smersh(ostream& sout, const Environment& env)
121 Douglas Thrift 166 {
122 Douglas Thrift 182 if (&env == &Smersh::env) sout << "Content-Type: text/html; charset=UTF-8\r"
123     << "\n\r\n";
124 Douglas Thrift 166
125 Douglas Thrift 167 vector<Person> people(1);
126 Douglas Thrift 166
127     for (multimap<string, string>::iterator itor(cgi.lower_bound("sn")); itor
128     != cgi.upper_bound("sn"); ++itor)
129     {
130     string sn(itor->second);
131    
132     for (string::size_type index(0); index < sn.length(); ++index)
133     {
134 Douglas Thrift 171 while (!isalnum(sn[index]) && index < sn.length())
135 Douglas Thrift 167 {
136 Douglas Thrift 171 if (sn[index] == '%' && index + 2 < sn.length())
137     {
138     istringstream code(sn.substr(index + 1, 2));
139     unsigned short character;
140 Douglas Thrift 167
141 Douglas Thrift 171 code.setf(ios_base::hex, ios_base::basefield);
142 Douglas Thrift 167
143 Douglas Thrift 171 code >> character;
144 Douglas Thrift 167
145 Douglas Thrift 171 sn.replace(index, 3, 1, character);
146 Douglas Thrift 167
147 Douglas Thrift 171 if (isalnum(sn[index])) break;
148     }
149    
150 Douglas Thrift 166 sn.erase(index, 1);
151     }
152     }
153    
154 Douglas Thrift 170 if (debug) cerr << "sn = " << sn << '\n';
155    
156 Douglas Thrift 166 Person person(sn);
157    
158     people.push_back(person);
159    
160     if (person.isMultiple())
161     {
162     people.insert(people.end(), person.beginMultiple(),
163     person.endMultiple());
164     person.clearMultiple();
165     }
166     }
167    
168 Douglas Thrift 167 if (people.size() > 1)
169     {
170     people.erase(people.begin());
171    
172     sort(people.begin(), people.end());
173    
174     people.erase(unique(people.begin(), people.end()), people.end());
175     }
176    
177 Douglas Thrift 166 sout << "<html><head><title>Hello, " << flush;
178    
179     output(sout, people);
180    
181     sout << "!</title></head><body><p style=\"font-variant: small-caps\"><stro"
182     << "ng><font face=\"Comic Sans MS\" size=\"4\">Hello, " << flush;
183    
184     output(sout, people);
185    
186 Douglas Thrift 182 sout << "!</font></strong></p></body></html>\r\n";
187 Douglas Thrift 166 }
188    
189     void Smersh::output(ostream& sout, const vector<Person>& people)
190     {
191     for (vector<Person>::const_iterator person(people.begin()); person !=
192     people.end(); ++person)
193     {
194     sout << *person;
195    
196     if (person + 2 == people.end())
197     {
198 Douglas Thrift 167 sout << (people.size() > 2 ? "," : "") << " and/or ";
199 Douglas Thrift 166 }
200     else if (person + 1 != people.end())
201     {
202     sout << ", ";
203     }
204    
205     sout << flush;
206     }
207     }

Properties

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