diff options
| author | Unbit <info@unbit.it> | 2014-01-07 09:22:49 +0100 |
|---|---|---|
| committer | Unbit <info@unbit.it> | 2014-01-07 09:22:49 +0100 |
| commit | eae335a43e35a1cf280c00d63a12977a78a757a7 (patch) | |
| tree | 0bf637064a57bfe5b76b7369904361b7ead207fd /core/json.c | |
| parent | e0cf245a0e7f08955d344d5434bcf72d18b4e3eb (diff) | |
| download | uwsgi-eae335a43e35a1cf280c00d63a12977a78a757a7.tar.gz | |
added support for yajl 1.0
Diffstat (limited to 'core/json.c')
| -rw-r--r-- | core/json.c | 119 |
1 files changed, 118 insertions, 1 deletions
diff --git a/core/json.c b/core/json.c index 49d313e0..54c321eb 100644 --- a/core/json.c +++ b/core/json.c @@ -4,7 +4,124 @@ extern struct uwsgi_server uwsgi; -#ifdef UWSGI_JSON_YAJL +#if defined(UWSGI_JSON_YAJL_OLD) +#include <yajl/yajl_parse.h> + +struct uwsgi_yajl_old_state { + char *object; + char *key; + int in_object; + int is_array; +}; + +static int uwsgi_yajl_cb_null(void *ctx) { + struct uwsgi_yajl_old_state *uyos = (struct uwsgi_yajl_old_state *) ctx; + if (!uyos->key) return 1; + add_exported_option(uyos->key, strdup("0"), 0); + return 1; +} + +static int uwsgi_yajl_cb_boolean(void *ctx, int b) { + struct uwsgi_yajl_old_state *uyos = (struct uwsgi_yajl_old_state *) ctx; + if (!uyos->key) return 1; + if (b) { + add_exported_option(uyos->key, strdup("1"), 0); + } + else { + add_exported_option(uyos->key, strdup("0"), 0); + } + return 1; +} + +static int uwsgi_yajl_cb_integer(void *ctx, long n) { + struct uwsgi_yajl_old_state *uyos = (struct uwsgi_yajl_old_state *) ctx; + if (!uyos->key) return 1; + add_exported_option(uyos->key, uwsgi_64bit2str((int64_t) n), 0); + return 1; +} + +static int uwsgi_yajl_cb_double(void *ctx, double n) { + return uwsgi_yajl_cb_integer(ctx, (long) n); +} + +static int uwsgi_yajl_cb_string(void *ctx, const unsigned char *s, unsigned int s_len) { + struct uwsgi_yajl_old_state *uyos = (struct uwsgi_yajl_old_state *) ctx; + if (!uyos->key) return 1; + add_exported_option(uyos->key, uwsgi_concat2n((char *)s, (size_t)s_len, "", 0), 0); + return 1; +} + +static int uwsgi_yajl_cb_number(void *ctx, const char *n, unsigned int n_len) { + return uwsgi_yajl_cb_string(ctx, (const unsigned char *)n, n_len); +} + +static int uwsgi_yajl_cb_map_key(void *ctx, const unsigned char *s, unsigned int s_len) { + struct uwsgi_yajl_old_state *uyos = (struct uwsgi_yajl_old_state *) ctx; + if (!uyos->in_object) { + if (!uwsgi_strncmp(uyos->object, strlen(uyos->object), (char *)s, (size_t)s_len)) { + uyos->in_object = 1; + } + return 1; + } + + uyos->key = uwsgi_concat2n((char *)s, (size_t)s_len, "", 0); + return 1; +} + + +static yajl_callbacks callbacks = { + .yajl_null = uwsgi_yajl_cb_null, + .yajl_boolean = uwsgi_yajl_cb_boolean, + .yajl_integer = uwsgi_yajl_cb_integer, + .yajl_double = uwsgi_yajl_cb_double, + .yajl_number = uwsgi_yajl_cb_number, + .yajl_string = uwsgi_yajl_cb_string, + .yajl_start_map = NULL, + .yajl_map_key = uwsgi_yajl_cb_map_key, + .yajl_end_map = NULL, + .yajl_start_array = NULL, + .yajl_end_array = NULL, +}; + +void uwsgi_json_config(char *file, char *magic_table[]) { + size_t len = 0; + + char *object_asked = "uwsgi"; + char *colon; + + if (uwsgi_check_scheme(file)) { + colon = uwsgi_get_last_char(file, '/'); + colon = uwsgi_get_last_char(colon, ':'); + } + else { + colon = uwsgi_get_last_char(file, ':'); + } + + if (colon) { + colon[0] = 0; + if (colon[1] != 0) { + object_asked = colon + 1; + } + } + + struct uwsgi_yajl_old_state uyos; + memset(&uyos, 0, sizeof(struct uwsgi_yajl_old_state)); + uyos.object = object_asked; + uwsgi_log_initial("[uWSGI] getting JSON configuration from %s\n", file); + + char *json_data = uwsgi_open_and_read(file, &len, 1, magic_table); + + yajl_handle hand = yajl_alloc(&callbacks, NULL, NULL, &uyos); + + yajl_status s = yajl_parse(hand, (const unsigned char *)json_data, len); + if (s != yajl_status_ok) { + uwsgi_log("%s\n", yajl_get_error(hand, 1, (const unsigned char *)json_data, len)); + exit(1); + } +} + + +#elif defined(UWSGI_JSON_YAJL) #include <yajl/yajl_tree.h> void uwsgi_json_config(char *file, char *magic_table[]) { |
