Browse Source

Modify Http parser/generator classes.

adaptive-webui-19844
Vladimir Golovnev (Glassez) 10 years ago
parent
commit
9853a9fec9
  1. 161
      src/webui/httpheader.cpp
  2. 75
      src/webui/httpheader.h
  3. 112
      src/webui/httprequestheader.cpp
  4. 59
      src/webui/httprequestheader.h
  5. 344
      src/webui/httprequestparser.cpp
  6. 52
      src/webui/httprequestparser.h
  7. 103
      src/webui/httpresponsegenerator.cpp
  8. 23
      src/webui/httpresponsegenerator.h
  9. 131
      src/webui/httpresponseheader.cpp
  10. 56
      src/webui/httpresponseheader.h
  11. 89
      src/webui/httptypes.h

161
src/webui/httpheader.cpp

@ -1,161 +0,0 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2014 sledgehammer999
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : hammered999@gmail.com
*/
#include "httpheader.h"
HttpHeader::HttpHeader() : m_valid(true) {}
HttpHeader::HttpHeader(const QString &str) : m_valid(true) {
parse(str);
}
HttpHeader::~HttpHeader() {}
void HttpHeader::addValue(const QString &key, const QString &value) {
m_headers.insert(key.toLower(), value.trimmed());
}
QStringList HttpHeader::allValues(const QString &key) const {
QStringList list = m_headers.values(key);
return list;
}
uint HttpHeader::contentLength() const {
QString lenVal = m_headers.value("content-length");
return lenVal.toUInt();
}
QString HttpHeader::contentType() const {
QString str = m_headers.value("content-type");
// content-type might have a parameter so we need to strip it.
// eg. application/x-www-form-urlencoded; charset=utf-8
int index = str.indexOf(';');
if (index == -1)
return str;
else
return str.left(index);
}
bool HttpHeader::hasContentLength() const {
return m_headers.contains("content-length");
}
bool HttpHeader::hasContentType() const {
return m_headers.contains("content-type");
}
bool HttpHeader::hasKey(const QString &key) const {
return m_headers.contains(key.toLower());
}
bool HttpHeader::isValid() const {
return m_valid;
}
QStringList HttpHeader::keys() const {
QStringList list = m_headers.keys();
return list;
}
void HttpHeader::removeAllValues(const QString &key) {
m_headers.remove(key);
}
void HttpHeader::removeValue(const QString &key) {
m_headers.remove(key);
}
void HttpHeader::setContentLength(int len) {
m_headers.replace("content-length", QString::number(len));
}
void HttpHeader::setContentType(const QString &type) {
m_headers.replace("content-type", type.trimmed());
}
void HttpHeader::setValue(const QString &key, const QString &value) {
m_headers.replace(key.toLower(), value.trimmed());
}
void HttpHeader::setValues(const QList<QPair<QString, QString> > &values) {
for (int i=0; i < values.size(); ++i) {
setValue(values[i].first, values[i].second);
}
}
QString HttpHeader::toString() const {
QString str;
typedef QMultiHash<QString, QString>::const_iterator h_it;
for (h_it it = m_headers.begin(), itend = m_headers.end();
it != itend; ++it) {
str = str + it.key() + ": " + it.value() + "\r\n";
}
str += "\r\n";
return str;
}
QString HttpHeader::value(const QString &key) const {
return m_headers.value(key.toLower());
}
QList<QPair<QString, QString> > HttpHeader::values() const {
QList<QPair<QString, QString> > list;
typedef QMultiHash<QString, QString>::const_iterator h_it;
for (h_it it = m_headers.begin(), itend = m_headers.end();
it != itend; ++it) {
list.append(qMakePair(it.key(), it.value()));
}
return list;
}
void HttpHeader::parse(const QString &str) {
QStringList headers = str.split("\r\n", QString::SkipEmptyParts, Qt::CaseInsensitive);
for (int i=0; i < headers.size(); ++i) {
int index = headers[i].indexOf(':', Qt::CaseInsensitive);
if (index == -1) {
setValid(false);
break;
}
QString key = headers[i].left(index);
QString value = headers[i].right(headers[i].size() - index - 1);
m_headers.insert(key.toLower(), value.trimmed());
}
}
void HttpHeader::setValid(bool valid) {
m_valid = valid;
if (!m_valid)
m_headers.clear();
}

75
src/webui/httpheader.h

