Browse Source

Web UI code clean up

adaptive-webui-19844
Christophe Dumez 13 years ago
parent
commit
3f4947259b
  1. 63
      src/webui/httpconnection.cpp
  2. 26
      src/webui/httpconnection.h
  3. 28
      src/webui/httprequestparser.cpp
  4. 21
      src/webui/httpresponsegenerator.cpp
  5. 6
      src/webui/httpresponsegenerator.h

63
src/webui/httpconnection.cpp

@ -80,7 +80,7 @@ void HttpConnection::handleDownloadFailure(const QString& url, @@ -80,7 +80,7 @@ void HttpConnection::handleDownloadFailure(const QString& url,
}
void HttpConnection::read() {
QByteArray input = m_socket->readAll();
const QByteArray input = m_socket->readAll();
if(input.size() > 100000) {
qDebug("Request too big");
m_generator.setStatusLine(400, "Bad Request");
@ -88,24 +88,17 @@ void HttpConnection::read() { @@ -88,24 +88,17 @@ void HttpConnection::read() {
return;
}
m_parser.write(input);
if(m_parser.isError())
{
if(m_parser.isError()) {
m_generator.setStatusLine(400, "Bad Request");
write();
}
else
} else {
if (m_parser.isParsable())
respond();
}
}
void HttpConnection::write()
{
QByteArray output = m_generator.toByteArray();
/*qDebug(" --------");
qDebug("|RESPONSE|");
qDebug(" --------");
qDebug()<<output;*/
m_socket->write(output);
void HttpConnection::write() {
m_socket->write(m_generator.toByteArray());
m_socket->disconnectFromHost();
}
@ -118,7 +111,7 @@ void HttpConnection::translateDocument(QString& data) { @@ -118,7 +111,7 @@ void HttpConnection::translateDocument(QString& data) {
"options_imp", "Preferences", "TrackersAdditionDlg",
"ScanFoldersModel", "PropTabBar", "TorrentModel",
"downloadFromURL"};
int i=0;
int i = 0;
bool found;
do {
@ -149,8 +142,9 @@ void HttpConnection::translateDocument(QString& data) { @@ -149,8 +142,9 @@ void HttpConnection::translateDocument(QString& data) {
}
void HttpConnection::respond() {
if((m_socket->peerAddress() != QHostAddress::LocalHost && m_socket->peerAddress() != QHostAddress::LocalHostIPv6)
|| m_httpserver->isLocalAuthEnabled()) {
if((m_socket->peerAddress() != QHostAddress::LocalHost
&& m_socket->peerAddress() != QHostAddress::LocalHostIPv6)
|| m_httpserver->isLocalAuthEnabled()) {
// Authentication
const QString peer_ip = m_socket->peerAddress().toString();
const int nb_fail = m_httpserver->NbFailedAttemptsForIp(peer_ip);
@ -199,36 +193,35 @@ void HttpConnection::respond() { @@ -199,36 +193,35 @@ void HttpConnection::respond() {
}
return;
}
QStringList list = url.split('/', QString::SkipEmptyParts);
if (list.contains(".") || list.contains(".."))
{
if (list.contains(".") || list.contains("..")) {
respondNotFound();
return;
}
if (list.size() == 0)
if (list.isEmpty())
list.append("index.html");
if (list.size() >= 2)
{
if (list[0] == "json")
{
if (list[1] == "events")
{
if (list.size() >= 2) {
if (list[0] == "json") {
if (list[1] == "events") {
respondJson();
return;
}
if(list.size() > 2) {
if(list[1] == "propertiesGeneral") {
QString hash = list[2];
const QString& hash = list[2];
respondGenPropertiesJson(hash);
return;
}
if(list[1] == "propertiesTrackers") {
QString hash = list[2];
const QString& hash = list[2];
respondTrackersPropertiesJson(hash);
return;
}
if(list[1] == "propertiesFiles") {
QString hash = list[2];
const QString& hash = list[2];
respondFilesPropertiesJson(hash);
return;
}
@ -244,8 +237,7 @@ void HttpConnection::respond() { @@ -244,8 +237,7 @@ void HttpConnection::respond() {
}
}
}
if (list[0] == "command")
{
if (list[0] == "command") {
QString command = list[1];
respondCommand(command);
m_generator.setStatusLine(200, "OK");
@ -253,6 +245,7 @@ void HttpConnection::respond() { @@ -253,6 +245,7 @@ void HttpConnection::respond() {
return;
}
}
// Icons from theme
qDebug() << "list[0]" << list[0];
if(list[0] == "theme" && list.size() == 2) {
@ -318,7 +311,7 @@ void HttpConnection::respondJson() @@ -318,7 +311,7 @@ void HttpConnection::respondJson()
write();
}
void HttpConnection::respondGenPropertiesJson(QString hash) {
void HttpConnection::respondGenPropertiesJson(const QString& hash) {
EventManager* manager = m_httpserver->eventManager();
QString string = json::toJson(manager->getPropGeneralInfo(hash));
m_generator.setStatusLine(200, "OK");
@ -327,7 +320,7 @@ void HttpConnection::respondGenPropertiesJson(QString hash) { @@ -327,7 +320,7 @@ void HttpConnection::respondGenPropertiesJson(QString hash) {
write();
}
void HttpConnection::respondTrackersPropertiesJson(QString hash) {
void HttpConnection::respondTrackersPropertiesJson(const QString& hash) {
EventManager* manager = m_httpserver->eventManager();
QString string = json::toJson(manager->getPropTrackersInfo(hash));
m_generator.setStatusLine(200, "OK");
@ -336,7 +329,7 @@ void HttpConnection::respondTrackersPropertiesJson(QString hash) { @@ -336,7 +329,7 @@ void HttpConnection::respondTrackersPropertiesJson(QString hash) {
write();
}
void HttpConnection::respondFilesPropertiesJson(QString hash) {
void HttpConnection::respondFilesPropertiesJson(const QString& hash) {
EventManager* manager = m_httpserver->eventManager();
QString string = json::toJson(manager->getPropFilesInfo(hash));
m_generator.setStatusLine(200, "OK");
@ -366,7 +359,7 @@ void HttpConnection::respondGlobalTransferInfoJson() { @@ -366,7 +359,7 @@ void HttpConnection::respondGlobalTransferInfoJson() {
write();
}
void HttpConnection::respondCommand(QString command)
void HttpConnection::respondCommand(const QString& command)
{
if(command == "download")
{
@ -574,7 +567,7 @@ void HttpConnection::respondCommand(QString command) @@ -574,7 +567,7 @@ void HttpConnection::respondCommand(QString command)
}
}
void HttpConnection::recheckTorrent(QString hash) {
void HttpConnection::recheckTorrent(const QString& hash) {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if(h.is_valid()){
QBtSession::instance()->recheckTorrent(h.hash());

26
src/webui/httpconnection.h

@ -56,16 +56,16 @@ protected slots: @@ -56,16 +56,16 @@ protected slots:
void write();
void respond();
void respondJson();
void respondGenPropertiesJson(QString hash);
void respondTrackersPropertiesJson(QString hash);
void respondFilesPropertiesJson(QString hash);
void respondGenPropertiesJson(const QString& hash);
void respondTrackersPropertiesJson(const QString& hash);
void respondFilesPropertiesJson(const QString& hash);
void respondPreferencesJson();
void respondGlobalTransferInfoJson();
void respondCommand(QString command);
void respondCommand(const QString& command);
void respondNotFound();
void processDownloadedFile(const QString& url, const QString& file_path);
void handleDownloadFailure(const QString& url, const QString& reason);
void recheckTorrent(QString hash);
void recheckTorrent(const QString& hash);
void recheckAllTorrents();
void decreaseTorrentsPriority(const QStringList& hashes);
void increaseTorrentsPriority(const QStringList& hashes);
@ -74,14 +74,14 @@ private slots: @@ -74,14 +74,14 @@ private slots:
void read();
signals:
void UrlReadyToBeDownloaded(QString url);
void MagnetReadyToBeDownloaded(QString uri);
void torrentReadyToBeDownloaded(QString, bool, QString, bool);
void deleteTorrent(QString hash, bool permanently);
void resumeTorrent(QString hash);
void pauseTorrent(QString hash);
void increasePrioTorrent(QString hash);
void decreasePrioTorrent(QString hash);
void UrlReadyToBeDownloaded(const QString& url);
void MagnetReadyToBeDownloaded(const QString& uri);
void torrentReadyToBeDownloaded(const QString&, bool, const QString&, bool);
void deleteTorrent(const QString& hash, bool permanently);
void resumeTorrent(const QString& hash);
void pauseTorrent(const QString& hash);
void increasePrioTorrent(const QString& hash);
void decreasePrioTorrent(const QString& hash);
void resumeAllTorrents();
void pauseAllTorrents();

28
src/webui/httprequestparser.cpp

@ -42,46 +42,39 @@ HttpRequestParser::~HttpRequestParser() @@ -42,46 +42,39 @@ HttpRequestParser::~HttpRequestParser()
{
}
bool HttpRequestParser::isParsable() const
{
bool HttpRequestParser::isParsable() const {
return !m_error && m_headerDone && isValid()
&& (m_messageDone || !hasContentLength() || contentLength() == 0);
}
bool HttpRequestParser::isError() const
{
bool HttpRequestParser::isError() const {
return m_error;
}
QString HttpRequestParser::url() const
{
QString HttpRequestParser::url() const {
return m_path;
}
QByteArray HttpRequestParser::message() const
{
if(isParsable())
QByteArray HttpRequestParser::message() const {
if (isParsable())
return m_data;
return QByteArray();
}
QString HttpRequestParser::get(const QString& key) const
{
QString HttpRequestParser::get(const QString& key) const {
return m_getMap.value(key);
}
QString HttpRequestParser::post(const QString& key) const
{
QString HttpRequestParser::post(const QString& key) const {
return m_postMap.value(key);
}
QByteArray HttpRequestParser::torrent() const
{
QByteArray HttpRequestParser::torrent() const {
return m_torrentContent;
}
void HttpRequestParser::write(QByteArray ba)
{
void HttpRequestParser::write(QByteArray ba) {
// Parse header
while (!m_headerDone && !ba.isEmpty()) {
const int index = ba.indexOf('\n') + 1;
if(index == 0) {
@ -105,6 +98,7 @@ void HttpRequestParser::write(QByteArray ba) @@ -105,6 +98,7 @@ void HttpRequestParser::write(QByteArray ba)
}
}
}
// Parse message
if(!m_messageDone && !ba.isEmpty()) {
if(hasContentLength()) {
m_data += ba;

21
src/webui/httpresponsegenerator.cpp

@ -41,35 +41,28 @@ void HttpResponseGenerator::setMessage(const QString& message) { @@ -41,35 +41,28 @@ void HttpResponseGenerator::setMessage(const QString& message) {
setMessage(message.toUtf8());
}
void HttpResponseGenerator::stripMessage()
{
void HttpResponseGenerator::stripMessage() {
message.clear();
}
void HttpResponseGenerator::setContentTypeByExt(const QString ext)
{
if(ext == "css")
{
void HttpResponseGenerator::setContentTypeByExt(const QString& ext) {
if(ext == "css") {
setContentType("text/css");
return;
}
if(ext == "gif")
{
if(ext == "gif") {
setContentType("image/gif");
return;
}
if(ext == "htm" || ext == "html")
{
if(ext == "htm" || ext == "html") {
setContentType("text/html");
return;
}
if(ext == "js")
{
if(ext == "js") {
setContentType("text/javascript");
return;
}
if(ext == "png")
{
if(ext == "png") {
setContentType("image/x-png");
return;
}

6
src/webui/httpresponsegenerator.h

@ -41,8 +41,10 @@ class HttpResponseGenerator : public QHttpResponseHeader @@ -41,8 +41,10 @@ class HttpResponseGenerator : public QHttpResponseHeader
void setMessage(const QByteArray& message);
void setMessage(const QString& message);
void stripMessage();
void setContentTypeByExt(const QString ext);
inline QByteArray toByteArray() const { return QHttpResponseHeader::toString().toLocal8Bit() + message; }
void setContentTypeByExt(const QString& ext);
inline QByteArray toByteArray() const {
return QHttpResponseHeader::toString().toLocal8Bit() + message;
}
private:
QByteArray message;

Loading…
Cancel
Save