00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef _WIN32
00024 #pragma warning(disable:4786)
00025 #endif
00026 #include "HttpClientSocket.h"
00027 #include "StdLog.h"
00028 #include "ISocketHandler.h"
00029
00030
00031 #ifdef SOCKETS_NAMESPACE
00032 namespace SOCKETS_NAMESPACE {
00033 #endif
00034
00035
00036 HttpClientSocket::HttpClientSocket(ISocketHandler& h)
00037 :HTTPSocket(h)
00038 ,m_data_ptr(NULL)
00039 ,m_data_size(0)
00040 ,m_content_length(0)
00041 ,m_data_ptr_set(false)
00042 ,m_fil(NULL)
00043 ,m_content_ptr(0)
00044 ,m_b_complete(false)
00045 ,m_b_close_when_complete(false)
00046 {
00047 }
00048
00049
00050 HttpClientSocket::HttpClientSocket(ISocketHandler& h,const std::string& url_in)
00051 :HTTPSocket(h)
00052 ,m_data_ptr(NULL)
00053 ,m_data_size(0)
00054 ,m_content_length(0)
00055 ,m_data_ptr_set(false)
00056 ,m_fil(NULL)
00057 ,m_content_ptr(0)
00058 ,m_b_complete(false)
00059 ,m_b_close_when_complete(false)
00060 {
00061 std::string url;
00062 url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename);
00063 SetUrl(url);
00064 }
00065
00066
00067 HttpClientSocket::~HttpClientSocket()
00068 {
00069 if (m_data_ptr && !m_data_ptr_set)
00070 {
00071 delete[] m_data_ptr;
00072 }
00073 if (m_fil)
00074 {
00075 fclose(m_fil);
00076 }
00077 }
00078
00079
00080 void HttpClientSocket::OnFirst()
00081 {
00082 if (!IsResponse())
00083 {
00084 Handler().LogError(this, "OnFirst", 0, "Response expected but not received - aborting", LOG_LEVEL_FATAL);
00085 SetCloseAndDelete();
00086 }
00087 m_content = GetHttpVersion() + " " + GetStatus() + " " + GetStatusText() + "\r\n";
00088 }
00089
00090
00091 void HttpClientSocket::OnHeader(const std::string& key,const std::string& value)
00092 {
00093 m_content += key + ": " + value + "\r\n";
00094 if (!strcasecmp(key.c_str(), "content-length"))
00095 {
00096 m_content_length = atoi(value.c_str());
00097 }
00098 else
00099 if (!strcasecmp(key.c_str(), "content-type"))
00100 {
00101 m_content_type = value;
00102 }
00103 }
00104
00105
00106 void HttpClientSocket::OnHeaderComplete()
00107 {
00108 if (m_filename.size())
00109 {
00110 m_fil = fopen(m_filename.c_str(), "wb");
00111 }
00112 else
00113 if (!m_data_ptr && m_content_length)
00114 {
00115 m_data_ptr = new unsigned char[m_content_length];
00116 m_data_size = m_content_length;
00117 }
00118 }
00119
00120
00121 void HttpClientSocket::OnData(const char *buf,size_t len)
00122 {
00123 if (m_fil)
00124 {
00125 fwrite(buf, 1, len, m_fil);
00126 }
00127 else
00128 if (m_data_ptr)
00129 {
00130 if (m_content_ptr + len > m_data_size)
00131 {
00132 Handler().LogError(this, "OnData", -1, "content buffer overflow", LOG_LEVEL_ERROR);
00133 }
00134 else
00135 {
00136 memcpy(m_data_ptr + m_content_ptr, buf, len);
00137 }
00138 }
00139 m_content_ptr += len;
00140 if (m_content_ptr == m_content_length && m_content_length)
00141 {
00142 if (m_fil)
00143 {
00144 fclose(m_fil);
00145 m_fil = NULL;
00146 }
00147 m_b_complete = true;
00148 OnContent();
00149 if (m_b_close_when_complete)
00150 {
00151 SetCloseAndDelete();
00152 }
00153 }
00154 }
00155
00156
00157 void HttpClientSocket::OnDelete()
00158 {
00159 if (!m_b_complete)
00160 {
00161 if (m_fil)
00162 {
00163 fclose(m_fil);
00164 m_fil = NULL;
00165 }
00166 m_b_complete = true;
00167 OnContent();
00168 }
00169 }
00170
00171
00172 void HttpClientSocket::SetFilename(const std::string& x)
00173 {
00174 m_filename = x;
00175 }
00176
00177
00178 void HttpClientSocket::SetDataPtr(unsigned char *buf,size_t len)
00179 {
00180 m_data_ptr = buf;
00181 m_data_size = len;
00182 m_data_ptr_set = true;
00183 }
00184
00185
00186 const std::string& HttpClientSocket::GetContent()
00187 {
00188 return m_content;
00189 }
00190
00191
00192 size_t HttpClientSocket::GetContentLength()
00193 {
00194 return m_content_length;
00195 }
00196
00197
00198 size_t HttpClientSocket::GetContentPtr()
00199 {
00200 return m_content_ptr;
00201 }
00202
00203
00204 size_t HttpClientSocket::GetPos()
00205 {
00206 return m_content_ptr;
00207 }
00208
00209
00210 bool HttpClientSocket::Complete()
00211 {
00212 return m_b_complete;
00213 }
00214
00215
00216 const unsigned char *HttpClientSocket::GetDataPtr() const
00217 {
00218 return m_data_ptr;
00219 }
00220
00221
00222 void HttpClientSocket::OnContent()
00223 {
00224 }
00225
00226
00227 void HttpClientSocket::SetCloseOnComplete(bool x)
00228 {
00229 m_b_close_when_complete = x;
00230 }
00231
00232
00233 const std::string& HttpClientSocket::GetUrlProtocol()
00234 {
00235 return m_protocol;
00236 }
00237
00238
00239 const std::string& HttpClientSocket::GetUrlHost()
00240 {
00241 return m_host;
00242 }
00243
00244
00245 port_t HttpClientSocket::GetUrlPort()
00246 {
00247 return m_port;
00248 }
00249
00250
00251 const std::string& HttpClientSocket::GetUrlFilename()
00252 {
00253 return m_url_filename;
00254 }
00255
00256
00257 const std::string& HttpClientSocket::GetContentType()
00258 {
00259 return m_content_type;
00260 }
00261
00262
00263 void HttpClientSocket::Url(const std::string& url_in,std::string& host,port_t& port)
00264 {
00265 std::string url;
00266 url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename);
00267 SetUrl(url);
00268 host = GetUrlHost();
00269 port = GetUrlPort();
00270 }
00271
00272
00273 #ifdef SOCKETS_NAMESPACE
00274 }
00275 #endif