Logo
~Sockets~
~Examples~
~Contact~


HTTPSocket Class Reference
[HTTP Sockets]

HTTP request/response base class. More...

#include <HTTPSocket.h>

Inheritance diagram for HTTPSocket:

Inheritance graph
[legend]
Collaboration diagram for HTTPSocket:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 HTTPSocket (ISocketHandler &)
 ~HTTPSocket ()
void OnRawData (const char *buf, size_t len)
 Reimplementation of ReadLine(), to properly detect end of header start of body.
void OnLine (const std::string &line)
 Callback fires when a socket in line protocol has read one full line.
virtual void OnFirst ()=0
 Callback executes when first line has been received.
virtual void OnHeader (const std::string &key, const std::string &value)=0
 For each header line this callback is executed.
virtual void OnHeaderComplete ()=0
 Callback fires when all http headers have been received.
virtual void OnData (const char *, size_t)=0
 Chunk of http body data recevied.
const std::string & GetMethod ()
 Get http method from incoming request, ie GET/POST/PUT etc.
void SetMethod (const std::string &x)
 Set http method to be used in request.
const std::string & GetUrl ()
 Get url from request.
void SetUrl (const std::string &x)
 Set url to be used in outgoing request.
const std::string & GetUri ()
 Get part of url before '?' character.
void SetUri (const std::string &x)
 Now why would I need this when there is a SetUrl method?
const std::string & GetQueryString ()
 Get part of url after '?' character.
const std::string & GetHttpVersion ()
 Get http version from incoming request/response.
const std::string & GetStatus ()
 Get http status from incoming response.
const std::string & GetStatusText ()
 Get http statustext from incoming response.
bool IsRequest ()
 Incoming header has been identified as a request (method url http_version
).
bool IsResponse ()
 Incoming header has been identified as a response (http_version status status_text
).
void SetHttpVersion (const std::string &x)
 Set http version to be used in outgoing request/response.
void SetStatus (const std::string &x)
 Set http status for outgoing response.
void SetStatusText (const std::string &x)
 Set http statustext for outgoing response.
void AddResponseHeader (const std::string &x, const std::string &y)
 Add html header.
void AddResponseHeader (const std::string &x, const char *format,...)
 Add html header.
void SendResponse ()
 Send response prepared with calls to methods SetHttpVersion, SetStatus, SetStatusText, and AddResponseHeader.
void SendRequest ()
 Send request prepared with calls to methods SetMethod, SetUrl, SetHttpVersion, and AddResponseHeader.
virtual std::string MyUseragent ()
 Implement this to return your own User-agent string.
void url_this (const std::string &url_in, std::string &protocol, std::string &host, port_t &port, std::string &url, std::string &file)
 Parse url.

Protected Member Functions

 HTTPSocket (const HTTPSocket &s)
virtual void Reset ()
 Reset state of socket to sucessfully implement keep-alive.

Private Types

typedef std::map< std::string,
std::string > 
string_m
 map to hold http header values.

Private Member Functions

HTTPSocketoperator= (const HTTPSocket &)

Private Attributes

bool m_first
bool m_header
std::string m_line
 Current line in line protocol mode.
std::string m_method
std::string m_url
std::string m_uri
std::string m_query_string
std::string m_http_version
std::string m_status
std::string m_status_text
bool m_request
bool m_response
string_m m_response_header

Detailed Description

HTTP request/response base class.

Definition at line 44 of file HTTPSocket.h.


Member Typedef Documentation

typedef std::map<std::string,std::string> HTTPSocket::string_m [private]

map to hold http header values.

Definition at line 47 of file HTTPSocket.h.


Constructor & Destructor Documentation

HTTPSocket::HTTPSocket ( ISocketHandler  ) 

Definition at line 45 of file HTTPSocket.cpp.

References TcpSocket::DisableInputBuffer(), and TcpSocket::SetLineProtocol().

