Implement vitastor-cli serve command to serve simple HTTP API
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user