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