@ -1,75 +0,0 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2014 sledgehammer999
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : hammered999@gmail.com
*/
#ifndef HTTPHEADER_H
#define HTTPHEADER_H
#include <QString>
#include <QStringList>
#include <QMultiHash>
#include <QPair>
class HttpHeader {
public:
HttpHeader();
HttpHeader(const QString &str);
virtual ~HttpHeader();
void addValue(const QString &key, const QString &value);
QStringList allValues(const QString &key) const;
uint contentLength() const;
QString contentType() const;
bool hasContentLength() const;
bool hasContentType() const;
bool hasKey(const QString &key) const;
bool isValid() const;
QStringList keys() const;
virtual int majorVersion() const =0;
virtual int minorVersion() const =0;
void removeAllValues(const QString &key);
void removeValue(const QString &key);
void setContentLength(int len);
void setContentType(const QString &type);
void setValue(const QString &key, const QString &value);
void setValues(const QList<QPair<QString, QString> > &values);
virtual QString toString() const;
QString value(const QString &key) const;
QList<QPair<QString, QString> > values() const;
protected:
void parse(const QString &str);
void setValid(bool valid = true);
private:
QMultiHash<QString, QString> m_headers;
bool m_valid;
};
#endif // HTTPHEADER_H

112
src/webui/httprequestheader.cpp

@ -1,112 +0,0 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2014 sledgehammer999
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : hammered999@gmail.com
*/
#include "httprequestheader.h"
HttpRequestHeader::HttpRequestHeader() : HttpHeader(), m_majorVersion(1), m_minorVersion(1) {}
HttpRequestHeader::HttpRequestHeader(const QString &method, const QString &path, int majorVer, int minorVer) :
HttpHeader(), m_method(method), m_path(path), m_majorVersion(majorVer), m_minorVersion(minorVer) {}
HttpRequestHeader::HttpRequestHeader(const QString &str): HttpHeader() {
int line = str.indexOf("\r\n", 0, Qt::CaseInsensitive);
QString req = str.left(line);
QString headers = str.right(str.size() - line - 2); //"\r\n" == 2
QStringList method = req.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive);
if (method.size() != 3)
setValid(false);
else {
m_method = method[0];
m_path = method[1];
if (parseVersions(method[2]))
parse(headers);
else
setValid(false);
}
}
int HttpRequestHeader::majorVersion() const {
return m_majorVersion;
}
int HttpRequestHeader::minorVersion() const {
return m_minorVersion;
}
QString HttpRequestHeader::method() const {
return m_method;
}
QString HttpRequestHeader::path() const {
return m_path;
}
void HttpRequestHeader::setRequest(const QString &method, const QString &path, int majorVer, int minorVer) {
m_method = method;
m_path = path;
m_majorVersion = majorVer;
m_minorVersion = minorVer;
}
QString HttpRequestHeader::toString() const {
QString str = m_method + " ";
str+= m_path + " ";
str+= "HTTP/" + QString::number(m_majorVersion) + "." + QString::number(m_minorVersion) + "\r\n";
str += HttpHeader::toString();
return str;
}
bool HttpRequestHeader::parseVersions(const QString &str) {
if (str.size() <= 5) // HTTP/ which means version missing
return false;
if (str.left(4) != "HTTP")
return false;
QString versions = str.right(str.size() - 5); // Strip "HTTP/"
int decPoint = versions.indexOf('.');
if (decPoint == -1)
return false;
bool ok;
m_majorVersion = versions.left(decPoint).toInt(&ok);
if (!ok)
return false;
m_minorVersion = versions.right(versions.size() - decPoint - 1).toInt(&ok);
if (!ok)
return false;
return true;
}

59
src/webui/httprequestheader.h

@ -1,59 +0,0 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2014 sledgehammer999
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : hammered999@gmail.com
*/
#ifndef HTTPREQUESTHEADER_H
#define HTTPREQUESTHEADER_H
#include "httpheader.h"
class HttpRequestHeader: public HttpHeader {
public:
HttpRequestHeader();
// The path argument must be properly encoded for an HTTP request.
HttpRequestHeader(const QString &method, const QString &path, int majorVer = 1, int minorVer = 1);
HttpRequestHeader(const QString &str);
virtual int majorVersion() const;
virtual int minorVersion() const;
QString method() const;
// This is the raw path from the header. No decoding/encoding is performed.
QString path() const;
// The path argument must be properly encoded for an HTTP request.
void setRequest(const QString &method, const QString &path, int majorVer = 1, int minorVer = 1);
virtual QString toString() const;
private:
bool parseVersions(const QString &str);
QString m_method;
QString m_path;
int m_majorVersion;
int m_minorVersion;
};
#endif // HTTPREQUESTHEADER_H

344
src/webui/httprequestparser.cpp

