1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

fixed unit tests; added logging support; allow to specify filesystem name

This commit is contained in:
Falk Werner
2020-11-12 20:36:05 +01:00
parent 8140afe2ab
commit c26342cefd
15 changed files with 296 additions and 88 deletions

View File

@@ -165,6 +165,20 @@ void wfp_client_config_enable_authentication(
wfp_impl_client_config_enable_authentication(config, get_credentials);
}
void wfp_client_config_set_fsname(
struct wfp_client_config * config,
char const * name)
{
wfp_impl_client_config_set_fsname(config, name);
}
void wfp_client_config_set_logger(
struct wfp_client_config * config,
wfp_log_fn * log)
{
wfp_impl_client_config_set_logger(config, log);
}
// protocol

View File

@@ -28,9 +28,9 @@ struct wfp_client * wfp_impl_client_create(
struct wfp_client_config * config)
{
wfp_impl_lwslog_disable();
struct wfp_client * client = malloc(sizeof(struct wfp_client));
wfp_impl_client_protocol_init(&client->protocol, &config->provider, config->user_data);
wfp_impl_client_protocol_init(&client->protocol, &config->provider, config->fs_name, config->user_data);
memset(client->protocols, 0, sizeof(struct lws_protocols) * WFP_CLIENT_PROTOCOL_COUNT);
wfp_impl_client_protocol_init_lws(&client->protocol, &client->protocols[0]);
@@ -65,7 +65,7 @@ void wfp_impl_client_dispose(
struct wfp_client * client)
{
lws_context_destroy(client->context);
wfp_impl_client_protocol_cleanup(&client->protocol);
wfp_impl_client_protocol_cleanup(&client->protocol);
free(client);
}

View File

@@ -11,6 +11,7 @@ struct wfp_client_config * wfp_impl_client_config_create(void)
config->key_path = NULL;
config->cert_path = NULL;
config->ca_filepath = NULL;
config->fs_name = strdup("cprovider");
return config;
}
@@ -18,6 +19,7 @@ struct wfp_client_config * wfp_impl_client_config_create(void)
void wfp_impl_client_config_dispose(
struct wfp_client_config * config)
{
free(config->fs_name);
free(config->key_path);
free(config->cert_path);
free(config->ca_filepath);
@@ -52,7 +54,7 @@ void wfp_impl_client_config_set_ca_filepath(
char const * ca_filepath)
{
free(config->ca_filepath);
config->ca_filepath = strdup(ca_filepath);
config->ca_filepath = strdup(ca_filepath);
}
void wfp_impl_client_config_set_onconnected(
@@ -117,3 +119,18 @@ void wfp_impl_client_config_enable_authentication(
{
config->provider.get_credentials = get_credentials;
}
void wfp_impl_client_config_set_fsname(
struct wfp_client_config * config,
char const * name)
{
free(config->fs_name);
config->fs_name = strdup(name);
}
void wfp_impl_client_config_set_logger(
struct wfp_client_config * config,
wfp_log_fn * log)
{
config->provider.log = log;
}

View File

@@ -16,6 +16,7 @@ struct wfp_client_config
char * key_path;
char * cert_path;
char * ca_filepath;
char * fs_name;
};
extern struct wfp_client_config * wfp_impl_client_config_create(void);
@@ -75,6 +76,14 @@ extern void wfp_impl_client_config_enable_authentication(
struct wfp_client_config * config,
wfp_get_credentials_fn * get_credentials);
extern void wfp_impl_client_config_set_fsname(
struct wfp_client_config * config,
char const * name);
extern void wfp_impl_client_config_set_logger(
struct wfp_client_config * config,
wfp_log_fn * log);
#ifdef __cplusplus
}
#endif

View File

@@ -35,7 +35,7 @@ static void wfp_impl_client_protocol_respond(
}
static void wfp_impl_client_protocol_process(
struct wfp_client_protocol * protocol,
struct wfp_client_protocol * protocol,
char * data,
size_t length)
{
@@ -64,62 +64,71 @@ static void wfp_impl_client_protocol_process(
}
}
static void
static void
wfp_impl_client_protocol_on_add_filesystem_finished(
void * user_data,
struct wfp_json const * result,
struct wfp_jsonrpc_error const * WFP_UNUSED_PARAM(error))
struct wfp_jsonrpc_error const * WFP_UNUSED_PARAM(error))
{
struct wfp_client_protocol * protocol = user_data;
if (NULL == protocol->wsi) { return; }
if (NULL != result)
{
protocol->provider.log(protocol->user_data, 0, "filesystem added");
protocol->provider.log(protocol->user_data, 0, "handshake finished");
protocol->is_connected = true;
protocol->provider.connected(protocol->user_data);
}
else
{
protocol->provider.log(protocol->user_data, 0, "add filesystem failed");
protocol->is_shutdown_requested = true;
lws_callback_on_writable(protocol->wsi);
}
}
}
static void wfp_impl_client_protocol_add_filesystem(
struct wfp_client_protocol * protocol)
{
protocol->provider.log(protocol->user_data, 0, "begin add filesystem");
wfp_jsonrpc_proxy_invoke(
protocol->proxy,
protocol->proxy,
&wfp_impl_client_protocol_on_add_filesystem_finished,
protocol,
"add_filesystem",
"s",
"cprovider");
protocol->fs_name);
}
static void
static void
wfp_impl_client_protocol_on_authenticate_finished(
void * user_data,
struct wfp_json const * result,
struct wfp_jsonrpc_error const * WFP_UNUSED_PARAM(error))
struct wfp_jsonrpc_error const * WFP_UNUSED_PARAM(error))
{
struct wfp_client_protocol * protocol = user_data;
if (NULL == protocol->wsi) { return; }
if (NULL != result)
{
protocol->provider.log(protocol->user_data, 0, "authentication finished");
wfp_impl_client_protocol_add_filesystem(protocol);
}
else
{
protocol->provider.log(protocol->user_data, 0, "authentication failed");
protocol->is_shutdown_requested = true;
lws_callback_on_writable(protocol->wsi);
}
}
}
static void wfp_impl_client_protocol_authenticate(
struct wfp_client_protocol * protocol)
{
protocol->provider.log(protocol->user_data, 0, "begin authenticate");
struct wfp_credentials credentials;
wfp_impl_credentials_init(&credentials);
@@ -128,10 +137,10 @@ static void wfp_impl_client_protocol_authenticate(
char const * cred_type = wfp_impl_credentials_get_type(&credentials);
wfp_jsonrpc_proxy_invoke(
protocol->proxy,
&wfp_impl_client_protocol_on_authenticate_finished,
protocol,
"authenticate",
protocol->proxy,
&wfp_impl_client_protocol_on_authenticate_finished,
protocol,
"authenticate",
"sj",
cred_type, &wfp_impl_credentials_write, &credentials);
@@ -141,12 +150,15 @@ static void wfp_impl_client_protocol_authenticate(
static void wfp_impl_client_protocol_handshake(
struct wfp_client_protocol * protocol)
{
protocol->provider.log(protocol->user_data, 0, "begin connection handshake");
if (wfp_impl_provider_is_authentication_enabled(&protocol->provider))
{
wfp_impl_client_protocol_authenticate(protocol);
}
else
{
protocol->provider.log(protocol->user_data, 0, "skip authentication");
wfp_impl_client_protocol_add_filesystem(protocol);
}
}
@@ -159,7 +171,7 @@ static int wfp_impl_client_protocol_callback(
size_t len)
{
int result = 0;
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
struct wfp_client_protocol * protocol = (NULL != ws_protocol) ? ws_protocol->user: NULL;
if (NULL != protocol)
@@ -169,24 +181,28 @@ static int wfp_impl_client_protocol_callback(
switch (reason)
{
case LWS_CALLBACK_CLIENT_ESTABLISHED:
protocol->provider.log(protocol->user_data, 0, "connection established");
wfp_impl_client_protocol_handshake(protocol);
break;
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
protocol->provider.log(protocol->user_data, 0, "connect error");
protocol->is_connected = false;
protocol->provider.disconnected(protocol->user_data);
break;
case LWS_CALLBACK_CLIENT_CLOSED:
protocol->provider.log(protocol->user_data, 0, "connection closed");
protocol->is_connected = false;
protocol->provider.disconnected(protocol->user_data);
protocol->provider.disconnected(protocol->user_data);
protocol->wsi = NULL;
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
protocol->provider.log(protocol->user_data, 0, "received: \"%.*s\"", len, in);
wfp_impl_client_protocol_process(protocol, in, len);
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
// fall-through
case LWS_CALLBACK_CLIENT_WRITEABLE:
if (wsi == protocol->wsi)
if (wsi == protocol->wsi)
{
if (protocol->is_shutdown_requested)
{
@@ -194,8 +210,10 @@ static int wfp_impl_client_protocol_callback(
}
else if (!wfp_slist_empty(&protocol->messages))
{
struct wfp_slist_item * item = wfp_slist_remove_first(&protocol->messages);
struct wfp_message * message = wfp_container_of(item, struct wfp_message, item);
protocol->provider.log(protocol->user_data, 0, "send: \"%.*s\"", message->length, message->data);
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
wfp_message_dispose(message);
@@ -207,7 +225,7 @@ static int wfp_impl_client_protocol_callback(
}
break;
default:
break;
break;
}
}
@@ -229,6 +247,7 @@ static void wfp_impl_client_protocol_send(
void wfp_impl_client_protocol_init(
struct wfp_client_protocol * protocol,
struct wfp_provider const * provider,
char const * fs_name,
void * user_data)
{
protocol->is_connected = false;
@@ -243,6 +262,7 @@ void wfp_impl_client_protocol_init(
protocol->timer_manager = wfp_timer_manager_create();
protocol->proxy = wfp_jsonrpc_proxy_create(protocol->timer_manager, WFP_DEFAULT_TIMEOUT, &wfp_impl_client_protocol_send, protocol);
protocol->fs_name = strdup(fs_name);
protocol->user_data = user_data;
wfp_impl_provider_init_from_prototype(&protocol->provider, provider);
}
@@ -253,13 +273,14 @@ void wfp_impl_client_protocol_cleanup(
wfp_jsonrpc_proxy_dispose(protocol->proxy);
wfp_timer_manager_dispose(protocol->timer_manager);
wfp_message_queue_cleanup(&protocol->messages);
free(protocol->fs_name);
}
struct wfp_client_protocol * wfp_impl_client_protocol_create(
struct wfp_client_config const * config)
{
struct wfp_client_protocol * protocol = malloc(sizeof(struct wfp_client_protocol));
wfp_impl_client_protocol_init(protocol, &config->provider, config->user_data);
wfp_impl_client_protocol_init(protocol, &config->provider, config->fs_name, config->user_data);
return protocol;
}
@@ -311,7 +332,7 @@ void wfp_impl_client_protocol_connect(
{
protocol->provider.disconnected(protocol->user_data);
}
}
void wfp_impl_client_protocol_disconnect(

View File

@@ -23,6 +23,7 @@ struct wfp_client_protocol
bool is_shutdown_requested;
struct wfp_request request;
struct wfp_provider provider;
char * fs_name;
void * user_data;
struct lws * wsi;
struct wfp_timer_manager * timer_manager;
@@ -33,6 +34,7 @@ struct wfp_client_protocol
extern void wfp_impl_client_protocol_init(
struct wfp_client_protocol * protocol,
struct wfp_provider const * provider,
char const * fs_name,
void * user_data);
extern void wfp_impl_client_protocol_cleanup(
@@ -57,7 +59,7 @@ extern void wfp_impl_client_protocol_disconnect(
struct wfp_client_protocol * protocol);
#ifdef __cplusplus
}
}
#endif
#endif

View File

@@ -28,7 +28,7 @@ struct wfp_impl_method
};
static void wfp_impl_provider_invoke_method(
struct wfp_impl_invokation_context * context,
struct wfp_impl_invokation_context * context,
char const * method_name,
struct wfp_json const * params,
int id)
@@ -70,6 +70,7 @@ void wfp_impl_provider_init(
provider->connected = &wfp_impl_connected_default;
provider->disconnected = &wfp_impl_disconnected_default;
provider->get_credentials = NULL;
provider->log = &wfp_impl_log_default;
}
void wfp_impl_provider_init_from_prototype(
@@ -85,6 +86,7 @@ void wfp_impl_provider_init_from_prototype(
provider->connected = prototype->connected;
provider->disconnected = prototype->disconnected;
provider->get_credentials = prototype->get_credentials;
provider->log = prototype->log;
}
void wfp_impl_provider_invoke(
@@ -120,9 +122,22 @@ void wfp_impl_disconnected_default(
// empty
}
void wfp_impl_log_default(
void * user_data,
int level,
char const * format,
...)
{
(void) user_data;
(void) level;
(void) format;
// empty
}
bool wfp_impl_provider_is_authentication_enabled(
struct wfp_provider * provider)
{
return (NULL != provider->get_credentials);
}

View File

@@ -3,6 +3,9 @@
#ifndef __cplusplus
#include <stdbool.h>
#include <stdarg.h>
#else
#include <cstdarg>
#endif
#include "webfuse_provider/client_config.h"
@@ -26,6 +29,7 @@ struct wfp_provider
wfp_close_fn * close;
wfp_read_fn * read;
wfp_get_credentials_fn * get_credentials;
wfp_log_fn * log;
};
struct wfp_impl_invokation_context
@@ -49,15 +53,21 @@ extern void wfp_impl_provider_invoke(
extern bool wfp_impl_provider_is_authentication_enabled(
struct wfp_provider * provider);
extern void wfp_impl_connected_default(
void * user_data);
extern void wfp_impl_disconnected_default(
void * user_data);
#ifdef __cplusplus
extern void wfp_impl_log_default(
void * user_data,
int level,
char const * format,
...);
#ifdef __cplusplus
}
#endif
#endif
#endif