ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Redirector.cpp
Revision: 349
Committed: 2004-12-16T18:15:34-08:00 (20 years, 6 months ago) by douglas
File size: 2079 byte(s)
Log Message:
Horribly broken, including parts of menes.

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 205 ios::ToIoStream sio(&client->socket, &client->socket);
12 Douglas Thrift 180 Environment env;
13     stringstream post;
14     ostringstream log;
15     Status code(request(sio, env, post, log));
16 Douglas Thrift 174
17 Douglas Thrift 180 if (code == ok) code = env.get("REQUEST_METHOD") != "POST" ? found :
18     seeOther;
19 Douglas Thrift 174
20 Douglas Thrift 180 response(sio, code);
21    
22     bool head(env.get("REQUEST_METHOD") == "HEAD");
23     streamsize sent(0);
24    
25 Douglas Thrift 182 if (code == found || code == seeOther)
26 Douglas Thrift 180 {
27     string location(redirect + '?' + query(post, env));
28    
29     sio << "Location: " << location << crlf;
30    
31     if (!head)
32     {
33     string reason(this->reason(code));
34     ostringstream content;
35    
36     content << "<html><head><title>" << code << ' ' << reason << "</tit"
37     << "le></head><body><h1>" << reason << "</h1><p>Don't come here"
38     << ", go <a href=\"" << location << "\">there</a>.</p><hr /><ad"
39 Douglas Thrift 181 << "dress>" << server(env) << "</address></body></html>\r\n";
40 Douglas Thrift 180 sio << "Content-Length: " << content.str().length() << crlf
41     << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
42    
43     sio.write(content.str().data(), content.str().size());
44    
45     sent = content.str().size();
46     }
47     else sio << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
48     }
49     else if (head) sio << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
50 Douglas Thrift 181 else sent = error(sio, code, env);
51 Douglas Thrift 180
52     ofstream fout(this->log.c_str(), ios_base::app);
53    
54     fout << inet_ntoa(client->ip->sin_addr) << " - - " << date(true) << ' '
55     << log.str() << code << ' ' << lexical_cast<string>(sent) << " \""
56     << env.get("HTTP_REFERER") << "\" \"" << env.get("HTTP_USER_AGENT")
57     << "\"\n";
58     sio << flush;
59    
60     client->socket.ShutdownWrite();
61    
62 Douglas Thrift 174 delete client;
63 Douglas Thrift 241
64     return 0;
65 Douglas Thrift 172 }
66 Douglas Thrift 180
67 Douglas Thrift 182 string Redirector::query(const stringstream& post, const Environment& env)
68 Douglas Thrift 180 {
69 Douglas Thrift 182 string query(env.get("REQUEST_METHOD") != "POST" ? env.get("QUERY_STRING") :
70     post.str());
71    
72     for (string::size_type index(0); index < query.length(); ++index)
73     {
74     if (isspace(query[index])) query.replace(index, 1, 1, '+');
75     }
76    
77 douglas 349 if (Smersh::debug) cerr << "query = " << query << '\n';
78 Douglas Thrift 182
79     return query;
80 Douglas Thrift 180 }

Properties

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