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 44 of file HttpDebugSocket.h.
| HttpDebugSocket::HttpDebugSocket | ( | ISocketHandler & | h | ) |
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 | ( | ) |
| 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 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(), TcpSocket::Send(), and TcpSocket::Sendf().
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>"); 00078 Send( "<form method='post' action='/test_post'>" 00079 "<input type='text' name='text' value='test text'><br>" 00080 "<input type='submit' name='submit' value=' OK '></form>"); 00081 00082 // enctype 'multipart/form-data' 00083 Sendf("<form action='/test_post' method='post' enctype='multipart/form-data'>"); 00084 Sendf("<input type=file name=the_file><br>"); 00085 Sendf("<input type=text name=the_name><br>"); 00086 Sendf("<input type=submit name=submit value=' test form-data '>"); 00087 Sendf("</form>"); 00088 00089 Send( "<pre style='background: #e0e0e0'>"); 00090 Send(GetMethod() + " " + GetUrl() + " " + GetHttpVersion() + "\n"); 00091 }
| 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 94 of file HttpDebugSocket.cpp.
References m_content_length, and TcpSocket::Send().
00095 { 00096 if (!strcasecmp(key.c_str(),"content-length")) 00097 m_content_length = atoi(value.c_str()); 00098 00099 Send(key + ": " + value + "\n"); 00100 }
| void HttpDebugSocket::OnHeaderComplete | ( | ) | [virtual] |
Callback fires when all http headers have been received.
Implements HTTPSocket.
Definition at line 103 of file HttpDebugSocket.cpp.
References HTTPSocket::IsChunked(), m_content_length, TcpSocket::Send(), and Socket::SetCloseAndDelete().
00104 { 00105 if (m_content_length || IsChunked()) 00106 { 00107 Send("</pre><h3>Request Body</h3><pre style='background: #e0e0e0'>"); 00108 } 00109 else 00110 { 00111 Send("</pre><hr></body></html>"); 00112 SetCloseAndDelete(); 00113 } 00114 }
| void HttpDebugSocket::OnData | ( | const char * | , | |
| size_t | ||||
| ) | [virtual] |
Chunk of http body data recevied.
Implements HTTPSocket.
Definition at line 117 of file HttpDebugSocket.cpp.
References m_content_length, m_read_ptr, TcpSocket::Send(), TcpSocket::SendBuf(), and Socket::SetCloseAndDelete().
00118 { 00119 SendBuf(p,l); 00120 m_read_ptr += (int)l; 00121 if (m_read_ptr >= m_content_length && m_content_length) 00122 { 00123 Send("</pre><hr></body></html>"); 00124 SetCloseAndDelete(); 00125 } 00126 }
| void HttpDebugSocket::OnDataComplete | ( | ) | [virtual] |
The full request/response body has been received.
Reimplemented from HTTPSocket.
Definition at line 129 of file HttpDebugSocket.cpp.
References Socket::CloseAndDelete(), TcpSocket::Send(), and Socket::SetCloseAndDelete().
00130 { 00131 if (!CloseAndDelete()) 00132 { 00133 Send("</pre><hr></body></html>"); 00134 SetCloseAndDelete(); 00135 } 00136 }
| HttpDebugSocket& HttpDebugSocket::operator= | ( | const HttpDebugSocket & | ) | [inline, private] |
int HttpDebugSocket::m_content_length [private] |
Definition at line 61 of file HttpDebugSocket.h.
Referenced by OnData(), OnHeader(), and OnHeaderComplete().
int HttpDebugSocket::m_read_ptr [private] |
1.4.4