1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
/*
* Copyright (c) 2023 Balazs Scheidler <balazs.scheidler@axoflow.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "filterx/object-json-internal.h"
#include "filterx/object-null.h"
#include "filterx/object-primitive.h"
#include "filterx/object-string.h"
#include "filterx/object-message-value.h"
#include "filterx/filterx-weakrefs.h"
#include "filterx/object-list-interface.h"
#include "scanner/list-scanner/list-scanner.h"
#include "str-repr/encode.h"
#define JSON_ARRAY_MAX_SIZE 65536
struct FilterXJsonArray_
{
FilterXList super;
FilterXWeakRef root_container;
struct json_object *jso;
};
static gboolean
_truthy(FilterXObject *s)
{
return TRUE;
}
static gboolean
_marshal_to_json_literal_append(FilterXJsonArray *self, GString *repr, LogMessageValueType *t)
{
*t = LM_VT_JSON;
const gchar *json_repr = json_object_to_json_string_ext(self->jso, JSON_C_TO_STRING_PLAIN);
g_string_append(repr, json_repr);
return TRUE;
}
static gboolean
_marshal(FilterXObject *s, GString *repr, LogMessageValueType *t)
{
FilterXJsonArray *self = (FilterXJsonArray *) s;
for (gint i = 0; i < json_object_array_length(self->jso); i++)
{
struct json_object *el = json_object_array_get_idx(self->jso, i);
if (json_object_get_type(el) != json_type_string)
return _marshal_to_json_literal_append(self, repr, t);
if (i != 0)
g_string_append_c(repr, ',');
str_repr_encode_append(repr, json_object_get_string(el), -1, NULL);
}
*t = LM_VT_LIST;
return TRUE;
}
static gboolean
_map_to_json(FilterXObject *s, struct json_object **jso, FilterXObject **assoc_object)
{
FilterXJsonArray *self = (FilterXJsonArray *) s;
*jso = json_object_get(self->jso);
return TRUE;
}
static FilterXObject *
_clone(FilterXObject *s)
{
FilterXJsonArray *self = (FilterXJsonArray *) s;
struct json_object *jso = filterx_json_deep_copy(self->jso);
if (!jso)
return NULL;
return filterx_json_array_new_sub(jso, NULL);
}
static FilterXObject *
_get_subscript(FilterXList *s, guint64 index)
{
FilterXJsonArray *self = (FilterXJsonArray *) s;
struct json_object *jso = json_object_array_get_idx(self->jso, index);
if (!jso)
return NULL;
return filterx_json_convert_json_to_object_cached(&s->super, &self->root_container, jso);
}
static guint64
_len(FilterXList *s)
{
FilterXJsonArray *self = (FilterXJsonArray *) s;
return json_object_array_length(self->jso);
}
static gboolean
_append(FilterXList *s, FilterXObject **new_value)
{
FilterXJsonArray *self = (FilterXJsonArray *) s;
if (G_UNLIKELY(_len(s) >= JSON_ARRAY_MAX_SIZE))
return FALSE;
struct json_object *jso = NULL;
FilterXObject *assoc_object = NULL;
if (!filterx_object_map_to_json(*new_value, &jso, &assoc_object))
return FALSE;
filterx_json_associate_cached_object(jso, assoc_object);
if (json_object_array_add(self->jso, jso) != 0)
{
filterx_object_unref(assoc_object);
json_object_put(jso);
return FALSE;
}
self->super.super.modified_in_place = TRUE;
FilterXObject *root_container = filterx_weakref_get(&self->root_container);
if (root_container)
{
root_container->modified_in_place = TRUE;
filterx_object_unref(root_container);
}
filterx_object_unref(*new_value);
*new_value = assoc_object;
return TRUE;
}
static gboolean
_set_subscript(FilterXList *s, guint64 index, FilterXObject **new_value)
{
FilterXJsonArray *self = (FilterXJsonArray *) s;
if (G_UNLIKELY(index >= JSON_ARRAY_MAX_SIZE))
return FALSE;
struct json_object *jso = NULL;
FilterXObject *assoc_object = NULL;
if (!filterx_object_map_to_json(*new_value, &jso, &assoc_object))
return FALSE;
filterx_json_associate_cached_object(jso, assoc_object);
if (json_object_array_put_idx(self->jso, index, jso) != 0)
{
filterx_object_unref(assoc_object);
json_object_put(jso);
return FALSE;
}
self->super.super.modified_in_place = TRUE;
FilterXObject *root_container = filterx_weakref_get(&self->root_container);
if (root_container)
{
root_container->modified_in_place = TRUE;
filterx_object_unref(root_container);
}
filterx_object_unref(*new_value);
*new_value = assoc_object;
return TRUE;
}
static gboolean
_unset_index(FilterXList *s, guint64 index)
{
FilterXJsonArray *self = (FilterXJsonArray *) s;
if (G_UNLIKELY(index >= JSON_ARRAY_MAX_SIZE))
return FALSE;
if (json_object_array_del_idx(self->jso, index, 1) != 0)
return FALSE;
self->super.super.modified_in_place = TRUE;
FilterXObject *root_container = filterx_weakref_get(&self->root_container);
if (root_container)
{
root_container->modified_in_place = TRUE;
filterx_object_unref(root_container);
}
return TRUE;
}
/* NOTE: consumes root ref */
FilterXObject *
filterx_json_array_new_sub(struct json_object *jso, FilterXObject *root)
{
FilterXJsonArray *self = g_new0(FilterXJsonArray, 1);
filterx_list_init_instance(&self->super, &FILTERX_TYPE_NAME(json_array));
self->super.get_subscript = _get_subscript;
self->super.set_subscript = _set_subscript;
self->super.append = _append;
self->super.unset_index = _unset_index;
self->super.len = _len;
filterx_weakref_set(&self->root_container, root);
filterx_object_unref(root);
self->jso = jso;
return &self->super.super;
}
static void
_free(FilterXObject *s)
{
FilterXJsonArray *self = (FilterXJsonArray *) s;
json_object_put(self->jso);
filterx_weakref_clear(&self->root_container);
}
FilterXObject *
filterx_json_array_new_from_repr(const gchar *repr, gssize repr_len)
{
struct json_tokener *tokener = json_tokener_new();
struct json_object *jso;
jso = json_tokener_parse_ex(tokener, repr, repr_len < 0 ? strlen(repr) : repr_len);
if (repr_len >= 0 && json_tokener_get_error(tokener) == json_tokener_continue)
{
/* pass the closing NUL character */
jso = json_tokener_parse_ex(tokener, "", 1);
}
json_tokener_free(tokener);
if (!jso)
return NULL;
if (!json_object_is_type(jso, json_type_array))
{
json_object_put(jso);
return NULL;
}
return filterx_json_array_new_sub(jso, NULL);
}
FilterXObject *
filterx_json_array_new_from_syslog_ng_list(const gchar *repr, gssize repr_len)
{
struct json_object *jso = json_object_new_array();
ListScanner scanner;
list_scanner_init(&scanner);
list_scanner_input_string(&scanner, repr, repr_len);
for (gint i = 0; list_scanner_scan_next(&scanner); i++)
{
json_object_array_put_idx(jso, i,
json_object_new_string_len(list_scanner_get_current_value(&scanner),
list_scanner_get_current_value_len(&scanner)));
}
list_scanner_deinit(&scanner);
return filterx_json_array_new_sub(jso, NULL);
}
FilterXObject *
filterx_json_array_new_from_args(GPtrArray *args)
{
if (!args || args->len == 0)
return filterx_json_array_new_empty();
if (args->len != 1)
{
msg_error("FilterX: Failed to create JSON array: invalid number of arguments. "
"Usage: json_array() or json_array($raw_json_string) or json_array($existing_json_array)");
return NULL;
}
FilterXObject *arg = (FilterXObject *) g_ptr_array_index(args, 0);
if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(json_array)))
return filterx_object_ref(arg);
if (filterx_object_is_type(arg, &FILTERX_TYPE_NAME(message_value)))
{
FilterXObject *unmarshalled = filterx_object_unmarshal(arg);
if (!filterx_object_is_type(unmarshalled, &FILTERX_TYPE_NAME(json_array)))
{
filterx_object_unref(unmarshalled);
goto error;
}
return unmarshalled;
}
gsize repr_len;
const gchar *repr = filterx_string_get_value(arg, &repr_len);
if (repr)
return filterx_json_array_new_from_repr(repr, repr_len);
error:
msg_error("FilterX: Failed to create JSON object: invalid argument type. "
"Usage: json_array() or json_array($raw_json_string) or json_array($syslog_ng_list) or "
"json_array($existing_json_array)",
evt_tag_str("type", arg->type->name));
return NULL;
}
FilterXObject *
filterx_json_array_new_empty(void)
{
return filterx_json_array_new_sub(json_object_new_array(), NULL);
}
const gchar *
filterx_json_array_to_json_literal(FilterXObject *s)
{
FilterXJsonArray *self = (FilterXJsonArray *) s;
if (!filterx_object_is_type(s, &FILTERX_TYPE_NAME(json_array)))
return NULL;
return json_object_to_json_string_ext(self->jso, JSON_C_TO_STRING_PLAIN);
}
FILTERX_DEFINE_TYPE(json_array, FILTERX_TYPE_NAME(list),
.is_mutable = TRUE,
.truthy = _truthy,
.free_fn = _free,
.marshal = _marshal,
.map_to_json = _map_to_json,
.clone = _clone,
.list_factory = filterx_json_array_new_empty,
.dict_factory = filterx_json_object_new_empty,
);
|