Request types

FossilOrigin-Name: 3848018170493f77b60835e0bbc41ad546b319363f9181517f7408c94dba4ec3
This commit is contained in:
nekobit 2022-10-24 15:13:30 +00:00
parent 6fa530814c
commit eba0a88769
7 changed files with 60 additions and 17 deletions

View file

@ -19,6 +19,8 @@
#include <cstdint>
#include <cstring>
#include <array>
#include <iostream>
#include <string_view>
#include <utility>
#include "microhttpd_server.h"
#include "http/mime.h"
@ -43,15 +45,27 @@ namespace
}
// 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)
enum MHD_Result gcv_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)
switch (kind)
{
case MHD_GET_ARGUMENT_KIND:
request.get.insert({std::string_view(key, key_len), std::string(value, value_len)});
break;
case MHD_HEADER_KIND:
if (std::string_view(key, key_len) == "Content-Type")
{
request.mimetypes = MIME::str_to_mime({value, value_len});
}
break;
default:
// Unhandled
break;
}
return MHD_YES;
@ -73,8 +87,14 @@ namespace
// Create generic request from args
HTTP::Request req{str_to_method(method), url};
// Maps to string_view
req.data = { upload_data, *upload_data_size };
MHD_get_connection_values_n(conn, MHD_GET_ARGUMENT_KIND, key_val_iterator, &req);
MHD_get_connection_values_n(conn,
static_cast<MHD_ValueKind>(
MHD_GET_ARGUMENT_KIND | MHD_HEADER_KIND),
gcv_iterator,
&req);
that->log_request(req);

View file

@ -21,6 +21,7 @@
#include <any>
#include <functional>
#include <string>
#include <string_view>
#include <utility>
#include <optional>
#include <vector>
@ -37,7 +38,7 @@ namespace HTTP
*/
using RequestArgs_t = std::vector<std::string>;
using RequestCallback_t = std::function<
std::optional<HTTP::Response const>( std::any&, const Request&, const RequestArgs_t& )>;
std::optional<HTTP::Response const>( std::any&, const Request&, RequestArgs_t& )>;
using RequestCallbackPair_t = std::pair<Request, RequestCallback_t>;
using KeyStrVarMap = std::map<std::string_view,
@ -122,12 +123,14 @@ namespace HTTP
inline HTTP::MIME::Mimetypes get_mime() const noexcept
{ return static_cast<HTTP::MIME::Mimetypes>(mimetypes); }
KeyStrVarMap get;
/** @see mime.h */
unsigned mimetypes;
std::string_view data;
private:
Type type;
/** @remark Used as a plain old path too. */
std::string url;
/** @see mime.h */
unsigned mimetypes;
};
}

View file

@ -26,3 +26,10 @@ std::string rjson::to_string(rjson::Document& doc)
// Make copy into string on return
return buf.GetString();
}
bool try_load_args_from_json(const HTTP::Request& req,
HTTP::RequestArgs_t& arg)
{
return true;
}

View file

@ -17,6 +17,7 @@
*/
#pragma once
#include "http/request.h"
#define RAPIDJSON_HAS_STDSTRING 1
#define RAPIDJSON_NAMESPACE rjson
#define RAPIDJSON_NAMESPACE_BEGIN namespace rjson {
@ -26,16 +27,25 @@
#include <rapidjson/rapidjson.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include "http/httpserver.h"
#define RJSON_OPTIONAL(ne, o) if (!(ne).empty()) { o; }
namespace rjson
{
std::string to_string(rjson::Document& doc);
// Bad, ignore this
/* inline Value& move(std::string str, Document::AllocatorType& a) */
/* { */
/* return Value(str.c_str(), a).Move(); */
/* } */
}
/**
* @brief Fix-up arguments from the JSON data in request
*
* This is used by Pleroma and some of the frontends. They send JSON
* instead of standard POST requests, but for compliance, this isn't
* forced to all POST requests (i.e. Webfinger)
*
* @param req Request to get from
* @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);

View file

@ -38,10 +38,10 @@ using namespace std::string_literals;
HTTP::Response Route::MastoAPI::create_app(std::any& args,
const HTTP::Request& req,
const HTTP::RequestArgs_t& arg)
HTTP::RequestArgs_t& arg)
{
DESTRUCT_WORMHOLE_ARGS(args);
try_load_args_from_json(req, arg);
return HTTP::Response{ "{}", HTTP::MIME::JSON };
}

View file

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

View file

@ -27,6 +27,7 @@
#include "http/httpserver.h"
#include "http/error_response.h"
#include "http/request.h"
#include "apps.h"
#include "types/user.h"
#include "logger.h"
#include "jsonhelper.h"
@ -47,4 +48,6 @@ HTTP::Response Route::MastoAPI::test(std::any& args,
void MastoAPI::init_masto_api(HTTP::Server* server)
{
server->add_route({{HTTP::Request::Type::GET, "/api/v1/wormhole_test"}, Route::MastoAPI::test});
init_masto_apps(server);
}