NAWA 0.9
Web Application Framework for C++
Post.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 Tobias Flaig.
3 *
4 * This file is part of nawa.
5 *
6 * nawa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License,
8 * version 3, as published by the Free Software Foundation.
9 *
10 * nawa is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
17 */
18
26
27using namespace nawa;
28using namespace std;
29
30struct request::Post::Data {
31 std::string contentType;
32 std::shared_ptr<std::string> rawPost;
33 std::unordered_multimap<std::string, File> fileMap;
34
35 explicit Data(RequestInitContainer const& requestInit) : contentType(requestInit.postContentType),
36 rawPost(requestInit.rawPost),
37 fileMap(requestInit.postFiles) {}
38};
39
41
42request::Post::Post(RequestInitContainer const& requestInit) : GPC(requestInit, GPC::Source::POST) {
43 data = make_unique<Data>(requestInit);
44}
45
46request::Post::operator bool() const {
47 return !(getMultimap().empty() && data->fileMap.empty());
48}
49
50shared_ptr<std::string const> request::Post::getRaw() const {
51 return data->rawPost;
52}
53
54std::string request::Post::getContentType() const {
55 return data->contentType;
56}
57
59 return !data->fileMap.empty();
60}
61
62std::optional<File> request::Post::getFile(std::string const& key) const {
63 auto e = data->fileMap.find(key);
64 if (e != data->fileMap.end())
65 return e->second;
66 return nullopt;
67}
68
69std::vector<File> request::Post::getFileVector(std::string const& key) const {
70 vector<File> ret;
71 auto e = data->fileMap.equal_range(key);
72 for (auto it = e.first; it != e.second; ++it) {
73 ret.push_back(it->second);
74 }
75 return ret;
76}
77
78size_t request::Post::countFiles(std::string const& key) const {
79 return data->fileMap.count(key);
80}
81
82std::unordered_multimap<std::string, File> const& request::Post::getFileMultimap() const {
83 return data->fileMap;
84}
Specialization of the GPC class for POST variables.
Container used by request handlers to initiate the nawa::Request object.
std::vector< File > getFileVector(std::string const &key) const
Definition: Post.cpp:69
std::unordered_multimap< std::string, File > const & getFileMultimap() const
Definition: Post.cpp:82
bool hasFiles() const
Definition: Post.cpp:58
std::optional< File > getFile(std::string const &key) const
Definition: Post.cpp:62
size_t countFiles(std::string const &key) const
Definition: Post.cpp:78
std::string getContentType() const
Definition: Post.cpp:54
#define NAWA_DEFAULT_DESTRUCTOR_IMPL_WITH_NS(Namespace, Class)
Definition: macros.h:37
Definition: AppInit.h:31