NAWA 0.9
Web Application Framework for C++
GPC.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::GPC::Data {
31 Source source;
32 unordered_multimap<string, string> dataMap;
33
34 explicit Data(Source source) : source(source) {}
35};
36
38
39request::GPC::GPC(RequestInitContainer const& requestInit, Source source) {
40 data = make_unique<Data>(source);
41
42 switch (source) {
43 case Source::COOKIE:
44 data->dataMap = requestInit.cookieVars;
45 break;
46 case Source::POST:
47 data->dataMap = requestInit.postVars;
48 break;
49 default:
50 data->dataMap = requestInit.getVars;
51 }
52}
53
54std::string request::GPC::operator[](std::string const& gpcVar) const {
55 auto e = data->dataMap.find(gpcVar);
56 if (e != data->dataMap.end())
57 return e->second;
58 else
59 return "";
60}
61
62std::vector<std::string> request::GPC::getVector(std::string const& gpcVar) const {
63 vector<string> ret;
64 auto e = data->dataMap.equal_range(gpcVar);
65 for (auto it = e.first; it != e.second; ++it) {
66 ret.push_back(it->second);
67 }
68 return ret;
69}
70
71size_t request::GPC::count(std::string const& gpcVar) const {
72 return data->dataMap.count(gpcVar);
73}
74
75std::unordered_multimap<std::string, std::string> const& request::GPC::getMultimap() const {
76 return data->dataMap;
77}
78
79std::unordered_multimap<std::string, std::string>::const_iterator request::GPC::begin() const {
80 return data->dataMap.begin();
81}
82
83std::unordered_multimap<std::string, std::string>::const_iterator request::GPC::end() const {
84 return data->dataMap.end();
85}
86
87request::GPC::operator bool() const {
88 return !data->dataMap.empty();
89}
Accessor class for GET, POST, and COOKIE variables.
Container used by request handlers to initiate the nawa::Request object.
std::unordered_multimap< std::string, std::string >::const_iterator end() const
Definition: GPC.cpp:83
std::unordered_multimap< std::string, std::string > const & getMultimap() const
Definition: GPC.cpp:75
size_t count(std::string const &gpcVar) const
Definition: GPC.cpp:71
std::unordered_multimap< std::string, std::string >::const_iterator begin() const
Definition: GPC.cpp:79
std::vector< std::string > getVector(std::string const &gpcVar) const
Definition: GPC.cpp:62
#define NAWA_DEFAULT_DESTRUCTOR_IMPL_WITH_NS(Namespace, Class)
Definition: macros.h:37
Definition: AppInit.h:31