Follow project coding style. Issue #2192.

This commit is contained in:
buinsky 2014-12-07 23:33:36 +03:00
parent 513f71e000
commit dbc5ffee75

View file

@ -36,145 +36,137 @@
#include "abstractrequesthandler.h" #include "abstractrequesthandler.h"
AbstractRequestHandler::AbstractRequestHandler(const HttpRequest &request, const HttpEnvironment &env, WebApplication *app) AbstractRequestHandler::AbstractRequestHandler(const HttpRequest &request, const HttpEnvironment &env, WebApplication *app)
: app_(app), session_(0), request_(request), env_(env) : app_(app), session_(0), request_(request), env_(env)
{ {
sessionInitialize(); sessionInitialize();
if (!sessionActive() && !isAuthNeeded()) if (!sessionActive() && !isAuthNeeded())
sessionStart(); sessionStart();
} }
HttpResponse AbstractRequestHandler::run() HttpResponse AbstractRequestHandler::run()
{ {
response_ = HttpResponse(); response_ = HttpResponse();
if (isBanned()) if (isBanned()) {
{ status(403, "Forbidden");
status(403, "Forbidden"); print(QObject::tr("Your IP address has been banned after too many failed authentication attempts."), CONTENT_TYPE_TXT);
print(QObject::tr("Your IP address has been banned after too many failed authentication attempts."), CONTENT_TYPE_TXT); }
} else {
else processRequest();
{ }
processRequest();
}
return response_; return response_;
} }
bool AbstractRequestHandler::isAuthNeeded() bool AbstractRequestHandler::isAuthNeeded()
{ {
return return
( (
env_.clientAddress != QHostAddress::LocalHost && env_.clientAddress != QHostAddress::LocalHost &&
env_.clientAddress != QHostAddress::LocalHostIPv6 env_.clientAddress != QHostAddress::LocalHostIPv6
) || Preferences::instance()->isWebUiLocalAuthEnabled(); ) || Preferences::instance()->isWebUiLocalAuthEnabled();
} }
void AbstractRequestHandler::status(uint code, const QString& text) void AbstractRequestHandler::status(uint code, const QString& text)
{ {
response_.status = HttpResponseStatus(code, text); response_.status = HttpResponseStatus(code, text);
} }
void AbstractRequestHandler::header(const QString& name, const QString& value) void AbstractRequestHandler::header(const QString& name, const QString& value)
{ {
response_.headers[name] = value; response_.headers[name] = value;
} }
void AbstractRequestHandler::print(const QString& text, const QString& type) void AbstractRequestHandler::print(const QString& text, const QString& type)
{ {
print_impl(text.toUtf8(), type); print_impl(text.toUtf8(), type);
} }
void AbstractRequestHandler::print(const QByteArray& data, const QString& type) void AbstractRequestHandler::print(const QByteArray& data, const QString& type)
{ {
print_impl(data, type); print_impl(data, type);
} }
void AbstractRequestHandler::print_impl(const QByteArray& data, const QString& type) void AbstractRequestHandler::print_impl(const QByteArray& data, const QString& type)
{ {
if (!response_.headers.contains(HEADER_CONTENT_TYPE)) if (!response_.headers.contains(HEADER_CONTENT_TYPE))
response_.headers[HEADER_CONTENT_TYPE] = type; response_.headers[HEADER_CONTENT_TYPE] = type;
if (type.indexOf("image") > -1) if (type.indexOf("image") > -1)
response_.headers[HEADER_CACHE_CONTROL] = "max-age=3000000"; response_.headers[HEADER_CACHE_CONTROL] = "max-age=3000000";
response_.content += data; response_.content += data;
} }
void AbstractRequestHandler::printFile(const QString& path) void AbstractRequestHandler::printFile(const QString& path)
{ {
QByteArray data; QByteArray data;
QString type; QString type;
if (!app_->readFile(path, data, type)) if (!app_->readFile(path, data, type)) {
{ status(404, "Not Found");
status(404, "Not Found"); return;
return; }
}
print(data, type); print(data, type);
} }
void AbstractRequestHandler::sessionInitialize() void AbstractRequestHandler::sessionInitialize()
{ {
app_->sessionInitialize(this); app_->sessionInitialize(this);
} }
void AbstractRequestHandler::sessionStart() void AbstractRequestHandler::sessionStart()
{ {
if (app_->sessionStart(this)) if (app_->sessionStart(this)) {
{ QNetworkCookie cookie(C_SID.toUtf8(), session_->id.toUtf8());
QNetworkCookie cookie(C_SID.toUtf8(), session_->id.toUtf8()); cookie.setPath("/");
cookie.setPath("/"); header(HEADER_SET_COOKIE, cookie.toRawForm());
header(HEADER_SET_COOKIE, cookie.toRawForm()); }
}
} }
void AbstractRequestHandler::sessionEnd() void AbstractRequestHandler::sessionEnd()
{ {
if (sessionActive()) if (sessionActive()) {
{ QNetworkCookie cookie(C_SID.toUtf8(), session_->id.toUtf8());
QNetworkCookie cookie(C_SID.toUtf8(), session_->id.toUtf8()); cookie.setPath("/");
cookie.setPath("/"); cookie.setExpirationDate(QDateTime::currentDateTime());
cookie.setExpirationDate(QDateTime::currentDateTime());
if (app_->sessionEnd(this)) if (app_->sessionEnd(this))
{ header(HEADER_SET_COOKIE, cookie.toRawForm());
header(HEADER_SET_COOKIE, cookie.toRawForm());
} }
}
} }
bool AbstractRequestHandler::isBanned() const bool AbstractRequestHandler::isBanned() const
{ {
return app_->isBanned(this); return app_->isBanned(this);
} }
int AbstractRequestHandler::failedAttempts() const int AbstractRequestHandler::failedAttempts() const
{ {
return app_->failedAttempts(this); return app_->failedAttempts(this);
} }
void AbstractRequestHandler::resetFailedAttempts() void AbstractRequestHandler::resetFailedAttempts()
{ {
app_->resetFailedAttempts(this); app_->resetFailedAttempts(this);
} }
void AbstractRequestHandler::increaseFailedAttempts() void AbstractRequestHandler::increaseFailedAttempts()
{ {
app_->increaseFailedAttempts(this); app_->increaseFailedAttempts(this);
} }
QString AbstractRequestHandler::saveTmpFile(const QByteArray &data) QString AbstractRequestHandler::saveTmpFile(const QByteArray &data)
{ {
QTemporaryFile tmpfile(QDir::temp().absoluteFilePath("qBT-XXXXXX.torrent")); QTemporaryFile tmpfile(QDir::temp().absoluteFilePath("qBT-XXXXXX.torrent"));
tmpfile.setAutoRemove(false); tmpfile.setAutoRemove(false);
if (tmpfile.open()) if (tmpfile.open()) {
{ tmpfile.write(data);
tmpfile.write(data); tmpfile.close();
tmpfile.close(); return tmpfile.fileName();
return tmpfile.fileName(); }
}
qWarning() << "I/O Error: Could not create temporary file"; qWarning() << "I/O Error: Could not create temporary file";
return QString(); return QString();
} }