refactor: fix credentials

pull/3/head
Falk Werner 4 years ago
parent fe3e80ed0a
commit 6155b03cf8

@ -125,8 +125,6 @@ static void wfp_impl_client_protocol_authenticate(
protocol->provider.get_credentials(&credentials, protocol->user_data); protocol->provider.get_credentials(&credentials, protocol->user_data);
char const * cred_type = wfp_impl_credentials_get_type(&credentials); 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( wfp_jsonrpc_proxy_invoke(
protocol->proxy, protocol->proxy,
@ -134,7 +132,7 @@ static void wfp_impl_client_protocol_authenticate(
protocol, protocol,
"authenticate", "authenticate",
"sj", "sj",
cred_type, creds); cred_type, &wfp_impl_credentials_write, &credentials);
wfp_impl_credentials_cleanup(&credentials); wfp_impl_credentials_cleanup(&credentials);
} }
@ -216,25 +214,15 @@ static int wfp_impl_client_protocol_callback(
} }
static void wfp_impl_client_protocol_send( static void wfp_impl_client_protocol_send(
json_t * request, char * data,
size_t length,
void * user_data) void * user_data)
{ {
struct wfp_client_protocol * protocol = user_data; struct wfp_client_protocol * protocol = user_data;
size_t length = json_dumpb(request, NULL, 0, JSON_COMPACT); struct wfp_message * message = wfp_message_create(data, length);
if (0 < length) wfp_slist_append(&protocol->messages, &message->item);
{ lws_callback_on_writable(protocol->wsi);
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;
} }
void wfp_impl_client_protocol_init( void wfp_impl_client_protocol_init(

@ -1,4 +1,5 @@
#include "webfuse_provider/impl/credentials.h" #include "webfuse_provider/impl/credentials.h"
#include "webfuse_provider/impl/json/writer.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -33,14 +34,29 @@ void wfp_impl_credentials_add(
json_object_set_new(credentials->contents, key, json_string(value)); json_object_set_new(credentials->contents, key, json_string(value));
} }
char const * wfp_impl_credentials_get_type( void
struct wfp_credentials * credentials) wfp_impl_credentials_write(
struct wfp_json_writer * writer,
void * data)
{ {
return credentials->type; 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);
} }
json_t * wfp_impl_credentials_get( char const * wfp_impl_credentials_get_type(
struct wfp_credentials * credentials) struct wfp_credentials * credentials)
{ {
return credentials->contents; return credentials->type;
} }

