initial commit
This commit is contained in:
commit
9d20827c46
2469 changed files with 470994 additions and 0 deletions
474
iris-legacy/cutestuff/network/bsocket.cpp
Normal file
474
iris-legacy/cutestuff/network/bsocket.cpp
Normal file
|
|
@ -0,0 +1,474 @@
|
|||
/*
|
||||
* bsocket.cpp - QSocket wrapper based on Bytestream with SRV DNS support
|
||||
* Copyright (C) 2003 Justin Karneges
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include <QTcpSocket>
|
||||
#include <QHostAddress>
|
||||
#include <QMetaType>
|
||||
|
||||
#include "bsocket.h"
|
||||
|
||||
//#include "safedelete.h"
|
||||
#ifndef NO_NDNS
|
||||
#include "ndns.h"
|
||||
#endif
|
||||
#include "srvresolver.h"
|
||||
|
||||
//#define BS_DEBUG
|
||||
|
||||
#ifdef BS_DEBUG
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#define READBUFSIZE 65536
|
||||
|
||||
// CS_NAMESPACE_BEGIN
|
||||
|
||||
#include "psilogger.h"
|
||||
|
||||
class QTcpSocketSignalRelay : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
QTcpSocketSignalRelay(QTcpSocket *sock, QObject *parent = 0)
|
||||
:QObject(parent)
|
||||
{
|
||||
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
|
||||
connect(sock, SIGNAL(hostFound()), SLOT(sock_hostFound()), Qt::QueuedConnection);
|
||||
connect(sock, SIGNAL(connected()), SLOT(sock_connected()), Qt::QueuedConnection);
|
||||
connect(sock, SIGNAL(disconnected()), SLOT(sock_disconnected()), Qt::QueuedConnection);
|
||||
connect(sock, SIGNAL(readyRead()), SLOT(sock_readyRead()), Qt::QueuedConnection);
|
||||
connect(sock, SIGNAL(bytesWritten(qint64)), SLOT(sock_bytesWritten(qint64)), Qt::QueuedConnection);
|
||||
connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(sock_error(QAbstractSocket::SocketError)), Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
signals:
|
||||
void hostFound();
|
||||
void connected();
|
||||
void disconnected();
|
||||
void readyRead();
|
||||
void bytesWritten(qint64);
|
||||
void error(QAbstractSocket::SocketError);
|
||||
|
||||
public slots:
|
||||
void sock_hostFound()
|
||||
{
|
||||
emit hostFound();
|
||||
}
|
||||
|
||||
void sock_connected()
|
||||
{
|
||||
emit connected();
|
||||
}
|
||||
|
||||
void sock_disconnected()
|
||||
{
|
||||
emit disconnected();
|
||||
}
|
||||
|
||||
void sock_readyRead()
|
||||
{
|
||||
emit readyRead();
|
||||
}
|
||||
|
||||
void sock_bytesWritten(qint64 x)
|
||||
{
|
||||
emit bytesWritten(x);
|
||||
}
|
||||
|
||||
void sock_error(QAbstractSocket::SocketError x)
|
||||
{
|
||||
emit error(x);
|
||||
}
|
||||
};
|
||||
|
||||
class BSocket::Private
|
||||
{
|
||||
public:
|
||||
Private()
|
||||
{
|
||||
qsock = 0;
|
||||
qsock_relay = 0;
|
||||
}
|
||||
|
||||
QTcpSocket *qsock;
|
||||
QTcpSocketSignalRelay *qsock_relay;
|
||||
int state;
|
||||
|
||||
#ifndef NO_NDNS
|
||||
NDns ndns;
|
||||
#endif
|
||||
SrvResolver srv;
|
||||
QString host;
|
||||
int port;
|
||||
//SafeDelete sd;
|
||||
};
|
||||
|
||||
BSocket::BSocket(QObject *parent)
|
||||
:ByteStream(parent)
|
||||
{
|
||||
d = new Private;
|
||||
#ifndef NO_NDNS
|
||||
connect(&d->ndns, SIGNAL(resultsReady()), SLOT(ndns_done()));
|
||||
#endif
|
||||
connect(&d->srv, SIGNAL(resultsReady()), SLOT(srv_done()));
|
||||
|
||||
reset();
|
||||
}
|
||||
|
||||
BSocket::~BSocket()
|
||||
{
|
||||
reset(true);
|
||||
delete d;
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::~BSocket()").arg(LOG_THIS));
|
||||
}
|
||||
|
||||
void BSocket::reset(bool clear)
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::reset()").arg(LOG_THIS));
|
||||
|
||||
if(d->qsock) {
|
||||
delete d->qsock_relay;
|
||||
d->qsock_relay = 0;
|
||||
|
||||
/*d->qsock->disconnect(this);
|
||||
|
||||
if(!clear && d->qsock->isOpen() && d->qsock->isValid()) {*/
|
||||
// move remaining into the local queue
|
||||
QByteArray block(d->qsock->bytesAvailable(), 0);
|
||||
d->qsock->read(block.data(), block.size());
|
||||
appendRead(block);
|
||||
//}
|
||||
|
||||
//d->sd.deleteLater(d->qsock);
|
||||
// delete d->qsock;
|
||||
d->qsock->deleteLater();
|
||||
d->qsock = 0;
|
||||
}
|
||||
else {
|
||||
if(clear)
|
||||
clearReadBuffer();
|
||||
}
|
||||
|
||||
if(d->srv.isBusy())
|
||||
d->srv.stop();
|
||||
#ifndef NO_NDNS
|
||||
if(d->ndns.isBusy())
|
||||
d->ndns.stop();
|
||||
#endif
|
||||
d->state = Idle;
|
||||
}
|
||||
|
||||
void BSocket::ensureSocket()
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::ensureSocket()").arg(LOG_THIS));
|
||||
if(!d->qsock) {
|
||||
d->qsock = new QTcpSocket;
|
||||
#if QT_VERSION >= 0x030200
|
||||
d->qsock->setReadBufferSize(READBUFSIZE);
|
||||
#endif
|
||||
d->qsock_relay = new QTcpSocketSignalRelay(d->qsock);
|
||||
connect(d->qsock_relay, SIGNAL(hostFound()), SLOT(qs_hostFound()));
|
||||
connect(d->qsock_relay, SIGNAL(connected()), SLOT(qs_connected()));
|
||||
connect(d->qsock_relay, SIGNAL(disconnected()), SLOT(qs_closed()));
|
||||
connect(d->qsock_relay, SIGNAL(readyRead()), SLOT(qs_readyRead()));
|
||||
connect(d->qsock_relay, SIGNAL(bytesWritten(qint64)), SLOT(qs_bytesWritten(qint64)));
|
||||
connect(d->qsock_relay, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(qs_error(QAbstractSocket::SocketError)));
|
||||
}
|
||||
}
|
||||
|
||||
void BSocket::connectToHost(const QString &host, quint16 port)
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::connectToHost(%2, %3)").arg(LOG_THIS).arg(host).arg(port));
|
||||
reset(true);
|
||||
d->host = host;
|
||||
d->port = port;
|
||||
#ifdef NO_NDNS
|
||||
d->state = Connecting;
|
||||
do_connect();
|
||||
#else
|
||||
d->state = HostLookup;
|
||||
d->ndns.resolve(d->host);
|
||||
#endif
|
||||
}
|
||||
|
||||
void BSocket::connectToServer(const QString &srv, const QString &type)
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::connectToServer(%2, %3)").arg(LOG_THIS).arg(srv).arg(type));
|
||||
reset(true);
|
||||
d->state = HostLookup;
|
||||
d->srv.resolve(srv, type, "tcp");
|
||||
}
|
||||
|
||||
int BSocket::socket() const
|
||||
{
|
||||
if(d->qsock)
|
||||
return d->qsock->socketDescriptor();
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
void BSocket::setSocket(int s)
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::setSocket(%2)").arg(LOG_THIS).arg(s));
|
||||
reset(true);
|
||||
ensureSocket();
|
||||
d->state = Connected;
|
||||
d->qsock->setSocketDescriptor(s);
|
||||
}
|
||||
|
||||
int BSocket::state() const
|
||||
{
|
||||
return d->state;
|
||||
}
|
||||
|
||||
bool BSocket::isOpen() const
|
||||
{
|
||||
if(d->state == Connected)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void BSocket::close()
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::close()").arg(LOG_THIS));
|
||||
if(d->state == Idle)
|
||||
return;
|
||||
|
||||
if(d->qsock) {
|
||||
d->qsock->close();
|
||||
d->state = Closing;
|
||||
if(d->qsock->bytesToWrite() == 0)
|
||||
reset();
|
||||
}
|
||||
else {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
void BSocket::write(const QByteArray &a)
|
||||
{
|
||||
if(d->state != Connected)
|
||||
return;
|
||||
#ifdef BS_DEBUG
|
||||
QString s = QString::fromUtf8(a);
|
||||
fprintf(stderr, "BSocket: writing [%d]: {%s}\n", a.size(), s.latin1());
|
||||
#endif
|
||||
d->qsock->write(a.data(), a.size());
|
||||
}
|
||||
|
||||
QByteArray BSocket::read(int bytes)
|
||||
{
|
||||
QByteArray block;
|
||||
if(d->qsock) {
|
||||
int max = bytesAvailable();
|
||||
if(bytes <= 0 || bytes > max)
|
||||
bytes = max;
|
||||
block.resize(bytes);
|
||||
d->qsock->read(block.data(), block.size());
|
||||
}
|
||||
else
|
||||
block = ByteStream::read(bytes);
|
||||
|
||||
#ifdef BS_DEBUG
|
||||
QString s = QString::fromUtf8(block);
|
||||
fprintf(stderr, "BSocket: read [%d]: {%s}\n", block.size(), s.latin1());
|
||||
#endif
|
||||
return block;
|
||||
}
|
||||
|
||||
int BSocket::bytesAvailable() const
|
||||
{
|
||||
if(d->qsock)
|
||||
return d->qsock->bytesAvailable();
|
||||
else
|
||||
return ByteStream::bytesAvailable();
|
||||
}
|
||||
|
||||
int BSocket::bytesToWrite() const
|
||||
{
|
||||
if(!d->qsock)
|
||||
return 0;
|
||||
return d->qsock->bytesToWrite();
|
||||
}
|
||||
|
||||
QHostAddress BSocket::address() const
|
||||
{
|
||||
if(d->qsock)
|
||||
return d->qsock->localAddress();
|
||||
else
|
||||
return QHostAddress();
|
||||
}
|
||||
|
||||
quint16 BSocket::port() const
|
||||
{
|
||||
if(d->qsock)
|
||||
return d->qsock->localPort();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
QHostAddress BSocket::peerAddress() const
|
||||
{
|
||||
if(d->qsock)
|
||||
return d->qsock->peerAddress();
|
||||
else
|
||||
return QHostAddress();
|
||||
}
|
||||
|
||||
quint16 BSocket::peerPort() const
|
||||
{
|
||||
if(d->qsock)
|
||||
return d->qsock->peerPort();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void BSocket::srv_done()
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::srv_done()").arg(LOG_THIS));
|
||||
if(d->srv.failed()) {
|
||||
#ifdef BS_DEBUG
|
||||
fprintf(stderr, "BSocket: Error resolving hostname.\n");
|
||||
#endif
|
||||
error(ErrHostNotFound);
|
||||
return;
|
||||
}
|
||||
|
||||
d->host = d->srv.resultAddress().toString();
|
||||
d->port = d->srv.resultPort();
|
||||
do_connect();
|
||||
//QTimer::singleShot(0, this, SLOT(do_connect()));
|
||||
//hostFound();
|
||||
}
|
||||
|
||||
void BSocket::ndns_done()
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::ndns_done()").arg(LOG_THIS));
|
||||
#ifndef NO_NDNS
|
||||
if(!d->ndns.result().isNull()) {
|
||||
d->host = d->ndns.resultString();
|
||||
d->state = Connecting;
|
||||
do_connect();
|
||||
//QTimer::singleShot(0, this, SLOT(do_connect()));
|
||||
//hostFound();
|
||||
}
|
||||
else {
|
||||
#ifdef BS_DEBUG
|
||||
fprintf(stderr, "BSocket: Error resolving hostname.\n");
|
||||
#endif
|
||||
error(ErrHostNotFound);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void BSocket::do_connect()
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::do_connect()").arg(LOG_THIS));
|
||||
#ifdef BS_DEBUG
|
||||
fprintf(stderr, "BSocket: Connecting to %s:%d\n", d->host.latin1(), d->port);
|
||||
#endif
|
||||
ensureSocket();
|
||||
d->qsock->connectToHost(d->host, d->port);
|
||||
}
|
||||
|
||||
void BSocket::qs_hostFound()
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::qs_hostFound()").arg(LOG_THIS));
|
||||
//SafeDeleteLock s(&d->sd);
|
||||
}
|
||||
|
||||
void BSocket::qs_connected()
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::qs_connected()").arg(LOG_THIS));
|
||||
d->state = Connected;
|
||||
#ifdef BS_DEBUG
|
||||
fprintf(stderr, "BSocket: Connected.\n");
|
||||
#endif
|
||||
//SafeDeleteLock s(&d->sd);
|
||||
connected();
|
||||
}
|
||||
|
||||
void BSocket::qs_closed()
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::qs_closed()").arg(LOG_THIS));
|
||||
if(d->state == Closing)
|
||||
{
|
||||
#ifdef BS_DEBUG
|
||||
fprintf(stderr, "BSocket: Delayed Close Finished.\n");
|
||||
#endif
|
||||
//SafeDeleteLock s(&d->sd);
|
||||
reset();
|
||||
delayedCloseFinished();
|
||||
}
|
||||
}
|
||||
|
||||
void BSocket::qs_readyRead()
|
||||
{
|
||||
//SafeDeleteLock s(&d->sd);
|
||||
readyRead();
|
||||
}
|
||||
|
||||
void BSocket::qs_bytesWritten(qint64 x64)
|
||||
{
|
||||
int x = x64;
|
||||
#ifdef BS_DEBUG
|
||||
fprintf(stderr, "BSocket: BytesWritten [%d].\n", x);
|
||||
#endif
|
||||
//SafeDeleteLock s(&d->sd);
|
||||
bytesWritten(x);
|
||||
}
|
||||
|
||||
void BSocket::qs_error(QAbstractSocket::SocketError x)
|
||||
{
|
||||
PsiLogger::instance()->log(QString("%1 BSocket::qs_error(%2)").arg(LOG_THIS).arg(x));
|
||||
if(x == QTcpSocket::RemoteHostClosedError) {
|
||||
#ifdef BS_DEBUG
|
||||
fprintf(stderr, "BSocket: Connection Closed.\n");
|
||||
#endif
|
||||
//SafeDeleteLock s(&d->sd);
|
||||
reset();
|
||||
connectionClosed();
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef BS_DEBUG
|
||||
fprintf(stderr, "BSocket: Error.\n");
|
||||
#endif
|
||||
//SafeDeleteLock s(&d->sd);
|
||||
|
||||
// connection error during SRV host connect? try next
|
||||
if(d->state == HostLookup && (x == QTcpSocket::ConnectionRefusedError || x == QTcpSocket::HostNotFoundError)) {
|
||||
d->srv.next();
|
||||
return;
|
||||
}
|
||||
|
||||
reset();
|
||||
if(x == QTcpSocket::ConnectionRefusedError)
|
||||
error(ErrConnectionRefused);
|
||||
else if(x == QTcpSocket::HostNotFoundError)
|
||||
error(ErrHostNotFound);
|
||||
else
|
||||
error(ErrRead);
|
||||
}
|
||||
|
||||
#include "bsocket.moc"
|
||||
|
||||
// CS_NAMESPACE_END
|
||||
90
iris-legacy/cutestuff/network/bsocket.h
Normal file
90
iris-legacy/cutestuff/network/bsocket.h
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* bsocket.h - QSocket wrapper based on Bytestream with SRV DNS support
|
||||
* Copyright (C) 2003 Justin Karneges
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CS_BSOCKET_H
|
||||
#define CS_BSOCKET_H
|
||||
|
||||
#include <QAbstractSocket>
|
||||
|
||||
#include "bytestream.h"
|
||||
|
||||
class QString;
|
||||
class QObject;
|
||||
class QByteArray;
|
||||
|
||||
// CS_NAMESPACE_BEGIN
|
||||
|
||||
class BSocket : public ByteStream
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Error { ErrConnectionRefused = ErrCustom, ErrHostNotFound };
|
||||
enum State { Idle, HostLookup, Connecting, Connected, Closing };
|
||||
BSocket(QObject *parent=0);
|
||||
~BSocket();
|
||||
|
||||
void connectToHost(const QString &host, quint16 port);
|
||||
void connectToServer(const QString &srv, const QString &type);
|
||||
int socket() const;
|
||||
void setSocket(int);
|
||||
int state() const;
|
||||
|
||||
// from ByteStream
|
||||
bool isOpen() const;
|
||||
void close();
|
||||
void write(const QByteArray &);
|
||||
QByteArray read(int bytes=0);
|
||||
int bytesAvailable() const;
|
||||
int bytesToWrite() const;
|
||||
|
||||
// local
|
||||
QHostAddress address() const;
|
||||
quint16 port() const;
|
||||
|
||||
// remote
|
||||
QHostAddress peerAddress() const;
|
||||
quint16 peerPort() const;
|
||||
|
||||
signals:
|
||||
void hostFound();
|
||||
void connected();
|
||||
|
||||
private slots:
|
||||
void qs_hostFound();
|
||||
void qs_connected();
|
||||
void qs_closed();
|
||||
void qs_readyRead();
|
||||
void qs_bytesWritten(qint64);
|
||||
void qs_error(QAbstractSocket::SocketError);
|
||||
void srv_done();
|
||||
void ndns_done();
|
||||
void do_connect();
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private *d;
|
||||
|
||||
void reset(bool clear=false);
|
||||
void ensureSocket();
|
||||
};
|
||||
|
||||
// CS_NAMESPACE_END
|
||||
|
||||
#endif
|
||||
363
iris-legacy/cutestuff/network/httpconnect.cpp
Normal file
363
iris-legacy/cutestuff/network/httpconnect.cpp
Normal file
|
|
@ -0,0 +1,363 @@
|
|||
/*
|
||||
* httpconnect.cpp - HTTP "CONNECT" proxy
|
||||
* Copyright (C) 2003 Justin Karneges
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include "httpconnect.h"
|
||||
|
||||
#include <qstringlist.h>
|
||||
//Added by qt3to4:
|
||||
#include <Q3CString>
|
||||
#include "bsocket.h"
|
||||
#include <QtCrypto>
|
||||
|
||||
#ifdef PROX_DEBUG
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
// CS_NAMESPACE_BEGIN
|
||||
|
||||
static QString extractLine(QByteArray *buf, bool *found)
|
||||
{
|
||||
// Scan for newline
|
||||
int index = buf->indexOf ("\r\n");
|
||||
if (index == -1) {
|
||||
// Newline not found
|
||||
if (found)
|
||||
*found = false;
|
||||
return "";
|
||||
}
|
||||
else {
|
||||
// Found newline
|
||||
QString s = QString::fromAscii(buf->left(index));
|
||||
buf->remove(0, index + 2);
|
||||
|
||||
if (found)
|
||||
*found = true;
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
static bool extractMainHeader(const QString &line, QString *proto, int *code, QString *msg)
|
||||
{
|
||||
int n = line.find(' ');
|
||||
if(n == -1)
|
||||
return false;
|
||||
if(proto)
|
||||
*proto = line.mid(0, n);
|
||||
++n;
|
||||
int n2 = line.find(' ', n);
|
||||
if(n2 == -1)
|
||||
return false;
|
||||
if(code)
|
||||
*code = line.mid(n, n2-n).toInt();
|
||||
n = n2+1;
|
||||
if(msg)
|
||||
*msg = line.mid(n);
|
||||
return true;
|
||||
}
|
||||
|
||||
class HttpConnect::Private
|
||||
{
|
||||
public:
|
||||
Private() {}
|
||||
|
||||
BSocket sock;
|
||||
QString host;
|
||||
int port;
|
||||
QString user, pass;
|
||||
QString real_host;
|
||||
int real_port;
|
||||
|
||||
QByteArray recvBuf;
|
||||
|
||||
bool inHeader;
|
||||
QStringList headerLines;
|
||||
|
||||
int toWrite;
|
||||
bool active;
|
||||
};
|
||||
|
||||
HttpConnect::HttpConnect(QObject *parent)
|
||||
:ByteStream(parent)
|
||||
{
|
||||
d = new Private;
|
||||
connect(&d->sock, SIGNAL(connected()), SLOT(sock_connected()));
|
||||
connect(&d->sock, SIGNAL(connectionClosed()), SLOT(sock_connectionClosed()));
|
||||
connect(&d->sock, SIGNAL(delayedCloseFinished()), SLOT(sock_delayedCloseFinished()));
|
||||
connect(&d->sock, SIGNAL(readyRead()), SLOT(sock_readyRead()));
|
||||
connect(&d->sock, SIGNAL(bytesWritten(int)), SLOT(sock_bytesWritten(int)));
|
||||
connect(&d->sock, SIGNAL(error(int)), SLOT(sock_error(int)));
|
||||
|
||||
reset(true);
|
||||
}
|
||||
|
||||
HttpConnect::~HttpConnect()
|
||||
{
|
||||
reset(true);
|
||||
delete d;
|
||||
}
|
||||
|
||||
void HttpConnect::reset(bool clear)
|
||||
{
|
||||
if(d->sock.state() != BSocket::Idle)
|
||||
d->sock.close();
|
||||
if(clear) {
|
||||
clearReadBuffer();
|
||||
d->recvBuf.resize(0);
|
||||
}
|
||||
d->active = false;
|
||||
}
|
||||
|
||||
void HttpConnect::setAuth(const QString &user, const QString &pass)
|
||||
{
|
||||
d->user = user;
|
||||
d->pass = pass;
|
||||
}
|
||||
|
||||
void HttpConnect::connectToHost(const QString &proxyHost, int proxyPort, const QString &host, int port)
|
||||
{
|
||||
reset(true);
|
||||
|
||||
d->host = proxyHost;
|
||||
d->port = proxyPort;
|
||||
d->real_host = host;
|
||||
d->real_port = port;
|
||||
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpConnect: Connecting to %s:%d", proxyHost.latin1(), proxyPort);
|
||||
if(d->user.isEmpty())
|
||||
fprintf(stderr, "\n");
|
||||
else
|
||||
fprintf(stderr, ", auth {%s,%s}\n", d->user.latin1(), d->pass.latin1());
|
||||
#endif
|
||||
d->sock.connectToHost(d->host, d->port);
|
||||
}
|
||||
|
||||
bool HttpConnect::isOpen() const
|
||||
{
|
||||
return d->active;
|
||||
}
|
||||
|
||||
void HttpConnect::close()
|
||||
{
|
||||
d->sock.close();
|
||||
if(d->sock.bytesToWrite() == 0)
|
||||
reset();
|
||||
}
|
||||
|
||||
void HttpConnect::write(const QByteArray &buf)
|
||||
{
|
||||
if(d->active)
|
||||
d->sock.write(buf);
|
||||
}
|
||||
|
||||
QByteArray HttpConnect::read(int bytes)
|
||||
{
|
||||
return ByteStream::read(bytes);
|
||||
}
|
||||
|
||||
int HttpConnect::bytesAvailable() const
|
||||
{
|
||||
return ByteStream::bytesAvailable();
|
||||
}
|
||||
|
||||
int HttpConnect::bytesToWrite() const
|
||||
{
|
||||
if(d->active)
|
||||
return d->sock.bytesToWrite();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void HttpConnect::sock_connected()
|
||||
{
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpConnect: Connected\n");
|
||||
#endif
|
||||
d->inHeader = true;
|
||||
d->headerLines.clear();
|
||||
|
||||
// connected, now send the request
|
||||
QString s;
|
||||
s += QString("CONNECT ") + d->real_host + ':' + QString::number(d->real_port) + " HTTP/1.0\r\n";
|
||||
if(!d->user.isEmpty()) {
|
||||
QString str = d->user + ':' + d->pass;
|
||||
s += QString("Proxy-Authorization: Basic ") + QCA::Base64().encodeString(str) + "\r\n";
|
||||
}
|
||||
s += "Pragma: no-cache\r\n";
|
||||
s += "\r\n";
|
||||
|
||||
Q3CString cs = s.utf8();
|
||||
QByteArray block(cs.length());
|
||||
memcpy(block.data(), cs.data(), block.size());
|
||||
d->toWrite = block.size();
|
||||
d->sock.write(block);
|
||||
}
|
||||
|
||||
void HttpConnect::sock_connectionClosed()
|
||||
{
|
||||
if(d->active) {
|
||||
reset();
|
||||
connectionClosed();
|
||||
}
|
||||
else {
|
||||
error(ErrProxyNeg);
|
||||
}
|
||||
}
|
||||
|
||||
void HttpConnect::sock_delayedCloseFinished()
|
||||
{
|
||||
if(d->active) {
|
||||
reset();
|
||||
delayedCloseFinished();
|
||||
}
|
||||
}
|
||||
|
||||
void HttpConnect::sock_readyRead()
|
||||
{
|
||||
QByteArray block = d->sock.read();
|
||||
|
||||
if(!d->active) {
|
||||
ByteStream::appendArray(&d->recvBuf, block);
|
||||
|
||||
if(d->inHeader) {
|
||||
// grab available lines
|
||||
while(1) {
|
||||
bool found;
|
||||
QString line = extractLine(&d->recvBuf, &found);
|
||||
if(!found)
|
||||
break;
|
||||
if(line.isEmpty()) {
|
||||
d->inHeader = false;
|
||||
break;
|
||||
}
|
||||
d->headerLines += line;
|
||||
}
|
||||
|
||||
// done with grabbing the header?
|
||||
if(!d->inHeader) {
|
||||
QString str = d->headerLines.first();
|
||||
d->headerLines.remove(d->headerLines.begin());
|
||||
|
||||
QString proto;
|
||||
int code;
|
||||
QString msg;
|
||||
if(!extractMainHeader(str, &proto, &code, &msg)) {
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpConnect: invalid header!\n");
|
||||
#endif
|
||||
reset(true);
|
||||
error(ErrProxyNeg);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpConnect: header proto=[%s] code=[%d] msg=[%s]\n", proto.latin1(), code, msg.latin1());
|
||||
for(QStringList::ConstIterator it = d->headerLines.begin(); it != d->headerLines.end(); ++it)
|
||||
fprintf(stderr, "HttpConnect: * [%s]\n", (*it).latin1());
|
||||
#endif
|
||||
}
|
||||
|
||||
if(code == 200) { // OK
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpConnect: << Success >>\n");
|
||||
#endif
|
||||
d->active = true;
|
||||
connected();
|
||||
|
||||
if(!d->recvBuf.isEmpty()) {
|
||||
appendRead(d->recvBuf);
|
||||
d->recvBuf.resize(0);
|
||||
readyRead();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int err;
|
||||
QString errStr;
|
||||
if(code == 407) { // Authentication failed
|
||||
err = ErrProxyAuth;
|
||||
errStr = tr("Authentication failed");
|
||||
}
|
||||
else if(code == 404) { // Host not found
|
||||
err = ErrHostNotFound;
|
||||
errStr = tr("Host not found");
|
||||
}
|
||||
else if(code == 403) { // Access denied
|
||||
err = ErrProxyNeg;
|
||||
errStr = tr("Access denied");
|
||||
}
|
||||
else if(code == 503) { // Connection refused
|
||||
err = ErrConnectionRefused;
|
||||
errStr = tr("Connection refused");
|
||||
}
|
||||
else { // invalid reply
|
||||
err = ErrProxyNeg;
|
||||
errStr = tr("Invalid reply");
|
||||
}
|
||||
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpConnect: << Error >> [%s]\n", errStr.latin1());
|
||||
#endif
|
||||
reset(true);
|
||||
error(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
appendRead(block);
|
||||
readyRead();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void HttpConnect::sock_bytesWritten(int x)
|
||||
{
|
||||
if(d->toWrite > 0) {
|
||||
int size = x;
|
||||
if(d->toWrite < x)
|
||||
size = d->toWrite;
|
||||
d->toWrite -= size;
|
||||
x -= size;
|
||||
}
|
||||
|
||||
if(d->active && x > 0)
|
||||
bytesWritten(x);
|
||||
}
|
||||
|
||||
void HttpConnect::sock_error(int x)
|
||||
{
|
||||
if(d->active) {
|
||||
reset();
|
||||
error(ErrRead);
|
||||
}
|
||||
else {
|
||||
reset(true);
|
||||
if(x == BSocket::ErrHostNotFound)
|
||||
error(ErrProxyConnect);
|
||||
else if(x == BSocket::ErrConnectionRefused)
|
||||
error(ErrProxyConnect);
|
||||
else if(x == BSocket::ErrRead)
|
||||
error(ErrProxyNeg);
|
||||
}
|
||||
}
|
||||
|
||||
// CS_NAMESPACE_END
|
||||
67
iris-legacy/cutestuff/network/httpconnect.h
Normal file
67
iris-legacy/cutestuff/network/httpconnect.h
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* httpconnect.h - HTTP "CONNECT" proxy
|
||||
* Copyright (C) 2003 Justin Karneges
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CS_HTTPCONNECT_H
|
||||
#define CS_HTTPCONNECT_H
|
||||
|
||||
#include "bytestream.h"
|
||||
|
||||
// CS_NAMESPACE_BEGIN
|
||||
|
||||
class HttpConnect : public ByteStream
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Error { ErrConnectionRefused = ErrCustom, ErrHostNotFound, ErrProxyConnect, ErrProxyNeg, ErrProxyAuth };
|
||||
HttpConnect(QObject *parent=0);
|
||||
~HttpConnect();
|
||||
|
||||
void setAuth(const QString &user, const QString &pass="");
|
||||
void connectToHost(const QString &proxyHost, int proxyPort, const QString &host, int port);
|
||||
|
||||
// from ByteStream
|
||||
bool isOpen() const;
|
||||
void close();
|
||||
void write(const QByteArray &);
|
||||
QByteArray read(int bytes=0);
|
||||
int bytesAvailable() const;
|
||||
int bytesToWrite() const;
|
||||
|
||||
signals:
|
||||
void connected();
|
||||
|
||||
private slots:
|
||||
void sock_connected();
|
||||
void sock_connectionClosed();
|
||||
void sock_delayedCloseFinished();
|
||||
void sock_readyRead();
|
||||
void sock_bytesWritten(int);
|
||||
void sock_error(int);
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private *d;
|
||||
|
||||
void reset(bool clear=false);
|
||||
};
|
||||
|
||||
// CS_NAMESPACE_END
|
||||
|
||||
#endif
|
||||
666
iris-legacy/cutestuff/network/httppoll.cpp
Normal file
666
iris-legacy/cutestuff/network/httppoll.cpp
Normal file
|
|
@ -0,0 +1,666 @@
|
|||
/*
|
||||
* httppoll.cpp - HTTP polling proxy
|
||||
* Copyright (C) 2003 Justin Karneges
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include "httppoll.h"
|
||||
|
||||
#include <qstringlist.h>
|
||||
#include <q3url.h>
|
||||
#include <qtimer.h>
|
||||
#include <qpointer.h>
|
||||
#include <QtCrypto>
|
||||
//Added by qt3to4:
|
||||
#include <Q3CString>
|
||||
#include <stdlib.h>
|
||||
#include "bsocket.h"
|
||||
|
||||
#ifdef PROX_DEBUG
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#define POLL_KEYS 64
|
||||
|
||||
// CS_NAMESPACE_BEGIN
|
||||
|
||||
static QByteArray randomArray(int size)
|
||||
{
|
||||
QByteArray a(size);
|
||||
for(int n = 0; n < size; ++n)
|
||||
a[n] = (char)(256.0*rand()/(RAND_MAX+1.0));
|
||||
return a;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// HttpPoll
|
||||
//----------------------------------------------------------------------------
|
||||
static QString hpk(int n, const QString &s)
|
||||
{
|
||||
if(n == 0)
|
||||
return s;
|
||||
else
|
||||
return QCA::Base64().arrayToString( QCA::Hash("sha1").hash( Q3CString(hpk(n - 1, s).latin1()) ).toByteArray() );
|
||||
}
|
||||
|
||||
class HttpPoll::Private
|
||||
{
|
||||
public:
|
||||
Private() {}
|
||||
|
||||
HttpProxyPost http;
|
||||
QString host;
|
||||
int port;
|
||||
QString user, pass;
|
||||
QString url;
|
||||
bool use_proxy;
|
||||
|
||||
QByteArray out;
|
||||
|
||||
int state;
|
||||
bool closing;
|
||||
QString ident;
|
||||
|
||||
QTimer *t;
|
||||
|
||||
QString key[POLL_KEYS];
|
||||
int key_n;
|
||||
|
||||
int polltime;
|
||||
};
|
||||
|
||||
HttpPoll::HttpPoll(QObject *parent)
|
||||
:ByteStream(parent)
|
||||
{
|
||||
d = new Private;
|
||||
|
||||
d->polltime = 30;
|
||||
d->t = new QTimer;
|
||||
connect(d->t, SIGNAL(timeout()), SLOT(do_sync()));
|
||||
|
||||
connect(&d->http, SIGNAL(result()), SLOT(http_result()));
|
||||
connect(&d->http, SIGNAL(error(int)), SLOT(http_error(int)));
|
||||
|
||||
reset(true);
|
||||
}
|
||||
|
||||
HttpPoll::~HttpPoll()
|
||||
{
|
||||
reset(true);
|
||||
delete d->t;
|
||||
delete d;
|
||||
}
|
||||
|
||||
void HttpPoll::reset(bool clear)
|
||||
{
|
||||
if(d->http.isActive())
|
||||
d->http.stop();
|
||||
if(clear)
|
||||
clearReadBuffer();
|
||||
clearWriteBuffer();
|
||||
d->out.resize(0);
|
||||
d->state = 0;
|
||||
d->closing = false;
|
||||
d->t->stop();
|
||||
}
|
||||
|
||||
void HttpPoll::setAuth(const QString &user, const QString &pass)
|
||||
{
|
||||
d->user = user;
|
||||
d->pass = pass;
|
||||
}
|
||||
|
||||
void HttpPoll::connectToUrl(const QString &url)
|
||||
{
|
||||
connectToHost("", 0, url);
|
||||
}
|
||||
|
||||
void HttpPoll::connectToHost(const QString &proxyHost, int proxyPort, const QString &url)
|
||||
{
|
||||
reset(true);
|
||||
|
||||
// using proxy?
|
||||
if(!proxyHost.isEmpty()) {
|
||||
d->host = proxyHost;
|
||||
d->port = proxyPort;
|
||||
d->url = url;
|
||||
d->use_proxy = true;
|
||||
}
|
||||
else {
|
||||
Q3Url u = url;
|
||||
d->host = u.host();
|
||||
if(u.hasPort())
|
||||
d->port = u.port();
|
||||
else
|
||||
d->port = 80;
|
||||
d->url = u.encodedPathAndQuery();
|
||||
d->use_proxy = false;
|
||||
}
|
||||
|
||||
resetKey();
|
||||
bool last;
|
||||
QString key = getKey(&last);
|
||||
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpPoll: Connecting to %s:%d [%s]", d->host.latin1(), d->port, d->url.latin1());
|
||||
if(d->user.isEmpty())
|
||||
fprintf(stderr, "\n");
|
||||
else
|
||||
fprintf(stderr, ", auth {%s,%s}\n", d->user.latin1(), d->pass.latin1());
|
||||
#endif
|
||||
QPointer<QObject> self = this;
|
||||
syncStarted();
|
||||
if(!self)
|
||||
return;
|
||||
|
||||
d->state = 1;
|
||||
d->http.setAuth(d->user, d->pass);
|
||||
d->http.post(d->host, d->port, d->url, makePacket("0", key, "", QByteArray()), d->use_proxy);
|
||||
}
|
||||
|
||||
QByteArray HttpPoll::makePacket(const QString &ident, const QString &key, const QString &newkey, const QByteArray &block)
|
||||
{
|
||||
QString str = ident;
|
||||
if(!key.isEmpty()) {
|
||||
str += ';';
|
||||
str += key;
|
||||
}
|
||||
if(!newkey.isEmpty()) {
|
||||
str += ';';
|
||||
str += newkey;
|
||||
}
|
||||
str += ',';
|
||||
Q3CString cs = str.latin1();
|
||||
int len = cs.length();
|
||||
|
||||
QByteArray a(len + block.size());
|
||||
memcpy(a.data(), cs.data(), len);
|
||||
memcpy(a.data() + len, block.data(), block.size());
|
||||
return a;
|
||||
}
|
||||
|
||||
int HttpPoll::pollInterval() const
|
||||
{
|
||||
return d->polltime;
|
||||
}
|
||||
|
||||
void HttpPoll::setPollInterval(int seconds)
|
||||
{
|
||||
d->polltime = seconds;
|
||||
}
|
||||
|
||||
bool HttpPoll::isOpen() const
|
||||
{
|
||||
return (d->state == 2 ? true: false);
|
||||
}
|
||||
|
||||
void HttpPoll::close()
|
||||
{
|
||||
if(d->state == 0 || d->closing)
|
||||
return;
|
||||
|
||||
if(bytesToWrite() == 0)
|
||||
reset();
|
||||
else
|
||||
d->closing = true;
|
||||
}
|
||||
|
||||
void HttpPoll::http_result()
|
||||
{
|
||||
// check for death :)
|
||||
QPointer<QObject> self = this;
|
||||
syncFinished();
|
||||
if(!self)
|
||||
return;
|
||||
|
||||
// get id and packet
|
||||
QString id;
|
||||
QString cookie = d->http.getHeader("Set-Cookie");
|
||||
int n = cookie.find("ID=");
|
||||
if(n == -1) {
|
||||
reset();
|
||||
error(ErrRead);
|
||||
return;
|
||||
}
|
||||
n += 3;
|
||||
int n2 = cookie.find(';', n);
|
||||
if(n2 != -1)
|
||||
id = cookie.mid(n, n2-n);
|
||||
else
|
||||
id = cookie.mid(n);
|
||||
QByteArray block = d->http.body();
|
||||
|
||||
// session error?
|
||||
if(id.right(2) == ":0") {
|
||||
if(id == "0:0" && d->state == 2) {
|
||||
reset();
|
||||
connectionClosed();
|
||||
return;
|
||||
}
|
||||
else {
|
||||
reset();
|
||||
error(ErrRead);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
d->ident = id;
|
||||
bool justNowConnected = false;
|
||||
if(d->state == 1) {
|
||||
d->state = 2;
|
||||
justNowConnected = true;
|
||||
}
|
||||
|
||||
// sync up again soon
|
||||
if(bytesToWrite() > 0 || !d->closing)
|
||||
d->t->start(d->polltime * 1000, true);
|
||||
|
||||
// connecting
|
||||
if(justNowConnected) {
|
||||
connected();
|
||||
}
|
||||
else {
|
||||
if(!d->out.isEmpty()) {
|
||||
int x = d->out.size();
|
||||
d->out.resize(0);
|
||||
takeWrite(x);
|
||||
bytesWritten(x);
|
||||
}
|
||||
}
|
||||
|
||||
if(!self)
|
||||
return;
|
||||
|
||||
if(!block.isEmpty()) {
|
||||
appendRead(block);
|
||||
readyRead();
|
||||
}
|
||||
|
||||
if(!self)
|
||||
return;
|
||||
|
||||
if(bytesToWrite() > 0) {
|
||||
do_sync();
|
||||
}
|
||||
else {
|
||||
if(d->closing) {
|
||||
reset();
|
||||
delayedCloseFinished();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HttpPoll::http_error(int x)
|
||||
{
|
||||
reset();
|
||||
if(x == HttpProxyPost::ErrConnectionRefused)
|
||||
error(ErrConnectionRefused);
|
||||
else if(x == HttpProxyPost::ErrHostNotFound)
|
||||
error(ErrHostNotFound);
|
||||
else if(x == HttpProxyPost::ErrSocket)
|
||||
error(ErrRead);
|
||||
else if(x == HttpProxyPost::ErrProxyConnect)
|
||||
error(ErrProxyConnect);
|
||||
else if(x == HttpProxyPost::ErrProxyNeg)
|
||||
error(ErrProxyNeg);
|
||||
else if(x == HttpProxyPost::ErrProxyAuth)
|
||||
error(ErrProxyAuth);
|
||||
}
|
||||
|
||||
int HttpPoll::tryWrite()
|
||||
{
|
||||
if(!d->http.isActive())
|
||||
do_sync();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void HttpPoll::do_sync()
|
||||
{
|
||||
if(d->http.isActive())
|
||||
return;
|
||||
|
||||
d->t->stop();
|
||||
d->out = takeWrite(0, false);
|
||||
|
||||
bool last;
|
||||
QString key = getKey(&last);
|
||||
QString newkey;
|
||||
if(last) {
|
||||
resetKey();
|
||||
newkey = getKey(&last);
|
||||
}
|
||||
|
||||
QPointer<QObject> self = this;
|
||||
syncStarted();
|
||||
if(!self)
|
||||
return;
|
||||
|
||||
d->http.post(d->host, d->port, d->url, makePacket(d->ident, key, newkey, d->out), d->use_proxy);
|
||||
}
|
||||
|
||||
void HttpPoll::resetKey()
|
||||
{
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpPoll: reset key!\n");
|
||||
#endif
|
||||
QByteArray a = randomArray(64);
|
||||
QString str = QString::fromLatin1(a.data(), a.size());
|
||||
|
||||
d->key_n = POLL_KEYS;
|
||||
for(int n = 0; n < POLL_KEYS; ++n)
|
||||
d->key[n] = hpk(n+1, str);
|
||||
}
|
||||
|
||||
const QString & HttpPoll::getKey(bool *last)
|
||||
{
|
||||
*last = false;
|
||||
--(d->key_n);
|
||||
if(d->key_n == 0)
|
||||
*last = true;
|
||||
return d->key[d->key_n];
|
||||
}
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// HttpProxyPost
|
||||
//----------------------------------------------------------------------------
|
||||
static QString extractLine(QByteArray *buf, bool *found)
|
||||
{
|
||||
// scan for newline
|
||||
int n;
|
||||
for(n = 0; n < (int)buf->size()-1; ++n) {
|
||||
if(buf->at(n) == '\r' && buf->at(n+1) == '\n') {
|
||||
//Q3CString cstr;
|
||||
//cstr.resize(n+1);
|
||||
QByteArray cstr;
|
||||
cstr.resize(n);
|
||||
memcpy(cstr.data(), buf->data(), n);
|
||||
n += 2; // hack off CR/LF
|
||||
|
||||
memmove(buf->data(), buf->data() + n, buf->size() - n);
|
||||
buf->resize(buf->size() - n);
|
||||
QString s = QString::fromUtf8(cstr);
|
||||
|
||||
if(found)
|
||||
*found = true;
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
if(found)
|
||||
*found = false;
|
||||
return "";
|
||||
}
|
||||
|
||||
static bool extractMainHeader(const QString &line, QString *proto, int *code, QString *msg)
|
||||
{
|
||||
int n = line.find(' ');
|
||||
if(n == -1)
|
||||
return false;
|
||||
if(proto)
|
||||
*proto = line.mid(0, n);
|
||||
++n;
|
||||
int n2 = line.find(' ', n);
|
||||
if(n2 == -1)
|
||||
return false;
|
||||
if(code)
|
||||
*code = line.mid(n, n2-n).toInt();
|
||||
n = n2+1;
|
||||
if(msg)
|
||||
*msg = line.mid(n);
|
||||
return true;
|
||||
}
|
||||
|
||||
class HttpProxyPost::Private
|
||||
{
|
||||
public:
|
||||
Private() {}
|
||||
|
||||
BSocket sock;
|
||||
QByteArray postdata, recvBuf, body;
|
||||
QString url;
|
||||
QString user, pass;
|
||||
bool inHeader;
|
||||
QStringList headerLines;
|
||||
bool asProxy;
|
||||
QString host;
|
||||
};
|
||||
|
||||
HttpProxyPost::HttpProxyPost(QObject *parent)
|
||||
:QObject(parent)
|
||||
{
|
||||
d = new Private;
|
||||
connect(&d->sock, SIGNAL(connected()), SLOT(sock_connected()));
|
||||
connect(&d->sock, SIGNAL(connectionClosed()), SLOT(sock_connectionClosed()));
|
||||
connect(&d->sock, SIGNAL(readyRead()), SLOT(sock_readyRead()));
|
||||
connect(&d->sock, SIGNAL(error(int)), SLOT(sock_error(int)));
|
||||
reset(true);
|
||||
}
|
||||
|
||||
HttpProxyPost::~HttpProxyPost()
|
||||
{
|
||||
reset(true);
|
||||
delete d;
|
||||
}
|
||||
|
||||
void HttpProxyPost::reset(bool clear)
|
||||
{
|
||||
if(d->sock.state() != BSocket::Idle)
|
||||
d->sock.close();
|
||||
d->recvBuf.resize(0);
|
||||
if(clear)
|
||||
d->body.resize(0);
|
||||
}
|
||||
|
||||
void HttpProxyPost::setAuth(const QString &user, const QString &pass)
|
||||
{
|
||||
d->user = user;
|
||||
d->pass = pass;
|
||||
}
|
||||
|
||||
bool HttpProxyPost::isActive() const
|
||||
{
|
||||
return (d->sock.state() == BSocket::Idle ? false: true);
|
||||
}
|
||||
|
||||
void HttpProxyPost::post(const QString &proxyHost, int proxyPort, const QString &url, const QByteArray &data, bool asProxy)
|
||||
{
|
||||
reset(true);
|
||||
|
||||
d->host = proxyHost;
|
||||
d->url = url;
|
||||
d->postdata = data;
|
||||
d->asProxy = asProxy;
|
||||
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpProxyPost: Connecting to %s:%d", proxyHost.latin1(), proxyPort);
|
||||
if(d->user.isEmpty())
|
||||
fprintf(stderr, "\n");
|
||||
else
|
||||
fprintf(stderr, ", auth {%s,%s}\n", d->user.latin1(), d->pass.latin1());
|
||||
#endif
|
||||
d->sock.connectToHost(proxyHost, proxyPort);
|
||||
}
|
||||
|
||||
void HttpProxyPost::stop()
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
QByteArray HttpProxyPost::body() const
|
||||
{
|
||||
return d->body;
|
||||
}
|
||||
|
||||
QString HttpProxyPost::getHeader(const QString &var) const
|
||||
{
|
||||
for(QStringList::ConstIterator it = d->headerLines.begin(); it != d->headerLines.end(); ++it) {
|
||||
const QString &s = *it;
|
||||
int n = s.find(": ");
|
||||
if(n == -1)
|
||||
continue;
|
||||
QString v = s.mid(0, n);
|
||||
if(v == var)
|
||||
return s.mid(n+2);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
void HttpProxyPost::sock_connected()
|
||||
{
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpProxyPost: Connected\n");
|
||||
#endif
|
||||
d->inHeader = true;
|
||||
d->headerLines.clear();
|
||||
|
||||
Q3Url u = d->url;
|
||||
|
||||
// connected, now send the request
|
||||
QString s;
|
||||
s += QString("POST ") + d->url + " HTTP/1.0\r\n";
|
||||
if(d->asProxy) {
|
||||
if(!d->user.isEmpty()) {
|
||||
QString str = d->user + ':' + d->pass;
|
||||
s += QString("Proxy-Authorization: Basic ") + QCA::Base64().encodeString(str) + "\r\n";
|
||||
}
|
||||
s += "Pragma: no-cache\r\n";
|
||||
s += QString("Host: ") + u.host() + "\r\n";
|
||||
}
|
||||
else {
|
||||
s += QString("Host: ") + d->host + "\r\n";
|
||||
}
|
||||
s += "Content-Type: application/x-www-form-urlencoded\r\n";
|
||||
s += QString("Content-Length: ") + QString::number(d->postdata.size()) + "\r\n";
|
||||
s += "\r\n";
|
||||
|
||||
// write request
|
||||
Q3CString cs = s.utf8();
|
||||
QByteArray block(cs.length());
|
||||
memcpy(block.data(), cs.data(), block.size());
|
||||
d->sock.write(block);
|
||||
|
||||
// write postdata
|
||||
d->sock.write(d->postdata);
|
||||
}
|
||||
|
||||
void HttpProxyPost::sock_connectionClosed()
|
||||
{
|
||||
d->body = d->recvBuf;
|
||||
reset();
|
||||
result();
|
||||
}
|
||||
|
||||
void HttpProxyPost::sock_readyRead()
|
||||
{
|
||||
QByteArray block = d->sock.read();
|
||||
ByteStream::appendArray(&d->recvBuf, block);
|
||||
|
||||
if(d->inHeader) {
|
||||
// grab available lines
|
||||
while(1) {
|
||||
bool found;
|
||||
QString line = extractLine(&d->recvBuf, &found);
|
||||
if(!found)
|
||||
break;
|
||||
if(line.isEmpty()) {
|
||||
d->inHeader = false;
|
||||
break;
|
||||
}
|
||||
d->headerLines += line;
|
||||
}
|
||||
|
||||
// done with grabbing the header?
|
||||
if(!d->inHeader) {
|
||||
QString str = d->headerLines.first();
|
||||
d->headerLines.remove(d->headerLines.begin());
|
||||
|
||||
QString proto;
|
||||
int code;
|
||||
QString msg;
|
||||
if(!extractMainHeader(str, &proto, &code, &msg)) {
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpProxyPost: invalid header!\n");
|
||||
#endif
|
||||
reset(true);
|
||||
error(ErrProxyNeg);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpProxyPost: header proto=[%s] code=[%d] msg=[%s]\n", proto.latin1(), code, msg.latin1());
|
||||
for(QStringList::ConstIterator it = d->headerLines.begin(); it != d->headerLines.end(); ++it)
|
||||
fprintf(stderr, "HttpProxyPost: * [%s]\n", (*it).latin1());
|
||||
#endif
|
||||
}
|
||||
|
||||
if(code == 200) { // OK
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpProxyPost: << Success >>\n");
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
int err;
|
||||
QString errStr;
|
||||
if(code == 407) { // Authentication failed
|
||||
err = ErrProxyAuth;
|
||||
errStr = tr("Authentication failed");
|
||||
}
|
||||
else if(code == 404) { // Host not found
|
||||
err = ErrHostNotFound;
|
||||
errStr = tr("Host not found");
|
||||
}
|
||||
else if(code == 403) { // Access denied
|
||||
err = ErrProxyNeg;
|
||||
errStr = tr("Access denied");
|
||||
}
|
||||
else if(code == 503) { // Connection refused
|
||||
err = ErrConnectionRefused;
|
||||
errStr = tr("Connection refused");
|
||||
}
|
||||
else { // invalid reply
|
||||
err = ErrProxyNeg;
|
||||
errStr = tr("Invalid reply");
|
||||
}
|
||||
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpProxyPost: << Error >> [%s]\n", errStr.latin1());
|
||||
#endif
|
||||
reset(true);
|
||||
error(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HttpProxyPost::sock_error(int x)
|
||||
{
|
||||
#ifdef PROX_DEBUG
|
||||
fprintf(stderr, "HttpProxyPost: socket error: %d\n", x);
|
||||
#endif
|
||||
reset(true);
|
||||
if(x == BSocket::ErrHostNotFound)
|
||||
error(ErrProxyConnect);
|
||||
else if(x == BSocket::ErrConnectionRefused)
|
||||
error(ErrProxyConnect);
|
||||
else if(x == BSocket::ErrRead)
|
||||
error(ErrProxyNeg);
|
||||
}
|
||||
|
||||
// CS_NAMESPACE_END
|
||||
104
iris-legacy/cutestuff/network/httppoll.h
Normal file
104
iris-legacy/cutestuff/network/httppoll.h
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* httppoll.h - HTTP polling proxy
|
||||
* Copyright (C) 2003 Justin Karneges
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CS_HTTPPOLL_H
|
||||
#define CS_HTTPPOLL_H
|
||||
|
||||
#include "bytestream.h"
|
||||
|
||||
// CS_NAMESPACE_BEGIN
|
||||
|
||||
class HttpPoll : public ByteStream
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Error { ErrConnectionRefused = ErrCustom, ErrHostNotFound, ErrProxyConnect, ErrProxyNeg, ErrProxyAuth };
|
||||
HttpPoll(QObject *parent=0);
|
||||
~HttpPoll();
|
||||
|
||||
void setAuth(const QString &user, const QString &pass="");
|
||||
void connectToUrl(const QString &url);
|
||||
void connectToHost(const QString &proxyHost, int proxyPort, const QString &url);
|
||||
|
||||
int pollInterval() const;
|
||||
void setPollInterval(int seconds);
|
||||
|
||||
// from ByteStream
|
||||
bool isOpen() const;
|
||||
void close();
|
||||
|
||||
signals:
|
||||
void connected();
|
||||
void syncStarted();
|
||||
void syncFinished();
|
||||
|
||||
protected:
|
||||
int tryWrite();
|
||||
|
||||
private slots:
|
||||
void http_result();
|
||||
void http_error(int);
|
||||
void do_sync();
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private *d;
|
||||
|
||||
void reset(bool clear=false);
|
||||
QByteArray makePacket(const QString &ident, const QString &key, const QString &newkey, const QByteArray &block);
|
||||
void resetKey();
|
||||
const QString & getKey(bool *);
|
||||
};
|
||||
|
||||
class HttpProxyPost : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Error { ErrConnectionRefused, ErrHostNotFound, ErrSocket, ErrProxyConnect, ErrProxyNeg, ErrProxyAuth };
|
||||
HttpProxyPost(QObject *parent=0);
|
||||
~HttpProxyPost();
|
||||
|
||||
void setAuth(const QString &user, const QString &pass="");
|
||||
bool isActive() const;
|
||||
void post(const QString &proxyHost, int proxyPort, const QString &url, const QByteArray &data, bool asProxy=true);
|
||||
void stop();
|
||||
QByteArray body() const;
|
||||
QString getHeader(const QString &) const;
|
||||
|
||||
signals:
|
||||
void result();
|
||||
void error(int);
|
||||
|
||||
private slots:
|
||||
void sock_connected();
|
||||
void sock_connectionClosed();
|
||||
void sock_readyRead();
|
||||
void sock_error(int);
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private *d;
|
||||
|
||||
void reset(bool clear=false);
|
||||
};
|
||||
|
||||
// CS_NAMESPACE_END
|
||||
|
||||
#endif
|
||||
1231
iris-legacy/cutestuff/network/socks.cpp
Normal file
1231
iris-legacy/cutestuff/network/socks.cpp
Normal file
File diff suppressed because it is too large
Load diff
160
iris-legacy/cutestuff/network/socks.h
Normal file
160
iris-legacy/cutestuff/network/socks.h
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
/*
|
||||
* socks.h - SOCKS5 TCP proxy client/server
|
||||
* Copyright (C) 2003 Justin Karneges
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CS_SOCKS_H
|
||||
#define CS_SOCKS_H
|
||||
|
||||
#include "bytestream.h"
|
||||
|
||||
// CS_NAMESPACE_BEGIN
|
||||
|
||||
class QHostAddress;
|
||||
class SocksClient;
|
||||
class SocksServer;
|
||||
|
||||
class SocksUDP : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
~SocksUDP();
|
||||
|
||||
void change(const QString &host, int port);
|
||||
void write(const QByteArray &data);
|
||||
|
||||
signals:
|
||||
void packetReady(const QByteArray &data);
|
||||
|
||||
private slots:
|
||||
void sn_activated(int);
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private *d;
|
||||
|
||||
friend class SocksClient;
|
||||
SocksUDP(SocksClient *sc, const QString &host, int port, const QHostAddress &routeAddr, int routePort);
|
||||
};
|
||||
|
||||
class SocksClient : public ByteStream
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Error { ErrConnectionRefused = ErrCustom, ErrHostNotFound, ErrProxyConnect, ErrProxyNeg, ErrProxyAuth };
|
||||
enum Method { AuthNone=0x0001, AuthUsername=0x0002 };
|
||||
enum Request { ReqConnect, ReqUDPAssociate };
|
||||
SocksClient(QObject *parent=0);
|
||||
SocksClient(int, QObject *parent=0);
|
||||
~SocksClient();
|
||||
|
||||
bool isIncoming() const;
|
||||
|
||||
// outgoing
|
||||
void setAuth(const QString &user, const QString &pass="");
|
||||
void connectToHost(const QString &proxyHost, int proxyPort, const QString &host, int port, bool udpMode=false);
|
||||
|
||||
// incoming
|
||||
void chooseMethod(int);
|
||||
void authGrant(bool);
|
||||
void requestDeny();
|
||||
void grantConnect();
|
||||
void grantUDPAssociate(const QString &relayHost, int relayPort);
|
||||
|
||||
// from ByteStream
|
||||
bool isOpen() const;
|
||||
void close();
|
||||
void write(const QByteArray &);
|
||||
QByteArray read(int bytes=0);
|
||||
int bytesAvailable() const;
|
||||
int bytesToWrite() const;
|
||||
|
||||
// remote address
|
||||
QHostAddress peerAddress() const;
|
||||
Q_UINT16 peerPort() const;
|
||||
|
||||
// udp
|
||||
QString udpAddress() const;
|
||||
Q_UINT16 udpPort() const;
|
||||
SocksUDP *createUDP(const QString &host, int port, const QHostAddress &routeAddr, int routePort);
|
||||
|
||||
signals:
|
||||
// outgoing
|
||||
void connected();
|
||||
|
||||
// incoming
|
||||
void incomingMethods(int);
|
||||
void incomingAuth(const QString &user, const QString &pass);
|
||||
void incomingConnectRequest(const QString &host, int port);
|
||||
void incomingUDPAssociateRequest();
|
||||
|
||||
private slots:
|
||||
void sock_connected();
|
||||
void sock_connectionClosed();
|
||||
void sock_delayedCloseFinished();
|
||||
void sock_readyRead();
|
||||
void sock_bytesWritten(int);
|
||||
void sock_error(int);
|
||||
void serve();
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private *d;
|
||||
|
||||
void init();
|
||||
void reset(bool clear=false);
|
||||
void do_request();
|
||||
void processOutgoing(const QByteArray &);
|
||||
void processIncoming(const QByteArray &);
|
||||
void continueIncoming();
|
||||
void writeData(const QByteArray &a);
|
||||
};
|
||||
|
||||
class SocksServer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SocksServer(QObject *parent=0);
|
||||
~SocksServer();
|
||||
|
||||
bool isActive() const;
|
||||
bool listen(Q_UINT16 port, bool udp=false);
|
||||
void stop();
|
||||
int port() const;
|
||||
QHostAddress address() const;
|
||||
SocksClient *takeIncoming();
|
||||
|
||||
void writeUDP(const QHostAddress &addr, int port, const QByteArray &data);
|
||||
|
||||
signals:
|
||||
void incomingReady();
|
||||
void incomingUDP(const QString &host, int port, const QHostAddress &addr, int sourcePort, const QByteArray &data);
|
||||
|
||||
private slots:
|
||||
void connectionReady(int);
|
||||
void connectionError();
|
||||
void sn_activated(int);
|
||||
|
||||
private:
|
||||
class Private;
|
||||
Private *d;
|
||||
};
|
||||
|
||||
// CS_NAMESPACE_END
|
||||
|
||||
#endif
|
||||
Reference in a new issue