00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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