Logo
~Sockets~
~Examples~
~Contact~


HttpDebugSocket.cpp

Go to the documentation of this file.
00001 
00004 /*
00005 Copyright (C) 2004-2007  Anders Hedström (grymse@alhem.net)
00006 
00007 This library is made available under the terms of the GNU GPL.
00008 
00009 If you would like to use this library in a closed-source application,
00010 a separate license agreement is available. For information about 
00011 the closed-source license agreement for the C++ sockets library,
00012 please visit http://www.alhem.net/Sockets/license.html and/or
00013 email license@alhem.net.
00014 
00015 This program is free software; you can redistribute it and/or
00016 modify it under the terms of the GNU General Public License
00017 as published by the Free Software Foundation; either version 2
00018 of the License, or (at your option) any later version.
00019 
00020 This program is distributed in the hope that it will be useful,
00021 but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 GNU General Public License for more details.
00024 
00025 You should have received a copy of the GNU General Public License
00026 along with this program; if not, write to the Free Software
00027 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00028 */
00029 #ifdef _WIN32
00030 #pragma warning(disable:4786)
00031 #endif
00032 #include "HttpDebugSocket.h"
00033 #include "Utility.h"
00034 #include "ISocketHandler.h"
00035 
00036 
00037 #ifdef SOCKETS_NAMESPACE
00038 namespace SOCKETS_NAMESPACE {
00039 #endif
00040 
00041 
00042 HttpDebugSocket::HttpDebugSocket(ISocketHandler& h) : HTTPSocket(h)
00043 ,m_content_length(0)
00044 ,m_read_ptr(0)
00045 {
00046 }
00047 
00048 
00049 HttpDebugSocket::~HttpDebugSocket()
00050 {
00051 }
00052 
00053 
00054 void HttpDebugSocket::Init()
00055 {
00056         if (GetParent() -> GetPort() == 443)
00057         {
00058 #ifdef HAVE_OPENSSL
00059                 EnableSSL();
00060 #else
00061                 Handler().LogError(this, "url_this", -1, "SSL not available", LOG_LEVEL_WARNING);
00062 #endif
00063         }
00064 }
00065 
00066 
00067 void HttpDebugSocket::OnFirst()
00068 {
00069         Send(
00070                 "HTTP/1.1 200 OK\n"
00071                 "Content-type: text/html\n"
00072                 "Connection: close\n"
00073                 "Server: HttpDebugSocket/1.0\n"
00074                 "\n");
00075         Send(
00076                 "<html><head><title>Echo Request</title></head>"
00077                 "<body><h3>Request Header</h3><pre style='background: #e0e0e0'>");
00078         Send(GetMethod() + " " + GetUrl() + " " + GetHttpVersion() + "\n");
00079 }
00080 
00081 
00082 void HttpDebugSocket::OnHeader(const std::string& key,const std::string& value)
00083 {
00084         if (!strcasecmp(key.c_str(),"content-length"))
00085                 m_content_length = atoi(value.c_str());
00086 
00087         Send(key + ": " + value + "\n");
00088 }
00089 
00090 
00091 void HttpDebugSocket::OnHeaderComplete()
00092 {
00093         if (m_content_length)
00094         {
00095                 Send("</pre><h3>Request Body</h3><pre style='background: #e0e0e0'>");
00096         }
00097         else
00098         {
00099                 Send("</pre><hr></body></html>");
00100                 SetCloseAndDelete();
00101         }
00102 }
00103 
00104 
00105 void HttpDebugSocket::OnData(const char *p,size_t l)
00106 {
00107         SendBuf(p,l);
00108         m_read_ptr += (int)l;
00109         if (m_read_ptr >= m_content_length && m_content_length)
00110         {
00111                 Send("</pre><hr></body></html>");
00112                 SetCloseAndDelete();
00113         }
00114 }
00115 
00116 
00117 #ifdef SOCKETS_NAMESPACE
00118 }
00119 #endif
00120 
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4