From d9cffb269f9db5283071392f5038717b543e92db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Dejean?= Date: Mon, 9 Jul 2018 20:08:12 +0200 Subject: [PATCH] Bundle gmap.html directly into the code to make dump1090 independant of any external file. --- Makefile | 8 ++++++-- dump1090.c | 40 +++++++++++++++------------------------- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index f9637b7eb..1cffae02f 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,15 @@ PROGNAME=dump1090 all: dump1090 +gmap.c: gmap.html + xxd -i $^ > $@ + sed -i "s/^unsigned/const unsigned/" $@ + %.o: %.c $(CC) $(CFLAGS) -c $< -dump1090: dump1090.o anet.o - $(CC) -g -o dump1090 dump1090.o anet.o $(LDFLAGS) $(LDLIBS) +dump1090: dump1090.o anet.o gmap.o + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS) clean: rm -f *.o dump1090 diff --git a/dump1090.c b/dump1090.c index 7fa5327a3..419b4f7ab 100644 --- a/dump1090.c +++ b/dump1090.c @@ -46,6 +46,9 @@ #include "rtl-sdr.h" #include "anet.h" +extern const unsigned char gmap_html[]; +extern const unsigned gmap_html_len; + #define MODES_DEFAULT_RATE 2000000 #define MODES_DEFAULT_FREQ 1090000000 #define MODES_DEFAULT_WIDTH 1000 @@ -2230,26 +2233,8 @@ int handleHTTPRequest(struct client *c) { content = aircraftsToJson(&clen); ctype = MODES_CONTENT_TYPE_JSON; } else { - struct stat sbuf; - int fd = -1; - - if (stat("gmap.html",&sbuf) != -1 && - (fd = open("gmap.html",O_RDONLY)) != -1) - { - content = malloc(sbuf.st_size); - if (read(fd,content,sbuf.st_size) == -1) { - snprintf(content,sbuf.st_size,"Error reading from file: %s", - strerror(errno)); - } - clen = sbuf.st_size; - } else { - char buf[128]; - - clen = snprintf(buf,sizeof(buf),"Error opening HTML file: %s", - strerror(errno)); - content = strdup(buf); - } - if (fd != -1) close(fd); + content = (char*) gmap_html; + clen = gmap_html_len; ctype = MODES_CONTENT_TYPE_HTML; } @@ -2269,16 +2254,21 @@ int handleHTTPRequest(struct client *c) { if (Modes.debug & MODES_DEBUG_NET) printf("HTTP Reply header:\n%s", hdr); + int ret; /* Send header and content. */ if (write(c->fd, hdr, hdrlen) != hdrlen || write(c->fd, content, clen) != clen) { - free(content); - return 1; + ret = 1; + } else { + Modes.stat_http_requests++; + ret = !keepalive; } - free(content); - Modes.stat_http_requests++; - return !keepalive; + + if (content != (char*)gmap_html) + free(content); + + return ret; } /* This function polls the clients using read() in order to receive new