Skip to content

Commit

Permalink
fix: add json_value_destroy() in RPCServer::add_service()
Browse files Browse the repository at this point in the history
  • Loading branch information
holmes1412 committed Sep 6, 2024
1 parent 6cdcea0 commit e6b46b4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
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
21 changes: 11 additions & 10 deletions src/rpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ template<class RPCTYPE>
inline int RPCServer<RPCTYPE>::add_service(RPCService *service,
const char *trans_coding)
{
const json_value_t *val;
json_value_t *val;
const json_object_t *obj;
const char *k;
const json_value_t *v;
std::string str;
int ret = 0;

val = json_value_parse(trans_coding);
if (val && json_value_type(val) == JSON_VALUE_OBJECT)
Expand All @@ -167,21 +168,21 @@ inline int RPCServer<RPCTYPE>::add_service(RPCService *service,
{
str = "/" + service->get_name() + "/" + json_value_string(v);
this->path_map.emplace(k, str);
}
else
{
} else {
errno = EINVAL;
return -1;
ret = -1;
}
}
}
else
{
} else {
errno = EINVAL;
return -1;
ret = -1;
}
json_value_destroy(val);

if (ret == 0)
ret = this->add_service(service);

return add_service(service);
return ret;
}

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\" }");
const char *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 e6b46b4

Please sign in to comment.