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

fix: handle incoming websocket requests; fix: removed double free; introduced jsonrpc notifications (to handle close)

This commit is contained in:
Falk Werner 2019-02-09 12:01:58 +01:00
parent b698119079
commit aec60d194e
9 changed files with 92 additions and 39 deletions

View File

@ -53,7 +53,7 @@ class FileSystemHandler {
const inode = request.params[0]; const inode = request.params[0];
const handle = request.params[1]; const handle = request.params[1];
const mode = request.params[2]; const mode = request.params[2];
result = this._fs.open(inode, handle, mode); this._fs.close(inode, handle, mode);
} }
break; break;
case "read": case "read":
@ -69,6 +69,8 @@ class FileSystemHandler {
break; break;
} }
if ("number" == typeof(request.id))
{
if ("number" !== typeof(result)) { if ("number" !== typeof(result)) {
response = {result: result, id: request.id}; response = {result: result, id: request.id};
} }
@ -79,6 +81,7 @@ class FileSystemHandler {
this._connection.send(JSON.stringify(response)); this._connection.send(JSON.stringify(response));
} }
} }
}
catch (ex) { console.log(ex, message); } catch (ex) { console.log(ex, message); }
}; };
} }

View File

@ -35,7 +35,10 @@ json_t * wsfs_jsonrpc_request_create(
json_object_set_new(request, "params", params); json_object_set_new(request, "params", params);
if (0 != id)
{
json_object_set_new(request, "id", json_integer(id)); json_object_set_new(request, "id", json_integer(id));
}
return request; return request;
} }

View File

