00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef _WIN32
00024 #pragma warning(disable:4786)
00025 #endif
00026
00027 #include <Parse.h>
00028
00029 #include "MyTcpSocket.h"
00030 #include "MyHandler.h"
00031 #include "RedirWwwSocket.h"
00032
00033 #define DEB(x) x
00034
00035
00036
00037
00038 RedirWwwSocket::RedirWwwSocket(ISocketHandler& h)
00039 :TcpSocket(h)
00040 {
00041 SetLineProtocol();
00042 }
00043
00044
00045 RedirWwwSocket::~RedirWwwSocket()
00046 {
00047 }
00048
00049
00050 void RedirWwwSocket::OnLine(const std::string& line)
00051 {
00052 Parse pa(line,":");
00053 std::string key = pa.getword();
00054
00055 DEB( printf("%s\n",line.c_str());)
00056
00057 if (key == "GET")
00058 {
00059 m_method = key;
00060 m_url = pa.getword();
00061 }
00062 else
00063 if (key == "")
00064 {
00065 std::string response;
00066 if (m_method == "GET")
00067 {
00068 HOST *p = static_cast<MyHandler&>(Handler()).GetRandomHOST( "viawww", true );
00069 if (p)
00070 {
00071 std::string ip;
00072 Utility::l2ip(p -> ip, ip);
00073 response = "HTTP/1.0 302 Moved\n"
00074 "Location: http://" + ip + ":8080" + m_url + "\n"
00075 "\n";
00076 Send(response);
00077 DEB( printf("Response:\n%s\n",response.c_str());)
00078 SetCloseAndDelete();
00079 }
00080 else
00081 {
00082 DEB( printf("no random host found\n");)
00083 response = "HTTP/1.0 404 Not Found\n"
00084 "Content-Type: text/html\n"
00085 "\n"
00086 "<html><body><h1>Document not found</ht></body></html>";
00087 Send(response);
00088 SetCloseAndDelete();
00089 }
00090 }
00091 else
00092 {
00093 DEB( printf("m_method != GET ('%s')\n",m_method.c_str());)
00094 SetCloseAndDelete();
00095 }
00096 }
00097 }
00098
00099
00100
00101