mirror of
https://github.com/owntone/owntone-server.git
synced 2025-01-15 16:53:18 -05:00
[main/httpd] Configurable web root directory over cli parameter
This commit is contained in:
parent
8776aa36e2
commit
7083c65314
26
src/httpd.c
26
src/httpd.c
@ -72,8 +72,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define WEB_ROOT DATADIR "/htdocs"
|
|
||||||
|
|
||||||
#define STREAM_CHUNK_SIZE (64 * 1024)
|
#define STREAM_CHUNK_SIZE (64 * 1024)
|
||||||
#define ERR_PAGE "<html>\n<head>\n" \
|
#define ERR_PAGE "<html>\n<head>\n" \
|
||||||
"<title>%d %s</title>\n" \
|
"<title>%d %s</title>\n" \
|
||||||
@ -117,6 +115,7 @@ static const struct content_type_map ext2ctype[] =
|
|||||||
|
|
||||||
static const char *http_reply_401 = "<html><head><title>401 Unauthorized</title></head><body>Authorization required</body></html>";
|
static const char *http_reply_401 = "<html><head><title>401 Unauthorized</title></head><body>Authorization required</body></html>";
|
||||||
|
|
||||||
|
static const char *webroot_directory;
|
||||||
struct event_base *evbase_httpd;
|
struct event_base *evbase_httpd;
|
||||||
|
|
||||||
#ifdef HAVE_EVENTFD
|
#ifdef HAVE_EVENTFD
|
||||||
@ -142,7 +141,7 @@ struct stream_ctx *g_st;
|
|||||||
static int
|
static int
|
||||||
path_is_legal(const char *path)
|
path_is_legal(const char *path)
|
||||||
{
|
{
|
||||||
return strncmp(WEB_ROOT, path, strlen(WEB_ROOT));
|
return strncmp(webroot_directory, path, strlen(webroot_directory));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Callback from the worker thread (async operation as it may block) */
|
/* Callback from the worker thread (async operation as it may block) */
|
||||||
@ -279,7 +278,7 @@ serve_file(struct evhttp_request *req, const char *uri)
|
|||||||
if (!httpd_admin_check_auth(req))
|
if (!httpd_admin_check_auth(req))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ret = snprintf(path, sizeof(path), "%s%s", WEB_ROOT, uri);
|
ret = snprintf(path, sizeof(path), "%s%s", webroot_directory, uri);
|
||||||
if ((ret < 0) || (ret >= sizeof(path)))
|
if ((ret < 0) || (ret >= sizeof(path)))
|
||||||
{
|
{
|
||||||
DPRINTF(E_LOG, L_HTTPD, "Request exceeds PATH_MAX: %s\n", uri);
|
DPRINTF(E_LOG, L_HTTPD, "Request exceeds PATH_MAX: %s\n", uri);
|
||||||
@ -1555,13 +1554,30 @@ httpd_basic_auth(struct evhttp_request *req, const char *user, const char *passw
|
|||||||
|
|
||||||
/* Thread: main */
|
/* Thread: main */
|
||||||
int
|
int
|
||||||
httpd_init(void)
|
httpd_init(const char *webroot)
|
||||||
{
|
{
|
||||||
|
struct stat sb;
|
||||||
int v6enabled;
|
int v6enabled;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
httpd_exit = 0;
|
httpd_exit = 0;
|
||||||
|
|
||||||
|
DPRINTF(E_DBG, L_HTTPD, "Starting web server with root directory '%s'\n", webroot);
|
||||||
|
ret = lstat(webroot, &sb);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_HTTPD, "Could not lstat() web root directory '%s': %s\n", webroot, strerror(errno));
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!S_ISDIR(sb.st_mode))
|
||||||
|
{
|
||||||
|
DPRINTF(E_LOG, L_HTTPD, "Web root directory '%s' is not a directory\n", webroot);
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
webroot_directory = webroot;
|
||||||
|
|
||||||
evbase_httpd = event_base_new();
|
evbase_httpd = event_base_new();
|
||||||
if (!evbase_httpd)
|
if (!evbase_httpd)
|
||||||
{
|
{
|
||||||
|
@ -157,7 +157,7 @@ int
|
|||||||
httpd_basic_auth(struct evhttp_request *req, const char *user, const char *passwd, const char *realm);
|
httpd_basic_auth(struct evhttp_request *req, const char *user, const char *passwd, const char *realm);
|
||||||
|
|
||||||
int
|
int
|
||||||
httpd_init(void);
|
httpd_init(const char *webroot);
|
||||||
|
|
||||||
void
|
void
|
||||||
httpd_deinit(void);
|
httpd_deinit(void);
|
||||||
|
13
src/main.c
13
src/main.c
@ -79,6 +79,7 @@ GCRY_THREAD_OPTION_PTHREAD_IMPL;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define PIDFILE STATEDIR "/run/" PACKAGE ".pid"
|
#define PIDFILE STATEDIR "/run/" PACKAGE ".pid"
|
||||||
|
#define WEB_ROOT DATADIR "/htdocs"
|
||||||
|
|
||||||
struct event_base *evbase_main;
|
struct event_base *evbase_main;
|
||||||
|
|
||||||
@ -108,6 +109,7 @@ usage(char *program)
|
|||||||
printf(" -f Run in foreground\n");
|
printf(" -f Run in foreground\n");
|
||||||
printf(" -b <id> ffid to be broadcast\n");
|
printf(" -b <id> ffid to be broadcast\n");
|
||||||
printf(" -v Display version information\n");
|
printf(" -v Display version information\n");
|
||||||
|
printf(" -w <directory> Use <directory> as the web root directory for serving static files\n");
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
printf("Available log domains:\n");
|
printf("Available log domains:\n");
|
||||||
logger_domains();
|
logger_domains();
|
||||||
@ -469,6 +471,7 @@ main(int argc, char **argv)
|
|||||||
char *logfile;
|
char *logfile;
|
||||||
char *ffid;
|
char *ffid;
|
||||||
char *pidfile;
|
char *pidfile;
|
||||||
|
char *webroot;
|
||||||
char **buildopts;
|
char **buildopts;
|
||||||
const char *av_version;
|
const char *av_version;
|
||||||
const char *gcry_version;
|
const char *gcry_version;
|
||||||
@ -489,6 +492,7 @@ main(int argc, char **argv)
|
|||||||
{ "config", 1, NULL, 'c' },
|
{ "config", 1, NULL, 'c' },
|
||||||
{ "pidfile", 1, NULL, 'P' },
|
{ "pidfile", 1, NULL, 'P' },
|
||||||
{ "version", 0, NULL, 'v' },
|
{ "version", 0, NULL, 'v' },
|
||||||
|
{ "webroot", 1, NULL, 'w' },
|
||||||
|
|
||||||
{ "mdns-no-rsp", 0, NULL, 512 },
|
{ "mdns-no-rsp", 0, NULL, 512 },
|
||||||
{ "mdns-no-daap", 0, NULL, 513 },
|
{ "mdns-no-daap", 0, NULL, 513 },
|
||||||
@ -498,6 +502,7 @@ main(int argc, char **argv)
|
|||||||
|
|
||||||
configfile = CONFFILE;
|
configfile = CONFFILE;
|
||||||
pidfile = PIDFILE;
|
pidfile = PIDFILE;
|
||||||
|
webroot = WEB_ROOT;
|
||||||
loglevel = -1;
|
loglevel = -1;
|
||||||
logdomains = NULL;
|
logdomains = NULL;
|
||||||
logfile = NULL;
|
logfile = NULL;
|
||||||
@ -506,7 +511,7 @@ main(int argc, char **argv)
|
|||||||
mdns_no_rsp = 0;
|
mdns_no_rsp = 0;
|
||||||
mdns_no_daap = 0;
|
mdns_no_daap = 0;
|
||||||
|
|
||||||
while ((option = getopt_long(argc, argv, "D:d:c:P:fb:v", option_map, NULL)) != -1)
|
while ((option = getopt_long(argc, argv, "D:d:c:P:fb:vw:", option_map, NULL)) != -1)
|
||||||
{
|
{
|
||||||
switch (option)
|
switch (option)
|
||||||
{
|
{
|
||||||
@ -551,6 +556,10 @@ main(int argc, char **argv)
|
|||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'w':
|
||||||
|
webroot = optarg;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
usage(argv[0]);
|
usage(argv[0]);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
@ -763,7 +772,7 @@ main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Spawn HTTPd thread */
|
/* Spawn HTTPd thread */
|
||||||
ret = httpd_init();
|
ret = httpd_init(webroot);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
{
|
{
|
||||||
DPRINTF(E_FATAL, L_MAIN, "HTTPd thread failed to start\n");
|
DPRINTF(E_FATAL, L_MAIN, "HTTPd thread failed to start\n");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user