C++ Web Framework  3.0
httpparser.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 HTTPPARSER_H
9 #define HTTPPARSER_H
10 
11 #include <QMap>
12 #include <QVector>
13 #include <QByteArray>
14 #include <QNetworkCookie>
15 #include "cppwebframework_global.h"
16 
17 CWF_BEGIN_NAMESPACE
21 class CPPWEBFRAMEWORKSHARED_EXPORT HttpParser
22 {
23  friend class HttpReadRequest;
24  qint64 contentLenght = 0;
25  QByteArray contentType;
26  QByteArray httpVersion;
27  QByteArray method;
28  QByteArray body;
29  QByteArray sessionId;
30  QByteArray url;
31  QMultiMap<QByteArray, QByteArray> parameters;
32  QMultiMap<QByteArray, QByteArray> headerField;
33  QMultiMap<QByteArray, QByteArray> files;
34  QVector<QNetworkCookie> cookies;
35  bool valid = false;
36  bool multiPart = false;
37  bool readFile = false;
38  bool extractHeaderAndBody(QByteArray &httpMessage);
39  void doParse(QByteArray &httpMessage);
40  void doParseHttpHeader(QByteArray &httpMessage);
41  void doParseUrl();
42  void doParseBody();
43  void doParseFiles();
44  void extractCookies();
45 public:
50  explicit HttpParser(QByteArray &httpMessage) { doParse(httpMessage); }
55  inline qint64 getContentLenght() const noexcept { return contentLenght; }
60  inline QByteArray getContentType() const noexcept { return contentType; }
65  inline QByteArray getHttpVersion() const noexcept { return httpVersion; }
70  inline QByteArray getMethod() const noexcept { return method; }
75  inline QByteArray getBody() const noexcept { return body; }
80  inline QByteArray getSessionId() const noexcept { return sessionId; }
85  inline QByteArray getUrl() const noexcept { return url; }
94  inline QByteArray getParameter(const QByteArray &name) const noexcept { return parameters.value(name); }
100  inline QByteArrayList getParameters(const QByteArray &name) const noexcept { return parameters.values(name); }
105  inline QMultiMap<QByteArray, QByteArray> getParameters() const noexcept { return parameters; }
110  inline QMultiMap<QByteArray, QByteArray> getUploadedFiles() const noexcept { return files; }
115  inline QVector<QNetworkCookie> getCookies() const noexcept { return cookies; }
121  inline QByteArrayList getHeaderFields(const QByteArray &name) const noexcept { return headerField.values(name); }
130  inline QByteArray getHeaderField(const QByteArray &name) const noexcept { return headerField.value(name); }
135  inline bool isValid() const noexcept { return valid; }
140  inline bool isMultiPart() const noexcept { return multiPart; }
145  inline bool getReadFile() const noexcept { return readFile; }
146 };
147 
148 CWF_END_NAMESPACE
149 
150 #endif // HTTPPARSER_H
HttpParser(QByteArray &httpMessage)
This constructor receives a HTTP message and parses it.
Definition: httpparser.h:50
QByteArray getHeaderField(const QByteArray &name) const noexcept
Returns a specific header field given a name. If the header field name does not exists, the function returns defaultValue. If no defaultValue is specified, the function returns a default-constructed value. If there are multiple header field with a name, the value of the most recently inserted one is returned.
Definition: httpparser.h:130
QByteArray getSessionId() const noexcept
Returns session id.
Definition: httpparser.h:80
qint64 getContentLenght() const noexcept
Returns the content lenght.
Definition: httpparser.h:55
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
QByteArray getMethod() const noexcept
Returns HTTP method.
Definition: httpparser.h:70
QByteArrayList getHeaderFields(const QByteArray &name) const noexcept
Returns all header fields given a specific name.
Definition: httpparser.h:121
QByteArray getContentType() const noexcept
Returns the content type.
Definition: httpparser.h:60
The HttpReadRequest class is created automatically by the CppWebServer and inserted in a QThreadPoo...
Definition: httpreadrequest.h:36
bool getReadFile() const noexcept
Returns true if all message was read.
Definition: httpparser.h:145
QMultiMap< QByteArray, QByteArray > getParameters() const noexcept
Returns all parameters.
Definition: httpparser.h:105
bool isValid() const noexcept
Returns true if HTTP is valid, else it returns false.
Definition: httpparser.h:135
QByteArray getUrl() const noexcept
Returns the url.
Definition: httpparser.h:85
bool isMultiPart() const noexcept
Returns the multi part.
Definition: httpparser.h:140
QVector< QNetworkCookie > getCookies() const noexcept
Returns all cookies.
Definition: httpparser.h:115
The class parses a HTTP message.
Definition: httpparser.h:21
QByteArray getBody() const noexcept
Returns HTTP body message.
Definition: httpparser.h:75
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