ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Smersh.cpp
Revision: 242
Committed: 2004-09-11T21:06:02-07:00 (20 years, 9 months ago) by Douglas Thrift
File size: 4055 byte(s)
Log Message:
Oops!

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

Properties

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