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 _MSC_VER
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>");
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
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 }
00092
00093
00094 void HttpDebugSocket::OnHeader(const std::string& key,const std::string& value)
00095 {
00096 if (!strcasecmp(key.c_str(),"content-length"))
00097 m_content_length = atoi(value.c_str());
00098
00099 Send(key + ": " + value + "\n");
00100 }
00101
00102
00103 void HttpDebugSocket::OnHeaderComplete()
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 }
00115
00116
00117 void HttpDebugSocket::OnData(const char *p,size_t l)
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 }
00127
00128
00129 void HttpDebugSocket::OnDataComplete()
00130 {
00131 if (!CloseAndDelete())
00132 {
00133 Send("</pre><hr></body></html>");
00134 SetCloseAndDelete();
00135 }
00136 }
00137
00138
00139 #ifdef SOCKETS_NAMESPACE
00140 }
00141 #endif
00142
00143