Logo
~Apps~
~Projects~
~Contact~


StatusSocket.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2005  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #ifdef _WIN32
00024 #pragma warning(disable:4786)
00025 #endif
00026 #include <Utility.h>
00027 #include <HttpdCookies.h>
00028 #include <HttpdForm.h>
00029 #include <Parse.h>
00030 #include "PeerHandler.h"
00031 #include "StatusSocket.h"
00032 
00033 #ifdef _DEBUG
00034 #define DEB(x) x
00035 #else
00036 #define DEB(x)
00037 #endif
00038 
00039 
00040 
00041 StatusSocket::StatusSocket(ISocketHandler& h)
00042 :HttpdSocket(h)
00043 ,m_sheet(0)
00044 {
00045         SetFlushBeforeClose();
00046 }
00047 
00048 
00049 StatusSocket::~StatusSocket()
00050 {
00051 }
00052 
00053 
00054 void StatusSocket::Exec()
00055 {
00056 DEB(printf("%s %s %s\n", GetMethod().c_str(), GetUrl().c_str(), GetHttpVersion().c_str());)
00057         GetEnvironment();
00058         ReadCookies();
00059         FormInput();
00060         CreateHeader();
00061         GenerateDocument();
00062 }
00063 
00064 
00065 void StatusSocket::GetEnvironment()
00066 {
00067 }
00068 
00069 
00070 void StatusSocket::ReadCookies()
00071 {
00072         HttpdCookies *cs = GetCookies();
00073         if (!cs)
00074                 return;
00075         std::string slask;
00076         if (cs -> getvalue("bleep", slask))
00077         {
00078                 Parse pa(slask,":");
00079                 m_sheet = pa.getvalue();
00080         }
00081 }
00082 
00083 
00084 void StatusSocket::FormInput()
00085 {
00086         const HttpdForm *form = GetForm();
00087         if (!form)
00088         {
00089 DEB(printf("form is NULL\n");)
00090                 return;
00091         }
00092 
00093         m_tmp = form -> getvalue("ah");
00094 
00095         if (GetUri() == "/stop")
00096         {
00097                 static_cast<PeerHandler&>(Handler()).SetQuit();
00098         }
00099         if (GetUri() == "/debug")
00100         {
00101                 int val = 0;
00102                 std::string key;
00103                 std::string value;
00104                 form -> getfirst(key, value);
00105                 while (key.size())
00106                 {
00107 DEB(printf("%s == '%s'\n", key.c_str(), value.c_str());)
00108                         if (!strcmp(key.c_str(), "debug"))
00109                                 val |= atoi(value.c_str());
00110                         //
00111                         form -> getnext(key, value);
00112                 }
00113                 static_cast<PeerHandler&>(Handler()).SetDebug(val);
00114         }
00115         std::string tmp = form -> getvalue("sheet");
00116         if (tmp.size())
00117                 m_sheet = atoi(tmp.c_str());
00118 }
00119 
00120 
00121 void StatusSocket::CreateHeader()
00122 {
00123         // header
00124         AddResponseHeader("Date", GetHttpDate());
00125         AddResponseHeader("Server", "++ 0.001");
00126         AddResponseHeader("Connection", "close");
00127         AddResponseHeader("Content-type", "text/html");
00128 
00129         // status
00130         if (GetUri() == "/favicon.ico")
00131         {
00132                 SetStatus("404");
00133                 SetStatusText("Not Found");
00134         }
00135         else
00136         {
00137                 SetStatus("200");
00138                 SetStatusText("OK");
00139 
00140                 // cookie
00141                 SetTheCookie();
00142         }
00143 
00144         // send
00145         SendResponse();
00146 }
00147 
00148 
00149 void StatusSocket::GenerateDocument()
00150 {
00151         if (GetStatus() != "200")
00152                 return;
00153 
00154         // page
00155         Send("<html>"
00156                 "<head>"
00157                 "<style type='text/css'>"
00158                 "td.h {"
00159                 "background: #e0e0e0;"
00160                 "}"
00161                 "</style>"
00162                 "</head>"
00163                 "<body>"); //<h1>status</h1>");
00164         Send("<div style='border: 2px #000000 solid; background: #e0e0e0; margin: 5px;'>");
00165         menyval(1, m_sheet, "server commands");
00166         menyval(2, m_sheet, "debug options");
00167         menyval(3, m_sheet, "sessions");
00168         Send("</div>");
00169         Send("<br>");
00170 
00171         switch (m_sheet)
00172         {
00173         case 1:
00174                 Send("<a href='/stop'>stop server</a><br>");
00175                 Send("AH: " + m_tmp + "<br>");
00176                 break;
00177         case 2:
00178                 Send("<h3>Debug</h3>");
00179                 Send("<form action=/debug method=POST>");
00180                 {
00181                 int debug = static_cast<PeerHandler&>(Handler()).GetDebug();
00182                 char slask[200];
00183                 sprintf(slask, "<input type=checkbox name=debug value=1%s>     * 0 - choke<br>", (debug & 1) ? " checked" : "");
00184                 Send(slask);
00185                 sprintf(slask, "<input type=checkbox name=debug value=2%s>     * 1 - unchoke<br>", (debug & 2) ? " checked" : "");
00186                 Send(slask);
00187                 sprintf(slask, "<input type=checkbox name=debug value=4%s>     * 2 - interested<br>", (debug & 4) ? " checked" : "");
00188                 Send(slask);
00189                 sprintf(slask, "<input type=checkbox name=debug value=8%s>     * 3 - not interested<br>", (debug & 8) ? " checked" : "");
00190                 Send(slask);
00191                 sprintf(slask, "<input type=checkbox name=debug value=16%s>     * 4 - have [piece(integer)]<br>", (debug & 16) ? " checked" : "");
00192                 Send(slask);
00193                 sprintf(slask, "<input type=checkbox name=debug value=32%s>     * 5 - bitfield [bitmap]<br>", (debug & 32) ? " checked" : "");
00194                 Send(slask);
00195                 sprintf(slask, "<input type=checkbox name=debug value=64%s>     * 6 - request [index begin length]<br>", (debug & 64) ? " checked" : "");
00196                 Send(slask);
00197                 sprintf(slask, "<input type=checkbox name=debug value=128%s>     * 7 - piece [index begin piece(byte[])]<br>", (debug & 128) ? " checked" : "");
00198                 Send(slask);
00199                 sprintf(slask, "<input type=checkbox name=debug value=256%s>     * 8 - cancel [index begin length]<br>", (debug & 256) ? " checked" : "");
00200                 Send(slask);
00201                 sprintf(slask, "<input type=checkbox name=debug value=512%s> Time<br>", (debug & 512) ? " checked" : "");
00202                 Send(slask);
00203                 sprintf(slask, "<input type=checkbox name=debug value=1024%s> Connect<br>", (debug & 1024) ? " checked" : "");
00204                 Send(slask);
00205                 sprintf(slask, "<input type=checkbox name=debug value=2048%s> SocketLog<br>", (debug & 2048) ? " checked" : "");
00206                 Send(slask);
00207                 sprintf(slask, "<input type=checkbox name=debug value=4096%s> Speeds<br>", (debug & 4096) ? " checked" : "");
00208                 Send(slask);
00209                 Send("<input type=submit name=submit value=' Update '>");
00210                 }
00211                 Send("</form>");
00212                 break;
00213         case 3:
00214                 static_cast<PeerHandler&>(Handler()).Show( this );
00215                 break;
00216         } // switch (m_sheet)
00217         
00218 
00219         //
00220         Send("</body></html>");
00221 
00222         // close
00223         SetCloseAndDelete();
00224 }
00225 
00226 
00227 void StatusSocket::SetTheCookie()
00228 {
00229         char slask[1000];
00230         sprintf(slask, "%d", m_sheet);
00231         HttpdCookies *cs = GetCookies();
00232         if (!cs)
00233                 return;
00234         cs -> setcookie(this, COOKIE_DOMAIN, COOKIE_PATH, "bleep", slask);
00235 }
00236 
00237 
00238 void StatusSocket::menyval(int val,int sheet,const std::string& text)
00239 {
00240         char slask[1000];
00241         Send("&nbsp;&nbsp;&nbsp;");
00242         if (val == sheet)
00243         {
00244                 sprintf(slask,"<a href='/?sheet=%d'>%s</a>", val, text.c_str());
00245         }
00246         else
00247         {
00248                 sprintf(slask,"<a style='text-decoration: none;' href='/?sheet=%d'>%s</a>", val, text.c_str());
00249         }
00250         Send(slask);
00251         Send("&nbsp;&nbsp;&nbsp;");
00252 }
00253 
00254 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4