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

ProxyRequestCommand.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00002 
00003 static const string proxyRequestCommandString = "proxyRequest";
00004 
00009 void
00010 ProxyRequestCommand::run(std::ostream& s) {
00011     int serviceNumber;
00012     int bytesRead;
00013     int bytesWritten;
00014     const int responseSize = 32768;
00015     vector<char> response(responseSize);
00016     
00017     serviceNumber = GlobalObjects::instance()->getTransportLayer()->connect();
00018     if (serviceNumber == PB_NOT_CONNECTED) {
00019         s << "Peekabooty Error: You are not connected to any nodes.\n";
00020         return;
00021     }
00022     else if (serviceNumber == PB_UNREACHABLE) {
00023         s << "Peekabooty Error: Unable to reach destination.\n";
00024         return;
00025     }
00026     else if (serviceNumber <= 0) {
00027         debug(DEBUG_PROXY, "Peekabooty Error: Unspecified.\n");
00028         return;
00029     }
00030 
00031     //debug(DEBUG_PROXY, "HTTP Request:\n%s", m_args[1].c_str());
00032 
00033     //rewriteRequest();
00034 
00035     //debug(DEBUG_PROXY, "Rewritten as:\n%s", m_args[1].c_str());
00036 
00037     bytesWritten = GlobalObjects::instance()->getTransportLayer()->send(serviceNumber, 
00038                                                                 (unsigned char*)m_args[1].data(), 
00039                                                                 m_args[1].size());
00040     if (bytesWritten < 0) {
00041         debug(DEBUG_PROXY, "encountered error during transmission");
00042         GlobalObjects::instance()->getTransportLayer()->disconnect(serviceNumber);
00043         return;
00044     }
00045 
00046     int totalBytesRead = 0;
00047     int readCount = 0;
00048     do {
00049         // read the response
00050         bytesRead = GlobalObjects::instance()->getTransportLayer()->receive(serviceNumber, 
00051                                                                             (unsigned char*)&response[0], 
00052                                                                             response.capacity());
00053         if (bytesRead > 0)  {
00054             totalBytesRead += bytesRead;
00055             readCount++;
00056             //debug(DEBUG_PROXY, "Writing %d bytes to browser, total bytes written: %d", bytesRead, totalBytesRead);
00057             // write it back to the browser
00058             s.write(&response[0], bytesRead);
00059             //s.flush();
00060         }
00061     } while (bytesRead > 0);
00062 
00063     //debug(DEBUG_PROXY, "End of transmission, read %d times, wrote %d bytes", readCount, totalBytesRead);
00064     debug(DEBUG_PROXY, "Disconnecting");
00065     if (!GlobalObjects::instance()->getTransportLayer()->disconnect(serviceNumber)) {
00066         debug(DEBUG_PROXY, "Error disconnecting.");
00067         return;
00068     }
00069     //debug(DEBUG_PROXY, "Request fulfilled");
00070 } // fn run
00071 
00072 
00076 void
00077 ProxyRequestCommand::rewriteRequest() {
00078     string match = "Proxy-Connection: Keep-Alive";
00079     string replaceWith = "Connection: close";
00080     int startPos;
00081     if ((startPos = m_args[1].find(match)) != string::npos) {
00082         m_args[1].replace(startPos, startPos+match.length()-1, replaceWith.c_str(), replaceWith.length());
00083     }
00084 } // fn rewriteRequest
00085 
00086 
00087 string
00088 ProxyRequestCommand::getCommandString() {
00089     return proxyRequestCommandString;
00090 } // fn getCommandString
00091 
00092 
00093 void
00094 ProxyRequestCommand::getHtmlInterface(std::ostream& s) {
00095     s << "<h2>Proxy Request</h2>"
00096         << "Sorry, no interface for this command.\n";
00097 } // fn getHtmlInterface
00098 

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