C and C++ web framework.
http://rapida.vilor.one/docs
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.0 KiB
102 lines
2.0 KiB
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
|
/* Copyright 2022 Ivan Polyakov */ |
|
|
|
#ifndef RAPIDA_RESPONSE_HXX_ENTRY |
|
#define RAPIDA_RESPONSE_HXX_ENTRY |
|
|
|
#include "response.h" |
|
#include <stdlib.h> |
|
#include <string.h> |
|
|
|
#ifdef EXTENSIONS_INJA |
|
#include <nlohmann/json.hpp> |
|
#endif |
|
|
|
namespace rpd { |
|
/*! |
|
* \brief C++ response wrapper. |
|
*/ |
|
class Response { |
|
public: |
|
/*! |
|
* \brief Constructor. |
|
* |
|
* Creates C++ wrapper from original C response. |
|
* |
|
* \param res C response implementation. |
|
*/ |
|
Response(rpd_res *res) |
|
{ |
|
this->res = res; |
|
} |
|
|
|
/*! |
|
* \brief Sets response status code. |
|
* |
|
* See rpd_res_statuses enumeration. |
|
* |
|
* \param status Status code. |
|
*/ |
|
void status(rpd_res_statuses status) |
|
{ |
|
res->status = status; |
|
} |
|
|
|
/*! |
|
* \brief Sets response status code. |
|
* |
|
* Sets numbered status code. |
|
* May be useful when needed status code is |
|
* unavailable in rpd_res_status. |
|
* |
|
* If not, please, use enumeration. |
|
* |
|
* \param status Status code. |
|
*/ |
|
void status(long status) |
|
{ |
|
res->status = static_cast<rpd_res_statuses>(status); |
|
} |
|
|
|
/*! |
|
* \brief Sets response header. |
|
* |
|
* \param key Header key. |
|
* \param value Header value |
|
* |
|
* \return Status code. 0 is success. |
|
*/ |
|
int header(const char *key, const char *value); |
|
|
|
/*! |
|
* \brief Sets response body. |
|
* |
|
* \param body Response body. |
|
*/ |
|
void body(const char *body) |
|
{ |
|
if (res->body) |
|
free(res->body); |
|
res->body = strdup(body); |
|
} |
|
|
|
#ifdef EXTENSIONS_INJA |
|
/*! |
|
* \brief Render data to HTML template. |
|
* \param path Path to HTML template relative to dist location. |
|
* \param data Template data to interpolate. |
|
*/ |
|
void render(const char *path, nlohmann::json data); |
|
#endif |
|
|
|
/*! |
|
* \brief Destructor. |
|
*/ |
|
virtual ~Response() { } |
|
|
|
private: |
|
rpd_res *res; //< Composition with C implementation. |
|
}; |
|
} |
|
|
|
#endif // RAPIDA_RESPONSE_HXX_ENTRY
|
|
|