00046 :TcpSocket(h)
00047 ,m_first(true)
00048 ,m_header(true)
00049 ,m_http_version("HTTP/1.0")
00050 ,m_request(false)
00051 ,m_response(false)
00052 {
00053         SetLineProtocol();
00054         DisableInputBuffer();
00055 }

HTTPSocket::~HTTPSocket (  ) 

Definition at line 58 of file HTTPSocket.cpp.

00059 {
00060 }

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

Definition at line 118 of file HTTPSocket.h.

00118 : TcpSocket(s) {}


Member Function Documentation

void HTTPSocket::OnRawData ( const char *  buf,
size_t  len 
) [virtual]

Reimplementation of ReadLine(), to properly detect end of header start of body.

Reimplemented from TcpSocket.

Definition at line 63 of file HTTPSocket.cpp.

References m_header, and OnData().

00064 {
00065         if (!m_header)
00066         {
00067                 OnData(buf, len);
00068         }
00069 }

void HTTPSocket::OnLine ( const std::string &  line  )  [virtual]

Callback fires when a socket in line protocol has read one full line.

Parameters:
line Line read

Reimplemented from TcpSocket.

Definition at line 72 of file HTTPSocket.cpp.

References Parse::getrest(), Parse::getword(), m_first, m_header, m_http_version, m_method, m_query_string, m_request, m_response, m_status, m_status_text, m_uri, m_url, OnFirst(), OnHeader(), OnHeaderComplete(), TcpSocket::SetLineProtocol(), and Socket::SetRetain().

00073 {
00074         if (m_first)
00075         {
00076                 Parse pa(line);
00077                 std::string str = pa.getword();
00078                 if (str.substr(0,4) == "HTTP") // response
00079                 {
00080                         m_http_version = str;
00081                         m_status = pa.getword();
00082                         m_status_text = pa.getrest();
00083                         m_response = true;
00084                 }
00085                 else // request
00086                 {
00087                         m_method = str;
00088                         m_url = pa.getword();
00089                         size_t spl = m_url.find("?");
00090                         if (spl != std::string::npos)
00091                         {
00092                                 m_uri = m_url.substr(0,spl);
00093                                 m_query_string = m_url.substr(spl + 1);
00094                         }
00095                         else
00096                         {
00097                                 m_uri = m_url;
00098                         }
00099                         m_http_version = pa.getword();
00100                         m_request = true;
00101                 }
00102                 m_first = false;
00103                 OnFirst();
00104                 return;
00105         }
00106         if (!line.size())
00107         {
00108                 SetLineProtocol(false);
00109                 m_header = false;
00110                 OnHeaderComplete();
00111                 return;
00112         }
00113         Parse pa(line,":");
00114         std::string key = pa.getword();
00115         std::string value = pa.getrest();
00116         OnHeader(key,value);
00117         /* If remote end tells us to keep connection alive, and we're operating
00118         in http/1.1 mode (not http/1.0 mode), then we mark the socket to be
00119         retained. */
00120 #ifdef ENABLE_POOL
00121         if (!strcasecmp(key.c_str(), "connection") &&
00122             !strcasecmp(value.c_str(), "keep-alive") )
00123         {
00124                 SetRetain();
00125         }
00126 #endif
00127 }

virtual void HTTPSocket::OnFirst (  )  [pure virtual]

Callback executes when first line has been received.

GetMethod, GetUrl/GetUri, and GetHttpVersion are valid when this callback is executed.

Implemented in HttpClientSocket, HttpDebugSocket, and HttpdSocket.

Referenced by OnLine().

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

For each header line this callback is executed.

Parameters:
key Http header name
value Http header value

Implemented in HttpClientSocket, HttpDebugSocket, and HttpdSocket.

Referenced by OnLine().

virtual void HTTPSocket::OnHeaderComplete (  )  [pure virtual]

Callback fires when all http headers have been received.

Implemented in HttpClientSocket, HttpDebugSocket, and HttpdSocket.

Referenced by OnLine().

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

Chunk of http body data recevied.

Implemented in HttpClientSocket, HttpDebugSocket, and HttpdSocket.

Referenced by OnRawData().

const std::string & HTTPSocket::GetMethod (  ) 

Get http method from incoming request, ie GET/POST/PUT etc.

Definition at line 203 of file HTTPSocket.cpp.

References m_method.

Referenced by HttpDebugSocket::OnFirst(), and HttpdSocket::OnHeaderComplete().

00204 {
00205         return m_method;
00206 }

void HTTPSocket::SetMethod ( const std::string &  x  ) 

Set http method to be used in request.

Definition at line 209 of file HTTPSocket.cpp.

References m_method.

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

00210 {
00211         m_method = x;
00212 }

const std::string & HTTPSocket::GetUrl (  ) 

Get url from request.

Definition at line 215 of file HTTPSocket.cpp.

References m_url.

Referenced by HttpDebugSocket::OnFirst().

00216 {
00217         return m_url;
00218 }

void HTTPSocket::SetUrl ( const std::string &  x  ) 

Set url to be used in outgoing request.

Definition at line 221 of file HTTPSocket.cpp.

References m_url.

Referenced by HttpClientSocket::HttpClientSocket(), HttpGetSocket::HttpGetSocket(), and HttpClientSocket::Url().

00222 {
00223         m_url = x;
00224 }

const std::string & HTTPSocket::GetUri (  ) 

Get part of url before '?' character.

Definition at line 227 of file HTTPSocket.cpp.

References m_uri.

Referenced by HttpdSocket::OnData(), and HttpdSocket::OnHeaderComplete().

00228 {
00229         return m_uri;
00230 }

void HTTPSocket::SetUri ( const std::string &  x  ) 

Now why would I need this when there is a SetUrl method?

Definition at line 293 of file HTTPSocket.cpp.

References m_uri.

00294 {
00295         m_uri = x;
00296 }

const std::string & HTTPSocket::GetQueryString (  ) 

Get part of url after '?' character.

Definition at line 233 of file HTTPSocket.cpp.

References m_query_string.

Referenced by HttpdSocket::OnHeaderComplete().

00234 {
00235         return m_query_string;
00236 }

const std::string & HTTPSocket::GetHttpVersion (  ) 

Get http version from incoming request/response.

Definition at line 239 of file HTTPSocket.cpp.

References m_http_version.

Referenced by HttpDebugSocket::OnFirst(), and HttpClientSocket::OnFirst().

00240 {
00241         return m_http_version;
00242 }

const std::string & HTTPSocket::GetStatus (  ) 

Get http status from incoming response.

Definition at line 245 of file HTTPSocket.cpp.

References m_status.

Referenced by HttpClientSocket::OnFirst().

00246 {
00247         return m_status;
00248 }

const std::string & HTTPSocket::GetStatusText (  ) 

Get http statustext from incoming response.

Definition at line 251 of file HTTPSocket.cpp.

References m_status_text.

Referenced by HttpClientSocket::OnFirst().

00252 {
00253         return m_status_text;
00254 }

bool HTTPSocket::IsRequest (  ) 

Incoming header has been identified as a request (method url http_version
).

Definition at line 257 of file HTTPSocket.cpp.

References m_request.

00258 {
00259         return m_request;
00260 }

bool HTTPSocket::IsResponse (  ) 

Incoming header has been identified as a response (http_version status status_text
).

Definition at line 263 of file HTTPSocket.cpp.

References m_response.

Referenced by HttpClientSocket::OnFirst().

00264 {
00265         return m_response;
00266 }

void HTTPSocket::SetHttpVersion ( const std::string &  x  ) 

Set http version to be used in outgoing request/response.

Definition at line 269 of file HTTPSocket.cpp.

References m_http_version.

Referenced by HttpPostSocket::DoMultipartPost(), HttpPutSocket::OnConnect(), and HttpPostSocket::OnConnect().