@ -83,6 +83,11 @@ void wsfs_jsonrpc_server_invoke(
{ {
if (!method->invoke(method->user_data, request)) if (!method->invoke(method->user_data, request))
{ {
server->request.is_pending = false;
server->request.finished = NULL;
server->request.user_data = NULL;
server->request.id = 0;
finished(user_data, WSFS_BAD, NULL); finished(user_data, WSFS_BAD, NULL);
} }
json_decref(request); json_decref(request);
@ -97,9 +102,33 @@ void wsfs_jsonrpc_server_invoke(
{ {
finished(user_data, WSFS_BAD_BUSY, NULL); finished(user_data, WSFS_BAD_BUSY, NULL);
} }
}
extern void wsfs_jsonrpc_server_notify(
struct wsfs_jsonrpc_server * server,
char const * method_name,
char const * param_info,
...
)
{
struct wsfs_jsonrpc_method const * method = wsfs_jsonrpc_server_getmethod(server, method_name);
if (NULL != method)
{
va_list args;
va_start(args, param_info);
json_t * request = wsfs_jsonrpc_request_create(method_name, 0, param_info, args);
va_end(args);
if (NULL != request)
{
method->invoke(method->user_data, request);
json_decref(request);
}
}
} }
void wsfs_jsonrpc_server_onresult( void wsfs_jsonrpc_server_onresult(
struct wsfs_jsonrpc_server * server, struct wsfs_jsonrpc_server * server,
char const * message, char const * message,
@ -108,8 +137,6 @@ void wsfs_jsonrpc_server_onresult(
struct wsfs_jsonrpc_response response; struct wsfs_jsonrpc_response response;
wsfs_jsonrpc_response_init(&response, message, length); wsfs_jsonrpc_response_init(&response, message, length);
if (-1 != response.id)
{
if ((server->request.is_pending) && (response.id == server->request.id)) if ((server->request.is_pending) && (response.id == server->request.id))
{ {
wsfs_jsonrpc_method_finished_fn * finished = server->request.finished; wsfs_jsonrpc_method_finished_fn * finished = server->request.finished;
@ -122,7 +149,6 @@ void wsfs_jsonrpc_server_onresult(
finished(user_data, response.status, response.result); finished(user_data, response.status, response.result);
} }
}
wsfs_jsonrpc_response_cleanup(&response); wsfs_jsonrpc_response_cleanup(&response);
} }

View File

@ -50,7 +50,14 @@ extern void wsfs_jsonrpc_server_invoke(
struct wsfs_jsonrpc_server * server, struct wsfs_jsonrpc_server * server,
wsfs_jsonrpc_method_finished_fn * finished, wsfs_jsonrpc_method_finished_fn * finished,
void * user_data, void * user_data,
char const * method, char const * method_name,
char const * param_info,
...
);
extern void wsfs_jsonrpc_server_notify(
struct wsfs_jsonrpc_server * server,
char const * method_name,
char const * param_info, char const * param_info,
... ...
); );

View File

@ -7,16 +7,6 @@
#include "wsfs/jsonrpc/server.h" #include "wsfs/jsonrpc/server.h"
#include "wsfs/util.h" #include "wsfs/util.h"
static void wsfs_operation_close_finished(
void * user_data,
wsfs_status status,
json_t const * WSFS_UNUSED_PARAM(result))
{
fuse_req_t request = (fuse_req_t) user_data;
fuse_reply_err(request, (WSFS_GOOD == status) ? 0 : ENOENT);
}
void wsfs_operation_close( void wsfs_operation_close(
fuse_req_t request, fuse_req_t request,
fuse_ino_t inode, fuse_ino_t inode,
@ -26,7 +16,6 @@ void wsfs_operation_close(
struct wsfs_jsonrpc_server * rpc = user_data->rpc; struct wsfs_jsonrpc_server * rpc = user_data->rpc;
int handle = (int) (file_info->fh & INT_MAX); int handle = (int) (file_info->fh & INT_MAX);
wsfs_jsonrpc_server_invoke( wsfs_jsonrpc_server_notify(rpc, "close", "iii", inode, handle, file_info->flags);
rpc, &wsfs_operation_close_finished, request, fuse_reply_err(request, 0);
"close", "iii", inode, handle, file_info->flags);
} }

View File

@ -6,6 +6,7 @@
#include "wsfs/jsonrpc/server.h" #include "wsfs/jsonrpc/server.h"
#include "wsfs/util.h" #include "wsfs/util.h"
#include "wsfs/status.h"
static void wsfs_operation_open_finished( static void wsfs_operation_open_finished(
void * user_data, void * user_data,

View File

@ -11,8 +11,8 @@ static int wsfs_server_protocol_callback(
struct lws * wsi, struct lws * wsi,
enum lws_callback_reasons reason, enum lws_callback_reasons reason,
void * WSFS_UNUSED_PARAM(user), void * WSFS_UNUSED_PARAM(user),
void * WSFS_UNUSED_PARAM(in), void * in,
size_t WSFS_UNUSED_PARAM(len)) size_t len)
{ {
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi); struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
struct wsfs_server_protocol * protocol = ws_protocol->user; struct wsfs_server_protocol * protocol = ws_protocol->user;
@ -56,6 +56,11 @@ static int wsfs_server_protocol_callback(
} }
} }
break; break;
case LWS_CALLBACK_RECEIVE:
{
wsfs_jsonrpc_server_onresult(&protocol->rpc, in, len);
}
break;
case LWS_CALLBACK_RAW_RX_FILE: case LWS_CALLBACK_RAW_RX_FILE:
{ {
wsfs_filesystem_process_request(&protocol->filesystem); wsfs_filesystem_process_request(&protocol->filesystem);

View File

@ -10,8 +10,25 @@
case WSFS_BAD_NOTIMPLEMENTED: return -ENOSYS; case WSFS_BAD_NOTIMPLEMENTED: return -ENOSYS;
case WSFS_BAD_TIMEOUT: return -ETIMEDOUT; case WSFS_BAD_TIMEOUT: return -ETIMEDOUT;
case WSFS_BAD_BUSY: return -ENOENT; case WSFS_BAD_BUSY: return -ENOENT;
case WSFS_BAD_FORMAT: return -ENOENT;
case WSFS_BAD_NOENTRY: return -ENOENT; case WSFS_BAD_NOENTRY: return -ENOENT;
case WSFS_BAD_NOACCESS: return -EACCES; case WSFS_BAD_NOACCESS: return -EACCES;
default: return -ENOENT; default: return -ENOENT;
} }
} }
char const * wsfs_status_tostring(wsfs_status status)
{
switch(status)
{
case WSFS_GOOD: return "Good";
case WSFS_BAD: return "Bad";
case WSFS_BAD_NOTIMPLEMENTED: return "Bad (not implelemted)";
case WSFS_BAD_TIMEOUT: return "Bad (timeout)";
case WSFS_BAD_BUSY: return "Bad (busy)";
case WSFS_BAD_FORMAT: return "Bad (format)";
case WSFS_BAD_NOENTRY: return "Bad (no entry)";
case WSFS_BAD_NOACCESS: return "Bad (no access)";
default: return "Bad (unknown)";
}
}

View File

@ -20,6 +20,8 @@ extern "C" {
extern int wsfs_status_to_rc(wsfs_status status); extern int wsfs_status_to_rc(wsfs_status status);
extern char const * wsfs_status_tostring(wsfs_status status);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif