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 |
|
|
15 |
– |
server.Create(); |
15 |
|
server.SetAddress(api::InternetAddress(api::InternetAddress::Any, port)); |
16 |
|
|
17 |
|
if (fork) |
34 |
|
|
35 |
|
while (true) |
36 |
|
{ |
37 |
< |
Client* client (new Client); |
39 |
< |
|
40 |
< |
server.Accept(client->socket, &client->ip); |
41 |
< |
|
37 |
> |
Client* client (new Client(server)); |
38 |
|
api::Thread thread(etl::BindAll(&Daemon::handle, self, client)); |
39 |
|
} |
40 |
|
} |
67 |
|
|
68 |
|
headers(sin, env); |
69 |
|
|
70 |
+ |
if (env.get("HTTP_HOST").empty()) return bad; |
71 |
|
if (method[0] == "POST") return message(sin, env, post); |
72 |
|
|
73 |
|
return ok; |
132 |
|
return string("Smersh/0.9 (") + system.sysname + ')'; |
133 |
|
} |
134 |
|
|
135 |
< |
streamsize Daemon::error(ostream& sout, Status status) |
135 |
> |
string Daemon::server(const Environment& env) |
136 |
> |
{ |
137 |
> |
ostringstream server; |
138 |
> |
string port(env.get("SERVER_PORT")); |
139 |
> |
|
140 |
> |
server << this->server() << " Server at " << env.get("SERVER_NAME") << " Po" |
141 |
> |
<< "rt " << (port.empty() ? lexical_cast<string>(this->port) : port); |
142 |
> |
|
143 |
> |
return server.str(); |
144 |
> |
} |
145 |
> |
|
146 |
> |
streamsize Daemon::error(ostream& sout, Status status, const Environment& env) |
147 |
|
{ |
148 |
|
string reason(this->reason(status)); |
149 |
|
ostringstream error; |
150 |
|
|
151 |
|
error << "<html><head><title>" << status << ' ' << reason << "</title></hea" |
152 |
|
<< "d><body><h1>" << reason << "</h1><p>Mistakes were made, deal with t" |
153 |
< |
<< "hem.</p><hr /><address>" << server() + "</address></body></html>\n"; |
153 |
> |
<< "hem.</p><hr /><address>" << server(env) << "</address></body></html" |
154 |
> |
<< ">\r\n"; |
155 |
|
sout << "Content-Length: " << error.str().length() << crlf |
156 |
|
<< "Content-Type: text/html; charset=UTF-8\r\n\r\n" << error.str(); |
157 |
|
|
189 |
|
bool head(env.get("REQUEST_METHOD") == "HEAD"); |
190 |
|
streamsize sent(0); |
191 |
|
|
192 |
< |
if (code == ok && !head) |
192 |
> |
if (code == ok && env.get("REQUEST_URI") == "/robots.txt") |
193 |
|
{ |
194 |
< |
ostringstream content; |
194 |
> |
sio << "Content-Length: 28\r\nContent-Type: text/plain; charset=UTF-8\r" |
195 |
> |
<< "\n\r\n"; |
196 |
|
|
197 |
< |
if (env.get("REQUEST_URI") == "/robots.txt") |
198 |
< |
{ |
199 |
< |
content << string(40, '#') << "User-agent: *\r\nDisallow: /\r\n"; |
200 |
< |
} |
201 |
< |
else Smersh smersh(post, content, env); |
197 |
> |
if (!head) { sio << "User-agent: *\r\nDisallow: /\r\n"; sent = 28; } |
198 |
> |
} |
199 |
> |
else if (code == ok && !head) |
200 |
> |
{ |
201 |
> |
ostringstream content; |
202 |
> |
Smersh smersh(post, content, env); |
203 |
|
|
204 |
< |
sio << "Content-Length: " << content.str().substr(40).length() << crlf |
204 |
> |
sio << "Content-Length: " << content.str().length() << crlf |
205 |
|
<< "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
206 |
|
|
207 |
< |
sio.write(content.str().substr(40).data(), content.str().size() - 40); |
207 |
> |
sio.write(content.str().data(), content.str().size()); |
208 |
|
|
209 |
< |
sent = content.str().size() - 40; |
209 |
> |
sent = content.str().size(); |
210 |
|
} |
211 |
|
else if (head) sio << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
212 |
< |
else sent = error(sio, code); |
212 |
> |
else sent = error(sio, code, env); |
213 |
|
|
214 |
|
ofstream fout(this->log.c_str(), ios_base::app); |
215 |
|
|
258 |
|
{ |
259 |
|
if (value == matcher) env.set("CONTENT_TYPE", matcher[1]); |
260 |
|
} |
261 |
+ |
else if (name == "Host") |
262 |
+ |
{ |
263 |
+ |
Matcher matcher("^\\s*(.+?)(:[0-9]+)?\\s*$"); |
264 |
+ |
|
265 |
+ |
if (value == matcher) |
266 |
+ |
{ |
267 |
+ |
bool port(matcher.size() > 2); |
268 |
+ |
|
269 |
+ |
env.set("HTTP_HOST", matcher[1] + (port ? matcher[2] : "")); |
270 |
+ |
env.set("SERVER_NAME", matcher[1]); |
271 |
+ |
|
272 |
+ |
if (port) env.set("SERVER_PORT", matcher[2].substr(1)); |
273 |
+ |
} |
274 |
+ |
} |
275 |
|
else if (name == "Referer") |
276 |
|
{ |
277 |
|
if (value == matcher) env.set("HTTP_REFERER", matcher[1]); |