8 |
|
|
9 |
|
string Daemon::crlf("\r\n"); |
10 |
|
|
11 |
< |
void Daemon::serve(int port, bool fork, Daemon* self) |
11 |
> |
void Daemon::serve(bool fork, Daemon* self) |
12 |
|
{ |
13 |
|
api::TcpSocket server; |
14 |
|
|
71 |
|
|
72 |
|
headers(sin, env); |
73 |
|
|
74 |
+ |
if (env.get("HTTP_HOST").empty()) return bad; |
75 |
|
if (method[0] == "POST") return message(sin, env, post); |
76 |
|
|
77 |
|
return ok; |
136 |
|
return string("Smersh/0.9 (") + system.sysname + ')'; |
137 |
|
} |
138 |
|
|
139 |
< |
streamsize Daemon::error(ostream& sout, Status status) |
139 |
> |
string Daemon::server(const Environment& env) |
140 |
> |
{ |
141 |
> |
ostringstream server; |
142 |
> |
string port(env.get("SERVER_PORT")); |
143 |
> |
|
144 |
> |
server << this->server() << " Server at " << env.get("SERVER_NAME") << " Po" |
145 |
> |
<< "rt " << (port.empty() ? lexical_cast<string>(this->port) : port); |
146 |
> |
|
147 |
> |
return server.str(); |
148 |
> |
} |
149 |
> |
|
150 |
> |
streamsize Daemon::error(ostream& sout, Status status, const Environment& env) |
151 |
|
{ |
152 |
|
string reason(this->reason(status)); |
153 |
|
ostringstream error; |
154 |
|
|
155 |
|
error << "<html><head><title>" << status << ' ' << reason << "</title></hea" |
156 |
|
<< "d><body><h1>" << reason << "</h1><p>Mistakes were made, deal with t" |
157 |
< |
<< "hem.</p><hr /><address>" << server() + "</address></body></html>\n"; |
157 |
> |
<< "hem.</p><hr /><address>" << server(env) << "</address></body></html" |
158 |
> |
<< ">\r\n"; |
159 |
|
sout << "Content-Length: " << error.str().length() << crlf |
160 |
|
<< "Content-Type: text/html; charset=UTF-8\r\n\r\n" << error.str(); |
161 |
|
|
193 |
|
bool head(env.get("REQUEST_METHOD") == "HEAD"); |
194 |
|
streamsize sent(0); |
195 |
|
|
196 |
< |
if (code == ok && !head) |
196 |
> |
if (code == ok && env.get("REQUEST_URI") == "/robots.txt") |
197 |
|
{ |
198 |
< |
ostringstream content; |
198 |
> |
sio << "Content-Length: 28\r\nContent-Type: text/plain; charset=UTF-8\r" |
199 |
> |
<< "\n\r\n"; |
200 |
|
|
201 |
< |
if (env.get("REQUEST_URI") == "/robots.txt") |
202 |
< |
{ |
203 |
< |
content << string(40, '#') << "User-agent: *\r\nDisallow: /\r\n"; |
204 |
< |
} |
205 |
< |
else Smersh smersh(post, content, env); |
201 |
> |
if (!head) { sio << "User-agent: *\r\nDisallow: /\r\n"; sent = 28; } |
202 |
> |
} |
203 |
> |
else if (code == ok && !head) |
204 |
> |
{ |
205 |
> |
ostringstream content; |
206 |
> |
Smersh smersh(post, content, env); |
207 |
|
|
208 |
|
sio << "Content-Length: " << content.str().substr(40).length() << crlf |
209 |
|
<< "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
213 |
|
sent = content.str().size() - 40; |
214 |
|
} |
215 |
|
else if (head) sio << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
216 |
< |
else sent = error(sio, code); |
216 |
> |
else sent = error(sio, code, env); |
217 |
|
|
218 |
|
ofstream fout(this->log.c_str(), ios_base::app); |
219 |
|
|
262 |
|
{ |
263 |
|
if (value == matcher) env.set("CONTENT_TYPE", matcher[1]); |
264 |
|
} |
265 |
+ |
else if (name == "Host") |
266 |
+ |
{ |
267 |
+ |
Matcher matcher("^\\s*(.+?)(:[0-9]+)?\\s*$"); |
268 |
+ |
|
269 |
+ |
if (value == matcher) |
270 |
+ |
{ |
271 |
+ |
bool port(matcher.size() > 2); |
272 |
+ |
|
273 |
+ |
env.set("HTTP_HOST", matcher[1] + (port ? matcher[2] : "")); |
274 |
+ |
env.set("SERVER_NAME", matcher[1]); |
275 |
+ |
|
276 |
+ |
if (port) env.set("SERVER_PORT", matcher[2].substr(1)); |
277 |
+ |
} |
278 |
+ |
} |
279 |
|
else if (name == "Referer") |
280 |
|
{ |
281 |
|
if (value == matcher) env.set("HTTP_REFERER", matcher[1]); |