Implement vitastor-cli serve command to serve simple HTTP API

This commit is contained in:
Vitaliy Filippov
2026-04-22 01:41:10 +03:00
parent 157191b770
commit 54feea5234
9 changed files with 561 additions and 38 deletions
+34
View File
@@ -1,6 +1,7 @@
// Copyright (c) Vitaliy Filippov, 2019+
// License: VNPL-1.1 or GNU GPL-2.0+ (see README.md for details)
#include "str_util.h"
#include "json_util.h"
std::map<std::string, std::string> json_to_string_map(const json11::Json::object & config)
@@ -49,3 +50,36 @@ std::string implode(const std::string & sep, json11::Json array)
}
return res;
}
static void json_array_add(json11::Json & to, json11::Json item)
{
if (to.is_null())
to = item;
else if (to.is_array())
{
auto a = to.array_items();
a.push_back(item);
to = a;
}
else
{
json11::Json::array a = {to};
a.push_back(item);
to = a;
}
}
json11::Json::object parse_uri_params(const std::string & params)
{
json11::Json::object obj;
auto list = explode("&", params, true);
for (auto & param: list)
{
auto pos = param.find("=");
if (pos != std::string::npos)
json_array_add(obj[urldecode(param.substr(0, pos))], urldecode(param.substr(pos+1)));
else
json_array_add(obj[urldecode(param)], true);
}
return obj;
}
+1
View File
@@ -14,5 +14,6 @@ std::map<std::string, std::string> json_to_string_map(const json11::Json::object
bool json_is_true(const json11::Json & val);
bool json_is_false(const json11::Json & val);
std::string implode(const std::string & sep, json11::Json array);
json11::Json::object parse_uri_params(const std::string & params);
#pragma GCC visibility pop
+31
View File
@@ -503,3 +503,34 @@ bool is_zero(void *buf, size_t size)
}
return true;
}
char hextochar(char h)
{
if (h >= 'a' && h <= 'f')
return h-'a'+10;
else if (h >= 'A' && h <= 'F')
return h-'A'+10;
else if (h >= '0' && h <= '9')
return h-'0';
return 0;
}
std::string urldecode(const std::string & orig)
{
size_t len = orig.size();
std::string res;
res.reserve(len);
for (size_t i = 0; i < len; i++)
{
if (orig[i] == '+')
res.push_back(' ');
else if (orig[i] == '%' && i < len-2)
{
res.push_back(hextochar(orig[i+1])*16 + hextochar(orig[i+2]));
i += 2;
}
else
res.push_back(orig[i]);
}
return res;
}
+1
View File
@@ -34,5 +34,6 @@ std::string addslashes(const std::string & str, const char *toescape = "\\\"");
std::string realpath_str(std::string path, bool nofail = true);
std::string format_datetime(uint64_t unixtime);
bool is_zero(void *buf, size_t size);
std::string urldecode(const std::string & orig);
#pragma GCC visibility pop