mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
refactor: merged code structure
This commit is contained in:
183
lib/webfuse_provider/impl/base64.c
Normal file
183
lib/webfuse_provider/impl/base64.c
Normal file
@@ -0,0 +1,183 @@
|
||||
#include "webfuse_provider/impl/base64.h"
|
||||
|
||||
static const uint8_t wf_base64_decode_table[256] = {
|
||||
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 1
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 62, 0x80, 0x80, 0x80, 63, // 2
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0x80, 0x80, 0x80, 0, 0x80, 0x80, // 3
|
||||
0x80, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 4
|
||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0x80, 0x80, 0x80, 0x80, 0x80, // 5
|
||||
0x80, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 6
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0x80, 0x80, 0x80, 0x80, 0x80, // 7
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 8
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 9
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // A
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // B
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // C
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // D
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // E
|
||||
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // F
|
||||
};
|
||||
|
||||
|
||||
|
||||
size_t wf_base64_encoded_size(size_t length)
|
||||
{
|
||||
return ((length + 2) / 3) * 4;
|
||||
}
|
||||
|
||||
size_t wf_base64_encode(
|
||||
uint8_t const * data,
|
||||
size_t length,
|
||||
char * buffer,
|
||||
size_t buffer_size)
|
||||
{
|
||||
// 0 1 2 3 4 5 6
|
||||
// 0123456789012345678901234567890123456789012345678901234567890123
|
||||
static char const table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
size_t const length_needed = wf_base64_encoded_size(length);
|
||||
if (buffer_size < length_needed)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
size_t out_pos = 0;
|
||||
for(; (length - pos) >= 3; pos += 3)
|
||||
{
|
||||
buffer[out_pos++] = table[ data[pos] >> 2 ];
|
||||
buffer[out_pos++] = table[ ((data[pos ] & 0x03) << 4) | (data[pos + 1] >> 4) ];
|
||||
buffer[out_pos++] = table[ ((data[pos + 1] & 0x0f) << 2) | (data[pos + 2] >> 6) ];
|
||||
buffer[out_pos++] = table[ data[pos + 2] & 0x3f ];
|
||||
}
|
||||
|
||||
switch((length - pos))
|
||||
{
|
||||
case 1:
|
||||
buffer[out_pos++] = table[ data[pos] >> 2 ];
|
||||
buffer[out_pos++] = table[ ((data[pos] & 0x03) << 4) ];
|
||||
buffer[out_pos++] = '=';
|
||||
buffer[out_pos++] = '=';
|
||||
break;
|
||||
case 2:
|
||||
buffer[out_pos++] = table[ data[pos] >> 2 ];
|
||||
buffer[out_pos++] = table[ ((data[pos ] & 0x03) << 4) | (data[pos + 1] >> 4) ];
|
||||
buffer[out_pos++] = table[ ((data[pos + 1] & 0x0f) << 2) ];
|
||||
buffer[out_pos++] = '=';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (buffer_size > out_pos)
|
||||
{
|
||||
buffer[out_pos] = '\0';
|
||||
}
|
||||
|
||||
return out_pos;
|
||||
}
|
||||
|
||||
size_t wf_base64_decoded_size(char const * data, size_t length)
|
||||
{
|
||||
size_t result = 0;
|
||||
if ((length > 0) && ((length % 4) == 0))
|
||||
{
|
||||
result = (length / 4) * 3;
|
||||
|
||||
if ('=' == data[length - 1])
|
||||
{
|
||||
result--;
|
||||
if ('=' == data[length - 2])
|
||||
{
|
||||
result--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t wf_base64_decode(
|
||||
char const * data,
|
||||
size_t length,
|
||||
uint8_t * buffer,
|
||||
size_t buffer_size)
|
||||
{
|
||||
uint8_t const * table = wf_base64_decode_table;
|
||||
size_t needed_size = wf_base64_decoded_size(data, length);
|
||||
if ((0 == needed_size) || (buffer_size < needed_size))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t out_pos = 0;
|
||||
size_t pos = 0;
|
||||
for(; pos < length - 4; pos += 4)
|
||||
{
|
||||
uint8_t a = table[ (unsigned char) data[pos ] ];
|
||||
uint8_t b = table[ (unsigned char) data[pos + 1] ];
|
||||
uint8_t c = table[ (unsigned char) data[pos + 2] ];
|
||||
uint8_t d = table[ (unsigned char) data[pos + 3] ];
|
||||
|
||||
buffer[out_pos++] = (a << 2) | (b >> 4);
|
||||
buffer[out_pos++] = (b << 4) | (c >> 2);
|
||||
buffer[out_pos++] = (c << 6) | d;
|
||||
}
|
||||
|
||||
// decode last block
|
||||
{
|
||||
uint8_t a = table[ (unsigned char) data[pos ] ];
|
||||
uint8_t b = table[ (unsigned char) data[pos + 1] ];
|
||||
uint8_t c = table[ (unsigned char) data[pos + 2] ];
|
||||
uint8_t d = table[ (unsigned char) data[pos + 3] ];
|
||||
|
||||
buffer[out_pos++] = (a << 2) | (b >> 4);
|
||||
if ('=' != data[pos + 2])
|
||||
{
|
||||
buffer[out_pos++] = (b << 4) | (c >> 2);
|
||||
if ('=' != data[pos + 3])
|
||||
{
|
||||
buffer[out_pos++] = (c << 6) | d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out_pos;
|
||||
}
|
||||
|
||||
extern bool wf_base64_isvalid(char const * data, size_t length)
|
||||
{
|
||||
uint8_t const * table = wf_base64_decode_table;
|
||||
|
||||
if ((length == 0) || ((length % 4) != 0))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
for(; pos < (length - 2); pos++)
|
||||
{
|
||||
unsigned char c = (unsigned char) data[pos];
|
||||
if (('=' == c) || (0x80 == table[c]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (('=' == data[pos]) && ('=' != data[pos + 1]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for(;pos < length; pos++)
|
||||
{
|
||||
char c = data[pos];
|
||||
if (0x80 == table[ (unsigned char) c])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
40
lib/webfuse_provider/impl/base64.h
Normal file
40
lib/webfuse_provider/impl/base64.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef WF_BASE64_H
|
||||
#define WF_BASE64_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cinttypes>
|
||||
#include <cstddef>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern size_t wf_base64_encoded_size(size_t length);
|
||||
|
||||
extern size_t wf_base64_encode(
|
||||
uint8_t const * data,
|
||||
size_t length,
|
||||
char * buffer,
|
||||
size_t buffer_size);
|
||||
|
||||
extern size_t wf_base64_decoded_size(char const * data, size_t length);
|
||||
|
||||
extern size_t wf_base64_decode(
|
||||
char const * data,
|
||||
size_t length,
|
||||
uint8_t * buffer,
|
||||
size_t buffer_size);
|
||||
|
||||
extern bool wf_base64_isvalid(char const * data, size_t length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
95
lib/webfuse_provider/impl/client.c
Normal file
95
lib/webfuse_provider/impl/client.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include "webfuse_provider/impl/client.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
#include "webfuse_provider/impl/client_protocol.h"
|
||||
#include "webfuse_provider/impl/client_config.h"
|
||||
#include "webfuse_provider/impl/lws_log.h"
|
||||
|
||||
#define WFP_CLIENT_PROTOCOL_COUNT 2
|
||||
|
||||
struct wfp_client
|
||||
{
|
||||
struct wfp_client_protocol protocol;
|
||||
struct lws_context_creation_info info;
|
||||
struct lws_protocols protocols[WFP_CLIENT_PROTOCOL_COUNT];
|
||||
struct lws_context * context;
|
||||
char * key_path;
|
||||
char * cert_path;
|
||||
};
|
||||
|
||||
|
||||
struct wfp_client * wfp_impl_client_create(
|
||||
struct wfp_client_config * config)
|
||||
{
|
||||
wf_lwslog_disable();
|
||||
|
||||
struct wfp_client * client = malloc(sizeof(struct wfp_client));
|
||||
wfp_impl_client_protocol_init(&client->protocol, &config->provider, 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]);
|
||||
|
||||
memset(&client->info, 0, sizeof(struct lws_context_creation_info));
|
||||
client->info.port = CONTEXT_PORT_NO_LISTEN;
|
||||
client->info.protocols = client->protocols;
|
||||
client->info.uid = -1;
|
||||
client->info.gid = -1;
|
||||
|
||||
if ((NULL != config->cert_path) && (NULL != config->key_path))
|
||||
{
|
||||
client->info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS;
|
||||
}
|
||||
|
||||
client->context = lws_create_context(&client->info);
|
||||
|
||||
if ((NULL != config->cert_path) && (NULL != config->key_path))
|
||||
{
|
||||
struct lws_vhost * vhost = lws_create_vhost(client->context, &client->info);
|
||||
client->info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
||||
client->info.client_ssl_cert_filepath = config->cert_path;
|
||||
client->info.client_ssl_private_key_filepath = config->key_path;
|
||||
client->info.client_ssl_ca_filepath = config->ca_filepath;
|
||||
lws_init_vhost_client_ssl(&client->info, vhost);
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
void wfp_impl_client_dispose(
|
||||
struct wfp_client * client)
|
||||
{
|
||||
lws_context_destroy(client->context);
|
||||
wfp_impl_client_protocol_cleanup(&client->protocol);
|
||||
free(client);
|
||||
}
|
||||
|
||||
void wfp_impl_client_connect(
|
||||
struct wfp_client * client,
|
||||
char const * url)
|
||||
{
|
||||
wfp_impl_client_protocol_connect(&client->protocol, client->context, url);
|
||||
}
|
||||
|
||||
void wfp_impl_client_disconnect(
|
||||
struct wfp_client * client)
|
||||
{
|
||||
wfp_impl_client_protocol_disconnect(&client->protocol);
|
||||
}
|
||||
|
||||
void wfp_impl_client_service(
|
||||
struct wfp_client * client)
|
||||
{
|
||||
lws_service(client->context, 0);
|
||||
}
|
||||
|
||||
void wfp_impl_client_interrupt(
|
||||
struct wfp_client * client)
|
||||
{
|
||||
lws_cancel_service(client->context);
|
||||
}
|
||||
48
lib/webfuse_provider/impl/client.h
Normal file
48
lib/webfuse_provider/impl/client.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef WF_PROVIDER_IMPL_CLIENT_H
|
||||
#define WF_PROVIDER_IMPL_CLIENT_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_client;
|
||||
struct wfp_client_config;
|
||||
|
||||
extern struct wfp_client * wfp_impl_client_create(
|
||||
struct wfp_client_config * config);
|
||||
|
||||
extern void wfp_impl_client_set_keypath(
|
||||
struct wfp_client * client,
|
||||
char * key_path);
|
||||
|
||||
extern void wfp_impl_client_set_certpath(
|
||||
struct wfp_client * client,
|
||||
char * cert_path);
|
||||
|
||||
extern void wfp_impl_client_connect(
|
||||
struct wfp_client * client,
|
||||
char const * url);
|
||||
|
||||
extern void wfp_impl_client_disconnect(
|
||||
struct wfp_client * client);
|
||||
|
||||
extern void wfp_impl_client_dispose(
|
||||
struct wfp_client * client);
|
||||
|
||||
extern void wfp_impl_client_service(
|
||||
struct wfp_client * client);
|
||||
|
||||
extern void wfp_impl_client_interrupt(
|
||||
struct wfp_client * client);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
119
lib/webfuse_provider/impl/client_config.c
Normal file
119
lib/webfuse_provider/impl/client_config.c
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "webfuse_provider/impl/client_config.h"
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wfp_client_config * wfp_impl_client_config_create(void)
|
||||
{
|
||||
struct wfp_client_config * config = malloc(sizeof(struct wfp_client_config));
|
||||
wfp_impl_provider_init(&config->provider);
|
||||
config->user_data = NULL;
|
||||
config->key_path = NULL;
|
||||
config->cert_path = NULL;
|
||||
config->ca_filepath = NULL;
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_dispose(
|
||||
struct wfp_client_config * config)
|
||||
{
|
||||
free(config->key_path);
|
||||
free(config->cert_path);
|
||||
free(config->ca_filepath);
|
||||
free(config);
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_userdata(
|
||||
struct wfp_client_config * config,
|
||||
void * user_data)
|
||||
{
|
||||
config->user_data = user_data;
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_keypath(
|
||||
struct wfp_client_config * config,
|
||||
char const * key_path)
|
||||
{
|
||||
free(config->key_path);
|
||||
config->key_path = strdup(key_path);
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_certpath(
|
||||
struct wfp_client_config * config,
|
||||
char const * cert_path)
|
||||
{
|
||||
free(config->cert_path);
|
||||
config->cert_path = strdup(cert_path);
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_ca_filepath(
|
||||
struct wfp_client_config * config,
|
||||
char const * ca_filepath)
|
||||
{
|
||||
free(config->ca_filepath);
|
||||
config->ca_filepath = strdup(ca_filepath);
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_onconnected(
|
||||
struct wfp_client_config * config,
|
||||
wfp_connected_fn * handler)
|
||||
{
|
||||
config->provider.connected = handler;
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_ondisconnected(
|
||||
struct wfp_client_config * config,
|
||||
wfp_disconnected_fn * handler)
|
||||
{
|
||||
config->provider.disconnected = handler;
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_onlookup(
|
||||
struct wfp_client_config * config,
|
||||
wfp_lookup_fn * handler)
|
||||
{
|
||||
config->provider.lookup = handler;
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_ongetattr(
|
||||
struct wfp_client_config * config,
|
||||
wfp_getattr_fn * handler)
|
||||
{
|
||||
config->provider.getattr = handler;
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_onreaddir(
|
||||
struct wfp_client_config * config,
|
||||
wfp_readdir_fn * handler)
|
||||
{
|
||||
config->provider.readdir = handler;
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_onopen(
|
||||
struct wfp_client_config * config,
|
||||
wfp_open_fn * handler)
|
||||
{
|
||||
config->provider.open = handler;
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_onclose(
|
||||
struct wfp_client_config * config,
|
||||
wfp_close_fn * handler)
|
||||
{
|
||||
config->provider.close = handler;
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_set_onread(
|
||||
struct wfp_client_config * config,
|
||||
wfp_read_fn * handler)
|
||||
{
|
||||
config->provider.read = handler;
|
||||
}
|
||||
|
||||
void wfp_impl_client_config_enable_authentication(
|
||||
struct wfp_client_config * config,
|
||||
wfp_get_credentials_fn * get_credentials)
|
||||
{
|
||||
config->provider.get_credentials = get_credentials;
|
||||
}
|
||||
82
lib/webfuse_provider/impl/client_config.h
Normal file
82
lib/webfuse_provider/impl/client_config.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#ifndef WF_PROVIDER_IMPL_CLIENT_CONFIG_H
|
||||
#define WF_PROVIDER_IMPL_CLIENT_CONFIG_H
|
||||
|
||||
#include "webfuse_provider/client_config.h"
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_client_config
|
||||
{
|
||||
struct wfp_provider provider;
|
||||
void * user_data;
|
||||
char * key_path;
|
||||
char * cert_path;
|
||||
char * ca_filepath;
|
||||
};
|
||||
|
||||
extern struct wfp_client_config * wfp_impl_client_config_create(void);
|
||||
|
||||
extern void wfp_impl_client_config_dispose(
|
||||
struct wfp_client_config * config);
|
||||
|
||||
extern void wfp_impl_client_config_set_userdata(
|
||||
struct wfp_client_config * config,
|
||||
void * user_data);
|
||||
|
||||
extern void wfp_impl_client_config_set_keypath(
|
||||
struct wfp_client_config * config,
|
||||
char const * key_path);
|
||||
|
||||
extern void wfp_impl_client_config_set_certpath(
|
||||
struct wfp_client_config * config,
|
||||
char const * cert_path);
|
||||
|
||||
extern void wfp_impl_client_config_set_ca_filepath(
|
||||
struct wfp_client_config * config,
|
||||
char const * ca_filepath);
|
||||
|
||||
extern void wfp_impl_client_config_set_onconnected(
|
||||
struct wfp_client_config * config,
|
||||
wfp_connected_fn * handler);
|
||||
|
||||
extern void wfp_impl_client_config_set_ondisconnected(
|
||||
struct wfp_client_config * config,
|
||||
wfp_disconnected_fn * handler);
|
||||
|
||||
extern void wfp_impl_client_config_set_onlookup(
|
||||
struct wfp_client_config * config,
|
||||
wfp_lookup_fn * handler);
|
||||
|
||||
extern void wfp_impl_client_config_set_ongetattr(
|
||||
struct wfp_client_config * config,
|
||||
wfp_getattr_fn * handler);
|
||||
|
||||
extern void wfp_impl_client_config_set_onreaddir(
|
||||
struct wfp_client_config * config,
|
||||
wfp_readdir_fn * handler);
|
||||
|
||||
extern void wfp_impl_client_config_set_onopen(
|
||||
struct wfp_client_config * config,
|
||||
wfp_open_fn * handler);
|
||||
|
||||
extern void wfp_impl_client_config_set_onclose(
|
||||
struct wfp_client_config * config,
|
||||
wfp_close_fn * handler);
|
||||
|
||||
extern void wfp_impl_client_config_set_onread(
|
||||
struct wfp_client_config * config,
|
||||
wfp_read_fn * handler);
|
||||
|
||||
extern void wfp_impl_client_config_enable_authentication(
|
||||
struct wfp_client_config * config,
|
||||
wfp_get_credentials_fn * get_credentials);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
340
lib/webfuse_provider/impl/client_protocol.c
Normal file
340
lib/webfuse_provider/impl/client_protocol.c
Normal file
@@ -0,0 +1,340 @@
|
||||
#include "webfuse_provider/impl/client_protocol.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#include "webfuse_provider/impl/client_config.h"
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
#include "webfuse_provider/impl/credentials.h"
|
||||
#include "webfuse_provider/impl/util.h"
|
||||
#include "webfuse_provider/impl/message.h"
|
||||
#include "webfuse_provider/impl/message_queue.h"
|
||||
#include "webfuse_provider/impl/container_of.h"
|
||||
#include "webfuse_provider/impl/url.h"
|
||||
#include "webfuse_provider/protocol_names.h"
|
||||
|
||||
#include "webfuse_provider/impl/timer/manager.h"
|
||||
|
||||
#include "webfuse_provider/impl/jsonrpc/response.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/request.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy.h"
|
||||
|
||||
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
||||
|
||||
static void wfp_impl_client_protocol_respond(
|
||||
json_t * response,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_client_protocol * protocol = (struct wfp_client_protocol *) user_data;
|
||||
|
||||
struct wf_message * message = wf_message_create(response);
|
||||
if (NULL != message)
|
||||
{
|
||||
wf_slist_append(&protocol->messages, &message->item);
|
||||
lws_callback_on_writable(protocol->wsi);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_impl_client_protocol_process(
|
||||
struct wfp_client_protocol * protocol,
|
||||
char const * data,
|
||||
size_t length)
|
||||
{
|
||||
json_t * message = json_loadb(data, length, 0, NULL);
|
||||
if (NULL != message)
|
||||
{
|
||||
if (wf_jsonrpc_is_response(message))
|
||||
{
|
||||
wf_jsonrpc_proxy_onresult(protocol->proxy, message);
|
||||
}
|
||||
|
||||
if (wf_jsonrpc_is_request(message))
|
||||
{
|
||||
struct wfp_impl_invokation_context context =
|
||||
{
|
||||
.provider = &protocol->provider,
|
||||
.user_data = protocol->user_data,
|
||||
.request = &protocol->request
|
||||
};
|
||||
|
||||
wfp_impl_provider_invoke(&context, message);
|
||||
}
|
||||
|
||||
json_decref(message);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
wfp_impl_client_protocol_on_add_filesystem_finished(
|
||||
void * user_data,
|
||||
json_t const * result,
|
||||
json_t const * WF_UNUSED_PARAM(error))
|
||||
{
|
||||
struct wfp_client_protocol * protocol = user_data;
|
||||
if (NULL == protocol->wsi) { return; }
|
||||
|
||||
if (NULL != result)
|
||||
{
|
||||
protocol->is_connected = true;
|
||||
protocol->provider.connected(protocol->user_data);
|
||||
}
|
||||
else
|
||||
{
|
||||
protocol->is_shutdown_requested = true;
|
||||
lws_callback_on_writable(protocol->wsi);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_impl_client_protocol_add_filesystem(
|
||||
struct wfp_client_protocol * protocol)
|
||||
{
|
||||
wf_jsonrpc_proxy_invoke(
|
||||
protocol->proxy,
|
||||
&wfp_impl_client_protocol_on_add_filesystem_finished,
|
||||
protocol,
|
||||
"add_filesystem",
|
||||
"s",
|
||||
"cprovider");
|
||||
}
|
||||
|
||||
static void
|
||||
wfp_impl_client_protocol_on_authenticate_finished(
|
||||
void * user_data,
|
||||
json_t const * result,
|
||||
json_t const * WF_UNUSED_PARAM(error))
|
||||
{
|
||||
struct wfp_client_protocol * protocol = user_data;
|
||||
if (NULL == protocol->wsi) { return; }
|
||||
|
||||
if (NULL != result)
|
||||
{
|
||||
wfp_impl_client_protocol_add_filesystem(protocol);
|
||||
}
|
||||
else
|
||||
{
|
||||
protocol->is_shutdown_requested = true;
|
||||
lws_callback_on_writable(protocol->wsi);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfp_impl_client_protocol_authenticate(
|
||||
struct wfp_client_protocol * protocol)
|
||||
{
|
||||
struct wfp_credentials credentials;
|
||||
wfp_impl_credentials_init(&credentials);
|
||||
|
||||
protocol->provider.get_credentials(&credentials, protocol->user_data);
|
||||
|
||||
char const * cred_type = wfp_impl_credentials_get_type(&credentials);
|
||||
json_t * creds = wfp_impl_credentials_get(&credentials);
|
||||
json_incref(creds);
|
||||
|
||||
wf_jsonrpc_proxy_invoke(
|
||||
protocol->proxy,
|
||||
&wfp_impl_client_protocol_on_authenticate_finished,
|
||||
protocol,
|
||||
"authenticate",
|
||||
"sj",
|
||||
cred_type, creds);
|
||||
|
||||
wfp_impl_credentials_cleanup(&credentials);
|
||||
}
|
||||
|
||||
static void wfp_impl_client_protocol_handshake(
|
||||
struct wfp_client_protocol * protocol)
|
||||
{
|
||||
if (wfp_impl_provider_is_authentication_enabled(&protocol->provider))
|
||||
{
|
||||
wfp_impl_client_protocol_authenticate(protocol);
|
||||
}
|
||||
else
|
||||
{
|
||||
wfp_impl_client_protocol_add_filesystem(protocol);
|
||||
}
|
||||
}
|
||||
|
||||
static int wfp_impl_client_protocol_callback(
|
||||
struct lws * wsi,
|
||||
enum lws_callback_reasons reason,
|
||||
void * WF_UNUSED_PARAM(user),
|
||||
void * in,
|
||||
size_t len)
|
||||
{
|
||||
int result = 0;
|
||||
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)
|
||||
{
|
||||
wf_timer_manager_check(protocol->timer_manager);
|
||||
|
||||
switch (reason)
|
||||
{
|
||||
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
||||
wfp_impl_client_protocol_handshake(protocol);
|
||||
break;
|
||||
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
||||
protocol->is_connected = false;
|
||||
protocol->provider.disconnected(protocol->user_data);
|
||||
break;
|
||||
case LWS_CALLBACK_CLIENT_CLOSED:
|
||||
protocol->is_connected = false;
|
||||
protocol->provider.disconnected(protocol->user_data);
|
||||
protocol->wsi = NULL;
|
||||
break;
|
||||
case LWS_CALLBACK_CLIENT_RECEIVE:
|
||||
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 (protocol->is_shutdown_requested)
|
||||
{
|
||||
result = 1;
|
||||
}
|
||||
else if (!wf_slist_empty(&protocol->messages))
|
||||
{
|
||||
struct wf_slist_item * item = wf_slist_remove_first(&protocol->messages);
|
||||
struct wf_message * message = wf_container_of(item, struct wf_message, item);
|
||||
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
|
||||
wf_message_dispose(message);
|
||||
|
||||
if (!wf_slist_empty(&protocol->messages))
|
||||
{
|
||||
lws_callback_on_writable(wsi);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool wfp_impl_client_protocol_send(
|
||||
json_t * request,
|
||||
void * user_data)
|
||||
{
|
||||
bool result = false;
|
||||
struct wfp_client_protocol * protocol = user_data;
|
||||
|
||||
struct wf_message * message = wf_message_create(request);
|
||||
if (NULL != message)
|
||||
{
|
||||
wf_slist_append(&protocol->messages, &message->item);
|
||||
lws_callback_on_writable(protocol->wsi);
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void wfp_impl_client_protocol_init(
|
||||
struct wfp_client_protocol * protocol,
|
||||
struct wfp_provider const * provider,
|
||||
void * user_data)
|
||||
{
|
||||
protocol->is_connected = false;
|
||||
protocol->is_shutdown_requested = false;
|
||||
wf_slist_init(&protocol->messages);
|
||||
|
||||
protocol->wsi = NULL;
|
||||
|
||||
protocol->request.respond = &wfp_impl_client_protocol_respond;
|
||||
protocol->request.user_data = protocol;
|
||||
|
||||
protocol->timer_manager = wf_timer_manager_create();
|
||||
protocol->proxy = wf_jsonrpc_proxy_create(protocol->timer_manager, WF_DEFAULT_TIMEOUT, &wfp_impl_client_protocol_send, protocol);
|
||||
|
||||
protocol->user_data = user_data;
|
||||
wfp_impl_provider_init_from_prototype(&protocol->provider, provider);
|
||||
}
|
||||
|
||||
void wfp_impl_client_protocol_cleanup(
|
||||
struct wfp_client_protocol * protocol)
|
||||
{
|
||||
wf_jsonrpc_proxy_dispose(protocol->proxy);
|
||||
wf_timer_manager_dispose(protocol->timer_manager);
|
||||
wf_message_queue_cleanup(&protocol->messages);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return protocol;
|
||||
}
|
||||
|
||||
void wfp_impl_client_protocol_dispose(
|
||||
struct wfp_client_protocol * protocol)
|
||||
{
|
||||
wfp_impl_client_protocol_cleanup(protocol);
|
||||
free(protocol);
|
||||
}
|
||||
|
||||
void wfp_impl_client_protocol_init_lws(
|
||||
struct wfp_client_protocol * protocol,
|
||||
struct lws_protocols * lws_protocol)
|
||||
{
|
||||
lws_protocol->name = WF_PROTOCOL_NAME_PROVIDER_CLIENT;
|
||||
lws_protocol->callback = &wfp_impl_client_protocol_callback;
|
||||
lws_protocol->per_session_data_size = 0;
|
||||
lws_protocol->user = protocol;
|
||||
}
|
||||
|
||||
void wfp_impl_client_protocol_connect(
|
||||
struct wfp_client_protocol * protocol,
|
||||
struct lws_context * context,
|
||||
char const * url)
|
||||
{
|
||||
struct wf_url url_data;
|
||||
bool const success = wf_url_init(&url_data, url);
|
||||
if (success)
|
||||
{
|
||||
struct lws_client_connect_info info;
|
||||
memset(&info, 0, sizeof(struct lws_client_connect_info));
|
||||
info.context = context;
|
||||
info.port = url_data.port;
|
||||
info.address = url_data.host;
|
||||
info.path = url_data.path;
|
||||
info.host = info.address;
|
||||
info.origin = info.address;
|
||||
info.ssl_connection = (url_data.use_tls) ? LCCSCF_USE_SSL : 0;
|
||||
info.protocol = WF_PROTOCOL_NAME_ADAPTER_SERVER;
|
||||
info.local_protocol_name = WF_PROTOCOL_NAME_PROVIDER_CLIENT;
|
||||
info.pwsi = &protocol->wsi;
|
||||
|
||||
lws_client_connect_via_info(&info);
|
||||
|
||||
wf_url_cleanup(&url_data);
|
||||
}
|
||||
else
|
||||
{
|
||||
protocol->provider.disconnected(protocol->user_data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void wfp_impl_client_protocol_disconnect(
|
||||
struct wfp_client_protocol * protocol)
|
||||
{
|
||||
if (protocol->is_connected)
|
||||
{
|
||||
protocol->is_shutdown_requested = true;
|
||||
lws_callback_on_writable(protocol->wsi);
|
||||
}
|
||||
else
|
||||
{
|
||||
protocol->provider.disconnected(protocol->user_data);
|
||||
}
|
||||
}
|
||||
63
lib/webfuse_provider/impl/client_protocol.h
Normal file
63
lib/webfuse_provider/impl/client_protocol.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef WF_PROVIDER_IMPL_CLIENT_PROTOCOL_H
|
||||
#define WF_PROVIDER_IMPL_CLIENT_PROTOCOL_H
|
||||
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
#include "webfuse_provider/impl/request.h"
|
||||
|
||||
#include "webfuse_provider/impl/slist.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_client_config;
|
||||
struct lws_protocols;
|
||||
struct lws_context;
|
||||
struct wf_jsonrpc_proxy;
|
||||
struct wf_timer_manager;
|
||||
|
||||
struct wfp_client_protocol
|
||||
{
|
||||
bool is_connected;
|
||||
bool is_shutdown_requested;
|
||||
struct wfp_request request;
|
||||
struct wfp_provider provider;
|
||||
void * user_data;
|
||||
struct lws * wsi;
|
||||
struct wf_timer_manager * timer_manager;
|
||||
struct wf_jsonrpc_proxy * proxy;
|
||||
struct wf_slist messages;
|
||||
};
|
||||
|
||||
extern void wfp_impl_client_protocol_init(
|
||||
struct wfp_client_protocol * protocol,
|
||||
struct wfp_provider const * provider,
|
||||
void * user_data);
|
||||
|
||||
extern void wfp_impl_client_protocol_cleanup(
|
||||
struct wfp_client_protocol * protocol);
|
||||
|
||||
extern struct wfp_client_protocol * wfp_impl_client_protocol_create(
|
||||
struct wfp_client_config const * config);
|
||||
|
||||
extern void wfp_impl_client_protocol_dispose(
|
||||
struct wfp_client_protocol * protocol);
|
||||
|
||||
extern void wfp_impl_client_protocol_init_lws(
|
||||
struct wfp_client_protocol * protocol,
|
||||
struct lws_protocols * lws_protocol);
|
||||
|
||||
extern void wfp_impl_client_protocol_connect(
|
||||
struct wfp_client_protocol * protocol,
|
||||
struct lws_context * context,
|
||||
char const * url);
|
||||
|
||||
extern void wfp_impl_client_protocol_disconnect(
|
||||
struct wfp_client_protocol * protocol);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
21
lib/webfuse_provider/impl/container_of.h
Normal file
21
lib/webfuse_provider/impl/container_of.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef WF_CONTAINER_OF_H
|
||||
#define WF_CONTAINER_OF_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define wf_container_of(pointer, type, member) \
|
||||
({ \
|
||||
const typeof( ((type *)0)->member ) * __member = (pointer); \
|
||||
(type *)( (char *)__member - offsetof(type, member) ); \
|
||||
})
|
||||
#else
|
||||
#define wf_container_of(pointer, type, member) \
|
||||
(type *) (((char *) pointer) - offsetof(type, member))
|
||||
#endif
|
||||
|
||||
#endif
|
||||
46
lib/webfuse_provider/impl/credentials.c
Normal file
46
lib/webfuse_provider/impl/credentials.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "webfuse_provider/impl/credentials.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void wfp_impl_credentials_init(
|
||||
struct wfp_credentials * credentials)
|
||||
{
|
||||
credentials->type = NULL;
|
||||
credentials->contents = json_object();
|
||||
}
|
||||
|
||||
void wfp_impl_credentials_cleanup(
|
||||
struct wfp_credentials * credentials)
|
||||
{
|
||||
free(credentials->type);
|
||||
json_decref(credentials->contents);
|
||||
}
|
||||
|
||||
void wfp_impl_credentials_set_type(
|
||||
struct wfp_credentials * credentials,
|
||||
char const * type)
|
||||
{
|
||||
free(credentials->type);
|
||||
credentials->type = strdup(type);
|
||||
}
|
||||
|
||||
void wfp_impl_credentials_add(
|
||||
struct wfp_credentials * credentials,
|
||||
char const * key,
|
||||
char const * value)
|
||||
{
|
||||
json_object_set_new(credentials->contents, key, json_string(value));
|
||||
}
|
||||
|
||||
char const * wfp_impl_credentials_get_type(
|
||||
struct wfp_credentials * credentials)
|
||||
{
|
||||
return credentials->type;
|
||||
}
|
||||
|
||||
json_t * wfp_impl_credentials_get(
|
||||
struct wfp_credentials * credentials)
|
||||
{
|
||||
return credentials->contents;
|
||||
}
|
||||
43
lib/webfuse_provider/impl/credentials.h
Normal file
43
lib/webfuse_provider/impl/credentials.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef WF_PROVIDER_IMPL_CREDENTIALS_H
|
||||
#define WF_PROVIDER_IMPL_CREDENTIALS_H
|
||||
|
||||
#include "webfuse_provider/credentials.h"
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_credentials
|
||||
{
|
||||
char * type;
|
||||
json_t * contents;
|
||||
};
|
||||
|
||||
extern void wfp_impl_credentials_init(
|
||||
struct wfp_credentials * credentials);
|
||||
|
||||
extern void wfp_impl_credentials_cleanup(
|
||||
struct wfp_credentials * credentials);
|
||||
|
||||
extern void wfp_impl_credentials_set_type(
|
||||
struct wfp_credentials * credentials,
|
||||
char const * type);
|
||||
|
||||
extern void wfp_impl_credentials_add(
|
||||
struct wfp_credentials * credentials,
|
||||
char const * key,
|
||||
char const * value);
|
||||
|
||||
extern char const * wfp_impl_credentials_get_type(
|
||||
struct wfp_credentials * credentials);
|
||||
|
||||
extern json_t * wfp_impl_credentials_get(
|
||||
struct wfp_credentials * credentials);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
42
lib/webfuse_provider/impl/dirbuffer.c
Normal file
42
lib/webfuse_provider/impl/dirbuffer.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "webfuse_provider/impl/dirbuffer.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void)
|
||||
{
|
||||
struct wfp_dirbuffer * buffer = malloc(sizeof(struct wfp_dirbuffer));
|
||||
buffer->entries = json_array();
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void wfp_impl_dirbuffer_dispose(
|
||||
struct wfp_dirbuffer * buffer)
|
||||
{
|
||||
if (NULL != buffer->entries)
|
||||
{
|
||||
json_decref(buffer->entries);
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void wfp_impl_dirbuffer_add(
|
||||
struct wfp_dirbuffer * buffer,
|
||||
char const * name,
|
||||
ino_t inode)
|
||||
{
|
||||
json_t * entry = json_object();
|
||||
json_object_set_new(entry, "name", json_string(name));
|
||||
json_object_set_new(entry, "inode", json_integer(inode));
|
||||
|
||||
json_array_append_new(buffer->entries, entry);
|
||||
}
|
||||
|
||||
json_t * wfp_impl_dirbuffer_take(
|
||||
struct wfp_dirbuffer * buffer)
|
||||
{
|
||||
json_t * entries = buffer->entries;
|
||||
|
||||
buffer->entries = NULL;
|
||||
return entries;
|
||||
}
|
||||
37
lib/webfuse_provider/impl/dirbuffer.h
Normal file
37
lib/webfuse_provider/impl/dirbuffer.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef WF_PROVIDER_IMPL_DIRBUFFER_H
|
||||
#define WF_PROVIDER_IMPL_DIRBUFFER_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_dirbuffer
|
||||
{
|
||||
json_t * entries;
|
||||
};
|
||||
|
||||
extern struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void);
|
||||
|
||||
extern void wfp_impl_dirbuffer_dispose(
|
||||
struct wfp_dirbuffer * buffer);
|
||||
|
||||
extern void wfp_impl_dirbuffer_add(
|
||||
struct wfp_dirbuffer * buffer,
|
||||
char const * name,
|
||||
ino_t inode);
|
||||
|
||||
extern json_t * wfp_impl_dirbuffer_take(
|
||||
struct wfp_dirbuffer * buffer);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
27
lib/webfuse_provider/impl/json_util.c
Normal file
27
lib/webfuse_provider/impl/json_util.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "webfuse_provider/impl/json_util.h"
|
||||
|
||||
int wf_impl_json_get_int(json_t const * object, char const * key, int default_value)
|
||||
{
|
||||
int result = default_value;
|
||||
|
||||
json_t * holder = json_object_get(object, key);
|
||||
if ((NULL != holder) && (json_is_integer(holder)))
|
||||
{
|
||||
result = json_integer_value(holder);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
wf_status
|
||||
wf_impl_jsonrpc_get_status(
|
||||
json_t const * error)
|
||||
{
|
||||
wf_status status = WF_GOOD;
|
||||
if (NULL != error)
|
||||
{
|
||||
status = wf_impl_json_get_int(error, "code", WF_BAD_FORMAT);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
26
lib/webfuse_provider/impl/json_util.h
Normal file
26
lib/webfuse_provider/impl/json_util.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef WF_JSON_UTIL_H
|
||||
#define WF_JSON_UTIL_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern int
|
||||
wf_impl_json_get_int(
|
||||
json_t const * object,
|
||||
char const * key,
|
||||
int default_value);
|
||||
|
||||
extern wf_status
|
||||
wf_impl_jsonrpc_get_status(
|
||||
json_t const * error);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
27
lib/webfuse_provider/impl/jsonrpc/error.c
Normal file
27
lib/webfuse_provider/impl/jsonrpc/error.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "webfuse_provider/impl/jsonrpc/error.h"
|
||||
|
||||
json_t *
|
||||
wf_jsonrpc_error(
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * error = json_object();
|
||||
json_object_set_new(error, "code", json_integer(code));
|
||||
json_object_set_new(error, "message", json_string(message));
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_propate_error(
|
||||
wf_jsonrpc_proxy_finished_fn * finised,
|
||||
void * user_data,
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * error = wf_jsonrpc_error(code, message);
|
||||
finised(user_data, NULL, error);
|
||||
|
||||
json_decref(error);
|
||||
}
|
||||
|
||||
29
lib/webfuse_provider/impl/jsonrpc/error.h
Normal file
29
lib/webfuse_provider/impl/jsonrpc/error.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef WF_JSONRPC_ERROR_H
|
||||
#define WF_JSONRPC_ERROR_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy_finished_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern json_t *
|
||||
wf_jsonrpc_error(
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_propate_error(
|
||||
wf_jsonrpc_proxy_finished_fn * finised,
|
||||
void * user_data,
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
24
lib/webfuse_provider/impl/jsonrpc/method.c
Normal file
24
lib/webfuse_provider/impl/jsonrpc/method.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "webfuse_provider/impl/jsonrpc/method.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_jsonrpc_method * wf_jsonrpc_method_create(
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_jsonrpc_method * method = malloc(sizeof(struct wf_jsonrpc_method));
|
||||
method->next = NULL;
|
||||
method->name = strdup(method_name);
|
||||
method->invoke = invoke;
|
||||
method->user_data = user_data;
|
||||
|
||||
return method;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_method_dispose(
|
||||
struct wf_jsonrpc_method * method)
|
||||
{
|
||||
free(method->name);
|
||||
free(method);
|
||||
}
|
||||
34
lib/webfuse_provider/impl/jsonrpc/method.h
Normal file
34
lib/webfuse_provider/impl/jsonrpc/method.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef WF_JSONRPC_METHOD_H
|
||||
#define WF_JSONRPC_METHOD_H
|
||||
|
||||
#include "webfuse_provider/impl/jsonrpc/method_invoke_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_method
|
||||
{
|
||||
struct wf_jsonrpc_method * next;
|
||||
char * name;
|
||||
wf_jsonrpc_method_invoke_fn * invoke;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
extern struct wf_jsonrpc_method *
|
||||
wf_jsonrpc_method_create(
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_method_dispose(
|
||||
struct wf_jsonrpc_method * method);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
24
lib/webfuse_provider/impl/jsonrpc/method_invoke_fn.h
Normal file
24
lib/webfuse_provider/impl/jsonrpc/method_invoke_fn.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef WF_JSONRPC_METHOD_INVOKE_FN_H
|
||||
#define WF_JSONRPC_METHOD_INVOKE_FN_H
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_request;
|
||||
|
||||
typedef void wf_jsonrpc_method_invoke_fn(
|
||||
struct wf_jsonrpc_request * request,
|
||||
char const * method_name,
|
||||
json_t * params,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
216
lib/webfuse_provider/impl/jsonrpc/proxy.c
Normal file
216
lib/webfuse_provider/impl/jsonrpc/proxy.c
Normal file
@@ -0,0 +1,216 @@
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy_intern.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/error.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
#include "webfuse_provider/impl/timer/timer.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_jsonrpc_proxy *
|
||||
wf_jsonrpc_proxy_create(
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_jsonrpc_proxy * proxy = malloc(sizeof(struct wf_jsonrpc_proxy));
|
||||
wf_jsonrpc_proxy_init(proxy, manager, timeout, send, user_data);
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_proxy_dispose(
|
||||
struct wf_jsonrpc_proxy * proxy)
|
||||
{
|
||||
wf_jsonrpc_proxy_cleanup(proxy);
|
||||
free(proxy);
|
||||
}
|
||||
|
||||
static void wf_jsonrpc_proxy_on_timeout(
|
||||
struct wf_timer * timer, void * proxy_ptr)
|
||||
{
|
||||
struct wf_jsonrpc_proxy * proxy = proxy_ptr;
|
||||
|
||||
if (proxy->request.is_pending)
|
||||
{
|
||||
wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
void * user_data = proxy->request.user_data;
|
||||
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.id = 0;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.finished = NULL;
|
||||
wf_timer_cancel(timer);
|
||||
|
||||
wf_jsonrpc_propate_error(finished, user_data, WF_BAD_TIMEOUT, "Timeout");
|
||||
}
|
||||
}
|
||||
|
||||
static json_t * wf_jsonrpc_request_create(
|
||||
char const * method,
|
||||
int id,
|
||||
char const * param_info,
|
||||
va_list args)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
json_object_set_new(request, "method", json_string(method));
|
||||
json_t * params = json_array();
|
||||
|
||||
for (char const * param_type = param_info; '\0' != *param_type; param_type++)
|
||||
{
|
||||
switch(*param_type)
|
||||
{
|
||||
case 's':
|
||||
{
|
||||
char const * const value = va_arg(args, char const *);
|
||||
json_array_append_new(params, json_string(value));
|
||||
}
|
||||
break;
|
||||
case 'i':
|
||||
{
|
||||
int const value = va_arg(args, int);
|
||||
json_array_append_new(params, json_integer(value));
|
||||
}
|
||||
break;
|
||||
case 'j':
|
||||
{
|
||||
json_t * const value = va_arg(args, json_t *);
|
||||
json_array_append_new(params, value);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);
|
||||
json_decref(params);
|
||||
json_decref(request);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
json_object_set_new(request, "params", params);
|
||||
if (0 != id)
|
||||
{
|
||||
json_object_set_new(request, "id", json_integer(id));
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_proxy_init(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
struct wf_timer_manager * timeout_manager,
|
||||
int timeout,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
proxy->send = send;
|
||||
proxy->timeout = timeout;
|
||||
proxy->user_data = user_data;
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.timer = wf_timer_create(timeout_manager,
|
||||
&wf_jsonrpc_proxy_on_timeout, proxy);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_proxy_cleanup(
|
||||
struct wf_jsonrpc_proxy * proxy)
|
||||
{
|
||||
if (proxy->request.is_pending)
|
||||
{
|
||||
void * user_data = proxy->request.user_data;
|
||||
wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.finished = NULL;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.id = 0;
|
||||
wf_timer_cancel(proxy->request.timer);
|
||||
|
||||
wf_jsonrpc_propate_error(finished, user_data, WF_BAD, "Bad: cancelled pending request during shutdown");
|
||||
}
|
||||
|
||||
wf_timer_dispose(proxy->request.timer);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_proxy_vinvoke(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
wf_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args)
|
||||
{
|
||||
if (!proxy->request.is_pending)
|
||||
{
|
||||
proxy->request.is_pending = true;
|
||||
proxy->request.finished = finished;
|
||||
proxy->request.user_data = user_data;
|
||||
proxy->request.id = 42;
|
||||
wf_timer_start(proxy->request.timer, proxy->timeout);
|
||||
|
||||
json_t * request = wf_jsonrpc_request_create(method_name, proxy->request.id, param_info, args);
|
||||
|
||||
bool const is_send = ((NULL != request) && (proxy->send(request, proxy->user_data)));
|
||||
if (!is_send)
|
||||
{
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.finished = NULL;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.id = 0;
|
||||
wf_timer_cancel(proxy->request.timer);
|
||||
|
||||
wf_jsonrpc_propate_error(finished, user_data, WF_BAD, "Bad: requenst is not sent");
|
||||
}
|
||||
|
||||
if (NULL != request)
|
||||
{
|
||||
json_decref(request);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
wf_jsonrpc_propate_error(finished, user_data, WF_BAD_BUSY, "Busy");
|
||||
}
|
||||
}
|
||||
|
||||
extern void wf_jsonrpc_proxy_vnotify(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args)
|
||||
{
|
||||
json_t * request = wf_jsonrpc_request_create(method_name, 0, param_info, args);
|
||||
|
||||
if (NULL != request)
|
||||
{
|
||||
proxy->send(request, proxy->user_data);
|
||||
json_decref(request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void wf_jsonrpc_proxy_onresult(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
json_t * message)
|
||||
{
|
||||
struct wf_jsonrpc_response response;
|
||||
wf_jsonrpc_response_init(&response, message);
|
||||
|
||||
if ((proxy->request.is_pending) && (response.id == proxy->request.id))
|
||||
{
|
||||
wf_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||
void * user_data = proxy->request.user_data;
|
||||
|
||||
proxy->request.is_pending = false;
|
||||
proxy->request.id = 0;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.finished = NULL;
|
||||
wf_timer_cancel(proxy->request.timer);
|
||||
|
||||
finished(user_data, response.result, response.error);
|
||||
}
|
||||
|
||||
wf_jsonrpc_response_cleanup(&response);
|
||||
}
|
||||
|
||||
74
lib/webfuse_provider/impl/jsonrpc/proxy.h
Normal file
74
lib/webfuse_provider/impl/jsonrpc/proxy.h
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef WF_JSONRPC_PROXY_H
|
||||
#define WF_JSONRPC_PROXY_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/send_fn.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy_finished_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_proxy;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern struct wf_jsonrpc_proxy *
|
||||
wf_jsonrpc_proxy_create(
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_jsonrpc_proxy_dispose(
|
||||
struct wf_jsonrpc_proxy * proxy);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// \brief Invokes a method.
|
||||
///
|
||||
/// Creates a method an sends it using the send function.
|
||||
/// Proxy keeps track of method invokation. If no response is returned within
|
||||
/// timeout, an error is propagated.
|
||||
///
|
||||
/// \param proxy pointer to proxy instance
|
||||
/// \param finished function which is called exactly once, either on success or
|
||||
/// on failure.
|
||||
/// \param method_name name of the method to invoke
|
||||
/// \param param_info types of the param (s = string, i = integer, j = json)
|
||||
/// \param ... params
|
||||
//------------------------------------------------------------------------------
|
||||
extern void wf_jsonrpc_proxy_invoke(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
wf_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
);
|
||||
|
||||
extern void wf_jsonrpc_proxy_notify(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
);
|
||||
|
||||
extern void wf_jsonrpc_proxy_onresult(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
json_t * message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
20
lib/webfuse_provider/impl/jsonrpc/proxy_finished_fn.h
Normal file
20
lib/webfuse_provider/impl/jsonrpc/proxy_finished_fn.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef WF_JSONRPC_PROXY_FINISHED_FN_H
|
||||
#define WF_JSONRPC_PROXY_FINISHED_FN_H
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef void wf_jsonrpc_proxy_finished_fn(
|
||||
void * user_data,
|
||||
json_t const * result,
|
||||
json_t const * error);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
62
lib/webfuse_provider/impl/jsonrpc/proxy_intern.h
Normal file
62
lib/webfuse_provider/impl/jsonrpc/proxy_intern.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef WF_JSONRPC_PROXY_INTERN_H
|
||||
#define WF_JSONRPC_PROXY_INTERN_H
|
||||
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy_finished_fn.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
|
||||
struct wf_jsonrpc_request
|
||||
{
|
||||
bool is_pending;
|
||||
wf_jsonrpc_proxy_finished_fn * finished;
|
||||
void * user_data;
|
||||
int id;
|
||||
struct wf_timer * timer;
|
||||
};
|
||||
|
||||
struct wf_jsonrpc_proxy
|
||||
{
|
||||
struct wf_jsonrpc_request request;
|
||||
int timeout;
|
||||
wf_jsonrpc_send_fn * send;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_proxy_init(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_proxy_cleanup(
|
||||
struct wf_jsonrpc_proxy * proxy);
|
||||
|
||||
extern void wf_jsonrpc_proxy_vinvoke(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
wf_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args);
|
||||
|
||||
extern void wf_jsonrpc_proxy_vnotify(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
va_list args);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
28
lib/webfuse_provider/impl/jsonrpc/proxy_variadic.c
Normal file
28
lib/webfuse_provider/impl/jsonrpc/proxy_variadic.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy_intern.h"
|
||||
|
||||
void wf_jsonrpc_proxy_invoke(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
wf_jsonrpc_proxy_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
wf_jsonrpc_proxy_vinvoke(proxy, finished, user_data, method_name, param_info, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
extern void wf_jsonrpc_proxy_notify(
|
||||
struct wf_jsonrpc_proxy * proxy,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, param_info);
|
||||
wf_jsonrpc_proxy_vnotify(proxy, method_name, param_info, args);
|
||||
va_end(args);
|
||||
}
|
||||
81
lib/webfuse_provider/impl/jsonrpc/request.c
Normal file
81
lib/webfuse_provider/impl/jsonrpc/request.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "webfuse_provider/impl/jsonrpc/request.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/error.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
struct wf_jsonrpc_request
|
||||
{
|
||||
int id;
|
||||
wf_jsonrpc_send_fn * send;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
bool
|
||||
wf_jsonrpc_is_request(
|
||||
json_t * message)
|
||||
{
|
||||
json_t * id = json_object_get(message, "id");
|
||||
json_t * method = json_object_get(message, "method");
|
||||
json_t * params = json_object_get(message, "params");
|
||||
|
||||
return (json_is_integer(id) && json_is_string(method) &&
|
||||
(json_is_array(params) || json_is_object(params)));
|
||||
}
|
||||
|
||||
|
||||
struct wf_jsonrpc_request *
|
||||
wf_jsonrpc_request_create(
|
||||
int id,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_jsonrpc_request * request = malloc(sizeof(struct wf_jsonrpc_request));
|
||||
request->id = id;
|
||||
request->send = send;
|
||||
request->user_data = user_data;
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_request_dispose(
|
||||
struct wf_jsonrpc_request * request)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
|
||||
void *
|
||||
wf_jsonrpc_request_get_userdata(
|
||||
struct wf_jsonrpc_request * request)
|
||||
{
|
||||
return request->user_data;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
wf_jsonrpc_respond(
|
||||
struct wf_jsonrpc_request * request,
|
||||
json_t * result)
|
||||
{
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", result);
|
||||
json_object_set_new(response, "id", json_integer(request->id));
|
||||
|
||||
request->send(response, request->user_data);
|
||||
json_decref(response);
|
||||
wf_jsonrpc_request_dispose(request);
|
||||
}
|
||||
|
||||
void wf_jsonrpc_respond_error(
|
||||
struct wf_jsonrpc_request * request,
|
||||
int code,
|
||||
char const * message)
|
||||
{
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "error", wf_jsonrpc_error(code, message));
|
||||
json_object_set_new(response, "id", json_integer(request->id));
|
||||
|
||||
request->send(response, request->user_data);
|
||||
json_decref(response);
|
||||
wf_jsonrpc_request_dispose(request);
|
||||
}
|
||||
|
||||
53
lib/webfuse_provider/impl/jsonrpc/request.h
Normal file
53
lib/webfuse_provider/impl/jsonrpc/request.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef WF_JSONRPC_REQUEST_H
|
||||
#define WF_JSONRPC_REQUEST_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_request;
|
||||
|
||||
extern bool wf_jsonrpc_is_request(
|
||||
json_t * message);
|
||||
|
||||
extern struct wf_jsonrpc_request *
|
||||
wf_jsonrpc_request_create(
|
||||
int id,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_jsonrpc_request_dispose(
|
||||
struct wf_jsonrpc_request * request);
|
||||
|
||||
extern void * wf_jsonrpc_request_get_userdata(
|
||||
struct wf_jsonrpc_request * request);
|
||||
|
||||
extern void wf_jsonrpc_respond(
|
||||
struct wf_jsonrpc_request * request,
|
||||
json_t * result);
|
||||
|
||||
extern void wf_jsonrpc_respond_error(
|
||||
struct wf_jsonrpc_request * request,
|
||||
int code,
|
||||
char const * message);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
68
lib/webfuse_provider/impl/jsonrpc/response.c
Normal file
68
lib/webfuse_provider/impl/jsonrpc/response.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/error.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
bool
|
||||
wf_jsonrpc_is_response(
|
||||
json_t * message)
|
||||
{
|
||||
json_t * id = json_object_get(message, "id");
|
||||
json_t * err = json_object_get(message, "error");
|
||||
json_t * result = json_object_get(message, "result");
|
||||
|
||||
return (json_is_integer(id) &&
|
||||
(json_is_object(err) || (NULL != result)));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
wf_jsonrpc_response_init(
|
||||
struct wf_jsonrpc_response * result,
|
||||
json_t * response)
|
||||
{
|
||||
result->id = -1;
|
||||
result->result = NULL;
|
||||
result->error = NULL;
|
||||
|
||||
json_t * id_holder = json_object_get(response, "id");
|
||||
if (!json_is_integer(id_holder))
|
||||
{
|
||||
result->error = wf_jsonrpc_error(WF_BAD_FORMAT, "invalid format: missing id");
|
||||
return;
|
||||
}
|
||||
|
||||
result->id = json_integer_value(id_holder);
|
||||
result->result = json_object_get(response, "result");
|
||||
if (NULL != result->result)
|
||||
{
|
||||
json_incref(result->result);
|
||||
}
|
||||
else
|
||||
{
|
||||
json_t * error = json_object_get(response, "error");
|
||||
if ((json_is_object(error)) && (json_is_integer(json_object_get(error, "code"))))
|
||||
{
|
||||
result->error = error;
|
||||
json_incref(result->error);
|
||||
}
|
||||
else
|
||||
{
|
||||
result->error = wf_jsonrpc_error(WF_BAD_FORMAT, "invalid format: invalid error object");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_response_cleanup(
|
||||
struct wf_jsonrpc_response * response)
|
||||
{
|
||||
if (NULL != response->result)
|
||||
{
|
||||
json_decref(response->result);
|
||||
}
|
||||
|
||||
if (NULL != response->error)
|
||||
{
|
||||
json_decref(response->error);
|
||||
}
|
||||
}
|
||||
22
lib/webfuse_provider/impl/jsonrpc/response.h
Normal file
22
lib/webfuse_provider/impl/jsonrpc/response.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef WF_JSONRPC_RESPONSE_H
|
||||
#define WF_JSONRPC_RESPONSE_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern bool wf_jsonrpc_is_response(
|
||||
json_t * message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
36
lib/webfuse_provider/impl/jsonrpc/response_intern.h
Normal file
36
lib/webfuse_provider/impl/jsonrpc/response_intern.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef WF_JSONRPC_RESPONSE_INTERN_H
|
||||
#define WF_JSONRPC_RESPONSE_INTERN_H
|
||||
|
||||
#include "webfuse_provider/impl/jsonrpc/response.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_response
|
||||
{
|
||||
json_t * result;
|
||||
json_t * error;
|
||||
int id;
|
||||
};
|
||||
|
||||
extern void wf_jsonrpc_response_init(
|
||||
struct wf_jsonrpc_response * response,
|
||||
json_t * message);
|
||||
|
||||
extern void wf_jsonrpc_response_cleanup(
|
||||
struct wf_jsonrpc_response * response);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
23
lib/webfuse_provider/impl/jsonrpc/send_fn.h
Normal file
23
lib/webfuse_provider/impl/jsonrpc/send_fn.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef WF_JSONRPC_SEND_FN_H
|
||||
#define WF_JSONRPC_SEND_FN_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef bool wf_jsonrpc_send_fn(
|
||||
json_t * request,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
128
lib/webfuse_provider/impl/jsonrpc/server.c
Normal file
128
lib/webfuse_provider/impl/jsonrpc/server.c
Normal file
@@ -0,0 +1,128 @@
|
||||
#include "webfuse_provider/impl/jsonrpc/server.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/method.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/request.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
#include "webfuse_provider/impl/util.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_jsonrpc_server
|
||||
{
|
||||
struct wf_jsonrpc_method * methods;
|
||||
};
|
||||
|
||||
static void
|
||||
wf_jsonrpc_server_init(
|
||||
struct wf_jsonrpc_server * server);
|
||||
|
||||
static void
|
||||
wf_jsonrpc_server_cleanup(
|
||||
struct wf_jsonrpc_server * server);
|
||||
|
||||
struct wf_jsonrpc_server *
|
||||
wf_jsonrpc_server_create(void)
|
||||
{
|
||||
struct wf_jsonrpc_server * server = malloc(sizeof(struct wf_jsonrpc_server));
|
||||
wf_jsonrpc_server_init(server);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
void
|
||||
wf_jsonrpc_server_dispose(
|
||||
struct wf_jsonrpc_server * server)
|
||||
{
|
||||
wf_jsonrpc_server_cleanup(server);
|
||||
free(server);
|
||||
}
|
||||
|
||||
static void wf_jsonrpc_server_init(
|
||||
struct wf_jsonrpc_server * server)
|
||||
{
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
static void wf_jsonrpc_server_cleanup(
|
||||
struct wf_jsonrpc_server * server)
|
||||
{
|
||||
struct wf_jsonrpc_method * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
struct wf_jsonrpc_method * next = current->next;
|
||||
wf_jsonrpc_method_dispose(current);
|
||||
current = next;
|
||||
}
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_server_add(
|
||||
struct wf_jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_jsonrpc_method * method = wf_jsonrpc_method_create(method_name, invoke, user_data);
|
||||
method->next = server->methods;
|
||||
server->methods = method;
|
||||
}
|
||||
|
||||
static void wf_jsonrpc_server_invalid_method_invoke(
|
||||
struct wf_jsonrpc_request * request,
|
||||
char const * WF_UNUSED_PARAM(method_name),
|
||||
json_t * WF_UNUSED_PARAM(params),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
wf_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED, "not implemented");
|
||||
}
|
||||
|
||||
static struct wf_jsonrpc_method const wf_jsonrpc_server_invalid_method =
|
||||
{
|
||||
.next = NULL,
|
||||
.name = "<invalid>",
|
||||
.invoke = &wf_jsonrpc_server_invalid_method_invoke,
|
||||
.user_data = NULL
|
||||
};
|
||||
|
||||
static struct wf_jsonrpc_method const *
|
||||
wf_jsonrpc_server_get_method(
|
||||
struct wf_jsonrpc_server * server,
|
||||
char const * method_name)
|
||||
{
|
||||
struct wf_jsonrpc_method const * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
if (0 == strcmp(method_name, current->name))
|
||||
{
|
||||
return current;
|
||||
}
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
return &wf_jsonrpc_server_invalid_method;
|
||||
}
|
||||
|
||||
void wf_jsonrpc_server_process(
|
||||
struct wf_jsonrpc_server * server,
|
||||
json_t * request_data,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
json_t * method_holder = json_object_get(request_data, "method");
|
||||
json_t * params = json_object_get(request_data, "params");
|
||||
json_t * id_holder = json_object_get(request_data, "id");
|
||||
|
||||
if (json_is_string(method_holder) &&
|
||||
(json_is_array(params) || (json_is_object(params))) &&
|
||||
json_is_integer(id_holder))
|
||||
{
|
||||
char const * method_name = json_string_value(method_holder);
|
||||
int id = json_integer_value(id_holder);
|
||||
struct wf_jsonrpc_request * request = wf_jsonrpc_request_create(id, send, user_data);
|
||||
struct wf_jsonrpc_method const * method = wf_jsonrpc_server_get_method(server, method_name);
|
||||
|
||||
method->invoke(request, method_name, params, method->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
45
lib/webfuse_provider/impl/jsonrpc/server.h
Normal file
45
lib/webfuse_provider/impl/jsonrpc/server.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef WF_JSONRPC_SERVER_H
|
||||
#define WF_JSONRPC_SERVER_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse_provider/impl/jsonrpc/method_invoke_fn.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_jsonrpc_server;
|
||||
|
||||
extern struct wf_jsonrpc_server *
|
||||
wf_jsonrpc_server_create(void);
|
||||
|
||||
extern void
|
||||
wf_jsonrpc_server_dispose(
|
||||
struct wf_jsonrpc_server * server);
|
||||
|
||||
extern void wf_jsonrpc_server_add(
|
||||
struct wf_jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
wf_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_jsonrpc_server_process(
|
||||
struct wf_jsonrpc_server * server,
|
||||
json_t * request,
|
||||
wf_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
18
lib/webfuse_provider/impl/lws_log.c
Normal file
18
lib/webfuse_provider/impl/lws_log.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "webfuse_provider/impl/lws_log.h"
|
||||
#include <stdbool.h>
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#define WF_LWSLOG_DISABLE 0
|
||||
|
||||
static bool wf_lwslog_is_diabled = false;
|
||||
|
||||
void wf_lwslog_disable(void)
|
||||
{
|
||||
if (!wf_lwslog_is_diabled)
|
||||
{
|
||||
lws_set_log_level(WF_LWSLOG_DISABLE, NULL);
|
||||
wf_lwslog_is_diabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
15
lib/webfuse_provider/impl/lws_log.h
Normal file
15
lib/webfuse_provider/impl/lws_log.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef WF_LWS_LOG_H
|
||||
#define WF_LWS_LOG_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wf_lwslog_disable(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
28
lib/webfuse_provider/impl/message.c
Normal file
28
lib/webfuse_provider/impl/message.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "webfuse_provider/impl/message.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <libwebsockets.h>
|
||||
|
||||
extern struct wf_message * wf_message_create(json_t const * value)
|
||||
{
|
||||
struct wf_message * message = NULL;
|
||||
size_t const length = json_dumpb(value, NULL, 0, JSON_COMPACT);
|
||||
|
||||
if (0 < length)
|
||||
{
|
||||
char * data = malloc(sizeof(struct wf_message) + LWS_PRE + length);
|
||||
message = (struct wf_message *) data;
|
||||
message->data = &data[sizeof(struct wf_message) + LWS_PRE];
|
||||
message->length = length;
|
||||
|
||||
json_dumpb(value, message->data, length, JSON_COMPACT);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
void wf_message_dispose(
|
||||
struct wf_message * message)
|
||||
{
|
||||
free(message);
|
||||
}
|
||||
36
lib/webfuse_provider/impl/message.h
Normal file
36
lib/webfuse_provider/impl/message.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef WF_MESSAGE_H
|
||||
#define WF_MESSAGE_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse_provider/impl/slist.h"
|
||||
|
||||
struct wf_message
|
||||
{
|
||||
struct wf_slist_item item;
|
||||
char * data;
|
||||
size_t length;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern struct wf_message * wf_message_create(
|
||||
json_t const * value);
|
||||
|
||||
extern void wf_message_dispose(
|
||||
struct wf_message * message);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
17
lib/webfuse_provider/impl/message_queue.c
Normal file
17
lib/webfuse_provider/impl/message_queue.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "webfuse_provider/impl/message_queue.h"
|
||||
#include "webfuse_provider/impl/message.h"
|
||||
#include "webfuse_provider/impl/container_of.h"
|
||||
|
||||
void wf_message_queue_cleanup(
|
||||
struct wf_slist * queue)
|
||||
{
|
||||
struct wf_slist_item * item = wf_slist_first(queue);
|
||||
while (NULL != item)
|
||||
{
|
||||
struct wf_slist_item * next = item->next;
|
||||
struct wf_message * message = wf_container_of(item, struct wf_message, item);
|
||||
wf_message_dispose(message);
|
||||
item = next;
|
||||
}
|
||||
wf_slist_init(queue);
|
||||
}
|
||||
19
lib/webfuse_provider/impl/message_queue.h
Normal file
19
lib/webfuse_provider/impl/message_queue.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef WF_MESSAGE_QUEUE_H
|
||||
#define WF_MESSAGE_QUEUE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_slist;
|
||||
|
||||
extern void wf_message_queue_cleanup(
|
||||
struct wf_slist * queue);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
38
lib/webfuse_provider/impl/operation/close.c
Normal file
38
lib/webfuse_provider/impl/operation/close.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "webfuse_provider/impl/operation/close.h"
|
||||
#include <limits.h>
|
||||
#include "webfuse_provider/impl/util.h"
|
||||
|
||||
void wfp_impl_close(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int WF_UNUSED_PARAM(id))
|
||||
{
|
||||
size_t const param_count = json_array_size(params);
|
||||
if (4 == param_count)
|
||||
{
|
||||
json_t * inode_holder = json_array_get(params, 1);
|
||||
json_t * handle_holder = json_array_get(params, 2);
|
||||
json_t * flags_holder = json_array_get(params, 3);
|
||||
|
||||
if (json_is_integer(inode_holder) &&
|
||||
json_is_integer(handle_holder) &&
|
||||
json_is_integer(flags_holder))
|
||||
{
|
||||
ino_t inode = (ino_t) json_integer_value(inode_holder);
|
||||
uint32_t handle = (uint32_t) (json_integer_value(handle_holder) & UINT32_MAX);
|
||||
int flags = json_integer_value(flags_holder);
|
||||
|
||||
context->provider->close(inode, handle, flags, context->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void wfp_impl_close_default(
|
||||
ino_t WF_UNUSED_PARAM(inode),
|
||||
uint32_t WF_UNUSED_PARAM(handle),
|
||||
int WF_UNUSED_PARAM(flags),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
// empty
|
||||
}
|
||||
26
lib/webfuse_provider/impl/operation/close.h
Normal file
26
lib/webfuse_provider/impl/operation/close.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef WF_PROVIDER_IMPL_OPERATION_CLOSE_H
|
||||
#define WF_PROVIDER_IMPL_OPERATION_CLOSE_H
|
||||
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wfp_impl_close(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wfp_impl_close_default(
|
||||
ino_t inode,
|
||||
uint32_t handle,
|
||||
int flags,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
22
lib/webfuse_provider/impl/operation/error.h
Normal file
22
lib/webfuse_provider/impl/operation/error.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef WFP_OPERATION_IMPL_ERROR_H
|
||||
#define WFP_OPERATION_IMPL_ERROR_H
|
||||
|
||||
#include "webfuse_provider/api.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_request;
|
||||
|
||||
extern WFP_API void wfp_impl_respond_error(
|
||||
struct wfp_request * request,
|
||||
wf_status status);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
64
lib/webfuse_provider/impl/operation/getattr.c
Normal file
64
lib/webfuse_provider/impl/operation/getattr.c
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "webfuse_provider/impl/operation/getattr.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "webfuse_provider/impl/operation/error.h"
|
||||
#include "webfuse_provider/impl/request.h"
|
||||
#include "webfuse_provider/impl/util.h"
|
||||
|
||||
|
||||
void wfp_impl_getattr(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
size_t const count = json_array_size(params);
|
||||
if (2 == count)
|
||||
{
|
||||
json_t * inode_holder = json_array_get(params, 1);
|
||||
|
||||
if (json_is_integer(inode_holder))
|
||||
{
|
||||
ino_t inode = (ino_t) json_integer_value(inode_holder);
|
||||
struct wfp_request * request = wfp_impl_request_create(context->request, id);
|
||||
|
||||
context->provider->getattr(request, inode, context->user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void wfp_impl_getattr_default(
|
||||
struct wfp_request * request,
|
||||
ino_t WF_UNUSED_PARAM(inode),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
wfp_impl_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
|
||||
void wfp_impl_respond_getattr(
|
||||
struct wfp_request * request,
|
||||
struct stat const * stat)
|
||||
{
|
||||
bool const is_file = (0 != (stat->st_mode & S_IFREG));
|
||||
bool const is_dir = (0 != (stat->st_mode & S_IFDIR));
|
||||
|
||||
json_t * result = json_object();
|
||||
json_object_set_new(result, "inode", json_integer(stat->st_ino));
|
||||
json_object_set_new(result, "mode", json_integer(stat->st_mode & 0777));
|
||||
json_object_set_new(result, "atime", json_integer(stat->st_atime));
|
||||
json_object_set_new(result, "mtime", json_integer(stat->st_mtime));
|
||||
json_object_set_new(result, "ctime", json_integer(stat->st_ctime));
|
||||
|
||||
if (is_file)
|
||||
{
|
||||
json_object_set_new(result, "type", json_string("file"));
|
||||
json_object_set_new(result, "size", json_integer(stat->st_size));
|
||||
}
|
||||
|
||||
if (is_dir)
|
||||
{
|
||||
json_object_set_new(result, "type", json_string("dir"));
|
||||
}
|
||||
|
||||
wfp_impl_respond(request, result);
|
||||
}
|
||||
29
lib/webfuse_provider/impl/operation/getattr.h
Normal file
29
lib/webfuse_provider/impl/operation/getattr.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef WF_PROVIDER_IMPL_OPERATION_GETATTR_H
|
||||
#define WF_PROVIDER_IMPL_OPERATION_GETATTR_H
|
||||
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wfp_impl_respond_getattr(
|
||||
struct wfp_request * request,
|
||||
struct stat const * stat);
|
||||
|
||||
extern void wfp_impl_getattr(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wfp_impl_getattr_default(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
68
lib/webfuse_provider/impl/operation/lookup.c
Normal file
68
lib/webfuse_provider/impl/operation/lookup.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "webfuse_provider/impl/operation/lookup.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "webfuse_provider/impl/operation/error.h"
|
||||
#include "webfuse_provider/impl/request.h"
|
||||
#include "webfuse_provider/impl/util.h"
|
||||
|
||||
void wfp_impl_lookup(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
size_t const count = json_array_size(params);
|
||||
if (3 == count)
|
||||
{
|
||||
json_t * inode_holder = json_array_get(params, 1);
|
||||
json_t * name_holder = json_array_get(params, 2);
|
||||
|
||||
if (json_is_integer(inode_holder) &&
|
||||
json_is_string(name_holder))
|
||||
{
|
||||
ino_t inode = json_integer_value(inode_holder);
|
||||
char const * name = json_string_value(name_holder);
|
||||
|
||||
struct wfp_request * request = wfp_impl_request_create(context->request, id);
|
||||
context->provider->lookup(request, inode, name, context->user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void wfp_impl_respond_lookup(
|
||||
struct wfp_request * request,
|
||||
struct stat const * stat)
|
||||
{
|
||||
bool const is_file = (0 != (stat->st_mode & S_IFREG));
|
||||
bool const is_dir = (0 != (stat->st_mode & S_IFDIR));
|
||||
|
||||
json_t * result = json_object();
|
||||
json_object_set_new(result, "inode", json_integer(stat->st_ino));
|
||||
json_object_set_new(result, "mode", json_integer(stat->st_mode & 0777));
|
||||
json_object_set_new(result, "atime", json_integer(stat->st_atime));
|
||||
json_object_set_new(result, "mtime", json_integer(stat->st_mtime));
|
||||
json_object_set_new(result, "ctime", json_integer(stat->st_ctime));
|
||||
|
||||
if (is_file)
|
||||
{
|
||||
json_object_set_new(result, "type", json_string("file"));
|
||||
json_object_set_new(result, "size", json_integer(stat->st_size));
|
||||
}
|
||||
|
||||
if (is_dir)
|
||||
{
|
||||
json_object_set_new(result, "type", json_string("dir"));
|
||||
}
|
||||
|
||||
wfp_impl_respond(request, result);
|
||||
}
|
||||
|
||||
void wfp_impl_lookup_default(
|
||||
struct wfp_request * request,
|
||||
ino_t WF_UNUSED_PARAM(parent),
|
||||
char const * WF_UNUSED_PARAM(name),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
wfp_impl_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
|
||||
30
lib/webfuse_provider/impl/operation/lookup.h
Normal file
30
lib/webfuse_provider/impl/operation/lookup.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef WF_PROVIDER_IMPL_OPERATION_LOOKUP_H
|
||||
#define WF_PROVIDER_IMPL_OPERATION_LOOKUP_H
|
||||
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wfp_impl_respond_lookup(
|
||||
struct wfp_request * request,
|
||||
struct stat const * stat);
|
||||
|
||||
extern void wfp_impl_lookup(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wfp_impl_lookup_default(
|
||||
struct wfp_request * request,
|
||||
ino_t parent,
|
||||
char const * name,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
47
lib/webfuse_provider/impl/operation/open.c
Normal file
47
lib/webfuse_provider/impl/operation/open.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "webfuse_provider/impl/operation/open.h"
|
||||
#include "webfuse_provider/impl/operation/error.h"
|
||||
#include "webfuse_provider/impl/request.h"
|
||||
#include "webfuse_provider/impl/util.h"
|
||||
|
||||
void wfp_impl_open(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
size_t const count = json_array_size(params);
|
||||
if (3 == count)
|
||||
{
|
||||
json_t * inode_holder = json_array_get(params, 1);
|
||||
json_t * flags_holder = json_array_get(params, 2);
|
||||
|
||||
if (json_is_integer(inode_holder) &&
|
||||
json_is_integer(flags_holder))
|
||||
{
|
||||
ino_t inode = (ino_t) json_integer_value(inode_holder);
|
||||
int flags = (ino_t) json_integer_value(flags_holder);
|
||||
|
||||
struct wfp_request * request = wfp_impl_request_create(context->request, id);
|
||||
|
||||
context->provider->open(request, inode, flags, context->user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void wfp_impl_open_default(
|
||||
struct wfp_request * request,
|
||||
ino_t WF_UNUSED_PARAM(inode),
|
||||
int WF_UNUSED_PARAM(flags),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
wfp_impl_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
|
||||
void wfp_impl_respond_open(
|
||||
struct wfp_request * request,
|
||||
uint32_t handle)
|
||||
{
|
||||
json_t * result = json_object();
|
||||
json_object_set_new(result, "handle", json_integer((int) handle));
|
||||
|
||||
wfp_impl_respond(request, result);
|
||||
}
|
||||
30
lib/webfuse_provider/impl/operation/open.h
Normal file
30
lib/webfuse_provider/impl/operation/open.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef WF_PROVIDER_IMPL_OPERATION_OPEN_H
|
||||
#define WF_PROVIDER_IMPL_OPERATION_OPEN_H
|
||||
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wfp_impl_respond_open(
|
||||
struct wfp_request * request,
|
||||
uint32_t handle);
|
||||
|
||||
extern void wfp_impl_open(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wfp_impl_open_default(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
int flags,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
78
lib/webfuse_provider/impl/operation/read.c
Normal file
78
lib/webfuse_provider/impl/operation/read.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "webfuse_provider/impl/operation/read.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "webfuse_provider/impl/operation/error.h"
|
||||
#include "webfuse_provider/impl/request.h"
|
||||
#include "webfuse_provider/impl/util.h"
|
||||
#include "webfuse_provider/impl/base64.h"
|
||||
|
||||
void wfp_impl_read(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
size_t const count = json_array_size(params);
|
||||
if (5 == count)
|
||||
{
|
||||
json_t * inode_holder = json_array_get(params, 1);
|
||||
json_t * handle_holder = json_array_get(params, 2);
|
||||
json_t * offset_holder = json_array_get(params, 3);
|
||||
json_t * length_holder = json_array_get(params, 4);
|
||||
|
||||
if (json_is_integer(inode_holder) &&
|
||||
json_is_integer(handle_holder) &&
|
||||
json_is_integer(offset_holder) &&
|
||||
json_is_integer(length_holder))
|
||||
{
|
||||
ino_t inode = (ino_t) json_integer_value(inode_holder);
|
||||
int handle = json_integer_value(handle_holder);
|
||||
size_t offset = json_integer_value(offset_holder);
|
||||
size_t length = json_integer_value(length_holder);
|
||||
struct wfp_request * request = wfp_impl_request_create(context->request, id);
|
||||
|
||||
context->provider->read(request, inode, handle, offset, length, context->user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void wfp_impl_read_default(
|
||||
struct wfp_request * request,
|
||||
ino_t WF_UNUSED_PARAM(inode),
|
||||
uint32_t WF_UNUSED_PARAM(handle),
|
||||
size_t WF_UNUSED_PARAM(offset),
|
||||
size_t WF_UNUSED_PARAM(length),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
wfp_impl_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
|
||||
void wfp_impl_respond_read(
|
||||
struct wfp_request * request,
|
||||
char const * data,
|
||||
size_t length)
|
||||
{
|
||||
if (0 < length)
|
||||
{
|
||||
size_t const size = wf_base64_encoded_size(length) + 1;
|
||||
char * buffer = malloc(size);
|
||||
wf_base64_encode((uint8_t const *) data, length, buffer, size);
|
||||
|
||||
json_t * result = json_object();
|
||||
json_object_set_new(result, "data", json_string(buffer));
|
||||
json_object_set_new(result, "format", json_string("base64"));
|
||||
json_object_set_new(result, "count", json_integer((int) length));
|
||||
|
||||
wfp_impl_respond(request, result);
|
||||
free(buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
json_t * result = json_object();
|
||||
json_object_set_new(result, "data", json_string(""));
|
||||
json_object_set_new(result, "format", json_string("identity"));
|
||||
json_object_set_new(result, "count", json_integer(0));
|
||||
|
||||
wfp_impl_respond(request, result);
|
||||
}
|
||||
}
|
||||
33
lib/webfuse_provider/impl/operation/read.h
Normal file
33
lib/webfuse_provider/impl/operation/read.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef WF_PROVIDER_IMPL_OPERATION_READ_H
|
||||
#define WF_PROVIDER_IMPL_OPERATION_READ_H
|
||||
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wfp_impl_respond_read(
|
||||
struct wfp_request * request,
|
||||
char const * data,
|
||||
size_t length);
|
||||
|
||||
extern void wfp_impl_read(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wfp_impl_read_default(
|
||||
struct wfp_request * request,
|
||||
ino_t inode,
|
||||
uint32_t handle,
|
||||
size_t offset,
|
||||
size_t length,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
42
lib/webfuse_provider/impl/operation/readdir.c
Normal file
42
lib/webfuse_provider/impl/operation/readdir.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "webfuse_provider/impl/operation/readdir.h"
|
||||
#include "webfuse_provider/impl/operation/error.h"
|
||||
#include "webfuse_provider/impl/dirbuffer.h"
|
||||
#include "webfuse_provider/impl/request.h"
|
||||
#include "webfuse_provider/impl/util.h"
|
||||
|
||||
void wfp_impl_readdir(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
size_t const count = json_array_size(params);
|
||||
if (2 == count)
|
||||
{
|
||||
json_t * inode_holder = json_array_get(params, 1);
|
||||
|
||||
if (json_is_integer(inode_holder))
|
||||
{
|
||||
ino_t inode = (ino_t) json_integer_value(inode_holder);
|
||||
struct wfp_request * request = wfp_impl_request_create(context->request, id);
|
||||
|
||||
context->provider->readdir(request, inode, context->user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void wfp_impl_readdir_default(
|
||||
struct wfp_request * request,
|
||||
ino_t WF_UNUSED_PARAM(directory),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
wfp_impl_respond_error(request, WF_BAD_NOENTRY);
|
||||
}
|
||||
|
||||
void wfp_impl_respond_readdir(
|
||||
struct wfp_request * request,
|
||||
struct wfp_dirbuffer * dirbuffer)
|
||||
{
|
||||
json_t * result = wfp_impl_dirbuffer_take(dirbuffer);
|
||||
wfp_impl_respond(request, result);
|
||||
}
|
||||
|
||||
29
lib/webfuse_provider/impl/operation/readdir.h
Normal file
29
lib/webfuse_provider/impl/operation/readdir.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef WF_PROVIDER_IMPL_OPERATION_READDIR_H
|
||||
#define WF_PROVIDER_IMPL_OPERATION_READDIR_H
|
||||
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wfp_impl_respond_readdir(
|
||||
struct wfp_request * request,
|
||||
struct wfp_dirbuffer * dirbuffer);
|
||||
|
||||
extern void wfp_impl_readdir(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wfp_impl_readdir_default(
|
||||
struct wfp_request * request,
|
||||
ino_t directory,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
125
lib/webfuse_provider/impl/provider.c
Normal file
125
lib/webfuse_provider/impl/provider.c
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "webfuse_provider/impl/request.h"
|
||||
#include "webfuse_provider/impl/operation/lookup.h"
|
||||
#include "webfuse_provider/impl/operation/getattr.h"
|
||||
#include "webfuse_provider/impl/operation/readdir.h"
|
||||
#include "webfuse_provider/impl/operation/open.h"
|
||||
#include "webfuse_provider/impl/operation/close.h"
|
||||
#include "webfuse_provider/impl/operation/read.h"
|
||||
|
||||
typedef void wfp_impl_invoke_fn(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
|
||||
struct wfp_impl_method
|
||||
{
|
||||
char const * name;
|
||||
wfp_impl_invoke_fn * invoke;
|
||||
bool is_notification;
|
||||
};
|
||||
|
||||
static void wfp_impl_provider_invoke_method(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
char const * method_name,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
static struct wfp_impl_method const methods[] =
|
||||
{
|
||||
{"lookup", &wfp_impl_lookup, false},
|
||||
{"getattr", &wfp_impl_getattr, false},
|
||||
{"readdir", &wfp_impl_readdir, false},
|
||||
{"open", &wfp_impl_open, false},
|
||||
{"close", &wfp_impl_close, true},
|
||||
{"read", &wfp_impl_read, false}
|
||||
};
|
||||
static size_t const count = sizeof(methods) / sizeof(methods[0]);
|
||||
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
struct wfp_impl_method const * method = &methods[i];
|
||||
if (0 == strcmp(method_name, method->name))
|
||||
{
|
||||
if ((0 < id) || (method->is_notification))
|
||||
{
|
||||
method->invoke(context, params, id);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void wfp_impl_provider_init(
|
||||
struct wfp_provider * provider)
|
||||
{
|
||||
provider->lookup = &wfp_impl_lookup_default;
|
||||
provider->getattr = &wfp_impl_getattr_default;
|
||||
provider->readdir = &wfp_impl_readdir_default;
|
||||
provider->open = &wfp_impl_open_default;
|
||||
provider->close = &wfp_impl_close_default;
|
||||
provider->read = &wfp_impl_read_default;
|
||||
provider->connected = &wfp_impl_connected_default;
|
||||
provider->disconnected = &wfp_impl_disconnected_default;
|
||||
provider->get_credentials = NULL;
|
||||
}
|
||||
|
||||
void wfp_impl_provider_init_from_prototype(
|
||||
struct wfp_provider * provider,
|
||||
struct wfp_provider const * prototype)
|
||||
{
|
||||
provider->lookup = prototype->lookup;
|
||||
provider->getattr = prototype->getattr;
|
||||
provider->readdir = prototype->readdir;
|
||||
provider->open = prototype->open;
|
||||
provider->close = prototype->close;
|
||||
provider->read = prototype->read;
|
||||
provider->connected = prototype->connected;
|
||||
provider->disconnected = prototype->disconnected;
|
||||
provider->get_credentials = prototype->get_credentials;
|
||||
}
|
||||
|
||||
void wfp_impl_provider_invoke(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * request)
|
||||
{
|
||||
json_t * method_holder = json_object_get(request, "method");
|
||||
json_t * params = json_object_get(request, "params");
|
||||
json_t * id_holder = json_object_get(request, "id");
|
||||
|
||||
if ((json_is_string(method_holder)) && (json_is_array(params)))
|
||||
{
|
||||
char const * method = json_string_value(method_holder);
|
||||
int id = json_is_integer(id_holder) ? json_integer_value(id_holder) : 0;
|
||||
|
||||
wfp_impl_provider_invoke_method(context, method, params, id);
|
||||
}
|
||||
}
|
||||
|
||||
void wfp_impl_connected_default(
|
||||
void * user_data)
|
||||
{
|
||||
(void) user_data;
|
||||
|
||||
// empty
|
||||
}
|
||||
|
||||
void wfp_impl_disconnected_default(
|
||||
void * user_data)
|
||||
{
|
||||
(void) user_data;
|
||||
|
||||
// empty
|
||||
}
|
||||
|
||||
bool wfp_impl_provider_is_authentication_enabled(
|
||||
struct wfp_provider * provider)
|
||||
{
|
||||
return (NULL != provider->get_credentials);
|
||||
}
|
||||
|
||||
62
lib/webfuse_provider/impl/provider.h
Normal file
62
lib/webfuse_provider/impl/provider.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef WF_PROVIDER_IMPL_PROVIDER_H
|
||||
#define WF_PROVIDER_IMPL_PROVIDER_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse_provider/client_config.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_provider
|
||||
{
|
||||
wfp_connected_fn * connected;
|
||||
wfp_disconnected_fn * disconnected;
|
||||
wfp_lookup_fn * lookup;
|
||||
wfp_getattr_fn * getattr;
|
||||
wfp_readdir_fn * readdir;
|
||||
wfp_open_fn * open;
|
||||
wfp_close_fn * close;
|
||||
wfp_read_fn * read;
|
||||
wfp_get_credentials_fn * get_credentials;
|
||||
};
|
||||
|
||||
struct wfp_impl_invokation_context
|
||||
{
|
||||
struct wfp_provider const * provider;
|
||||
void * user_data;
|
||||
struct wfp_request * request;
|
||||
};
|
||||
|
||||
extern void wfp_impl_provider_init(
|
||||
struct wfp_provider * provider);
|
||||
|
||||
extern void wfp_impl_provider_init_from_prototype(
|
||||
struct wfp_provider * provider,
|
||||
struct wfp_provider const * prototype);
|
||||
|
||||
|
||||
extern void wfp_impl_provider_invoke(
|
||||
struct wfp_impl_invokation_context * context,
|
||||
json_t * request);
|
||||
|
||||
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
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
52
lib/webfuse_provider/impl/request.c
Normal file
52
lib/webfuse_provider/impl/request.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "webfuse_provider/impl/request.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "webfuse_provider/impl/operation/error.h"
|
||||
|
||||
struct wfp_request * wfp_impl_request_create(
|
||||
struct wfp_request * prototype,
|
||||
int id)
|
||||
{
|
||||
struct wfp_request * request = malloc(sizeof(struct wfp_request));
|
||||
request->respond = prototype->respond;
|
||||
request->user_data = prototype->user_data;
|
||||
request->id = id;
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
void wfp_impl_request_dispose(
|
||||
struct wfp_request * request)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
|
||||
extern void wfp_impl_respond(
|
||||
struct wfp_request * request,
|
||||
json_t * result)
|
||||
{
|
||||
json_t * response = json_object();
|
||||
json_object_set_new(response, "result", result);
|
||||
json_object_set_new(response, "id", json_integer(request->id));
|
||||
|
||||
request->respond(response, request->user_data);
|
||||
|
||||
json_decref(response);
|
||||
wfp_impl_request_dispose(request);
|
||||
}
|
||||
|
||||
void wfp_impl_respond_error(
|
||||
struct wfp_request * request,
|
||||
wf_status status)
|
||||
{
|
||||
json_t * response = json_object();
|
||||
json_t * error = json_object();
|
||||
json_object_set_new(error, "code", json_integer(status));
|
||||
json_object_set_new(response, "error", error);
|
||||
json_object_set_new(response, "id", json_integer(request->id));
|
||||
|
||||
request->respond(response, request->user_data);
|
||||
|
||||
json_decref(response);
|
||||
wfp_impl_request_dispose(request);
|
||||
}
|
||||
43
lib/webfuse_provider/impl/request.h
Normal file
43
lib/webfuse_provider/impl/request.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef WF_PROVIDER_IMPL_REQUEST_H
|
||||
#define WF_PROVIDER_IMPL_REQUEST_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse_provider/impl/provider.h"
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef void wfp_impl_request_respond_fn(
|
||||
json_t * response,
|
||||
void * user_data);
|
||||
|
||||
struct wfp_request
|
||||
{
|
||||
wfp_impl_request_respond_fn * respond;
|
||||
void * user_data;
|
||||
int id;
|
||||
};
|
||||
|
||||
extern void wfp_impl_respond_error(
|
||||
struct wfp_request * request,
|
||||
wf_status status);
|
||||
|
||||
extern struct wfp_request * wfp_impl_request_create(
|
||||
struct wfp_request * prototype,
|
||||
int id);
|
||||
|
||||
extern void wfp_impl_request_dispose(
|
||||
struct wfp_request * request);
|
||||
|
||||
extern void wfp_impl_respond(
|
||||
struct wfp_request * request,
|
||||
json_t * result);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
56
lib/webfuse_provider/impl/slist.c
Normal file
56
lib/webfuse_provider/impl/slist.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "webfuse_provider/impl/slist.h"
|
||||
#include <stddef.h>
|
||||
|
||||
void wf_slist_init(
|
||||
struct wf_slist * list)
|
||||
{
|
||||
list->head.next = NULL;
|
||||
list->last = &list->head;
|
||||
}
|
||||
|
||||
bool wf_slist_empty(
|
||||
struct wf_slist * list)
|
||||
{
|
||||
return (list->last == &list->head);
|
||||
}
|
||||
|
||||
struct wf_slist_item * wf_slist_first(
|
||||
struct wf_slist * list)
|
||||
{
|
||||
return list->head.next;
|
||||
}
|
||||
|
||||
void wf_slist_append(
|
||||
struct wf_slist * list,
|
||||
struct wf_slist_item * item)
|
||||
{
|
||||
item->next = NULL;
|
||||
list->last->next = item;
|
||||
list->last = item;
|
||||
}
|
||||
|
||||
struct wf_slist_item * wf_slist_remove_first(
|
||||
struct wf_slist * list)
|
||||
{
|
||||
return wf_slist_remove_after(list, &list->head);
|
||||
}
|
||||
|
||||
struct wf_slist_item * wf_slist_remove_after(
|
||||
struct wf_slist * list,
|
||||
struct wf_slist_item * prev)
|
||||
{
|
||||
|
||||
struct wf_slist_item * result = prev->next;
|
||||
|
||||
if (NULL != result)
|
||||
{
|
||||
prev->next = result->next;
|
||||
|
||||
if (list->last == result)
|
||||
{
|
||||
list->last = prev;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
48
lib/webfuse_provider/impl/slist.h
Normal file
48
lib/webfuse_provider/impl/slist.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef WF_SLIST_H
|
||||
#define WF_SLIST_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_slist_item
|
||||
{
|
||||
struct wf_slist_item * next;
|
||||
};
|
||||
|
||||
struct wf_slist
|
||||
{
|
||||
struct wf_slist_item head;
|
||||
struct wf_slist_item * last;
|
||||
};
|
||||
|
||||
extern void wf_slist_init(
|
||||
struct wf_slist * list);
|
||||
|
||||
extern bool wf_slist_empty(
|
||||
struct wf_slist * list);
|
||||
|
||||
extern struct wf_slist_item * wf_slist_first(
|
||||
struct wf_slist * list);
|
||||
|
||||
extern void wf_slist_append(
|
||||
struct wf_slist * list,
|
||||
struct wf_slist_item * item);
|
||||
|
||||
extern struct wf_slist_item * wf_slist_remove_first(
|
||||
struct wf_slist * list);
|
||||
|
||||
extern struct wf_slist_item * wf_slist_remove_after(
|
||||
struct wf_slist * list,
|
||||
struct wf_slist_item * prev);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
34
lib/webfuse_provider/impl/status.c
Normal file
34
lib/webfuse_provider/impl/status.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "webfuse_provider/impl/status_intern.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
int wf_status_to_rc(wf_status status)
|
||||
{
|
||||
switch(status)
|
||||
{
|
||||
case WF_GOOD: return 0;
|
||||
case WF_BAD_NOTIMPLEMENTED: return -ENOSYS;
|
||||
case WF_BAD_TIMEOUT: return -ETIMEDOUT;
|
||||
case WF_BAD_BUSY: return -ENOENT;
|
||||
case WF_BAD_FORMAT: return -ENOENT;
|
||||
case WF_BAD_NOENTRY: return -ENOENT;
|
||||
case WF_BAD_ACCESS_DENIED: return -EACCES;
|
||||
default: return -ENOENT;
|
||||
}
|
||||
}
|
||||
|
||||
char const * wf_status_tostring(wf_status status)
|
||||
{
|
||||
switch(status)
|
||||
{
|
||||
case WF_GOOD: return "Good";
|
||||
case WF_BAD: return "Bad";
|
||||
case WF_BAD_NOTIMPLEMENTED: return "Bad (not implemented)";
|
||||
case WF_BAD_TIMEOUT: return "Bad (timeout)";
|
||||
case WF_BAD_BUSY: return "Bad (busy)";
|
||||
case WF_BAD_FORMAT: return "Bad (format)";
|
||||
case WF_BAD_NOENTRY: return "Bad (no entry)";
|
||||
case WF_BAD_ACCESS_DENIED: return "Bad (access denied)";
|
||||
default: return "Bad (unknown)";
|
||||
}
|
||||
}
|
||||
19
lib/webfuse_provider/impl/status_intern.h
Normal file
19
lib/webfuse_provider/impl/status_intern.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef WF_STATUS_INTERN_H
|
||||
#define WF_STATUS_INTERN_H
|
||||
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int wf_status_to_rc(wf_status status);
|
||||
|
||||
extern char const * wf_status_tostring(wf_status status);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
93
lib/webfuse_provider/impl/timer/manager.c
Normal file
93
lib/webfuse_provider/impl/timer/manager.c
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "webfuse_provider/impl/timer/manager_intern.h"
|
||||
#include "webfuse_provider/impl/timer/timer_intern.h"
|
||||
#include "webfuse_provider/impl/timer/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct wf_timer_manager
|
||||
{
|
||||
struct wf_timer * timers;
|
||||
};
|
||||
|
||||
struct wf_timer_manager *
|
||||
wf_timer_manager_create(void)
|
||||
{
|
||||
struct wf_timer_manager * manager = malloc(sizeof(struct wf_timer_manager));
|
||||
manager->timers = NULL;
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
wf_timer_trigger(timer);
|
||||
timer = next;
|
||||
}
|
||||
|
||||
free(manager);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
if (wf_timer_is_timeout(timer))
|
||||
{
|
||||
wf_timer_manager_removetimer(manager, timer);
|
||||
wf_timer_trigger(timer);
|
||||
}
|
||||
|
||||
timer = next;
|
||||
}
|
||||
}
|
||||
|
||||
void wf_timer_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
if (NULL != manager->timers)
|
||||
{
|
||||
manager->timers->prev = timer;
|
||||
}
|
||||
|
||||
timer->next = manager->timers;
|
||||
timer->prev = NULL;
|
||||
manager->timers = timer;
|
||||
}
|
||||
|
||||
void wf_timer_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
struct wf_timer * prev = timer->prev;
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
if (NULL != prev)
|
||||
{
|
||||
prev->next = next;
|
||||
}
|
||||
|
||||
if (NULL != next)
|
||||
{
|
||||
next->prev = prev;
|
||||
}
|
||||
|
||||
if (manager->timers == timer)
|
||||
{
|
||||
manager->timers = next;
|
||||
}
|
||||
}
|
||||
|
||||
27
lib/webfuse_provider/impl/timer/manager.h
Normal file
27
lib/webfuse_provider/impl/timer/manager.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef WF_TIMER_MANAGER_H
|
||||
#define WF_TIMER_MANAGER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern struct wf_timer_manager *
|
||||
wf_timer_manager_create(void);
|
||||
|
||||
extern void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern void
|
||||
wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
26
lib/webfuse_provider/impl/timer/manager_intern.h
Normal file
26
lib/webfuse_provider/impl/timer/manager_intern.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef WF_TIMER_MANAGER_INTERN_H
|
||||
#define WF_TIMER_MANAGER_INTERN_H
|
||||
|
||||
#include "webfuse_provider/impl/timer/manager.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
|
||||
extern void wf_timer_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
19
lib/webfuse_provider/impl/timer/on_timer_fn.h
Normal file
19
lib/webfuse_provider/impl/timer/on_timer_fn.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef WF_TIMER_ON_TIMER_FN_H
|
||||
#define WF_TIMER_ON_TIMER_FN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
|
||||
typedef void wf_timer_on_timer_fn(
|
||||
struct wf_timer * timer,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
31
lib/webfuse_provider/impl/timer/timepoint.c
Normal file
31
lib/webfuse_provider/impl/timer/timepoint.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "webfuse_provider/impl/timer/timepoint.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define WF_TIMER_MSEC_PER_SEC ((wf_timer_timepoint) 1000)
|
||||
#define WF_TIMER_NSEC_PER_MSEC ((wf_timer_timepoint) 1000 * 1000)
|
||||
|
||||
wf_timer_timepoint wf_timer_timepoint_now(void)
|
||||
{
|
||||
struct timespec tp;
|
||||
clock_gettime(CLOCK_MONOTONIC, &tp);
|
||||
|
||||
wf_timer_timepoint const now = (tp.tv_sec * WF_TIMER_MSEC_PER_SEC) + (tp.tv_nsec / WF_TIMER_NSEC_PER_MSEC);
|
||||
return now;
|
||||
}
|
||||
|
||||
wf_timer_timepoint wf_timer_timepoint_in_msec(wf_timer_timediff value)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_timepoint_now();
|
||||
wf_timer_timepoint result = now + ((wf_timer_timepoint) value);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool wf_timer_timepoint_is_elapsed(wf_timer_timepoint tp)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_timepoint_now();
|
||||
wf_timer_timediff const diff = (wf_timer_timediff) (tp - now);
|
||||
|
||||
return (0 > diff);
|
||||
}
|
||||
31
lib/webfuse_provider/impl/timer/timepoint.h
Normal file
31
lib/webfuse_provider/impl/timer/timepoint.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef WF_TIMER_TIMEPOINT_H
|
||||
#define WF_TIMER_TIMEPOINT_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
#else
|
||||
#include <cinttypes>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef uint64_t wf_timer_timepoint;
|
||||
typedef int64_t wf_timer_timediff;
|
||||
|
||||
extern wf_timer_timepoint wf_timer_timepoint_now(void);
|
||||
|
||||
extern wf_timer_timepoint wf_timer_timepoint_in_msec(
|
||||
wf_timer_timediff value);
|
||||
|
||||
extern bool wf_timer_timepoint_is_elapsed(
|
||||
wf_timer_timepoint timepoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
67
lib/webfuse_provider/impl/timer/timer.c
Normal file
67
lib/webfuse_provider/impl/timer/timer.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "webfuse_provider/impl/timer/timer_intern.h"
|
||||
#include "webfuse_provider/impl/timer/manager_intern.h"
|
||||
#include "webfuse_provider/impl/timer/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_timer * timer = malloc(sizeof(struct wf_timer));
|
||||
timer->manager = manager;
|
||||
timer->timeout = 0;
|
||||
timer->on_timer = on_timer;
|
||||
timer->user_data = user_data;
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
return timer;
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
free(timer);
|
||||
}
|
||||
|
||||
void wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms)
|
||||
{
|
||||
timer->timeout = wf_timer_timepoint_in_msec(timeout_ms);
|
||||
|
||||
wf_timer_manager_addtimer(timer->manager, timer);
|
||||
}
|
||||
|
||||
void wf_timer_cancel(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_manager_removetimer(timer->manager, timer);
|
||||
|
||||
timer->timeout = 0;
|
||||
}
|
||||
|
||||
bool wf_timer_is_timeout(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
return wf_timer_timepoint_is_elapsed(timer->timeout);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_trigger(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
if (0 != timer->on_timer)
|
||||
{
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
timer->on_timer(timer, timer->user_data);
|
||||
}
|
||||
}
|
||||
37
lib/webfuse_provider/impl/timer/timer.h
Normal file
37
lib/webfuse_provider/impl/timer/timer.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef WF_TIMER_TIMER_H
|
||||
#define WF_TIMER_TIMER_H
|
||||
|
||||
#include "webfuse_provider/impl/timer/on_timer_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern struct wf_timer *
|
||||
wf_timer_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void
|
||||
wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms);
|
||||
|
||||
extern void
|
||||
wf_timer_cancel(
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
40
lib/webfuse_provider/impl/timer/timer_intern.h
Normal file
40
lib/webfuse_provider/impl/timer/timer_intern.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef WF_TIMER_TIMER_H
|
||||
#define WF_TIMER_TIMER_H
|
||||
|
||||
#include "webfuse_provider/impl/timer/timer.h"
|
||||
#include "webfuse_provider/impl/timer/on_timer_fn.h"
|
||||
#include "webfuse_provider/impl/timer/timepoint.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer
|
||||
{
|
||||
struct wf_timer_manager * manager;
|
||||
wf_timer_timepoint timeout;
|
||||
wf_timer_on_timer_fn * on_timer;
|
||||
void * user_data;
|
||||
struct wf_timer * next;
|
||||
struct wf_timer * prev;
|
||||
};
|
||||
|
||||
extern bool wf_timer_is_timeout(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_trigger(
|
||||
struct wf_timer * timer);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
125
lib/webfuse_provider/impl/url.c
Normal file
125
lib/webfuse_provider/impl/url.c
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "webfuse_provider/impl/url.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_url_protocol
|
||||
{
|
||||
char const * name;
|
||||
size_t name_length;
|
||||
int default_port;
|
||||
bool use_tls;
|
||||
};
|
||||
|
||||
static bool wf_url_readprotocol(
|
||||
struct wf_url * url,
|
||||
char const * * data)
|
||||
{
|
||||
static struct wf_url_protocol const known_protocols[] =
|
||||
{
|
||||
{"ws://", 5, 80, false},
|
||||
{"wss://", 6, 443, true}
|
||||
};
|
||||
static size_t const count = (sizeof(known_protocols) / sizeof(known_protocols[0]));
|
||||
|
||||
bool found = false;
|
||||
for(size_t i = 0; (!found) && (i < count); i++)
|
||||
{
|
||||
struct wf_url_protocol const * protocol = &known_protocols[i];
|
||||
if (0 == strncmp(*data, protocol->name, protocol->name_length))
|
||||
{
|
||||
url->port = protocol->default_port;
|
||||
url->use_tls = protocol->use_tls;
|
||||
*data = *data + protocol->name_length;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
static bool wf_url_readhost(
|
||||
struct wf_url * url,
|
||||
char const * * data)
|
||||
{
|
||||
char * end = strpbrk(*data, ":/");
|
||||
bool const result = (NULL != end);
|
||||
|
||||
if (result)
|
||||
{
|
||||
size_t length = end - *data;
|
||||
url->host = strndup(*data, length);
|
||||
*data = end;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool wf_url_readport(
|
||||
struct wf_url * url,
|
||||
char const * * data)
|
||||
{
|
||||
bool result;
|
||||
|
||||
if (':' == **data)
|
||||
{
|
||||
*data = *data + 1;
|
||||
char * end = strchr(*data, '/');
|
||||
result = (NULL != end);
|
||||
|
||||
if (result)
|
||||
{
|
||||
url->port = atoi(*data);
|
||||
*data = end;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = ('/' == **data);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool wf_url_readpath(
|
||||
struct wf_url * url,
|
||||
char const * * data)
|
||||
{
|
||||
bool const result = ('/' == **data);
|
||||
url->path = strdup(*data);
|
||||
*data = NULL;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
bool wf_url_init(
|
||||
struct wf_url * url,
|
||||
char const * value)
|
||||
{
|
||||
memset(url, 0, sizeof(struct wf_url));
|
||||
char const * data = value;
|
||||
|
||||
bool const result =
|
||||
wf_url_readprotocol(url, &data) &&
|
||||
wf_url_readhost(url, &data) &&
|
||||
wf_url_readport(url, &data) &&
|
||||
wf_url_readpath(url, &data)
|
||||
;
|
||||
|
||||
if (!result)
|
||||
{
|
||||
wf_url_cleanup(url);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void wf_url_cleanup(
|
||||
struct wf_url * url)
|
||||
{
|
||||
free(url->host);
|
||||
free(url->path);
|
||||
memset(url, 0, sizeof(struct wf_url));
|
||||
}
|
||||
|
||||
32
lib/webfuse_provider/impl/url.h
Normal file
32
lib/webfuse_provider/impl/url.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef WF_URL_H
|
||||
#define WF_URL_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
struct wf_url
|
||||
{
|
||||
char * host;
|
||||
int port;
|
||||
char * path;
|
||||
bool use_tls;
|
||||
};
|
||||
|
||||
extern bool wf_url_init(
|
||||
struct wf_url * url,
|
||||
char const * value);
|
||||
|
||||
extern void wf_url_cleanup(
|
||||
struct wf_url * url);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
10
lib/webfuse_provider/impl/util.h
Normal file
10
lib/webfuse_provider/impl/util.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef WF_UTIL_H
|
||||
#define WF_UTIL_H
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define WF_UNUSED_PARAM(param) param __attribute__((unused))
|
||||
#else
|
||||
#define WF_UNUSED_PARAM(param)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user