Insert get values

FossilOrigin-Name: 77ad58c567d4daf74172c85719903614b908aa176cddf20f2c24abf917c6e417
This commit is contained in:
nekobit 2022-10-07 00:34:57 +00:00
parent 5f77d0705c
commit 739a6c6e78
7 changed files with 138 additions and 18 deletions

View file

@ -12,6 +12,7 @@ add_executable(wormhole src/main.cpp
src/http/request.cpp
src/http/response.cpp
src/http/mime.cpp
src/http/header.cpp
src/config/config_http.cpp
src/config/config_db.cpp
src/config/config_loader.cpp

23
src/http/header.cpp Normal file
View file

@ -0,0 +1,23 @@
/*
* Wormhole - Federated social network
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "header.h"
using namespace HTTP;

41
src/http/header.h Normal file
View file

@ -0,0 +1,41 @@
/*
* Wormhole - Federated social network
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <string>
#include <optional>
#include <utility>
namespace HTTP
{
/** @brief A struct with all the get values that can be used */
struct GetValues
{
GetValues() {};
std::optional<std::string> resource;
std::optional<int> key;
// Setters
inline void set_resource(const decltype(resource)::value_type& _)
{ resource = _; }
inline void set_key(decltype(key)::value_type _)
{ key = _; }
};
}

View file

@ -43,6 +43,22 @@ namespace
return HTTP::Request::Type::UNSUPPORTED;
}
// Iterator function for POST, GET, etc.
enum MHD_Result key_val_iterator(void* cls, MHD_ValueKind kind,
const char* key, size_t key_len,
const char* value, size_t value_len)
{
HTTP::Request& request = *reinterpret_cast<HTTP::Request*>(cls);
if ((kind & MHD_GET_ARGUMENT_KIND) == kind)
{
request.get.insert({std::string_view(key, key_len), std::string(value, value_len)});
}
return MHD_YES;
}
enum MHD_Result new_connection(void* cls,
MHD_Connection* conn,
const char* url,
@ -58,9 +74,13 @@ namespace
// Create generic request from args
HTTP::Request req{str_to_method(method), url};
MHD_get_connection_values_n(conn, MHD_GET_ARGUMENT_KIND, key_val_iterator, &req);
// Call response, ensure that the request above is proprogated well!
std::optional<HTTP::Response> resp = that->handle_request(req);
// TODO Slow to copy each time... Do alternative here.
// TODO URGENT Slow to copy each time... Do alternative here.
if (!resp) return MHD_NO;
response = MHD_create_response_from_buffer(resp->data.size(),
(void*)(resp->data.data()),

View file

@ -26,6 +26,7 @@
#include <vector>
#include <unordered_map>
#include "response.h"
#include "header.h"
namespace HTTP
{
@ -39,6 +40,9 @@ namespace HTTP
std::optional<HTTP::Response const>( std::any& args, const RequestArgs_t& )>;
using RequestCallbackPair_t = std::pair<Request, RequestCallback_t>;
using KeyStrVarMap = std::map<std::string_view,
StringVariant>;
/**
* @brief A HTTP Request structure
*
@ -106,24 +110,10 @@ namespace HTTP
inline Type get_type() const noexcept { return type; }
// For consistancy...
inline const std::string& get_url() const noexcept { return url; }
KeyStrVarMap get;
private:
Type type;
std::string url;
};
}
// Hashmap
template <>
struct std::hash<HTTP::Request>
{
// This does nothing
std::size_t operator()(const HTTP::Request& req) const
{
HTTP::Request::Type type;
std::hash<std::string> url_h;
return static_cast<unsigned>(type) ^ (url_h(req.get_url()) + 0x9e3779b9 +
(static_cast<unsigned>(type) << 6) +
(static_cast<unsigned>(type) >> 2));
}
};

View file

@ -20,14 +20,17 @@
#include <string>
#include <vector>
#include <string_view>
#include <map>
#include "responsecode.h"
#include "type/stringvariant.h"
namespace HTTP
{
class Response
{
public:
/** Initialize from vector of unsigned chars **/
/** Initialize from vector of unsigned chars */
Response(std::vector<unsigned char> data,
const char* mimetype,
HTTP::Code code = HTTP::Code::OK);

42
src/type/stringvariant.h Normal file
View file

@ -0,0 +1,42 @@
/*
* Wormhole - Federated social network
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <utility>
#include <string>
/**
* @brief Little helper class which wraps around a string
*
* Mainly used for the GET/POST value maps
*/
class StringVariant
{
public:
StringVariant(const std::string str) : str{std::move(str)} {}
~StringVariant() = default;
inline unsigned long as_unsigned_long() const { return std::stoul(str); }
inline long as_long() const { return std::stol(str); }
inline int as_int() const { return std::stoi(str); }
inline float as_float() const { return std::stof(str); }
inline double as_double() const { return std::stof(str); }
inline const std::string& string() const { return str; }
private:
const std::string str;
};