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