00270 {
00271         m_http_version = x;
00272 }

void HTTPSocket::SetStatus ( const std::string &  x  ) 

Set http status for outgoing response.

Definition at line 275 of file HTTPSocket.cpp.

References m_status.

Referenced by HttpdSocket::OnHeaderComplete(), and HttpdSocket::Send64().

00276 {
00277         m_status = x;
00278 }

void HTTPSocket::SetStatusText ( const std::string &  x  ) 

Set http statustext for outgoing response.

Definition at line 281 of file HTTPSocket.cpp.

References m_status_text.

Referenced by HttpdSocket::OnHeaderComplete(), and HttpdSocket::Send64().

00282 {
00283         m_status_text = x;
00284 }

void HTTPSocket::AddResponseHeader ( const std::string &  x,
const std::string &  y 
)

void HTTPSocket::AddResponseHeader ( const std::string &  x,
const char *  format,
  ... 
)

Add html header.

Definition at line 145 of file HTTPSocket.cpp.

References m_response_header.

00146 {
00147         char slask[5000]; // temporary for vsprintf / vsnprintf
00148         va_list ap;
00149 
00150         va_start(ap, format);
00151 #ifdef _WIN32
00152         vsprintf(slask, format, ap);
00153 #else
00154         vsnprintf(slask, 5000, format, ap);
00155 #endif
00156         va_end(ap);
00157 
00158         m_response_header[header] = slask;
00159 }

void HTTPSocket::SendResponse (  ) 

Send response prepared with calls to methods SetHttpVersion, SetStatus, SetStatusText, and AddResponseHeader.

Definition at line 130 of file HTTPSocket.cpp.

References m_http_version, m_response_header, m_status, m_status_text, and TcpSocket::Send().

Referenced by HttpdSocket::OnHeaderComplete(), and HttpdSocket::Send64().

00131 {
00132         std::string msg;
00133         msg = m_http_version + " " + m_status + " " + m_status_text + "\r\n";
00134         for (string_m::iterator it = m_response_header.begin(); it != m_response_header.end(); it++)
00135         {
00136                 std::string key = (*it).first;
00137                 std::string val = (*it).second;
00138                 msg += key + ": " + val + "\r\n";
00139         }
00140         msg += "\r\n";
00141         Send( msg );
00142 }

void HTTPSocket::SendRequest (  ) 

Send request prepared with calls to methods SetMethod, SetUrl, SetHttpVersion, and AddResponseHeader.

Definition at line 162 of file HTTPSocket.cpp.

References m_http_version, m_method, m_response_header, m_url, and TcpSocket::Send().

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

00163 {
00164         std::string msg;
00165         msg = m_method + " " + m_url + " " + m_http_version + "\r\n";
00166         for (string_m::iterator it = m_response_header.begin(); it != m_response_header.end(); it++)
00167         {
00168                 std::string key = (*it).first;
00169                 std::string val = (*it).second;
00170                 msg += key + ": " + val + "\r\n";
00171         }
00172         msg += "\r\n";
00173         Send( msg );
00174 }

std::string HTTPSocket::MyUseragent (  )  [virtual]

Implement this to return your own User-agent string.

Definition at line 177 of file HTTPSocket.cpp.

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

00178 {
00179         std::string version = "C++Sockets/";
00180 #ifdef _VERSION
00181         version += _VERSION;
00182 #endif
00183         return version;
00184 }

void HTTPSocket::url_this ( const std::string &  url_in,
std::string &  protocol,
std::string &  host,
port_t port,
std::string &  url,
std::string &  file 
)

Parse url.

If protocol is https, EnableSSL() will be called.

Definition at line 299 of file HTTPSocket.cpp.

References Socket::EnableSSL(), Parse::getrest(), Parse::getvalue(), Parse::getword(), Socket::Handler(), LOG_LEVEL_WARNING, and ISocketHandler::LogError().

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

