Logo
~Sockets~
~Examples~
~Contact~


HttpBaseSocket Class Reference

#include <HttpBaseSocket.h>

Inheritance diagram for HttpBaseSocket:
Collaboration diagram for HttpBaseSocket:

List of all members.


Public Member Functions

 HttpBaseSocket (ISocketHandler &h)
 ~HttpBaseSocket ()
void OnFirst ()
 Callback executes when first line has been received.
void OnHeader (const std::string &key, const std::string &value)
 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 Respond (const HttpResponse &res)
 Send response.
void OnTransferLimit ()
 This callback fires when the output buffer drops below the value set by SetTransferLimit.

Protected Member Functions

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

Protected Attributes

HttpRequest m_req
HttpResponse m_res

Private Member Functions

HttpBaseSocketoperator= (const HttpBaseSocket &)
void Execute ()

Private Attributes

size_t m_body_size_left
bool m_b_keepalive

Detailed Description

Definition at line 46 of file HttpBaseSocket.h.


Constructor & Destructor Documentation

HttpBaseSocket::HttpBaseSocket ( ISocketHandler h  ) 

Definition at line 51 of file HttpBaseSocket.cpp.

00052 :HTTPSocket(h)
00053 ,m_b_keepalive(false)
00054 {
00055 }

HttpBaseSocket::~HttpBaseSocket (  ) 

Definition at line 58 of file HttpBaseSocket.cpp.

00059 {
00060 }

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

Definition at line 63 of file HttpBaseSocket.h.

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


Member Function Documentation

void HttpBaseSocket::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 63 of file HttpBaseSocket.cpp.

References DEB, HTTPSocket::GetHttpVersion(), HTTPSocket::GetMethod(), HTTPSocket::GetQueryString(), Socket::GetRemoteAddress(), Socket::GetSockAddress(), Socket::GetSockPort(), HTTPSocket::GetUri(), m_req, HttpRequest::SetAttribute(), HttpRequest::SetHttpMethod(), HttpRequest::SetHttpVersion(), HttpRequest::SetRemoteAddr(), HttpRequest::SetRemoteHost(), HttpRequest::SetServerName(), HttpRequest::SetServerPort(), HttpRequest::SetUri(), and Utility::ToLower().

00064 {
00065 DEB(fprintf(stderr, "  %s %s %s\n", GetMethod().c_str(), GetUri().c_str(), GetHttpVersion().c_str());)
00066         m_req.SetHttpMethod( GetMethod() );
00067         m_req.SetUri( GetUri() );
00068         m_req.SetHttpVersion( GetHttpVersion() );
00069 
00070         if (Utility::ToLower(GetMethod()) == "get" && !GetQueryString().empty())
00071         {
00072                 m_req.SetAttribute("query_string", GetQueryString() );
00073         }
00074 
00075         m_req.SetRemoteAddr( GetRemoteAddress() );
00076         m_req.SetRemoteHost( "" ); // %!
00077         m_req.SetServerName( GetSockAddress() );
00078         m_req.SetServerPort( GetSockPort() );
00079 }

void HttpBaseSocket::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 82 of file HttpBaseSocket.cpp.

References HttpRequest::AddCookie(), DEB, m_req, HttpTransaction::SetHeader(), and Utility::ToLower().

00083 {
00084 DEB(fprintf(stderr, "  (request)OnHeader %s: %s\n", key.c_str(), value.c_str());)
00085         if (Utility::ToLower(key) == "cookie")
00086                 m_req.AddCookie(value);
00087         else
00088                 m_req.SetHeader(key, value);
00089 }

void HttpBaseSocket::OnHeaderComplete (  )  [virtual]

Callback fires when all http headers have been received.

Implements HTTPSocket.

Definition at line 92 of file HttpBaseSocket.cpp.

References Execute(), HttpTransaction::Header(), HttpRequest::InitBody(), m_body_size_left, and m_req.

00093 {
00094         m_body_size_left = atol( m_req.Header("content-length").c_str() );
00095         if (m_body_size_left > 0)
00096         {
00097                 m_req.InitBody( m_body_size_left );
00098         }
00099         else
00100         {
00101                 // execute
00102                 Execute();
00103         }
00104 }

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

Chunk of http body data recevied.

Implements HTTPSocket.

Definition at line 107 of file HttpBaseSocket.cpp.

References HttpRequest::CloseBody(), Execute(), m_body_size_left, m_req, and HttpRequest::Write().

00108 {
00109         m_req.Write( buf, sz );
00110         m_body_size_left -= sz;
00111         if (!m_body_size_left)
00112         {
00113                 m_req.CloseBody();
00114 
00115                 // execute
00116                 Execute();
00117         }
00118 }

void HttpBaseSocket::Respond ( const HttpResponse res  )  [virtual]

Send response.

Implements IHttpServer.

Definition at line 150 of file HttpBaseSocket.cpp.

References HTTPSocket::AddResponseHeader(), HTTPSocket::AppendResponseHeader(), HttpResponse::Cookie(), HttpResponse::CookieNames(), DEB, HttpResponse::GetFile(), HttpTransaction::Headers(), HttpResponse::HttpStatusCode(), HttpResponse::HttpStatusMsg(), HttpResponse::HttpVersion(), Utility::l2string(), m_res, OnTransferLimit(), HTTPSocket::ResponseHeaderIsSet(), HTTPSocket::SendResponse(), HTTPSocket::SetHttpVersion(), HTTPSocket::SetStatus(), HTTPSocket::SetStatusText(), and IFile::size().

