1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

changes interface of jsonrpc_proxy_onresult to accept previously parsed messages

This commit is contained in:
Falk Werner 2019-03-31 18:13:32 +02:00
parent dc8ac6d8cf
commit b4fac43cd1
11 changed files with 49 additions and 39 deletions

View File

@ -157,11 +157,10 @@ extern void wf_impl_jsonrpc_proxy_notify(
void wf_impl_jsonrpc_proxy_onresult(
struct wf_impl_jsonrpc_proxy * proxy,
char const * message,
size_t length)
json_t * message)
{
struct wf_impl_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, message, length);
wf_impl_jsonrpc_response_init(&response, message);
if ((proxy->request.is_pending) && (response.id == proxy->request.id))
{

View File

@ -70,8 +70,7 @@ extern void wf_impl_jsonrpc_proxy_notify(
extern void wf_impl_jsonrpc_proxy_onresult(
struct wf_impl_jsonrpc_proxy * proxy,
char const * message,
size_t length);
json_t * message);
#ifdef __cplusplus
}

View File

@ -2,20 +2,12 @@
void wf_impl_jsonrpc_response_init(
struct wf_impl_jsonrpc_response * result,
char const * buffer,
size_t length)
json_t * response)
{
result->status = WF_BAD;
result->id = -1;
result->result = NULL;
json_t * response = json_loadb(buffer, length, 0, NULL);
if (NULL == response)
{
result->status = WF_BAD_FORMAT;
return;
}
json_t * id_holder = json_object_get(response, "id");
if ((NULL == id_holder) || (!json_is_integer(id_holder)))
{
@ -45,8 +37,6 @@ void wf_impl_jsonrpc_response_init(
}
}
}
json_decref(response);
}
void wf_impl_jsonrpc_response_cleanup(

View File

@ -24,8 +24,7 @@ struct wf_impl_jsonrpc_response
extern void wf_impl_jsonrpc_response_init(
struct wf_impl_jsonrpc_response * response,
char const * buffer,
size_t buffer_length);
json_t * message);
extern void wf_impl_jsonrpc_response_cleanup(
struct wf_impl_jsonrpc_response * response);

View File

