Skip to content

Commit

Permalink
Merge pull request #391 from proftpd/json-api
Browse files Browse the repository at this point in the history
Provide a JSON API which uses pools, and which wraps/hides the CCAN J…
  • Loading branch information
Castaglia committed Jan 16, 2017
2 parents 8c56bb9 + b568182 commit fab26d5
Show file tree
Hide file tree
Showing 11 changed files with 2,833 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -68,7 +68,7 @@ after_success:
# capture the test coverage info
- lcov --ignore-errors gcov,source --directory . --capture --output-file coverage.info
# filter out system and test code
- lcov --remove coverage.info 'lib/glibc-glob.*' 'lib/pr_fnmatch_loop.*' 'tests/*' '/usr/*' --output-file coverage.info
- lcov --remove coverage.info 'lib/glibc-glob.*' 'lib/ccan-json.*' 'lib/hanson-tpl.*' 'lib/pr_fnmatch_loop.*' 'tests/*' '/usr/*' --output-file coverage.info
# debug before upload
- lcov --list coverage.info
# upload coverage info to coveralls
Expand Down
4 changes: 2 additions & 2 deletions Make.rules.in
Expand Up @@ -69,7 +69,7 @@ OBJS=main.o timers.o sets.o pool.o privs.o str.o table.o regexp.o configdb.o \
feat.o netio.o cmd.o response.o ascii.o data.o modules.o stash.o \
display.o auth.o fsio.o mkhome.o ctrls.o event.o var.o throttle.o \
session.o trace.o encode.o proctitle.o filter.o pidfile.o env.o version.o \
rlimit.o wtmp.o memcache.o
rlimit.o wtmp.o json.o memcache.o

BUILD_OBJS=src/main.o src/timers.o src/sets.o src/pool.o src/privs.o src/str.o \
src/table.o src/regexp.o src/configdb.o src/dirtree.o src/expr.o \
Expand All @@ -81,7 +81,7 @@ BUILD_OBJS=src/main.o src/timers.o src/sets.o src/pool.o src/privs.o src/str.o \
src/mkhome.o src/ctrls.o src/event.o src/var.o src/throttle.o \
src/session.o src/trace.o src/encode.o src/proctitle.o src/filter.o \
src/pidfile.o src/env.o src/version.o src/rlimit.o src/wtmp.o \
src/memcache.o
src/json.o src/memcache.o

SHARED_MODULE_DIRS=@SHARED_MODULE_DIRS@
SHARED_MODULE_LIBS=@SHARED_MODULE_LIBS@
Expand Down
2 changes: 1 addition & 1 deletion include/ccan-json.h
Expand Up @@ -77,7 +77,7 @@ int json_validate (const char *json);

/*** Lookup and traversal ***/

JsonNode *json_find_element (JsonNode *array, int index);
JsonNode *json_find_element (JsonNode *array, unsigned int index);
JsonNode *json_find_member (JsonNode *object, const char *key);

