Response value

FossilOrigin-Name: 076a1107f3339a8894486d41208d911529615b66558f23326a53c5c23ff8ca1f
This commit is contained in:
nekobit 2022-10-06 22:56:19 +00:00
parent b149674924
commit 5f77d0705c
7 changed files with 48 additions and 17 deletions

View file

@ -11,6 +11,7 @@ add_executable(wormhole src/main.cpp
src/http/microhttpd_server.cpp
src/http/request.cpp
src/http/response.cpp
src/http/mime.cpp
src/config/config_http.cpp
src/config/config_db.cpp
src/config/config_loader.cpp

View file

@ -42,7 +42,7 @@ namespace
else
return HTTP::Request::Type::UNSUPPORTED;
}
enum MHD_Result new_connection(void* cls,
MHD_Connection* conn,
const char* url,
@ -66,8 +66,9 @@ namespace
(void*)(resp->data.data()),
MHD_RESPMEM_MUST_COPY);
MHD_add_response_header(response, "Content-Type", resp->minetype);
ret = MHD_queue_response(conn, MHD_HTTP_OK, response);
MHD_add_response_header(response, "Content-Type", resp->mimetype);
// HTTP::Code works for MHD already since it's just int's that represent the HTTP response codes
ret = MHD_queue_response(conn, resp->code, response);
MHD_destroy_response(response);

27
src/http/mime.cpp Normal file
View file

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

View file

@ -18,16 +18,14 @@
#pragma once
#include <string_view>
namespace HTTP
{
namespace MIME
{
constexpr std::string_view HTML = "text/html";
constexpr std::string_view CSS = "text/css";
constexpr std::string_view JSON = "application/json";
constexpr std::string_view JSON_JD = "application/ld+json";
constexpr std::string_view XML = "application/xml";
extern const char* HTML; // = "text/html"
extern const char* CSS; // = "text/css"
extern const char* JSON; // = "application/json"
extern const char* JSON_LD; // = "application/ld+json"
extern const char* XML; // = "application/xml"
}
}

View file

@ -21,8 +21,12 @@
using namespace HTTP;
Response::Response(std::vector<unsigned char> data)
: data{std::move(data)}
Response::Response(std::vector<unsigned char> data,
const char* mimetype,
HTTP::Code code /* = HTTP::Code::OK*/)
: data{std::move(data)},
mimetype{mimetype},
code{code}
{}

View file

@ -29,14 +29,16 @@ namespace HTTP
public:
/** Initialize from vector of unsigned chars **/
Response(std::vector<unsigned char> data,
std::string mimetype,
HTTP::Code code);
const char* mimetype,
HTTP::Code code = HTTP::Code::OK);
/** Initialize from string */
Response(const std::string& str);
~Response() = default;
const std::vector<unsigned char> data;
const std::string mimetype;
const char* mimetype;
/// @see responsecode.h
HTTP::Code code;
};
}

View file

@ -130,8 +130,6 @@ int start_wormhole()
} },
});
Logger::instance() << std::to_string(database->get_user("test0").id);
// Start the server!
server->start();