httpclient.cpp 7.68 KB
Newer Older
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
/**
 * \file httpclient.cpp
 * Client to connect to HTTP server.
 *
 * \b Project: Kid3
 * \author Urs Fleisch
 * \date 30 Dec 2008
 *
 * Copyright (C) 2008-2009  Urs Fleisch
 *
 * This file is part of Kid3.
 *
 * Kid3 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Kid3 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "httpclient.h"
#include "kid3.h"

Urs Fleisch's avatar
Urs Fleisch committed
30
/** Only defined for generation of KDE3 translation files */
Urs Fleisch's avatar
Urs Fleisch committed
31
32
#define FOR_KDE3_PO_1 I18N_NOOP("Data received: %1")

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
/**
 * Constructor.
 */
HttpClient::HttpClient() :
	m_rcvIdx(0), m_rcvBodyIdx(0), m_rcvBodyLen(0)
{
#if QT_VERSION >= 0x040000
	m_sock = new QTcpSocket();
	connect(m_sock, SIGNAL(disconnected()),
			this, SLOT(slotConnectionClosed()));
	connect(m_sock, SIGNAL(error(QAbstractSocket::SocketError)),
			this, SLOT(slotError(QAbstractSocket::SocketError)));
#else
	m_sock = new QSocket();
	connect(m_sock, SIGNAL(connectionClosed()),
			this, SLOT(slotConnectionClosed()));
	connect(m_sock, SIGNAL(error(int)),
			this, SLOT(slotError(int)));
#endif
	connect(m_sock, SIGNAL(hostFound()),
			this, SLOT(slotHostFound()));
	connect(m_sock, SIGNAL(connected()),
			this, SLOT(slotConnected()));
	connect(m_sock, SIGNAL(readyRead()),
			this, SLOT(slotReadyRead()));
}

/**
 * Destructor.
 */
HttpClient::~HttpClient()
{
	m_sock->close();
	m_sock->disconnect();
	delete m_sock;
}

/**
 * Emit a progress signal.
 *
 * @param text state text
 */
void HttpClient::emitProgress(const QString& text)
{
	emit progress(text, m_rcvIdx - m_rcvBodyIdx, m_rcvBodyLen);
}

/**
 * Get string with proxy or destination and port.
 * If a proxy is set, the proxy is returned, else the real destination.
 *
 * @param dst real destination
 *
 * @return "destinationname:port".
 */
QString HttpClient::getProxyOrDest(const QString& dst)
{
	QString dest;
	if (Kid3App::s_miscCfg.m_useProxy) {
		dest = Kid3App::s_miscCfg.m_proxy;
	}
	if (dest.isEmpty()) {
		dest = dst;
	}
	return dest;
}

/**
 * Extract name and port from string.
 *
 * @param namePort input string with "name:port"
 * @param name     output string with "name"
 * @param port     output integer with port
 */
void HttpClient::splitNamePort(const QString& namePort,
																 QString& name, int& port)
{
	int colPos = namePort.QCM_lastIndexOf(':');
	if (colPos >= 0) {
		bool ok;
		port = namePort.mid(colPos + 1).toInt(&ok);
		if (!ok) port = 80;
		name = namePort.left(colPos);
	} else {
		name = namePort;
		port = 80;
	}
}

/**
 * Display status if host is found.
 */
void HttpClient::slotHostFound()
{
	emitProgress(i18n("Host found..."));
}

/**
 * Display status if connection is established.
 */
void HttpClient::slotConnected()
{
	m_sock->QCM_writeBlock(m_request.QCM_latin1(), m_request.length());
	emitProgress(i18n("Request sent..."));
}

/**
 * Read the available bytes.
 */
void HttpClient::readBytesAvailable()
{
	unsigned long len = m_sock->bytesAvailable();
	if (len > 0) {
		m_rcvBuf.resize(m_rcvIdx + len);
		unsigned long bytesRead = m_sock->QCM_readBlock(m_rcvBuf.data() + m_rcvIdx,
		                                                len);
		if (bytesRead > 0) {
			m_rcvIdx += bytesRead;
			if (bytesRead < len) {
				m_rcvBuf.resize(m_rcvIdx);
			}
			if (m_rcvBodyIdx == 0 && m_rcvBodyLen == 0) {
#if QT_VERSION >= 0x040000
				QByteArray& str(m_rcvBuf);
#else
				QCString str(m_rcvBuf.data(), m_rcvIdx + 1);
#endif
				int contentLengthPos = str.QCM_indexOf("Content-Length:");
				if (contentLengthPos != -1) {
					contentLengthPos += 15;
					while (str[contentLengthPos] == ' ' ||
					       str[contentLengthPos] == '\t') {
						++contentLengthPos;
					}
					int contentLengthLen = str.QCM_indexOf("\r\n", contentLengthPos);
					if (contentLengthLen > contentLengthPos) {
						contentLengthLen -= contentLengthPos;
					}
					m_rcvBodyLen = str.mid(contentLengthPos, contentLengthLen).toULong();
				}
				int contentTypePos = str.QCM_indexOf("Content-Type:");
				if (contentTypePos != -1) {
					contentTypePos += 13;
					char ch;
					while (contentTypePos < static_cast<int>(m_rcvIdx) &&
					       ((ch = str[contentTypePos]) == ' ' || ch == '\t')) {
						++contentTypePos;
					}
					int contentTypeEnd = contentTypePos;
					while (contentTypeEnd < static_cast<int>(m_rcvIdx) &&
					       (ch = str[contentTypeEnd]) != ' ' && ch != '\t' &&
					       ch != '\r' && ch != '\n' && ch != ';') {
						++contentTypeEnd;
					}
					m_rcvBodyType = str.mid(contentTypePos,
					                        contentTypeEnd - contentTypePos);
				}
				int bodyPos = str.QCM_indexOf("\r\n\r\n");
				if (bodyPos != -1) {
					m_rcvBodyIdx = bodyPos + 4;
				}
			}
		}
	}
}

/**
 * Read received data when the server has closed the connection.
 * A bytesReceived() signal is emitted.
 */
void HttpClient::slotConnectionClosed()
{
	readBytesAvailable();
#if QT_VERSION >= 0x040000
	QByteArray body(m_rcvBuf.data() + m_rcvBodyIdx,
	                m_rcvBuf.size() - m_rcvBodyIdx);
#else
	QByteArray body;
	body.duplicate(m_rcvBuf.data() + m_rcvBodyIdx,
	               m_rcvBuf.size() - m_rcvBodyIdx);
	body.resize(m_rcvBuf.size() - m_rcvBodyIdx + 1);
	body[m_rcvBuf.size() - m_rcvBodyIdx] = '\0';
#endif
	emit bytesReceived(body);
	m_sock->close();
	emitProgress(i18n("Ready."));
}

/**
 * Display information about read progress.
 */
void HttpClient::slotReadyRead()
{
	readBytesAvailable();
	emitProgress(KCM_i18n1("Data received: %1", m_rcvIdx));
}

/**
 * Display information about socket error.
 */
#if QT_VERSION >= 0x040000
void HttpClient::slotError(QAbstractSocket::SocketError err)
{
	if (err == QAbstractSocket::RemoteHostClosedError)
		return;
	QString msg(i18n("Socket error: "));
	switch (err) {
		case QAbstractSocket::ConnectionRefusedError:
			msg += i18n("Connection refused");
			break;
		case QAbstractSocket::HostNotFoundError:
			msg += i18n("Host not found");
			break;
		case QAbstractSocket::SocketAccessError:
			msg += i18n("Read failed");
			break;
		default:
			msg += m_sock->errorString();
	}
	emitProgress(msg);
}
#else
void HttpClient::slotError(int err)
{
	QString msg(i18n("Socket error: "));
	switch (err) {
		case QSocket::ErrConnectionRefused:
			msg += i18n("Connection refused");
			break;
		case QSocket::ErrHostNotFound:
			msg += i18n("Host not found");
			break;
		case QSocket::ErrSocketRead:
			msg += i18n("Read failed");
			break;
		default:
			msg += QString::number(err);
	}
	emitProgress(msg);
}
#endif

/**
 * Send a HTTP GET request.
 *
 * @param server host name
Urs Fleisch's avatar
Urs Fleisch committed
279
 * @param path   path of the URL
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
 */
void HttpClient::sendRequest(const QString& server, const QString& path)
{
	QString dest;
	int destPort;
	QString destNamePort(getProxyOrDest(server));
	splitNamePort(destNamePort, dest, destPort);
	QString serverName;
	int serverPort;
	splitNamePort(server, serverName, serverPort);
	m_request = "GET ";
	if (dest != serverName) {
		m_request += "http://";
		m_request += serverName;
		if (serverPort != 80) {
			m_request += ':';
			m_request += QString::number(serverPort);
		}
	}
	m_request += path;
	m_request += " HTTP/1.1\r\nUser-Agent: Kid3/" VERSION "\r\nHost: ";
	m_request += serverName;
	m_request += "\r\nConnection: close\r\n\r\n";

	m_rcvBuf.resize(0);
	m_rcvIdx = 0;
	m_rcvBodyIdx = 0;
	m_rcvBodyLen = 0;
	m_rcvBodyType = "";

	m_sock->connectToHost(dest, destPort);
	emitProgress(i18n("Connecting..."));
}