@ -7,6 +7,7 @@
#include "webfuse/core/util.h"
#include "webfuse/adapter/impl/filesystem.h"
#include "webfuse/adapter/impl/jsonrpc/request.h"
static int wf_impl_server_protocol_callback(
struct lws * wsi,
@ -38,7 +39,8 @@ static int wf_impl_server_protocol_callback(
&protocol->session_manager,
wsi,
&protocol->authenticators,
&protocol->timeout_manager);
&protocol->timeout_manager,
&protocol->server);
if (NULL != session)
{
@ -102,6 +104,15 @@ void wf_impl_server_protocol_init_lws(
lws_protocol->user = protocol;
}
static void wf_impl_server_protocol_authenticate(
struct wf_impl_jsonrpc_request * request,
char const * WF_UNUSED_PARAM(method_name),
json_t * WF_UNUSED_PARAM(params),
void * WF_UNUSED_PARAM(user_data))
{
wf_impl_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED);
}
bool wf_impl_server_protocol_init(
struct wf_server_protocol * protocol,
char * mount_point)
@ -110,11 +121,15 @@ bool wf_impl_server_protocol_init(
wf_impl_session_manager_init(&protocol->session_manager);
wf_impl_authenticators_init(&protocol->authenticators);
wf_impl_jsonrpc_server_init(&protocol->server);
wf_impl_jsonrpc_server_add(&protocol->server, "authenticate", &wf_impl_server_protocol_authenticate, protocol);
bool const success = wf_impl_filesystem_init(&protocol->filesystem, &protocol->session_manager, mount_point);
// cleanup on error
if (!success)
{
wf_impl_jsonrpc_server_cleanup(&protocol->server);
wf_impl_authenticators_cleanup(&protocol->authenticators);
wf_impl_timeout_manager_cleanup(&protocol->timeout_manager);
wf_impl_session_manager_cleanup(&protocol->session_manager);
@ -127,6 +142,7 @@ void wf_impl_server_protocol_cleanup(
struct wf_server_protocol * protocol)
{
wf_impl_filesystem_cleanup(&protocol->filesystem);
wf_impl_jsonrpc_server_cleanup(&protocol->server);
wf_impl_timeout_manager_cleanup(&protocol->timeout_manager);
wf_impl_authenticators_cleanup(&protocol->authenticators);
wf_impl_session_manager_cleanup(&protocol->session_manager);

View File

@ -6,6 +6,7 @@
#include "webfuse/adapter/impl/time/timeout_manager.h"
#include "webfuse/adapter/impl/authenticators.h"
#include "webfuse/adapter/impl/session_manager.h"
#include "webfuse/adapter/impl/jsonrpc/server.h"
#ifdef __cplusplus
extern "C"
@ -20,6 +21,7 @@ struct wf_server_protocol
struct wf_impl_filesystem filesystem;
struct wf_impl_authenticators authenticators;
struct wf_impl_session_manager session_manager;
struct wf_impl_jsonrpc_server server;
};
extern bool wf_impl_server_protocol_init(

View File

@ -35,11 +35,13 @@ void wf_impl_session_init(
struct wf_impl_session * session,
struct lws * wsi,
struct wf_impl_authenticators * authenticators,
struct wf_impl_timeout_manager * timeout_manager)
struct wf_impl_timeout_manager * timeout_manager,
struct wf_impl_jsonrpc_server * server)
{
session->wsi = wsi;
session->is_authenticated = false;
session->authenticators = authenticators;
session->server = server;
wf_impl_jsonrpc_proxy_init(&session->rpc, timeout_manager, &wf_impl_session_send, session);
wf_message_queue_init(&session->queue);
}
@ -52,6 +54,7 @@ void wf_impl_session_cleanup(
session->is_authenticated = false;
session->wsi = NULL;
session->authenticators = NULL;
session->server = NULL;
}
void wf_impl_session_authenticate(
@ -83,5 +86,11 @@ void wf_impl_session_receive(
char const * data,
size_t length)
{
wf_impl_jsonrpc_proxy_onresult(&session->rpc, data, length);
json_t * message = json_loadb(data, length, 0, NULL);
if (NULL != message)
{
wf_impl_jsonrpc_proxy_onresult(&session->rpc, message);
json_decref(message);
}
}

View File

@ -11,6 +11,7 @@ using std::size_t;
#include "webfuse/core/message_queue.h"
#include "webfuse/adapter/impl/jsonrpc/proxy.h"
#include "webfuse/adapter/impl/jsonrpc/server.h"
#ifdef __cplusplus
extern "C"
@ -29,6 +30,7 @@ struct wf_impl_session
bool is_authenticated;
struct wf_message_queue queue;
struct wf_impl_authenticators * authenticators;
struct wf_impl_jsonrpc_server * server;
struct wf_impl_jsonrpc_proxy rpc;
};
@ -36,7 +38,8 @@ extern void wf_impl_session_init(
struct wf_impl_session * session,
struct lws * wsi,
struct wf_impl_authenticators * authenticators,
struct wf_impl_timeout_manager * timeout_manager);
struct wf_impl_timeout_manager * timeout_manager,
struct wf_impl_jsonrpc_server * server);
extern void wf_impl_session_authenticate(
struct wf_impl_session * session,

View File

@ -5,7 +5,7 @@
void wf_impl_session_manager_init(
struct wf_impl_session_manager * manager)
{
wf_impl_session_init(&manager->session, NULL, NULL, NULL);
wf_impl_session_init(&manager->session, NULL, NULL, NULL, NULL);
}
void wf_impl_session_manager_cleanup(
@ -18,13 +18,14 @@ struct wf_impl_session * wf_impl_session_manager_add(
struct wf_impl_session_manager * manager,
struct lws * wsi,
struct wf_impl_authenticators * authenticators,
struct wf_impl_timeout_manager * timeout_manager)
struct wf_impl_timeout_manager * timeout_manager,
struct wf_impl_jsonrpc_server * server)
{
struct wf_impl_session * session = NULL;
if (NULL == manager->session.wsi)
{
session = &manager->session;
wf_impl_session_init(&manager->session, wsi, authenticators, timeout_manager);
wf_impl_session_init(&manager->session, wsi, authenticators, timeout_manager, server);
}
return session;

View File

@ -15,6 +15,7 @@ extern "C"
struct lws;
struct wf_impl_timeout_manager;
struct wf_impl_jsonrpc_server;
struct wf_impl_session_manager
{
@ -31,7 +32,8 @@ extern struct wf_impl_session * wf_impl_session_manager_add(
struct wf_impl_session_manager * manager,
struct lws * wsi,
struct wf_impl_authenticators * authenticators,
struct wf_impl_timeout_manager * timeout_manager);
struct wf_impl_timeout_manager * timeout_manager,
struct wf_impl_jsonrpc_server * server);
extern struct wf_impl_session * wf_impl_session_manager_get(
struct wf_impl_session_manager * manager,

View File

@ -8,25 +8,15 @@ static void response_parse_str(
std::string const & buffer,
struct wf_impl_jsonrpc_response * response)
{
wf_impl_jsonrpc_response_init(response, buffer.c_str(), buffer.size());
json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr);
wf_impl_jsonrpc_response_init(response, message);
json_decref(message);
}
TEST(response_parser, test)
{
struct wf_impl_jsonrpc_response response;
// invalid json
response_parse_str("", &response);
ASSERT_NE(WF_GOOD, response.status);
ASSERT_EQ(-1, response.id);
ASSERT_EQ(nullptr, response.result);
// invalid json
response_parse_str("invalid_json", &response);
ASSERT_NE(WF_GOOD, response.status);
ASSERT_EQ(-1, response.id);
ASSERT_EQ(nullptr, response.result);
// no object
response_parse_str("[]", &response);
ASSERT_NE(WF_GOOD, response.status);