ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Smersh.cpp
Revision: 260
Committed: 2004-10-04T12:41:21-07:00 (20 years, 8 months ago) by Douglas Thrift
File size: 3968 byte(s)
Log Message:
Still no worky, but...

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

Properties

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