Logo
~Sockets~
~Examples~
~Contact~


HttpClientSocket Class Reference
[HTTP Sockets]

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

#include <HttpClientSocket.h>

Inheritance diagram for HttpClientSocket:

Inheritance graph
[legend]
Collaboration diagram for HttpClientSocket:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 HttpClientSocket (ISocketHandler &)
 HttpClientSocket (ISocketHandler &, 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.
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 35 of file HttpClientSocket.h.


Constructor & Destructor Documentation

HttpClientSocket::HttpClientSocket ( ISocketHandler  ) 

Definition at line 36 of file HttpClientSocket.cpp.

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 }

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

Definition at line 50 of file HttpClientSocket.cpp.

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

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 }

HttpClientSocket::~HttpClientSocket (  ) 

Definition at line 67 of file HttpClientSocket.cpp.

References m_data_ptr, m_data_ptr_set, and m_fil.

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 }

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

Definition at line 93 of file HttpClientSocket.h.

00093 : 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 263 of file HttpClientSocket.cpp.

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

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 }

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

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 }

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

References m_content, m_content_length, and m_content_type.

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 }

void HttpClientSocket::OnHeaderComplete (  )  [virtual]

Callback fires when all http headers have been received.

Implements HTTPSocket.

Definition at line 106 of file HttpClientSocket.cpp.

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

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 }

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

Chunk of http body data recevied.

Implements HTTPSocket.

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

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 }

void HttpClientSocket::OnDelete (  )  [virtual]

Called before a socket class is deleted by the ISocketHandler.

Reimplemented from Socket.

Definition at line 157 of file HttpClientSocket.cpp.

References m_b_complete, m_fil, and OnContent().

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 }

void HttpClientSocket::OnContent (  )  [virtual]

New callback method fires when all data is received.

Definition at line 222 of file HttpClientSocket.cpp.

Referenced by OnData(), and OnDelete().

00223 {
00224 }

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

Write response to this file.

Definition at line 172 of file HttpClientSocket.cpp.

References m_filename.

Referenced by HttpGetSocket::HttpGetSocket().

00173 {
00174         m_filename = x;
00175 }

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

Store response in this buffer.

Definition at line 178 of file HttpClientSocket.cpp.

References m_data_ptr, m_data_ptr_set, and m_data_size.

00179 {
00180         m_data_ptr = buf;
00181         m_data_size = len;
00182         m_data_ptr_set = true;
00183 }

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

Get response headers.

Definition at line 186 of file HttpClientSocket.cpp.

References m_content.

00187 {
00188         return m_content;
00189 }

size_t HttpClientSocket::GetContentLength (  ) 

Get size of response body.

Definition at line 192 of file HttpClientSocket.cpp.

References m_content_length.

00193 {
00194         return m_content_length;
00195 }

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

Get content type from response header.

Definition at line 257 of file HttpClientSocket.cpp.

References m_content_type.

00258 {
00259         return m_content_type;
00260 }

size_t HttpClientSocket::GetContentPtr (  ) 

Get size of received response body.

Definition at line 198 of file HttpClientSocket.cpp.

References m_content_ptr.

00199 {
00200         return m_content_ptr;
00201 }

size_t HttpClientSocket::GetPos (  ) 

Get size of received response body.

Definition at line 204 of file HttpClientSocket.cpp.

References m_content_ptr.

00205 {
00206         return m_content_ptr;
00207 }

bool HttpClientSocket::Complete (  ) 

Complete response has been received.

Definition at line 210 of file HttpClientSocket.cpp.

References m_b_complete.

00211 {
00212         return m_b_complete;
00213 }

const unsigned char * HttpClientSocket::GetDataPtr (  )  const

Get ptr to response data buffer.

Definition at line 216 of file HttpClientSocket.cpp.

References m_data_ptr.

00217 {
00218         return m_data_ptr;
00219 }

void HttpClientSocket::SetCloseOnComplete ( bool  = true  ) 

Close socket when response received.

Definition at line 227 of file HttpClientSocket.cpp.

References m_b_close_when_complete.

00228 {
00229         m_b_close_when_complete = x;
00230 }

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

Get protocol used from url.

Definition at line 233 of file HttpClientSocket.cpp.

References m_protocol.

00234 {
00235         return m_protocol;
00236 }

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

port_t HttpClientSocket::GetUrlPort (  ) 

Get port from url.

Definition at line 245 of file HttpClientSocket.cpp.

References m_port.

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

00246 {
00247         return m_port;
00248 }

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

Get filename part of url.

Definition at line 251 of file HttpClientSocket.cpp.

References m_url_filename.

00252 {
00253         return m_url_filename;
00254 }

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

Definition at line 94 of file HttpClientSocket.h.

00094 { return *this; } // assignment operator


Member Data Documentation

std::string HttpClientSocket::m_filename [private]

Filename to write response to.

Reimplemented in HttpPutSocket.

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

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

Max size of data buffer.

Definition at line 98 of file HttpClientSocket.h.

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

Content-length header received from remote.

Reimplemented in HttpPostSocket, and HttpPutSocket.

Definition at line 99 of file HttpClientSocket.h.

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

std::string HttpClientSocket::m_content [private]

Received http headers.

Definition at line 100 of file HttpClientSocket.h.

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

Buffer set from outside, do not delete.

Definition at line 101 of file HttpClientSocket.h.

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

FILE* HttpClientSocket::m_fil [private]

Output file.

Definition at line 102 of file HttpClientSocket.h.

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

Number of bytes received from body.

Definition at line 103 of file HttpClientSocket.h.

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

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

Definition at line 104 of file HttpClientSocket.h.

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

Close when the full response has been received.

Definition at line 105 of file HttpClientSocket.h.

Referenced by OnData(), and SetCloseOnComplete().

std::string HttpClientSocket::m_protocol [private]

Protocol part of url_in.

Definition at line 106 of file HttpClientSocket.h.

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

std::string HttpClientSocket::m_host [private]

Hostname from url_in.

Definition at line 107 of file HttpClientSocket.h.

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

Port from url_in.

Definition at line 108 of file HttpClientSocket.h.

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

std::string HttpClientSocket::m_url_filename [private]

Filename from url_in.

Definition at line 109 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 110 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