00300 {
00301         Parse pa(url_in,"/");
00302         protocol = pa.getword(); // http
00303         if (!strcasecmp(protocol.c_str(), "https:"))
00304         {
00305 #ifdef HAVE_OPENSSL
00306                 EnableSSL();
00307 #else
00308                 Handler().LogError(this, "url_this", -1, "SSL not available", LOG_LEVEL_WARNING);
00309 #endif
00310                 port = 443;
00311         }
00312         else
00313         {
00314                 port = 80;
00315         }
00316         host = pa.getword();
00317         if (strstr(host.c_str(),":"))
00318         {
00319                 Parse pa(host,":");
00320                 pa.getword(host);
00321                 port = static_cast<port_t>(pa.getvalue());
00322         }
00323         url = "/" + pa.getrest();
00324         {
00325                 Parse pa(url,"/");
00326                 std::string tmp = pa.getword();
00327                 while (tmp.size())
00328                 {
00329                         file = tmp;
00330                         tmp = pa.getword();
00331                 }
00332         }
00333 } // url_this

void HTTPSocket::Reset (  )  [protected, virtual]

Reset state of socket to sucessfully implement keep-alive.

Reimplemented in HttpdSocket.

Definition at line 187 of file HTTPSocket.cpp.

References m_first, m_header, m_request, m_response, m_response_header, and TcpSocket::SetLineProtocol().

Referenced by HttpdSocket::Reset().

00188 {
00189         m_first = true;
00190         m_header = true;
00191         m_request = false;
00192         m_response = false;
00193         SetLineProtocol(true);
00194         while (m_response_header.size())
00195         {
00196                 string_m::iterator it = m_response_header.begin();
00197                 m_response_header.erase(it);
00198         }
00199 
00200 }

HTTPSocket& HTTPSocket::operator= ( const HTTPSocket  )  [inline, private]

Definition at line 123 of file HTTPSocket.h.

00123 { return *this; }


Member Data Documentation

bool HTTPSocket::m_first [private]

Definition at line 124 of file HTTPSocket.h.

Referenced by OnLine(), and Reset().

bool HTTPSocket::m_header [private]

Definition at line 125 of file HTTPSocket.h.

Referenced by OnLine(), OnRawData(), and Reset().

std::string HTTPSocket::m_line [private]

Current line in line protocol mode.

Reimplemented from TcpSocket.

Definition at line 126 of file HTTPSocket.h.

std::string HTTPSocket::m_method [private]

Definition at line 127 of file HTTPSocket.h.

Referenced by GetMethod(), OnLine(), SendRequest(), and SetMethod().

std::string HTTPSocket::m_url [private]

Definition at line 128 of file HTTPSocket.h.

Referenced by GetUrl(), OnLine(), SendRequest(), and SetUrl().

std::string HTTPSocket::m_uri [private]

Definition at line 129 of file HTTPSocket.h.

Referenced by GetUri(), OnLine(), and SetUri().

std::string HTTPSocket::m_query_string [private]

Definition at line 130 of file HTTPSocket.h.

Referenced by GetQueryString(), and OnLine().

std::string HTTPSocket::m_http_version [private]

Definition at line 131 of file HTTPSocket.h.

Referenced by GetHttpVersion(), OnLine(), SendRequest(), SendResponse(), and SetHttpVersion().

std::string HTTPSocket::m_status [private]

Definition at line 132 of file HTTPSocket.h.

Referenced by GetStatus(), OnLine(), SendResponse(), and SetStatus().

std::string HTTPSocket::m_status_text [private]

Definition at line 133 of file HTTPSocket.h.

Referenced by GetStatusText(), OnLine(), SendResponse(), and SetStatusText().

bool HTTPSocket::m_request [private]

Definition at line 134 of file HTTPSocket.h.

Referenced by IsRequest(), OnLine(), and Reset().

bool HTTPSocket::m_response [private]

Definition at line 135 of file HTTPSocket.h.

Referenced by IsResponse(), OnLine(), and Reset().


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