Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

HttpTestCommandProcessor.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00002 
00003 
00004 HttpTestCommandProcessor::HttpTestCommandProcessor(int port) {
00005     m_listener = new TcpConnection(port);
00006     m_listener->listen();
00007 
00008     if (!m_listener->isConnected()) {
00009         debug(DEBUG_CMD, "Could not bind to HTTP test command socket");
00010     }
00011     else {
00012         debug(DEBUG_CMD, "HTTP test command interface listening on port %d\n", m_listener->getSocketAddress()->getPort());
00013     }
00014 } // ctor
00015 
00016 
00017 void
00018 HttpTestCommandProcessor::stopThread() {
00019     // close listening socket
00020     delete m_listener;
00021     m_listener = NULL;
00022 
00023     // kill this thread
00024     CommandProcessor::stopThread();
00025 } // fn stopThread
00026 
00027 
00028 void
00029 HttpTestCommandProcessor::changePort(int port) {
00030     // close the listening port
00031     m_listener->close();
00032 
00033     // change the port
00034     SocketAddress sa(*m_listener->getSocketAddress());
00035     sa.setPort(port);
00036     m_listener->setSocketAddress(&sa);
00037 
00038     // reopen the listening socket on a new port.
00039     m_listener->listen();
00040 } // fn changePort
00041 
00042 
00048 void 
00049 HttpTestCommandProcessor::readCommand(string* input) {
00050     const int bufSize = 8192;
00051     char buf[bufSize];
00052     int bytesRead;
00053     int readVal;
00054 
00055     if (m_listener->getStream() == OS_SPEC_INVALID_SOCKET) {
00056         debug(DEBUG_CMD, "HTTP test command thread exiting due to invalid socket.");
00057         pthread_exit(NULL);
00058     }
00059 
00060 
00061     if ((m_connection = (TcpConnection*)m_listener->accept()) == NULL) {
00062         debug(DEBUG_CMD, "accept failed\n");
00063         return;
00064     }
00065     debug(DEBUG_CMD, "accepted connection");
00066     
00067     memset(&buf[0], 0, bufSize);
00068     
00069     bytesRead = 0;
00070     
00071     while (bytesRead <= 14) {
00072         if ((readVal = m_connection->read((unsigned char*)&buf[0], bufSize, 0)) <= 0) {
00073             debug(DEBUG_CMD, "read error, closing connection");
00074             m_connection->close();
00075             return;
00076         }
00077         bytesRead += readVal;
00078         debug(DEBUG_CMD, "Received:\n%s", &buf[0]);
00079     }
00080     
00081     if (bytesRead <= 14) {
00082         debug(DEBUG_CMD, "short HTTP read, closing connection");
00083         m_connection->close();
00084     }
00085     
00086     *input = buf;
00087 } // fn readCommand
00088 
00089 
00093 void
00094 HttpTestCommandProcessor::displayResponse(string* output) {
00095         const int BUFSIZE = 128;
00096     char theTime[BUFSIZE];
00097     time_t now = time(NULL);
00098     struct tm *tp = threadsafe_gmtime(&now);
00099     strftime(theTime, BUFSIZE, "%a, %d %b %Y %H:%M:%S %Z", tp);
00100     
00101     string prefix = "<HTML><HEAD>";
00102     // The title of the page
00103     prefix += "<TITLE>Peek A Booty - Proxy Interface</TITLE>\n";
00104     // Tell browser not to cache the page
00105     prefix += "<META HTTP-EQUIV=\"expires\" CONTENT=\"Tue, 31 Dec 1996 01:23:45 GMT\">\n";
00106     prefix += "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n";
00107     /*
00108     prefix += "<noscript>\n";
00109     prefix += "<meta uiv=\"refresh\" content=\"1\">\n";
00110     prefix += "</noscript>\n";
00111     
00112     prefix += "<script language=\"JavaScript\">\n";
00113     prefix += "<!--\n";
00114     prefix += "function doLoad()\n";
00115     prefix += "{\n";
00116     prefix += "  setTimeout( \"refresh()\", 1000 );\n";
00117     prefix += "}\n";
00118     prefix += "function refresh()\n";
00119     prefix += "{\n";
00120     prefix += "  location.reload();\n";
00121     prefix += "}\n";
00122     prefix += "//-->\n";
00123     prefix += "</script>";
00124     */
00125 
00126     prefix += "</HEAD>";
00127     // Set the colors for the page
00128     prefix += "<BODY>";// BGCOLOR=\"#000000\"><FONT COLOR=\"#00FF00\">";
00129     // Display everything as preformatted text
00130     prefix += "<BR><pre>";
00131     
00132     string postfix = "</pre><BR><BR>";
00133     postfix += "</BODY></HTML>";
00134     
00135     *output = prefix + *output + postfix;
00136 
00137     stringstream buf;
00138     buf << "HTTP/1.0 200 OK\r\n"
00139         << "Date: " << theTime << "\r\n"
00140         << "Server: Peekabooty\r\n"
00141         << "Content-type: text/html\r\n"
00142         << "Content-Length: " << output->length() << "\r\n"
00143         << "Expires: 0\r\n"
00144         << "Pragma: no-cache\r\n"
00145         << "Last-Modified: -1\r\n" //" << theTime << "\r\n"
00146         << "Connection: close\r\n\r\n"
00147         << *output;
00148 
00149     m_connection->write((unsigned char*)buf.str().c_str(), buf.str().length());
00150     delete m_connection;
00151     m_connection = NULL;
00152 } // fn displayResponse
00153 
00154 
00155 void
00156 HttpTestCommandProcessor::toStream(std::ostream& out) {
00157     CommandProcessor::toStream(out);
00158     out << "Listening port: " << m_listener->getSocketAddress()->getPort() << "\n";
00159 }
00160 

Generated at Thu Jul 11 13:31:50 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001