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 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. | |
| 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_data_ptr_set(false) 00053 ,m_fil(NULL) 00054 ,m_content_ptr(0) 00055 ,m_b_complete(false) 00056 ,m_b_close_when_complete(false) 00057 { 00058 }
| HttpClientSocket::HttpClientSocket | ( | ISocketHandler & | h, | |
| const std::string & | url_in | |||
| ) |
Definition at line 61 of file HttpClientSocket.cpp.
References m_host, m_port, m_protocol, m_url_filename, HTTPSocket::SetUrl(), and HTTPSocket::url_this().
00062 :HTTPSocket(h) 00063 ,m_data_ptr(NULL) 00064 ,m_data_size(0) 00065 ,m_content_length(0) 00066 ,m_data_ptr_set(false) 00067 ,m_fil(NULL) 00068 ,m_content_ptr(0) 00069 ,m_b_complete(false) 00070 ,m_b_close_when_complete(false) 00071 { 00072 std::string url; 00073 url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename); 00074 SetUrl( url ); 00075 }
| HttpClientSocket::HttpClientSocket | ( | ISocketHandler & | h, | |
| const std::string & | host, | |||
| port_t | port, | |||
| const std::string & | url_in | |||
| ) |
Definition at line 78 of file HttpClientSocket.cpp.
References Utility::l2string(), m_host, m_port, m_protocol, m_url_filename, HTTPSocket::SetUrl(), and HTTPSocket::url_this().
00079 :HTTPSocket(h) 00080 ,m_data_ptr(NULL) 00081 ,m_data_size(0) 00082 ,m_content_length(0) 00083 ,m_data_ptr_set(false) 00084 ,m_fil(NULL) 00085 ,m_content_ptr(0) 00086 ,m_b_complete(false) 00087 ,m_b_close_when_complete(false) 00088 { 00089 std::string url; 00090 std::string tmp = "http://" + host + ":" + Utility::l2string(port) + url_in; 00091 url_this(tmp, m_protocol, m_host, m_port, url, m_url_filename); 00092 SetUrl( url ); 00093 }
| HttpClientSocket::~HttpClientSocket | ( | ) |
Definition at line 96 of file HttpClientSocket.cpp.
References m_data_ptr, m_data_ptr_set, and m_fil.
00097 { 00098 if (m_data_ptr && !m_data_ptr_set) 00099 { 00100 delete[] m_data_ptr; 00101 } 00102 if (m_fil) 00103 { 00104 m_fil -> fclose(); 00105 } 00106 }
| 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 299 of file HttpClientSocket.cpp.
References GetUrlHost(), GetUrlPort(), m_host, m_port, m_protocol, m_url_filename, HTTPSocket::SetUrl(), and HTTPSocket::url_this().
00300 { 00301 std::string url; 00302 url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename); 00303 SetUrl(url); 00304 host = GetUrlHost(); 00305 port = GetUrlPort(); 00306 }
| 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 109 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().
00110 { 00111 if (!IsResponse()) 00112 { 00113 Handler().LogError(this, "OnFirst", 0, "Response expected but not received - aborting", LOG_LEVEL_FATAL); 00114 SetCloseAndDelete(); 00115 } 00116 m_content = GetHttpVersion() + " " + GetStatus() + " " + GetStatusText() + "\r\n"; 00117 }
| 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 120 of file HttpClientSocket.cpp.
References m_content, m_content_length, and m_content_type.
00121 { 00122 m_content += key + ": " + value + "\r\n"; 00123 if (!strcasecmp(key.c_str(), "content-length")) 00124 { 00125 m_content_length = atoi(value.c_str()); 00126 } 00127 else 00128 if (!strcasecmp(key.c_str(), "content-type")) 00129 { 00130 m_content_type = value; 00131 } 00132 }
| void HttpClientSocket::OnHeaderComplete | ( | ) | [virtual] |
Callback fires when all http headers have been received.
Implements HTTPSocket.
Definition at line 135 of file HttpClientSocket.cpp.
References m_content_length, m_data_ptr, m_data_size, m_fil, and m_filename.
00136 { 00137 if (m_filename.size()) 00138 { 00139 m_fil = new File; 00140 if (!m_fil -> fopen(m_filename, "wb")) 00141 { 00142 delete m_fil; 00143 m_fil = NULL; 00144 } 00145 } 00146 else 00147 if (!m_data_ptr && m_content_length) 00148 { 00149 m_data_ptr = new unsigned char[m_content_length]; 00150 m_data_size = m_content_length; 00151 } 00152 }
| void HttpClientSocket::OnData | ( | const char * | , | |
| size_t | ||||
| ) | [virtual] |
Chunk of http body data recevied.
Implements HTTPSocket.
Definition at line 155 of file HttpClientSocket.cpp.
References Socket::Handler(), LOG_LEVEL_ERROR, ISocketHandler::LogError(), m_b_close_when_complete, m_b_complete, m_content_length, m_content_ptr, m_data_ptr, m_data_size, m_fil, OnContent(), and Socket::SetCloseAndDelete().
00156 { 00157 if (m_fil) 00158 { 00159 m_fil -> fwrite(buf, 1, len); 00160 } 00161 else 00162 if (m_data_ptr) 00163 { 00164 if (m_content_ptr + len > m_data_size) 00165 { 00166 Handler().LogError(this, "OnData", -1, "content buffer overflow", LOG_LEVEL_ERROR); 00167 } 00168 else 00169 { 00170 memcpy(m_data_ptr + m_content_ptr, buf, len); 00171 } 00172 } 00173 m_content_ptr += len; 00174 if (m_content_ptr == m_content_length && m_content_length) 00175 { 00176 if (m_fil) 00177 { 00178 m_fil -> fclose(); 00179 delete m_fil; 00180 m_fil = NULL; 00181 } 00182 m_b_complete = true; 00183 OnContent(); 00184 if (m_b_close_when_complete) 00185 { 00186 SetCloseAndDelete(); 00187 } 00188 } 00189 }
| void HttpClientSocket::OnDelete | ( | ) | [virtual] |
Called before a socket class is deleted by the ISocketHandler.
Reimplemented from Socket.
Definition at line 192 of file HttpClientSocket.cpp.
References m_b_complete, m_fil, and OnContent().
00193 { 00194 if (!m_b_complete) 00195 { 00196 if (m_fil) 00197 { 00198 m_fil -> fclose(); 00199 delete m_fil; 00200 m_fil = NULL; 00201 } 00202 m_b_complete = true; 00203 OnContent(); 00204 } 00205 }
| void HttpClientSocket::OnContent | ( | ) | [virtual] |
New callback method fires when all data is received.
Definition at line 258 of file HttpClientSocket.cpp.
Referenced by OnData(), and OnDelete().
| void HttpClientSocket::SetFilename | ( | const std::string & | x | ) |
Write response to this file.
Definition at line 208 of file HttpClientSocket.cpp.
References m_filename.
Referenced by HttpGetSocket::HttpGetSocket().
00209 { 00210 m_filename = x; 00211 }
| const std::string& HttpClientSocket::Filename | ( | ) | const [inline] |
| void HttpClientSocket::SetDataPtr | ( | unsigned char * | buf, | |
| size_t | len | |||
| ) |
Store response in this buffer.
Definition at line 214 of file HttpClientSocket.cpp.
References m_data_ptr, m_data_ptr_set, and m_data_size.
00215 { 00216 m_data_ptr = buf; 00217 m_data_size = len; 00218 m_data_ptr_set = true; 00219 }
| const std::string & HttpClientSocket::GetContent | ( | ) |
Get response headers.
Definition at line 222 of file HttpClientSocket.cpp.
References m_content.
00223 { 00224 return m_content; 00225 }
| size_t HttpClientSocket::GetContentLength | ( | ) |
Get size of response body.
Definition at line 228 of file HttpClientSocket.cpp.
References m_content_length.
00229 { 00230 return m_content_length; 00231 }
| const std::string & HttpClientSocket::GetContentType | ( | ) |
Get content type from response header.
Definition at line 293 of file HttpClientSocket.cpp.
References m_content_type.
00294 { 00295 return m_content_type; 00296 }
| size_t HttpClientSocket::GetContentPtr | ( | ) |
Get size of received response body.
Definition at line 234 of file HttpClientSocket.cpp.
References m_content_ptr.
00235 { 00236 return m_content_ptr; 00237 }
| size_t HttpClientSocket::GetPos | ( | ) |
Get size of received response body.
Definition at line 240 of file HttpClientSocket.cpp.
References m_content_ptr.
00241 { 00242 return m_content_ptr; 00243 }
| bool HttpClientSocket::Complete | ( | ) |
Complete response has been received.
Definition at line 246 of file HttpClientSocket.cpp.
References m_b_complete.
00247 { 00248 return m_b_complete; 00249 }
| const unsigned char * HttpClientSocket::GetDataPtr | ( | ) | const |
Get ptr to response data buffer.
Definition at line 252 of file HttpClientSocket.cpp.
References m_data_ptr.
00253 { 00254 return m_data_ptr; 00255 }
| void HttpClientSocket::SetCloseOnComplete | ( | bool | x = true |
) |
Close socket when response received.
Definition at line 263 of file HttpClientSocket.cpp.
References m_b_close_when_complete.
00264 { 00265 m_b_close_when_complete = x; 00266 }
| const std::string & HttpClientSocket::GetUrlProtocol | ( | ) |
Get protocol used from url.
Definition at line 269 of file HttpClientSocket.cpp.
References m_protocol.
00270 { 00271 return m_protocol; 00272 }
| const std::string & HttpClientSocket::GetUrlHost | ( | ) |
Get hostname from url.
Definition at line 275 of file HttpClientSocket.cpp.
References m_host.
Referenced by HttpPostSocket::DoMultipartPost(), HttpGetSocket::HttpGetSocket(), HttpPutSocket::OnConnect(), HttpPostSocket::OnConnect(), HttpGetSocket::OnConnect(), HttpPutSocket::Open(), HttpPostSocket::Open(), and Url().
00276 { 00277 return m_host; 00278 }
| port_t HttpClientSocket::GetUrlPort | ( | ) |
Get port from url.
Definition at line 281 of file HttpClientSocket.cpp.
References m_port.
Referenced by HttpGetSocket::HttpGetSocket(), HttpGetSocket::OnConnect(), HttpPutSocket::Open(), HttpPostSocket::Open(), and Url().
00282 { 00283 return m_port; 00284 }
| const std::string & HttpClientSocket::GetUrlFilename | ( | ) |
Get filename part of url.
Definition at line 287 of file HttpClientSocket.cpp.
References m_url_filename.
00288 { 00289 return m_url_filename; 00290 }
| HttpClientSocket& HttpClientSocket::operator= | ( | const HttpClientSocket & | ) | [inline, protected] |
std::string HttpClientSocket::m_filename [private] |
Filename to write response to.
Reimplemented in HttpPutSocket.
Definition at line 110 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 111 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 112 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 113 of file HttpClientSocket.h.
Referenced by GetContentLength(), OnData(), OnHeader(), and OnHeaderComplete().
std::string HttpClientSocket::m_content [private] |
Received http headers.
Definition at line 114 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 115 of file HttpClientSocket.h.
Referenced by SetDataPtr(), and ~HttpClientSocket().
IFile* HttpClientSocket::m_fil [private] |
Output file.
Definition at line 116 of file HttpClientSocket.h.
Referenced by OnData(), OnDelete(), OnHeaderComplete(), and ~HttpClientSocket().
size_t HttpClientSocket::m_content_ptr [private] |
Number of bytes received from body.
Definition at line 117 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 118 of file HttpClientSocket.h.
Referenced by Complete(), OnData(), and OnDelete().
bool HttpClientSocket::m_b_close_when_complete [private] |
Close when the full response has been received.
Definition at line 119 of file HttpClientSocket.h.
Referenced by OnData(), and SetCloseOnComplete().
std::string HttpClientSocket::m_protocol [private] |
Protocol part of url_in.
Definition at line 120 of file HttpClientSocket.h.
Referenced by GetUrlProtocol(), HttpClientSocket(), and Url().
std::string HttpClientSocket::m_host [private] |
Hostname from url_in.
Definition at line 121 of file HttpClientSocket.h.
Referenced by GetUrlHost(), HttpClientSocket(), and Url().
port_t HttpClientSocket::m_port [private] |
Port from url_in.
Definition at line 122 of file HttpClientSocket.h.
Referenced by GetUrlPort(), HttpClientSocket(), and Url().
std::string HttpClientSocket::m_url_filename [private] |
Filename from url_in.
Definition at line 123 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 124 of file HttpClientSocket.h.
Referenced by GetContentType(), and OnHeader().
1.4.4