ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Smersh.cpp
Revision: 171
Committed: 2004-06-19T23:07:20-07:00 (21 years ago) by Douglas Thrift
File size: 3161 byte(s)
Log Message:
Fixed a bug!

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

Properties

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