drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
MultiPart.h
Go to the documentation of this file.
1
14
15#pragma once
16
18#include <drogon/exports.h>
19#include <drogon/HttpRequest.h>
20#include <unordered_map>
21#include <string>
22#include <vector>
23#include <memory>
24#include <string_view>
25
26namespace drogon
27{
28class HttpFileImpl;
29
34class DROGON_EXPORT HttpFile
35{
36 public:
37 explicit HttpFile(std::shared_ptr<HttpFileImpl> &&implPtr) noexcept;
39 const std::string &getFileName() const noexcept;
40
44 std::string_view getFileExtension() const noexcept;
45
47 const std::string &getItemName() const noexcept;
48
50 FileType getFileType() const noexcept;
51
53 void setFileName(const std::string &fileName) noexcept;
54
57 void setFile(const char *data, size_t length) noexcept;
58
60
64 int save() const noexcept;
65
67
73 int save(const std::string &path) const noexcept;
74
76
81 int saveAs(const std::string &fileName) const noexcept;
82
88 std::string_view fileContent() const noexcept
89 {
90 return std::string_view{fileData(), fileLength()};
91 }
92
94 size_t fileLength() const noexcept;
95
97 drogon::ContentType getContentType() const noexcept;
108 const char *fileData() const noexcept;
109
111 std::string getMd5() const noexcept;
112
114 const std::string &getContentTransferEncoding() const noexcept;
115
116 private:
117 std::shared_ptr<HttpFileImpl> implPtr_;
118};
119
122class DROGON_EXPORT MultiPartParser
123{
124 public:
125 MultiPartParser(){};
126 MultiPartParser(const MultiPartParser &other) = default; // Copyable
127 MultiPartParser(MultiPartParser &&other) = default; // Movable
128 ~MultiPartParser(){};
131 const std::vector<HttpFile> &getFiles() const;
132
134 std::unordered_map<std::string, HttpFile> getFilesMap() const;
135
138 const SafeStringMap<std::string> &getParameters() const;
139
142 template <typename T>
143 std::optional<T> getOptionalParameter(const std::string &key)
144 {
145 auto &params = getParameters();
146 auto it = params.find(key);
147 if (it != params.end())
148 {
149 try
150 {
151 return std::optional<T>(utils::fromString<T>(it->second));
152 }
153 catch (const std::exception &e)
154 {
155 LOG_ERROR << e.what();
156 return std::optional<T>{};
157 }
158 }
159 else
160 {
161 return std::optional<T>{};
162 }
163 }
164
168 template <typename T>
169 T getParameter(const std::string &key)
170 {
171 return getOptionalParameter<T>(key).value_or(T{});
172 }
173
175 int parse(const HttpRequestPtr &req);
176
177 protected:
178 std::vector<HttpFile> files_;
179 SafeStringMap<std::string> parameters_;
180 int parse(const HttpRequestPtr &req,
181 const char *boundaryData,
182 size_t boundaryLen);
183 int parseEntity(const HttpRequestPtr &req,
184 const char *begin,
185 const char *end);
186};
187
190
191} // namespace drogon
const std::string & getFileName() const noexcept
Return the file name;.
const char * fileData() const noexcept
return the pointer of the file data.
std::string_view fileContent() const noexcept
return the content of the file.
Definition MultiPart.h:88
const std::string & getItemName() const noexcept
Return the name of the item in multiple parts.
int save() const noexcept
Save the file to the file system.
void setFile(const char *data, size_t length) noexcept
FileType getFileType() const noexcept
Return the type of file.
size_t fileLength() const noexcept
Return the file length.
void setFileName(const std::string &fileName) noexcept
Set the file name, usually called by the MultiPartParser parser.
drogon::ContentType getContentType() const noexcept
Return the content-type of the file.
int saveAs(const std::string &fileName) const noexcept
Save the file to file system with a new name.
std::string_view getFileExtension() const noexcept
const std::string & getContentTransferEncoding() const noexcept
Return the content transfer encoding of the file.
std::string getMd5() const noexcept
Return the md5 string of the file.
Definition MultiPart.h:123
T getParameter(const std::string &key)
Definition MultiPart.h:169
const std::vector< HttpFile > & getFiles() const
int parse(const HttpRequestPtr &req)
Parse the http request stream to get files and parameters.
const SafeStringMap< std::string > & getParameters() const
std::optional< T > getOptionalParameter(const std::string &key)
Definition MultiPart.h:143
std::unordered_map< std::string, HttpFile > getFilesMap() const
Get files in a map, the keys of the map are item names of the files.
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
MultiPartParser FileUpload
In order to be compatible with old interfaces.
Definition MultiPart.h:189
STL namespace.