Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes

ACE::HTTP::SessionBase Class Reference

#include <HTTP_SessionBase.h>

Inheritance diagram for ACE::HTTP::SessionBase:
Inheritance graph
[legend]
Collaboration diagram for ACE::HTTP::SessionBase:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SessionBase (u_short port, bool keep_alive=false)
 SessionBase (u_short port, const ACE_Time_Value &timeout, bool keep_alive=false, const ACE_Time_Value *alive_timeout=0)
virtual ~SessionBase ()
void set_keep_alive (bool f)
bool keep_alive () const
virtual bool is_connected () const =0
void set_host (const ACE_CString &host, u_short port)
void set_host (const ACE_CString &host)
void set_port (u_short port)
void set_proxy_target (const ACE_CString &host, u_short port)
const ACE_CStringget_host () const
u_short get_port () const
bool is_proxy_connection () const
const ACE_CStringget_proxy_target_host () const
u_short get_proxy_target_port () const
bool connect (bool use_reactor=false)
std::ostream & send_request (Request &request)
std::ostream & request_stream ()
std::ostream & request_stream (ACE::IOS::StreamInterceptor &interceptor)
std::istream & receive_response (Response &response)
std::istream & response_stream ()
std::istream & response_stream (ACE::IOS::StreamInterceptor &interceptor)
void close ()

Protected Types

enum  { DEFAULT_TIMEOUT = 30, DEFAULT_KEEP_ALIVE_TIMEOUT = 8 }

Protected Member Functions

bool reconnect_needed ()
void close_streams ()
virtual bool connect_i (const ACE_Synch_Options &sync_opt)=0
virtual void close_i ()=0
virtual std::iostream & sock_stream ()=0

Protected Attributes

ACE_CString host_
u_short port_
bool proxy_connection_
ACE_CString proxy_target_host_
u_short proxy_target_port_
bool reactive_
IStreamin_stream_
OStreamout_stream_
ACE_Time_Value http_timeout_
ACE_Time_Value keep_alive_timeout_
ACE_Time_Value reconnect_timer_
ACE_Countdown_Time reconnect_countdown_
bool keep_alive_
bool needs_reconnect_
bool cannot_reconnect_
bool expects_response_body_

Detailed Description

Definition at line 34 of file HTTP_SessionBase.h.


Member Enumeration Documentation

anonymous enum [protected]
Enumerator:
DEFAULT_TIMEOUT 
DEFAULT_KEEP_ALIVE_TIMEOUT 

Definition at line 100 of file HTTP_SessionBase.h.

              {
                DEFAULT_TIMEOUT = 30,           // sec
                DEFAULT_KEEP_ALIVE_TIMEOUT = 8  // sec
              };


Constructor & Destructor Documentation

ACE::HTTP::SessionBase::SessionBase ( u_short  port,
bool  keep_alive = false 
)
ACE::HTTP::SessionBase::SessionBase ( u_short  port,
const ACE_Time_Value timeout,
bool  keep_alive = false,
const ACE_Time_Value alive_timeout = 0 
)

Definition at line 43 of file HTTP_SessionBase.cpp.

ACE::HTTP::SessionBase::~SessionBase (  )  [virtual]

Definition at line 67 of file HTTP_SessionBase.cpp.

      {
        INET_TRACE ("ACE_HTTP_SessionBase - dtor");
        this->close_streams ();
      }


Member Function Documentation

void ACE::HTTP::SessionBase::close ( void   ) 

Definition at line 302 of file HTTP_SessionBase.cpp.

      {
        INET_TRACE ("ACE_HTTP_SessionBase::close");

        this->close_streams ();

        this->close_i ();
      }

virtual void ACE::HTTP::SessionBase::close_i (  )  [protected, pure virtual]
void ACE::HTTP::SessionBase::close_streams (  )  [inline, protected]

Definition at line 108 of file HTTP_SessionBase.inl.

      {
        if (this->in_stream_)
          {
            delete this->in_stream_;
            this->in_stream_ = 0;
          }
        if (this->out_stream_)
          {
            delete this->out_stream_;
            this->out_stream_ = 0;
          }
      }

bool ACE::HTTP::SessionBase::connect ( bool  use_reactor = false  ) 

Definition at line 73 of file HTTP_SessionBase.cpp.

      {
        INET_TRACE ("ACE_HTTP_SessionBase::connect");

        this->close ();

        unsigned long f_reactor = use_reactor ? ACE_Synch_Options::USE_REACTOR : 0;
        ACE_Synch_Options sync_opt (ACE_Synch_Options::USE_TIMEOUT | f_reactor,
                                    this->http_timeout_);

        return this->connect_i (sync_opt);
      }

virtual bool ACE::HTTP::SessionBase::connect_i ( const ACE_Synch_Options sync_opt  )  [protected, pure virtual]
const ACE_CString & ACE::HTTP::SessionBase::get_host (  )  const [inline]

Definition at line 65 of file HTTP_SessionBase.inl.

      {
        return this->host_;
      }

u_short ACE::HTTP::SessionBase::get_port (  )  const [inline]

Definition at line 71 of file HTTP_SessionBase.inl.

      {
        return this->port_;
      }

const ACE_CString & ACE::HTTP::SessionBase::get_proxy_target_host (  )  const [inline]

Definition at line 83 of file HTTP_SessionBase.inl.

      {
        return this->proxy_target_host_;
      }

u_short ACE::HTTP::SessionBase::get_proxy_target_port (  )  const [inline]

Definition at line 89 of file HTTP_SessionBase.inl.

      {
        return this->proxy_target_port_;
      }

virtual bool ACE::HTTP::SessionBase::is_connected (  )  const [pure virtual]
bool ACE::HTTP::SessionBase::is_proxy_connection (  )  const [inline]

Definition at line 77 of file HTTP_SessionBase.inl.

      {
        return this->proxy_connection_;
      }

bool ACE::HTTP::SessionBase::keep_alive (  )  const [inline]

Definition at line 18 of file HTTP_SessionBase.inl.

      {
        return this->keep_alive_;
      }

std::istream & ACE::HTTP::SessionBase::receive_response ( Response response  ) 

Definition at line 204 of file HTTP_SessionBase.cpp.

      {
        INET_TRACE ("ACE_HTTP_SessionBase::receive_response");

        if (this->in_stream_)
          {
            INET_ERROR (1, (LM_ERROR, DLINFO
                            ACE_TEXT ("HTTP_Session::receive_response - ")
                            ACE_TEXT ("invalid invocation without send_request\n")));
            // receive_response called second time without
            // new send_request in between
            return ACE::IOS::Null::in_stream_;
          }

        if (this->out_stream_)
          {
            delete this->out_stream_;
            this->out_stream_ = 0;
          }

        this->sock_stream ().flush ();

        do
        {
          response.clear ();
          if (!response.read (this->sock_stream ()))
            {
              INET_ERROR (1, (LM_ERROR, DLINFO
                              ACE_TEXT ("(%d) HTTP_Session::receive_response - ")
                              ACE_TEXT ("failed to read response\n"),
                              ACE_OS::last_error ()));
              return ACE::IOS::Null::in_stream_;
            }
        }
        while (response.get_status ().get_status() == Status::HTTP_CONTINUE);

        this->needs_reconnect_ = this->keep_alive () && !response.has_keep_alive ();

        if (!this->expects_response_body_)
          {
            FixedLengthStreamPolicy* pol;
            ACE_NEW_RETURN (pol,
                            FixedLengthStreamPolicy (0),
                            ACE::IOS::Null::in_stream_);
            ACE_NEW_RETURN (this->in_stream_,
                            IStream (this->sock_stream (), pol),
                            ACE::IOS::Null::in_stream_);
          }
        else if (response.has_chunked_transfer_encoding ())
          {
            ChunkedTransferStreamPolicy* pol;
            ACE_NEW_RETURN (pol,
                            ChunkedTransferStreamPolicy (),
                            ACE::IOS::Null::in_stream_);
            ACE_NEW_RETURN (this->in_stream_,
                            IStream (this->sock_stream (), pol),
                            ACE::IOS::Null::in_stream_);
          }
        else if (response.get_content_length () != Header::UNKNOWN_CONTENT_LENGTH)
          {
            FixedLengthStreamPolicy* pol;
            ACE_NEW_RETURN (pol,
                            FixedLengthStreamPolicy (response.get_content_length ()),
                            ACE::IOS::Null::in_stream_);
            ACE_NEW_RETURN (this->in_stream_,
                            IStream (this->sock_stream (), pol),
                            ACE::IOS::Null::in_stream_);
          }
        else
          {
            ACE_NEW_RETURN (this->in_stream_,
                            IStream (this->sock_stream ()),
                            ACE::IOS::Null::in_stream_);
          }

        return *this->in_stream_;
      }

bool ACE::HTTP::SessionBase::reconnect_needed (  )  [inline, protected]

Definition at line 95 of file HTTP_SessionBase.inl.

      {
        if (this->cannot_reconnect_)
          return false;
        if (!this->needs_reconnect_)
          {
            this->reconnect_countdown_.update ();
            return this->reconnect_timer_ == ACE_Time_Value::zero;
          }
        return true;
      }

std::ostream & ACE::HTTP::SessionBase::request_stream ( ACE::IOS::StreamInterceptor interceptor  ) 

Definition at line 192 of file HTTP_SessionBase.cpp.

      {
        if (this->out_stream_)
          {
            this->out_stream_->set_interceptor (interceptor);
            return *this->out_stream_;
          }
        else
          return ACE::IOS::Null::out_stream_;
      }

std::ostream & ACE::HTTP::SessionBase::request_stream (  ) 

Definition at line 184 of file HTTP_SessionBase.cpp.

      {
        if (this->out_stream_)
          return *this->out_stream_;
        else
          return ACE::IOS::Null::out_stream_;
      }

std::istream & ACE::HTTP::SessionBase::response_stream (  ) 

Definition at line 282 of file HTTP_SessionBase.cpp.

      {
        if (this->in_stream_)
          return *this->in_stream_;
        else
          return ACE::IOS::Null::in_stream_;
      }

std::istream & ACE::HTTP::SessionBase::response_stream ( ACE::IOS::StreamInterceptor interceptor  ) 

Definition at line 290 of file HTTP_SessionBase.cpp.

      {
        if (this->in_stream_)
          {
            this->in_stream_->set_interceptor (interceptor);
            return *this->in_stream_;
          }
        else
          return ACE::IOS::Null::in_stream_;
      }

std::ostream & ACE::HTTP::SessionBase::send_request ( Request request  ) 

Definition at line 86 of file HTTP_SessionBase.cpp.

      {
        INET_TRACE ("ACE_HTTP_SessionBase::send_request");

        if (this->in_stream_)
          {
            delete this->in_stream_;
            this->in_stream_ = 0;
          }

        bool keep_alive = this->keep_alive ();
        if ((this->is_connected () && !keep_alive) || this->reconnect_needed ())
        {
          close();
          this->needs_reconnect_ = false;
        }

        if (this->out_stream_)
          {
            delete this->out_stream_;
            this->out_stream_ = 0;
          }

        if (!this->is_connected ())
          {
            if (this->cannot_reconnect_ || !this->connect(this->reactive_))
              {
                if (!this->cannot_reconnect_)
                  INET_ERROR (1, (LM_ERROR, DLINFO
                                  ACE_TEXT ("(%d) HTTP_SessionBase::send_request - ")
                                  ACE_TEXT ("reconnect failed\n"),
                                  ACE_OS::last_error ()));
                return ACE::IOS::Null::out_stream_;
              }
          }
        if (!keep_alive)
          request.set_keep_alive (false);
        if (!request.has_host ())
          {
            if (this->port_ == URL::HTTP_PORT)
              request.set_host (this->host_);
            else
              request.set_host (this->host_, this->port_);
          }

        this->expects_response_body_ = request.get_method() != Request::HTTP_HEAD;

        if (request.has_chunked_transfer_encoding ())
        {
          request.write (this->sock_stream ());
          ChunkedTransferStreamPolicy* pol;
          ACE_NEW_RETURN (pol,
                          ChunkedTransferStreamPolicy (),
                          ACE::IOS::Null::out_stream_);
          ACE_NEW_RETURN (this->out_stream_,
                          OStream (this->sock_stream (), pol),
                          ACE::IOS::Null::out_stream_);
        }
        else if (request.get_content_length () != Header::UNKNOWN_CONTENT_LENGTH)
        {
          ACE::IOS::CString_OStream cs;
          request.write (cs);
          FixedLengthStreamPolicy* pol;
          ACE_NEW_RETURN (pol,
                          FixedLengthStreamPolicy (cs.str ().length () + request.get_content_length ()),
                          ACE::IOS::Null::out_stream_);
          ACE_NEW_RETURN (this->out_stream_,
                          OStream (this->sock_stream (), pol),
                          ACE::IOS::Null::out_stream_);
          (*this->out_stream_) << cs.str ().c_str ();
        }
        else if (request.get_method () != Request::HTTP_PUT && request.get_method() != Request::HTTP_POST)
        {
          ACE::IOS::CString_OStream cs;
          request.write (cs);
          FixedLengthStreamPolicy* pol;
          ACE_NEW_RETURN (pol,
                          FixedLengthStreamPolicy (cs.str ().length ()),
                          ACE::IOS::Null::out_stream_);
          ACE_NEW_RETURN (this->out_stream_,
                          OStream (this->sock_stream (), pol),
                          ACE::IOS::Null::out_stream_);
          (*this->out_stream_) << cs.str ().c_str ();
        }
        else
        {
          ACE_NEW_RETURN (this->out_stream_,
                          OStream (this->sock_stream ()),
                          ACE::IOS::Null::out_stream_);
          request.write (*this->out_stream_);
        }
        // reset reconnect timer
        this->reconnect_timer_ = this->keep_alive_timeout_;
        this->reconnect_countdown_.start ();

        return *this->out_stream_;
      }

