Skip to content

Commit 3391e5e

Browse files
committed
Auto decode parameters by default
1 parent 3fbe336 commit 3391e5e

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

CPPWebFramework/cwf/httpparser.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <QVector>
1313
#include <QByteArray>
1414
#include <QNetworkCookie>
15+
#include "urlencoder.h"
1516
#include "cppwebframework_global.h"
1617

1718
CWF_BEGIN_NAMESPACE
@@ -91,7 +92,10 @@ class CPPWEBFRAMEWORKSHARED_EXPORT HttpParser
9192
* @param const QByteArray &name : Parameter name.
9293
* @return QByteArray : Parameter value.
9394
*/
94-
inline QByteArray getParameter(const QByteArray &name) const noexcept { return parameters.value(name); }
95+
inline QByteArray getParameter(const QByteArray &name, bool urlDecode = true, bool replacePlusForSpace = true) const noexcept
96+
{
97+
return urlDecode ? URLEncoder::paramDecode(parameters.value(name), replacePlusForSpace).toUtf8() : parameters.value(name);
98+
}
9599
/**
96100
* @brief Returns all parameters with a specific name.
97101
* @param const QByteArray &name : Parameter name.

CPPWebFramework/cwf/request.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ Request::~Request() noexcept
2727
delete requestDispatcher;
2828
}
2929

30-
void Request::fillQObject(QObject *object)
30+
void Request::fillQObject(QObject *object, bool urlDecode, bool replacePlusForSpace)
3131
{
32-
fillQObject(object, httpParser->getParameters());
32+
fillQObject(object, httpParser->getParameters(), urlDecode, replacePlusForSpace);
3333
}
3434

35-
void Request::fillQObject(QObject *object, const QMap<QByteArray, QByteArray> &parameters)
35+
void Request::fillQObject(QObject *object, const QMap<QByteArray, QByteArray> &parameters, bool urlDecode, bool replacePlusForSpace)
3636
{
3737
MetaClassParser meta(object);
3838

@@ -50,10 +50,12 @@ void Request::fillQObject(QObject *object, const QMap<QByteArray, QByteArray> &p
5050

5151
if(parameterType == CSTL::SUPPORTED_TYPES::QSTRING)
5252
{
53+
value = URLEncoder::paramDecode(value.toLatin1(), replacePlusForSpace);
5354
QMetaObject::invokeMethod(object, method.toStdString().data(), Q_ARG(QString, value));
5455
}
5556
else if(parameterType == CSTL::SUPPORTED_TYPES::STD_STRING)
5657
{
58+
value = URLEncoder::paramDecode(value.toLatin1(), replacePlusForSpace);
5759
QMetaObject::invokeMethod(object, method.toStdString().data(), Q_ARG(std::string, value.toStdString()));
5860
}
5961
else if(parameterType == CSTL::SUPPORTED_TYPES::BOOL)

CPPWebFramework/cwf/request.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
#include <QJsonObject>
1515
#include <QJsonDocument>
1616
#include "httpparser.h"
17+
#include "urlencoder.h"
1718
#include "session.h"
1819
#include "filemanager.h"
1920
#include "configuration.h"
2021
#include "requestdispatcher.h"
2122
#include "cppwebframework_global.h"
2223

24+
2325
CWF_BEGIN_NAMESPACE
2426
/**
2527
* @brief The Request class holds all information about a http request.
@@ -133,7 +135,10 @@ class CPPWEBFRAMEWORKSHARED_EXPORT Request
133135
* @param decode : If true, decode the parameter.
134136
* @return QByteArray
135137
*/
136-
QByteArray getParameter(const QByteArray &name) const noexcept { return httpParser->getParameter(name); }
138+
QByteArray getParameter(const QByteArray &name, bool urlDecode = true, bool replacePlusForSpace = true) const noexcept
139+
{
140+
return httpParser->getParameter(name, urlDecode, replacePlusForSpace);
141+
}
137142
/**
138143
* @brief This method returns the parameters from a request given an specific name.
139144
* @param name : This is a reference to a QByteArray.
@@ -330,9 +335,11 @@ class CPPWEBFRAMEWORKSHARED_EXPORT Request
330335
* }
331336
* @endcode
332337
*/
333-
void fillQObject(QObject *object);
338+
void fillQObject(QObject *object, bool urlDecode = true, bool replacePlusForSpace = true);
334339

335-
void fillQObject(QObject *object, const QMap<QByteArray, QByteArray> &parameters);
340+
void fillQObject(QObject *object,
341+
const QMap<QByteArray, QByteArray> &parameters,
342+
bool urlDecode = true, bool replacePlusForSpace = true);
336343
};
337344

338345
CWF_END_NAMESPACE

CPPWebFramework/cwf/urlencoder.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
CWF_BEGIN_NAMESPACE
1111

12-
QString URLEncoder::decode(const QByteArray &url)
12+
QString URLEncoder::decode(QByteArray url, bool replacePlusForSpace)
1313
{
14+
if(replacePlusForSpace)
15+
url = url.replace("+", " ");
1416
QUrl copy(url);
1517
copy.setQuery(copy.query(QUrl::FullyDecoded), QUrl::DecodedMode);
1618

19+
1720
return copy.toString();
1821
}
1922

@@ -29,8 +32,10 @@ QString URLEncoder::paramEncode(const QByteArray &param)
2932
return url.toEncoded().remove(0, 3);
3033
}
3134

32-
QString URLEncoder::paramDecode(const QByteArray &param)
35+
QString URLEncoder::paramDecode(QByteArray param, bool replacePlusForSpace)
3336
{
37+
if(replacePlusForSpace)
38+
param = param.replace("+", " ");
3439
QUrl url("?p=" + param);
3540
url.setQuery(url.query(QUrl::FullyDecoded), QUrl::DecodedMode);
3641

CPPWebFramework/cwf/urlencoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CPPWEBFRAMEWORKSHARED_EXPORT URLEncoder
2424
* @param url
2525
* @return
2626
*/
27-
static QString decode(const QByteArray &url);
27+
static QString decode(QByteArray url, bool replacePlusForSpace = true);
2828
/**
2929
* @brief encode
3030
* @param url
@@ -42,7 +42,7 @@ class CPPWEBFRAMEWORKSHARED_EXPORT URLEncoder
4242
* @param param
4343
* @return
4444
*/
45-
static QString paramDecode(const QByteArray &param);
45+
static QString paramDecode(QByteArray param, bool replacePlusForSpace = true);
4646
};
4747

4848
CWF_END_NAMESPACE

0 commit comments

Comments
 (0)