gloox  0.9.9.12
connectionhttpproxy.cpp
1 /*
2  Copyright (c) 2004-2008 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 
15 #include "gloox.h"
16 
17 #include "connectionhttpproxy.h"
18 #include "dns.h"
19 #include "logsink.h"
20 #include "prep.h"
21 #include "base64.h"
22 
23 #include <string>
24 
25 #ifndef _WIN32_WCE
26 # include <sstream>
27 #endif
28 
29 namespace gloox
30 {
31 
33  const std::string& server, int port )
34  : ConnectionBase( 0 ), m_connection( connection ),
35  m_logInstance( logInstance ), m_http11( false )
36  {
37  m_server = prep::idna( server );
38  m_port = port;
39 
40  if( m_connection )
41  m_connection->registerConnectionDataHandler( this );
42  }
43 
45  const LogSink& logInstance,
46  const std::string& server, int port )
47  : ConnectionBase( cdh ), m_connection( connection ),
48  m_logInstance( logInstance )
49  {
50  m_server = prep::idna( server );
51  m_port = port;
52 
53  if( m_connection )
54  m_connection->registerConnectionDataHandler( this );
55  }
56 
58  {
59  if( m_connection )
60  delete m_connection;
61  }
62 
64  {
65  ConnectionBase* conn = m_connection ? m_connection->newInstance() : 0;
66  return new ConnectionHTTPProxy( m_handler, conn, m_logInstance, m_server, m_port );
67  }
68 
70  {
71  if( m_connection )
72  delete m_connection;
73 
74  m_connection = connection;
75  }
76 
78  {
79  if( m_connection && m_handler )
80  {
81  m_state = StateConnecting;
82  return m_connection->connect();
83  }
84 
85  return ConnNotConnected;
86  }
87 
89  {
90  m_state = StateDisconnected;
91  if( m_connection )
92  m_connection->disconnect();
93  }
94 
96  {
97  if( m_connection )
98  return m_connection->recv( timeout );
99  else
100  return ConnNotConnected;
101  }
102 
104  {
105  if( m_connection )
106  return m_connection->receive();
107  else
108  return ConnNotConnected;
109  }
110 
111  bool ConnectionHTTPProxy::send( const std::string& data )
112  {
113  if( m_connection )
114  return m_connection->send( data );
115 
116  return false;
117  }
118 
120  {
121  m_state = StateDisconnected;
122 
123  if( m_connection )
124  m_connection->cleanup();
125  }
126 
127  void ConnectionHTTPProxy::getStatistics( int &totalIn, int &totalOut )
128  {
129  if( m_connection )
130  m_connection->getStatistics( totalIn, totalOut );
131  else
132  {
133  totalIn = 0;
134  totalOut = 0;
135  }
136  }
137 
139  const std::string& data )
140  {
141  if( !m_handler )
142  return;
143 
144  if( m_state == StateConnecting )
145  {
146  m_proxyHandshakeBuffer += data;
147  if( ( m_proxyHandshakeBuffer.substr( 0, 12 ) == "HTTP/1.0 200"
148  || m_proxyHandshakeBuffer.substr( 0, 12 ) == "HTTP/1.1 200" )
149  && m_proxyHandshakeBuffer.substr( m_proxyHandshakeBuffer.length() - 4 ) == "\r\n\r\n" )
150  {
151  m_proxyHandshakeBuffer = "";
152  m_state = StateConnected;
154  "http proxy connection established" );
155  m_handler->handleConnect( this );
156  }
157  else if( m_proxyHandshakeBuffer.substr( 9, 3 ) == "407" )
158  {
159  m_handler->handleDisconnect( this, ConnProxyAuthRequired );
160  m_connection->disconnect();
161  }
162  else if( m_proxyHandshakeBuffer.substr( 9, 3 ) == "403" ||
163  m_proxyHandshakeBuffer.substr( 9, 3 ) == "404" )
164  {
165  m_handler->handleDisconnect( this, ConnProxyAuthFailed );
166  m_connection->disconnect();
167  }
168  }
169  else if( m_state == StateConnected )
170  m_handler->handleReceivedData( this, data );
171  }
172 
174  {
175  if( m_connection )
176  {
177  std::string server = m_server;
178  int port = m_port;
179  if( port == -1 )
180  {
181  DNS::HostMap servers = DNS::resolve( m_server, m_logInstance );
182  if( servers.size() )
183  {
184  server = (*(servers.begin())).first;
185  port = (*(servers.begin())).second;
186  }
187  }
188 #ifndef _WIN32_WCE
189  std::ostringstream oss;
190  oss << "requesting http proxy connection to " << server << ":" << port;
191  m_logInstance.log( LogLevelDebug, LogAreaClassConnectionHTTPProxy, oss.str() );
192 #endif
193  std::ostringstream os;
194  os << "CONNECT " << server << ":" << port << ( m_http11 ? " HTTP/1.1" : " HTTP/1.0" ) << "\r\n";
195  os << "Host: " << server << "\r\n";
196  os << "Content-Length: 0\r\n";
197  os << "Proxy-Connection: Keep-Alive\r\n";
198  os << "Pragma: no-cache\r\n";
199  if( !m_proxyUser.empty() && !m_proxyPassword.empty() )
200  {
201  os << "Proxy-Authorization: Basic " << Base64::encode64( m_proxyUser + ":" + m_proxyPassword )
202  << "\r\n";
203  }
204  os << "\r\n";
205 
206  if( !m_connection->send( os.str() ) )
207  {
208  m_state = StateDisconnected;
209  if( m_handler )
210  m_handler->handleDisconnect( this, ConnIoError );
211  }
212  }
213  }
214 
216  ConnectionError reason )
217  {
218  m_state = StateDisconnected;
219  m_logInstance.log( LogLevelDebug, LogAreaClassConnectionHTTPProxy, "http proxy connection closed" );
220 
221  if( m_handler )
222  m_handler->handleDisconnect( this, reason );
223  }
224 
225 }