diff --git a/lib/webfuse/adapter/impl/jsonrpc/proxy.c b/lib/webfuse/adapter/impl/jsonrpc/proxy.c index 46a88b9..5435562 100644 --- a/lib/webfuse/adapter/impl/jsonrpc/proxy.c +++ b/lib/webfuse/adapter/impl/jsonrpc/proxy.c @@ -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)) { diff --git a/lib/webfuse/adapter/impl/jsonrpc/proxy.h b/lib/webfuse/adapter/impl/jsonrpc/proxy.h index 0940ef9..92b4508 100644 --- a/lib/webfuse/adapter/impl/jsonrpc/proxy.h +++ b/lib/webfuse/adapter/impl/jsonrpc/proxy.h @@ -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 } diff --git a/lib/webfuse/adapter/impl/jsonrpc/response.c b/lib/webfuse/adapter/impl/jsonrpc/response.c index 74f230f..5cb2818 100644 --- a/lib/webfuse/adapter/impl/jsonrpc/response.c +++ b/lib/webfuse/adapter/impl/jsonrpc/response.c @@ -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( diff --git a/lib/webfuse/adapter/impl/jsonrpc/response.h b/lib/webfuse/adapter/impl/jsonrpc/response.h index b537994..90b2658 100644 --- a/lib/webfuse/adapter/impl/jsonrpc/response.h +++ b/lib/webfuse/adapter/impl/jsonrpc/response.h @@ -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); diff --git a/lib/webfuse/adapter/impl/server_protocol.c b/lib/webfuse/adapter/impl/server_protocol.c index e4666e1..673415e 100644 --- a/lib/webfuse/adapter/impl/server_protocol.c +++ b/lib/webfuse/adapter/impl/server_protocol.c @@ -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); diff --git a/lib/webfuse/adapter/impl/server_protocol.h b/lib/webfuse/adapter/impl/server_protocol.h index 8f7d658..8a4b0d7 100644 --- a/lib/webfuse/adapter/impl/server_protocol.h +++ b/lib/webfuse/adapter/impl/server_protocol.h @@ -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( diff --git a/lib/webfuse/adapter/impl/session.c b/lib/webfuse/adapter/impl/session.c index 4d25fe2..2f6eabc 100644 --- a/lib/webfuse/adapter/impl/session.c +++ b/lib/webfuse/adapter/impl/session.c @@ -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); + } + } \ No newline at end of file diff --git a/lib/webfuse/adapter/impl/session.h b/lib/webfuse/adapter/impl/session.h index b6bb7ee..a945b4f 100644 --- a/lib/webfuse/adapter/impl/session.h +++ b/lib/webfuse/adapter/impl/session.h @@ -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, diff --git a/lib/webfuse/adapter/impl/session_manager.c b/lib/webfuse/adapter/impl/session_manager.c index 3883a71..be87c2a 100644 --- a/lib/webfuse/adapter/impl/session_manager.c +++ b/lib/webfuse/adapter/impl/session_manager.c @@ -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; diff --git a/lib/webfuse/adapter/impl/session_manager.h b/lib/webfuse/adapter/impl/session_manager.h index 3b01304..6b7a26b 100644 --- a/lib/webfuse/adapter/impl/session_manager.h +++ b/lib/webfuse/adapter/impl/session_manager.h @@ -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, diff --git a/test/test_response_parser.cc b/test/test_response_parser.cc index bacb7a9..e149628 100644 --- a/test/test_response_parser.cc +++ b/test/test_response_parser.cc @@ -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);