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
00032
00033
00034
00035
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
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
00057
00058 s.write(&response[0], bytesRead);
00059
00060 }
00061 } while (bytesRead > 0);
00062
00063
00064 debug(DEBUG_PROXY, "Disconnecting");
00065 if (!GlobalObjects::instance()->getTransportLayer()->disconnect(serviceNumber)) {
00066 debug(DEBUG_PROXY, "Error disconnecting.");
00067 return;
00068 }
00069
00070 }
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 }
00085
00086
00087 string
00088 ProxyRequestCommand::getCommandString() {
00089 return proxyRequestCommandString;
00090 }
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 }
00098