TreeFrogFramework  2.8
tmultipartformdata.h
Go to the documentation of this file.
1 #pragma once
2 #include <QFile>
3 #include <QMap>
4 #include <QPair>
5 #include <QStringList>
6 #include <TGlobal>
7 
8 class TActionContext;
9 class QIODevice;
10 
11 
13 public:
15  TMimeHeader(const TMimeHeader &other);
16  TMimeHeader &operator=(const TMimeHeader &other);
17 
18  bool isEmpty() const { return _headers.isEmpty(); }
19  QByteArray header(const QByteArray &headerName) const;
20  void setHeader(const QByteArray &headerName, const QByteArray &value);
21  QByteArray contentDispositionParameter(const QByteArray &name) const;
22  QByteArray dataName() const;
23  QString originalFileName() const;
24 
25 protected:
26  static QMap<QByteArray, QByteArray> parseHeaderParameter(const QByteArray &header);
27 
28 private:
29  static int skipWhitespace(const QByteArray &text, int pos);
30  QList<QPair<QByteArray, QByteArray>> _headers;
31 };
32 
33 
35 public:
36  static const QFile::Permissions DefaultPermissions;
37 
39  TMimeEntity(const TMimeEntity &other);
40  TMimeEntity &operator=(const TMimeEntity &other);
41 
42  const TMimeHeader &header() const { return _entity.first; }
43  TMimeHeader &header() { return _entity.first; }
44  QByteArray header(const QByteArray &headerName) const { return _entity.first.header(headerName); }
45  QByteArray dataName() const { return _entity.first.dataName(); }
46  QString contentType() const;
47  int64_t fileSize() const;
48  QString originalFileName() const { return _entity.first.originalFileName(); }
49  bool renameUploadedFile(const QString &newName, bool overwrite = false, QFile::Permissions permissions = DefaultPermissions);
50  QString uploadedFilePath() const;
51 
52 private:
53  TMimeEntity(const TMimeHeader &header, const QString &body);
54  QPair<TMimeHeader, QString> _entity;
55  friend class TMultipartFormData;
56 };
57 
58 
60 public:
61  static const QFile::Permissions DefaultPermissions;
62 
63  TMultipartFormData(const QByteArray &boundary = QByteArray());
64  TMultipartFormData(const QByteArray &formData, const QByteArray &boundary, TActionContext *context);
65  TMultipartFormData(const QString &bodyFilePath, const QByteArray &boundary, TActionContext *context);
67 
68  bool isEmpty() const;
69  bool hasFormItem(const QString &name) const;
70  QString formItemValue(const QString &name) const;
71  QStringList allFormItemValues(const QString &name) const;
72  QVariantList formItemVariantList(const QString &key) const;
73  QVariantMap formItems(const QString &key) const;
74  QVariantMap formItems() const;
75 
76  QString contentType(const QByteArray &dataName) const;
77  QString originalFileName(const QByteArray &dataName) const;
78  int64_t size(const QByteArray &dataName) const;
79  bool renameUploadedFile(const QByteArray &dataName, const QString &newName, bool overwrite = false, QFile::Permissions permissions = DefaultPermissions);
80  QString uploadedFilePath(const QByteArray &dataName) const;
81  void clear();
82 
83  bool hasEntity(const QByteArray &dataName) const;
84  TMimeEntity entity(const QByteArray &dataName) const;
85  QList<TMimeEntity> entityList(const QByteArray &dataName) const;
86 
87 protected:
88  void parse(QIODevice *dev, TActionContext *context);
89 
90 private:
91  TMimeHeader parseMimeHeader(QIODevice *dev) const;
92  QByteArray parseContent(QIODevice *dev) const;
93  QString writeContent(QIODevice *dev, TActionContext *context) const;
94 
95  QByteArray dataBoundary;
96  QList<QPair<QString, QString>> postParameters;
97  QList<TMimeEntity> uploadedFiles;
98  QString bodyFile;
99 
100  friend class THttpRequest;
101 };
102 
103 
104 Q_DECLARE_METATYPE(TMimeHeader)
105 Q_DECLARE_METATYPE(TMimeEntity)
106 Q_DECLARE_METATYPE(TMultipartFormData)
107 
The THttpRequest class contains request information for HTTP.
Definition: thttprequest.h:34
The TMimeEntity represents a MIME entity.
Definition: tmultipartformdata.h:34
const TMimeHeader & header() const
Returns a reference to the MIME header contained in this entity.
Definition: tmultipartformdata.h:42
QByteArray header(const QByteArray &headerName) const
Returns the value of the header headerName contained in this entity.
Definition: tmultipartformdata.h:44
QString originalFileName() const
Returns the original name of the file contained in this entity.
Definition: tmultipartformdata.h:48
TMimeEntity()
Definition: tmultipartformdata.h:38
static const QFile::Permissions DefaultPermissions
Definition: tmultipartformdata.h:36
TMimeHeader & header()
Returns a reference to the MIME header contained in this entity.
Definition: tmultipartformdata.h:43
QByteArray dataName() const
Returns the parameter 'name' of the header field content-disposition in this entity.
Definition: tmultipartformdata.h:45
The TMimeHeader class contains MIME header information for internet.
Definition: tmultipartformdata.h:12
TMimeHeader()
Definition: tmultipartformdata.h:14
bool isEmpty() const
Returns true if the MIME header is empty; otherwise returns false.
Definition: tmultipartformdata.h:18
The TMultipartFormData represents a media-type multipart/form-data.
Definition: tmultipartformdata.h:59
static const QFile::Permissions DefaultPermissions
Definition: tmultipartformdata.h:61
~TMultipartFormData()
Definition: tmultipartformdata.h:66
#define T_CORE_EXPORT
Definition: tdeclexport.h:28