C++ Web Framework  3.0
Public Member Functions | Friends | List of all members
Request Class Reference

The Request class holds all information about a http request. More...

#include <request.h>

Public Member Functions

 Request (QTcpSocket &socket, QMapThreadSafety< QString, Session *> &sessions, const Configuration &configuration)
 This constructor needs to receive a reference to a QTcpSocket and QByteArray. The parameter parent is optional. NOTE: The CppWebServer is responsable to create the QTcpSocket, and the HttpReadRequest is responsable to create a HttpReadRequest and a Response.
 
virtual ~Request () noexcept
 Destroys dynamically allocated resources.
 
void addAttribute (const std::initializer_list< QPair< QString, QObject *>> &value) noexcept
 This method add attributes that will be passed to a view page. The object can be processed within a page using view CSTL. For this to be possible the object must inherit from QObject and methods and must be in session "public slots".
 
void addAttribute (const QString &name, QObject *value) noexcept
 This method add an attribute that will be passed to a view page. The object can be processed within a page using view CSTL. For this to be possible the object must inherit from QObject and methods and must be in session "public slots".
 
QMap< QString, QObject * > getAttributes () const noexcept
 This method returns all the attributes of a HttpReadRequest.
 
const QObject * getAttribute (const QString &name) const noexcept
 This method returns a specific object given its name.
 
const QByteArray getBody () const noexcept
 Returns the request body.
 
QJsonObject bodyToJsonObject () const noexcept
 Tries returns the body of the converted request to QJsonObject.
 
QJsonArray bodyToJsonArray () const noexcept
 Tries returns the body of the converted request to QJsonArray.
 
RequestDispatchergetRequestDispatcher (const QString &page)
 This method returns a requestDispatcher given an specific page. More...
 
QByteArray getProtocol () const noexcept
 This method returns the http protocol. More...
 
void clearAttributes () noexcept
 This method will clear all the attributes.
 
void setHttpParser (HttpParser &httpParser) noexcept
 This method set the HttpParser. More...
 
HttpParsergetHttpParser () const noexcept
 This method returns the HttpParser. More...
 
QByteArray getRequestURL () const noexcept
 This method returns the requested url. More...
 
QByteArray getRequestURI () const noexcept
 This method returns the requested url. More...
 
SessiongetSession ()
 This method returns the user's session.
 
void setSession (Session &session) noexcept
 This method set the user's session. More...
 
QByteArray getParameter (const QByteArray &name) const noexcept
 This method returns the most recent parameter from a request given an specific name. More...
 
QByteArrayList getParameters (const QByteArray &name) const noexcept
 This method returns the parameters from a request given an specific name. More...
 
QTcpSocket & getSocket () const noexcept
 This method returns a reference to the current socket. More...
 
QString getPath () const noexcept
 This method returns the path. More...
 
QMultiMap< QByteArray, QByteArray > getUploadedFiles () const noexcept
 This method returns all the files that the user has sent. More...
 
void fillQObject (QObject *object)
 Fill a QObject using parameters of a HTTP message. More...
 
void fillQObject (QObject *object, const QMap< QByteArray, QByteArray > &parameters)
 

Friends

class HttpReadRequest
 
class RequestDispatcher
 

Detailed Description

The Request class holds all information about a http request.

Member Function Documentation

◆ fillQObject()

void Request::fillQObject ( QObject *  object)

Fill a QObject using parameters of a HTTP message.

