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 }
00015
00016
00017 void
00018 HttpTestCommandProcessor::stopThread() {
00019
00020 delete m_listener;
00021 m_listener = NULL;
00022
00023
00024 CommandProcessor::stopThread();
00025 }
00026
00027
00028 void
00029 HttpTestCommandProcessor::changePort(int port) {
00030
00031 m_listener->close();
00032
00033
00034 SocketAddress sa(*m_listener->getSocketAddress());
00035 sa.setPort(port);
00036 m_listener->setSocketAddress(&sa);
00037
00038
00039 m_listener->listen();
00040 }
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 }
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
00103 prefix += "<TITLE>Peek A Booty - Proxy Interface</TITLE>\n";
00104
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
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 prefix += "</HEAD>";
00127
00128 prefix += "<BODY>";
00129
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"
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 }
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