gluon-status-page-api: use sse-multiplex

This commit is contained in:
Matthias Schiffer 2015-12-13 14:46:27 +01:00
parent fa504dae8d
commit 90a64ca87f
7 changed files with 41 additions and 34 deletions

View File

@ -12,7 +12,7 @@ define Package/gluon-status-page-api
SECTION:=gluon
CATEGORY:=Gluon
TITLE:=API for gluon-status-page
DEPENDS:=+gluon-core +uhttpd +gluon-neighbour-info +gluon-announced +libiwinfo +libjson-c
DEPENDS:=+gluon-core +uhttpd +sse-multiplex +gluon-neighbour-info +gluon-announced +libiwinfo +libjson-c
endef
define Build/Prepare
@ -21,9 +21,9 @@ define Build/Prepare
endef
define Package/gluon-status-page-api/install
$(INSTALL_DIR) $(1)/lib/gluon/status-page/www/cgi-bin/dyn
$(INSTALL_BIN) $(PKG_BUILD_DIR)/neighbours-batadv $(1)/lib/gluon/status-page/www/cgi-bin/dyn/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations $(1)/lib/gluon/status-page/www/cgi-bin/dyn/
$(INSTALL_DIR) $(1)/lib/gluon/status-page/providers
$(INSTALL_BIN) $(PKG_BUILD_DIR)/neighbours-batadv $(1)/lib/gluon/status-page/providers/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/stations $(1)/lib/gluon/status-page/providers/
$(CP) ./files/* $(1)/
endef

View File

@ -0,0 +1,7 @@
#!/bin/sh
echo 'Access-Control-Allow-Origin: *'
CMD='exec /lib/gluon/status-page/providers/neighbours-batadv'
exec /usr/sbin/sse-multiplex "$CMD"

View File

@ -1,7 +1,13 @@
#!/bin/sh
badrequest() {
echo 'Status: 400 Bad Request'
echo
exit 1
}
echo 'Access-Control-Allow-Origin: *'
batctl if | cut -d: -f1 | grep -qxF "$QUERY_STRING" || exit 1
batctl if | cut -d: -f1 | grep -qxF "$QUERY_STRING" || badrequest
exec /usr/bin/gluon-neighbour-info -s neighbour -i "$QUERY_STRING" -d ff02::2:1001 -p 1001 -r nodeinfo

View File

@ -0,0 +1,15 @@
#!/bin/sh
badrequest() {
echo 'Status: 400 Bad Request'
echo
exit 1
}
echo 'Access-Control-Allow-Origin: *'
batctl if | cut -d: -f1 | grep -qxF "$QUERY_STRING" || badrequest
CMD="exec /lib/gluon/status-page/providers/stations '$QUERY_STRING'"
exec /usr/sbin/sse-multiplex "$CMD"

View File

@ -1,5 +1,7 @@
#!/bin/sh
echo "Access-Control-Allow-Origin: *"
echo 'Access-Control-Allow-Origin: *'
exec gluon-neighbour-info -s "" -l -d ::1 -p 1001 -t 3 -r statistics
CMD='exec gluon-neighbour-info -s "" -l -d ::1 -p 1001 -t 3 -r statistics'
exec /usr/sbin/sse-multiplex "$CMD"

2
package/gluon-status-page-api/src/neighbours-batadv.c Executable file → Normal file
View File

@ -50,8 +50,8 @@ static json_object *neighbours(void) {
int main(void) {
struct json_object *obj;
printf("Access-Control-Allow-Origin: *\n");
printf("Content-type: text/event-stream\n\n");
fflush(stdout);
while (1) {
obj = neighbours();

29
package/gluon-status-page-api/src/stations.c Executable file → Normal file
View File

@ -1,15 +1,10 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <json-c/json.h>
#include <iwinfo.h>
#include <net/if.h>
#define STR(x) #x
#define XSTR(x) STR(x)
#define BATIF_PREFIX "/sys/class/net/bat0/lower_"
static struct json_object *get_stations(const struct iwinfo_ops *iw, const char *ifname) {
int len;
@ -45,34 +40,16 @@ static void badrequest() {
exit(1);
}
bool interface_is_valid(const char *ifname) {
if (strlen(ifname) > IF_NAMESIZE)
return false;
if (strchr(ifname, '/') != NULL)
return false;
char *path = alloca(1 + strlen(BATIF_PREFIX) + strlen(ifname));
sprintf(path, "%s%s", BATIF_PREFIX, ifname);
return access(path, F_OK) == 0;
}
int main(void) {
char *ifname = getenv("QUERY_STRING");
if (ifname == NULL)
badrequest();
if (!interface_is_valid(ifname))
int main(int argc, char *argv[]) {
if (argc != 2)
badrequest();
const char *ifname = argv[1];
const struct iwinfo_ops *iw = iwinfo_backend(ifname);
if (iw == NULL)
badrequest();
printf("Access-Control-Allow-Origin: *\n");
printf("Content-type: text/event-stream\n\n");
while (true) {