Frontend stuff, fix request bugs, fcgi init

FossilOrigin-Name: c50619fe310abc08cf7a4753fd36542197373c27118d9378fa7265fb84e64d32
This commit is contained in:
nekobit 2022-10-30 06:14:37 +00:00
parent 6bef9b6d9a
commit 96a8adbb20
13 changed files with 125 additions and 13 deletions

View file

@ -42,6 +42,7 @@ add_module(WEBFINGER)
add_module(ACTIVITYPUB)
add_module(OAUTH)
add_module(MASTO_API)
add_module(FCGI)
# General includes
include(CTest)
@ -101,6 +102,7 @@ CACHE INTERNAL "")
include_directories(wormhole ${WORMHOLE_INCLUDE_DIRS})
add_subdirectory(src/protocol)
add_subdirectory(src/frontends)
# automated tests
if(BUILD_TESTING)
@ -113,6 +115,7 @@ target_link_libraries(wormhole wormhole_lib
wormhole_host_meta_module
wormhole_oauth_module
wormhole_masto_api_module
wormhole_fcgi_module
${CMAKE_DL_LIBS}
${LIBMICROHTTPD_LIBRARIES}
${SQLite3_LIBRARIES}

View file

@ -21,7 +21,7 @@
#include <string>
#include <yaml-cpp/yaml.h>
#include <cstdint>
#include "frontend/types.h"
#include "frontends/types.h"
#include "database/database.h"
namespace Config

View file

@ -45,6 +45,7 @@ void ConfigLoader::load_config(const std::filesystem::path& path)
Config::load_http(config.http, node);
Config::load_database(config.database, node);
Config::load_instance(config.instance, node);
Config::load_frontend(config.frontend, node);
}
catch (const YAML::BadFile& err)
{

View file

@ -24,6 +24,7 @@
#include "config_db.h"
#include "config_http.h"
#include "config_instance.h"
#include "config_frontend.h"
namespace Config
{
@ -43,6 +44,7 @@ namespace Config
Config::HTTP http;
Config::Database database;
Config::Instance instance;
Config::Frontend frontend;
} config;
};

View file

@ -0,0 +1,3 @@
if (MODULE_FCGI)
add_subdirectory(fcgi)
endif()

View file

@ -0,0 +1,4 @@
add_library(wormhole_fcgi_module
fcgi.cpp)
include_directories(wormhole_fcgi_module ${WORMHOLE_INCLUDE_DIRS})
target_link_libraries(wormhole_fcgi_module wormhole_lib)

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/>.
*/
#include <iostream>
#include <unistd.h>
#include "fcgi.h"
#include "config/config_loader.h"
HTTP::Response Route::fcgi_request(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg)
{
using namespace std::string_literals;
return HTTP::Response{ "Hello FCGI"s + req.get_url(), HTTP::MIME::HTML };
}
void Frontend::init_fcgi(HTTP::Server* server)
{
if (Config::instance().config.frontend.type != Type::FCGI)
return;
// TODO Use bitmasks for this, fucking hell...
server->add_route({{HTTP::Request::Type::GET, "/:"}, Route::fcgi_request});
server->add_route({{HTTP::Request::Type::PUT, "/:"}, Route::fcgi_request});
server->add_route({{HTTP::Request::Type::POST, "/:"}, Route::fcgi_request});
server->add_route({{HTTP::Request::Type::DELETE, "/:"}, Route::fcgi_request});
server->add_route({{HTTP::Request::Type::PATCH, "/:"}, Route::fcgi_request});
server->add_route({{HTTP::Request::Type::OTHER, "/:"}, Route::fcgi_request});
}

31
src/frontends/fcgi/fcgi.h Normal file
View file

@ -0,0 +1,31 @@
/*
* 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 "http/httpserver.h"
namespace Route
{
HTTP::Response fcgi_request(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg);
}
namespace Frontend
{
void init_fcgi(HTTP::Server* server);
}

View file

@ -95,11 +95,13 @@ std::optional<RequestArgs_t> Request::match_get_args(Request& other)
}
// Compare rest of n
if (url.length() != n_last)
if (url.length() != n_last && url[n_last + 1] != '\0')
{
if (!std::equal(url.begin() + n_last + 1,
url.begin() + url.length() - 1,
other_url.begin() + curr))
return std::nullopt;
}
return args;
}

View file

@ -49,6 +49,9 @@
#ifdef MODULE_MASTO_API
#include "protocol/masto-api/mastoapi.h"
#endif // MODULE_MASTO_API
#ifdef MODULE_FCGI
#include "frontends/fcgi/fcgi.h"
#endif // MODULE_FCGI
void init_routes(const std::unique_ptr<HTTP::Server>& server)
{
@ -67,6 +70,10 @@ void init_routes(const std::unique_ptr<HTTP::Server>& server)
#ifdef MODULE_MASTO_API
Protocol::MastoAPI::init_masto_api(server.get());
#endif // MODULE_MASTO_API
#ifdef MODULE_FCGI
Frontend::init_fcgi(server.get());
#endif // MODULE_FCGI
}
int start_wormhole()

View file

@ -16,20 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "instance/instance.h"
#include "jsonhelper.h"
#include <stdexcept>
#include <utility>
#include "route_args.h"
#include "http/mime.h"
#include "http/response.h"
#include "http/responsecode.h"
#include "types/user.h"
#include "instance/instance.h"
#include "hostmeta.h"
#include "http/httpserver.h"
#include "http/request.h"
#include "logger.h"
#include "http/error_response.h"
using namespace Protocol;

26
src/types/app.cpp Normal file
View file

@ -0,0 +1,26 @@
/*
* 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 "app.h"
void App::generate_tokens(Random& rng)
{
constexpr size_t token_size = 48;
client_id = rng.generate_token(token_size);
client_secret = rng.generate_token(token_size);
}