Fix response constructors

FossilOrigin-Name: c77f391407ef1d875a3c5c1c6fe54291b5fc14b3109f192350f8f7760abbb1ba
This commit is contained in:
nekobit 2022-10-07 00:54:49 +00:00
parent 739a6c6e78
commit a45c13f4b5
11 changed files with 33 additions and 83 deletions

View file

@ -12,7 +12,6 @@ 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

View file

@ -1,23 +0,0 @@
/*
* 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;

View file

@ -1,41 +0,0 @@
/*
* 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

@ -59,7 +59,7 @@ std::optional<HTTP::Response const> Server::handle_request(HTTP::Request &reques
auto args = reqs.first.match_get_args(request);
if (args)
{
return reqs.second(cb_args, *args);
return reqs.second(cb_args, request, *args);
}
}
return std::nullopt;

View file

@ -18,10 +18,8 @@
#include "http/mime.h"
using namespace HTTP::MIME;
static const char* HTML = "text/html";
static const char* CSS = "text/css";
static const char* JSON = "application/json";
static const char* JSON_LD = "application/ld+json";
static const char* XML = "application/xml";
const char* HTTP::MIME::HTML = "text/html";
const char* HTTP::MIME::CSS = "text/css";
const char* HTTP::MIME::JSON = "application/json";
const char* HTTP::MIME::JSON_LD = "application/ld+json";
const char* HTTP::MIME::XML = "application/xml";

View file

@ -26,7 +26,6 @@
#include <vector>
#include <unordered_map>
#include "response.h"
#include "header.h"
namespace HTTP
{
@ -37,7 +36,7 @@ namespace HTTP
*/
using RequestArgs_t = std::vector<std::string>;
using RequestCallback_t = std::function<
std::optional<HTTP::Response const>( std::any& args, const RequestArgs_t& )>;
std::optional<HTTP::Response const>( std::any&, const Request&, const RequestArgs_t& )>;
using RequestCallbackPair_t = std::pair<Request, RequestCallback_t>;
using KeyStrVarMap = std::map<std::string_view,

View file

@ -17,6 +17,7 @@
*/
#include <utility>
#include "http/mime.h"
#include "response.h"
using namespace HTTP;
@ -29,7 +30,15 @@ Response::Response(std::vector<unsigned char> data,
code{code}
{}
Response::Response(const std::string& str)
: data{str.begin(), str.end()}
: Response(str, HTTP::MIME::JSON)
{}
Response::Response(const std::string& str,
const char* mimetype,
HTTP::Code code /* = HTTP::Code::OK*/)
: data{str.begin(), str.end()},
mimetype{mimetype},
code{code}
{}

View file

@ -37,6 +37,9 @@ namespace HTTP
/** Initialize from string */
Response(const std::string& str);
Response(const std::string& str,
const char* mimetype,
HTTP::Code code = HTTP::Code::OK);
~Response() = default;
const std::vector<unsigned char> data;

View file

@ -87,7 +87,7 @@ int start_wormhole()
// TODO Move to another file
server->add_routes({
{ {HTTP::Request::Type::GET, "/"}, [](std::any& args, const HTTP::RequestArgs_t& arg){
{ {HTTP::Request::Type::GET, "/"}, [](std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg){
// TODO Perl static script to just do this and put it in a header file
return HTTP::Response{R"html(
<!DOCTYPE html>
@ -121,11 +121,11 @@ int start_wormhole()
</html>
)html"};
} },
{ {HTTP::Request::Type::GET, "/test"}, [](std::any& args, const HTTP::RequestArgs_t& arg){
{ {HTTP::Request::Type::GET, "/test"}, [](std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg){
return HTTP::Response{"<html><body>Hello, browser!</body></html>"};
} },
{ {HTTP::Request::Type::GET, "/welcome/:"}, [](std::any& args, const HTTP::RequestArgs_t& arg){
{ {HTTP::Request::Type::GET, "/welcome/:"}, [](std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg){
return HTTP::Response{std::string("<html><body>Hello, ") + arg.at(0) + std::string("!</body></html>")};
} },
});

View file

@ -21,10 +21,13 @@
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <utility>
#include "http/mime.h"
#include "http/response.h"
#include "http/responsecode.h"
#include "webfinger.h"
#include "http/httpserver.h"
#include "http/request.h"
#include "logger.h"
using namespace Protocol;
@ -36,6 +39,7 @@ namespace
std::string type;
std::string tmpl;
};
rapidjson::Value make_resource(Resource&& res, rapidjson::Document::AllocatorType& a)
{
using Value = rapidjson::Value;
@ -50,12 +54,14 @@ namespace
}
// TODO a lot
HTTP::Response Route::webfinger(std::any& args, const HTTP::RequestArgs_t& arg)
HTTP::Response Route::webfinger(std::any& args, const HTTP::Request& req, const HTTP::RequestArgs_t& arg)
{
rapidjson::Document root(rapidjson::kObjectType);
rapidjson::Document::AllocatorType& a = root.GetAllocator();
rapidjson::Value links(rapidjson::kArrayType);
// Logger::instance() << req.get.at("resource").string();
// Add profile page
links.PushBack(make_resource({"https://localhost/users/",
"http://webfinger.net/rel/profile-page",
@ -80,7 +86,7 @@ HTTP::Response Route::webfinger(std::any& args, const HTTP::RequestArgs_t& arg)
rapidjson::Writer<rapidjson::StringBuffer> writer(buf);
root.Accept(writer);
return { buf.GetString() };
return HTTP::Response( buf.GetString(), HTTP::MIME::JSON );
}
void Webfinger::init_webfinger(HTTP::Server* server)

View file

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