Load arguments from json

FossilOrigin-Name: ccb8abd657a30d321d48c9b8e3ae0cc5ae522a2b20053f996fbfbdc1aadeadb5
This commit is contained in:
nekobit 2022-10-25 14:03:24 +00:00
parent 57f39986a7
commit 6b8b8ebdb6
8 changed files with 24 additions and 16 deletions

View file

@ -63,7 +63,7 @@ namespace
switch (kind)
{
case MHD_GET_ARGUMENT_KIND:
request.get.insert({std::string_view(key, key_len), std::string(value, value_len)});
request.param.insert({ {key, key + key_len}, std::string(value, value_len)});
break;
case MHD_HEADER_KIND:
if (std::string_view(key, key_len) == "Content-Type")

View file

@ -24,7 +24,8 @@ using namespace HTTP;
Request::Request(Type type, const std::string url, unsigned mimetypes /* = HTTP::MIME::ANY */)
: type{type},
url{std::move(url)},
mimetypes{mimetypes}
mimetypes{mimetypes},
param{}
{}
bool Request::operator==(const Request& other) const

View file

@ -38,10 +38,10 @@ namespace HTTP
*/
using RequestArgs_t = std::vector<std::string>;
using RequestCallback_t = std::function<
std::optional<HTTP::Response const>( std::any&, const Request&, RequestArgs_t& )>;
std::optional<HTTP::Response const>( std::any&, Request&, RequestArgs_t& )>;
using RequestCallbackPair_t = std::pair<Request, RequestCallback_t>;
using KeyStrVarMap = std::map<std::string_view,
using KeyStrVarMap = std::map<std::string,
StringVariant>;
/**
@ -122,7 +122,10 @@ namespace HTTP
/** @remark Assuming this wasn't a propagated mimetype (OR mime) */
inline HTTP::MIME::Mimetypes get_mime() const noexcept
{ return static_cast<HTTP::MIME::Mimetypes>(mimetypes); }
KeyStrVarMap get;
/** @brief GET or POST parameters */
KeyStrVarMap param;
/** @see mime.h */
unsigned mimetypes;

View file

@ -27,9 +27,14 @@ std::string rjson::to_string(rjson::Document& doc)
return buf.GetString();
}
bool try_load_args_from_json(const HTTP::Request& req,
HTTP::RequestArgs_t& arg)
#include <iostream>
bool try_load_args_from_json(HTTP::Request& req)
{
rjson::Document doc;
doc.Parse(req.data.data());
for (auto itr = doc.MemberBegin(); itr != doc.MemberEnd(); ++itr)
{
req.param.insert({ itr->name.GetString(), std::string(itr->value.GetString()) });
}
return true;
}

View file

@ -47,5 +47,4 @@ namespace rjson
* @param arg Argument that will get modified (if no exception thrown)
* @return true if modified, false if the same.
*/
bool try_load_args_from_json(const HTTP::Request& req,
HTTP::RequestArgs_t& arg);
bool try_load_args_from_json(HTTP::Request& req);

View file

@ -37,11 +37,11 @@ using namespace Protocol;
using namespace std::string_literals;
HTTP::Response Route::MastoAPI::create_app(std::any& args,
const HTTP::Request& req,
HTTP::RequestArgs_t& arg)
HTTP::Request& req,
const HTTP::RequestArgs_t& arg)
{
DESTRUCT_WORMHOLE_ARGS(args);
try_load_args_from_json(req, arg);
try_load_args_from_json(req);
Logger::instance() << req.data.data();

View file

@ -26,8 +26,8 @@ namespace Route
namespace MastoAPI
{
HTTP::Response create_app(std::any& args,
const HTTP::Request& req,
HTTP::RequestArgs_t& arg);
HTTP::Request& req,
const HTTP::RequestArgs_t& arg);
}
}

View file

@ -72,7 +72,7 @@ HTTP::Response Route::webfinger(std::any& args, const HTTP::Request& req, const
try
{
resource = std::move(req.get.at("resource").string());
resource = std::move(req.param.at("resource").string());
}
catch (const std::out_of_range& err) {
return HTTP::Error::make_json_error_response("Missing resource parameter", HTTP::Code::BAD_REQUEST);