ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Smersh.cpp
Revision: 349
Committed: 2004-12-16T18:15:34-08:00 (20 years, 6 months ago) by douglas
File size: 3959 byte(s)
Log Message:
Horribly broken, including parts of menes.

File Contents

# User Rev Content
1 Douglas Thrift 162 // Smersh
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Smersh.hpp"
8 douglas 349 #include "Matcher/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 260 struct SmershCommand : public app::Application
17 Douglas Thrift 162 {
18 Douglas Thrift 260 virtual int Run(const app::ArgumentList& args)
19     {
20 douglas 349 Smersh::program = api::GetExecutablePath().GetName();
21 Douglas Thrift 252
22 Douglas Thrift 260 int port(54321);
23     bool daemon(false), redirector(false);
24 douglas 349 ext::String redirect, log("smersh.log");
25 Douglas Thrift 252
26 douglas 349 _foreach (app::ArgumentList, arg, args)
27 Douglas Thrift 260 {
28     Matcher matcher;
29 Douglas Thrift 162
30 douglas 349 if (*arg == "-daemon")
31 Douglas Thrift 260 {
32     if (!daemon) daemon = true;
33     }
34 douglas 349 else if (*arg == matcher("^-redirector=(.+)$"))
35 Douglas Thrift 260 {
36     if (!redirector) redirector = true;
37    
38     redirect = matcher[1];
39     }
40 douglas 349 else if (*arg == matcher("^-port=([0-9]+)$"))
41 Douglas Thrift 260 {
42     port = lexical_cast<int>(matcher[1]);
43     }
44 douglas 349 else if (*arg == matcher("^-log=(.+)$"))
45 Douglas Thrift 260 {
46     log = matcher[1];
47     }
48 douglas 349 else if (*arg == "-D")
49 Douglas Thrift 260 {
50 douglas 349 if (!Smersh::debug) Smersh::debug = true;
51 Douglas Thrift 260 }
52     else
53     {
54 douglas 349 api::Cerr << "Usage: " << Smersh::program << " [-daemon|-redirector=redirect] [-port=port] [-log=log] [-D]\n";
55 Douglas Thrift 260
56     return 1;
57     }
58 Douglas Thrift 252 }
59 Douglas Thrift 165
60 Douglas Thrift 260 if (daemon)
61 Douglas Thrift 165 {
62 Douglas Thrift 260 Daemon daemon(port, log);
63 Douglas Thrift 165 }
64 Douglas Thrift 260 else if (redirector)
65 Douglas Thrift 252 {
66 Douglas Thrift 260 Redirector redirector(port, log, redirect);
67 Douglas Thrift 252 }
68 Douglas Thrift 172 else
69     {
70 Douglas Thrift 260 Smersh smersh;
71 Douglas Thrift 172 }
72 Douglas Thrift 165
73 Douglas Thrift 260 return 0;
74 Douglas Thrift 165 }
75 Douglas Thrift 260 } smersh;
76 Douglas Thrift 165
77 douglas 349 Smersh::Smersh(std::istream& sin, std::ostream& sout, const Environment& env)
78 Douglas Thrift 162 {
79 Douglas Thrift 173 parse(sin, env);
80 Douglas Thrift 182 smersh(sout, env);
81 Douglas Thrift 162 }
82    
83 douglas 349 ext::String Smersh::program;
84     bool Smersh::debug(false);
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 349 std::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 349 std::streamsize length(lexical_cast<std::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 douglas 349
103 Douglas Thrift 172 if (query.str() == "") return;
104    
105 Douglas Thrift 162 do
106     {
107 douglas 349 std::string name, value;
108 Douglas Thrift 162
109 douglas 349 std::getline(query, name, '=');
110     std::getline(query, value, '&');
111 Douglas Thrift 162
112 douglas 349 cgi.insert(std::pair<std::string, std::string>(name, value));
113 Douglas Thrift 162 }
114 Douglas Thrift 172 while (query.good());
115 Douglas Thrift 162 }
116 Douglas Thrift 166
117 douglas 349 void Smersh::smersh(std::ostream& sout, const Environment& env)
118 Douglas Thrift 166 {
119 douglas 349 if (&env == &Smersh::env) sout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
120 Douglas Thrift 166
121 douglas 349 ext::RedBlackSet<Person> people;
122 Douglas Thrift 166
123 douglas 349 typedef std::multimap<std::string, std::string> MultiMap;
124    
125     _for (MultiMap::iterator, itor, cgi.lower_bound("sn"), cgi.upper_bound("sn"))
126 Douglas Thrift 166 {
127 douglas 349 std::string sn(itor->second);
128 Douglas Thrift 166
129 douglas 349 for (std::string::size_type index(0); index < sn.length(); ++index)
130 Douglas Thrift 166 {
131 Douglas Thrift 171 while (!isalnum(sn[index]) && index < sn.length())
132 Douglas Thrift 167 {
133 Douglas Thrift 171 if (sn[index] == '%' && index + 2 < sn.length())
134     {
135 douglas 349 std::istringstream code(sn.substr(index + 1, 2));
136 Douglas Thrift 171 unsigned short character;
137 Douglas Thrift 167
138 douglas 349 code.setf(std::ios_base::hex, std::ios_base::basefield);
139 Douglas Thrift 167
140 Douglas Thrift 171 code >> character;
141 Douglas Thrift 167
142 Douglas Thrift 171 sn.replace(index, 3, 1, character);
143 Douglas Thrift 167
144 Douglas Thrift 171 if (isalnum(sn[index])) break;
145     }
146    
147 Douglas Thrift 166 sn.erase(index, 1);
148     }
149     }
150    
151 Douglas Thrift 170 if (debug) cerr << "sn = " << sn << '\n';
152    
153 Douglas Thrift 166 Person person(sn);
154    
155 douglas 349 people.Insert(person);
156 Douglas Thrift 166
157     if (person.isMultiple())
158     {
159 douglas 349 _foreach (ext::Vector<Person>, single, person.getMultiple()) people.Insert(*single);
160    
161 Douglas Thrift 166 person.clearMultiple();
162     }
163     }
164    
165 douglas 349 if (people.IsEmpty()) people.Insert(Person());
166 Douglas Thrift 167
167 douglas 349 sout << "<html><head><title>Hello, " << std::flush;
168 Douglas Thrift 167
169 Douglas Thrift 166 output(sout, people);
170    
171 douglas 349 sout << "!</title></head><body><p style=\"font-variant: small-caps\"><strong><font face=\"Comic Sans MS\" size=\"4\">Hello, " << std::flush;
172 Douglas Thrift 166
173     output(sout, people);
174    
175 Douglas Thrift 182 sout << "!</font></strong></p></body></html>\r\n";
176 Douglas Thrift 166 }
177    
178 douglas 349 void Smersh::output(std::ostream& sout, const ext::RedBlackSet<Person>& people)
179 Douglas Thrift 166 {
180 douglas 349 _foreach (ext::RedBlackSet<Person>, person, people)
181 Douglas Thrift 166 {
182     sout << *person;
183    
184 douglas 349 if (person + 2 == people.End())
185 Douglas Thrift 166 {
186 douglas 349 sout << (people.GetSize() > 2 ? "," : "") << " and/or ";
187 Douglas Thrift 166 }
188 douglas 349 else if (person + 1 != people.End())
189 Douglas Thrift 166 {
190     sout << ", ";
191     }
192    
193 douglas 349 sout << std::flush;
194 Douglas Thrift 166 }
195     }

Properties

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