mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
reorganized project: prepared to extract common functionality
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "wsfs/filesystem.h"
|
||||
#include "wsfs/operations.h"
|
||||
#include "wsfs/jsonrpc/server.h"
|
||||
#include "wsfs/adapter/filesystem.h"
|
||||
#include "wsfs/adapter/operations.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
@@ -5,8 +5,8 @@
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include "wsfs/fuse_wrapper.h"
|
||||
#include "wsfs/operations.h"
|
||||
#include "wsfs/adapter/fuse_wrapper.h"
|
||||
#include "wsfs/adapter/operations.h"
|
||||
|
||||
struct wsfs_jsonrpc_server;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/jsonrpc/method_intern.h"
|
||||
#include "wsfs/adapter/jsonrpc/method_intern.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -6,7 +6,7 @@
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "wsfs/status.h"
|
||||
#include "wsfs/adapter/status.h"
|
||||
|
||||
|
||||
typedef bool wsfs_jsonrpc_method_invoke_fn(
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef WSFS_JSONRPC_METHOD_INTERN_H
|
||||
#define WSFS_JSONRPC_METHOD_INTERN_H
|
||||
|
||||
#include "wsfs/jsonrpc/method.h"
|
||||
#include "wsfs/adapter/jsonrpc/method.h"
|
||||
|
||||
struct wsfs_jsonrpc_method
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/jsonrpc/request.h"
|
||||
#include "wsfs/adapter/jsonrpc/request.h"
|
||||
|
||||
json_t * wsfs_jsonrpc_request_create(
|
||||
char const * method,
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/jsonrpc/response.h"
|
||||
#include "wsfs/adapter/jsonrpc/response.h"
|
||||
|
||||
void wsfs_jsonrpc_response_init(
|
||||
struct wsfs_jsonrpc_response * result,
|
||||
@@ -9,7 +9,7 @@ using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "wsfs/status.h"
|
||||
#include "wsfs/adapter/status.h"
|
||||
|
||||
struct wsfs_jsonrpc_response
|
||||
{
|
||||
@@ -1,9 +1,9 @@
|
||||
#include "wsfs/jsonrpc/server.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "wsfs/jsonrpc/method_intern.h"
|
||||
#include "wsfs/jsonrpc/request.h"
|
||||
#include "wsfs/jsonrpc/response.h"
|
||||
#include "wsfs/adapter/jsonrpc/method_intern.h"
|
||||
#include "wsfs/adapter/jsonrpc/request.h"
|
||||
#include "wsfs/adapter/jsonrpc/response.h"
|
||||
|
||||
#define WSFS_DEFAULT_TIMEOUT (10 * 1000)
|
||||
|
||||
@@ -12,9 +12,9 @@ using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "wsfs/jsonrpc/method.h"
|
||||
#include "wsfs/time/timeout_manager.h"
|
||||
#include "wsfs/time/timer.h"
|
||||
#include "wsfs/adapter/jsonrpc/method.h"
|
||||
#include "wsfs/adapter/time/timeout_manager.h"
|
||||
#include "wsfs/adapter/time/timer.h"
|
||||
|
||||
|
||||
struct wsfs_jsonrpc_request
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/jsonrpc/util.h"
|
||||
#include "wsfs/adapter/jsonrpc/util.h"
|
||||
|
||||
int wsfs_json_get_int(json_t const * object, char const * key, int default_value)
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/message.h"
|
||||
#include "wsfs/adapter/message.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <libwebsockets.h>
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "wsfs/message_queue.h"
|
||||
#include "wsfs/message.h"
|
||||
#include "wsfs/adapter/message_queue.h"
|
||||
#include "wsfs/adapter/message.h"
|
||||
|
||||
void wsfs_message_queue_init(
|
||||
struct wsfs_message_queue * queue)
|
||||
@@ -1,10 +1,10 @@
|
||||
#include "wsfs/operations.h"
|
||||
#include "wsfs/adapter/operations.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#include "wsfs/jsonrpc/server.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
#include "wsfs/util.h"
|
||||
|
||||
void wsfs_operation_close(
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/operations.h"
|
||||
#include "wsfs/adapter/operations.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
@@ -7,8 +7,8 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "wsfs/jsonrpc/server.h"
|
||||
#include "wsfs/jsonrpc/util.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
#include "wsfs/adapter/jsonrpc/util.h"
|
||||
#include "wsfs/util.h"
|
||||
|
||||
struct wsfs_operation_getattr_context
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/operations.h"
|
||||
#include "wsfs/adapter/operations.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
@@ -10,8 +10,8 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "wsfs/jsonrpc/server.h"
|
||||
#include "wsfs/jsonrpc/util.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
#include "wsfs/adapter/jsonrpc/util.h"
|
||||
#include "wsfs/util.h"
|
||||
|
||||
struct wsfs_operation_lookup_context
|
||||
@@ -1,12 +1,12 @@
|
||||
#include "wsfs/operations.h"
|
||||
#include "wsfs/adapter/operations.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#include "wsfs/jsonrpc/server.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
#include "wsfs/util.h"
|
||||
#include "wsfs/status.h"
|
||||
#include "wsfs/adapter/status.h"
|
||||
|
||||
static void wsfs_operation_open_finished(
|
||||
void * user_data,
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/operations.h"
|
||||
#include "wsfs/adapter/operations.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <jansson.h>
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include "wsfs/jsonrpc/server.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
|
||||
#define WSFS_MAX_READ_LENGTH 4096
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/operations.h"
|
||||
#include "wsfs/adapter/operations.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "wsfs/jsonrpc/server.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
#include "wsfs/util.h"
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef WSFS_OPERATIONS
|
||||
#define WSFS_OPERATIONS
|
||||
|
||||
#include "wsfs/fuse_wrapper.h"
|
||||
#include "wsfs/adapter/fuse_wrapper.h"
|
||||
|
||||
struct wsfs_jsonrpc_server;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#include "wsfs/server.h"
|
||||
#include "wsfs/adapter/server.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include "wsfs/server_config.h"
|
||||
#include "wsfs/server_protocol_intern.h"
|
||||
#include "wsfs/adapter/server_config.h"
|
||||
#include "wsfs/adapter/server_protocol_intern.h"
|
||||
|
||||
#define WSFS_DISABLE_LWS_LOG 0
|
||||
#define WSFS_SERVER_PROTOCOL_COUNT 3
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/server_config.h"
|
||||
#include "wsfs/adapter/server_config.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -1,10 +1,10 @@
|
||||
#include "wsfs/server_protocol_intern.h"
|
||||
#include "wsfs/adapter/server_protocol_intern.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include "wsfs/message.h"
|
||||
#include "wsfs/filesystem.h"
|
||||
#include "wsfs/adapter/message.h"
|
||||
#include "wsfs/adapter/filesystem.h"
|
||||
#include "wsfs/util.h"
|
||||
|
||||
static int wsfs_server_protocol_callback(
|
||||
@@ -1,11 +1,11 @@
|
||||
#ifndef WSFS_SERVER_PROTOCOL_INTERN_H
|
||||
#define WSFS_SERVER_PROTOCOL_INTERN_H
|
||||
|
||||
#include "wsfs/server_protocol.h"
|
||||
#include "wsfs/filesystem.h"
|
||||
#include "wsfs/message_queue.h"
|
||||
#include "wsfs/jsonrpc/server.h"
|
||||
#include "wsfs/time/timeout_manager.h"
|
||||
#include "wsfs/adapter/server_protocol.h"
|
||||
#include "wsfs/adapter/filesystem.h"
|
||||
#include "wsfs/adapter/message_queue.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
#include "wsfs/adapter/time/timeout_manager.h"
|
||||
|
||||
struct wsfs_server_protocol
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/status.h"
|
||||
#include "wsfs/adapter/status.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "wsfs/time/timeout_manager_intern.h"
|
||||
#include "wsfs/adapter/time/timeout_manager_intern.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include "wsfs/time/timer_intern.h"
|
||||
#include "wsfs/time/timepoint.h"
|
||||
#include "wsfs/adapter/time/timer_intern.h"
|
||||
#include "wsfs/adapter/time/timepoint.h"
|
||||
|
||||
void wsfs_timeout_manager_init(
|
||||
struct wsfs_timeout_manager * manager)
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef WSFS_TIME_TIMEOUT_MANAGER_INTERN_H
|
||||
#define WSFS_TIME_TIMEOUT_MANAGER_INTERN_H
|
||||
|
||||
#include "wsfs/time/timeout_manager.h"
|
||||
#include "wsfs/adapter/time/timeout_manager.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/time/timepoint.h"
|
||||
#include "wsfs/adapter/time/timepoint.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "wsfs/time/timer_intern.h"
|
||||
#include "wsfs/time/timeout_manager_intern.h"
|
||||
#include "wsfs/adapter/time/timer_intern.h"
|
||||
#include "wsfs/adapter/time/timeout_manager_intern.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef WSFS_TIMER_H
|
||||
#define WSFS_TIMER_H
|
||||
|
||||
#include "wsfs/time/timepoint.h"
|
||||
#include "wsfs/adapter/time/timepoint.h"
|
||||
|
||||
struct wsfs_timer;
|
||||
struct wsfs_timeout_manager;
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include "wsfs/time/timer.h"
|
||||
#include "wsfs/adapter/time/timer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
125
lib/wsfs/provider/client.c
Normal file
125
lib/wsfs/provider/client.c
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "wsfs/provider/client.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include "wsfs/provider/provider.h"
|
||||
#include "wsfs/provider/client_protocol_intern.h"
|
||||
#include "wsfs/provider/url.h"
|
||||
|
||||
#define WSFSP_PROTOCOL ("fs")
|
||||
#define WSFSP_DISABLE_LWS_LOG 0
|
||||
#define WSFSP_CLIENT_PROTOCOL_COUNT 2
|
||||
#define WSFSP_CLIENT_TIMEOUT (1 * 1000)
|
||||
|
||||
struct wsfsp_client
|
||||
{
|
||||
volatile bool is_running;
|
||||
struct wsfsp_provider provider;
|
||||
struct wsfsp_client_protocol protocol;
|
||||
struct lws_context_creation_info info;
|
||||
struct lws_protocols protocols[WSFSP_CLIENT_PROTOCOL_COUNT];
|
||||
struct lws_context * context;
|
||||
struct lws * wsi;
|
||||
};
|
||||
|
||||
|
||||
struct wsfsp_client * wsfsp_client_create(
|
||||
struct wsfsp_provider * provider,
|
||||
void * user_data)
|
||||
{
|
||||
lws_set_log_level(WSFSP_DISABLE_LWS_LOG, NULL);
|
||||
|
||||
struct wsfsp_client * client = malloc(sizeof(struct wsfsp_client));
|
||||
if (NULL != client)
|
||||
{
|
||||
client->is_running = true;
|
||||
wsfsp_client_protocol_init(&client->protocol, provider, user_data);
|
||||
|
||||
memset(client->protocols, 0, sizeof(struct lws_protocols) * WSFSP_CLIENT_PROTOCOL_COUNT);
|
||||
client->protocols[0].name = "fs";
|
||||
wsfsp_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;
|
||||
|
||||
client->context = lws_create_context(&client->info);
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
void wsfsp_client_dispose(
|
||||
struct wsfsp_client * client)
|
||||
{
|
||||
lws_context_destroy(client->context);
|
||||
wsfsp_client_protocol_cleanup(&client->protocol);
|
||||
free(client);
|
||||
}
|
||||
|
||||
void wsfsp_client_connect(
|
||||
struct wsfsp_client * client,
|
||||
char const * url)
|
||||
{
|
||||
struct wsfsp_url url_data;
|
||||
bool const success = wsfsp_url_init(&url_data, url);
|
||||
if (success)
|
||||
{
|
||||
struct lws_client_connect_info info;
|
||||
memset(&info, 0, sizeof(struct lws_client_connect_info));
|
||||
info.context = client->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 = WSFSP_PROTOCOL;
|
||||
info.pwsi = &client->wsi;
|
||||
|
||||
lws_client_connect_via_info(&info);
|
||||
|
||||
wsfsp_url_cleanup(&url_data);
|
||||
}
|
||||
}
|
||||
|
||||
void wsfsp_client_disconnect(
|
||||
struct wsfsp_client * client)
|
||||
{
|
||||
(void) client;
|
||||
|
||||
// ToDo: implement me
|
||||
}
|
||||
|
||||
void wsfsp_client_settimeout(
|
||||
struct wsfsp_client * client,
|
||||
unsigned int timepoint)
|
||||
{
|
||||
(void) client;
|
||||
(void) timepoint;
|
||||
|
||||
// ToDo: implement me
|
||||
}
|
||||
|
||||
|
||||
void wsfsp_client_run(
|
||||
struct wsfsp_client * client)
|
||||
{
|
||||
while (client->is_running)
|
||||
{
|
||||
lws_service(client->context, WSFSP_CLIENT_TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
void wsfsp_client_shutdown(
|
||||
struct wsfsp_client * client)
|
||||
{
|
||||
client->is_running = false;
|
||||
}
|
||||
|
||||
146
lib/wsfs/provider/client_protocol.c
Normal file
146
lib/wsfs/provider/client_protocol.c
Normal file
@@ -0,0 +1,146 @@
|
||||
#include "wsfs/provider/client_protocol_intern.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <jansson.h>
|
||||
|
||||
|
||||
#include "wsfs/provider/provider_intern.h"
|
||||
#include "wsfs/provider/operation/lookup_intern.h"
|
||||
#include "wsfs/provider/operation/getattr_intern.h"
|
||||
#include "wsfs/provider/operation/readdir_intern.h"
|
||||
#include "wsfs/provider/operation/open_intern.h"
|
||||
#include "wsfs/provider/operation/close_intern.h"
|
||||
#include "wsfs/provider/operation/read_intern.h"
|
||||
|
||||
#include "wsfs/util.h"
|
||||
|
||||
static void wsfsp_client_protocol_respond(
|
||||
json_t * response,
|
||||
void * user_data)
|
||||
{
|
||||
// ToDo: implment me
|
||||
(void) user_data;
|
||||
|
||||
char * value = json_dumps(response, 0);
|
||||
if (NULL != value)
|
||||
{
|
||||
puts(value);
|
||||
}
|
||||
free(value);
|
||||
}
|
||||
|
||||
static void wsfsp_client_protocol_process_request(
|
||||
struct wsfsp_client_protocol * protocol,
|
||||
char const * message,
|
||||
size_t length)
|
||||
{
|
||||
json_t * request = json_loadb(message, length, 0, NULL);
|
||||
if (NULL != request)
|
||||
{
|
||||
struct wsfsp_invokation_context context =
|
||||
{
|
||||
.provider = &protocol->provider,
|
||||
.user_data = protocol->user_data,
|
||||
.request = &protocol->request
|
||||
};
|
||||
|
||||
puts("wsfsp_provider_invoke");
|
||||
wsfsp_provider_invoke(&context, request);
|
||||
json_decref(request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int wsfsp_client_protocol_callback(
|
||||
struct lws * wsi,
|
||||
enum lws_callback_reasons reason,
|
||||
void * WSFS_UNUSED_PARAM(user),
|
||||
void * in,
|
||||
size_t len)
|
||||
{
|
||||
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
|
||||
struct wsfsp_client_protocol * protocol = (NULL != ws_protocol) ? ws_protocol->user: NULL;
|
||||
|
||||
if (NULL != protocol)
|
||||
{
|
||||
switch (reason)
|
||||
{
|
||||
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
||||
puts("established");
|
||||
protocol->provider.connected(protocol->user_data);
|
||||
break;
|
||||
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
||||
protocol->provider.disconnected(protocol->user_data);
|
||||
break;
|
||||
case LWS_CALLBACK_CLIENT_CLOSED:
|
||||
protocol->provider.connected(protocol->user_data);
|
||||
break;
|
||||
case LWS_CALLBACK_CLIENT_RECEIVE:
|
||||
wsfsp_client_protocol_process_request(protocol, in, len);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void wsfsp_client_protocol_init(
|
||||
struct wsfsp_client_protocol * protocol,
|
||||
struct wsfsp_provider const * provider,
|
||||
void * user_data)
|
||||
{
|
||||
protocol->request.respond = &wsfsp_client_protocol_respond;
|
||||
protocol->request.user_data = protocol;
|
||||
|
||||
protocol->user_data = user_data;
|
||||
protocol->provider.lookup = (NULL != provider->lookup) ? provider->lookup : &wsfsp_lookup_default;
|
||||
protocol->provider.getattr = (NULL != provider->getattr) ? provider->getattr : &wsfsp_getattr_default;
|
||||
protocol->provider.readdir = (NULL != provider->readdir) ? provider->readdir : &wsfsp_readdir_default;
|
||||
protocol->provider.open = (NULL != provider->open) ? provider->open : &wsfsp_open_default;
|
||||
protocol->provider.close = (NULL != provider->close) ? provider->close : &wsfsp_close_default;
|
||||
protocol->provider.read = (NULL != provider->read) ? provider->read : &wsfsp_read_default;
|
||||
protocol->provider.connected = (NULL != provider->connected) ? provider->connected : &wsfsp_connected_default;
|
||||
protocol->provider.disconnected = (NULL != provider->disconnected) ? provider->disconnected : &wsfsp_disconnected_default;
|
||||
protocol->provider.ontimer = (NULL != provider->ontimer) ? provider->ontimer : &wsfsp_ontimer_default;
|
||||
}
|
||||
|
||||
void wsfsp_client_protocol_cleanup(
|
||||
struct wsfsp_client_protocol * protocol)
|
||||
{
|
||||
(void) protocol;
|
||||
}
|
||||
|
||||
struct wsfsp_client_protocol * wsfsp_client_protocol_create(
|
||||
struct wsfsp_provider const * provider,
|
||||
void * user_data)
|
||||
{
|
||||
struct wsfsp_client_protocol * protocol = malloc(sizeof(struct wsfsp_client_protocol));
|
||||
if (NULL != protocol)
|
||||
{
|
||||
wsfsp_client_protocol_init(protocol, provider, user_data);
|
||||
}
|
||||
|
||||
return protocol;
|
||||
}
|
||||
|
||||
void wsfsp_client_protocol_dispose(
|
||||
struct wsfsp_client_protocol * protocol)
|
||||
{
|
||||
wsfsp_client_protocol_cleanup(protocol);
|
||||
free(protocol);
|
||||
}
|
||||
|
||||
void wsfsp_client_protocol_init_lws(
|
||||
struct wsfsp_client_protocol * protocol,
|
||||
struct lws_protocols * lws_protocol)
|
||||
{
|
||||
lws_protocol->callback = &wsfsp_client_protocol_callback;
|
||||
lws_protocol->per_session_data_size = 0;
|
||||
lws_protocol->user = protocol;
|
||||
}
|
||||
32
lib/wsfs/provider/client_protocol_intern.h
Normal file
32
lib/wsfs/provider/client_protocol_intern.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef WSFSP_CLIENT_PROTOCOL_INTERN_H
|
||||
#define WSFSP_CLIENT_PROTOCOL_INTERN_H
|
||||
|
||||
#include "wsfs/provider/client_protocol.h"
|
||||
#include "wsfs/provider/provider.h"
|
||||
#include "wsfs/provider/request.h"
|
||||
|
||||
struct wsfsp_client_protocol
|
||||
{
|
||||
struct wsfsp_request request;
|
||||
struct wsfsp_provider provider;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfsp_client_protocol_init(
|
||||
struct wsfsp_client_protocol * protocol,
|
||||
struct wsfsp_provider const * provider,
|
||||
void * user_data);
|
||||
|
||||
extern void wsfsp_client_protocol_cleanup(
|
||||
struct wsfsp_client_protocol * protocol);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
43
lib/wsfs/provider/operation/close.c
Normal file
43
lib/wsfs/provider/operation/close.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "wsfs/provider/operation/close_intern.h"
|
||||
#include <limits.h>
|
||||
#include "wsfs/util.h"
|
||||
|
||||
void wsfsp_close(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int WSFS_UNUSED_PARAM(id))
|
||||
{
|
||||
size_t const param_count = json_array_size(params);
|
||||
if (3 == param_count)
|
||||
{
|
||||
json_t * inode_holder = json_array_get(params, 0);
|
||||
json_t * handle_holder = json_array_get(params, 1);
|
||||
json_t * flags_holder = json_array_get(params, 2);
|
||||
|
||||
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 wsfsp_close_default(
|
||||
ino_t inode,
|
||||
uint32_t handle,
|
||||
int flags,
|
||||
void * user_data)
|
||||
{
|
||||
(void) inode;
|
||||
(void) handle;
|
||||
(void) flags;
|
||||
(void) user_data;
|
||||
|
||||
// empty
|
||||
}
|
||||
27
lib/wsfs/provider/operation/close_intern.h
Normal file
27
lib/wsfs/provider/operation/close_intern.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef WSFSP_OPERATION_CLOSE_INTERN_H
|
||||
#define WSFSP_OPERATION_CLOSE_INTERN_H
|
||||
|
||||
#include "wsfs/provider/operation/close.h"
|
||||
#include "wsfs/provider/provider_intern.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfsp_close(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wsfsp_close_default(
|
||||
ino_t inode,
|
||||
uint32_t handle,
|
||||
int flags,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
19
lib/wsfs/provider/operation/error.c
Normal file
19
lib/wsfs/provider/operation/error.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "wsfs/provider/operation/error.h"
|
||||
#include <jansson.h>
|
||||
#include "wsfs/provider/request.h"
|
||||
|
||||
void wsfsp_respond_error(
|
||||
struct wsfsp_request * request,
|
||||
int 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);
|
||||
wsfsp_request_dispose(request);
|
||||
}
|
||||
46
lib/wsfs/provider/operation/getattr.c
Normal file
46
lib/wsfs/provider/operation/getattr.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "wsfs/provider/operation/getattr_intern.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "wsfs/provider/operation/error.h"
|
||||
|
||||
|
||||
void wsfsp_getattr(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
size_t const count = json_array_size(params);
|
||||
if (1 == count)
|
||||
{
|
||||
json_t * inode_holder = json_array_get(params, 0);
|
||||
|
||||
if ((NULL != inode_holder) && (json_is_integer(inode_holder)))
|
||||
{
|
||||
ino_t inode = (ino_t) json_integer_value(inode_holder);
|
||||
struct wsfsp_request * request = wsfsp_request_create(context->request, id);
|
||||
|
||||
context->provider->getattr(request, inode, context->user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void wsfsp_getattr_default(
|
||||
struct wsfsp_request * request,
|
||||
ino_t inode,
|
||||
void * user_data)
|
||||
{
|
||||
(void) inode;
|
||||
(void) user_data;
|
||||
|
||||
wsfsp_respond_error(request, -1);
|
||||
}
|
||||
|
||||
void wsfsp_respond_getattr(
|
||||
struct wsfsp_request * request,
|
||||
struct stat const * stat)
|
||||
{
|
||||
(void) request;
|
||||
(void) stat;
|
||||
|
||||
// ToDo: implement me
|
||||
}
|
||||
26
lib/wsfs/provider/operation/getattr_intern.h
Normal file
26
lib/wsfs/provider/operation/getattr_intern.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef WSFSP_OPERATION_GETATTR_INTERN_H
|
||||
#define WSFSP_OPERATION_GETATTR_INTERN_H
|
||||
|
||||
#include "wsfs/provider/operation/getattr.h"
|
||||
#include "wsfs/provider/provider_intern.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfsp_getattr(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wsfsp_getattr_default(
|
||||
struct wsfsp_request * request,
|
||||
ino_t inode,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
38
lib/wsfs/provider/operation/lookup.c
Normal file
38
lib/wsfs/provider/operation/lookup.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "wsfs/provider/operation/lookup_intern.h"
|
||||
#include <stdio.h>
|
||||
#include "wsfs/provider/operation/error.h"
|
||||
|
||||
void wsfsp_lookup(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
(void) context;
|
||||
(void) params;
|
||||
(void) id;
|
||||
|
||||
puts("lookup");
|
||||
}
|
||||
|
||||
void wsfsp_respond_lookup(
|
||||
struct wsfsp_request * request,
|
||||
struct stat const * stat)
|
||||
{
|
||||
(void) request;
|
||||
(void) stat;
|
||||
|
||||
}
|
||||
|
||||
void wsfsp_lookup_default(
|
||||
struct wsfsp_request * request,
|
||||
ino_t parent,
|
||||
char const * name,
|
||||
void * user_data)
|
||||
{
|
||||
(void) parent;
|
||||
(void) name;
|
||||
(void) user_data;
|
||||
|
||||
wsfsp_respond_error(request, -1);
|
||||
}
|
||||
|
||||
27
lib/wsfs/provider/operation/lookup_intern.h
Normal file
27
lib/wsfs/provider/operation/lookup_intern.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef WSFSP_OPERATION_LOOKUP_INTERN_H
|
||||
#define WSFSP_OPERATION_LOOKUP_INTERN_H
|
||||
|
||||
#include "wsfs/provider/operation/lookup.h"
|
||||
#include "wsfs/provider/provider_intern.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfsp_lookup(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wsfsp_lookup_default(
|
||||
struct wsfsp_request * request,
|
||||
ino_t parent,
|
||||
char const * name,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
38
lib/wsfs/provider/operation/open.c
Normal file
38
lib/wsfs/provider/operation/open.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "wsfs/provider/operation/open_intern.h"
|
||||
#include <stdio.h>
|
||||
#include "wsfs/provider/operation/error.h"
|
||||
|
||||
void wsfsp_open(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
(void) context;
|
||||
(void) params;
|
||||
(void) id;
|
||||
|
||||
puts("open");
|
||||
}
|
||||
|
||||
void wsfsp_open_default(
|
||||
struct wsfsp_request * request,
|
||||
ino_t inode,
|
||||
int flags,
|
||||
void * user_data)
|
||||
{
|
||||
(void) inode;
|
||||
(void) flags;
|
||||
(void) user_data;
|
||||
|
||||
wsfsp_respond_error(request, -1);
|
||||
}
|
||||
|
||||
void wsfsp_respond_open(
|
||||
struct wsfsp_request * request,
|
||||
uint32_t handle)
|
||||
{
|
||||
(void) request;
|
||||
(void) handle;
|
||||
|
||||
// ToDo: implement me
|
||||
}
|
||||
27
lib/wsfs/provider/operation/open_intern.h
Normal file
27
lib/wsfs/provider/operation/open_intern.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef WSFSP_OPERATION_OPEN_INTERN_H
|
||||
#define WSFSP_OPERATION_OPEN_INTERN_H
|
||||
|
||||
#include "wsfs/provider/operation/open.h"
|
||||
#include "wsfs/provider/provider_intern.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfsp_open(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wsfsp_open_default(
|
||||
struct wsfsp_request * request,
|
||||
ino_t inode,
|
||||
int flags,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
42
lib/wsfs/provider/operation/read.c
Normal file
42
lib/wsfs/provider/operation/read.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "wsfs/provider/operation/read_intern.h"
|
||||
#include <stdio.h>
|
||||
#include "wsfs/provider/operation/error.h"
|
||||
|
||||
void wsfsp_read(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
(void) context;
|
||||
(void) params;
|
||||
(void) id;
|
||||
|
||||
puts("read");
|
||||
}
|
||||
|
||||
void wsfsp_read_default(
|
||||
struct wsfsp_request * request,
|
||||
ino_t inode,
|
||||
uint32_t handle,
|
||||
size_t offset,
|
||||
size_t length,
|
||||
void * user_data)
|
||||
{
|
||||
(void) inode;
|
||||
(void) handle;
|
||||
(void) offset;
|
||||
(void) length;
|
||||
(void) user_data;
|
||||
|
||||
wsfsp_respond_error(request, -1);
|
||||
}
|
||||
|
||||
void wsfsp_respond_read(
|
||||
struct wsfsp_request * request,
|
||||
char const * data,
|
||||
size_t length)
|
||||
{
|
||||
(void) request;
|
||||
(void) data;
|
||||
(void) length;
|
||||
}
|
||||
29
lib/wsfs/provider/operation/read_intern.h
Normal file
29
lib/wsfs/provider/operation/read_intern.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef WSFSP_OPERATION_READ_INTERN_H
|
||||
#define WSFSP_OPERATION_READ_INTERN_H
|
||||
|
||||
#include "wsfs/provider/operation/read.h"
|
||||
#include "wsfs/provider/provider_intern.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfsp_read(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wsfsp_read_default(
|
||||
struct wsfsp_request * request,
|
||||
ino_t inode,
|
||||
uint32_t handle,
|
||||
size_t offset,
|
||||
size_t length,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
37
lib/wsfs/provider/operation/readdir.c
Normal file
37
lib/wsfs/provider/operation/readdir.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "wsfs/provider/operation/readdir_intern.h"
|
||||
#include <stdio.h>
|
||||
#include "wsfs/provider/operation/error.h"
|
||||
|
||||
void wsfsp_readdir(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
(void) context;
|
||||
(void) params;
|
||||
(void) id;
|
||||
|
||||
puts("readdir");
|
||||
}
|
||||
|
||||
void wsfsp_readdir_default(
|
||||
struct wsfsp_request * request,
|
||||
ino_t directory,
|
||||
void * user_data)
|
||||
{
|
||||
(void) directory;
|
||||
(void) user_data;
|
||||
|
||||
wsfsp_respond_error(request, -1);
|
||||
}
|
||||
|
||||
void wsfsp_respond_readdir(
|
||||
struct wsfsp_request * request,
|
||||
struct wsfsp_dirbuffer * dirbuffer)
|
||||
{
|
||||
(void) request;
|
||||
(void) dirbuffer;
|
||||
|
||||
// ToDo: implement me
|
||||
}
|
||||
|
||||
26
lib/wsfs/provider/operation/readdir_intern.h
Normal file
26
lib/wsfs/provider/operation/readdir_intern.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef WSFSP_OPERATION_READDIR_INTERN_H
|
||||
#define WSFSP_OPERATION_READDIR_INTERN_H
|
||||
|
||||
#include "wsfs/provider/operation/readdir.h"
|
||||
#include "wsfs/provider/provider_intern.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfsp_readdir(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
extern void wsfsp_readdir_default(
|
||||
struct wsfsp_request * request,
|
||||
ino_t directory,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
98
lib/wsfs/provider/provider.c
Normal file
98
lib/wsfs/provider/provider.c
Normal file
@@ -0,0 +1,98 @@
|
||||
#include "wsfs/provider/provider_intern.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wsfs/provider/request.h"
|
||||
#include "wsfs/provider/operation/lookup_intern.h"
|
||||
#include "wsfs/provider/operation/getattr_intern.h"
|
||||
#include "wsfs/provider/operation/readdir_intern.h"
|
||||
#include "wsfs/provider/operation/open_intern.h"
|
||||
#include "wsfs/provider/operation/close_intern.h"
|
||||
#include "wsfs/provider/operation/read_intern.h"
|
||||
|
||||
typedef void wsfsp_invoke_fn(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * params,
|
||||
int id);
|
||||
|
||||
|
||||
struct wsfsp_method
|
||||
{
|
||||
char const * name;
|
||||
wsfsp_invoke_fn * invoke;
|
||||
bool is_notification;
|
||||
};
|
||||
|
||||
static void wsfsp_provider_invoke_method(
|
||||
struct wsfsp_invokation_context * context,
|
||||
char const * method_name,
|
||||
json_t * params,
|
||||
int id)
|
||||
{
|
||||
static struct wsfsp_method const methods[] =
|
||||
{
|
||||
{"lookup", &wsfsp_lookup, false},
|
||||
{"getattr", &wsfsp_getattr, false},
|
||||
{"readdir", &wsfsp_readdir, false},
|
||||
{"open", &wsfsp_open, false},
|
||||
{"close", &wsfsp_close, true},
|
||||
{"read", &wsfsp_read, false}
|
||||
};
|
||||
static size_t const count = sizeof(methods) / sizeof(methods[0]);
|
||||
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
struct wsfsp_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 wsfsp_provider_invoke(
|
||||
struct wsfsp_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 ((NULL != method_holder) && (json_is_string(method_holder)) &&
|
||||
(NULL != params) && (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;
|
||||
|
||||
wsfsp_provider_invoke_method(context, method, params, id);
|
||||
}
|
||||
}
|
||||
|
||||
void wsfsp_connected_default(
|
||||
void * user_data)
|
||||
{
|
||||
(void) user_data;
|
||||
|
||||
// empty
|
||||
}
|
||||
|
||||
void wsfsp_disconnected_default(
|
||||
void * user_data)
|
||||
{
|
||||
(void) user_data;
|
||||
|
||||
// empty
|
||||
}
|
||||
|
||||
void wsfsp_ontimer_default(
|
||||
void * user_data)
|
||||
{
|
||||
(void) user_data;
|
||||
|
||||
// empty
|
||||
}
|
||||
38
lib/wsfs/provider/provider_intern.h
Normal file
38
lib/wsfs/provider/provider_intern.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef WSFSP_PROVIDER_INTERN_H
|
||||
#define WSFSP_PROVIDER_INTERN_H
|
||||
|
||||
#include "wsfs/provider/provider.h"
|
||||
#include "wsfs/provider/request.h"
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
struct wsfsp_invokation_context
|
||||
{
|
||||
struct wsfsp_provider * provider;
|
||||
void * user_data;
|
||||
struct wsfsp_request * request;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfsp_provider_invoke(
|
||||
struct wsfsp_invokation_context * context,
|
||||
json_t * request);
|
||||
|
||||
extern void wsfsp_connected_default(
|
||||
void * user_data);
|
||||
|
||||
extern void wsfsp_disconnected_default(
|
||||
void * user_data);
|
||||
|
||||
extern void wsfsp_ontimer_default(
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
24
lib/wsfs/provider/request.c
Normal file
24
lib/wsfs/provider/request.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "wsfs/provider/request.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
struct wsfsp_request * wsfsp_request_create(
|
||||
struct wsfsp_request * prototype,
|
||||
int id)
|
||||
{
|
||||
struct wsfsp_request * request = malloc(sizeof(struct wsfsp_request));
|
||||
if (NULL != request)
|
||||
{
|
||||
request->respond = prototype->respond;
|
||||
request->user_data = prototype->user_data;
|
||||
request->id = id;
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
void wsfsp_request_dispose(
|
||||
struct wsfsp_request * request)
|
||||
{
|
||||
free(request);
|
||||
}
|
||||
35
lib/wsfs/provider/request.h
Normal file
35
lib/wsfs/provider/request.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef WSFSP_REQUEST_H
|
||||
#define WSFSP_REQUEST_H
|
||||
|
||||
#include <jansson.h>
|
||||
#include "wsfs/provider/provider.h"
|
||||
|
||||
typedef void wsfsp_request_respond_fn(
|
||||
json_t * response,
|
||||
void * user_data);
|
||||
|
||||
struct wsfsp_request
|
||||
{
|
||||
wsfsp_request_respond_fn * respond;
|
||||
void * user_data;
|
||||
int id;
|
||||
};
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern struct wsfsp_request * wsfsp_request_create(
|
||||
struct wsfsp_request * prototype,
|
||||
int id);
|
||||
|
||||
extern void wsfsp_request_dispose(
|
||||
struct wsfsp_request * request);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
133
lib/wsfs/provider/url.c
Normal file
133
lib/wsfs/provider/url.c
Normal file
@@ -0,0 +1,133 @@
|
||||
#include "wsfs/provider/url.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wsfsp_url_protocol
|
||||
{
|
||||
char const * name;
|
||||
size_t name_length;
|
||||
int default_port;
|
||||
bool use_tls;
|
||||
};
|
||||
|
||||
static bool wsfsp_url_readprotocol(
|
||||
struct wsfsp_url * url,
|
||||
char const * * data)
|
||||
{
|
||||
static struct wsfsp_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 wsfsp_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 wsfsp_url_readhost(
|
||||
struct wsfsp_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 wsfsp_url_readport(
|
||||
struct wsfsp_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 wsfsp_url_readpath(
|
||||
struct wsfsp_url * url,
|
||||
char const * * data)
|
||||
{
|
||||
bool const result = ('/' == **data);
|
||||
url->path = strdup(*data);
|
||||
*data = NULL;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
bool wsfsp_url_init(
|
||||
struct wsfsp_url * url,
|
||||
char const * value)
|
||||
{
|
||||
memset(url, 0, sizeof(struct wsfsp_url));
|
||||
char const * data = value;
|
||||
|
||||
bool const result =
|
||||
wsfsp_url_readprotocol(url, &data) &&
|
||||
wsfsp_url_readhost(url, &data) &&
|
||||
wsfsp_url_readport(url, &data) &&
|
||||
wsfsp_url_readpath(url, &data)
|
||||
;
|
||||
|
||||
if (!result)
|
||||
{
|
||||
wsfsp_url_cleanup(url);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void wsfsp_url_cleanup(
|
||||
struct wsfsp_url * url)
|
||||
{
|
||||
free(url->host);
|
||||
free(url->path);
|
||||
memset(url, 0, sizeof(struct wsfsp_url));
|
||||
}
|
||||
|
||||
char const * wsfsp_url_gethost(
|
||||
struct wsfsp_url const * url);
|
||||
|
||||
int wsfsp_url_getport(
|
||||
struct wsfsp_url const * url);
|
||||
|
||||
char const * wsfsp_url_getpath(
|
||||
struct wsfsp_url const * url);
|
||||
33
lib/wsfs/provider/url.h
Normal file
33
lib/wsfs/provider/url.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef WSFSP_URL_H
|
||||
#define WSFSP_URL_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
struct wsfsp_url
|
||||
{
|
||||
char * host;
|
||||
int port;
|
||||
char * path;
|
||||
bool use_tls;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern bool wsfsp_url_init(
|
||||
struct wsfsp_url * url,
|
||||
char const * value);
|
||||
|
||||
extern void wsfsp_url_cleanup(
|
||||
struct wsfsp_url * url);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user