summaryrefslogtreecommitdiffstats
blob: 942361eee5f2c0a0d602d5af315efbdd523c4774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (c) 2010-2011 Thilo Cestonaro
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2011-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */


#include <Swiften/Network/HTTPConnectProxiedConnection.h>

#include <iostream>
#include <utility>

#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>

#include <Swiften/Base/foreach.h>
#include <Swiften/Base/Algorithm.h>
#include <Swiften/Base/Log.h>
#include <Swiften/Base/String.h>
#include <Swiften/Base/ByteArray.h>
#include <Swiften/Network/HostAddressPort.h>
#include <Swiften/Network/ConnectionFactory.h>
#include <Swiften/Network/HTTPTrafficFilter.h>
#include <Swiften/StringCodecs/Base64.h>

using namespace Swift;

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) {
}

void HTTPConnectProxiedConnection::setHTTPTrafficFilter(boost::shared_ptr<HTTPTrafficFilter> trafficFilter) {
	trafficFilter_ = trafficFilter;
}

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"));
	}
	append(data, createSafeByteArray("\r\n"));
	SWIFT_LOG(debug) << "HTTP Proxy send headers: " << byteArrayToString(ByteArray(data.begin(), data.end())) << std::endl;
	write(data);
}

void HTTPConnectProxiedConnection::parseHTTPHeader(const std::string& data, std::string& statusLine, std::vector<std::pair<std::string, std::string> >& headerFields) {
	std::istringstream dataStream(data);

	// parse status line
	std::getline(dataStream, statusLine);

	// parse fields
	std::string headerLine;
	std::string::size_type splitIndex;
	while (std::getline(dataStream, headerLine) && headerLine != "\r") {
		splitIndex = headerLine.find(':', 0);
		if (splitIndex != std::string::npos) {
			headerFields.push_back(std::pair<std::string, std::string>(headerLine.substr(0, splitIndex), headerLine.substr(splitIndex + 1)));
		}
	}
}

void HTTPConnectProxiedConnection::sendHTTPRequest(const std::string& statusLine, const std::vector<std::pair<std::string, std::string> >& headerFields) {
	typedef std::pair<std::string, std::string> HTTPHeaderField;
	std::stringstream request;

	request << statusLine << "\r\n";
	foreach (const HTTPHeaderField& field, headerFields) {
		request << field.first << ":" << field.second << "\r\n";
	}
	request << "\r\n";
	write(createSafeByteArray(request.str()));
}

void HTTPConnectProxiedConnection::handleProxyInitializeData(boost::shared_ptr<SafeByteArray> data) {
	std::string dataString = byteArrayToString(ByteArray(data->begin(), data->end()));
	SWIFT_LOG(debug) << data << std::endl;
	httpResponseBuffer_.append(dataString);

	std::string statusLine;
	std::vector<std::pair<std::string, std::string> > headerFields;

	std::string::size_type headerEnd = httpResponseBuffer_.find("\r\n\r\n", 0);
	if (headerEnd == std::string::npos) {
		if ((httpResponseBuffer_.size() > 4) && (httpResponseBuffer_.substr(0, 4) != "HTTP")) {
			setProxyInitializeFinished(false);
		}
		return;
	}

	parseHTTPHeader(httpResponseBuffer_.substr(0, headerEnd), statusLine, headerFields);

	if (trafficFilter_) {
		std::vector<std::pair<std::string, std::string> > newHeaderFields = trafficFilter_->filterHTTPResponseHeader(headerFields);
		if (!newHeaderFields.empty()) {
			std::stringstream statusLine;
			statusLine << "CONNECT " << getServer().getAddress().toString() << ":" << getServer().getPort();
			sendHTTPRequest(statusLine.str(), newHeaderFields);
			return;
		}
	}

	std::vector<std::string> tmp = String::split(statusLine, ' ');
	if (tmp.size() > 1) {
		try {
			int status = boost::lexical_cast<int>(tmp[1]);
			SWIFT_LOG(debug) << "Proxy Status: " << status << std::endl;
			if (status / 100 == 2) { // all 2XX states are OK
				setProxyInitializeFinished(true);
			}
			else {
				SWIFT_LOG(debug) << "HTTP Proxy returned an error: " << httpResponseBuffer_ << std::endl;
				setProxyInitializeFinished(false);
			}
		}
		catch (boost::bad_lexical_cast&) {
			SWIFT_LOG(warning) << "Unexpected response: " << tmp[1] << std::endl;
			setProxyInitializeFinished(false);
		}
	}
	else {
		setProxyInitializeFinished(false);
	}
	httpResponseBuffer_.clear();
}