62 |
|
return 0; |
63 |
|
} |
64 |
|
|
65 |
< |
Smersh::Smersh(ostream& sout) |
65 |
> |
Smersh::Smersh(istream& sin, ostream& sout) |
66 |
|
{ |
67 |
< |
string method = sgetenv("REQUEST_METHOD"); |
68 |
< |
|
69 |
< |
parse(method); |
70 |
< |
|
71 |
< |
sout << "Content-Type: text/plain\n\n" << (cgi.find("sn") != cgi.end() ? |
72 |
< |
cgi.find("sn")->second : "Unknown") << '\n'; |
67 |
> |
parse(sin); |
68 |
> |
smersh(sout); |
69 |
|
} |
70 |
|
|
71 |
< |
void Smersh::parse(const string& method) |
71 |
> |
void Smersh::parse(istream& sin) |
72 |
|
{ |
73 |
|
string query(sgetenv("QUERY_STRING")); |
74 |
|
|
75 |
< |
if (method == "POST") getline(cin, query); |
75 |
> |
if (sgetenv("REQUEST_METHOD") == "POST") getline(sin, query); |
76 |
|
if (query == "") return; |
77 |
|
|
78 |
|
istringstream input(query); |
88 |
|
} |
89 |
|
while (input.good()); |
90 |
|
} |
91 |
+ |
|
92 |
+ |
void Smersh::smersh(ostream& sout) |
93 |
+ |
{ |
94 |
+ |
sout << "Content-Type: text/html\n\n"; |
95 |
+ |
|
96 |
+ |
vector<Person> people; |
97 |
+ |
|
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 |
+ |
while (!isalnum(sn[index]) && index < sn.length()) |
106 |
+ |
{ |
107 |
+ |
sn.erase(index, 1); |
108 |
+ |
} |
109 |
+ |
|
110 |
+ |
if (index < sn.length()) sn[index] = tolower(sn[index]); |
111 |
+ |
} |
112 |
+ |
|
113 |
+ |
Person person(sn); |
114 |
+ |
|
115 |
+ |
people.push_back(person); |
116 |
+ |
|
117 |
+ |
if (person.isMultiple()) |
118 |
+ |
{ |
119 |
+ |
people.insert(people.end(), person.beginMultiple(), |
120 |
+ |
person.endMultiple()); |
121 |
+ |
person.clearMultiple(); |
122 |
+ |
} |
123 |
+ |
} |
124 |
+ |
|
125 |
+ |
sout << "<html><head><title>Hello, " << flush; |
126 |
+ |
|
127 |
+ |
output(sout, people); |
128 |
+ |
|
129 |
+ |
sout << "!</title></head><body><p style=\"font-variant: small-caps\"><stro" |
130 |
+ |
<< "ng><font face=\"Comic Sans MS\" size=\"4\">Hello, " << flush; |
131 |
+ |
|
132 |
+ |
output(sout, people); |
133 |
+ |
|
134 |
+ |
sout << "!</font></strong></p></body></html>\n"; |
135 |
+ |
} |
136 |
+ |
|
137 |
+ |
void Smersh::output(ostream& sout, const vector<Person>& people) |
138 |
+ |
{ |
139 |
+ |
for (vector<Person>::const_iterator person(people.begin()); person != |
140 |
+ |
people.end(); ++person) |
141 |
+ |
{ |
142 |
+ |
sout << *person; |
143 |
+ |
|
144 |
+ |
if (person + 2 == people.end()) |
145 |
+ |
{ |
146 |
+ |
sout << (people.size() > 2 ? "," : "") << " and "; |
147 |
+ |
} |
148 |
+ |
else if (person + 1 != people.end()) |
149 |
+ |
{ |
150 |
+ |
sout << ", "; |
151 |
+ |
} |
152 |
+ |
|
153 |
+ |
sout << flush; |
154 |
+ |
} |
155 |
+ |
} |