@ -1,5 +1,6 @@
/* /*
* Bittorrent Client using Qt4 and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Ishan Arora and Christophe Dumez * Copyright (C) 2006 Ishan Arora and Christophe Dumez
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -24,110 +25,231 @@
* modify file(s), you may extend this exception to your version of the file(s), * modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this * but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version. * exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/ */
#include <QStringList>
#include "httprequestparser.h"
#include <QUrl> #include <QUrl>
//#include <QVariant>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
#include <QUrlQuery> #include <QUrlQuery>
#endif #endif
#include <QDir>
#include <QTemporaryFile>
#include <QDebug> #include <QDebug>
#include "httprequestparser.h"
HttpRequestParser::HttpRequestParser(): m_error(false) const QByteArray EOL("\r\n");
const QByteArray EOH("\r\n\r\n");
inline QString unquoted(const QString& str)
{ {
if ((str[0] == '\"') && (str[str.length() - 1] == '\"'))
return str.mid(1, str.length() - 2);
return str;
} }
HttpRequestParser::~HttpRequestParser() HttpRequestParser::ErrorCode HttpRequestParser::parse(const QByteArray& data, HttpRequest& request, uint maxContentLength)
{ {
return HttpRequestParser(maxContentLength).parseHttpRequest(data, request);
} }
bool HttpRequestParser::isError() const { HttpRequestParser::HttpRequestParser(uint maxContentLength)
return m_error; : maxContentLength_(maxContentLength)
{
} }
const QString& HttpRequestParser::url() const { HttpRequestParser::ErrorCode HttpRequestParser::parseHttpRequest(const QByteArray& data, HttpRequest& request)
return m_path; {
request_ = HttpRequest();
// Parse HTTP request header
const int header_end = data.indexOf(EOH);
if (header_end < 0)
{
qDebug() << Q_FUNC_INFO << "incomplete request";
return IncompleteRequest;
} }
const QByteArray& HttpRequestParser::message() const { if (!parseHttpHeader(data.left(header_end)))
return m_data; {
qWarning() << Q_FUNC_INFO << "header parsing error";
return BadRequest;
} }
QString HttpRequestParser::get(const QString& key) const { // Parse HTTP request message
return m_getMap.value(key); int content_length = 0;
if (request_.headers.contains("content-length"))
{
content_length = request_.headers["content-length"].toInt();
if (content_length > static_cast<int>(maxContentLength_))
{
qWarning() << Q_FUNC_INFO << "bad request: message too long";
return BadRequest;
} }
QString HttpRequestParser::post(const QString& key) const { QByteArray content = data.mid(header_end + EOH.length(), content_length);
return m_postMap.value(key); if (content.length() < content_length)
{
qDebug() << Q_FUNC_INFO << "incomplete request";
return IncompleteRequest;
} }
const QList<QByteArray>& HttpRequestParser::torrents() const { if (!parseContent(content))
return m_torrents; {
qWarning() << Q_FUNC_INFO << "message parsing error";
return BadRequest;
}
} }
void HttpRequestParser::writeHeader(const QByteArray& ba) { // qDebug() << Q_FUNC_INFO;
m_error = false; // qDebug() << "HTTP Request header:";
// Parse header // qDebug() << data.left(header_end) << "\n";
m_header = HttpRequestHeader(ba);
QUrl url = QUrl::fromEncoded(m_header.path().toLatin1()); request = request_;
m_path = url.path(); return NoError;
}
bool HttpRequestParser::parseStartingLine(const QString &line)
{
const QRegExp rx("^([A-Z]+)\\s+(\\S+)\\s+HTTP/\\d\\.\\d$");
if (rx.indexIn(line.trimmed()) >= 0)
{
request_.method = rx.cap(1);
QUrl url = QUrl::fromEncoded(rx.cap(2).toLatin1());
request_.path = url.path(); // Path
// Parse GET parameters // Parse GET parameters
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
QUrlQuery query(url);
QListIterator<QPair<QString, QString> > i(query.queryItems());
#else
QListIterator<QPair<QString, QString> > i(url.queryItems()); QListIterator<QPair<QString, QString> > i(url.queryItems());
#else
QListIterator<QPair<QString, QString> > i(QUrlQuery(url).queryItems());
#endif #endif
while (i.hasNext()) { while (i.hasNext())
{
QPair<QString, QString> pair = i.next(); QPair<QString, QString> pair = i.next();
m_getMap[pair.first] = pair.second; request_.gets[pair.first] = pair.second;
}
return true;
}
qWarning() << Q_FUNC_INFO << "invalid http header:" << line;
return false;
}
bool HttpRequestParser::parseHeaderLine(const QString &line, QPair<QString, QString>& out)
{
int i = line.indexOf(QLatin1Char(':'));
if (i == -1)
{
qWarning() << Q_FUNC_INFO << "invalid http header:" << line;
return false;
}
out = qMakePair(line.left(i).trimmed().toLower(), line.mid(i + 1).trimmed());
return true;
}
bool HttpRequestParser::parseHttpHeader(const QByteArray &data)
{
QString str = QString::fromUtf8(data);
QStringList lines = str.trimmed().split(EOL);
QStringList headerLines;
foreach (const QString& line, lines)
{
if (line[0].isSpace()) // header line continuation
{
if (!headerLines.isEmpty()) // really continuation
{
headerLines.last() += QLatin1Char(' ');
headerLines.last() += line.trimmed();
}
}
else
{
headerLines.append(line);
}
}
if (headerLines.isEmpty())
return false; // Empty header
QStringList::Iterator it = headerLines.begin();
if (!parseStartingLine(*it))
return false;
++it;
for (; it != headerLines.end(); ++it)
{
QPair<QString, QString> header;
if (!parseHeaderLine(*it, header))
return false;
request_.headers[header.first] = header.second;
} }
return true;
} }
static QList<QByteArray> splitRawData(QByteArray rawData, const QByteArray& sep) QList<QByteArray> HttpRequestParser::splitMultipartData(const QByteArray& data, const QByteArray& boundary)
{ {
QList<QByteArray> ret; QList<QByteArray> ret;
QByteArray sep = boundary + EOL;
const int sepLength = sep.size(); const int sepLength = sep.size();
int index = 0;
while ((index = rawData.indexOf(sep)) >= 0) { int start = 0, end = 0;
ret << rawData.left(index); if ((end = data.indexOf(sep, start)) >= 0)
rawData = rawData.mid(index + sepLength); {
start = end + sepLength; // skip first boundary
while ((end = data.indexOf(sep, start)) >= 0)
{
ret << data.mid(start, end - start);
start = end + sepLength;
}
// last or single part
sep = boundary + "--" + EOL;
if ((end = data.indexOf(sep, start)) >= 0)
ret << data.mid(start, end - start);
} }
return ret; return ret;
} }
void HttpRequestParser::writeMessage(const QByteArray& ba) { bool HttpRequestParser::parseContent(const QByteArray& data)
{
// Parse message content // Parse message content
Q_ASSERT (m_header.hasContentLength()); qDebug() << Q_FUNC_INFO << "Content-Length: " << request_.headers["content-length"];
m_error = false; qDebug() << Q_FUNC_INFO << "data.size(): " << data.size();
m_data = ba;
qDebug() << Q_FUNC_INFO << "m_data.size(): " << m_data.size();
// Parse POST data // Parse url-encoded POST data
if (m_header.contentType() == "application/x-www-form-urlencoded") { if (request_.headers["content-type"].startsWith("application/x-www-form-urlencoded"))
{
QUrl url; QUrl url;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
QString tmp(m_data); url.setEncodedQuery(data);
QUrlQuery query(tmp);
QListIterator<QPair<QString, QString> > i(query.queryItems(QUrl::FullyDecoded));
#else
url.setEncodedQuery(m_data);
QListIterator<QPair<QString, QString> > i(url.queryItems()); QListIterator<QPair<QString, QString> > i(url.queryItems());
#else
url.setQuery(data);
QListIterator<QPair<QString, QString> > i(QUrlQuery(url).queryItems(QUrl::FullyDecoded));
#endif #endif
while (i.hasNext()) { while (i.hasNext())
{
QPair<QString, QString> pair = i.next(); QPair<QString, QString> pair = i.next();
m_postMap[pair.first] = pair.second; request_.posts[pair.first] = pair.second;
} }
return;
return true;
} }
// Parse multipart/form data (torrent file) // Parse multipart/form data (torrent file)
/** /**
m_data has the following format (if boundary is "cH2ae0GI3KM7GI3Ij5ae0ei4Ij5Ij5") data has the following format (if boundary is "cH2ae0GI3KM7GI3Ij5ae0ei4Ij5Ij5")
--cH2ae0GI3KM7GI3Ij5ae0ei4Ij5Ij5 --cH2ae0GI3KM7GI3Ij5ae0ei4Ij5Ij5
Content-Disposition: form-data; name=\"Filename\" Content-Disposition: form-data; name=\"Filename\"
@ -144,58 +266,108 @@ Content-Disposition: form-data; name=\"Upload\"
Submit Query Submit Query
--cH2ae0GI3KM7GI3Ij5ae0ei4Ij5Ij5-- --cH2ae0GI3KM7GI3Ij5ae0ei4Ij5Ij5--
**/ **/
if (m_header.contentType().startsWith("multipart/form-data")) { QString content_type = request_.headers["content-type"];
qDebug() << Q_FUNC_INFO << "header is: " << m_header.toString(); if (content_type.startsWith("multipart/form-data"))
static QRegExp boundaryRegexQuoted("boundary=\"([ \\w'()+,-\\./:=\\?]+)\""); {
static QRegExp boundaryRegexNotQuoted("boundary=([\\w'()+,-\\./:=\\?]+)"); const QRegExp boundaryRegexQuoted("boundary=\"([ \\w'()+,-\\./:=\\?]+)\"");
const QRegExp boundaryRegexNotQuoted("boundary=([\\w'()+,-\\./:=\\?]+)");
QByteArray boundary; QByteArray boundary;
if (boundaryRegexQuoted.indexIn(m_header.toString()) < 0) { if (boundaryRegexQuoted.indexIn(content_type) < 0)
if (boundaryRegexNotQuoted.indexIn(m_header.toString()) < 0) { {
if (boundaryRegexNotQuoted.indexIn(content_type) < 0)
{
qWarning() << "Could not find boundary in multipart/form-data header!"; qWarning() << "Could not find boundary in multipart/form-data header!";
m_error = true; return false;
return; }
} else { else
{
boundary = "--" + boundaryRegexNotQuoted.cap(1).toLatin1(); boundary = "--" + boundaryRegexNotQuoted.cap(1).toLatin1();
} }
} else { }
else
{
boundary = "--" + boundaryRegexQuoted.cap(1).toLatin1(); boundary = "--" + boundaryRegexQuoted.cap(1).toLatin1();
} }
qDebug() << "Boundary is " << boundary; qDebug() << "Boundary is " << boundary;
QList<QByteArray> parts = splitRawData(m_data, boundary); QList<QByteArray> parts = splitMultipartData(data, boundary);
qDebug() << parts.size() << "parts in data"; qDebug() << parts.size() << "parts in data";
foreach (const QByteArray& part, parts) {
const int filenameIndex = part.indexOf("filename="); foreach (const QByteArray& part, parts)
if (filenameIndex < 0) {
continue; if (!parseFormData(part))
qDebug() << "Found a torrent"; return false;
m_torrents << part.mid(part.indexOf("\r\n\r\n", filenameIndex + 9) + 4);
} }
return true;
} }
qWarning() << Q_FUNC_INFO << "unknown content type:" << qPrintable(content_type);
return false;
} }
bool HttpRequestParser::acceptsEncoding() { bool HttpRequestParser::parseFormData(const QByteArray& data)
QString encoding = m_header.value("Accept-Encoding"); {
// Parse form data header
const int header_end = data.indexOf(EOH);
if (header_end < 0)
{
qDebug() << "Invalid form data: \n" << data;
return false;
}
int pos = encoding.indexOf("gzip", 0, Qt::CaseInsensitive); QString header_str = QString::fromUtf8(data.left(header_end));
if (pos == -1) QStringList lines = header_str.trimmed().split(EOL);
QStringMap headers;
foreach (const QString& line, lines)
{
QPair<QString, QString> header;
if (!parseHeaderLine(line, header))
return false; return false;
// Let's see if there's a qvalue of 0.0 following headers[header.first] = header.second;
if (encoding[pos+4] != ';') //there isn't, so it accepts gzip anyway }
return true;
QStringMap disposition;
if (!headers.contains("content-disposition") ||
!parseHeaderValue(headers["content-disposition"], disposition) ||
!disposition.contains("name"))
{
qDebug() << "Invalid form data header: \n" << header_str;
return false;
}
//So let's find = and the next comma if (disposition.contains("filename"))
pos = encoding.indexOf("=", pos+4, Qt::CaseInsensitive); {
int comma_pos = encoding.indexOf(",", pos, Qt::CaseInsensitive); UploadedFile ufile;
ufile.filename = disposition["filename"];
ufile.type = disposition["content-type"];
ufile.data = data.mid(header_end + EOH.length());
QString value; request_.files[disposition["name"]] = ufile;
if (comma_pos == -1) }
value = encoding.mid(pos+1, comma_pos);
else else
value = encoding.mid(pos+1, comma_pos-(pos+1)); {
request_.posts[disposition["name"]] = QString::fromUtf8(data.mid(header_end + EOH.length()));
}
return true;
}
if (value.toDouble() == 0.0) bool HttpRequestParser::parseHeaderValue(const QString& value, QStringMap& out)
{
QStringList items = value.split(QLatin1Char(';'));
out[""] = items[0];
for (QStringList::size_type i = 1; i < items.size(); ++i)
{
int pos = items[i].indexOf("=");
if (pos < 0)
return false; return false;
else
out[items[i].left(pos).trimmed()] = unquoted(items[i].mid(pos + 1).trimmed());
}
return true; return true;
} }

52
src/webui/httprequestparser.h

@ -1,5 +1,6 @@
/* /*
* Bittorrent Client using Qt4 and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Ishan Arora and Christophe Dumez * Copyright (C) 2006 Ishan Arora and Christophe Dumez
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -24,41 +25,38 @@
* modify file(s), you may extend this exception to your version of the file(s), * modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this * but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version. * exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/ */
#ifndef HTTPREQUESTPARSER_H #ifndef HTTPREQUESTPARSER_H
#define HTTPREQUESTPARSER_H #define HTTPREQUESTPARSER_H
#include <QHash> #include "httptypes.h"
#include "httprequestheader.h"
class HttpRequestParser {
class HttpRequestParser
{
public: public:
HttpRequestParser(); enum ErrorCode { NoError = 0, IncompleteRequest, BadRequest };
~HttpRequestParser();
bool isError() const; // when result != NoError parsed request is undefined
const QString& url() const; // Warning! Header names are converted to lower-case.
const QByteArray& message() const; static ErrorCode parse(const QByteArray& data, HttpRequest& request, uint maxContentLength = 10000000 /* ~10MB */);
QString get(const QString& key) const;
QString post(const QString& key) const;
const QList<QByteArray>& torrents() const;
void writeHeader(const QByteArray& ba);
void writeMessage(const QByteArray& ba);
bool acceptsEncoding();
inline const HttpRequestHeader& header() const { return m_header; }
private: private:
HttpRequestHeader m_header; HttpRequestParser(uint maxContentLength);
bool m_error;
QByteArray m_data; ErrorCode parseHttpRequest(const QByteArray& data, HttpRequest& request);
QString m_path;
QHash<QString, QString> m_postMap; bool parseHttpHeader(const QByteArray& data);
QHash<QString, QString> m_getMap; bool parseStartingLine(const QString &line);
QList<QByteArray> m_torrents; bool parseContent(const QByteArray& data);
bool parseFormData(const QByteArray& data);
QList<QByteArray> splitMultipartData(const QByteArray& data, const QByteArray& boundary);
static bool parseHeaderLine(const QString& line, QPair<QString, QString>& out);
static bool parseHeaderValue(const QString& value, QStringMap& out);
const uint maxContentLength_;
HttpRequest request_;
}; };
#endif #endif

103
src/webui/httpresponsegenerator.cpp

@ -1,5 +1,6 @@
/* /*
* Bittorrent Client using Qt4 and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Ishan Arora and Christophe Dumez * Copyright (C) 2006 Ishan Arora and Christophe Dumez
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -24,48 +25,52 @@
* modify file(s), you may extend this exception to your version of the file(s), * modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this * but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version. * exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/ */
#include "httpresponsegenerator.h"
#include <zlib.h> #include <zlib.h>
#include "httpresponsegenerator.h"
void HttpResponseGenerator::setMessage(const QByteArray& message) bool gCompress(QByteArray data, QByteArray& dest_buffer);
{
m_message = message;
}
void HttpResponseGenerator::setMessage(const QString& message) QByteArray HttpResponseGenerator::generate(HttpResponse response)
{ {
setMessage(message.toUtf8()); if (response.headers[HEADER_CONTENT_ENCODING] == "gzip")
} {
// A gzip seems to have 23 bytes overhead.
void HttpResponseGenerator::setContentTypeByExt(const QString& ext) { // Also "Content-Encoding: gzip\r\n" is 26 bytes long
if (ext == "css") { // So we only benefit from gzip if the message is bigger than 23+26 = 49
setContentType("text/css"); // If the message is smaller than 49 bytes we actually send MORE data if we gzip
return; QByteArray dest_buf;
} if ((response.content.size() > 49) && (gCompress(response.content, dest_buf)))
if (ext == "gif") { {
setContentType("image/gif"); response.content = dest_buf;
return;
}
if (ext == "htm" || ext == "html") {
setContentType("text/html");
return;
} }
if (ext == "js") { else
setContentType("text/javascript"); {
return; response.headers.remove(HEADER_CONTENT_ENCODING);
} }
if (ext == "png") {
setContentType("image/png");
return;
} }
if (response.content.length() > 0)
response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length());
QString ret(QLatin1String("HTTP/1.1 %1 %2\r\n%3\r\n"));
QString header;
foreach (const QString& key, response.headers.keys())
header += QString("%1: %2\r\n").arg(key).arg(response.headers[key]);
ret = ret.arg(response.status.code).arg(response.status.text).arg(header);
// qDebug() << Q_FUNC_INFO;
// qDebug() << "HTTP Response header:";
// qDebug() << ret;
return ret.toUtf8() + response.content;
} }
bool HttpResponseGenerator::gCompress(QByteArray &dest_buffer) { bool gCompress(QByteArray data, QByteArray& dest_buffer)
{
static const int BUFSIZE = 128 * 1024; static const int BUFSIZE = 128 * 1024;
char tmp_buf[BUFSIZE]; char tmp_buf[BUFSIZE];
int ret; int ret;
@ -74,8 +79,8 @@ bool HttpResponseGenerator::gCompress(QByteArray &dest_buffer) {
strm.zalloc = Z_NULL; strm.zalloc = Z_NULL;
strm.zfree = Z_NULL; strm.zfree = Z_NULL;
strm.opaque = Z_NULL; strm.opaque = Z_NULL;
strm.next_in = reinterpret_cast<unsigned char*>(m_message.data()); strm.next_in = reinterpret_cast<unsigned char*>(data.data());
strm.avail_in = m_message.length(); strm.avail_in = data.length();
strm.next_out = reinterpret_cast<unsigned char*>(tmp_buf); strm.next_out = reinterpret_cast<unsigned char*>(tmp_buf);
strm.avail_out = BUFSIZE; strm.avail_out = BUFSIZE;
@ -92,6 +97,7 @@ bool HttpResponseGenerator::gCompress(QByteArray &dest_buffer) {
ret = deflate(&strm, Z_NO_FLUSH); ret = deflate(&strm, Z_NO_FLUSH);
if (ret != Z_OK) if (ret != Z_OK)
return false; return false;
if (strm.avail_out == 0) if (strm.avail_out == 0)
{ {
dest_buffer.append(tmp_buf, BUFSIZE); dest_buffer.append(tmp_buf, BUFSIZE);
@ -101,40 +107,23 @@ bool HttpResponseGenerator::gCompress(QByteArray &dest_buffer) {
} }
int deflate_res = Z_OK; int deflate_res = Z_OK;
while (deflate_res == Z_OK) { while (deflate_res == Z_OK)
if (strm.avail_out == 0) { {
if (strm.avail_out == 0)
{
dest_buffer.append(tmp_buf, BUFSIZE); dest_buffer.append(tmp_buf, BUFSIZE);
strm.next_out = reinterpret_cast<unsigned char*>(tmp_buf); strm.next_out = reinterpret_cast<unsigned char*>(tmp_buf);
strm.avail_out = BUFSIZE; strm.avail_out = BUFSIZE;
} }
deflate_res = deflate(&strm, Z_FINISH); deflate_res = deflate(&strm, Z_FINISH);
} }
if (deflate_res != Z_STREAM_END) if (deflate_res != Z_STREAM_END)
return false; return false;
dest_buffer.append(tmp_buf, BUFSIZE - strm.avail_out); dest_buffer.append(tmp_buf, BUFSIZE - strm.avail_out);
deflateEnd(&strm); deflateEnd(&strm);
return true; return true;
} }
QByteArray HttpResponseGenerator::toByteArray() {
// A gzip seems to have 23 bytes overhead.
// Also "content-encoding: gzip\r\n" is 26 bytes long
// So we only benefit from gzip if the message is bigger than 23+26 = 49
// If the message is smaller than 49 bytes we actually send MORE data if we gzip
if (m_gzip && m_message.size() > 49) {
QByteArray dest_buf;
if (gCompress(dest_buf)) {
setValue("content-encoding", "gzip");
#if QT_VERSION < 0x040800
m_message = dest_buf;
#else
m_message.swap(dest_buf);
#endif
}
}
setContentLength(m_message.size());
return HttpResponseHeader::toString().toUtf8() + m_message;
}

23
src/webui/httpresponsegenerator.h

@ -1,5 +1,6 @@
/* /*
* Bittorrent Client using Qt4 and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Ishan Arora and Christophe Dumez * Copyright (C) 2006 Ishan Arora and Christophe Dumez
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -24,32 +25,18 @@
* modify file(s), you may extend this exception to your version of the file(s), * modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this * but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version. * exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/ */
#ifndef HTTPRESPONSEGENERATOR_H #ifndef HTTPRESPONSEGENERATOR_H
#define HTTPRESPONSEGENERATOR_H #define HTTPRESPONSEGENERATOR_H
#include "httpresponseheader.h" #include "httptypes.h"
class HttpResponseGenerator : public HttpResponseHeader class HttpResponseGenerator
{ {
public: public:
HttpResponseGenerator(): m_gzip(false) {} static QByteArray generate(HttpResponse response);
void setMessage(const QByteArray& message);
void setMessage(const QString& message);
void setContentTypeByExt(const QString& ext);
void setContentEncoding(bool gzip) { m_gzip = gzip; }
QByteArray toByteArray();
private:
bool gCompress(QByteArray &dest_buffer);
QByteArray m_message;
bool m_gzip;
}; };
#endif #endif

131
src/webui/httpresponseheader.cpp

@ -1,131 +0,0 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2014 sledgehammer999
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : hammered999@gmail.com
*/
#include "httpresponseheader.h"
HttpResponseHeader::HttpResponseHeader(): HttpHeader(), m_code(200), m_majorVersion(1), m_minorVersion(1) {}
HttpResponseHeader::HttpResponseHeader(int code, const QString &text, int majorVer, int minorVer):
HttpHeader(), m_code(code), m_text(text), m_majorVersion(majorVer), m_minorVersion(minorVer) {}
HttpResponseHeader::HttpResponseHeader(const QString &str): HttpHeader() {
int line = str.indexOf("\r\n", 0, Qt::CaseInsensitive);
QString res = str.left(line);
QString headers = str.right(str.size() - line - 2); //"\r\n" == 2
QStringList status = res.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive);
if (status.size() != 3 || status.size() != 2) //reason-phrase could be empty
setValid(false);
else {
if (parseVersions(status[0])) {
bool ok;
m_code = status[1].toInt(&ok);
if (ok) {
m_text = status[2];
parse(headers);
}
else
setValid(false);
}
else
setValid(false);
}
}
int HttpResponseHeader::majorVersion() const {
return m_majorVersion;
}
int HttpResponseHeader::minorVersion() const {
return m_minorVersion;
}
QString HttpResponseHeader::reasonPhrase() const {
return m_text;
}
void HttpResponseHeader::setStatusLine(int code, const QString &text, int majorVer, int minorVer) {
m_code = code;
m_text = text;
m_majorVersion = majorVer;
m_minorVersion = minorVer;
}
int HttpResponseHeader::statusCode() const {
return m_code;
}
QString HttpResponseHeader::toString() const {
QString str = "HTTP/" + QString::number(m_majorVersion) + "." + QString::number(m_minorVersion) + " ";
QString code = QString::number(m_code);
if (code.size() > 3) {
str+= code.left(3) + " ";
}
else if (code.size() < 3) {
int padding = 3 - code.size();
for (int i = 0; i < padding; ++i)
code.push_back("0");
str += code + " ";
}
else {
str += code + " ";
}
str += m_text + "\r\n";
str += HttpHeader::toString();
return str;
}
bool HttpResponseHeader::parseVersions(const QString &str) {
if (str.size() <= 5) // HTTP/ which means version missing
return false;
if (str.left(4) != "HTTP")
return false;
QString versions = str.right(str.size() - 5); // Strip "HTTP/"
int decPoint = versions.indexOf('.');
if (decPoint == -1)
return false;
bool ok;
m_majorVersion = versions.left(decPoint).toInt(&ok);
if (!ok)
return false;
m_minorVersion = versions.right(versions.size() - decPoint - 1).toInt(&ok);
if (!ok)
return false;
return true;
}

56
src/webui/httpresponseheader.h

@ -1,56 +0,0 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2014 sledgehammer999
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : hammered999@gmail.com
*/
#ifndef HTTPRESPONSEHEADER_H
#define HTTPRESPONSEHEADER_H
#include "httpheader.h"
class HttpResponseHeader: public HttpHeader {
public:
HttpResponseHeader();
HttpResponseHeader(int code, const QString &text = QString(), int majorVer = 1, int minorVer = 1);
HttpResponseHeader(const QString &str);
virtual int majorVersion() const;
virtual int minorVersion() const;
QString reasonPhrase() const;
void setStatusLine(int code, const QString &text = QString(), int majorVer = 1, int minorVer = 1);
int statusCode() const;
virtual QString toString() const;
private:
bool parseVersions(const QString &str);
int m_code;
QString m_text;
int m_majorVersion;
int m_minorVersion;
};
#endif // HTTPRESPONSEHEADER_H

89
src/webui/httptypes.h

@ -0,0 +1,89 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#ifndef HTTPTYPES_H
#define HTTPTYPES_H
#include <QString>
#include <QMap>
#include <QHostAddress>
typedef QMap<QString, QString> QStringMap;
const QString HEADER_SET_COOKIE = "Set-Cookie";
const QString HEADER_CONTENT_TYPE = "Content-Type";
const QString HEADER_CONTENT_ENCODING = "Content-Encoding";
const QString HEADER_CONTENT_LENGTH = "Content-Length";
const QString CONTENT_TYPE_CSS = "text/css";
const QString CONTENT_TYPE_GIF = "image/gif";
const QString CONTENT_TYPE_HTML = "text/html";
const QString CONTENT_TYPE_JS = "text/javascript";
const QString CONTENT_TYPE_PNG = "image/png";
const QString CONTENT_TYPE_TXT = "text/plain";
struct HttpEnvironment
{
QHostAddress clientAddress;
};
struct UploadedFile
{
QString filename; // original filename
QString type; // MIME type
QByteArray data; // File data
};
struct HttpRequest
{
QString method;
QString path;
QStringMap headers;
QStringMap gets;
QStringMap posts;
QMap<QString, UploadedFile> files;
};
struct HttpResponseStatus
{
uint code;
QString text;
HttpResponseStatus(uint code = 200, const QString& text = "OK"): code(code), text(text) {}
};
struct HttpResponse
{
HttpResponseStatus status;
QStringMap headers;
QByteArray content;
HttpResponse(uint code = 200, const QString& text = "OK"): status(code, text) {}
};
#endif // HTTPTYPES_H
Loading…
Cancel
Save