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