1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00
falk-werner_webfuse-provider/lib/webfuse_provider/impl/message_writer.c

144 lines
4.4 KiB
C
Raw Normal View History

2020-07-08 19:29:18 +00:00
#include "webfuse_provider/impl/message_writer.h"
#include "webfuse_provider/impl/message.h"
#include "webfuse_provider/impl/util/base64.h"
#include "webfuse_provider/impl/dirbuffer.h"
2020-07-09 20:12:13 +00:00
#include "webfuse_provider/impl/json/writer.h"
2020-07-08 19:29:18 +00:00
#include <libwebsockets.h>
#include <stdlib.h>
2020-07-09 20:12:13 +00:00
#include <stdbool.h>
2020-07-08 19:29:18 +00:00
struct wfp_message_writer
{
2020-07-09 20:12:13 +00:00
struct wfp_json_writer * json_writer;
2020-07-08 19:29:18 +00:00
int id;
2020-07-09 20:12:13 +00:00
bool is_finished;
2020-07-08 19:29:18 +00:00
};
struct wfp_message_writer *
wfp_impl_message_writer_create(int id)
{
2020-07-09 20:35:15 +00:00
struct wfp_message_writer * writer = malloc(sizeof(struct wfp_message_writer));
2020-07-09 20:12:13 +00:00
writer->json_writer = wfp_impl_json_writer_create(1024, LWS_PRE);
2020-07-08 19:29:18 +00:00
writer->id = id;
2020-07-09 20:12:13 +00:00
writer->is_finished = false;
wfp_impl_json_writer_object_begin(writer->json_writer);
wfp_impl_json_writer_object_key(writer->json_writer, "result");
wfp_impl_json_writer_object_begin(writer->json_writer);
2020-07-08 19:29:18 +00:00
return writer;
}
void
wfp_impl_message_writer_dispose(
struct wfp_message_writer * writer)
{
2020-07-09 20:12:13 +00:00
wfp_impl_json_writer_dispose(writer->json_writer);
2020-07-08 19:29:18 +00:00
free(writer);
}
struct wfp_message *
wfp_impl_message_writer_take_message(
struct wfp_message_writer * writer)
{
2020-07-09 20:12:13 +00:00
if (!writer->is_finished)
2020-07-08 19:29:18 +00:00
{
2020-07-09 20:12:13 +00:00
wfp_impl_json_writer_object_end(writer->json_writer);
wfp_impl_json_writer_object_key(writer->json_writer, "id");
wfp_impl_json_writer_write_int(writer->json_writer, writer->id);
wfp_impl_json_writer_object_end(writer->json_writer);
writer->is_finished = true;
2020-07-08 19:29:18 +00:00
}
size_t length;
char * data = wfp_impl_json_writer_take_data(writer->json_writer, &length);
2020-07-08 19:29:18 +00:00
return wfp_message_create(data, length);
2020-07-08 19:29:18 +00:00
}
void
wfp_impl_message_writer_add_int(
struct wfp_message_writer * writer,
char const * key,
int value)
{
2020-07-09 20:12:13 +00:00
wfp_impl_json_writer_object_key(writer->json_writer, key);
wfp_impl_json_writer_write_int(writer->json_writer, value);
2020-07-08 19:29:18 +00:00
}
void
wfp_impl_message_writer_add_string(
struct wfp_message_writer * writer,
char const * key,
char const * value)
{
2020-07-09 20:12:13 +00:00
wfp_impl_json_writer_object_key(writer->json_writer, key);
wfp_impl_json_writer_write_string(writer->json_writer, value);
2020-07-08 19:29:18 +00:00
}
void
wfp_impl_message_writer_add_bytes(
struct wfp_message_writer * writer,
char const * key,
char const * data,
size_t length)
{
2020-07-09 20:12:13 +00:00
wfp_impl_json_writer_object_key(writer->json_writer, key);
wfp_impl_json_writer_write_bytes(writer->json_writer, data, length);
2020-07-08 19:29:18 +00:00
}
void
wfp_impl_message_writer_add_dirbuffer(
struct wfp_message_writer * writer,
struct wfp_dirbuffer * dirbuffer)
{
2020-07-09 20:12:13 +00:00
wfp_impl_json_writer_reset(writer->json_writer);
wfp_impl_json_writer_object_begin(writer->json_writer);
wfp_impl_json_writer_object_key(writer->json_writer, "result");
wfp_impl_json_writer_array_begin(writer->json_writer);
size_t const count = wfp_impl_dirbuffer_size(dirbuffer);
for (size_t i = 0; i < count; i++)
2020-07-09 20:12:13 +00:00
{
struct wfp_dirbuffer_entry const * entry = wfp_impl_dirbuffer_entry_at(dirbuffer, i);
2020-07-09 20:12:13 +00:00
wfp_impl_json_writer_object_begin(writer->json_writer);
wfp_impl_json_writer_object_key(writer->json_writer, "name");
wfp_impl_json_writer_write_string(writer->json_writer, entry->name);
2020-07-09 20:12:13 +00:00
wfp_impl_json_writer_object_key(writer->json_writer, "inode");
wfp_impl_json_writer_write_int(writer->json_writer, entry->inode);
2020-07-09 20:12:13 +00:00
wfp_impl_json_writer_object_end(writer->json_writer);
}
wfp_impl_json_writer_array_end(writer->json_writer);
wfp_impl_json_writer_object_key(writer->json_writer, "id");
wfp_impl_json_writer_write_int(writer->json_writer, writer->id);
wfp_impl_json_writer_object_end(writer->json_writer);
writer->is_finished = true;
2020-07-08 19:29:18 +00:00
}
void
wfp_impl_message_writer_set_error(
struct wfp_message_writer * writer,
int error_code)
{
2020-07-09 20:12:13 +00:00
wfp_impl_json_writer_reset(writer->json_writer);
wfp_impl_json_writer_object_begin(writer->json_writer);
wfp_impl_json_writer_object_key(writer->json_writer, "error");
wfp_impl_json_writer_object_begin(writer->json_writer);
wfp_impl_json_writer_object_key(writer->json_writer, "code");
wfp_impl_json_writer_write_int(writer->json_writer, error_code);
wfp_impl_json_writer_object_end(writer->json_writer);
wfp_impl_json_writer_object_key(writer->json_writer, "id");
wfp_impl_json_writer_write_int(writer->json_writer, writer->id);
wfp_impl_json_writer_object_end(writer->json_writer);
writer->is_finished = true;
2020-07-08 19:29:18 +00:00
}