summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2012-06-02 16:40:22 (GMT)
committerRemko Tronçon <git@el-tramo.be>2012-06-17 07:34:57 (GMT)
commitf171bc207c2a7371ac6924ff467049dd0258aa00 (patch)
treeeda5b5feb94dea264ea26b839b6f9ef5e32c915f /Swiften/Network/HTTPConnectProxiedConnection.cpp
parent5a1bdf2f6a4842176be5938f8db5cb9d151aceb5 (diff)
downloadswift-contrib-f171bc207c2a7371ac6924ff467049dd0258aa00.zip
swift-contrib-f171bc207c2a7371ac6924ff467049dd0258aa00.tar.bz2
Allow different connection methods for Client.
Diffstat (limited to 'Swiften/Network/HTTPConnectProxiedConnection.cpp')
-rw-r--r--Swiften/Network/HTTPConnectProxiedConnection.cpp130
1 files changed, 35 insertions, 95 deletions
diff --git a/Swiften/Network/HTTPConnectProxiedConnection.cpp b/Swiften/Network/HTTPConnectProxiedConnection.cpp
index 512381f..7bd7e41 100644
--- a/Swiften/Network/HTTPConnectProxiedConnection.cpp
+++ b/Swiften/Network/HTTPConnectProxiedConnection.cpp
@@ -15,7 +15,6 @@
#include <iostream>
#include <boost/bind.hpp>
-#include <boost/thread.hpp>
#include <boost/lexical_cast.hpp>
#include <Swiften/Base/Algorithm.h>
@@ -24,111 +23,52 @@
#include <Swiften/Base/ByteArray.h>
#include <Swiften/Network/HostAddressPort.h>
#include <Swiften/Network/ConnectionFactory.h>
-#include <Swiften/Network/CachingNameOnlyDomainNameResolver.h>
#include <Swiften/StringCodecs/Base64.h>
using namespace Swift;
-HTTPConnectProxiedConnection::HTTPConnectProxiedConnection(DomainNameResolver* resolver, ConnectionFactory* connectionFactory, TimerFactory* timerFactory, EventLoop* eventLoop, const std::string& proxyHost, int proxyPort, const SafeString& authID, const SafeString& authPassword) : connectionFactory_(connectionFactory), timerFactory_(timerFactory), proxyHost_(proxyHost), proxyPort_(proxyPort), server_(HostAddressPort(HostAddress("0.0.0.0"), 0)), authID_(authID), authPassword_(authPassword) {
- resolver_ = new CachingNameOnlyDomainNameResolver(resolver, eventLoop);
- connected_ = false;
+HTTPConnectProxiedConnection::HTTPConnectProxiedConnection(
+ DomainNameResolver* resolver,
+ ConnectionFactory* connectionFactory,
+ TimerFactory* timerFactory,
+ const std::string& proxyHost,
+ int proxyPort,
+ const SafeString& authID,
+ const SafeString& authPassword) :
+ ProxiedConnection(resolver, connectionFactory, timerFactory, proxyHost, proxyPort),
+ authID_(authID),
+ authPassword_(authPassword) {
}
-HTTPConnectProxiedConnection::~HTTPConnectProxiedConnection() {
- cancelConnector();
- delete resolver_;
- if (connection_) {
- connection_->onDataRead.disconnect(boost::bind(&HTTPConnectProxiedConnection::handleDataRead, shared_from_this(), _1));
- connection_->onDisconnected.disconnect(boost::bind(&HTTPConnectProxiedConnection::handleDisconnected, shared_from_this(), _1));
- }
-
- if (connected_) {
- std::cerr << "Warning: Connection was still established." << std::endl;
- }
-}
-
-void HTTPConnectProxiedConnection::cancelConnector() {
- if (connector_) {
- connector_->onConnectFinished.disconnect(boost::bind(&HTTPConnectProxiedConnection::handleConnectFinished, shared_from_this(), _1));
- connector_->stop();
- connector_.reset();
- }
-}
-
-void HTTPConnectProxiedConnection::connect(const HostAddressPort& server) {
- server_ = server;
- connector_ = Connector::create(proxyHost_, resolver_, connectionFactory_, timerFactory_, proxyPort_);
- connector_->onConnectFinished.connect(boost::bind(&HTTPConnectProxiedConnection::handleConnectFinished, shared_from_this(), _1));
- connector_->start();
-}
-void HTTPConnectProxiedConnection::listen() {
- assert(false);
- connection_->listen();
-}
-
-void HTTPConnectProxiedConnection::disconnect() {
- connected_ = false;
- connection_->disconnect();
-}
-
-void HTTPConnectProxiedConnection::handleDisconnected(const boost::optional<Error>& error) {
- onDisconnected(error);
-}
-
-void HTTPConnectProxiedConnection::write(const SafeByteArray& data) {
- connection_->write(data);
-}
-
-void HTTPConnectProxiedConnection::handleConnectFinished(Connection::ref connection) {
- cancelConnector();
- if (connection) {
- connection_ = connection;
- connection_->onDataRead.connect(boost::bind(&HTTPConnectProxiedConnection::handleDataRead, shared_from_this(), _1));
- connection_->onDisconnected.connect(boost::bind(&HTTPConnectProxiedConnection::handleDisconnected, shared_from_this(), _1));
-
- std::stringstream connect;
- connect << "CONNECT " << server_.getAddress().toString() << ":" << server_.getPort() << " HTTP/1.1\r\n";
- SafeByteArray data = createSafeByteArray(connect.str());
- if (!authID_.empty() && !authPassword_.empty()) {
- append(data, createSafeByteArray("Proxy-Authorization: Basic "));
- SafeByteArray credentials = authID_;
- append(credentials, createSafeByteArray(":"));
- append(credentials, authPassword_);
- append(data, Base64::encode(credentials));
- append(data, createSafeByteArray("\r\n"));
- }
+void HTTPConnectProxiedConnection::initializeProxy() {
+ std::stringstream connect;
+ connect << "CONNECT " << getServer().getAddress().toString() << ":" << getServer().getPort() << " HTTP/1.1\r\n";
+ SafeByteArray data = createSafeByteArray(connect.str());
+ if (!authID_.empty() && !authPassword_.empty()) {
+ append(data, createSafeByteArray("Proxy-Authorization: Basic "));
+ SafeByteArray credentials = authID_;
+ append(credentials, createSafeByteArray(":"));
+ append(credentials, authPassword_);
+ append(data, Base64::encode(credentials));
append(data, createSafeByteArray("\r\n"));
- SWIFT_LOG(debug) << "HTTP Proxy send headers: " << byteArrayToString(ByteArray(data.begin(), data.end())) << std::endl;
- connection_->write(data);
- }
- else {
- onConnectFinished(true);
}
+ append(data, createSafeByteArray("\r\n"));
+ SWIFT_LOG(debug) << "HTTP Proxy send headers: " << byteArrayToString(ByteArray(data.begin(), data.end())) << std::endl;
+ write(data);
}
-void HTTPConnectProxiedConnection::handleDataRead(boost::shared_ptr<SafeByteArray> data) {
- if (!connected_) {
- SWIFT_LOG(debug) << byteArrayToString(ByteArray(data->begin(), data->end())) << std::endl;
- std::vector<std::string> tmp = String::split(byteArrayToString(ByteArray(data->begin(), data->end())), ' ');
- if(tmp.size() > 1) {
- int status = boost::lexical_cast<int> (tmp[1].c_str());
- SWIFT_LOG(debug) << "Proxy Status: " << status << std::endl;
- if (status / 100 == 2) { // all 2XX states are OK
- connected_ = true;
- onConnectFinished(false);
- return;
- }
- SWIFT_LOG(debug) << "HTTP Proxy returned an error: " << byteArrayToString(ByteArray(data->begin(), data->end())) << std::endl;
+void HTTPConnectProxiedConnection::handleProxyInitializeData(boost::shared_ptr<SafeByteArray> data) {
+ SWIFT_LOG(debug) << byteArrayToString(ByteArray(data->begin(), data->end())) << std::endl;
+ std::vector<std::string> tmp = String::split(byteArrayToString(ByteArray(data->begin(), data->end())), ' ');
+ if(tmp.size() > 1) {
+ int status = boost::lexical_cast<int> (tmp[1].c_str());
+ SWIFT_LOG(debug) << "Proxy Status: " << status << std::endl;
+ if (status / 100 == 2) { // all 2XX states are OK
+ setProxyInitializeFinished(true);
+ return;
}
- disconnect();
- onConnectFinished(true);
- }
- else {
- onDataRead(data);
+ SWIFT_LOG(debug) << "HTTP Proxy returned an error: " << byteArrayToString(ByteArray(data->begin(), data->end())) << std::endl;
}
-}
-
-HostAddressPort HTTPConnectProxiedConnection::getLocalAddress() const {
- return connection_->getLocalAddress();
+ setProxyInitializeFinished(false);
}