mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
refactor: fix credentials
This commit is contained in:
@@ -125,8 +125,6 @@ static void wfp_impl_client_protocol_authenticate(
|
||||
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);
|
||||
|
||||
wfp_jsonrpc_proxy_invoke(
|
||||
protocol->proxy,
|
||||
@@ -134,7 +132,7 @@ static void wfp_impl_client_protocol_authenticate(
|
||||
protocol,
|
||||
"authenticate",
|
||||
"sj",
|
||||
cred_type, creds);
|
||||
cred_type, &wfp_impl_credentials_write, &credentials);
|
||||
|
||||
wfp_impl_credentials_cleanup(&credentials);
|
||||
}
|
||||
@@ -216,25 +214,15 @@ static int wfp_impl_client_protocol_callback(
|
||||
}
|
||||
|
||||
static void wfp_impl_client_protocol_send(
|
||||
json_t * request,
|
||||
char * data,
|
||||
size_t length,
|
||||
void * user_data)
|
||||
{
|
||||
struct wfp_client_protocol * protocol = user_data;
|
||||
|
||||
size_t length = json_dumpb(request, NULL, 0, JSON_COMPACT);
|
||||
if (0 < length)
|
||||
{
|
||||
char * raw_data = malloc(LWS_PRE + length);
|
||||
char * data = raw_data + LWS_PRE;
|
||||
json_dumpb(request, data, length, JSON_COMPACT);
|
||||
|
||||
struct wfp_message * message = wfp_message_create(data, length);
|
||||
wfp_slist_append(&protocol->messages, &message->item);
|
||||
lws_callback_on_writable(protocol->wsi);
|
||||
}
|
||||
|
||||
|
||||
// return true;
|
||||
struct wfp_message * message = wfp_message_create(data, length);
|
||||
wfp_slist_append(&protocol->messages, &message->item);
|
||||
lws_callback_on_writable(protocol->wsi);
|
||||
}
|
||||
|
||||
void wfp_impl_client_protocol_init(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "webfuse_provider/impl/credentials.h"
|
||||
#include "webfuse_provider/impl/json/writer.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -33,14 +34,29 @@ void wfp_impl_credentials_add(
|
||||
json_object_set_new(credentials->contents, key, json_string(value));
|
||||
}
|
||||
|
||||
void
|
||||
wfp_impl_credentials_write(
|
||||
struct wfp_json_writer * writer,
|
||||
void * data)
|
||||
{
|
||||
struct wfp_credentials * credentials = (struct wfp_credentials *) data;
|
||||
|
||||
wfp_impl_json_writer_object_begin(writer);
|
||||
char const * key;
|
||||
json_t * value;
|
||||
json_t * contents = credentials->contents;
|
||||
json_object_foreach(contents, key, value)
|
||||
{
|
||||
wfp_impl_json_writer_object_key(writer, key);
|
||||
wfp_impl_json_writer_write_string(writer, json_string_value(value));
|
||||
}
|
||||
|
||||
wfp_impl_json_writer_object_end(writer);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define WFP_IMPL_CREDENTIALS_H
|
||||
|
||||
#include "webfuse_provider/credentials.h"
|
||||
#include "webfuse_provider/impl/jsonrpc/proxy_intern.h"
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -9,6 +10,8 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfp_json_writer;
|
||||
|
||||
struct wfp_credentials
|
||||
{
|
||||
char * type;
|
||||
@@ -30,11 +33,15 @@ extern void wfp_impl_credentials_add(
|
||||
char const * key,
|
||||
char const * value);
|
||||
|
||||
extern char const * wfp_impl_credentials_get_type(
|
||||
extern void
|
||||
wfp_impl_credentials_write(
|
||||
struct wfp_json_writer * writer,
|
||||
void * data);
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
#include "webfuse_provider/status.h"
|
||||
|
||||
#include "webfuse_provider/impl/timer/timer.h"
|
||||
#include "webfuse_provider/impl/json/writer.h"
|
||||
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -48,16 +51,21 @@ static void wfp_jsonrpc_proxy_on_timeout(
|
||||
}
|
||||
}
|
||||
|
||||
static json_t * wfp_jsonrpc_request_create(
|
||||
static char * wfp_jsonrpc_request_create(
|
||||
size_t * length,
|
||||
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();
|
||||
struct wfp_json_writer * writer = wfp_impl_json_writer_create(128, LWS_PRE);
|
||||
wfp_impl_json_writer_object_begin(writer);
|
||||
|
||||
wfp_impl_json_writer_object_key(writer, "method");
|
||||
wfp_impl_json_writer_write_string(writer, method);
|
||||
|
||||
wfp_impl_json_writer_object_key(writer, "params");
|
||||
wfp_impl_json_writer_array_begin(writer);
|
||||
for (char const * param_type = param_info; '\0' != *param_type; param_type++)
|
||||
{
|
||||
switch(*param_type)
|
||||
@@ -65,19 +73,20 @@ static json_t * wfp_jsonrpc_request_create(
|
||||
case 's':
|
||||
{
|
||||
char const * const value = va_arg(args, char const *);
|
||||
json_array_append_new(params, json_string(value));
|
||||
wfp_impl_json_writer_write_string(writer, value);
|
||||
}
|
||||
break;
|
||||
case 'i':
|
||||
{
|
||||
int const value = va_arg(args, int);
|
||||
json_array_append_new(params, json_integer(value));
|
||||
wfp_impl_json_writer_write_int(writer, value);
|
||||
}
|
||||
break;
|
||||
case 'j':
|
||||
{
|
||||
json_t * const value = va_arg(args, json_t *);
|
||||
json_array_append_new(params, value);
|
||||
wfp_jsonrpc_custom_write_fn * write = va_arg(args, wfp_jsonrpc_custom_write_fn *);
|
||||
void * data = va_arg(args, void *);
|
||||
write(writer,data);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -85,15 +94,20 @@ static json_t * wfp_jsonrpc_request_create(
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
wfp_impl_json_writer_array_end(writer);
|
||||
|
||||
json_object_set_new(request, "params", params);
|
||||
if (0 != id)
|
||||
{
|
||||
json_object_set_new(request, "id", json_integer(id));
|
||||
}
|
||||
|
||||
return request;
|
||||
if (0 != id)
|
||||
{
|
||||
wfp_impl_json_writer_object_key(writer, "id");
|
||||
wfp_impl_json_writer_write_int(writer, id);
|
||||
}
|
||||
|
||||
wfp_impl_json_writer_object_end(writer);
|
||||
|
||||
char * message = wfp_impl_json_writer_take_data(writer, length);
|
||||
wfp_impl_json_writer_dispose(writer);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
void wfp_jsonrpc_proxy_init(
|
||||
@@ -147,9 +161,10 @@ void wfp_jsonrpc_proxy_vinvoke(
|
||||
proxy->request.id = 42;
|
||||
wfp_timer_start(proxy->request.timer, proxy->timeout);
|
||||
|
||||
json_t * request = wfp_jsonrpc_request_create(method_name, proxy->request.id, param_info, args);
|
||||
proxy->send(request, proxy->user_data);
|
||||
json_decref(request);
|
||||
size_t length;
|
||||
char * message = wfp_jsonrpc_request_create(&length, method_name, proxy->request.id, param_info, args);
|
||||
|
||||
proxy->send(message, length, proxy->user_data);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -163,13 +178,10 @@ extern void wfp_jsonrpc_proxy_vnotify(
|
||||
char const * param_info,
|
||||
va_list args)
|
||||
{
|
||||
json_t * request = wfp_jsonrpc_request_create(method_name, 0, param_info, args);
|
||||
size_t length;
|
||||
char * request = wfp_jsonrpc_request_create(&length, method_name, 0, param_info, args);
|
||||
|
||||
if (NULL != request)
|
||||
{
|
||||
proxy->send(request, proxy->user_data);
|
||||
json_decref(request);
|
||||
}
|
||||
proxy->send(request, length, proxy->user_data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,6 +21,12 @@ extern "C" {
|
||||
|
||||
struct wfp_jsonrpc_proxy;
|
||||
struct wfp_timer_manager;
|
||||
struct wfp_json_writer;
|
||||
|
||||
typedef void
|
||||
wfp_jsonrpc_custom_write_fn(
|
||||
struct wfp_json_writer * writer,
|
||||
void * data);
|
||||
|
||||
extern struct wfp_jsonrpc_proxy *
|
||||
wfp_jsonrpc_proxy_create(
|
||||
|
||||
@@ -3,17 +3,19 @@
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef void wfp_jsonrpc_send_fn(
|
||||
json_t * request,
|
||||
char * message,
|
||||
size_t length,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
Reference in New Issue
Block a user