Logo
~Sockets~
~Examples~
~Contact~


HttpClientSocket Class Reference
[HTTP Sockets]

Get http response to file or memory. More...

#include <HttpClientSocket.h>

Inheritance diagram for HttpClientSocket:
Collaboration diagram for HttpClientSocket:

List of all members.


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)
HttpClientSocketoperator= (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.
FILE * 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.

Detailed Description

Get http response to file or memory.

Definition at line 43 of file HttpClientSocket.h.


Constructor & Destructor Documentation

HttpClientSocket::HttpClientSocket ( ISocketHandler h  ) 

Definition at line 45 of file HttpClientSocket.cpp.

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 }

HttpClientSocket::HttpClientSocket ( ISocketHandler h,
const std::string &  url_in 
)

Definition at line 59 of file HttpClientSocket.cpp.

References m_host, m_port, m_protocol, m_url_filename, HTTPSocket::SetUrl(), and HTTPSocket::url_this().

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 }

HttpClientSocket::HttpClientSocket ( ISocketHandler h,
const std::string &  host,
port_t  port,
const std::string &  url_in 
)

Definition at line 76 of file HttpClientSocket.cpp.

References Utility::l2string(), m_host, m_port, m_protocol, m_url_filename, HTTPSocket::SetUrl(), and HTTPSocket::url_this().

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 }

HttpClientSocket::~HttpClientSocket (  ) 

Definition at line 94 of file HttpClientSocket.cpp.

References m_data_ptr, m_data_ptr_set, and m_fil.

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 }

HttpClientSocket::HttpClientSocket ( const HttpClientSocket s  )  [inline, protected]

Definition at line 103 of file HttpClientSocket.h.

00103 : HTTPSocket(s) {} // copy constructor


Member Function Documentation

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 290 of file HttpClientSocket.cpp.

References GetUrlHost(), GetUrlPort(), m_host, m_port, m_protocol, m_url_filename, HTTPSocket::SetUrl(), and HTTPSocket::url_this().

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 }

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 107 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().

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 }

void HttpClientSocket::OnHeader ( const std::string &  key,
const std::string &  value 
) [virtual]

For each header line this callback is executed.

Parameters:
key Http header name
value Http header value

Implements HTTPSocket.

Definition at line 118 of file HttpClientSocket.cpp.

References m_content, m_content_length, and m_content_type.

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 }

void HttpClientSocket::OnHeaderComplete (  )  [virtual]

Callback fires when all http headers have been received.

Implements HTTPSocket.

Definition at line 133 of file HttpClientSocket.cpp.

References m_content_length, m_data_ptr, m_data_size, m_fil, and m_filename.

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 }

void HttpClientSocket::OnData ( const char *  ,
size_t   
) [virtual]

Chunk of http body data recevied.

Implements HTTPSocket.

Definition at line 148 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().

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 }

void HttpClientSocket::OnDelete (  )  [virtual]

Called before a socket class is deleted by the ISocketHandler.

Reimplemented from Socket.

Definition at line 184 of file HttpClientSocket.cpp.

References m_b_complete, m_fil, and OnContent().

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 }

void HttpClientSocket::OnContent (  )  [virtual]

New callback method fires when all data is received.

Definition at line 249 of file HttpClientSocket.cpp.

Referenced by OnData(), and OnDelete().

00250 {
00251 }

void HttpClientSocket::SetFilename ( const std::string &  x  ) 

Write response to this file.

Definition at line 199 of file HttpClientSocket.cpp.

References m_filename.

Referenced by HttpGetSocket::HttpGetSocket().

00200 {
00201         m_filename = x;
00202 }

const std::string& HttpClientSocket::Filename (  )  const [inline]

Definition at line 65 of file HttpClientSocket.h.

00065 { return m_filename; }

void HttpClientSocket::SetDataPtr ( unsigned char *  buf,
size_t  len 
)

Store response in this buffer.

Definition at line 205 of file HttpClientSocket.cpp.

References m_data_ptr, m_data_ptr_set, and m_data_size.

00206 {
00207         m_data_ptr = buf;
00208         m_data_size = len;
00209         m_data_ptr_set = true;
00210 }

const std::string & HttpClientSocket::GetContent (  ) 

Get response headers.

Definition at line 213 of file HttpClientSocket.cpp.

References m_content.

00214 {
00215         return m_content;
00216 }

size_t HttpClientSocket::GetContentLength (  ) 

Get size of response body.

Definition at line 219 of file HttpClientSocket.cpp.

References m_content_length.

00220 {
00221         return m_content_length;
00222 }

const std::string & HttpClientSocket::GetContentType (  ) 

Get content type from response header.

Definition at line 284 of file HttpClientSocket.cpp.

References m_content_type.

00285 {
00286         return m_content_type;
00287 }