00151 {
00152 DEB(    Debug deb("HttpBaseSocket::Respond");)
00153         m_res = res;
00154 //      m_res.SetHeader("connection", "close");
00155 
00156         SetHttpVersion( m_res.HttpVersion() );
00157         SetStatus( Utility::l2string(m_res.HttpStatusCode()) );
00158         SetStatusText( m_res.HttpStatusMsg() );
00159 
00160         if (!ResponseHeaderIsSet("content-length"))
00161         {
00162                 AddResponseHeader( "content-length", Utility::l2string( m_res.GetFile().size() ) );
00163         }
00164         for (Utility::ncmap<std::string>::const_iterator it = m_res.Headers().begin(); it != m_res.Headers().end(); ++it)
00165         {
00166                 AddResponseHeader( it -> first, it -> second );
00167         }
00168         std::list<std::string> vec = m_res.CookieNames();
00169         for (std::list<std::string>::iterator it2 = vec.begin(); it2 != vec.end(); it2++)
00170         {
00171                 AppendResponseHeader( "set-cookie", m_res.Cookie(*it2) );
00172         }
00173         SendResponse();
00174 
00175         OnTransferLimit();
00176 }

void HttpBaseSocket::OnTransferLimit (  )  [virtual]

This callback fires when the output buffer drops below the value set by SetTransferLimit.

Default: 0 (disabled).

Reimplemented from TcpSocket.

Definition at line 180 of file HttpBaseSocket.cpp.

References DEB, IFile::fclose(), IFile::fread(), HttpResponse::GetFile(), TcpSocket::GetOutputLength(), m_b_keepalive, m_res, IHttpServer::OnResponseComplete(), TcpSocket::SendBuf(), Socket::SetCloseAndDelete(), and TcpSocket::SetTransferLimit().

Referenced by Respond().

00181 {
00182 DEB(    Debug deb("HttpBaseSocket::OnTransferLimit");)
00183         char msg[32768];
00184         size_t n = m_res.GetFile().fread(msg, 1, 32768);
00185         while (n > 0)
00186         {
00187                 SendBuf( msg, n );
00188                 if (GetOutputLength() > 1)
00189                 {
00190                         SetTransferLimit( 1 );
00191                         break;
00192                 }
00193                 n = m_res.GetFile().fread(msg, 1, 32768);
00194         }
00195         if (!GetOutputLength())
00196         {
00197                 m_res.GetFile().fclose();
00198                 OnResponseComplete();
00199                 if (!m_b_keepalive)
00200                 {
00201                         SetCloseAndDelete();
00202                 }
00203         }
00204 }

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

Reset state of socket to sucessfully implement keep-alive.

Reimplemented from HTTPSocket.

Definition at line 208 of file HttpBaseSocket.cpp.

References m_body_size_left, and HTTPSocket::Reset().

Referenced by Execute().

00209 {
00210         HTTPSocket::Reset();
00211         m_body_size_left = 0;
00212 }

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

Definition at line 70 of file HttpBaseSocket.h.

00070 { return *this; } // assignment operator

void HttpBaseSocket::Execute (  )  [private]

Definition at line 122 of file HttpBaseSocket.cpp.

References DEB, HttpTransaction::Header(), HttpRequest::HttpVersion(), m_b_keepalive, m_req, IHttpServer::OnExec(), HttpRequest::ParseBody(), Reset(), and HttpRequest::Reset().

Referenced by OnData(), and OnHeaderComplete().

00123 {
00124         // parse form data / query_string and cookie header if available
00125         m_req.ParseBody();
00126 
00127 DEB(printf(" *** http version: %s\n", m_req.HttpVersion().c_str());
00128 printf(" ***   connection: %s\n", m_req.Header("connection").c_str());)
00129         if ( !(m_req.HttpVersion().size() > 4 && m_req.HttpVersion().substr(m_req.HttpVersion().size() - 4) == "/1.1") ||
00130                         m_req.Header("connection") == "close")
00131         {
00132                 m_b_keepalive = false;
00133 DEB(printf(" *** keepalive: false\n");)
00134         }
00135         else
00136         {
00137                 m_b_keepalive = true;
00138 DEB(printf(" *** keepalive: true\n");)
00139         }
00140 
00141         // prepare page
00142         OnExec( m_req );
00143 
00144         m_req.Reset();
00145         Reset();
00146 }


Member Data Documentation

Definition at line 65 of file HttpBaseSocket.h.

Referenced by Execute(), OnData(), OnFirst(), OnHeader(), and OnHeaderComplete().

Definition at line 66 of file HttpBaseSocket.h.

Referenced by OnTransferLimit(), and Respond().

Reimplemented from HTTPSocket.

Definition at line 73 of file HttpBaseSocket.h.

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

Reimplemented from HTTPSocket.

Definition at line 74 of file HttpBaseSocket.h.

Referenced by Execute(), and OnTransferLimit().


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