ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Smersh.cpp
Revision: 199
Committed: 2004-08-30T18:24:07-07:00 (20 years, 9 months ago) by Douglas Thrift
File size: 3820 byte(s)
Log Message:
What?

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

Properties

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