size_t HttpClientSocket::GetContentPtr (  ) 

Get size of received response body.

Definition at line 225 of file HttpClientSocket.cpp.

References m_content_ptr.

00226 {
00227         return m_content_ptr;
00228 }

size_t HttpClientSocket::GetPos (  ) 

Get size of received response body.

Definition at line 231 of file HttpClientSocket.cpp.

References m_content_ptr.

00232 {
00233         return m_content_ptr;
00234 }

bool HttpClientSocket::Complete (  ) 

Complete response has been received.

Definition at line 237 of file HttpClientSocket.cpp.

References m_b_complete.

00238 {
00239         return m_b_complete;
00240 }

const unsigned char * HttpClientSocket::GetDataPtr (  )  const

Get ptr to response data buffer.

Definition at line 243 of file HttpClientSocket.cpp.

References m_data_ptr.

00244 {
00245         return m_data_ptr;
00246 }

void HttpClientSocket::SetCloseOnComplete ( bool  x = true  ) 

Close socket when response received.

Definition at line 254 of file HttpClientSocket.cpp.

References m_b_close_when_complete.

00255 {
00256         m_b_close_when_complete = x;
00257 }

const std::string & HttpClientSocket::GetUrlProtocol (  ) 

Get protocol used from url.

Definition at line 260 of file HttpClientSocket.cpp.

References m_protocol.

00261 {
00262         return m_protocol;
00263 }

const std::string & HttpClientSocket::GetUrlHost (  ) 

port_t HttpClientSocket::GetUrlPort (  ) 

Get port from url.

Definition at line 272 of file HttpClientSocket.cpp.

References m_port.

Referenced by HttpGetSocket::HttpGetSocket(), HttpGetSocket::OnConnect(), HttpPutSocket::Open(), HttpPostSocket::Open(), and Url().

00273 {
00274         return m_port;
00275 }

const std::string & HttpClientSocket::GetUrlFilename (  ) 

Get filename part of url.

Definition at line 278 of file HttpClientSocket.cpp.

References m_url_filename.

00279 {
00280         return m_url_filename;
00281 }

HttpClientSocket& HttpClientSocket::operator= ( const HttpClientSocket  )  [inline, protected]

Definition at line 104 of file HttpClientSocket.h.

00104 { return *this; } // assignment operator


Member Data Documentation

std::string HttpClientSocket::m_filename [private]

Filename to write response to.

Reimplemented in HttpPutSocket.

Definition at line 106 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 107 of file HttpClientSocket.h.

Referenced by GetDataPtr(), OnData(), OnHeaderComplete(), SetDataPtr(), and ~HttpClientSocket().

Max size of data buffer.

Definition at line 108 of file HttpClientSocket.h.

Referenced by OnData(), OnHeaderComplete(), and SetDataPtr().

Content-length header received from remote.

Reimplemented in HttpPostSocket, and HttpPutSocket.

Definition at line 109 of file HttpClientSocket.h.

Referenced by GetContentLength(), OnData(), OnHeader(), and OnHeaderComplete().

std::string HttpClientSocket::m_content [private]

Received http headers.

Definition at line 110 of file HttpClientSocket.h.

Referenced by GetContent(), OnFirst(), and OnHeader().

Buffer set from outside, do not delete.

Definition at line 111 of file HttpClientSocket.h.

Referenced by SetDataPtr(), and ~HttpClientSocket().

FILE* HttpClientSocket::m_fil [private]

Output file.

Definition at line 112 of file HttpClientSocket.h.

Referenced by OnData(), OnDelete(), OnHeaderComplete(), and ~HttpClientSocket().

Number of bytes received from body.

Definition at line 113 of file HttpClientSocket.h.

Referenced by GetContentPtr(), GetPos(), and OnData().

The entire content-length number of bytes has been received.

Definition at line 114 of file HttpClientSocket.h.

Referenced by Complete(), OnData(), and OnDelete().

Close when the full response has been received.

Definition at line 115 of file HttpClientSocket.h.

Referenced by OnData(), and SetCloseOnComplete().

std::string HttpClientSocket::m_protocol [private]

Protocol part of url_in.

Definition at line 116 of file HttpClientSocket.h.

Referenced by GetUrlProtocol(), HttpClientSocket(), and Url().

std::string HttpClientSocket::m_host [private]

Hostname from url_in.

Definition at line 117 of file HttpClientSocket.h.

Referenced by GetUrlHost(), HttpClientSocket(), and Url().

Port from url_in.

Definition at line 118 of file HttpClientSocket.h.

Referenced by GetUrlPort(), HttpClientSocket(), and Url().

std::string HttpClientSocket::m_url_filename [private]

Filename from url_in.

Definition at line 119 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 120 of file HttpClientSocket.h.

Referenced by GetContentType(), and OnHeader().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4