Request and response from HTTP

FossilOrigin-Name: d6717095c67fcfb2fcd120f16d392929ef8872c210fb55d2b393ae99c3d21177
This commit is contained in:
nekobit 2022-09-30 17:55:25 +00:00
parent a68848cdba
commit 23c6cce5a3
8 changed files with 77 additions and 7 deletions

View file

@ -28,6 +28,7 @@ set(sources src/main.cpp
src/http/httpserver.cpp
src/http/microhttpd_server.cpp
src/http/request.cpp
src/http/response.cpp
src/config/config_loader.cpp
src/logger.cpp)

View file

@ -17,6 +17,7 @@
*/
#include "httpserver.h"
#include "http/request.h"
#include <cstdint>
using namespace HTTP;
@ -31,8 +32,12 @@ void Server::start()
void Server::stop()
{}
void Server::map_requests(std::initializer_list<HTTP::Request> requests)
void Server::map_requests(std::initializer_list<RequestCallbackPair_t> requests)
{
for (auto& req: requests)
{
req_map.insert(req);
}
return;
}

View file

@ -23,6 +23,7 @@
#include <unordered_map>
#include <utility>
#include "request.h"
#include "response.h"
namespace HTTP
{
@ -56,9 +57,19 @@ namespace HTTP
*/
void map_requests(std::initializer_list<RequestCallbackPair_t> requests);
/**
* @brief Handles a crafted HTTP Request
*
* Calls a function if the url matches, or it calls the custom error provided
*
* @param request The request to check
* @return An HTTP response which contains the information
*/
const HTTP::Response handle_request(const HTTP::Request& request);
inline uint16_t get_port() const noexcept { return port; }
protected:
RequestCallbackHashMap_t req_map;
std::vector<RequestCallbackPair_t> req_map;
private:
/** Port number, should be set once */

View file

@ -21,6 +21,7 @@
#include <cstdint>
#include <cstring>
#include <microhttpd.h>
#include "http/response.h"
using namespace HTTP;
@ -36,12 +37,13 @@ namespace
void** con_cls)
{
MicroHttpdServer* that = reinterpret_cast<MicroHttpdServer*>(that);
const char* page = "<html><body>Hello from Wormhole!</body></html>";
MHD_Response* response;
enum MHD_Result ret;
// Create generic request from args
HTTP::Request req{HTTP::Request::Type::POST, url};
HTTP::Response resp = that->handle_request(req);
;
response = MHD_create_response_from_buffer(strlen (page), (void *) page,
MHD_RESPMEM_PERSISTENT);

View file

@ -23,11 +23,10 @@ using namespace HTTP;
Request::Request(Type type, const std::string url)
: type{type},
callback{},
url{std::move(url)}
{}
bool Request::operator==(const Request& other)
bool Request::operator==(const Request& other) const
{
return get_url() == other.get_url() && get_type() == other.get_type();
}

View file

@ -35,7 +35,6 @@ namespace HTTP
using RequestArgs_t = std::vector<std::string>;
using RequestCallback_t = std::function<void( const RequestArgs_t& )>;
using RequestCallbackPair_t = std::pair<Request, RequestCallback_t>;
using RequestCallbackHashMap_t = std::unordered_map<Request, RequestCallback_t>;
/**
* @brief A HTTP Request structure
@ -80,7 +79,7 @@ namespace HTTP
*
* @param other To compare against
*/
bool operator==(const Request& other);
bool operator==(const Request& other) const;
/**
* Compares a request with another requests, and expands ':' to arguments

24
src/http/response.cpp Normal file
View file

@ -0,0 +1,24 @@
/*
* 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 "response.h"
using namespace HTTP;
Response::Response()
{}

29
src/http/response.h Normal file
View file

@ -0,0 +1,29 @@
/*
* 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
namespace HTTP
{
class Response
{
public:
Response();
~Response() = default;
}
}