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
00030
00031 #ifdef _MSC_VER
00032 #pragma warning(disable:4786)
00033 #endif
00034 #include "HttpDebugSocket.h"
00035 #include "ISocketHandler.h"
00036
00037
00038 #ifdef SOCKETS_NAMESPACE
00039 namespace SOCKETS_NAMESPACE {
00040 #endif
00041
00042
00043 HttpDebugSocket::HttpDebugSocket(ISocketHandler& h) : HTTPSocket(h)
00044 ,m_content_length(0)
00045 ,m_read_ptr(0)
00046 {
00047 }
00048
00049
00050 HttpDebugSocket::~HttpDebugSocket()
00051 {
00052 }
00053
00054
00055 void HttpDebugSocket::Init()
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 }
00066
00067
00068 void HttpDebugSocket::OnFirst()
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
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 }
00093
00094
00095 void HttpDebugSocket::OnHeader(const std::string& key,const std::string& value)
00096 {
00097 if (!strcasecmp(key.c_str(),"content-length"))
00098 m_content_length = atoi(value.c_str());
00099
00100 Send(key + ": " + value + "\n");
00101 }
00102
00103
00104 void HttpDebugSocket::OnHeaderComplete()
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 }
00116
00117
00118 void HttpDebugSocket::OnData(const char *p,size_t l)
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 }
00128
00129
00130 void HttpDebugSocket::OnDataComplete()
00131 {
00132 if (!CloseAndDelete())
00133 {
00134 Send("</pre><hr></body></html>");
00135 SetCloseAndDelete();
00136 }
00137 }
00138
00139
00140 #ifdef SOCKETS_NAMESPACE
00141 }
00142 #endif
00143
00144