From b1bfb4d5208af907368c89a25b00ddf50ad92207 Mon Sep 17 00:00:00 2001 From: Oliver Jowett Date: Thu, 15 Jan 2015 20:54:22 +0000 Subject: [PATCH] Rationalization of JSON output. Pass actual URL path to URL handler. (cherry picked from commit becce8d69a6e6cee38d4fdb78b5a4e25d570baea) --- dump1090.c | 24 ++++++------------------ dump1090.h | 9 ++++----- net_io.c | 31 +++++++++++++++++++++++-------- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/dump1090.c b/dump1090.c index d904c1c..f584553 100644 --- a/dump1090.c +++ b/dump1090.c @@ -681,6 +681,7 @@ static void display_stats(void) { void backgroundTasks(void) { static time_t next_stats; static time_t next_json; + time_t now = time(NULL); if (Modes.net) { modesNetPeriodicWork(); @@ -697,7 +698,6 @@ void backgroundTasks(void) { } if (Modes.stats > 0) { - time_t now = time(NULL); if (now > next_stats) { if (next_stats != 0) display_stats(); @@ -705,12 +705,9 @@ void backgroundTasks(void) { } } - if (Modes.json_aircraft_path) { - time_t now = time(NULL); - if (now >= next_json) { - writeJsonToFile(Modes.json_aircraft_path, generateAircraftJson); - next_json = now + Modes.json_interval; - } + if ((Modes.json_dir || Modes.net_http_port) && now >= next_json) { + writeJsonToFile("aircraft.json", generateAircraftJson); + next_json = now + Modes.json_interval; } } @@ -907,14 +904,7 @@ int main(int argc, char **argv) { Modes.oversample = 1; #ifndef _WIN32 } else if (!strcmp(argv[j], "--write-json") && more) { - ++j; - Modes.json_aircraft_path = malloc(strlen(argv[j]) + 15); - strcpy(Modes.json_aircraft_path, argv[j]); - strcat(Modes.json_aircraft_path, "/aircraft.json"); - - Modes.json_metadata_path = malloc(strlen(argv[j]) + 15); - strcpy(Modes.json_metadata_path, argv[j]); - strcat(Modes.json_metadata_path, "/receiver.json"); + Modes.json_dir = strdup(argv[++j]); } else if (!strcmp(argv[j], "--write-json-every") && more) { Modes.json_interval = atoi(argv[++j]); if (Modes.json_interval < 1) @@ -972,9 +962,7 @@ int main(int argc, char **argv) { } if (Modes.net) modesInitNet(); - if (Modes.json_metadata_path) { - writeJsonToFile(Modes.json_metadata_path, generateReceiverJson); // once only on startup - } + writeJsonToFile("receiver.json", generateReceiverJson); // once only on startup // If the user specifies --net-only, just run in order to serve network // clients without reading data from the RTL device diff --git a/dump1090.h b/dump1090.h index 49c096a..236cdda 100644 --- a/dump1090.h +++ b/dump1090.h @@ -358,8 +358,7 @@ struct { // Internal state int metric; // Use metric units int mlat; // Use Beast ascii format for raw data output, i.e. @...; iso *...; int interactive_rtl1090; // flight table in interactive mode is formatted like RTL1090 - char *json_aircraft_path; // Path to json aircraft file to write, or NULL not to. - char *json_metadata_path; // Path to json metadata file to write, or NULL not to. + char *json_dir; // Path to json base directory, or NULL not to write json. int json_interval; // Interval between rewriting the json aircraft file int json_location_accuracy; // Accuracy of location metadata: 0=none, 1=approx, 2=exact @@ -485,9 +484,9 @@ void modesQueueOutput (struct modesMessage *mm); void modesReadFromClient(struct client *c, char *sep, int(*handler)(struct client *, char *)); void modesNetPeriodicWork (void); -void writeJsonToFile(const char *path, char * (*generator) (int*)); -char *generateAircraftJson(int *len); -char *generateReceiverJson(int *len); +void writeJsonToFile(const char *file, char * (*generator) (const char*,int*)); +char *generateAircraftJson(const char *url_path, int *len); +char *generateReceiverJson(const char *url_path, int *len); #ifdef __cplusplus } diff --git a/net_io.c b/net_io.c index 43681bb..46af05d 100644 --- a/net_io.c +++ b/net_io.c @@ -667,13 +667,15 @@ int decodeHexMessage(struct client *c, char *hex) { // // Return a description of planes in json. No metric conversion // -char *generateAircraftJson(int *len) { +char *generateAircraftJson(const char *url_path, int *len) { time_t now = time(NULL); struct aircraft *a = Modes.aircrafts; int buflen = 1024; // The initial buffer is incremented as needed char *buf = (char *) malloc(buflen), *p = buf, *end = buf+buflen; int first = 1; + (void) url_path; // unused + p += snprintf(p, end-p, "{ \"now\" : %d,\n" " \"messages\" : %u,\n" @@ -732,10 +734,12 @@ char *generateAircraftJson(int *len) { // // Return a description of the receiver in json. // -char *generateReceiverJson(int *len) +char *generateReceiverJson(const char *url_path, int *len) { char *buf = (char *) malloc(1024), *p = buf; + (void)url_path; // unused + p += sprintf(p, "{ " \ "\"version\" : \"%s\", " "\"refresh\" : %d", @@ -762,16 +766,20 @@ char *generateReceiverJson(int *len) } // Write JSON to file -void writeJsonToFile(const char *path, char * (*generator) (int*)) +void writeJsonToFile(const char *file, char * (*generator) (const char *,int*)) { #ifndef _WIN32 + char pathbuf[PATH_MAX]; char tmppath[PATH_MAX]; int fd; int len = 0; mode_t mask; char *content; - snprintf(tmppath, PATH_MAX, "%s.XXXXXX", path); + if (!Modes.json_dir) + return; + + snprintf(tmppath, PATH_MAX, "%s/%s.XXXXXX", Modes.json_dir, file); tmppath[PATH_MAX-1] = 0; fd = mkstemp(tmppath); if (fd < 0) @@ -781,7 +789,9 @@ void writeJsonToFile(const char *path, char * (*generator) (int*)) umask(mask); fchmod(fd, 0644 & ~mask); - content = generator(&len); + snprintf(pathbuf, PATH_MAX, "/data/%s", file); + pathbuf[PATH_MAX-1] = 0; + content = generator(pathbuf, &len); if (write(fd, content, len) != len) goto error_1; @@ -789,7 +799,9 @@ void writeJsonToFile(const char *path, char * (*generator) (int*)) if (close(fd) < 0) goto error_2; - rename(tmppath, path); + snprintf(pathbuf, PATH_MAX, "%s/%s", Modes.json_dir, file); + pathbuf[PATH_MAX-1] = 0; + rename(tmppath, pathbuf); free(content); return; @@ -814,7 +826,7 @@ void writeJsonToFile(const char *path, char * (*generator) (int*)) static struct { char *path; - char * (*handler)(int*); + char * (*handler)(const char*,int*); char *content_type; } url_handlers[] = { { "/data/aircraft.json", generateAircraftJson, MODES_CONTENT_TYPE_JSON }, @@ -877,7 +889,10 @@ int handleHTTPRequest(struct client *c, char *p) { for (i = 0; url_handlers[i].path; ++i) { if (!strcmp(url, url_handlers[i].path)) { content_type = url_handlers[i].content_type; - content = url_handlers[i].handler(&clen); + content = url_handlers[i].handler(url, &clen); + if (!content) + continue; + statuscode = 200; statusmsg = "OK"; if (Modes.debug & MODES_DEBUG_NET) {