C++ Web Framework  3.0
request.h
1 /*
2  Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
3  Distributed under MIT license, or public domain if desired and
4  recognized in your jurisdiction.
5  See file LICENSE for detail.
6 */
7 
8 #ifndef Request_H
9 #define Request_H
10 
11 #include <QThread>
12 #include <QTcpSocket>
13 #include <QJsonArray>
14 #include <QJsonObject>
15 #include <QJsonDocument>
16 #include "httpparser.h"
17 #include "session.h"
18 #include "filemanager.h"
19 #include "configuration.h"
20 #include "requestdispatcher.h"
21 #include "cppwebframework_global.h"
22 
23 CWF_BEGIN_NAMESPACE
27 class CPPWEBFRAMEWORKSHARED_EXPORT Request
28 {
29  friend class HttpReadRequest;
30  friend class RequestDispatcher;
31  QTcpSocket *socket;
32  Session *session = nullptr;
33  HttpParser *httpParser = nullptr;
34  RequestDispatcher *requestDispatcher = nullptr;
35  Response *response = nullptr;
36  QMap<QString, QObject *> attributes;
38  const Configuration &configuration;
39 public:
46  explicit Request(QTcpSocket &socket, QMapThreadSafety<QString, Session *> &sessions, const Configuration &configuration);
50  virtual ~Request() noexcept;
56  inline void addAttribute(const std::initializer_list<QPair<QString, QObject *>> &value) noexcept
57  {
58  std::for_each(value.begin(), value.end(), [&](const QPair<QString, QObject *> &v){attributes.insert(v.first, v.second);});
59  }
65  inline void addAttribute(const QString &name, QObject *value) noexcept { attributes.insert(name, value); }
69  inline QMap<QString, QObject *> getAttributes() const noexcept { return attributes; }
73  inline const QObject *getAttribute(const QString &name) const noexcept { return attributes.contains(name) ? attributes[name] : nullptr; }
77  inline const QByteArray getBody() const noexcept { return httpParser->getBody(); }
81  inline QJsonObject bodyToJsonObject() const noexcept { return QJsonDocument::fromJson(httpParser->getBody()).object(); }
85  inline QJsonArray bodyToJsonArray() const noexcept { return QJsonDocument::fromJson(httpParser->getBody()).array(); }
91  RequestDispatcher &getRequestDispatcher(const QString &page);
96  inline QByteArray getProtocol() const noexcept { return httpParser != nullptr ? httpParser->getHttpVersion() : ""; }
100  inline void clearAttributes() noexcept { attributes.clear(); }
105  inline void setHttpParser(HttpParser &httpParser) noexcept { this->httpParser = &httpParser; }
110  inline HttpParser &getHttpParser() const noexcept { return *httpParser; }
115  inline QByteArray getRequestURL() const noexcept{ return httpParser != nullptr ? httpParser->getUrl() : ""; }
120  inline QByteArray getRequestURI() const noexcept { return httpParser != nullptr ? httpParser->getUrl() : ""; }
124  Session &getSession();
129  inline void setSession(Session &session) noexcept { this->session = &session; }
136  QByteArray getParameter(const QByteArray &name) const noexcept { return httpParser->getParameter(name); }
142  inline QByteArrayList getParameters(const QByteArray &name) const noexcept { return httpParser->getParameters(name); }
147  inline QTcpSocket &getSocket() const noexcept { return *socket; }
152  inline QString getPath() const noexcept { return configuration.getPath(); }
157  inline QMultiMap<QByteArray, QByteArray> getUploadedFiles() const noexcept { return httpParser->getUploadedFiles(); }
333  void fillQObject(QObject *object);
334 
335  void fillQObject(QObject *object, const QMap<QByteArray, QByteArray> &parameters);
336 };
337 
338 CWF_END_NAMESPACE
339 
340 #endif // Request_H
QJsonObject bodyToJsonObject() const noexcept
Tries returns the body of the converted request to QJsonObject.
Definition: request.h:81
QTcpSocket & getSocket() const noexcept
This method returns a reference to the current socket.
Definition: request.h:147
void clearAttributes() noexcept
This method will clear all the attributes.
Definition: request.h:100
QJsonArray bodyToJsonArray() const noexcept
Tries returns the body of the converted request to QJsonArray.
Definition: request.h:85
QByteArrayList getParameters(const QByteArray &name) const noexcept
Returns all parameters with a specific name.
Definition: httpparser.h:100
QByteArray getHttpVersion() const noexcept
Returns the HTTP version.
Definition: httpparser.h:65
void setSession(Session &session) noexcept
This method set the user&#39;s session.
Definition: request.h:129
The Request class holds all information about a http request.
Definition: request.h:27
void setHttpParser(HttpParser &httpParser) noexcept
This method set the HttpParser.
Definition: request.h:105
QString getPath() const noexcept
This method returns the path.
Definition: request.h:152
The HttpReadRequest class is created automatically by the CppWebServer and inserted in a QThreadPoo...
Definition: httpreadrequest.h:36
The QMapThreadSafety class is a thread safe QMap.
Definition: qmapthreadsafety.h:23
The RequestDispatcher class can be used to dispatch a requisition to a page.
Definition: requestdispatcher.h:20
QByteArray getProtocol() const noexcept
This method returns the http protocol.
Definition: request.h:96
QByteArray getUrl() const noexcept
Returns the url.
Definition: httpparser.h:85
QMultiMap< QByteArray, QByteArray > getUploadedFiles() const noexcept
This method returns all the files that the user has sent.
Definition: request.h:157
QByteArray getRequestURI() const noexcept
This method returns the requested url.
Definition: request.h:120
The Session class holds information about a client session.
Definition: session.h:30
QMap< QString, QObject * > getAttributes() const noexcept
This method returns all the attributes of a HttpReadRequest.
Definition: request.h:69
QString getPath() const noexcept
Returns the path to the server&#39;s folder.
Definition: configuration.h:112
The class parses a HTTP message.
Definition: httpparser.h:21
QByteArrayList getParameters(const QByteArray &name) const noexcept
This method returns the parameters from a request given an specific name.
Definition: request.h:142
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...
Definition: request.h:65
QByteArray getBody() const noexcept
Returns HTTP body message.
Definition: httpparser.h:75
All classes of C++ Web Framework are contained within the namespace CWF.
Definition: configuration.h:24
The Response class is responsable to response a Http request.
Definition: response.h:25
QMultiMap< QByteArray, QByteArray > getUploadedFiles() const noexcept
Returns all uploaded files.
Definition: httpparser.h:110
QByteArray getParameter(const QByteArray &name) const noexcept
Returns a specific parameter given a name. If the parameter name does not exists, the function return...
Definition: httpparser.h:94
const QObject * getAttribute(const QString &name) const noexcept
This method returns a specific object given its name.
Definition: request.h:73
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 p...
Definition: request.h:56
QByteArray getRequestURL() const noexcept
This method returns the requested url.
Definition: request.h:115
const QByteArray getBody() const noexcept
Returns the request body.
Definition: request.h:77
QByteArray getParameter(const QByteArray &name) const noexcept
This method returns the most recent parameter from a request given an specific name.
Definition: request.h:136
HttpParser & getHttpParser() const noexcept
This method returns the HttpParser.
Definition: request.h:110