~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to iris/src/irisnet/noncore/cutestuff/httpconnect.cpp

  • Committer: Package Import Robot
  • Author(s): Jan Niehusmann
  • Date: 2014-07-01 21:49:34 UTC
  • mfrom: (6.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140701214934-gt4dkgm94byi4vnn
Tags: 0.15-1
* New upstream version
* set debhelper compat level to 9
* set Standards-Version to 3.9.5 (no further changes)
* add lintian override regarding license-problem-non-free-RFC
* use qconf to regenerate configure script
* implement hardening using buildflags instead of hardening-wrapper

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * httpconnect.cpp - HTTP "CONNECT" proxy
 
3
 * Copyright (C) 2003  Justin Karneges
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "httpconnect.h"
 
22
 
 
23
#include <qstringlist.h>
 
24
#include <QByteArray>
 
25
#include "bsocket.h"
 
26
#include <QtCrypto>
 
27
 
 
28
//#define PROX_DEBUG
 
29
 
 
30
#ifdef PROX_DEBUG
 
31
#include <stdio.h>
 
32
#endif
 
33
 
 
34
// CS_NAMESPACE_BEGIN
 
35
 
 
36
#ifdef PROX_DEBUG
 
37
QString escapeOutput(const QByteArray &in)
 
38
{
 
39
        QString out;
 
40
        for(int n = 0; n < in.size(); ++n) {
 
41
                if(in[n] == '\\') {
 
42
                        out += QString("\\\\");
 
43
                }
 
44
                else if(in[n] >= 32 && in[n] < 127) {
 
45
                        out += QChar::fromLatin1(in[n]);
 
46
                }
 
47
                else {
 
48
                        out += QString().sprintf("\\x%02x", (unsigned char)in[n]);
 
49
                }
 
50
        }
 
51
        return out;
 
52
}
 
53
#endif
 
54
 
 
55
static QString extractLine(QByteArray *buf, bool *found)
 
56
{
 
57
        // Scan for newline
 
58
        int index = buf->indexOf ("\r\n");
 
59
        if (index == -1) {
 
60
                // Newline not found
 
61
                if (found)
 
62
                        *found = false;
 
63
                return "";
 
64
        }
 
65
        else {
 
66
                // Found newline
 
67
                QString s = QString::fromAscii(buf->left(index));
 
68
                buf->remove(0, index + 2);
 
69
 
 
70
                if (found)
 
71
                        *found = true;
 
72
                return s;
 
73
        }
 
74
}
 
75
 
 
76
static bool extractMainHeader(const QString &line, QString *proto, int *code, QString *msg)
 
77
{
 
78
        int n = line.indexOf(' ');
 
79
        if(n == -1)
 
80
                return false;
 
81
        if(proto)
 
82
                *proto = line.mid(0, n);
 
83
        ++n;
 
84
        int n2 = line.indexOf(' ', n);
 
85
        if(n2 == -1)
 
86
                return false;
 
87
        if(code)
 
88
                *code = line.mid(n, n2-n).toInt();
 
89
        n = n2+1;
 
90
        if(msg)
 
91
                *msg = line.mid(n);
 
92
        return true;
 
93
}
 
94
 
 
95
class HttpConnect::Private
 
96
{
 
97
public:
 
98
        Private(HttpConnect *_q) :
 
99
                sock(_q)
 
100
        {
 
101
        }
 
102
 
 
103
        BSocket sock;
 
104
        QString host;
 
105
        int port;
 
106
        QString user, pass;
 
107
        QString real_host;
 
108
        int real_port;
 
109
 
 
110
        QByteArray recvBuf;
 
111
 
 
112
        bool inHeader;
 
113
        QStringList headerLines;
 
114
 
 
115
        int toWrite;
 
116
        bool active;
 
117
};
 
118
 
 
119
HttpConnect::HttpConnect(QObject *parent)
 
120
:ByteStream(parent)
 
121
{
 
122
        d = new Private(this);
 
123
        connect(&d->sock, SIGNAL(connected()), SLOT(sock_connected()));
 
124
        connect(&d->sock, SIGNAL(connectionClosed()), SLOT(sock_connectionClosed()));
 
125
        connect(&d->sock, SIGNAL(delayedCloseFinished()), SLOT(sock_delayedCloseFinished()));
 
126
        connect(&d->sock, SIGNAL(readyRead()), SLOT(sock_readyRead()));
 
127
        connect(&d->sock, SIGNAL(bytesWritten(int)), SLOT(sock_bytesWritten(int)));
 
128
        connect(&d->sock, SIGNAL(error(int)), SLOT(sock_error(int)));
 
129
 
 
130
        reset(true);
 
131
}
 
132
 
 
133
HttpConnect::~HttpConnect()
 
134
{
 
135
        reset(true);
 
136
        delete d;
 
137
}
 
138
 
 
139
void HttpConnect::reset(bool clear)
 
140
{
 
141
        if(d->sock.state() != BSocket::Idle)
 
142
                d->sock.close();
 
143
        if(clear) {
 
144
                clearReadBuffer();
 
145
                d->recvBuf.resize(0);
 
146
        }
 
147
        d->active = false;
 
148
}
 
149
 
 
150
void HttpConnect::setAuth(const QString &user, const QString &pass)
 
151
{
 
152
        d->user = user;
 
153
        d->pass = pass;
 
154
}
 
155
 
 
156
void HttpConnect::connectToHost(const QString &proxyHost, int proxyPort, const QString &host, int port)
 
157
{
 
158
        reset(true);
 
159
 
 
160
        d->host = proxyHost;
 
161
        d->port = proxyPort;
 
162
        d->real_host = host;
 
163
        d->real_port = port;
 
164
 
 
165
#ifdef PROX_DEBUG
 
166
        fprintf(stderr, "HttpConnect: Connecting to %s:%d", qPrintable(proxyHost), proxyPort);
 
167
        if(d->user.isEmpty())
 
168
                fprintf(stderr, "\n");
 
169
        else
 
170
                fprintf(stderr, ", auth {%s,%s}\n", qPrintable(d->user), qPrintable(d->pass));
 
171
#endif
 
172
        d->sock.connectToHost(d->host, d->port);
 
173
}
 
174
 
 
175
bool HttpConnect::isOpen() const
 
176
{
 
177
        return d->active;
 
178
}
 
179
 
 
180
void HttpConnect::close()
 
181
{
 
182
        d->sock.close();
 
183
        if(d->sock.bytesToWrite() == 0)
 
184
                reset();
 
185
}
 
186
 
 
187
void HttpConnect::write(const QByteArray &buf)
 
188
{
 
189
        if(d->active)
 
190
                d->sock.write(buf);
 
191
}
 
192
 
 
193
QByteArray HttpConnect::read(int bytes)
 
194
{
 
195
        return ByteStream::read(bytes);
 
196
}
 
197
 
 
198
int HttpConnect::bytesAvailable() const
 
199
{
 
200
        return ByteStream::bytesAvailable();
 
201
}
 
202
 
 
203
int HttpConnect::bytesToWrite() const
 
204
{
 
205
        if(d->active)
 
206
                return d->sock.bytesToWrite();
 
207
        else
 
208
                return 0;
 
209
}
 
210
 
 
211
void HttpConnect::sock_connected()
 
212
{
 
213
#ifdef PROX_DEBUG
 
214
        fprintf(stderr, "HttpConnect: Connected\n");
 
215
#endif
 
216
        d->inHeader = true;
 
217
        d->headerLines.clear();
 
218
 
 
219
        // connected, now send the request
 
220
        QString s;
 
221
        s += QString("CONNECT ") + d->real_host + ':' + QString::number(d->real_port) + " HTTP/1.0\r\n";
 
222
        if(!d->user.isEmpty()) {
 
223
                QString str = d->user + ':' + d->pass;
 
224
                s += QString("Proxy-Authorization: Basic ") + QCA::Base64().encodeString(str) + "\r\n";
 
225
        }
 
226
        s += "Pragma: no-cache\r\n";
 
227
        s += "\r\n";
 
228
 
 
229
        QByteArray block = s.toUtf8();
 
230
#ifdef PROX_DEBUG
 
231
        fprintf(stderr, "HttpConnect: writing: {%s}\n", qPrintable(escapeOutput(block)));
 
232
#endif
 
233
        d->toWrite = block.size();
 
234
        d->sock.write(block);
 
235
}
 
236
 
 
237
void HttpConnect::sock_connectionClosed()
 
238
{
 
239
        if(d->active) {
 
240
                reset();
 
241
                connectionClosed();
 
242
        }
 
243
        else {
 
244
                error(ErrProxyNeg);
 
245
        }
 
246
}
 
247
 
 
248
void HttpConnect::sock_delayedCloseFinished()
 
249
{
 
250
        if(d->active) {
 
251
                reset();
 
252
                delayedCloseFinished();
 
253
        }
 
254
}
 
255
 
 
256
void HttpConnect::sock_readyRead()
 
257
{
 
258
        QByteArray block = d->sock.read();
 
259
 
 
260
        if(!d->active) {
 
261
                ByteStream::appendArray(&d->recvBuf, block);
 
262
 
 
263
                if(d->inHeader) {
 
264
                        // grab available lines
 
265
                        while(1) {
 
266
                                bool found;
 
267
                                QString line = extractLine(&d->recvBuf, &found);
 
268
                                if(!found)
 
269
                                        break;
 
270
                                if(line.isEmpty()) {
 
271
                                        d->inHeader = false;
 
272
                                        break;
 
273
                                }
 
274
                                d->headerLines += line;
 
275
                        }
 
276
 
 
277
                        // done with grabbing the header?
 
278
                        if(!d->inHeader) {
 
279
                                QString str = d->headerLines.first();
 
280
                                d->headerLines.takeFirst();
 
281
 
 
282
                                QString proto;
 
283
                                int code;
 
284
                                QString msg;
 
285
                                if(!extractMainHeader(str, &proto, &code, &msg)) {
 
286
#ifdef PROX_DEBUG
 
287
                                        fprintf(stderr, "HttpConnect: invalid header!\n");
 
288
#endif
 
289
                                        reset(true);
 
290
                                        error(ErrProxyNeg);
 
291
                                        return;
 
292
                                }
 
293
                                else {
 
294
#ifdef PROX_DEBUG
 
295
                                        fprintf(stderr, "HttpConnect: header proto=[%s] code=[%d] msg=[%s]\n", qPrintable(proto), code, qPrintable(msg));
 
296
                                        for(QStringList::ConstIterator it = d->headerLines.begin(); it != d->headerLines.end(); ++it)
 
297
                                                fprintf(stderr, "HttpConnect: * [%s]\n", qPrintable(*it));
 
298
#endif
 
299
                                }
 
300
 
 
301
                                if(code == 200) { // OK
 
302
#ifdef PROX_DEBUG
 
303
                                        fprintf(stderr, "HttpConnect: << Success >>\n");
 
304
#endif
 
305
                                        d->active = true;
 
306
                                        connected();
 
307
 
 
308
                                        if(!d->recvBuf.isEmpty()) {
 
309
                                                appendRead(d->recvBuf);
 
310
                                                d->recvBuf.resize(0);
 
311
                                                readyRead();
 
312
                                                return;
 
313
                                        }
 
314
                                }
 
315
                                else {
 
316
                                        int err;
 
317
                                        QString errStr;
 
318
                                        if(code == 407) { // Authentication failed
 
319
                                                err = ErrProxyAuth;
 
320
                                                errStr = tr("Authentication failed");
 
321
                                        }
 
322
                                        else if(code == 404) { // Host not found
 
323
                                                err = ErrHostNotFound;
 
324
                                                errStr = tr("Host not found");
 
325
                                        }
 
326
                                        else if(code == 403) { // Access denied
 
327
                                                err = ErrProxyNeg;
 
328
                                                errStr = tr("Access denied");
 
329
                                        }
 
330
                                        else if(code == 503) { // Connection refused
 
331
                                                err = ErrConnectionRefused;
 
332
                                                errStr = tr("Connection refused");
 
333
                                        }
 
334
                                        else { // invalid reply
 
335
                                                err = ErrProxyNeg;
 
336
                                                errStr = tr("Invalid reply");
 
337
                                        }
 
338
 
 
339
#ifdef PROX_DEBUG
 
340
                                        fprintf(stderr, "HttpConnect: << Error >> [%s]\n", qPrintable(errStr));
 
341
#endif
 
342
                                        reset(true);
 
343
                                        error(err);
 
344
                                        return;
 
345
                                }
 
346
                        }
 
347
                }
 
348
        }
 
349
        else {
 
350
                appendRead(block);
 
351
                readyRead();
 
352
                return;
 
353
        }
 
354
}
 
355
 
 
356
void HttpConnect::sock_bytesWritten(int x)
 
357
{
 
358
        if(d->toWrite > 0) {
 
359
                int size = x;
 
360
                if(d->toWrite < x)
 
361
                        size = d->toWrite;
 
362
                d->toWrite -= size;
 
363
                x -= size;
 
364
        }
 
365
 
 
366
        if(d->active && x > 0)
 
367
                bytesWritten(x);
 
368
}
 
369
 
 
370
void HttpConnect::sock_error(int x)
 
371
{
 
372
        if(d->active) {
 
373
                reset();
 
374
                error(ErrRead);
 
375
        }
 
376
        else {
 
377
                reset(true);
 
378
                if(x == BSocket::ErrHostNotFound)
 
379
                        error(ErrProxyConnect);
 
380
                else if(x == BSocket::ErrConnectionRefused)
 
381
                        error(ErrProxyConnect);
 
382
                else if(x == BSocket::ErrRead)
 
383
                        error(ErrProxyNeg);
 
384
        }
 
385
}
 
386
 
 
387
// CS_NAMESPACE_END