From dd379084c02367ed4171476ac9d98262b4eb85b8 Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Thu, 20 Dec 2012 21:38:20 +0000 Subject: [PATCH] Fix #1427 - webui: Added reverse proxy support. This includes a next webroot command line argument (using -W) and some minor mods to the core HTTP/WebUI code to support this. Most of the mods are pretty trivial and hopefully nothing will break too badly. --- src/http.c | 14 ++++++++++++-- src/main.c | 7 ++++++- src/tvheadend.h | 1 + src/webui/static/app/tvheadend.js | 2 +- src/webui/webui.c | 16 ++++++++++++++-- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/http.c b/src/http.c index 07c88fc6..7610adf1 100644 --- a/src/http.c +++ b/src/http.c @@ -608,8 +608,18 @@ http_path_add(const char *path, void *opaque, http_callback_t *callback, { http_path_t *hp = malloc(sizeof(http_path_t)); - hp->hp_len = strlen(path); - hp->hp_path = strdup(path); + if (tvheadend_webroot) { + char *tmp; const char *pre = ""; + size_t len = strlen(tvheadend_webroot) + strlen(path) + 1; + if (*tvheadend_webroot != '/') { + len++; + pre = "/"; + } + hp->hp_path = tmp = malloc(len); + sprintf(tmp, "%s%s%s", pre, tvheadend_webroot, path); + } else + hp->hp_path = strdup(path); + hp->hp_len = strlen(hp->hp_path); hp->hp_opaque = opaque; hp->hp_callback = callback; hp->hp_accessmask = accessmask; diff --git a/src/main.c b/src/main.c index 564e592c..baae957c 100644 --- a/src/main.c +++ b/src/main.c @@ -77,6 +77,7 @@ int webui_port; int htsp_port; int htsp_port_extra; char *tvheadend_cwd; +const char *tvheadend_webroot; const char *tvheadend_capabilities[] = { #if ENABLE_CWC @@ -202,6 +203,7 @@ usage(const char *argv0) printf(" -s Log debug to syslog\n"); printf(" -w WebUI access port [default 9981]\n"); printf(" -e HTSP access port [default 9982]\n"); + printf(" -W WebUI context path [default /]\n"); printf("\n"); printf("Development options\n"); printf("\n"); @@ -297,7 +299,7 @@ main(int argc, char **argv) // make sure the timezone is set tzset(); - while((c = getopt(argc, argv, "Aa:fp:u:g:c:Chdr:j:sw:e:E:R:")) != -1) { + while((c = getopt(argc, argv, "Aa:fp:u:g:c:Chdr:j:sw:e:E:R:W:")) != -1) { switch(c) { case 'a': adapter_mask = 0x0; @@ -366,6 +368,9 @@ main(int argc, char **argv) case 'j': join_transport = optarg; break; + case 'W': + tvheadend_webroot = optarg; + break; default: usage(argv[0]); } diff --git a/src/tvheadend.h b/src/tvheadend.h index eb317b9c..8ad66cd0 100644 --- a/src/tvheadend.h +++ b/src/tvheadend.h @@ -37,6 +37,7 @@ extern const char *tvheadend_version; extern char *tvheadend_cwd; extern const char *tvheadend_capabilities[]; +extern const char *tvheadend_webroot; #define PTS_UNSET INT64_C(0x8000000000000000) diff --git a/src/webui/static/app/tvheadend.js b/src/webui/static/app/tvheadend.js index 2a1af2b7..d1754f49 100644 --- a/src/webui/static/app/tvheadend.js +++ b/src/webui/static/app/tvheadend.js @@ -34,7 +34,7 @@ tvheadend.help = function(title, pagename) { * General capabilities */ Ext.Ajax.request({ - url: '/capabilities', + url: 'capabilities', success: function(d) { if (d && d.responseText) diff --git a/src/webui/webui.c b/src/webui/webui.c index 170127cf..fbd0416f 100644 --- a/src/webui/webui.c +++ b/src/webui/webui.c @@ -74,13 +74,24 @@ static int page_root(http_connection_t *hc, const char *remain, void *opaque) { if(is_client_simple(hc)) { - http_redirect(hc, "/simple.html"); + http_redirect(hc, "simple.html"); } else { - http_redirect(hc, "/extjs.html"); + http_redirect(hc, "extjs.html"); } return 0; } +static int +page_root2(http_connection_t *hc, const char *remain, void *opaque) +{ + if (!tvheadend_webroot) return 1; + char *tmp = malloc(strlen(tvheadend_webroot) + 2); + sprintf(tmp, "%s/", tvheadend_webroot); + http_redirect(hc, tmp); + free(tmp); + return 0; +} + /** * Static download of a file from the filesystem */ @@ -922,6 +933,7 @@ int page_statedump(http_connection_t *hc, const char *remain, void *opaque); void webui_init(void) { + http_path_add("", NULL, page_root2, ACCESS_WEB_INTERFACE); http_path_add("/", NULL, page_root, ACCESS_WEB_INTERFACE); http_path_add("/dvrfile", NULL, page_dvrfile, ACCESS_WEB_INTERFACE);