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 |
|
|
175 |
|
|
176 |
|
int Daemon::handle(Client* client) |
177 |
|
{ |
178 |
< |
ios::InputOutputStreamBufAdapter adapter(client->socket); |
170 |
< |
iostream sio(&adapter); |
178 |
> |
ios::ToIoStream sio(&client->socket, &client->socket); |
179 |
|
Environment env; |
180 |
|
stringstream post; |
181 |
|
ostringstream log; |
188 |
|
bool head(env.get("REQUEST_METHOD") == "HEAD"); |
189 |
|
streamsize sent(0); |
190 |
|
|
191 |
< |
if (code == ok && !head) |
191 |
> |
if (code == ok && env.get("REQUEST_URI") == "/robots.txt") |
192 |
|
{ |
193 |
< |
ostringstream content; |
193 |
> |
sio << "Content-Length: 28\r\nContent-Type: text/plain; charset=UTF-8\r" |
194 |
> |
<< "\n\r\n"; |
195 |
|
|
196 |
< |
if (env.get("REQUEST_URI") == "/robots.txt") |
197 |
< |
{ |
198 |
< |
content << string(40, '#') << "User-agent: *\r\nDisallow: /\r\n"; |
199 |
< |
} |
200 |
< |
else Smersh smersh(post, content, env); |
196 |
> |
if (!head) { sio << "User-agent: *\r\nDisallow: /\r\n"; sent = 28; } |
197 |
> |
} |
198 |
> |
else if (code == ok && !head) |
199 |
> |
{ |
200 |
> |
ostringstream content; |
201 |
> |
Smersh smersh(post, content, env); |
202 |
|
|
203 |
< |
sio << "Content-Length: " << content.str().substr(40).length() << crlf |
203 |
> |
sio << "Content-Length: " << content.str().length() << crlf |
204 |
|
<< "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
205 |
|
|
206 |
< |
sio.write(content.str().substr(40).data(), content.str().size() - 40); |
206 |
> |
sio.write(content.str().data(), content.str().size()); |
207 |
|
|
208 |
< |
sent = content.str().size() - 40; |
208 |
> |
sent = content.str().size(); |
209 |
|
} |
210 |
|
else if (head) sio << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
211 |
< |
else sent = error(sio, code); |
211 |
> |
else sent = error(sio, code, env); |
212 |
|
|
213 |
|
ofstream fout(this->log.c_str(), ios_base::app); |
214 |
|
|
257 |
|
{ |
258 |
|
if (value == matcher) env.set("CONTENT_TYPE", matcher[1]); |
259 |
|
} |
260 |
+ |
else if (name == "Host") |
261 |
+ |
{ |
262 |
+ |
Matcher matcher("^\\s*(.+?)(:[0-9]+)?\\s*$"); |
263 |
+ |
|
264 |
+ |
if (value == matcher) |
265 |
+ |
{ |
266 |
+ |
bool port(matcher.size() > 2); |
267 |
+ |
|
268 |
+ |
env.set("HTTP_HOST", matcher[1] + (port ? matcher[2] : "")); |
269 |
+ |
env.set("SERVER_NAME", matcher[1]); |
270 |
+ |
|
271 |
+ |
if (port) env.set("SERVER_PORT", matcher[2].substr(1)); |
272 |
+ |
} |
273 |
+ |
} |
274 |
|
else if (name == "Referer") |
275 |
|
{ |
276 |
|
if (value == matcher) env.set("HTTP_REFERER", matcher[1]); |