summaryrefslogtreecommitdiffstats
blob: 377373df61e710c2f9972a3d9ad2760161cd2ec4 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (c) 2011 Thilo Cestonaro
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2011 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swiften/Network/BOSHConnection.h>

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

#include <Swiften/Base/Log.h>
#include <Swiften/Base/String.h>
#include <Swiften/Base/Concat.h>
#include <Swiften/Base/ByteArray.h>
#include <Swiften/Network/HostAddressPort.h>
#include <Swiften/Parser/BOSHBodyExtractor.h>

namespace Swift {

BOSHConnection::BOSHConnection(const URL& boshURL, Connector::ref connector, XMLParserFactory* parserFactory)
	: boshURL_(boshURL),
	  connector_(connector),
	  parserFactory_(parserFactory),
	  sid_(),
	  waitingForStartResponse_(false),
	  pending_(false),
	  connectionReady_(false)
{
}

BOSHConnection::~BOSHConnection() {
	cancelConnector();
	if (connection_) {
		connection_->onDataRead.disconnect(boost::bind(&BOSHConnection::handleDataRead, shared_from_this(), _1));
		connection_->onDisconnected.disconnect(boost::bind(&BOSHConnection::handleDisconnected, shared_from_this(), _1));
	}
	disconnect();
}

void BOSHConnection::connect() {
	connector_->onConnectFinished.connect(boost::bind(&BOSHConnection::handleConnectFinished, shared_from_this(), _1));
	connector_->start();
}

void BOSHConnection::cancelConnector() {
	if (connector_) {
		connector_->onConnectFinished.disconnect(boost::bind(&BOSHConnection::handleConnectFinished, shared_from_this(), _1));
		connector_->stop();
		connector_.reset();
	}
}

void BOSHConnection::disconnect() {
	if (connection_) {
		connection_->disconnect();
		sid_ = "";
	}
	else {
		/* handleDisconnected takes care of the connector_ as well */
		handleDisconnected(boost::optional<Connection::Error>());
	}
}

void BOSHConnection::restartStream() {
	write(createSafeByteArray(""), true, false);
}

void BOSHConnection::terminateStream() {
	write(createSafeByteArray(""), false, true);
}


void BOSHConnection::write(const SafeByteArray& data) {
	write(data, false, false);
}

std::pair<SafeByteArray, size_t> BOSHConnection::createHTTPRequest(const SafeByteArray& data, bool streamRestart, bool terminate, unsigned long long rid, const std::string& sid, const URL& boshURL) {
	size_t size;
	std::stringstream content;
	SafeByteArray contentTail = createSafeByteArray("</body>");
	std::stringstream header;

	content << "<body rid='" << rid << "' sid='" << sid << "'";
	if (streamRestart) {
		content << " xmpp:restart='true' xmlns:xmpp='urn:xmpp:xbosh'";
	}
	if (terminate) {
		content << " type='terminate'";
	}
	content << " xmlns='http://jabber.org/protocol/httpbind'>";

	SafeByteArray safeContent = createSafeByteArray(content.str());
	safeContent.insert(safeContent.end(), data.begin(), data.end());
	safeContent.insert(safeContent.end(), contentTail.begin(), contentTail.end());

	size = safeContent.size();

	header	<< "POST " << boshURL.getPath() << " HTTP/1.1\r\n"
			<< "Host: " << boshURL.getHost();
	if (boshURL.getPort()) {
			header << ":" << *boshURL.getPort();
	}
	header	<< "\r\n"
		/*<< "Accept-Encoding: deflate\r\n"*/
			<< "Content-Type: text/xml; charset=utf-8\r\n"
			<< "Content-Length: " << size << "\r\n\r\n";

	SafeByteArray safeHeader = createSafeByteArray(header.str());
	safeHeader.insert(safeHeader.end(), safeContent.begin(), safeContent.end());

	return std::pair<SafeByteArray, size_t>(safeHeader, size);
}

void BOSHConnection::write(const SafeByteArray& data, bool streamRestart, bool terminate) {
	assert(connectionReady_);
	assert(!sid_.empty());

	SafeByteArray safeHeader = createHTTPRequest(data, streamRestart, terminate, rid_, sid_, boshURL_).first;

	onBOSHDataWritten(safeHeader);
	connection_->write(safeHeader);
	pending_ = true;

	SWIFT_LOG(debug) << "write data: " << safeByteArrayToString(safeHeader) << std::endl;
}

void BOSHConnection::handleConnectFinished(Connection::ref connection) {
	cancelConnector();
	connectionReady_ = connection;
	if (connectionReady_) {
		connection_ = connection;
		connection_->onDataRead.connect(boost::bind(&BOSHConnection::handleDataRead, shared_from_this(), _1));
		connection_->onDisconnected.connect(boost::bind(&BOSHConnection::handleDisconnected, shared_from_this(), _1));
	}
	onConnectFinished(!connectionReady_);
}

void BOSHConnection::startStream(const std::string& to, unsigned long long rid) {
	assert(connectionReady_);
	// Session Creation Request
	std::stringstream content;
	std::stringstream header;
		
	content << "<body content='text/xml; charset=utf-8'"
			<< " hold='1'"
			<< " to='" << to << "'"
			<< " rid='" << rid << "'"
			<< " ver='1.6'"
			<< " wait='60'" /* FIXME: we probably want this configurable*/
			/*<< " ack='0'" FIXME: support acks */
			<< " xml:lang='en'"
			<< " xmlns:xmpp='urn:xmpp:bosh'"
			<< " xmpp:version='1.0'"
			<< " xmlns='http://jabber.org/protocol/httpbind' />";

	std::string contentString = content.str();

	header	<< "POST " << boshURL_.getPath() << " HTTP/1.1\r\n"
			<< "Host: " << boshURL_.getHost();
	if (boshURL_.getPort()) {
		header << ":" << *boshURL_.getPort();
	}
	header << "\r\n"
		/*<< "Accept-Encoding: deflate\r\n"*/
			<< "Content-Type: text/xml; charset=utf-8\r\n"
			<< "Content-Length: " << contentString.size() << "\r\n\r\n"
			<< contentString;
	
	waitingForStartResponse_ = true;
	SafeByteArray safeHeader = createSafeByteArray(header.str());
	onBOSHDataWritten(safeHeader);
	connection_->write(safeHeader);
	SWIFT_LOG(debug) << "write stream header: " << safeByteArrayToString(safeHeader) << std::endl;
}

void BOSHConnection::handleDataRead(boost::shared_ptr<SafeByteArray> data) {
	onBOSHDataRead(*data);
	buffer_ = concat(buffer_, *data);
	std::string response = safeByteArrayToString(buffer_);
	if (response.find("\r\n\r\n") == std::string::npos) {
		onBOSHDataRead(createSafeByteArray("[[Previous read incomplete, pending]]"));
		return;
	}
	
	std::string httpCode = response.substr(response.find(" ") + 1, 3);
	if (httpCode != "200") {
		onHTTPError(httpCode);
		return;
	}

	BOSHBodyExtractor parser(parserFactory_, createByteArray(response.substr(response.find("\r\n\r\n") + 4)));
	if (parser.getBody()) {
		if (parser.getBody()->attributes.getAttribute("type") == "terminate") {
			BOSHError::Type errorType = parseTerminationCondition(parser.getBody()->attributes.getAttribute("condition"));
			onSessionTerminated(errorType == BOSHError::NoError ? boost::shared_ptr<BOSHError>() : boost::make_shared<BOSHError>(errorType));
		}
		buffer_.clear();
		if (waitingForStartResponse_) {
			waitingForStartResponse_ = false;
			sid_ = parser.getBody()->attributes.getAttribute("sid");
			std::string requestsString = parser.getBody()->attributes.getAttribute("requests");
			int requests = 2;
			if (!requestsString.empty()) {
				try {
					requests = boost::lexical_cast<size_t>(requestsString);
				}
				catch (const boost::bad_lexical_cast&) {
				}
			}
			onSessionStarted(sid_, requests);
		}
		SafeByteArray payload = createSafeByteArray(parser.getBody()->content);
		/* Say we're good to go again, so don't add anything after here in the method */
		pending_ = false;
		onXMPPDataRead(payload);
	}

}

BOSHError::Type BOSHConnection::parseTerminationCondition(const std::string& text) {
	BOSHError::Type condition = BOSHError::UndefinedCondition;
	if (text == "bad-request") {
		condition = BOSHError::BadRequest;
	}
	else if (text == "host-gone") {
		condition = BOSHError::HostGone;
	}
	else if (text == "host-unknown") {
		condition = BOSHError::HostUnknown;
	}
	else if (text == "improper-addressing") {
		condition = BOSHError::ImproperAddressing;
	}
	else if (text == "internal-server-error") {
		condition = BOSHError::InternalServerError;
	}
	else if (text == "item-not-found") {
		condition = BOSHError::ItemNotFound;
	}
	else if (text == "other-request") {
		condition = BOSHError::OtherRequest;
	}
	else if (text == "policy-violation") {
		condition = BOSHError::PolicyViolation;
	}
	else if (text == "remote-connection-failed") {
		condition = BOSHError::RemoteConnectionFailed;
	}
	else if (text == "remote-stream-error") {
		condition = BOSHError::RemoteStreamError;
	}
	else if (text == "see-other-uri") {
		condition = BOSHError::SeeOtherURI;
	}
	else if (text == "system-shutdown") {
		condition = BOSHError::SystemShutdown;
	}
	else if (text == "") {
		condition = BOSHError::NoError;
	}
	return condition;
}

const std::string& BOSHConnection::getSID() {
	return sid_;
}

void BOSHConnection::setRID(unsigned long long rid) {
	rid_ = rid;
}

void BOSHConnection::setSID(const std::string& sid) {
	sid_ = sid;
}

void BOSHConnection::handleDisconnected(const boost::optional<Connection::Error>& error) {
	cancelConnector();
	onDisconnected(error);
	sid_ = "";
	connectionReady_ = false;
}


bool BOSHConnection::isReadyToSend() {
	/* Without pipelining you need to not send more without first receiving the response */
	/* With pipelining you can. Assuming we can't, here */
	return connectionReady_ && !pending_ && !waitingForStartResponse_ && !sid_.empty();
}

}