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

ProxyCommandProcessor.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00002 
00003 
00004 ProxyCommandProcessor::ProxyCommandProcessor(int proxyPort) {
00005     m_listener = new TcpConnection(proxyPort);
00006     if (!m_listener->listen()) {
00007         debug(DEBUG_CMD, "Could not bind to proxy socket");
00008     }
00009 } // ctor
00010 
00011 
00012 void
00013 ProxyCommandProcessor::stopThread() {
00014     // close listening socket
00015     delete m_listener;
00016     m_listener = NULL;
00017 
00018     // kill this thread
00019     CommandProcessor::stopThread();
00020 } // fn stopThread
00021 
00022 
00028 void 
00029 ProxyCommandProcessor::readCommand(string* input) {
00030     const int bufSize = 32768;
00031     char buf[bufSize];
00032     int bytesRead;
00033     int readVal;
00034 
00035     //debug(DEBUG_PROXY, "readCommand(): begin");
00036 
00037     if (m_listener->getStream() == OS_SPEC_INVALID_SOCKET) {
00038         debug(DEBUG_PROXY, "Proxy thread exiting due to invalid socket.");
00039         pthread_exit(NULL);
00040     }
00041 
00042     if ((m_connection = (TcpConnection*)m_listener->accept()) == NULL) {
00043         debug(DEBUG_PROXY, "proxy: accept failed\n");
00044         return;
00045     }
00046     debug(DEBUG_PROXY, "accepted connection");
00047     
00048     memset(&buf[0], 0, bufSize);
00049     
00050     bytesRead = 0;
00051     
00052     while (bytesRead <= 14) {
00053         if ((readVal = m_connection->read((unsigned char*)&buf[0], bufSize, 0)) <= 0) {
00054             debug(DEBUG_PROXY, "read error, closing connection");
00055             m_connection->close();
00056             return;
00057         }
00058         bytesRead += readVal;
00059         //debug(DEBUG_PROXY, "Received:\n%s", &buf[0]);
00060     }
00061     
00062     if (bytesRead <= 14) {
00063         debug(DEBUG_PROXY, "proxy: short client read, closing connection");
00064         m_connection->close();
00065     }
00066     
00067     *input = buf;
00068 } // fn readCommand
00069 
00070 
00074 void
00075 ProxyCommandProcessor::displayResponse(string* output) {
00076     //debug(DEBUG_PROXY, "Writing to browser");
00077     m_connection->write((unsigned char*)output->data(), output->length());
00078     delete m_connection;
00079     m_connection = NULL;
00080 } // fn displayResponse
00081 
00082 
00083 void
00084 ProxyCommandProcessor::generateError(string* input, std::ostream& output) {
00085         const int BUFSIZE = 128;
00086     char theTime[BUFSIZE];
00087     time_t now = time(NULL);
00088     struct tm *tp = threadsafe_gmtime(&now);
00089     strftime(theTime, BUFSIZE, "%a, %d %b %Y %H:%M:%S %Z", tp);
00090     string prefix = "<HTML><HEAD><TITLE>Peekabooty: ERROR</TITLE></HEAD>";
00091     prefix += "<BODY><BR><pre>";
00092     string postfix = "</pre><BR><BR></BODY></HTML>";
00093     string errorString = "Input not recognized:\n\n";    
00094     *input = prefix + errorString + *input + postfix;
00095 
00096     output << "HTTP/1.0 200 OK\r\n"
00097         << "Date: " << theTime << "\r\n"
00098         << "Server: Peekabooty\r\n"
00099         << "Content-type: text/html\r\n"
00100         << "Content-Length: " << input->length() << "\r\n"
00101         << "Last-Modified: " << theTime << "\r\n"
00102         << "Expires: 0\r\n"
00103         << "Pragma: no-cache\r\n"
00104         << "Connection: close\r\n\r\n"
00105         << *input;
00106 } // fn generateError
00107 
00108 
00109 void
00110 ProxyCommandProcessor::toStream(std::ostream& out) {
00111     CommandProcessor::toStream(out);
00112     out << "Listening port: " << m_listener->getSocketAddress()->getPort() << "\n";
00113 } // fn toStream
00114 

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