ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Redirector.cpp
Revision: 182
Committed: 2004-07-03T06:52:04-07:00 (20 years, 11 months ago) by Douglas Thrift
File size: 2087 byte(s)
Log Message:
Redirector redirects, nicely!

File Contents

# User Rev Content
1 Douglas Thrift 165 // Smersh
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Redirector.hpp"
8 Douglas Thrift 172
9 Douglas Thrift 179 int Redirector::handle(Client* client)
10 Douglas Thrift 172 {
11 Douglas Thrift 179 ios::InputOutputStreamBufAdapter adapter(client->socket);
12 Douglas Thrift 180 iostream sio(&adapter);
13     Environment env;
14     stringstream post;
15     ostringstream log;
16     Status code(request(sio, env, post, log));
17 Douglas Thrift 174
18 Douglas Thrift 180 if (code == ok) code = env.get("REQUEST_METHOD") != "POST" ? found :
19     seeOther;
20 Douglas Thrift 174
21 Douglas Thrift 180 response(sio, code);
22    
23     bool head(env.get("REQUEST_METHOD") == "HEAD");
24     streamsize sent(0);
25    
26 Douglas Thrift 182 if (code == found || code == seeOther)
27 Douglas Thrift 180 {
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 Douglas Thrift 181 << "dress>" << server(env) << "</address></body></html>\r\n";
41 Douglas Thrift 180 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 Douglas Thrift 181 else sent = error(sio, code, env);
52 Douglas Thrift 180
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 Douglas Thrift 174 delete client;
64 Douglas Thrift 172 }
65 Douglas Thrift 180
66 Douglas Thrift 182 string Redirector::query(const stringstream& post, const Environment& env)
67 Douglas Thrift 180 {
68 Douglas Thrift 182 string query(env.get("REQUEST_METHOD") != "POST" ? env.get("QUERY_STRING") :
69     post.str());
70    
71     for (string::size_type index(0); index < query.length(); ++index)
72     {
73     if (isspace(query[index])) query.replace(index, 1, 1, '+');
74     }
75    
76     if (debug) cerr << "query = " << query << '\n';
77    
78     return query;
79 Douglas Thrift 180 }

Properties

Name Value
svn:eol-style native
svn:keywords Id