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 }
00010
00011
00012 void
00013 ProxyCommandProcessor::stopThread() {
00014
00015 delete m_listener;
00016 m_listener = NULL;
00017
00018
00019 CommandProcessor::stopThread();
00020 }
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
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
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 }
00069
00070
00074 void
00075 ProxyCommandProcessor::displayResponse(string* output) {
00076
00077 m_connection->write((unsigned char*)output->data(), output->length());
00078 delete m_connection;
00079 m_connection = NULL;
00080 }
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 }
00107
00108
00109 void
00110 ProxyCommandProcessor::toStream(std::ostream& out) {
00111 CommandProcessor::toStream(out);
00112 out << "Listening port: " << m_listener->getSocketAddress()->getPort() << "\n";
00113 }
00114