Web UI code optimization and clean up

This commit is contained in:
Christophe Dumez
2011-09-25 17:26:02 +03:00
parent ab0355f8d6
commit 4385b941bb
6 changed files with 89 additions and 101 deletions

View File

@@ -91,7 +91,7 @@ void HttpConnection::read() {
m_generator.setStatusLine(400, "Bad Request");
write();
} else {
if (m_parser.isParsable())
if (!m_parser.isError())
respond();
}
}
@@ -129,7 +129,6 @@ void HttpConnection::translateDocument(QString& data) {
++context_index;
} while(translation == word && context_index < 15);
}
// Remove keyboard shortcuts
translation.replace(mnemonic, "");
@@ -153,7 +152,7 @@ void HttpConnection::respond() {
write();
return;
}
QString auth = m_parser.value("Authorization");
QString auth = m_parser.header().value("Authorization");
if(auth.isEmpty()) {
// Return unauthorized header
qDebug("Auth is Empty...");
@@ -164,7 +163,7 @@ void HttpConnection::respond() {
}
//qDebug("Auth: %s", qPrintable(auth.split(" ").first()));
if (QString::compare(auth.split(" ").first(), "Digest", Qt::CaseInsensitive) != 0
|| !m_httpserver->isAuthorized(auth.toLocal8Bit(), m_parser.method())) {
|| !m_httpserver->isAuthorized(auth.toLocal8Bit(), m_parser.header().method())) {
// Update failed attempt counter
m_httpserver->increaseNbFailedAttemptsForIp(peer_ip);
qDebug("client IP: %s (%d failed attempts)", qPrintable(peer_ip), nb_fail);
@@ -567,19 +566,16 @@ void HttpConnection::respondCommand(const QString& command) {
return;
}
if(command == "recheck"){
recheckTorrent(m_parser.post("hash"));
QBtSession::instance()->recheckTorrent(m_parser.post("hash"));
return;
}
}
void HttpConnection::recheckTorrent(const QString& hash) {
QBtSession::instance()->recheckTorrent(hash);
}
void HttpConnection::decreaseTorrentsPriority(const QStringList &hashes)
{
void HttpConnection::decreaseTorrentsPriority(const QStringList &hashes) {
qDebug() << Q_FUNC_INFO << hashes;
std::priority_queue<QPair<int, QTorrentHandle>, std::vector<QPair<int, QTorrentHandle> >, std::less<QPair<int, QTorrentHandle> > > torrent_queue;
std::priority_queue<QPair<int, QTorrentHandle>,
std::vector<QPair<int, QTorrentHandle> >,
std::less<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority
foreach(const QString &hash, hashes) {
try {
@@ -602,7 +598,9 @@ void HttpConnection::decreaseTorrentsPriority(const QStringList &hashes)
void HttpConnection::increaseTorrentsPriority(const QStringList &hashes)
{
qDebug() << Q_FUNC_INFO << hashes;
std::priority_queue<QPair<int, QTorrentHandle>, std::vector<QPair<int, QTorrentHandle> >, std::greater<QPair<int, QTorrentHandle> > > torrent_queue;
std::priority_queue<QPair<int, QTorrentHandle>,
std::vector<QPair<int, QTorrentHandle> >,
std::greater<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority
foreach(const QString &hash, hashes) {
try {

View File

@@ -65,7 +65,6 @@ protected slots:
void respondNotFound();
void processDownloadedFile(const QString& url, const QString& file_path);
void handleDownloadFailure(const QString& url, const QString& reason);
void recheckTorrent(const QString& hash);
void decreaseTorrentsPriority(const QStringList& hashes);
void increaseTorrentsPriority(const QStringList& hashes);

View File

@@ -33,8 +33,7 @@
#include <QUrl>
#include <QDebug>
HttpRequestParser::HttpRequestParser(): m_headerDone(false),
m_messageDone(false), m_error(false)
HttpRequestParser::HttpRequestParser(): m_error(false)
{
}
@@ -42,11 +41,6 @@ HttpRequestParser::~HttpRequestParser()
{
}
bool HttpRequestParser::isParsable() const {
return !m_error && m_headerDone && isValid()
&& (m_messageDone || !hasContentLength() || contentLength() == 0);
}
bool HttpRequestParser::isError() const {
return m_error;
}
@@ -56,71 +50,59 @@ QString HttpRequestParser::url() const {
}
QByteArray HttpRequestParser::message() const {
if (isParsable())
return m_data;
return QByteArray();
return m_data;
}
QString HttpRequestParser::get(const QString& key) const {
return m_getMap.value(key);
return m_getMap.value(key);
}
QString HttpRequestParser::post(const QString& key) const {
return m_postMap.value(key);
return m_postMap.value(key);
}
QByteArray HttpRequestParser::torrent() const {
return m_torrentContent;
}
void HttpRequestParser::write(QByteArray ba) {
// Parse header
while (!m_headerDone && !ba.isEmpty()) {
const int index = ba.indexOf('\n') + 1;
if(index == 0) {
m_data += ba;
ba.clear();
} else {
m_data += ba.left(index);
ba.remove(0, index);
if(m_data.right(4) == "\r\n\r\n") {
QHttpRequestHeader::operator=(QHttpRequestHeader(m_data));
m_headerDone = true;
m_data.clear();
QUrl url = QUrl::fromEncoded(QHttpRequestHeader::path().toAscii());
m_path = url.path();
void HttpRequestParser::write(const QByteArray& ba) {
int end_of_header = ba.indexOf("\r\n\r\n");
if (end_of_header < 0) {
qWarning() << "Could not find HTTP header: " << ba.constData();
m_error = true;
return;
}
QListIterator<QPair<QString, QString> > i(url.queryItems());
while (i.hasNext()) {
QPair<QString, QString> pair = i.next();
m_getMap[pair.first] = pair.second;
}
}
}
}
// Parse message
if(!m_messageDone && !ba.isEmpty()) {
if(hasContentLength()) {
m_data += ba;
if(m_data.size() >= (int) contentLength()) {
m_data.resize(contentLength());
m_messageDone = true;
//parse POST data
if(contentType() == "application/x-www-form-urlencoded") {
QUrl url;
url.setEncodedQuery(m_data);
QListIterator<QPair<QString, QString> > i(url.queryItems());
while (i.hasNext()) {
QPair<QString, QString> pair = i.next();
m_postMap[pair.first] = pair.second;
}
}
if(contentType() == "multipart/form-data") {
m_torrentContent = m_data.right(m_data.size()-m_data.indexOf("\r\n\r\n")-QByteArray("\r\n\r\n").size());
}
}
} else {
m_error = true;
// Parse header
m_header = QHttpRequestHeader(ba.left(end_of_header));
QUrl url = QUrl::fromEncoded(m_header.path().toAscii());
m_path = url.path();
// Parse GET parameters
QListIterator<QPair<QString, QString> > i(url.queryItems());
while (i.hasNext()) {
QPair<QString, QString> pair = i.next();
m_getMap[pair.first] = pair.second;
}
// Parse message content
if (m_header.hasContentLength()) {
m_data = ba.mid(end_of_header + 4, m_header.contentLength()); // +4 to skip "\r\n\r\n"
// Parse POST data
if(m_header.contentType() == "application/x-www-form-urlencoded") {
QUrl url;
url.setEncodedQuery(m_data);
QListIterator<QPair<QString, QString> > i(url.queryItems());
while (i.hasNext()) {
QPair<QString, QString> pair = i.next();
m_postMap[pair.first] = pair.second;
}
} else {
// Parse form data (torrent file)
if(m_header.contentType() == "multipart/form-data") {
m_torrentContent = m_data.right(m_data.size()-m_data.indexOf("\r\n\r\n")-QByteArray("\r\n\r\n").size());
}
}
}
}

View File

@@ -34,23 +34,22 @@
#include<QHttpRequestHeader>
class HttpRequestParser : public QHttpRequestHeader {
class HttpRequestParser {
public:
HttpRequestParser();
~HttpRequestParser();
bool isParsable() const;
bool isError() const;
QString url() const;
QByteArray message() const;
QString get(const QString& key) const;
QString post(const QString& key) const;
QByteArray torrent() const;
void write(QByteArray ba);
void write(const QByteArray& ba);
inline QHttpRequestHeader& header() { return m_header; }
private:
bool m_headerDone;
bool m_messageDone;
QHttpRequestHeader m_header;
bool m_error;
QByteArray m_data;
QString m_path;