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.
132 lines
2.7 KiB
132 lines
2.7 KiB
2 years ago
|
/* 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 _location_ response field.
|
||
|
*
|
||
|
* \param location Location URL.
|
||
|
*/
|
||
|
void location(const char *location)
|
||
|
{
|
||
|
if (res->location)
|
||
|
free(res->location);
|
||
|
res->location = strdup(location);
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
* \brief Sets _Content-Type_ response field.
|
||
|
*
|
||
|
* \param content_type Response content type.
|
||
|
*/
|
||
|
void content_type(const char *content_type)
|
||
|
{
|
||
|
if (res->content_type)
|
||
|
free(res->content_type);
|
||
|
res->content_type = strdup(content_type);
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
* \brief Sets cookie field.
|
||
|
*
|
||
|
* Adds new cookie field to key-value storage.
|
||
|
* If you need to set cookie parameters such as lifetime,
|
||
|
* place it to val.
|
||
|
*
|
||
|
* \param key Cookie field key.
|
||
|
* \param val Cookie field value.
|
||
|
*/
|
||
|
void cookie(const char *key, const char *val)
|
||
|
{
|
||
|
rpd_keyval_insert(&res->cookie, key, val);
|
||
|
}
|
||
|
|
||
|
/*!
|
||
|
* \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
|