Google
Web alhem.net
Main Page | Alphabetical List | Class List | File List | Class Members | File Members

RedirWwwSocket.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2004  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #ifdef _WIN32
00024 #pragma warning(disable:4786)
00025 #endif
00026 //#include <stdio.h>
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 

Generated on Tue Oct 3 23:47:05 2006 for The Minder by doxygen 1.3.6