ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Smersh.cpp
(Generate patch)

Comparing Smersh/Smersh.cpp (file contents):
Revision 162 by Douglas Thrift, 2004-06-17T15:51:47-07:00 vs.
Revision 176 by Douglas Thrift, 2004-06-25T20:32:24-07:00

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

Comparing Smersh/Smersh.cpp (property svn:eol-style):
Revision 162 by Douglas Thrift, 2004-06-17T15:51:47-07:00 vs.
Revision 176 by Douglas Thrift, 2004-06-25T20:32:24-07:00

# Line 0 | Line 1
1 + native

Comparing Smersh/Smersh.cpp (property svn:keywords):
Revision 162 by Douglas Thrift, 2004-06-17T15:51:47-07:00 vs.
Revision 176 by Douglas Thrift, 2004-06-25T20:32:24-07:00

# Line 0 | Line 1
1 + Id

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines