17#include "connectionhttpproxy.h"
33 m_logInstance( logInstance ), m_http11( false )
40 m_connection->registerConnectionDataHandler(
this );
48 m_logInstance( logInstance )
55 m_connection->registerConnectionDataHandler(
this );
65 ConnectionBase* conn = m_connection ? m_connection->newInstance() : 0;
74 m_connection = connection;
82 return m_connection->connect();
92 m_connection->disconnect();
107 return m_connection && m_connection->send( data );
115 m_connection->cleanup();
121 m_connection->getStatistics( totalIn, totalOut );
123 totalIn = totalOut = 0;
127 const std::string& data )
134 m_proxyHandshakeBuffer += data;
135 if( ( !m_proxyHandshakeBuffer.compare( 0, 12,
"HTTP/1.0 200" )
136 || !m_proxyHandshakeBuffer.compare( 0, 12,
"HTTP/1.1 200" ) )
137 && !m_proxyHandshakeBuffer.compare( m_proxyHandshakeBuffer.length() - 4, 4,
"\r\n\r\n" ) )
142 "HTTP proxy connection established" );
145 else if( !m_proxyHandshakeBuffer.compare( 9, 3,
"407" ) )
148 m_connection->disconnect();
150 else if( !m_proxyHandshakeBuffer.compare( 9, 3,
"403" )
151 || !m_proxyHandshakeBuffer.compare( 9, 3,
"404" ) )
154 m_connection->disconnect();
158 m_handler->handleReceivedData(
this, data );
170 if( !servers.empty() )
172 const std::pair< std::string, int >& host = *servers.begin();
177 std::string message =
"Requesting HTTP proxy connection to " +
server +
":"
178 + util::int2string(
port );
181 std::string os =
"CONNECT " +
server +
":" + util::int2string(
port ) +
" HTTP/1."
182 + util::int2string( m_http11 ? 1 : 0 ) +
"\r\n"
183 "Host: " +
server +
"\r\n"
184 "Content-Length: 0\r\n"
185 "Proxy-Connection: Keep-Alive\r\n"
186 "Pragma: no-cache\r\n"
189 if( !m_proxyUser.empty() && !m_proxyPwd.empty() )
191 os +=
"Proxy-Authorization: Basic " +
Base64::encode64( m_proxyUser +
":" + m_proxyPwd )
196 if( !m_connection->send( os ) )
212 m_handler->handleDisconnect(
this, reason );
const std::string & server() const
ConnectionDataHandler * m_handler
ConnectionBase(ConnectionDataHandler *cdh)
This is an abstract base class to receive events from a ConnectionBase-derived object.
virtual ~ConnectionHTTPProxy()
virtual void handleDisconnect(const ConnectionBase *connection, ConnectionError reason)
virtual ConnectionError recv(int timeout=-1)
virtual void handleConnect(const ConnectionBase *connection)
virtual void handleReceivedData(const ConnectionBase *connection, const std::string &data)
virtual void disconnect()
virtual ConnectionBase * newInstance() const
virtual ConnectionError connect()
void setConnectionImpl(ConnectionBase *connection)
virtual bool send(const std::string &data)
ConnectionHTTPProxy(ConnectionBase *connection, const LogSink &logInstance, const std::string &server, int port=-1)
virtual ConnectionError receive()
virtual void getStatistics(long int &totalIn, long int &totalOut)
static HostMap resolve(const std::string &service, const std::string &proto, const std::string &domain, const LogSink &logInstance)
std::map< std::string, int > HostMap
An implementation of log sink and source.
const std::string encode64(const std::string &input)
bool idna(const std::string &domain, std::string &out)
The namespace for the gloox library.
const std::string GLOOX_VERSION
@ LogAreaClassConnectionHTTPProxy
const std::string EmptyString