Skip to content

Commit

Permalink
Add API_PRETTY_JSON config option as run-time (instead of compile-tim…
Browse files Browse the repository at this point in the history
…e) option for human-friendy API output.

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Dec 3, 2019
1 parent 2710e22 commit 73072e4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
25 changes: 25 additions & 0 deletions src/api/http-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@
// Server context handle
static struct mg_context *ctx = NULL;

// Provides a compile-time flag for JSON formatting
// This should never be needed as all modern browsers
// tyoically contain a JSON explorer
const char* json_formatter(const cJSON *object)
{
if(httpsettings.prettyJSON)
{
/* Examplary output:
{
"queries in database": 70,
"database filesize": 49152,
"SQLite version": "3.30.1"
}
*/
return cJSON_Print(object);
}
else
{
/* Exemplary output
{"queries in database":70,"database filesize":49152,"SQLite version":"3.30.1"}
*/
return cJSON_PrintUnformatted(object);
}
}

int send_http(struct mg_connection *conn, const char *mime_type,
const char *msg)
{
Expand Down
2 changes: 2 additions & 0 deletions src/api/http-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
void http_init(void);
void http_terminate(void);

const char* json_formatter(const cJSON *object);

int send_http(struct mg_connection *conn, const char *mime_type, const char *msg);
int send_http_code(struct mg_connection *conn, const char *mime_type, int code, const char *msg);
int send_http_internal_error(struct mg_connection *conn);
Expand Down
27 changes: 4 additions & 23 deletions src/api/json_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,6 @@

#include "../cJSON/cJSON.h"

// Provides a compile-time flag for JSON formatting
// This should never be needed as all modern browsers
// tyoically contain a JSON explorer
#ifdef JSON_FORMATTED
/* Examplary output:
{
"queries in database": 70,
"database filesize": 49152,
"SQLite version": "3.30.1"
}
*/
#define JSON_FORMATTER(object) cJSON_Print(object)
#else
/* Exemplary output
{"queries in database":70,"database filesize":49152,"SQLite version":"3.30.1"}
*/
#define JSON_FORMATTER(object) cJSON_PrintUnformatted(object)
#endif

#define JSON_NEW_OBJ() cJSON_CreateObject();
#define JSON_NEW_ARRAY() cJSON_CreateArray();

Expand Down Expand Up @@ -122,7 +103,7 @@
#define JSON_DELETE(object) cJSON_Delete(object)

#define JSON_SEND_OBJECT(object){ \
const char* msg = JSON_FORMATTER(object); \
const char* msg = json_formatter(object); \
if(msg == NULL) \
{ \
cJSON_Delete(object); \
Expand All @@ -135,7 +116,7 @@
}

#define JSON_SEND_OBJECT_CODE(object, code){ \
const char* msg = JSON_FORMATTER(object); \
const char* msg = json_formatter(object); \
if(msg == NULL) \
{ \
cJSON_Delete(object); \
Expand All @@ -148,7 +129,7 @@
}
/*
#define JSON_SEND_OBJECT_AND_HEADERS(object, additional_headers){ \
const char* msg = JSON_FORMATTER(object); \
const char* msg = json_formatter(object); \
if(msg == NULL) \
{ \
cJSON_Delete(object); \
Expand All @@ -162,7 +143,7 @@
}
#define JSON_SEND_OBJECT_AND_HEADERS_CODE(object, code, additional_headers){ \
const char* msg = JSON_FORMATTER(object); \
const char* msg = json_formatter(object); \
if(msg == NULL) \
{ \
cJSON_Delete(object); \
Expand Down
19 changes: 16 additions & 3 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,18 +395,31 @@ void read_FTLconf(void)
else
logg(" API_AUTH_FOR_LOCALHOST: Inactive");

// HTTP_SESSION_TIMEOUT
// API_SESSION_TIMEOUT
// How long should a session be considered valid after login?
// defaults to: 300 seconds
httpsettings.session_timeout = 300;
buffer = parse_FTLconf(fp, "HTTP_SESSION_TIMEOUT");
buffer = parse_FTLconf(fp, "API_SESSION_TIMEOUT");

value = 0;
if(buffer != NULL && sscanf(buffer, "%i", &value) && value > 0)
{
httpsettings.session_timeout = value;
}
logg(" HTTP_SESSION_TIMEOUT: %u seconds", httpsettings.session_timeout);
logg(" API_SESSION_TIMEOUT: %u seconds", httpsettings.session_timeout);

// API_PRETTY_JSON
// defaults to: false
httpsettings.prettyJSON = false;
buffer = parse_FTLconf(fp, "API_PRETTY_JSON");

if(buffer != NULL && strcasecmp(buffer, "true") == 0)
httpsettings.prettyJSON = true;

if(httpsettings.prettyJSON)
logg(" API_PRETTY_JSON: Enabled. Using additional formatting in API output.");
else
logg(" API_PRETTY_JSON: Disabled. Compact API output.");

// Read DEBUG_... setting from pihole-FTL.conf
// This option should be the last one as it causes
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ typedef struct httpsettings {
char *webhome;
const char *acl;
bool api_auth_for_localhost;
bool prettyJSON;
char port[20]; // enough space for 2*(maximum length of number in a uint16_t = 5 characters) + ",[::]:" + NULL
unsigned int session_timeout;
} httpsettingsStruct;
Expand Down

0 comments on commit 73072e4

Please sign in to comment.