// Smersh
//
// Douglas Thrift
//
// $Id$
#include "Redirector.hpp"
int Redirector::handle(Client* client)
{
ios::InputOutputStreamBufAdapter adapter(client->socket);
iostream sio(&adapter);
Environment env;
stringstream post;
ostringstream log;
Status code(request(sio, env, post, log));
if (code == ok) code = env.get("REQUEST_METHOD") != "POST" ? found :
seeOther;
response(sio, code);
bool head(env.get("REQUEST_METHOD") == "HEAD");
streamsize sent(0);
if (code == found)
{
string location(redirect + '?' + query(post, env));
sio << "Location: " << location << crlf;
if (!head)
{
string reason(this->reason(code));
ostringstream content;
content << "
" << code << ' ' << reason << "" << reason << "
Don't come here"
<< ", go there.
" << server() << "\n";
sio << "Content-Length: " << content.str().length() << crlf
<< "Content-Type: text/html; charset=UTF-8\r\n\r\n";
sio.write(content.str().data(), content.str().size());
sent = content.str().size();
}
else sio << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
}
else if (head) sio << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
else sent = error(sio, code);
ofstream fout(this->log.c_str(), ios_base::app);
fout << inet_ntoa(client->ip->sin_addr) << " - - " << date(true) << ' '
<< log.str() << code << ' ' << lexical_cast(sent) << " \""
<< env.get("HTTP_REFERER") << "\" \"" << env.get("HTTP_USER_AGENT")
<< "\"\n";
sio << flush;
client->socket.ShutdownWrite();
delete client;
}
string Redirector::query(istream& sin, const Environment& env)
{
ostringstream query;
return query.str();
}