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

26
src/webui/httpconnection.h

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

28
src/webui/httprequestparser.cpp

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

21
src/webui/httpresponsegenerator.cpp

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

6
src/webui/httpresponsegenerator.h

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

Loading…
Cancel
Save