Parameters
QObject*object : Object to be filled.
Example
//----------------bmi.view----------------
<?xml version="1.0" encoding="iso-8859-1" ?>
<html>
<head>
<title>Body Mass Index (BMI)</title>
</head>
<body>
<form method="POST" action="/bmi">
Name<br/><input type="text" name="name"/><br/>
Mass(KG)<br/><input type="text" name="mass"/><br/>
Height(m)<br/><input type="text" name="height"/><br/><br/>
<input type="submit" name="submit" value="Calculate"/>
</form>
</body>
</html>
//----------------bmiresults.view----------------
<?xml version="1.0" encoding="iso-8859-1" ?>
<html>
<head>
<title>Body Mass Index (BMI) - Results</title>
</head>
<body>
Name: <out value="#{user.getName}"/><br/>
Mass(KG): <out value="#{user.getMass}"/><br/>
Height(m): <out value="#{user.getHeight}"/><br/>
BMI: <out value="#{user.getBmi}"/><br/>
Category: <out value="#{user.getCategory}"/>
</body>
</html>
//----------------user.h----------------
#ifndef USER_H
#define USER_H
#include <QObject>
#include <QString>
class User : public QObject
{
Q_OBJECT
private:
QString name;
QString category;
double mass = 0;
double height = 0;
double bmi = 0;
public:
explicit User(QObject *parent = 0) : QObject(parent)
{
}
public slots:
QString getName() const
{
return name;
}
void setName(const QString &value)
{
name = value;
}
QString getCategory() const
{
return category;
}
double getMass() const
{
return mass;
}
void setMass(double value)
{
mass = value;
}
double getHeight() const
{
return height;
}
void setHeight(double value)
{
height = value;
}
double getBmi()
{
bmi = height != 0 ? mass / (height * height) : 0;
if(bmi <= 15)
{
category = "Very severely underweight";
}
else if(bmi > 15 && bmi <= 16)
{
category = "Severely underweight";
}
else if(bmi > 16 && bmi <= 18.5)
{
category = "Underweight";
}
else if(bmi > 18.5 && bmi <= 25)
{
category = "Normal (healthy weight)";
}
else if(bmi > 25 && bmi <= 30)
{
category = "Overweight";
}
else if(bmi > 30 && bmi <= 35)
{
category = "Obese Class I (Moderately obese)";
}
else if(bmi > 35 && bmi <= 40)
{
category = "Obese Class II (Severely obese)";
}
else
{
category = "Obese Class III (Very severely obese)";
}
return bmi;
}
};
#endif // USER_H
//----------------bmicontroller.h----------------
#ifndef BMICONTROLLER_H
#define BMICONTROLLER_H
#include "cwf/controller.h"
#include "cwf/request.h"
#include "cwf/response.h"
#include "entities/user.h"
class BmiController : public CWF::Controller
{
public:
void doGet(CWF::Request &request, CWF::Response &response) override
{
request.getRequestDispatcher("/pages/bmi").forward(request, response);
}
void doPost(CWF::Request &request, CWF::Response &response) override
{
User user;
request.fillQObject(&user);
request.addAttribute("user", &user);
request.getRequestDispatcher("/pages/bmiresults.view").forward(request, response);
}
};
#endif // BMICONTROLLER_H
//----------------main.cpp----------------
#include <QCoreApplication>
#include <cwf/cppwebapplication.h>
#include <controllers/bmicontroller.h>
int main(int argc, char *argv[])
{
CWF::CppWebApplication server(argc, argv, "PATH_TO_SERVER_FOLDER");
server.addUrlController<BmiController>("/bmi");
return server.start();
}

◆ getHttpParser()

HttpParser& Request::getHttpParser ( ) const
inlinenoexcept

This method returns the HttpParser.

Returns
HttpParser

◆ getParameter()

QByteArray Request::getParameter ( const QByteArray &  name) const
inlinenoexcept

This method returns the most recent parameter from a request given an specific name.

Parameters
name: This is a reference to a QByteArray.
decode: If true, decode the parameter.
Returns
QByteArray

◆ getParameters()

QByteArrayList Request::getParameters ( const QByteArray &  name) const
inlinenoexcept

This method returns the parameters from a request given an specific name.

Parameters
name: This is a reference to a QByteArray.
Returns
QByteArray

◆ getPath()

QString Request::getPath ( ) const
inlinenoexcept

This method returns the path.

Returns
QString

◆ getProtocol()

QByteArray Request::getProtocol ( ) const
inlinenoexcept

This method returns the http protocol.

Returns
QByteArray

◆ getRequestDispatcher()

RequestDispatcher & Request::getRequestDispatcher ( const QString &  page)

This method returns a requestDispatcher given an specific page.

Parameters
page: This is a reference to a QByteArray.
Returns
RequestDispatcher

◆ getRequestURI()

QByteArray Request::getRequestURI ( ) const
inlinenoexcept

This method returns the requested url.

Returns
QByteArray

◆ getRequestURL()

QByteArray Request::getRequestURL ( ) const
inlinenoexcept

This method returns the requested url.

Returns
QByteArray

◆ getSocket()

QTcpSocket& Request::getSocket ( ) const
inlinenoexcept

This method returns a reference to the current socket.

Returns
QTcpSocket

◆ getUploadedFiles()

QMultiMap<QByteArray, QByteArray> Request::getUploadedFiles ( ) const
inlinenoexcept

This method returns all the files that the user has sent.

Returns
QMap<QByteArray, QByteArray>

◆ setHttpParser()

void Request::setHttpParser ( HttpParser httpParser)
inlinenoexcept

This method set the HttpParser.

Parameters
httpParser

◆ setSession()

void Request::setSession ( Session session)
inlinenoexcept

This method set the user's session.

Returns
Session

The documentation for this class was generated from the following files: