1 |
// Smersh |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Redirector.hpp" |
8 |
|
9 |
int Redirector::handle(Client* client) |
10 |
{ |
11 |
ios::InputOutputStreamBufAdapter adapter(client->socket); |
12 |
iostream sio(&adapter); |
13 |
Environment env; |
14 |
stringstream post; |
15 |
ostringstream log; |
16 |
Status code(request(sio, env, post, log)); |
17 |
|
18 |
if (code == ok) code = env.get("REQUEST_METHOD") != "POST" ? found : |
19 |
seeOther; |
20 |
|
21 |
response(sio, code); |
22 |
|
23 |
bool head(env.get("REQUEST_METHOD") == "HEAD"); |
24 |
streamsize sent(0); |
25 |
|
26 |
if (code == found) |
27 |
{ |
28 |
string location(redirect + '?' + query(post, env)); |
29 |
|
30 |
sio << "Location: " << location << crlf; |
31 |
|
32 |
if (!head) |
33 |
{ |
34 |
string reason(this->reason(code)); |
35 |
ostringstream content; |
36 |
|
37 |
content << "<html><head><title>" << code << ' ' << reason << "</tit" |
38 |
<< "le></head><body><h1>" << reason << "</h1><p>Don't come here" |
39 |
<< ", go <a href=\"" << location << "\">there</a>.</p><hr /><ad" |
40 |
<< "dress>" << server(env) << "</address></body></html>\r\n"; |
41 |
sio << "Content-Length: " << content.str().length() << crlf |
42 |
<< "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
43 |
|
44 |
sio.write(content.str().data(), content.str().size()); |
45 |
|
46 |
sent = content.str().size(); |
47 |
} |
48 |
else sio << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
49 |
} |
50 |
else if (head) sio << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
51 |
else sent = error(sio, code, env); |
52 |
|
53 |
ofstream fout(this->log.c_str(), ios_base::app); |
54 |
|
55 |
fout << inet_ntoa(client->ip->sin_addr) << " - - " << date(true) << ' ' |
56 |
<< log.str() << code << ' ' << lexical_cast<string>(sent) << " \"" |
57 |
<< env.get("HTTP_REFERER") << "\" \"" << env.get("HTTP_USER_AGENT") |
58 |
<< "\"\n"; |
59 |
sio << flush; |
60 |
|
61 |
client->socket.ShutdownWrite(); |
62 |
|
63 |
delete client; |
64 |
} |
65 |
|
66 |
string Redirector::query(istream& sin, const Environment& env) |
67 |
{ |
68 |
ostringstream query; |
69 |
|
70 |
return query.str(); |
71 |
} |