ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Smersh.cpp
Revision: 253
Committed: 2004-09-13T23:31:24-07:00 (20 years, 9 months ago) by Douglas Thrift
File size: 3810 byte(s)
Log Message:
This is a bit futzy!?

File Contents

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

Properties

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