SNode.C
Loading...
Searching...
No Matches
Request.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "express/Request.h"
21
22#include "web/http/server/Request.h"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#include "web/http/http_utils.h"
27
28#include <utility>
29
30#endif /* DOXYGEN_SHOULD_SKIP_THIS */
31
32namespace express {
33
34 Request::Request(const std::shared_ptr<web::http::server::Request>& request) noexcept
37 , url(request->url)
42 , headers(request->headers) // Do not move headers as they are possibly still needed in the source request
44 , body(std::move(request->body)) {
46 }
47
48 const std::string& Request::param(const std::string& id, const std::string& fallBack) {
49 return params.contains(id) ? params[id] : fallBack;
50 }
51
53 originalUrl = url;
54 url = httputils::url_decode(httputils::str_split_last(originalUrl, '?').first);
55 path = httputils::str_split_last(url, '/').first;
56 if (path.empty()) {
57 path = std::string("/");
58 }
59
60 return *this;
61 }
62
63 const std::string& Request::get(const std::string& key, int i) const {
64 return requestBase->get(key, i);
65 }
66
67 const std::string& Request::cookie(const std::string& key) const {
68 const std::map<std::string, std::string>::const_iterator it = cookies.find(key);
69
70 if (it != cookies.end()) {
71 return it->second;
72 }
73
74 return nullstr;
75 }
76
77 const std::string& Request::query(const std::string& key) const {
78 const std::map<std::string, std::string>::const_iterator it = queries.find(key);
79
80 if (it != queries.end()) {
81 return it->second;
82 }
83
84 return nullstr;
85 }
86
87} // namespace express
Request & extend()
Definition Request.cpp:52
const std::string & cookie(const std::string &key) const
Definition Request.cpp:67
const std::string & get(const std::string &key, int i=0) const
Definition Request.cpp:63
Request(const std::shared_ptr< web::http::server::Request > &request) noexcept
Definition Request.cpp:34
const std::string & query(const std::string &key) const
Definition Request.cpp:77
const std::string & param(const std::string &id, const std::string &fallBack="")
Definition Request.cpp:48