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 IHttpServer_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 48 of file HttpBaseSocket.h.


Constructor & Destructor Documentation

HttpBaseSocket::HttpBaseSocket ( ISocketHandler h  ) 

Definition at line 49 of file HttpBaseSocket.cpp.

00050 :HTTPSocket(h)
00051 ,m_b_keepalive(false)
00052 {
00053 }

HttpBaseSocket::~HttpBaseSocket (  ) 

Definition at line 56 of file HttpBaseSocket.cpp.

00057 {
00058 }

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

Definition at line 65 of file HttpBaseSocket.h.

00065 : 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 61 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().

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

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 80 of file HttpBaseSocket.cpp.

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

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

void HttpBaseSocket::OnHeaderComplete (  )  [virtual]

Callback fires when all http headers have been received.

Implements HTTPSocket.

Definition at line 90 of file HttpBaseSocket.cpp.

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

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

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

Chunk of http body data recevied.

Implements HTTPSocket.

Definition at line 105 of file HttpBaseSocket.cpp.

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

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

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

Send response.

Implements IHttpServer.

Definition at line 148 of file HttpBaseSocket.cpp.

References HTTPSocket::AddResponseHeader(), HTTPSocket::AppendResponseHeader(), HttpResponse::Cookie(), HttpResponse::CookieNames(), 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().

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

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 176 of file HttpBaseSocket.cpp.

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

Referenced by IHttpServer_Respond().

00177 {
00178         char msg[32768];
00179         size_t n = m_res.GetFile().fread(msg, 1, 32768);
00180         while (n > 0)
00181         {
00182                 SendBuf( msg, n );
00183                 if (GetOutputLength() > 1)
00184                 {
00185                         SetTransferLimit( 1 );
00186                         break;
00187                 }
00188                 n = m_res.GetFile().fread(msg, 1, 32768);
00189         }
00190         if (!GetOutputLength())
00191         {
00192                 SetTransferLimit(0);
00193                 m_res.GetFile().fclose();
00194                 IHttpServer_OnResponseComplete();
00195                 if (!m_b_keepalive)
00196                 {
00197                         SetCloseAndDelete();
00198                 }
00199         }
00200 }

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

Reset state of socket to sucessfully implement keep-alive.

Reimplemented from HTTPSocket.

Definition at line 204 of file HttpBaseSocket.cpp.

References m_body_size_left, and HTTPSocket::Reset().

Referenced by Execute().

00205 {
00206         HTTPSocket::Reset();
00207         m_body_size_left = 0;
00208 }

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

Definition at line 72 of file HttpBaseSocket.h.

00072 { return *this; } // assignment operator

void HttpBaseSocket::Execute (  )  [private]

Definition at line 120 of file HttpBaseSocket.cpp.

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

Referenced by OnData(), and OnHeaderComplete().

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


Member Data Documentation

Definition at line 67 of file HttpBaseSocket.h.

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

Definition at line 68 of file HttpBaseSocket.h.

Referenced by IHttpServer_Respond(), and OnTransferLimit().

Reimplemented from HTTPSocket.

Definition at line 75 of file HttpBaseSocket.h.

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

Reimplemented from HTTPSocket.

Definition at line 76 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