TreeFrogFramework  2.8
thttprequest.h
Go to the documentation of this file.
1 #pragma once
2 #include <QByteArray>
3 #include <QHostAddress>
4 #include <QJsonDocument>
5 #include <QList>
6 #include <QPair>
7 #include <QSharedData>
8 #include <QVariant>
9 #include <TCookieJar>
10 #include <TGlobal>
11 #include <THttpRequestHeader>
12 #include <TMultipartFormData>
13 
14 class TActionContext;
15 class QIODevice;
16 
17 
18 class T_CORE_EXPORT THttpRequestData : public QSharedData {
19 public:
21  THttpRequestData(const THttpRequestData &other);
23 
25  QByteArray bodyArray;
26  QList<QPair<QString, QString>> queryItems;
27  QList<QPair<QString, QString>> formItems;
29  QJsonDocument jsonData;
30  QHostAddress clientAddress;
31 };
32 
33 
35 public:
36  THttpRequest();
37  THttpRequest(const THttpRequest &other);
38  THttpRequest(const THttpRequestHeader &header, const QByteArray &body, const QHostAddress &clientAddress, TActionContext *context);
39  THttpRequest(const QByteArray &header, const QString &filePath, const QHostAddress &clientAddress, TActionContext *context);
40  virtual ~THttpRequest();
41  THttpRequest &operator=(const THttpRequest &other);
42 
43  const THttpRequestHeader &header() const { return d->header; }
44  Tf::HttpMethod method() const;
45  Tf::HttpMethod realMethod() const;
46  Tf::HttpMethod getHttpMethodOverride() const;
47  Tf::HttpMethod queryItemMethod() const;
48  QString parameter(const QString &name) const;
49  QVariantMap allParameters() const;
50 
51  bool hasQuery() const { return !d->queryItems.isEmpty(); }
52  bool hasQueryItem(const QString &name) const;
53  QString queryItemValue(const QString &name) const;
54  QString queryItemValue(const QString &name, const QString &defaultValue) const;
55  QStringList allQueryItemValues(const QString &name) const;
56  QStringList queryItemList(const QString &key) const;
57  QVariantList queryItemVariantList(const QString &key) const;
58  QVariantMap queryItems(const QString &key) const;
59  QVariantMap queryItems() const;
60  bool hasForm() const { return !d->formItems.isEmpty(); }
61  bool hasFormItem(const QString &name) const;
62  QString formItemValue(const QString &name) const;
63  QString formItemValue(const QString &name, const QString &defaultValue) const;
64  QStringList allFormItemValues(const QString &name) const;
65  QStringList formItemList(const QString &key) const;
66  QVariantList formItemVariantList(const QString &key) const;
67  QVariantMap formItems(const QString &key) const;
68  QVariantMap formItems() const;
69  TMultipartFormData &multipartFormData() { return d->multipartFormData; }
70  QByteArray cookie(const QString &name) const;
71  QList<TCookie> cookies() const;
72  QHostAddress clientAddress() const { return d->clientAddress; }
73  QHostAddress originatingClientAddress() const;
74  QIODevice *rawBody();
75  bool hasJson() const { return !d->jsonData.isNull(); }
76  const QJsonDocument &jsonData() const { return d->jsonData; }
77 
78  static QList<THttpRequest> generate(QByteArray &byteArray, const QHostAddress &address, TActionContext *context);
79  static QList<QPair<QString, QString>> fromQuery(const QString &query);
80 
81 protected:
82  QByteArray boundary() const;
83 
84  static bool hasItem(const QString &name, const QList<QPair<QString, QString>> &items);
85  static QString itemValue(const QString &name, const QString &defaultValue, const QList<QPair<QString, QString>> &items);
86  static QStringList allItemValues(const QString &name, const QList<QPair<QString, QString>> &items);
87  static QVariantList itemVariantList(const QString &key, const QList<QPair<QString, QString>> &items);
88  static QVariantMap itemMap(const QList<QPair<QString, QString>> &items);
89  static QVariantMap itemMap(const QString &key, const QList<QPair<QString, QString>> &items);
90 
91 private:
92  void parseBody(const QByteArray &body, const THttpRequestHeader &header, TActionContext *context);
93 
94  QSharedDataPointer<THttpRequestData> d;
95  QIODevice *bodyDevice {nullptr};
96 
97  friend class TMultipartFormData;
98 };
99 
100 Q_DECLARE_METATYPE(THttpRequest)
101 
The THttpRequestData class is for shared THttpRequest data objects.
Definition: thttprequest.h:18
TMultipartFormData multipartFormData
Definition: thttprequest.h:28
QList< QPair< QString, QString > > queryItems
Definition: thttprequest.h:26
THttpRequestHeader header
Definition: thttprequest.h:24
QList< QPair< QString, QString > > formItems
Definition: thttprequest.h:27
QByteArray bodyArray
Definition: thttprequest.h:25
THttpRequestData()
Definition: thttprequest.h:20
QJsonDocument jsonData
Definition: thttprequest.h:29
QHostAddress clientAddress
Definition: thttprequest.h:30
~THttpRequestData()
Definition: thttprequest.h:22
The THttpRequestHeader class contains request header information for HTTP.
Definition: thttpheader.h:24
The THttpRequest class contains request information for HTTP.
Definition: thttprequest.h:34
QHostAddress clientAddress() const
Returns the address of the client host.
Definition: thttprequest.h:72
bool hasJson() const
Returns true if the request contains JSON data.
Definition: thttprequest.h:75
TMultipartFormData & multipartFormData()
Returns a object of multipart/form-data.
Definition: thttprequest.h:69
bool hasForm() const
Returns true if the request contains form data.
Definition: thttprequest.h:60
bool hasQuery() const
Returns true if the URL contains a Query.
Definition: thttprequest.h:51
const QJsonDocument & jsonData() const
Return the JSON data contained in the request.
Definition: thttprequest.h:76
const THttpRequestHeader & header() const
Returns the HTTP header of the request.
Definition: thttprequest.h:43
The TMultipartFormData represents a media-type multipart/form-data.
Definition: tmultipartformdata.h:59
HttpMethod
Definition: tfnamespace.h:21
#define T_CORE_EXPORT
Definition: tdeclexport.h:28