@ -2,6 +2,7 @@
#define WFP_IMPL_CREDENTIALS_H #define WFP_IMPL_CREDENTIALS_H
#include "webfuse_provider/credentials.h" #include "webfuse_provider/credentials.h"
#include "webfuse_provider/impl/jsonrpc/proxy_intern.h"
#include <jansson.h> #include <jansson.h>
#ifdef __cplusplus #ifdef __cplusplus
@ -9,6 +10,8 @@ extern "C"
{ {
#endif #endif
struct wfp_json_writer;
struct wfp_credentials struct wfp_credentials
{ {
char * type; char * type;
@ -30,12 +33,16 @@ extern void wfp_impl_credentials_add(
char const * key, char const * key,
char const * value); char const * value);
extern char const * wfp_impl_credentials_get_type( extern void
struct wfp_credentials * credentials); wfp_impl_credentials_write(
struct wfp_json_writer * writer,
void * data);
extern json_t * wfp_impl_credentials_get( extern char const *
wfp_impl_credentials_get_type(
struct wfp_credentials * credentials); struct wfp_credentials * credentials);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -4,6 +4,9 @@
#include "webfuse_provider/status.h" #include "webfuse_provider/status.h"
#include "webfuse_provider/impl/timer/timer.h" #include "webfuse_provider/impl/timer/timer.h"
#include "webfuse_provider/impl/json/writer.h"
#include <libwebsockets.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.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, char const * method,
int id, int id,
char const * param_info, char const * param_info,
va_list args) va_list args)
{ {
json_t * request = json_object(); struct wfp_json_writer * writer = wfp_impl_json_writer_create(128, LWS_PRE);
json_object_set_new(request, "method", json_string(method)); wfp_impl_json_writer_object_begin(writer);
json_t * params = json_array();
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++) for (char const * param_type = param_info; '\0' != *param_type; param_type++)
{ {
switch(*param_type) switch(*param_type)
@ -65,19 +73,20 @@ static json_t * wfp_jsonrpc_request_create(
case 's': case 's':
{ {
char const * const value = va_arg(args, char const *); 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; break;
case 'i': case 'i':
{ {
int const value = va_arg(args, int); int const value = va_arg(args, int);
json_array_append_new(params, json_integer(value)); wfp_impl_json_writer_write_int(writer, value);
} }
break; break;
case 'j': case 'j':
{ {
json_t * const value = va_arg(args, json_t *); wfp_jsonrpc_custom_write_fn * write = va_arg(args, wfp_jsonrpc_custom_write_fn *);
json_array_append_new(params, value); void * data = va_arg(args, void *);
write(writer,data);
} }
break; break;
default: default:
@ -85,15 +94,20 @@ static json_t * wfp_jsonrpc_request_create(
break; break;
} }
} }
wfp_impl_json_writer_array_end(writer);
json_object_set_new(request, "params", params); if (0 != id)
if (0 != id) {
{ wfp_impl_json_writer_object_key(writer, "id");
json_object_set_new(request, "id", json_integer(id)); wfp_impl_json_writer_write_int(writer, id);
} }
return request; 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( void wfp_jsonrpc_proxy_init(
@ -147,9 +161,10 @@ void wfp_jsonrpc_proxy_vinvoke(
proxy->request.id = 42; proxy->request.id = 42;
wfp_timer_start(proxy->request.timer, proxy->timeout); wfp_timer_start(proxy->request.timer, proxy->timeout);
json_t * request = wfp_jsonrpc_request_create(method_name, proxy->request.id, param_info, args); size_t length;
proxy->send(request, proxy->user_data); char * message = wfp_jsonrpc_request_create(&length, method_name, proxy->request.id, param_info, args);
json_decref(request);
proxy->send(message, length, proxy->user_data);
} }
else else
{ {
@ -163,13 +178,10 @@ extern void wfp_jsonrpc_proxy_vnotify(
char const * param_info, char const * param_info,
va_list args) 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, length, proxy->user_data);
{
proxy->send(request, proxy->user_data);
json_decref(request);
}
} }

@ -21,6 +21,12 @@ extern "C" {
struct wfp_jsonrpc_proxy; struct wfp_jsonrpc_proxy;
struct wfp_timer_manager; 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 * extern struct wfp_jsonrpc_proxy *
wfp_jsonrpc_proxy_create( wfp_jsonrpc_proxy_create(

@ -3,17 +3,19 @@
#ifndef __cplusplus #ifndef __cplusplus
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#else
#include <cstddef>
#endif #endif
#include <jansson.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
typedef void wfp_jsonrpc_send_fn( typedef void wfp_jsonrpc_send_fn(
json_t * request, char * message,
size_t length,
void * user_data); void * user_data);
#ifdef __cplusplus #ifdef __cplusplus

@ -27,32 +27,27 @@ namespace
struct SendContext struct SendContext
{ {
json_t * response; std::string response;
bool is_called; bool is_called;
explicit SendContext() explicit SendContext()
: response(nullptr) : is_called(false)
, is_called(false)
{ {
} }
~SendContext() ~SendContext()
{ {
if (nullptr != response)
{
json_decref(response);
}
} }
}; };
void jsonrpc_send( void jsonrpc_send(
json_t * request, char * request,
size_t length,
void * user_data) void * user_data)
{ {
SendContext * context = reinterpret_cast<SendContext*>(user_data); SendContext * context = reinterpret_cast<SendContext*>(user_data);
context->is_called = true; context->is_called = true;
context->response = request; context->response = request;
json_incref(request);
} }
struct FinishedContext struct FinishedContext
@ -122,22 +117,7 @@ TEST(wfp_jsonrpc_proxy, invoke)
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_STREQ("{\"method\":\"foo\",\"params\":[\"bar\",42],\"id\":42}", send_context.response.c_str());
json_t * method = json_object_get(send_context.response, "method");
ASSERT_TRUE(json_is_string(method));
ASSERT_STREQ("foo", json_string_value(method));
json_t * params = json_object_get(send_context.response, "params");
ASSERT_TRUE(json_is_array(params));
ASSERT_EQ(2, json_array_size(params));
ASSERT_TRUE(json_is_string(json_array_get(params, 0)));
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0)));
ASSERT_TRUE(json_is_integer(json_array_get(params, 1)));
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1)));
json_t * id = json_object_get(send_context.response, "id");
ASSERT_TRUE(json_is_integer(id));
ASSERT_FALSE(finished_context.is_called); ASSERT_FALSE(finished_context.is_called);
@ -165,7 +145,6 @@ TEST(wfp_jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", ""); wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", "");
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
ASSERT_FALSE(finished_context.is_called); ASSERT_FALSE(finished_context.is_called);
@ -209,14 +188,10 @@ TEST(wfp_jsonrpc_proxy, on_result)
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
json_t * id = json_object_get(send_context.response, "id");
ASSERT_TRUE(json_is_number(id));
json_t * response = json_object(); json_t * response = json_object();
json_object_set_new(response, "result", json_string("okay")); json_object_set_new(response, "result", json_string("okay"));
json_object_set(response, "id", id); json_object_set_new(response, "id", json_integer(42));
wfp_jsonrpc_proxy_onresult(proxy, response); wfp_jsonrpc_proxy_onresult(proxy, response);
json_decref(response); json_decref(response);
@ -243,14 +218,10 @@ TEST(wfp_jsonrpc_proxy, on_result_reject_response_with_unknown_id)
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
json_t * id = json_object_get(send_context.response, "id");
ASSERT_TRUE(json_is_number(id));
json_t * response = json_object(); json_t * response = json_object();
json_object_set_new(response, "result", json_string("okay")); json_object_set_new(response, "result", json_string("okay"));
json_object_set_new(response, "id", json_integer(1 + json_integer_value(id))); json_object_set_new(response, "id", json_integer(1234));
wfp_jsonrpc_proxy_onresult(proxy, response); wfp_jsonrpc_proxy_onresult(proxy, response);
json_decref(response); json_decref(response);
@ -274,7 +245,6 @@ TEST(wfp_jsonrpc_proxy, timeout)
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
std::this_thread::sleep_for(10ms); std::this_thread::sleep_for(10ms);
wfp_timer_manager_check(timer_manager); wfp_timer_manager_check(timer_manager);
@ -299,7 +269,6 @@ TEST(wfp_jsonrpc_proxy, cleanup_pending_request)
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42); wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response));
ASSERT_FALSE(finished_context.is_called); ASSERT_FALSE(finished_context.is_called);
@ -323,22 +292,7 @@ TEST(wfp_jsonrpc_proxy, notify)
wfp_jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42); wfp_jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42);
ASSERT_TRUE(send_context.is_called); ASSERT_TRUE(send_context.is_called);
ASSERT_TRUE(json_is_object(send_context.response)); ASSERT_STREQ("{\"method\":\"foo\",\"params\":[\"bar\",42]}", send_context.response.c_str());
json_t * method = json_object_get(send_context.response, "method");
ASSERT_TRUE(json_is_string(method));
ASSERT_STREQ("foo", json_string_value(method));
json_t * params = json_object_get(send_context.response, "params");
ASSERT_TRUE(json_is_array(params));
ASSERT_EQ(2, json_array_size(params));
ASSERT_TRUE(json_is_string(json_array_get(params, 0)));
ASSERT_STREQ("bar", json_string_value(json_array_get(params, 0)));
ASSERT_TRUE(json_is_integer(json_array_get(params, 1)));
ASSERT_EQ(42, json_integer_value(json_array_get(params, 1)));
json_t * id = json_object_get(send_context.response, "id");
ASSERT_EQ(nullptr, id);
wfp_jsonrpc_proxy_dispose(proxy); wfp_jsonrpc_proxy_dispose(proxy);
wfp_timer_manager_dispose(timer_manager); wfp_timer_manager_dispose(timer_manager);

Loading…
Cancel
Save