NAWA 0.8
Web Application Framework for C++
GPC.cpp
Go to the documentation of this file.
1
6/*
7 * Copyright (C) 2019-2021 Tobias Flaig.
8 *
9 * This file is part of nawa.
10 *
11 * nawa is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License,
13 * version 3, as published by the Free Software Foundation.
14 *
15 * nawa is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
22 */
23
25#include <nawa/request/RequestInitContainer.h>
26
27using namespace nawa;
28using namespace std;
29
30struct request::GPC::Data {
31 Source source;
32 std::unordered_multimap<std::string, std::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
54string request::GPC::operator[](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
62vector<string> request::GPC::getVector(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(string const& gpcVar) const {
72 return data->dataMap.count(gpcVar);
73}
74
75unordered_multimap<string, string> const& request::GPC::getMultimap() const {
76 return data->dataMap;
77}
78
79unordered_multimap<string, string>::const_iterator request::GPC::begin() const {
80 return data->dataMap.begin();
81}
82
83unordered_multimap<string, 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.
std::vector< std::string > getVector(std::string const &gpcVar) const
Definition: GPC.cpp:62
std::unordered_multimap< std::string, std::string >::const_iterator end() const
Definition: GPC.cpp:83
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::unordered_multimap< std::string, std::string > const & getMultimap() const
Definition: GPC.cpp:75
#define NAWA_DEFAULT_DESTRUCTOR_IMPL_WITH_NS(Namespace, Class)
Definition: macros.h:37
Definition: AppInit.h:31