void ACE::HTTP::SessionBase::set_host ( const ACE_CString host  )  [inline]

Definition at line 35 of file HTTP_SessionBase.inl.

      {
        if (!this->is_connected ())
          {
            this->host_ = host;
            this->proxy_connection_ = false;
          }
      }

void ACE::HTTP::SessionBase::set_host ( const ACE_CString host,
u_short  port 
) [inline]

Definition at line 24 of file HTTP_SessionBase.inl.

      {
        if (!this->is_connected ())
          {
            this->host_ = host;
            this->port_ = port;
            this->proxy_connection_ = false;
          }
      }

void ACE::HTTP::SessionBase::set_keep_alive ( bool  f  )  [inline]

Definition at line 12 of file HTTP_SessionBase.inl.

      {
        this->keep_alive_ = f;
      }

void ACE::HTTP::SessionBase::set_port ( u_short  port  )  [inline]

Definition at line 45 of file HTTP_SessionBase.inl.

      {
        if (!this->is_connected ())
          {
            this->port_ = port;
          }
      }

void ACE::HTTP::SessionBase::set_proxy_target ( const ACE_CString host,
u_short  port 
) [inline]

Definition at line 54 of file HTTP_SessionBase.inl.

      {
        if (!this->is_connected ())
          {
            this->proxy_target_host_ = host;
            this->proxy_target_port_ = port;
            this->proxy_connection_ = true;
          }
      }

virtual std::iostream& ACE::HTTP::SessionBase::sock_stream (  )  [protected, pure virtual]

Member Data Documentation

Definition at line 121 of file HTTP_SessionBase.h.

Definition at line 122 of file HTTP_SessionBase.h.

Definition at line 106 of file HTTP_SessionBase.h.

Definition at line 115 of file HTTP_SessionBase.h.

Definition at line 113 of file HTTP_SessionBase.h.

Definition at line 119 of file HTTP_SessionBase.h.

Definition at line 116 of file HTTP_SessionBase.h.

Definition at line 120 of file HTTP_SessionBase.h.

Definition at line 114 of file HTTP_SessionBase.h.

u_short ACE::HTTP::SessionBase::port_ [protected]

Definition at line 107 of file HTTP_SessionBase.h.

Definition at line 108 of file HTTP_SessionBase.h.

Definition at line 109 of file HTTP_SessionBase.h.

Definition at line 110 of file HTTP_SessionBase.h.

Definition at line 112 of file HTTP_SessionBase.h.

Definition at line 118 of file HTTP_SessionBase.h.

Definition at line 117 of file HTTP_SessionBase.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines