Logo
~Sockets~
~Examples~
~Contact~


HttpDebugSocket Class Reference
[HTTP Sockets]

HTTP request "echo" class. More...

#include <HttpDebugSocket.h>

Inheritance diagram for HttpDebugSocket:

Inheritance graph
[legend]
Collaboration diagram for HttpDebugSocket:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 HttpDebugSocket (ISocketHandler &)
 ~HttpDebugSocket ()
void Init ()
 CTcpSocket uses this to create its ICrypt member variable.
void OnFirst ()
 Callback executes when first line has been received.
void OnHeader (const std::string &key, const std::string &value)
 For each header line this callback is executed.
void OnHeaderComplete ()
 Callback fires when all http headers have been received.
void OnData (const char *, size_t)
 Chunk of http body data recevied.

Private Member Functions

 HttpDebugSocket (const HttpDebugSocket &s)
HttpDebugSocketoperator= (const HttpDebugSocket &)

Private Attributes

int m_content_length
int m_read_ptr

Detailed Description

HTTP request "echo" class.

This class echoes a http request/body with a html formatted page.

Definition at line 44 of file HttpDebugSocket.h.


Constructor & Destructor Documentation

HttpDebugSocket::HttpDebugSocket ( ISocketHandler  ) 

Definition at line 42 of file HttpDebugSocket.cpp.

00042                                                   : HTTPSocket(h)
00043 ,m_content_length(0)
00044 ,m_read_ptr(0)
00045 {
00046 }

HttpDebugSocket::~HttpDebugSocket (  ) 

Definition at line 49 of file HttpDebugSocket.cpp.

00050 {
00051 }

HttpDebugSocket::HttpDebugSocket ( const HttpDebugSocket s  )  [inline, private]

Definition at line 58 of file HttpDebugSocket.h.

00058 : HTTPSocket(s) {} // copy constructor


Member Function Documentation

void HttpDebugSocket::Init (  )  [virtual]

CTcpSocket uses this to create its ICrypt member variable.

The ICrypt member variable is created by a virtual method, therefore it can't be called directly from the CTcpSocket constructor. Also used to determine if incoming HTTP connection is normal (port 80) or ssl (port 443).

Reimplemented from Socket.

Definition at line 54 of file HttpDebugSocket.cpp.

References Socket::EnableSSL(), Socket::GetParent(), Socket::GetPort(), Socket::Handler(), LOG_LEVEL_WARNING, and ISocketHandler::LogError().

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 }

void HttpDebugSocket::OnFirst (  )  [virtual]

Callback executes when first line has been received.

GetMethod, GetUrl/GetUri, and GetHttpVersion are valid when this callback is executed.

Implements HTTPSocket.

Definition at line 67 of file HttpDebugSocket.cpp.

References HTTPSocket::GetHttpVersion(), HTTPSocket::GetMethod(), HTTPSocket::GetUrl(), and TcpSocket::Send().

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 }

void HttpDebugSocket::OnHeader ( const std::string &  key,
const std::string &  value 
) [virtual]

For each header line this callback is executed.

Parameters:
key Http header name
value Http header value

Implements HTTPSocket.

Definition at line 82 of file HttpDebugSocket.cpp.

References m_content_length, and TcpSocket::Send().

00083 {
00084         if (!strcasecmp(key.c_str(),"content-length"))
00085                 m_content_length = atoi(value.c_str());
00086 
00087         Send(key + ": " + value + "\n");
00088 }

void HttpDebugSocket::OnHeaderComplete (  )  [virtual]

Callback fires when all http headers have been received.

Implements HTTPSocket.

Definition at line 91 of file HttpDebugSocket.cpp.

References m_content_length, TcpSocket::Send(), and Socket::SetCloseAndDelete().

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 }

void HttpDebugSocket::OnData ( const char *  ,
size_t   
) [virtual]

Chunk of http body data recevied.

Implements HTTPSocket.

Definition at line 105 of file HttpDebugSocket.cpp.

References m_content_length, m_read_ptr, TcpSocket::Send(), TcpSocket::SendBuf(), and Socket::SetCloseAndDelete().

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 }

HttpDebugSocket& HttpDebugSocket::operator= ( const HttpDebugSocket  )  [inline, private]

Definition at line 59 of file HttpDebugSocket.h.

00059 { return *this; } // assignment operator


Member Data Documentation

Definition at line 60 of file HttpDebugSocket.h.

Referenced by OnData(), OnHeader(), and OnHeaderComplete().

Definition at line 61 of file HttpDebugSocket.h.

Referenced by OnData().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4