![]() |
HttpDebugSocket Class ReferenceHTTP request "echo" class.
More...
|
Public Member Functions | |
HttpDebugSocket (ISocketHandler &) | |
~HttpDebugSocket () | |
void | Init () |
Called by ListenSocket after accept but before socket is added to handler. | |
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. | |
void | OnDataComplete () |
The full request/response body has been received. | |
Private Member Functions | |
HttpDebugSocket (const HttpDebugSocket &s) | |
HttpDebugSocket & | operator= (const HttpDebugSocket &) |
Private Attributes | |
int | m_content_length |
int | m_read_ptr |
This class echoes a http request/body with a html formatted page.
Definition at line 46 of file HttpDebugSocket.h.
HttpDebugSocket::HttpDebugSocket | ( | ISocketHandler & | h | ) |
Definition at line 43 of file HttpDebugSocket.cpp.
00043 : HTTPSocket(h) 00044 ,m_content_length(0) 00045 ,m_read_ptr(0) 00046 { 00047 }
HttpDebugSocket::~HttpDebugSocket | ( | ) |
HttpDebugSocket::HttpDebugSocket | ( | const HttpDebugSocket & | s | ) | [inline, private] |
void HttpDebugSocket::Init | ( | ) | [virtual] |
Called by ListenSocket after accept but before socket is added to handler.
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 55 of file HttpDebugSocket.cpp.
References Socket::EnableSSL(), Socket::GetParent(), Socket::GetPort(), Socket::Handler(), LOG_LEVEL_WARNING, and ISocketHandler::LogError().
00056 { 00057 if (GetParent() -> GetPort() == 443) 00058 { 00059 #ifdef HAVE_OPENSSL 00060 EnableSSL(); 00061 #else 00062 Handler().LogError(this, "url_this", -1, "SSL not available", LOG_LEVEL_WARNING); 00063 #endif 00064 } 00065 }
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 68 of file HttpDebugSocket.cpp.
References HTTPSocket::GetHttpVersion(), HTTPSocket::GetMethod(), HTTPSocket::GetUrl(), TcpSocket::Send(), and TcpSocket::Sendf().
00069 { 00070 Send( 00071 "HTTP/1.1 200 OK\n" 00072 "Content-type: text/html\n" 00073 "Connection: close\n" 00074 "Server: HttpDebugSocket/1.0\n" 00075 "\n"); 00076 Send( 00077 "<html><head><title>Echo Request</title></head>" 00078 "<body><h3>Request Header</h3>"); 00079 Send( "<form method='post' action='/test_post'>" 00080 "<input type='text' name='text' value='test text'><br>" 00081 "<input type='submit' name='submit' value=' OK '></form>"); 00082 00083 // enctype 'multipart/form-data' 00084 Sendf("<form action='/test_post' method='post' enctype='multipart/form-data'>"); 00085 Sendf("<input type=file name=the_file><br>"); 00086 Sendf("<input type=text name=the_name><br>"); 00087 Sendf("<input type=submit name=submit value=' test form-data '>"); 00088 Sendf("</form>"); 00089 00090 Send( "<pre style='background: #e0e0e0'>"); 00091 Send(GetMethod() + " " + GetUrl() + " " + GetHttpVersion() + "\n"); 00092 }
void HttpDebugSocket::OnHeader | ( | const std::string & | key, | |
const std::string & | value | |||
) | [virtual] |
For each header line this callback is executed.
key | Http header name | |
value | Http header value |
Implements HTTPSocket.
Definition at line 95 of file HttpDebugSocket.cpp.
References m_content_length, and TcpSocket::Send().
00096 { 00097 if (!strcasecmp(key.c_str(),"content-length")) 00098 m_content_length = atoi(value.c_str()); 00099 00100 Send(key + ": " + value + "\n"); 00101 }
void HttpDebugSocket::OnHeaderComplete | ( | ) | [virtual] |
Callback fires when all http headers have been received.
Implements HTTPSocket.
Definition at line 104 of file HttpDebugSocket.cpp.
References HTTPSocket::IsChunked(), m_content_length, TcpSocket::Send(), and Socket::SetCloseAndDelete().
00105 { 00106 if (m_content_length || IsChunked()) 00107 { 00108 Send("</pre><h3>Request Body</h3><pre style='background: #e0e0e0'>"); 00109 } 00110 else 00111 { 00112 Send("</pre><hr></body></html>"); 00113 SetCloseAndDelete(); 00114 } 00115 }
void HttpDebugSocket::OnData | ( | const char * | , | |
size_t | ||||
) | [virtual] |
Chunk of http body data recevied.
Implements HTTPSocket.
Definition at line 118 of file HttpDebugSocket.cpp.
References m_content_length, m_read_ptr, TcpSocket::Send(), TcpSocket::SendBuf(), and Socket::SetCloseAndDelete().
00119 { 00120 SendBuf(p,l); 00121 m_read_ptr += (int)l; 00122 if (m_read_ptr >= m_content_length && m_content_length) 00123 { 00124 Send("</pre><hr></body></html>"); 00125 SetCloseAndDelete(); 00126 } 00127 }
void HttpDebugSocket::OnDataComplete | ( | ) | [virtual] |
The full request/response body has been received.
Reimplemented from HTTPSocket.
Definition at line 130 of file HttpDebugSocket.cpp.
References Socket::CloseAndDelete(), TcpSocket::Send(), and Socket::SetCloseAndDelete().
00131 { 00132 if (!CloseAndDelete()) 00133 { 00134 Send("</pre><hr></body></html>"); 00135 SetCloseAndDelete(); 00136 } 00137 }
HttpDebugSocket& HttpDebugSocket::operator= | ( | const HttpDebugSocket & | ) | [inline, private] |
int HttpDebugSocket::m_content_length [private] |
Definition at line 63 of file HttpDebugSocket.h.
Referenced by OnData(), OnHeader(), and OnHeaderComplete().
int HttpDebugSocket::m_read_ptr [private] |