Compare commits

..

No commits in common. '8fdac02a42b92e06679f32f0cd1b1a22dfac8967' and '666d602b98a580b2d34ad87d715242b107760b97' have entirely different histories.

  1. 5
      README.md
  2. 34
      c/keyval.c
  3. 71
      c/response.c
  4. 61
      c/utils.c
  5. 17
      c/utils.h
  6. 12
      config.mk
  7. 12
      cxx/Request.cxx
  8. 32
      cxx/Response.cxx
  9. 8
      examples/c/example.c
  10. 12
      examples/c/minimal.c
  11. 2
      examples/cxx/example.cxx
  12. 3
      examples/cxx/minimal.cxx
  13. 18
      include/KeyVal.hxx
  14. 21
      include/Request.hxx
  15. 52
      include/Response.hxx
  16. 18
      include/keyval.h
  17. 3
      include/request.h
  18. 4
      include/response.h
  19. 57
      servers/fcgi.c
  20. 17
      servers/tcp.c
  21. 2
      tests/app.cxx
  22. 18
      tests/keyval.cxx

5
README.md

@ -25,6 +25,11 @@ make install clean
``` ```
Run `make help` to see targets, flags and arguments. Run `make help` to see targets, flags and arguments.
Extensions
----------
* [inja](https://github.com/pantor/inja) (template rendering,
requires [nlohmann/json](https://github.com/nlohmann/json))
Examples Examples
-------- --------
See [examples](examples). See [examples](examples).

34
c/keyval.c

@ -3,6 +3,7 @@
#include "keyval.h" #include "keyval.h"
#include "utils.h" #include "utils.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -19,7 +20,6 @@ static int rpd_keyval_realloc(rpd_keyval *keyval, int capacity);
int rpd_keyval_init(rpd_keyval *keyval, int capacity) int rpd_keyval_init(rpd_keyval *keyval, int capacity)
{ {
keyval->size = keyval->capacity = 0; keyval->size = keyval->capacity = 0;
keyval->unique = 1;
if (capacity == 0) { if (capacity == 0) {
keyval->items = NULL; keyval->items = NULL;
@ -46,7 +46,7 @@ int rpd_keyval_insert(rpd_keyval *keyval, const char *key, const char *value)
} }
rpd_keyval_item *item = rpd_keyval_find(keyval, key); rpd_keyval_item *item = rpd_keyval_find(keyval, key);
if (item && keyval->unique) { if (item) {
free(item->key); free(item->key);
item->key = NULL; item->key = NULL;
if (item->val) { if (item->val) {
@ -76,36 +76,6 @@ rpd_keyval_item *rpd_keyval_find(const rpd_keyval *keyval, const char *key)
return NULL; return NULL;
} }
rpd_keyval_item **rpd_keyval_findall(const rpd_keyval *keyval, const char *key)
{
int i = 0, n = 0;
rpd_keyval_item **items = NULL;
while (i < keyval->size) {
if (!strcmp(keyval->items[i].key, key)) {
n++;
}
i++;
}
if (!n)
return NULL;
items = malloc(sizeof(rpd_keyval_item *) * (n + 1));
if (!items)
return NULL;
items[n] = NULL;
i = 0, n = 0;
while (i < keyval->size) {
if (!strcmp(keyval->items[i].key, key)) {
items[n] = keyval->items + i;
n++;
}
i++;
}
return items;
}
void rpd_keyval_cleanup(rpd_keyval *keyval) void rpd_keyval_cleanup(rpd_keyval *keyval)
{ {
if (keyval->items) { if (keyval->items) {

71
c/response.c

@ -6,25 +6,22 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
static size_t calc_res_status_sz(const rpd_res *res); static size_t calc_res_headers_sz(const rpd_res *res);
static size_t calc_res_headers_sz(const rpd_keyval *res);
void rpd_res_init(rpd_res *dest) void rpd_res_init(rpd_res *dest)
{ {
dest->status = rpd_res_st_ok; dest->status = rpd_res_st_ok;
dest->location = dest->content_type = NULL;
dest->body = NULL; dest->body = NULL;
rpd_keyval_init(&dest->headers, 5);
dest->headers.unique = 0; rpd_keyval_init(&dest->cookie, 0);
} }
int rpd_res_headers_str(char **dest, const rpd_res *src) int rpd_res_headers_str(char **dest, const rpd_res *src)
{ {
size_t size, i = 0; size_t size = calc_res_headers_sz(src);
char *ptr; char *ptr;
size = calc_res_headers_sz(&src->headers);
*dest = (char *) malloc(sizeof(char) * size); *dest = (char *) malloc(sizeof(char) * size);
if (!*dest) { if (!*dest) {
perror("malloc"); perror("malloc");
@ -32,13 +29,12 @@ int rpd_res_headers_str(char **dest, const rpd_res *src)
} }
ptr = *dest; ptr = *dest;
while (i < src->headers.size) { if (src->content_type) {
ptr += sprintf( ptr += sprintf(ptr, "Content-Type: %s\r\n", src->content_type);
ptr, }
"%s: %s\r\n",
src->headers.items[i].key, if (src->location) {
src->headers.items[i].val); ptr += sprintf(ptr, "Location: %s\r\n", src->location);
i++;
} }
return 0; return 0;
@ -48,9 +44,7 @@ int rpd_res_str(char **dest, const rpd_res *res)
{ {
size_t headers_size, size; size_t headers_size, size;
char *ptr, *headers; char *ptr, *headers;
size = headers_size = calc_res_headers_sz(&res->headers); size = headers_size = calc_res_headers_sz(res);
size += calc_res_status_sz(res);
if (res->body) if (res->body)
size += 2 + strlen(res->body); size += 2 + strlen(res->body);
@ -84,24 +78,25 @@ int rpd_res_str(char **dest, const rpd_res *res)
return 0; return 0;
} }
static size_t calc_res_status_sz(const rpd_res *res) static size_t calc_res_headers_sz(const rpd_res *res)
{ {
size_t size = 0; size_t size = 0;
size += strlen("Status: \r\n");
size += 3; /* plus status code */ size += strlen("Status: \r\n") + 3;
return size; if (res->location) {
size += strlen("Location: \r\n") + strlen(res->location);
} }
static size_t calc_res_headers_sz(const rpd_keyval *headers) if (res->content_type) {
{ size += strlen("Content-Type: \r\n") + strlen(res->content_type);
size_t size = 0, i = 0; }
while (i < headers->size) { if (res->cookie.size) {
size += strlen(headers->items[i].key); size += strlen("Set-Cookie: \r\n") * res->cookie.size;
size += 2; /* plus ": " */ for (int i = 0; i < res->cookie.size; i++) {
size += strlen(headers->items[i].val); rpd_keyval_item *item = res->cookie.items + i;
size += 2; /* plus CRLF */ size += strlen(item->key) + strlen(item->val);
i++; }
} }
return size; return size;
@ -111,12 +106,22 @@ void rpd_res_cleanup(rpd_res *res)
{ {
res->status = rpd_res_st_ok; res->status = rpd_res_st_ok;
if (res->location) {
free(res->location);
res->location = NULL;
}
if (res->content_type) {
free(res->content_type);
res->content_type = NULL;
}
if (res->body) { if (res->body) {
free(res->body); free(res->body);
res->body = NULL; res->body = NULL;
} }
if (res->headers.capacity) { if (res->cookie.capacity) {
rpd_keyval_cleanup(&res->headers); rpd_keyval_cleanup(&res->cookie);
} }
} }

61
c/utils.c

@ -2,7 +2,6 @@
/* Copyright 2022 Ivan Polyakov */ /* Copyright 2022 Ivan Polyakov */
#include "utils.h" #include "utils.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -44,63 +43,3 @@ char *rpd_strsep(char **str, const char *sep)
*str = end; *str = end;
return s; return s;
} }
const char *rpd_splitbyc(char **dest1, char **dest2, const char *src, const char sep)
{
const char *start = src, *end = src;
if (!src)
return 0;
while (*end) {
if (*end == sep) {
size_t len = end - start;
*dest1 = malloc(sizeof(char) * (len + 1));
if (!*dest1) {
perror("malloc");
return NULL;
}
memcpy(*dest1, start, len);
(*dest1)[len] = '\0';
end++;
len = strlen(start) - (end - start);
if (!len) {
*dest2 = NULL;
return 0;
}
*dest2 = malloc(sizeof(char) * (len + 1));
if ((!*dest2)) {
perror("malloc");
return NULL;
}
memcpy(*dest2, end, len);
(*dest2)[len + 1] = '\0';
return 0;
}
end++;
}
return 0;
}
void rpd_strerase(char *src, int nchars)
{
char *ptr = NULL;
size_t len = strlen(src);
if (!src)
return;
ptr = src + nchars;
while (*ptr != '\0') {
*(ptr - nchars) = *ptr;
ptr++;
}
ptr = src + len;
while (ptr >= (src + len) - nchars) {
*ptr = '\0';
ptr--;
}
}

17
c/utils.h

@ -10,21 +10,4 @@ char *rpd_strdup(const char *src);
char *rpd_strsep(char **str, const char *sep); char *rpd_strsep(char **str, const char *sep);
const char *rpd_splitbyc(char **dest1, char **dest2, const char *src, const char sep);
/*!
* \brief Erases part of the string.
*
* This function moves characters to the beginning of the string
* and inserts '\0' at the original position without reallocation.
*
* To erase characters not from beginning of the string,
* you can pass a pointer to the beginning of the desired
* part of the string.
*
* \param src String to erase.
* \param nchars Number of charecters to erase.
*/
void rpd_strerase(char *src, int nchars);
#endif /* RAPIDA_UTILS_H_ENTRY */ #endif /* RAPIDA_UTILS_H_ENTRY */

12
config.mk

@ -1,4 +1,4 @@
VERSION=0.4 VERSION=0.3.1
#arg Installation prefix #arg Installation prefix
PREFIX=/usr/local PREFIX=/usr/local
@ -31,6 +31,16 @@ ifeq ($(FCGI_SERVER), 1)
LDFLAGS += -lfcgi LDFLAGS += -lfcgi
endif endif
#flag Enable inja extension
EXTENSIONS_INJA ?= 0
#arg Dist path. Needed only if inja is enabled.
DIST_PATH = /var/www/html
ifneq ($(EXTENSIONS_INJA), 0)
CXXFLAGS+=-DEXTENSIONS_INJA -DDIST_PATH=\"$(DIST_PATH)\"
CXXSTD=-std=c++17
endif
#flag Multithread support #flag Multithread support
MT_ENABLED ?= 0 MT_ENABLED ?= 0
#arg Number of threads. 8 by default. #arg Number of threads. 8 by default.

12
cxx/Request.cxx

@ -1,12 +0,0 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright 2022 Ivan Polyakov */
#include "Request.hxx"
using namespace rpd;
const char *Request::header(const char *key) const
{
rpd_keyval_item *hi = rpd_keyval_find(&req->headers, key);
return hi ? hi->val : NULL;
}

32
cxx/Response.cxx

@ -3,12 +3,34 @@
#include "Response.hxx" #include "Response.hxx"
#ifdef EXTENSIONS_INJA
#include <inja/inja.hpp>
#endif
using namespace rpd; using namespace rpd;
int Response::header(const char *key, const char *value) #ifdef EXTENSIONS_INJA
void Response::render(const char *path, nlohmann::json data)
{ {
return rpd_keyval_insert( inja::Environment env;
&this->res->headers, inja::Template tpl;
key,
value); try {
std::string tplpath = DIST_PATH;
tplpath += path;
tpl = env.parse_template(tplpath);
} catch (inja::FileError &e) {
std::cerr << e.what() << std::endl;
return;
}
try {
std::string result = env.render(tpl, data);
body(result.c_str());
} catch (inja::RenderError &e) {
std::cerr << e.what() << std::endl;
status(rpd_res_st_internal_server_error);
return;
}
} }
#endif

8
examples/c/example.c

@ -28,7 +28,6 @@ static void products_handler(rpd_req *req, rpd_res *res, void *userdata)
int bufflen = strlen(body) + strlen(cat->val) + strlen(id->val); int bufflen = strlen(body) + strlen(cat->val) + strlen(id->val);
res->body = (char *) malloc(sizeof(char) * (bufflen + 1)); res->body = (char *) malloc(sizeof(char) * (bufflen + 1));
if (!res->body) { if (!res->body) {
perror("malloc");
res->status = rpd_res_st_internal_server_error; res->status = rpd_res_st_internal_server_error;
return; return;
} }
@ -38,7 +37,12 @@ static void products_handler(rpd_req *req, rpd_res *res, void *userdata)
res->status = rpd_res_st_ok; res->status = rpd_res_st_ok;
rpd_keyval_insert(&res->headers, "Content-Type", "text/html"); /*
* place all values in heap, please,
* because after call handler
* `req` and `res` will be freed
*/
res->content_type = strdup("text/html");
} }
int main() int main()

12
examples/c/minimal.c

@ -15,17 +15,15 @@ static void home_page_handler(rpd_req *req, rpd_res *res, void *userdata)
/* Check request method */ /* Check request method */
switch (req->method) { switch (req->method) {
case HEAD: case HEAD:
/* Process GET request */
res->status = rpd_res_st_ok; res->status = rpd_res_st_ok;
rpd_keyval_insert(&res->headers, "Content-Type", "text/plain");
break;
case GET:
res->status = rpd_res_st_ok;
rpd_keyval_insert(&res->headers, "Content-Type", "text/plain");
/* Please allocate body on the heap, /* Please allocate data on the heap,
* because after calling this handler * because after calling this handler
* Rapida will free it. * Rapida will free it all.
*/ */
res->content_type = strdup("text/plain");
case GET:
res->body = strdup("Hello World!"); res->body = strdup("Hello World!");
break; break;
default: default:

2
examples/cxx/example.cxx

@ -35,7 +35,7 @@ protected:
virtual void handle_head(const rpd::Request &req, rpd::Response &res) override virtual void handle_head(const rpd::Request &req, rpd::Response &res) override
{ {
res.status(rpd_res_st_ok); res.status(rpd_res_st_ok);
res.header("Content-Type", "text/html"); res.content_type("text/html");
} }
}; };

3
examples/cxx/minimal.cxx

@ -1,6 +1,7 @@
#include "../../include/rapida.hxx" #include "../../include/rapida.hxx"
#include "../../include/servers/tcp.h" #include "../../include/servers/tcp.h"
/* /*
* \brief Home page route handler. * \brief Home page route handler.
*/ */
@ -28,7 +29,7 @@ protected:
virtual void handle_head(const rpd::Request &req, rpd::Response &res) override virtual void handle_head(const rpd::Request &req, rpd::Response &res) override
{ {
res.status(rpd_res_st_ok); res.status(rpd_res_st_ok);
res.header("Content-Type", "text/plain"); res.content_type("text/plain");
} }
}; };

18
include/KeyVal.hxx

@ -36,24 +36,6 @@ public:
_keyval = keyval; _keyval = keyval;
} }
/*!
* \brief Is the storage unique?
*/
bool unique() const
{
return _keyval->unique;
}
/*!
* \brief Sets the uniqueness flag.
*
* \param is_unique Uniqueness flag.
*/
void unique(bool is_unique)
{
_keyval->unique = is_unique;
}
/*! /*!
* \brief Returns real key-value storage. * \brief Returns real key-value storage.
* *

21
include/Request.hxx

@ -69,23 +69,24 @@ public:
} }
/*! /*!
* \brief Gets the request header. * \brief Gets authorization string.
* *
* \params key Header key. * \return Authorization string.
*
* \return Found header or NULL.
*/ */
const char *header(const char *key) const; const char *authorization() const
{
return req->auth;
}
/*! /*!
* \brief Gets all request headers. * \brief Gets cookie string.
* *
* \return Key-value pairs. * \return Cookie string.
*/ */
KeyVal headers() const const char *cookie() const
{ {
return &req->headers; return req->cookie;
} };
/*! /*!
* \brief Gets request body content. * \brief Gets request body content.

52
include/Response.hxx

@ -8,6 +8,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef EXTENSIONS_INJA
#include <nlohmann/json.hpp>
#endif
namespace rpd { namespace rpd {
/*! /*!
* \brief C++ response wrapper. * \brief C++ response wrapper.
@ -55,14 +59,43 @@ public:
} }
/*! /*!
* \brief Sets response header. * \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.
* *
* \param key Header key. * Adds new cookie field to key-value storage.
* \param value Header value * If you need to set cookie parameters such as lifetime,
* place it to val.
* *
* \return Status code. 0 is success. * \param key Cookie field key.
* \param val Cookie field value.
*/ */
int header(const char *key, const char *value); void cookie(const char *key, const char *val)
{
rpd_keyval_insert(&res->cookie, key, val);
}
/*! /*!
* \brief Sets response body. * \brief Sets response body.
@ -76,6 +109,15 @@ public:
res->body = strdup(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. * \brief Destructor.
*/ */

18
include/keyval.h

@ -9,7 +9,7 @@
#ifndef RAPIDA_KEYVAL_H_ENTRY #ifndef RAPIDA_KEYVAL_H_ENTRY
#define RAPIDA_KEYVAL_H_ENTRY #define RAPIDA_KEYVAL_H_ENTRY
#include <unistd.h> #include "unistd.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -30,7 +30,6 @@ typedef struct {
rpd_keyval_item *items; /**< Key-value pairs array. */ rpd_keyval_item *items; /**< Key-value pairs array. */
int size; /**< Number of elements in rpd_keyval::items. */ int size; /**< Number of elements in rpd_keyval::items. */
int capacity; /**< Current capacity (allocated memory). */ int capacity; /**< Current capacity (allocated memory). */
int unique; /**< Unique flag */
} rpd_keyval; } rpd_keyval;
/*! /*!
@ -71,21 +70,6 @@ int rpd_keyval_insert(rpd_keyval *keyval, const char *key, const char *value);
*/ */
rpd_keyval_item *rpd_keyval_find(const rpd_keyval *keyval, const char *key); rpd_keyval_item *rpd_keyval_find(const rpd_keyval *keyval, const char *key);
/*!
* \brief Finds all key-value pair by key.
*
* Useful when storage is not unique.
* In request header fields, for example.
*
* Last item will be NULL.
*
* \param keyval Key-value storage instance.
* \param key Key to search.
*
* \return Found items or NULL.
*/
rpd_keyval_item **rpd_keyval_findall(const rpd_keyval *keyval, const char *key);
/*! /*!
* \brief Free key-val pairs and reset. * \brief Free key-val pairs and reset.
* *

3
include/request.h

@ -36,9 +36,10 @@ enum rpd_req_methods {
*/ */
typedef struct { typedef struct {
enum rpd_req_methods method; /**< Request method. */ enum rpd_req_methods method; /**< Request method. */
char *auth; /**< Authorization field. */
char *cookie; /**< Cookie field. */
char *body; /**< Body field. */ char *body; /**< Body field. */
rpd_url path; /**< Requested URL. */ rpd_url path; /**< Requested URL. */
rpd_keyval headers; /**< Request headers. */
rpd_keyval query; /**< Query. */ rpd_keyval query; /**< Query. */
rpd_keyval params; /**< Dynamic parameters. */ rpd_keyval params; /**< Dynamic parameters. */
} rpd_req; } rpd_req;

4
include/response.h

@ -86,8 +86,10 @@ enum rpd_res_statuses {
*/ */
typedef struct { typedef struct {
enum rpd_res_statuses status; /**< Response status code. */ enum rpd_res_statuses status; /**< Response status code. */
char *location; /**< Location field. */
char *content_type; /**< Content type. */
char *body; /**< Response body. */ char *body; /**< Response body. */
rpd_keyval headers; /**< Response headers. */ rpd_keyval cookie; /**< Set-Cookie fields. */
} rpd_res; } rpd_res;
/*! /*!

57
servers/fcgi.c

@ -2,11 +2,7 @@
/* Copyright 2022 Ivan Polyakov */ /* Copyright 2022 Ivan Polyakov */
#include "../include/servers/fcgi.h" #include "../include/servers/fcgi.h"
#include "../c/utils.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#ifdef MT_ENABLED #ifdef MT_ENABLED
#include <pthread.h> #include <pthread.h>
@ -60,8 +56,6 @@ static int read_fcgx_req_body(char **dest, FCGX_Request *req);
*/ */
static void send_response(rpd_res *res, FCGX_Stream *out); static void send_response(rpd_res *res, FCGX_Stream *out);
static int env_to_req_header(char **key, char **val, const char *envp);
int rpd_fcgi_server_start(rpd_app *app, const char *sock_path) int rpd_fcgi_server_start(rpd_app *app, const char *sock_path)
{ {
FCGX_Init(); FCGX_Init();
@ -161,40 +155,9 @@ static int fcgx_to_rpd_req(rpd_req *dest, FCGX_Request *req)
dest->method = rpd_req_smethod(FCGX_GetParam("REQUEST_METHOD", req->envp)); dest->method = rpd_req_smethod(FCGX_GetParam("REQUEST_METHOD", req->envp));
rpd_url_parse(&dest->path, FCGX_GetParam("DOCUMENT_URI", req->envp)); rpd_url_parse(&dest->path, FCGX_GetParam("DOCUMENT_URI", req->envp));
// dest->auth = FCGX_GetParam("HTTP_AUTHORIZATION", req->envp); dest->auth = FCGX_GetParam("HTTP_AUTHORIZATION", req->envp);
// dest->cookie = FCGX_GetParam("HTTP_COOKIE", req->envp); dest->cookie = FCGX_GetParam("HTTP_COOKIE", req->envp);
rpd_keyval_init(&dest->params, 0); rpd_keyval_init(&dest->params, 0);
rpd_keyval_init(&dest->headers, 0);
dest->headers.unique = 0;
char **env = req->envp;
char *key = NULL, *val = NULL;
while (*(++env)) {
char *ptr = NULL;
/* keep only http request fields */
ptr = strstr(*env, "HTTP");
if (ptr == NULL || ptr - *env != 0)
continue;
env_to_req_header(&key, &val, *env);
if (!val || !key)
continue;
rpd_strerase(key, 5);
ptr = key;
while (*ptr) {
*ptr = tolower(*ptr);
if (ptr == key || *(ptr - 1) == '-')
*ptr = toupper(*ptr);
if (*ptr == '_')
*ptr = '-';
ptr++;
}
rpd_keyval_insert(&dest->headers, key, val);
free(key);
free(val);
}
if (dest->method != GET) { if (dest->method != GET) {
if (read_fcgx_req_body(&dest->body, req)) { if (read_fcgx_req_body(&dest->body, req)) {
@ -223,19 +186,3 @@ static int read_fcgx_req_body(char **dest, FCGX_Request *req)
return 0; return 0;
} }
static int env_to_req_header(char **key, char **val, const char *env)
{
const char *ptr = NULL;
rpd_splitbyc(key, val, env, '=');
if (!*key || !*val) {
if (*key)
free(*key);
if (*val)
free(*val);
return 1;
}
return 0;
}

17
servers/tcp.c

@ -45,23 +45,6 @@ static int mg_to_rpd_req(rpd_req *req, struct mg_http_message *msg)
rpd_query_parse(&req->query, tmp); rpd_query_parse(&req->query, tmp);
} }
size_t i, max = sizeof(msg->headers) / sizeof(msg->headers[0]);
rpd_keyval_init(&req->headers, max);
req->headers.unique = 0;
// Iterate over request headers
char *key = NULL, *val = NULL;
for (i = 0; i < max && msg->headers[i].name.len > 0; i++) {
struct mg_str *k = &msg->headers[i].name, *v = &msg->headers[i].value;
mg_str_alloc(&key, *k);
mg_str_alloc(&val, *v);
rpd_keyval_insert(&req->headers, key, val);
}
free(key);
free(val);
free(tmp); free(tmp);
return 0; return 0;
} }

2
tests/app.cxx

@ -9,7 +9,7 @@ using namespace rpd;
TEST_CASE("Application") TEST_CASE("Application")
{ {
rpd_app app; rpd_app app;
int res = rpd_app_create(&app); int res = rpd_app_create(&app, "/tmp/rapida.test.socket");
SECTION("App creation") SECTION("App creation")
{ {

18
tests/keyval.cxx

@ -36,24 +36,6 @@ TEST_CASE("Key-value storage")
REQUIRE(std::string(item->val) == "val"); REQUIRE(std::string(item->val) == "val");
} }
SECTION("Passing duplicates to non-unique storage and finding them")
{
int sz = keyval.size;
keyval.unique = 0;
rpd_keyval_insert(&keyval, "Set-Cookie", "param=val");
rpd_keyval_insert(&keyval, "Set-Cookie", "param1=val1");
REQUIRE(keyval.size == sz + 2);
rpd_keyval_item **cookies = rpd_keyval_findall(&keyval, "Set-Cookie");
REQUIRE(cookies != NULL);
REQUIRE(std::string(cookies[0]->key) == "Set-Cookie");
REQUIRE(std::string(cookies[0]->val) == "param=val");
REQUIRE(std::string(cookies[1]->key) == "Set-Cookie");
REQUIRE(std::string(cookies[1]->val) == "param1=val1");
REQUIRE(cookies[2] == NULL);
}
SECTION("Cleanup") SECTION("Cleanup")
{ {
rpd_keyval_cleanup(&keyval); rpd_keyval_cleanup(&keyval);

Loading…
Cancel
Save