![]() |
HttpClientSocket Class ReferenceGet http response to file or memory.
More...
|
Public Member Functions | |
HttpClientSocket (ISocketHandler &) | |
HttpClientSocket (ISocketHandler &, const std::string &url_in) | |
HttpClientSocket (ISocketHandler &, const std::string &host, port_t port, const std::string &url_in) | |
~HttpClientSocket () | |
void | Url (const std::string &url_in, std::string &host, port_t &port) |
Parse url to protocol,host,port,url and file. | |
void | OnFirst () |
Callback executes when first line has been received. | |
void | OnHeader (const std::string &, const std::string &) |
For each header line this callback is executed. | |
void | OnHeaderComplete () |
Callback fires when all http headers have been received. | |
void | OnData (const char *, size_t) |
Chunk of http body data recevied. | |
void | OnDelete () |
Called before a socket class is deleted by the ISocketHandler. | |
virtual void | OnContent () |
New callback method fires when all data is received. | |
void | SetFilename (const std::string &) |
Write response to this file. | |
const std::string & | Filename () const |
void | SetDataPtr (unsigned char *, size_t) |
Store response in this buffer. | |
const std::string & | GetContent () |
Get response headers. | |
size_t | GetContentLength () |
Get size of response body. | |
const std::string & | GetContentType () |
Get content type from response header. | |
size_t | GetContentPtr () |
Get size of received response body. | |
size_t | GetPos () |
Get size of received response body. | |
bool | Complete () |
Complete response has been received. | |
const unsigned char * | GetDataPtr () const |
Get ptr to response data buffer. | |
void | SetCloseOnComplete (bool=true) |
Close socket when response received. | |
const std::string & | GetUrlProtocol () |
Get protocol used from url. | |
const std::string & | GetUrlHost () |
Get hostname from url. | |
port_t | GetUrlPort () |
Get port from url. | |
const std::string & | GetUrlFilename () |
Get filename part of url. | |
Protected Member Functions | |
HttpClientSocket (const HttpClientSocket &s) | |
HttpClientSocket & | operator= (const HttpClientSocket &) |
Private Member Functions | |
void | EndConnection () |
Private Attributes | |
std::string | m_filename |
Filename to write response to. | |
unsigned char * | m_data_ptr |
Ptr to buffer where to store response. | |
size_t | m_data_size |
Max size of data buffer. | |
size_t | m_content_length |
Content-length header received from remote. | |
bool | m_content_length_is_set |
std::string | m_content |
Received http headers. | |
bool | m_data_ptr_set |
Buffer set from outside, do not delete. | |
IFile * | m_fil |
Output file. | |
size_t | m_content_ptr |
Number of bytes received from body. | |
bool | m_b_complete |
The entire content-length number of bytes has been received. | |
bool | m_b_close_when_complete |
Close when the full response has been received. | |
std::string | m_protocol |
Protocol part of url_in. | |
std::string | m_host |
Hostname from url_in. | |
port_t | m_port |
Port from url_in. | |
std::string | m_url_filename |
Filename from url_in. | |
std::string | m_content_type |
Content-type: header from response. |
Definition at line 47 of file HttpClientSocket.h.
HttpClientSocket::HttpClientSocket | ( | ISocketHandler & | h | ) |
Definition at line 47 of file HttpClientSocket.cpp.
00048 :HTTPSocket(h) 00049 ,m_data_ptr(NULL) 00050 ,m_data_size(0) 00051 ,m_content_length(0) 00052 ,m_content_length_is_set(false) 00053 ,m_data_ptr_set(false) 00054 ,m_fil(NULL) 00055 ,m_content_ptr(0) 00056 ,m_b_complete(false) 00057 ,m_b_close_when_complete(false) 00058 { 00059 }
HttpClientSocket::HttpClientSocket | ( | ISocketHandler & | h, | |
const std::string & | url_in | |||
) |
Definition at line 62 of file HttpClientSocket.cpp.
References m_host, m_port, m_protocol, m_url_filename, HTTPSocket::SetUrl(), and HTTPSocket::url_this().
00063 :HTTPSocket(h) 00064 ,m_data_ptr(NULL) 00065 ,m_data_size(0) 00066 ,m_content_length(0) 00067 ,m_content_length_is_set(false) 00068 ,m_data_ptr_set(false) 00069 ,m_fil(NULL) 00070 ,m_content_ptr(0) 00071 ,m_b_complete(false) 00072 ,m_b_close_when_complete(false) 00073 { 00074 std::string url; 00075 url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename); 00076 SetUrl( url ); 00077 }
HttpClientSocket::HttpClientSocket | ( | ISocketHandler & | h, | |
const std::string & | host, | |||
port_t | port, | |||
const std::string & | url_in | |||
) |
Definition at line 80 of file HttpClientSocket.cpp.
References Utility::l2string(), m_host, m_port, m_protocol, m_url_filename, HTTPSocket::SetUrl(), and HTTPSocket::url_this().
00081 :HTTPSocket(h) 00082 ,m_data_ptr(NULL) 00083 ,m_data_size(0) 00084 ,m_content_length(0) 00085 ,m_content_length_is_set(false) 00086 ,m_data_ptr_set(false) 00087 ,m_fil(NULL) 00088 ,m_content_ptr(0) 00089 ,m_b_complete(false) 00090 ,m_b_close_when_complete(false) 00091 { 00092 std::string url; 00093 std::string tmp = "http://" + host + ":" + Utility::l2string(port) + url_in; 00094 url_this(tmp, m_protocol, m_host, m_port, url, m_url_filename); 00095 SetUrl( url ); 00096 }
HttpClientSocket::~HttpClientSocket | ( | ) |
Definition at line 99 of file HttpClientSocket.cpp.
References m_data_ptr, m_data_ptr_set, and m_fil.
00100 { 00101 if (m_data_ptr && !m_data_ptr_set) 00102 { 00103 delete[] m_data_ptr; 00104 } 00105 if (m_fil) 00106 { 00107 m_fil -> fclose(); 00108 } 00109 }
HttpClientSocket::HttpClientSocket | ( | const HttpClientSocket & | s | ) | [inline, protected] |
void HttpClientSocket::Url | ( | const std::string & | url_in, | |
std::string & | host, | |||
port_t & | port | |||
) |
Parse url to protocol,host,port,url and file.
Definition at line 313 of file HttpClientSocket.cpp.
References GetUrlHost(), GetUrlPort(), m_host, m_port, m_protocol, m_url_filename, HTTPSocket::SetUrl(), and HTTPSocket::url_this().
00314 { 00315 std::string url; 00316 url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename); 00317 SetUrl(url); 00318 host = GetUrlHost(); 00319 port = GetUrlPort(); 00320 }
void HttpClientSocket::OnFirst | ( | ) | [virtual] |
Callback executes when first line has been received.
GetMethod, GetUrl/GetUri, and GetHttpVersion are valid when this callback is executed.
Implements HTTPSocket.
Definition at line 112 of file HttpClientSocket.cpp.
References HTTPSocket::GetHttpVersion(), HTTPSocket::GetStatus(), HTTPSocket::GetStatusText(), Socket::Handler(), HTTPSocket::IsResponse(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_content, and Socket::SetCloseAndDelete().
00113 { 00114 if (!IsResponse()) 00115 { 00116 Handler().LogError(this, "OnFirst", 0, "Response expected but not received - aborting", LOG_LEVEL_FATAL); 00117 SetCloseAndDelete(); 00118 } 00119 m_content = GetHttpVersion() + " " + GetStatus() + " " + GetStatusText() + "\r\n"; 00120 }
void HttpClientSocket::OnHeader | ( | const std::string & | key, | |
const std::string & | value | |||
) | [virtual] |
For each header line this callback is executed.
key | Http header name | |
value | Http header value |
Implements HTTPSocket.
Definition at line 123 of file HttpClientSocket.cpp.
References m_content, m_content_length, m_content_length_is_set, and m_content_type.
00124 { 00125 m_content += key + ": " + value + "\r\n"; 00126 if (!strcasecmp(key.c_str(), "content-length")) 00127 { 00128 m_content_length = atoi(value.c_str()); 00129 m_content_length_is_set = true; 00130 } 00131 else 00132 if (!strcasecmp(key.c_str(), "content-type")) 00133 { 00134 m_content_type = value; 00135 } 00136 }
void HttpClientSocket::OnHeaderComplete | ( | ) | [virtual] |
Callback fires when all http headers have been received.
Implements HTTPSocket.
Definition at line 139 of file HttpClientSocket.cpp.
References EndConnection(), m_content_length, m_content_length_is_set, m_data_ptr, m_data_size, m_fil, and m_filename.
00140 { 00141 if (m_filename.size()) 00142 { 00143 m_fil = new File; 00144 if (!m_fil -> fopen(m_filename, "wb")) 00145 { 00146 delete m_fil; 00147 m_fil = NULL; 00148 } 00149 } 00150 if (!m_data_ptr && m_content_length > 0) 00151 { 00152 m_data_ptr = new unsigned char[m_content_length]; 00153 m_data_size = m_content_length; 00154 } 00155 // make sure we finish in a good way even when response 00156 // has content-length: 0 00157 if (m_content_length_is_set && m_content_length == 0) 00158 { 00159 EndConnection(); 00160 } 00161 }
void HttpClientSocket::OnData | ( | const char * | , | |
size_t | ||||
) | [virtual] |
Chunk of http body data recevied.
Implements HTTPSocket.
Definition at line 181 of file HttpClientSocket.cpp.
References EndConnection(), Socket::Handler(), LOG_LEVEL_ERROR, ISocketHandler::LogError(), m_content_length, m_content_ptr, m_data_ptr, m_data_size, and m_fil.
00182 { 00183 if (m_fil) 00184 { 00185 m_fil -> fwrite(buf, 1, len); 00186 } 00187 if (m_data_ptr) 00188 { 00189 size_t left = m_data_size - m_content_ptr; 00190 size_t sz = len < left ? len : left; 00191 if (sz > 0) 00192 memcpy(m_data_ptr + m_content_ptr, buf, sz); 00193 m_content_ptr += sz; 00194 if (len > left) 00195 { 00196 Handler().LogError(this, "OnData", -1, "content buffer overflow", LOG_LEVEL_ERROR); 00197 } 00198 } 00199 if (m_content_ptr == m_content_length && m_content_length) 00200 { 00201 EndConnection(); 00202 } 00203 }
void HttpClientSocket::OnDelete | ( | ) | [virtual] |
Called before a socket class is deleted by the ISocketHandler.
Reimplemented from Socket.
Definition at line 206 of file HttpClientSocket.cpp.
References m_b_complete, m_fil, and OnContent().
00207 { 00208 if (!m_b_complete) 00209 { 00210 if (m_fil) 00211 { 00212 m_fil -> fclose(); 00213 delete m_fil; 00214 m_fil = NULL; 00215 } 00216 m_b_complete = true; 00217 OnContent(); 00218 } 00219 }
void HttpClientSocket::OnContent | ( | ) | [virtual] |
New callback method fires when all data is received.
Definition at line 272 of file HttpClientSocket.cpp.
Referenced by EndConnection(), and OnDelete().
void HttpClientSocket::SetFilename | ( | const std::string & | x | ) |
Write response to this file.
Definition at line 222 of file HttpClientSocket.cpp.
References m_filename.
Referenced by HttpGetSocket::HttpGetSocket().
00223 { 00224 m_filename = x; 00225 }
const std::string& HttpClientSocket::Filename | ( | ) | const [inline] |
void HttpClientSocket::SetDataPtr | ( | unsigned char * | buf, | |
size_t | len | |||
) |
Store response in this buffer.
Definition at line 228 of file HttpClientSocket.cpp.
References m_data_ptr, m_data_ptr_set, and m_data_size.
00229 { 00230 m_data_ptr = buf; 00231 m_data_size = len; 00232 m_data_ptr_set = true; 00233 }
const std::string & HttpClientSocket::GetContent | ( | ) |
Get response headers.
Definition at line 236 of file HttpClientSocket.cpp.
References m_content.
00237 { 00238 return m_content; 00239 }
size_t HttpClientSocket::GetContentLength | ( | ) |
Get size of response body.
Definition at line 242 of file HttpClientSocket.cpp.
References m_content_length.
00243 { 00244 return m_content_length; 00245 }
const std::string & HttpClientSocket::GetContentType | ( | ) |
Get content type from response header.
Definition at line 307 of file HttpClientSocket.cpp.
References m_content_type.
00308 { 00309 return m_content_type; 00310 }
size_t HttpClientSocket::GetContentPtr | ( | ) |
Get size of received response body.
Definition at line 248 of file HttpClientSocket.cpp.
References m_content_ptr.
00249 { 00250 return m_content_ptr; 00251 }
size_t HttpClientSocket::GetPos | ( | ) |
Get size of received response body.
Definition at line 254 of file HttpClientSocket.cpp.
References m_content_ptr.
00255 { 00256 return m_content_ptr; 00257 }
bool HttpClientSocket::Complete | ( | ) |
Complete response has been received.
Definition at line 260 of file HttpClientSocket.cpp.
References m_b_complete.
00261 { 00262 return m_b_complete; 00263 }
const unsigned char * HttpClientSocket::GetDataPtr | ( | ) | const |
Get ptr to response data buffer.
Definition at line 266 of file HttpClientSocket.cpp.
References m_data_ptr.
00267 { 00268 return m_data_ptr; 00269 }
void HttpClientSocket::SetCloseOnComplete | ( | bool | x = true |
) |
Close socket when response received.
Definition at line 277 of file HttpClientSocket.cpp.
References m_b_close_when_complete.
00278 { 00279 m_b_close_when_complete = x; 00280 }
const std::string & HttpClientSocket::GetUrlProtocol | ( | ) |
Get protocol used from url.
Definition at line 283 of file HttpClientSocket.cpp.
References m_protocol.
00284 { 00285 return m_protocol; 00286 }
const std::string & HttpClientSocket::GetUrlHost | ( | ) |
Get hostname from url.
Definition at line 289 of file HttpClientSocket.cpp.
References m_host.
Referenced by HttpGetSocket::DoConnect(), HttpPostSocket::DoMultipartPost(), HttpPutSocket::OnConnect(), HttpPostSocket::OnConnect(), HttpGetSocket::OnConnect(), HttpPutSocket::Open(), HttpPostSocket::Open(), and Url().
00290 { 00291 return m_host; 00292 }
port_t HttpClientSocket::GetUrlPort | ( | ) |
Get port from url.
Definition at line 295 of file HttpClientSocket.cpp.
References m_port.
Referenced by HttpGetSocket::DoConnect(), HttpGetSocket::OnConnect(), HttpPutSocket::Open(), HttpPostSocket::Open(), and Url().
00296 { 00297 return m_port; 00298 }
const std::string & HttpClientSocket::GetUrlFilename | ( | ) |
Get filename part of url.
Definition at line 301 of file HttpClientSocket.cpp.
References m_url_filename.
00302 { 00303 return m_url_filename; 00304 }
HttpClientSocket& HttpClientSocket::operator= | ( | const HttpClientSocket & | ) | [inline, protected] |
void HttpClientSocket::EndConnection | ( | ) | [private] |
Definition at line 164 of file HttpClientSocket.cpp.
References m_b_close_when_complete, m_b_complete, m_fil, OnContent(), and Socket::SetCloseAndDelete().
Referenced by OnData(), and OnHeaderComplete().
00165 { 00166 if (m_fil) 00167 { 00168 m_fil -> fclose(); 00169 delete m_fil; 00170 m_fil = NULL; 00171 } 00172 m_b_complete = true; 00173 OnContent(); 00174 if (m_b_close_when_complete) 00175 { 00176 SetCloseAndDelete(); 00177 } 00178 }
std::string HttpClientSocket::m_filename [private] |
Filename to write response to.
Reimplemented in HttpPutSocket.
Definition at line 111 of file HttpClientSocket.h.
Referenced by OnHeaderComplete(), and SetFilename().
unsigned char* HttpClientSocket::m_data_ptr [private] |
Ptr to buffer where to store response.
Definition at line 112 of file HttpClientSocket.h.
Referenced by GetDataPtr(), OnData(), OnHeaderComplete(), SetDataPtr(), and ~HttpClientSocket().
size_t HttpClientSocket::m_data_size [private] |
Max size of data buffer.
Definition at line 113 of file HttpClientSocket.h.
Referenced by OnData(), OnHeaderComplete(), and SetDataPtr().
size_t HttpClientSocket::m_content_length [private] |
Content-length header received from remote.
Reimplemented in HttpPostSocket, and HttpPutSocket.
Definition at line 114 of file HttpClientSocket.h.
Referenced by GetContentLength(), OnData(), OnHeader(), and OnHeaderComplete().
bool HttpClientSocket::m_content_length_is_set [private] |
Definition at line 115 of file HttpClientSocket.h.
Referenced by OnHeader(), and OnHeaderComplete().
std::string HttpClientSocket::m_content [private] |
Received http headers.
Definition at line 116 of file HttpClientSocket.h.
Referenced by GetContent(), OnFirst(), and OnHeader().
bool HttpClientSocket::m_data_ptr_set [private] |
Buffer set from outside, do not delete.
Definition at line 117 of file HttpClientSocket.h.
Referenced by SetDataPtr(), and ~HttpClientSocket().
IFile* HttpClientSocket::m_fil [private] |
Output file.
Definition at line 118 of file HttpClientSocket.h.
Referenced by EndConnection(), OnData(), OnDelete(), OnHeaderComplete(), and ~HttpClientSocket().
size_t HttpClientSocket::m_content_ptr [private] |
Number of bytes received from body.
Definition at line 119 of file HttpClientSocket.h.
Referenced by GetContentPtr(), GetPos(), and OnData().
bool HttpClientSocket::m_b_complete [private] |
The entire content-length number of bytes has been received.
Definition at line 120 of file HttpClientSocket.h.
Referenced by Complete(), EndConnection(), and OnDelete().
bool HttpClientSocket::m_b_close_when_complete [private] |
Close when the full response has been received.
Definition at line 121 of file HttpClientSocket.h.
Referenced by EndConnection(), and SetCloseOnComplete().
std::string HttpClientSocket::m_protocol [private] |
Protocol part of url_in.
Definition at line 122 of file HttpClientSocket.h.
Referenced by GetUrlProtocol(), HttpClientSocket(), and Url().
std::string HttpClientSocket::m_host [private] |
Hostname from url_in.
Definition at line 123 of file HttpClientSocket.h.
Referenced by GetUrlHost(), HttpClientSocket(), and Url().
port_t HttpClientSocket::m_port [private] |
Port from url_in.
Definition at line 124 of file HttpClientSocket.h.
Referenced by GetUrlPort(), HttpClientSocket(), and Url().
std::string HttpClientSocket::m_url_filename [private] |
Filename from url_in.
Definition at line 125 of file HttpClientSocket.h.
Referenced by GetUrlFilename(), HttpClientSocket(), and Url().
std::string HttpClientSocket::m_content_type [private] |
Content-type: header from response.
Reimplemented in HttpPostSocket, and HttpPutSocket.
Definition at line 126 of file HttpClientSocket.h.
Referenced by GetContentType(), and OnHeader().