JsonNode *json_first_child (const JsonNode *node);
Expand Down
1 change: 1 addition & 0 deletions include/conf.h
Expand Up @@ -460,6 +460,7 @@ typedef struct {
#include "pidfile.h"
#include "env.h"
#include "pr-syslog.h"
#include "json.h"
#include "memcache.h"

# ifdef HAVE_SETPASSENT
Expand Down
144 changes: 144 additions & 0 deletions include/json.h
@@ -0,0 +1,144 @@
/*
* ProFTPD - FTP server daemon
* Copyright (c) 2017 The ProFTPD Project team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, The ProFTPD Project team and other respective
* copyright holders give permission to link this program with OpenSSL, and
* distribute the resulting executable, without including the source code for
* OpenSSL in the source distribution.
*/

/* JSON API */

#ifndef PR_JSON_H
#define PR_JSON_H

#include "conf.h"

typedef struct json_list_st pr_json_array_t;
typedef struct json_obj_st pr_json_object_t;

/* JSON Objects */

pr_json_object_t *pr_json_object_alloc(pool *p);

int pr_json_object_free(pr_json_object_t *json);

pr_json_object_t *pr_json_object_from_text(pool *p, const char *text);

const char *pr_json_object_to_text(pool *p, const pr_json_object_t *json,
const char *indent);

/* Returns the number of members (keys) in the given object. */
int pr_json_object_count(const pr_json_object_t *json);

/* Removes the object member under this key. */
int pr_json_object_remove(pr_json_object_t *json, const char *key);

/* Checks where a member under the given key exists. Returns TRUE, FALSE,
* or -1 is there was some other error.
*/
int pr_json_object_exists(const pr_json_object_t *json, const char *key);

int pr_json_object_get_bool(pool *p, const pr_json_object_t *json,
const char *key, int *val);
int pr_json_object_set_bool(pool *p, pr_json_object_t *json, const char *key,
int val);

int pr_json_object_get_null(pool *p, const pr_json_object_t *json,
const char *key);
int pr_json_object_set_null(pool *p, pr_json_object_t *json, const char *key);

int pr_json_object_get_number(pool *p, const pr_json_object_t *json,
const char *key, double *val);
int pr_json_object_set_number(pool *p, pr_json_object_t *json, const char *key,
double val);

int pr_json_object_get_string(pool *p, const pr_json_object_t *json,
const char *key, char **val);
int pr_json_object_set_string(pool *p, pr_json_object_t *json, const char *key,
const char *val);

int pr_json_object_get_array(pool *p, const pr_json_object_t *json,
const char *key, pr_json_array_t **val);
int pr_json_object_set_array(pool *p, pr_json_object_t *json, const char *key,
const pr_json_array_t *val);

int pr_json_object_get_object(pool *p, const pr_json_object_t *json,
const char *key, pr_json_object_t **val);
int pr_json_object_set_object(pool *p, pr_json_object_t *json, const char *key,
const pr_json_object_t *val);

/* JSON Arrays */

pr_json_array_t *pr_json_array_alloc(pool *p);

int pr_json_array_free(pr_json_array_t *json);

pr_json_array_t *pr_json_array_from_text(pool *p, const char *text);

const char *pr_json_array_to_text(pool *p, const pr_json_array_t *json,
const char *indent);

/* Returns the number of items in the given array. */
int pr_json_array_count(const pr_json_array_t *json);

/* Removes the array item under this key. */
int pr_json_array_remove(pr_json_array_t *json, unsigned int idx);

/* Checks where an item at the given index exists. Returns TRUE, FALSE,
* or -1 is there was some other error.
*/
int pr_json_array_exists(const pr_json_array_t *json, unsigned int idx);

int pr_json_array_append_bool(pool *p, pr_json_array_t *json, int val);
int pr_json_array_get_bool(pool *p, const pr_json_array_t *json,
unsigned int idx, int *val);

int pr_json_array_append_null(pool *p, pr_json_array_t *json);
int pr_json_array_get_null(pool *p, const pr_json_array_t *json,
unsigned int idx);

int pr_json_array_append_number(pool *p, pr_json_array_t *json, double val);
int pr_json_array_get_number(pool *p, const pr_json_array_t *json,
unsigned int idx, double *val);

int pr_json_array_append_string(pool *p, pr_json_array_t *json,
const char *val);
int pr_json_array_get_string(pool *p, const pr_json_array_t *json,
unsigned int idx, char **val);

int pr_json_array_append_array(pool *p, pr_json_array_t *json,
const pr_json_array_t *val);
int pr_json_array_get_array(pool *p, const pr_json_array_t *json,
unsigned int idx, pr_json_array_t **val);

int pr_json_array_append_object(pool *p, pr_json_array_t *json,
const pr_json_object_t *val);
int pr_json_array_get_object(pool *p, const pr_json_array_t *json,
unsigned int idx, pr_json_object_t **val);

/* Miscellaneous */

/* Validates that the given text is a valid JSON string. */
int pr_json_text_validate(pool *p, const char *text);

/* Internal use only. */
int json_init(void);
int json_free(void);

#endif /* PR_JSON_H */
4 changes: 2 additions & 2 deletions lib/ccan-json.c
Expand Up @@ -472,10 +472,10 @@ int json_validate(const char *json)
return TRUE;
}

JsonNode *json_find_element(JsonNode *array, int idx)
JsonNode *json_find_element(JsonNode *array, unsigned int idx)
{
JsonNode *element;
int i = 0;
unsigned int i = 0;

if (array == NULL || array->tag != JSON_ARRAY)
return NULL;
Expand Down

0 comments on commit fab26d5

Please sign in to comment.