Browse Source

Refactor HTTP query parsing

adaptive-webui-19844
Chocobo1 6 years ago
parent
commit
bd8d70bf60
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 21
      src/base/bittorrent/tracker.cpp
  2. 1
      src/base/bittorrent/tracker.h
  3. 26
      src/base/http/requestparser.cpp
  4. 2
      src/base/http/types.h
  5. 15
      src/webui/webapplication.cpp

21
src/base/bittorrent/tracker.cpp

@ -29,16 +29,11 @@
#include "tracker.h" #include "tracker.h"
#include <vector>
#include <libtorrent/bencode.hpp> #include <libtorrent/bencode.hpp>
#include <libtorrent/entry.hpp> #include <libtorrent/entry.hpp>
#include "base/global.h"
#include "base/http/server.h" #include "base/http/server.h"
#include "base/preferences.h" #include "base/preferences.h"
#include "base/utils/bytearray.h"
#include "base/utils/string.h"
// static limits // static limits
static const int MAX_TORRENTS = 100; static const int MAX_TORRENTS = 100;
@ -133,21 +128,7 @@ Http::Response Tracker::processRequest(const Http::Request &request, const Http:
void Tracker::respondToAnnounceRequest() void Tracker::respondToAnnounceRequest()
{ {
QMap<QString, QByteArray> queryParams; const QMap<QString, QByteArray> &queryParams = m_request.query;
// Parse GET parameters
using namespace Utils::ByteArray;
for (const QByteArray &param : asConst(splitToViews(m_request.query, "&"))) {
const int sepPos = param.indexOf('=');
if (sepPos <= 0) continue; // ignores params without name
const QByteArray nameComponent = midView(param, 0, sepPos);
const QByteArray valueComponent = midView(param, (sepPos + 1));
const QString paramName = QString::fromUtf8(QByteArray::fromPercentEncoding(nameComponent).replace('+', ' '));
const QByteArray paramValue = QByteArray::fromPercentEncoding(valueComponent).replace('+', ' ');
queryParams[paramName] = paramValue;
}
TrackerAnnounceRequest announceReq; TrackerAnnounceRequest announceReq;
// IP // IP

1
src/base/bittorrent/tracker.h

@ -36,7 +36,6 @@
#include "base/http/irequesthandler.h" #include "base/http/irequesthandler.h"
#include "base/http/responsebuilder.h" #include "base/http/responsebuilder.h"
#include "base/http/types.h"
namespace libtorrent namespace libtorrent
{ {

26
src/base/http/requestparser.cpp

@ -38,6 +38,7 @@
#include <QUrl> #include <QUrl>
#include <QUrlQuery> #include <QUrlQuery>
#include "base/global.h"
#include "base/utils/bytearray.h" #include "base/utils/bytearray.h"
#include "base/utils/string.h" #include "base/utils/string.h"
@ -182,14 +183,29 @@ bool RequestParser::parseRequestLine(const QString &line)
m_request.method = match.captured(1); m_request.method = match.captured(1);
// Request Target // Request Target
// URL components should be separated before percent-decoding
// [rfc3986] 2.4 When to Encode or Decode
const QByteArray url {match.captured(2).toLatin1()}; const QByteArray url {match.captured(2).toLatin1()};
const int sepPos = url.indexOf('?'); const int sepPos = url.indexOf('?');
const QByteArray pathComponent = ((sepPos == -1) ? url : Utils::ByteArray::midView(url, 0, sepPos)); const QByteArray pathComponent = ((sepPos == -1) ? url : midView(url, 0, sepPos));
m_request.path = QString::fromUtf8(QByteArray::fromPercentEncoding(pathComponent)); m_request.path = QString::fromUtf8(QByteArray::fromPercentEncoding(pathComponent));
if (sepPos >= 0)
m_request.query = url.mid(sepPos + 1); if (sepPos >= 0) {
const QByteArray query = midView(url, (sepPos + 1));
// [rfc3986] 2.4 When to Encode or Decode
// URL components should be separated before percent-decoding
for (const QByteArray &param : asConst(splitToViews(query, "&"))) {
const int eqCharPos = param.indexOf('=');
if (eqCharPos <= 0) continue; // ignores params without name
const QByteArray nameComponent = midView(param, 0, eqCharPos);
const QByteArray valueComponent = midView(param, (eqCharPos + 1));
const QString paramName = QString::fromUtf8(QByteArray::fromPercentEncoding(nameComponent).replace('+', ' '));
const QByteArray paramValue = QByteArray::fromPercentEncoding(valueComponent).replace('+', ' ');
m_request.query[paramName] = paramValue;
}
}
// HTTP-version // HTTP-version
m_request.version = match.captured(3); m_request.version = match.captured(3);

2
src/base/http/types.h

@ -97,8 +97,8 @@ namespace Http
QString version; QString version;
QString method; QString method;
QString path; QString path;
QByteArray query;
QStringMap headers; QStringMap headers;
QMap<QString, QByteArray> query;
QStringMap posts; QStringMap posts;
QVector<UploadedFile> files; QVector<UploadedFile> files;
}; };

15
src/webui/webapplication.cpp

@ -417,19 +417,8 @@ Http::Response WebApplication::processRequest(const Http::Request &request, cons
m_params.clear(); m_params.clear();
if (m_request.method == Http::METHOD_GET) { if (m_request.method == Http::METHOD_GET) {
// Parse GET parameters for (auto iter = m_request.query.cbegin(); iter != m_request.query.cend(); ++iter)
using namespace Utils::ByteArray; m_params[iter.key()] = QString::fromUtf8(iter.value());
for (const QByteArray &param : asConst(splitToViews(m_request.query, "&"))) {
const int sepPos = param.indexOf('=');
if (sepPos <= 0) continue; // ignores params without name
const QByteArray nameComponent = midView(param, 0, sepPos);
const QByteArray valueComponent = midView(param, (sepPos + 1));
const QString paramName = QString::fromUtf8(QByteArray::fromPercentEncoding(nameComponent).replace('+', ' '));
const QString paramValue = QString::fromUtf8(QByteArray::fromPercentEncoding(valueComponent).replace('+', ' '));
m_params[paramName] = paramValue;
}
} }
else { else {
m_params = m_request.posts; m_params = m_request.posts;

Loading…
Cancel
Save