Skip to content

Commit

Permalink
fix: add json_value_destroy() in RPCCommon::json_to_map()
Browse files Browse the repository at this point in the history
  • Loading branch information
holmes1412 committed Sep 6, 2024
1 parent 6cdcea0 commit a6bdd61
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 36 deletions.
36 changes: 36 additions & 0 deletions src/rpc_basic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
limitations under the License.
*/

#include <workflow/json_parser.h>

#ifdef _WIN32
#include <workflow/PlatformSocket.h>
Expand Down Expand Up @@ -73,5 +74,40 @@ bool RPCCommon::addr_to_string(const struct sockaddr *addr,
return ret != NULL;
}

bool RPCCommon::json_to_map(const std::string &json_str,
std::map<std::string, std::string>& kv_map)
{
json_value_t *val;
const json_object_t *obj;
const char *k;
const json_value_t *v;
bool ret = true;

val = json_value_parse(json_str.c_str());
if (val)
{
if (json_value_type(val) == JSON_VALUE_OBJECT)
{
obj = json_value_object(val);
json_object_for_each(k, v, obj)
{
if (json_value_type(v) == JSON_VALUE_STRING)
{
kv_map.emplace(k, json_value_string(v));
} else {
ret = false;
break;
}
}
} else {
ret = false;
}

json_value_destroy(val);
}

return ret;
}

} // namespace srpc

2 changes: 2 additions & 0 deletions src/rpc_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ class RPCCommon
const RPCLogVector& fields);
static bool addr_to_string(const struct sockaddr *addr, char *ip_str,
socklen_t len, unsigned short *port);
static bool json_to_map(const std::string& json_str,
std::map<std::string, std::string>& kv_map);
};

static inline long long GET_CURRENT_MS()
Expand Down
10 changes: 5 additions & 5 deletions src/rpc_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class RPCClient
std::string service_name;

private:
void __task_init(COMPLEXTASK *task) const;
void task_init_internal(COMPLEXTASK *task) const;

protected:
RPCClientParams params;
Expand Down Expand Up @@ -199,7 +199,7 @@ inline void RPCClient<RPCTYPE>::init(const RPCClientParams *params)
}

template<class RPCTYPE>
inline void RPCClient<RPCTYPE>::__task_init(COMPLEXTASK *task) const
inline void RPCClient<RPCTYPE>::task_init_internal(COMPLEXTASK *task) const
{
if (this->has_addr_info)
{
Expand All @@ -216,7 +216,7 @@ inline void RPCClient<RPCTYPE>::__task_init(COMPLEXTASK *task) const
template<class RPCTYPE>
inline void RPCClient<RPCTYPE>::task_init(COMPLEXTASK *task) const
{
__task_init(task);
this->task_init_internal(task);
}

static inline void __set_host_by_uri(const ParsedURI *uri, bool is_ssl,
Expand Down Expand Up @@ -271,7 +271,7 @@ inline void RPCClient<RPCTYPESRPCHttp>::task_init(COMPLEXTASK *task) const
std::string header_host;
std::string request_uri;

__task_init(task);
this->task_init_internal(task);

if (this->has_addr_info)
header_host += this->params.host + ":" + std::to_string(this->params.port);
Expand All @@ -289,7 +289,7 @@ inline void RPCClient<RPCTYPEThriftHttp>::task_init(COMPLEXTASK *task) const
{
std::string header_host;

__task_init(task);
this->task_init_internal(task);

if (this->has_addr_info)
header_host += this->params.host + ":" + std::to_string(this->params.port);
Expand Down
40 changes: 11 additions & 29 deletions src/rpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <errno.h>
#include <workflow/WFServer.h>
#include <workflow/WFHttpServer.h>
#include <workflow/json_parser.h>
#include "rpc_basic.h"
#include "rpc_types.h"
#include "rpc_service.h"
#include "rpc_options.h"
Expand Down Expand Up @@ -50,7 +50,7 @@ class RPCServer : public WFServer<typename RPCTYPE::REQ,
RPCServer(const struct RPCServerParams *params);

int add_service(RPCService *service);
int add_service(RPCService *service, const char *trans_coding);
int add_service(RPCService *service, const std::string& trans_coding);
const RPCService* find_service(const std::string& name) const;
void add_filter(RPCFilter *filter);

Expand Down Expand Up @@ -149,39 +149,21 @@ inline int RPCServer<RPCTYPESRPCHttp>::add_service(RPCService *service)

template<class RPCTYPE>
inline int RPCServer<RPCTYPE>::add_service(RPCService *service,
const char *trans_coding)
const std::string &trans_coding)
{
const json_value_t *val;
const json_object_t *obj;
const char *k;
const json_value_t *v;
std::map<std::string, std::string> kv_map;
std::string str;

val = json_value_parse(trans_coding);
if (val && json_value_type(val) == JSON_VALUE_OBJECT)
{
obj = json_value_object(val);
json_object_for_each(k, v, obj)
{
if (json_value_type(v) == JSON_VALUE_STRING)
{
str = "/" + service->get_name() + "/" + json_value_string(v);
this->path_map.emplace(k, str);
}
else
{
errno = EINVAL;
return -1;
}
}
}
else
{
errno = EINVAL;
if (!RPCCommon::json_to_map(trans_coding, kv_map))
return -1;

for (const auto &kv : kv_map)
{
str = "/" + service->get_name() + "/" + kv.second;
this->path_map.emplace(std::move(kv.first), std::move(str));
}

return add_service(service);
return this->add_service(service);
}

template<class RPCTYPE>
Expand Down
11 changes: 9 additions & 2 deletions tutorial/tutorial-20-trans_coding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ int main()
SRPCHttpServer server;
ExampleServiceImpl impl;

server.add_service(&impl, "{ \"/test/echo\": \"Echo\","
"\"/no/method/for/test\": \"Test\" }");
std::string trans_coding = R"(
{
"/test/echo" : "Echo",
"/another/for/echo" : "Echo",
"/no/method/for/test" : "Test"
}
)";

server.add_service(&impl, trans_coding);

if (server.start(1412) == 0)
{
Expand Down

0 comments on commit a6bdd61

Please sign in to comment.