2009-04-22 15:25:41 -04:00
|
|
|
/*
|
2010-01-05 13:34:00 -05:00
|
|
|
* Copyright (C) 2009-2010 Julien BLACHE <jb@jblache.org>
|
2009-04-22 15:25:41 -04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
2009-04-25 04:40:47 -04:00
|
|
|
#include <time.h>
|
2010-01-09 07:42:23 -05:00
|
|
|
#include <sys/param.h>
|
2010-01-09 07:41:51 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2010-02-02 15:02:24 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <inttypes.h>
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2023-08-29 17:50:19 -04:00
|
|
|
#ifdef HAVE_SYS_GETTID
|
2023-01-26 15:00:40 -05:00
|
|
|
#include <sys/syscall.h> // get thread ID
|
|
|
|
#endif
|
2022-12-21 13:11:03 -05:00
|
|
|
|
2015-10-19 15:15:29 -04:00
|
|
|
#include <event2/event.h>
|
2022-12-18 07:08:29 -05:00
|
|
|
|
|
|
|
#include <regex.h>
|
2010-05-03 12:19:41 -04:00
|
|
|
#include <zlib.h>
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2009-05-08 11:46:32 -04:00
|
|
|
#include "logger.h"
|
2009-06-07 12:58:02 -04:00
|
|
|
#include "db.h"
|
2009-04-22 15:25:41 -04:00
|
|
|
#include "conffile.h"
|
2009-04-30 08:25:52 -04:00
|
|
|
#include "misc.h"
|
2016-09-02 15:52:45 -04:00
|
|
|
#include "worker.h"
|
2023-01-21 18:33:54 -05:00
|
|
|
#include "evthr.h"
|
2009-04-22 15:25:41 -04:00
|
|
|
#include "httpd.h"
|
2022-12-20 16:59:36 -05:00
|
|
|
#include "httpd_internal.h"
|
2009-05-01 09:33:48 -04:00
|
|
|
#include "transcode.h"
|
2016-09-02 15:52:45 -04:00
|
|
|
#ifdef LASTFM
|
|
|
|
# include "lastfm.h"
|
|
|
|
#endif
|
2017-09-15 18:05:50 -04:00
|
|
|
#ifdef HAVE_LIBWEBSOCKETS
|
2017-08-12 14:01:57 -04:00
|
|
|
# include "websocket.h"
|
|
|
|
#endif
|
|
|
|
|
2010-02-02 15:06:13 -05:00
|
|
|
#define STREAM_CHUNK_SIZE (64 * 1024)
|
2016-10-17 17:08:02 -04:00
|
|
|
#define ERR_PAGE "<html>\n<head>\n" \
|
|
|
|
"<title>%d %s</title>\n" \
|
|
|
|
"</head>\n<body>\n" \
|
|
|
|
"<h1>%s</h1>\n" \
|
|
|
|
"</body>\n</html>\n"
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2019-04-02 16:47:11 -04:00
|
|
|
#define HTTPD_STREAM_SAMPLE_RATE 44100
|
|
|
|
#define HTTPD_STREAM_BPS 16
|
|
|
|
#define HTTPD_STREAM_CHANNELS 2
|
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
extern struct httpd_module httpd_dacp;
|
|
|
|
extern struct httpd_module httpd_daap;
|
|
|
|
extern struct httpd_module httpd_jsonapi;
|
|
|
|
extern struct httpd_module httpd_artworkapi;
|
|
|
|
extern struct httpd_module httpd_streaming;
|
|
|
|
extern struct httpd_module httpd_oauth;
|
|
|
|
extern struct httpd_module httpd_rsp;
|
|
|
|
|
|
|
|
// Must be in sync with enum httpd_modules
|
|
|
|
static struct httpd_module *httpd_modules[] = {
|
|
|
|
&httpd_dacp,
|
|
|
|
&httpd_daap,
|
|
|
|
&httpd_jsonapi,
|
|
|
|
&httpd_artworkapi,
|
|
|
|
&httpd_streaming,
|
|
|
|
&httpd_oauth,
|
|
|
|
&httpd_rsp,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2019-04-02 16:47:11 -04:00
|
|
|
|
2009-04-22 15:25:41 -04:00
|
|
|
struct content_type_map {
|
|
|
|
char *ext;
|
|
|
|
char *ctype;
|
|
|
|
};
|
|
|
|
|
2009-05-03 06:19:04 -04:00
|
|
|
struct stream_ctx {
|
2022-12-21 13:11:03 -05:00
|
|
|
struct httpd_request *hreq;
|
2015-10-19 15:15:29 -04:00
|
|
|
struct event *ev;
|
2009-04-25 04:40:47 -04:00
|
|
|
int id;
|
|
|
|
int fd;
|
2010-02-02 15:02:24 -05:00
|
|
|
off_t size;
|
|
|
|
off_t stream_size;
|
|
|
|
off_t offset;
|
|
|
|
off_t start_offset;
|
|
|
|
off_t end_offset;
|
2009-04-25 04:40:47 -04:00
|
|
|
int marked;
|
2009-05-01 09:33:48 -04:00
|
|
|
struct transcode_ctx *xcode;
|
2009-04-25 04:40:47 -04:00
|
|
|
};
|
|
|
|
|
2010-03-23 13:55:39 -04:00
|
|
|
static const struct content_type_map ext2ctype[] =
|
2009-04-22 15:25:41 -04:00
|
|
|
{
|
|
|
|
{ ".html", "text/html; charset=utf-8" },
|
|
|
|
{ ".xml", "text/xml; charset=utf-8" },
|
|
|
|
{ ".css", "text/css; charset=utf-8" },
|
|
|
|
{ ".txt", "text/plain; charset=utf-8" },
|
|
|
|
{ ".js", "application/javascript; charset=utf-8" },
|
|
|
|
{ ".gif", "image/gif" },
|
|
|
|
{ ".ico", "image/x-ico" },
|
|
|
|
{ ".png", "image/png" },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2021-07-04 05:41:39 -04:00
|
|
|
static char webroot_directory[PATH_MAX];
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
static const char *httpd_allow_origin;
|
2016-11-05 08:38:15 -04:00
|
|
|
static int httpd_port;
|
2016-10-17 16:03:32 -04:00
|
|
|
|
2009-12-08 13:53:53 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
// The server is designed around a single thread listening for requests. When
|
|
|
|
// received, the request is passed to a thread from the worker pool, where a
|
|
|
|
// handler will process it and prepare a response for the httpd thread to send
|
|
|
|
// back. The idea is that the httpd thread never blocks. The handler in the
|
|
|
|
// worker thread can block, but shouldn't hold the thread if it is a long-
|
|
|
|
// running request (e.g. a long poll), because then we can run out of worker
|
|
|
|
// threads. The handler should use events to avoid this. Handlers, that are non-
|
|
|
|
// blocking and where the response must not be delayed can use
|
|
|
|
// HTTPD_HANDLER_REALTIME, then the httpd thread calls it directly (sync)
|
|
|
|
// instead of the async worker. In short, you shouldn't need to increase the
|
|
|
|
// below.
|
|
|
|
#define THREADPOOL_NTHREADS 1
|
2023-01-17 11:14:04 -05:00
|
|
|
|
|
|
|
static struct evthr_pool *httpd_threadpool;
|
|
|
|
|
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
/* -------------------------------- HELPERS --------------------------------- */
|
2014-05-30 17:39:03 -04:00
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
static int
|
|
|
|
path_is_legal(const char *path)
|
|
|
|
{
|
2017-12-03 03:21:17 -05:00
|
|
|
return strncmp(webroot_directory, path, strlen(webroot_directory));
|
2009-05-02 14:42:51 -04:00
|
|
|
}
|
|
|
|
|
2016-09-02 15:52:45 -04:00
|
|
|
/* Callback from the worker thread (async operation as it may block) */
|
2010-09-08 13:37:41 -04:00
|
|
|
static void
|
2016-09-02 15:52:45 -04:00
|
|
|
playcount_inc_cb(void *arg)
|
|
|
|
{
|
|
|
|
int *id = arg;
|
|
|
|
|
|
|
|
db_file_inc_playcount(*id);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LASTFM
|
|
|
|
/* Callback from the worker thread (async operation as it may block) */
|
|
|
|
static void
|
|
|
|
scrobble_cb(void *arg)
|
|
|
|
{
|
|
|
|
int *id = arg;
|
|
|
|
|
|
|
|
lastfm_scrobble(*id);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
/* --------------------------- MODULES INTERFACE ---------------------------- */
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
static void
|
|
|
|
modules_handlers_unset(struct httpd_uri_map *uri_map)
|
|
|
|
{
|
|
|
|
struct httpd_uri_map *uri;
|
2016-11-05 08:38:15 -04:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
for (uri = uri_map; uri->preg; uri++)
|
|
|
|
{
|
|
|
|
regfree(uri->preg); // Frees allocation by regcomp
|
|
|
|
free(uri->preg); // Frees our own calloc
|
|
|
|
}
|
|
|
|
}
|
2016-11-05 08:38:15 -04:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
static int
|
|
|
|
modules_handlers_set(struct httpd_uri_map *uri_map)
|
|
|
|
{
|
|
|
|
struct httpd_uri_map *uri;
|
|
|
|
char buf[64];
|
|
|
|
int ret;
|
2016-11-13 14:28:29 -05:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
for (uri = uri_map; uri->handler; uri++)
|
2016-11-05 08:38:15 -04:00
|
|
|
{
|
2022-12-20 16:59:36 -05:00
|
|
|
uri->preg = calloc(1, sizeof(regex_t));
|
|
|
|
if (!uri->preg)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Error setting URI handler, out of memory");
|
|
|
|
goto error;
|
|
|
|
}
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
ret = regcomp(uri->preg, uri->regexp, REG_EXTENDED | REG_NOSUB);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
regerror(ret, uri->preg, buf, sizeof(buf));
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Error setting URI handler, regexp error: %s\n", buf);
|
|
|
|
goto error;
|
|
|
|
}
|
2016-11-05 08:38:15 -04:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
return 0;
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
error:
|
|
|
|
modules_handlers_unset(uri_map);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-01-17 11:14:04 -05:00
|
|
|
modules_init(void)
|
2022-12-20 16:59:36 -05:00
|
|
|
{
|
|
|
|
struct httpd_module **ptr;
|
|
|
|
struct httpd_module *m;
|
2016-11-05 08:38:15 -04:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
for (ptr = httpd_modules; *ptr; ptr++)
|
2017-08-28 15:14:35 -04:00
|
|
|
{
|
2022-12-20 16:59:36 -05:00
|
|
|
m = *ptr;
|
2023-01-17 11:14:04 -05:00
|
|
|
m->initialized = (!m->init || m->init() == 0);
|
2022-12-20 16:59:36 -05:00
|
|
|
if (!m->initialized)
|
2017-08-28 15:14:35 -04:00
|
|
|
{
|
2022-12-20 16:59:36 -05:00
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "%s init failed\n", m->name);
|
|
|
|
return -1;
|
2017-08-28 15:14:35 -04:00
|
|
|
}
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
if (modules_handlers_set(m->handlers) != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "%s handler configuration failed\n", m->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-28 15:14:35 -04:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2016-11-05 08:38:15 -04:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
static void
|
|
|
|
modules_deinit(void)
|
|
|
|
{
|
|
|
|
struct httpd_module **ptr;
|
|
|
|
struct httpd_module *m;
|
|
|
|
|
|
|
|
for (ptr = httpd_modules; *ptr; ptr++)
|
|
|
|
{
|
|
|
|
m = *ptr;
|
|
|
|
if (m->initialized && m->deinit)
|
|
|
|
m->deinit();
|
|
|
|
|
|
|
|
modules_handlers_unset(m->handlers);
|
|
|
|
}
|
2010-09-08 13:37:41 -04:00
|
|
|
}
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
static struct httpd_module *
|
|
|
|
modules_search(const char *path)
|
|
|
|
{
|
|
|
|
struct httpd_module **ptr;
|
|
|
|
struct httpd_module *m;
|
|
|
|
const char **test;
|
|
|
|
bool is_found = false;
|
2017-11-07 18:32:03 -05:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
for (ptr = httpd_modules; *ptr; ptr++)
|
|
|
|
{
|
|
|
|
m = *ptr;
|
2023-04-25 17:04:44 -04:00
|
|
|
if (!m->request)
|
2022-12-20 16:59:36 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
for (test = m->subpaths; *test && !is_found; test++)
|
|
|
|
is_found = (strncmp(path, *test, strlen(*test)) == 0);
|
2010-09-08 13:37:41 -04:00
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
for (test = m->fullpaths; *test && !is_found; test++)
|
|
|
|
is_found = (strcmp(path, *test) == 0);
|
|
|
|
|
|
|
|
if (is_found)
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------- REQUEST HELPERS ------------------------------ */
|
2018-02-24 03:59:51 -05:00
|
|
|
|
2023-01-17 11:14:04 -05:00
|
|
|
static void
|
|
|
|
cors_headers_add(struct httpd_request *hreq, const char *allow_origin)
|
|
|
|
{
|
|
|
|
if (allow_origin)
|
|
|
|
httpd_header_add(hreq->out_headers, "Access-Control-Allow-Origin", httpd_allow_origin);
|
|
|
|
|
|
|
|
httpd_header_add(hreq->out_headers, "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
|
|
|
httpd_header_add(hreq->out_headers, "Access-Control-Allow-Headers", "authorization");
|
|
|
|
}
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
static bool
|
|
|
|
is_cors_preflight(struct httpd_request *hreq, const char *allow_origin)
|
2023-01-17 11:14:04 -05:00
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
return ( hreq->method == HTTPD_METHOD_OPTIONS && hreq->in_headers && allow_origin &&
|
|
|
|
httpd_header_find(hreq->in_headers, "Origin") &&
|
|
|
|
httpd_header_find(hreq->in_headers, "Access-Control-Request-Method") );
|
2023-01-17 11:14:04 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 17:15:08 -05:00
|
|
|
void
|
|
|
|
httpd_request_handler_set(struct httpd_request *hreq)
|
2018-02-24 03:59:51 -05:00
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
struct httpd_uri_map *map;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
// Path with e.g. /api -> JSON module
|
|
|
|
hreq->module = modules_search(hreq->path);
|
|
|
|
if (!hreq->module)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (map = hreq->module->handlers; map->handler; map++)
|
|
|
|
{
|
|
|
|
// Check if handler supports the current http request method
|
|
|
|
if (map->method && hreq->method && !(map->method & hreq->method))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = regexec(map->preg, hreq->path, 0, NULL, 0);
|
|
|
|
if (ret != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
hreq->handler = map->handler;
|
2023-01-27 17:13:46 -05:00
|
|
|
hreq->is_async = !(map->flags & HTTPD_HANDLER_REALTIME);
|
2022-12-21 13:11:03 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
httpd_redirect_to(struct httpd_request *hreq, const char *path)
|
|
|
|
{
|
|
|
|
httpd_header_add(hreq->out_headers, "Location", path);
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply(hreq, HTTP_MOVETEMP, "Moved", HTTPD_SEND_NO_GZIP);
|
2018-02-24 03:59:51 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 02:56:57 -04:00
|
|
|
/*
|
|
|
|
* Checks if the given ETag matches the "If-None-Match" request header
|
|
|
|
*
|
|
|
|
* If the request does not contains a "If-None-Match" header value or the value
|
|
|
|
* does not match the given ETag, it returns false (modified) and adds the
|
|
|
|
* "Cache-Control" and "ETag" headers to the response header.
|
|
|
|
*
|
|
|
|
* @param req The request with request and response headers
|
|
|
|
* @param etag The valid ETag for the requested resource
|
|
|
|
* @return True if the given ETag matches the request-header-value "If-None-Match", otherwise false
|
|
|
|
*/
|
|
|
|
bool
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_request_etag_matches(struct httpd_request *hreq, const char *etag)
|
2018-03-30 02:56:57 -04:00
|
|
|
{
|
|
|
|
const char *none_match;
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
none_match = httpd_header_find(hreq->in_headers, "If-None-Match");
|
2018-03-30 02:56:57 -04:00
|
|
|
|
|
|
|
// Return not modified, if given timestamp matches "If-Modified-Since" request header
|
|
|
|
if (none_match && (strcasecmp(etag, none_match) == 0))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Add cache headers to allow client side caching
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Cache-Control", "private,no-cache,max-age=0");
|
|
|
|
httpd_header_add(hreq->out_headers, "ETag", etag);
|
2018-03-30 02:56:57 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks if the given timestamp matches the "If-Modified-Since" request header
|
|
|
|
*
|
|
|
|
* If the request does not contains a "If-Modified-Since" header value or the value
|
|
|
|
* does not match the given timestamp, it returns false (modified) and adds the
|
|
|
|
* "Cache-Control" and "Last-Modified" headers to the response header.
|
|
|
|
*
|
|
|
|
* @param req The request with request and response headers
|
|
|
|
* @param mtime The last modified timestamp for the requested resource
|
|
|
|
* @return True if the given timestamp matches the request-header-value "If-Modified-Since", otherwise false
|
|
|
|
*/
|
|
|
|
bool
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_request_not_modified_since(struct httpd_request *hreq, time_t mtime)
|
2018-03-30 02:56:57 -04:00
|
|
|
{
|
|
|
|
char last_modified[1000];
|
|
|
|
const char *modified_since;
|
2020-08-10 16:19:34 -04:00
|
|
|
struct tm timebuf;
|
2018-03-30 02:56:57 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
modified_since = httpd_header_find(hreq->in_headers, "If-Modified-Since");
|
2018-03-30 02:56:57 -04:00
|
|
|
|
2020-08-10 16:19:34 -04:00
|
|
|
strftime(last_modified, sizeof(last_modified), "%a, %d %b %Y %H:%M:%S %Z", gmtime_r(&mtime, &timebuf));
|
2018-03-30 02:56:57 -04:00
|
|
|
|
|
|
|
// Return not modified, if given timestamp matches "If-Modified-Since" request header
|
|
|
|
if (modified_since && (strcasecmp(last_modified, modified_since) == 0))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Add cache headers to allow client side caching
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Cache-Control", "private,no-cache,max-age=0");
|
|
|
|
httpd_header_add(hreq->out_headers, "Last-Modified", last_modified);
|
2018-03-30 02:56:57 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-29 06:40:49 -04:00
|
|
|
void
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_response_not_cachable(struct httpd_request *hreq)
|
2020-08-29 06:40:49 -04:00
|
|
|
{
|
|
|
|
// Remove potentially set cache control headers
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_remove(hreq->out_headers, "Cache-Control");
|
|
|
|
httpd_header_remove(hreq->out_headers, "Last-Modified");
|
|
|
|
httpd_header_remove(hreq->out_headers, "ETag");
|
2020-08-29 06:40:49 -04:00
|
|
|
|
|
|
|
// Tell clients that they are not allowed to cache this response
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Cache-Control", "no-store");
|
2020-08-29 06:40:49 -04:00
|
|
|
}
|
|
|
|
|
2009-05-02 14:42:51 -04:00
|
|
|
static void
|
2022-12-21 13:11:03 -05:00
|
|
|
serve_file(struct httpd_request *hreq)
|
2009-05-02 14:42:51 -04:00
|
|
|
{
|
2017-11-05 13:34:50 -05:00
|
|
|
char *ext;
|
|
|
|
char path[PATH_MAX];
|
2018-02-24 03:59:51 -05:00
|
|
|
char deref[PATH_MAX];
|
2017-11-05 13:34:50 -05:00
|
|
|
char *ctype;
|
|
|
|
struct stat sb;
|
|
|
|
int fd;
|
|
|
|
int i;
|
|
|
|
uint8_t buf[4096];
|
2018-02-24 03:59:51 -05:00
|
|
|
bool slashed;
|
2009-05-02 14:42:51 -04:00
|
|
|
int ret;
|
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
/* Check authentication */
|
2022-12-21 13:11:03 -05:00
|
|
|
if (!httpd_admin_check_auth(hreq))
|
2017-11-11 15:17:42 -05:00
|
|
|
return;
|
2009-05-02 14:42:51 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
ret = snprintf(path, sizeof(path), "%s%s", webroot_directory, hreq->path);
|
2017-11-05 13:34:50 -05:00
|
|
|
if ((ret < 0) || (ret >= sizeof(path)))
|
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Request exceeds PATH_MAX: %s\n", hreq->uri);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
2017-11-05 13:34:50 -05:00
|
|
|
return;
|
|
|
|
}
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2021-03-14 04:08:16 -04:00
|
|
|
if (!realpath(path, deref))
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2021-03-14 04:08:16 -04:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not dereference %s: %s\n", path, strerror(errno));
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
2009-05-02 14:42:51 -04:00
|
|
|
return;
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2021-03-14 04:08:16 -04:00
|
|
|
if (strlen(deref) >= PATH_MAX)
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
2021-03-14 04:08:16 -04:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Dereferenced path exceeds PATH_MAX: %s\n", path);
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
2021-03-14 04:08:16 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-05-30 17:39:03 -04:00
|
|
|
|
2021-03-14 04:08:16 -04:00
|
|
|
ret = lstat(deref, &sb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_HTTPD, "Could not lstat() %s: %s\n", deref, strerror(errno));
|
2014-05-30 17:39:03 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
2021-03-14 04:08:16 -04:00
|
|
|
return;
|
2018-02-24 03:59:51 -05:00
|
|
|
}
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2018-02-24 03:59:51 -05:00
|
|
|
if (S_ISDIR(sb.st_mode))
|
|
|
|
{
|
|
|
|
slashed = (path[strlen(path) - 1] == '/');
|
2021-03-15 16:59:40 -04:00
|
|
|
strncat(path, ((slashed) ? "index.html" : "/index.html"), sizeof(path) - strlen(path) - 1);
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2021-03-14 04:08:16 -04:00
|
|
|
if (!realpath(path, deref))
|
2018-02-24 03:59:51 -05:00
|
|
|
{
|
2021-03-14 04:08:16 -04:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not dereference %s: %s\n", path, strerror(errno));
|
2018-02-24 03:59:51 -05:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
2021-03-14 04:08:16 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-02-24 03:59:51 -05:00
|
|
|
|
2021-03-14 04:08:16 -04:00
|
|
|
if (strlen(deref) >= PATH_MAX)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Dereferenced path exceeds PATH_MAX: %s\n", path);
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
2018-02-24 03:59:51 -05:00
|
|
|
|
2021-03-14 04:08:16 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = stat(deref, &sb);
|
2018-02-24 03:59:51 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-11-21 06:25:01 -05:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not stat() %s: %s\n", path, strerror(errno));
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
2020-11-21 06:25:01 -05:00
|
|
|
return;
|
2018-02-24 03:59:51 -05:00
|
|
|
}
|
2017-11-05 13:34:50 -05:00
|
|
|
}
|
|
|
|
|
2021-03-14 04:08:16 -04:00
|
|
|
if (path_is_legal(deref) != 0)
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
2021-03-14 04:08:16 -04:00
|
|
|
DPRINTF(E_WARN, L_HTTPD, "Access to file outside the web root dir forbidden: %s\n", deref);
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_error(hreq, HTTP_FORBIDDEN, "Forbidden");
|
2017-11-05 13:34:50 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
if (httpd_request_not_modified_since(hreq, sb.st_mtime))
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply(hreq, HTTP_NOTMODIFIED, NULL, HTTPD_SEND_NO_GZIP);
|
2017-11-05 13:34:50 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-14 04:08:16 -04:00
|
|
|
fd = open(deref, O_RDONLY);
|
2017-11-05 13:34:50 -05:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2021-03-14 04:08:16 -04:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not open %s: %s\n", deref, strerror(errno));
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
2017-11-05 13:34:50 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
ret = evbuffer_expand(hreq->out_body, sb.st_size);
|
2017-11-05 13:34:50 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Out of memory for htdocs-file\n");
|
|
|
|
goto out_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ret = read(fd, buf, sizeof(buf))) > 0)
|
2023-01-27 17:13:46 -05:00
|
|
|
evbuffer_add(hreq->out_body, buf, ret);
|
2017-11-05 13:34:50 -05:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not read file into evbuffer\n");
|
|
|
|
goto out_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctype = "application/octet-stream";
|
|
|
|
ext = strrchr(path, '.');
|
|
|
|
if (ext)
|
|
|
|
{
|
|
|
|
for (i = 0; ext2ctype[i].ext; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(ext, ext2ctype[i].ext) == 0)
|
|
|
|
{
|
|
|
|
ctype = ext2ctype[i].ctype;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Content-Type", ctype);
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply(hreq, HTTP_OK, "OK", HTTPD_SEND_NO_GZIP);
|
2017-11-05 13:34:50 -05:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
|
|
|
|
out_fail:
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_SERVUNAVAIL, "Internal error");
|
2017-11-05 13:34:50 -05:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------- STREAM HANDLING ----------------------------- */
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
// This will triggered in a httpd thread, but since the reading may be in a
|
|
|
|
// worker thread we just want to trigger the read loop
|
2017-11-05 13:34:50 -05:00
|
|
|
static void
|
2023-01-27 17:13:46 -05:00
|
|
|
stream_chunk_resched_cb(httpd_connection *conn, void *arg)
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
struct stream_ctx *st = arg;
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
// TODO not thread safe if st was freed in worker thread, but maybe not possible?
|
|
|
|
event_active(st->ev, 0, 0);
|
|
|
|
}
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
static void
|
|
|
|
stream_free(struct stream_ctx *st)
|
|
|
|
{
|
|
|
|
if (!st)
|
|
|
|
return;
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
if (st->ev)
|
|
|
|
event_free(st->ev);
|
|
|
|
if (st->fd >= 0)
|
|
|
|
close(st->fd);
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
transcode_cleanup(&st->xcode);
|
2017-11-05 13:34:50 -05:00
|
|
|
free(st);
|
|
|
|
}
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
static void
|
|
|
|
stream_end(struct stream_ctx *st)
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Ending stream %d\n", st->id);
|
|
|
|
|
|
|
|
httpd_send_reply_end(st->hreq);
|
|
|
|
|
|
|
|
stream_free(st);
|
|
|
|
}
|
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
static void
|
|
|
|
stream_end_register(struct stream_ctx *st)
|
|
|
|
{
|
|
|
|
if (!st->marked
|
|
|
|
&& (st->stream_size > ((st->size * 50) / 100))
|
|
|
|
&& (st->offset > ((st->size * 80) / 100)))
|
|
|
|
{
|
|
|
|
st->marked = 1;
|
|
|
|
worker_execute(playcount_inc_cb, &st->id, sizeof(int), 0);
|
|
|
|
#ifdef LASTFM
|
|
|
|
worker_execute(scrobble_cb, &st->id, sizeof(int), 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
static struct stream_ctx *
|
|
|
|
stream_new(struct media_file_info *mfi, struct httpd_request *hreq, event_callback_fn stream_cb)
|
|
|
|
{
|
|
|
|
struct stream_ctx *st;
|
|
|
|
|
|
|
|
CHECK_NULL(L_HTTPD, st = calloc(1, sizeof(struct stream_ctx)));
|
|
|
|
st->fd = -1;
|
|
|
|
|
|
|
|
st->ev = event_new(hreq->evbase, -1, EV_PERSIST, stream_cb, st);
|
|
|
|
if (!st->ev)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not create event for streaming\n");
|
|
|
|
|
|
|
|
httpd_send_error(hreq, HTTP_SERVUNAVAIL, "Internal Server Error");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_active(st->ev, 0, 0);
|
|
|
|
|
|
|
|
st->id = mfi->id;
|
|
|
|
st->hreq = hreq;
|
|
|
|
return st;
|
|
|
|
|
|
|
|
error:
|
|
|
|
stream_free(st);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct stream_ctx *
|
|
|
|
stream_new_transcode(struct media_file_info *mfi, struct httpd_request *hreq, int64_t offset, int64_t end_offset, event_callback_fn stream_cb)
|
|
|
|
{
|
|
|
|
struct stream_ctx *st;
|
|
|
|
struct media_quality quality = { HTTPD_STREAM_SAMPLE_RATE, HTTPD_STREAM_BPS, HTTPD_STREAM_CHANNELS, 0 };
|
|
|
|
|
|
|
|
st = stream_new(mfi, hreq, stream_cb);
|
|
|
|
if (!st)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
st->xcode = transcode_setup(XCODE_PCM16_HEADER, &quality, mfi->data_kind, mfi->path, mfi->song_length, &st->size);
|
|
|
|
if (!st->xcode)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_HTTPD, "Transcoding setup failed, aborting streaming\n");
|
|
|
|
|
|
|
|
httpd_send_error(hreq, HTTP_SERVUNAVAIL, "Internal Server Error");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
st->stream_size = st->size - offset;
|
|
|
|
if (end_offset > 0)
|
|
|
|
st->stream_size -= (st->size - end_offset);
|
|
|
|
|
|
|
|
st->start_offset = offset;
|
|
|
|
|
|
|
|
return st;
|
|
|
|
|
|
|
|
error:
|
|
|
|
stream_free(st);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct stream_ctx *
|
|
|
|
stream_new_raw(struct media_file_info *mfi, struct httpd_request *hreq, int64_t offset, int64_t end_offset, event_callback_fn stream_cb)
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
|
|
|
struct stream_ctx *st;
|
2023-01-27 17:13:46 -05:00
|
|
|
struct stat sb;
|
|
|
|
off_t pos;
|
2017-11-05 13:34:50 -05:00
|
|
|
int ret;
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
st = stream_new(mfi, hreq, stream_cb);
|
|
|
|
if (!st)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
st->fd = open(mfi->path, O_RDONLY);
|
|
|
|
if (st->fd < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not open %s: %s\n", mfi->path, strerror(errno));
|
|
|
|
|
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = stat(mfi->path, &sb);
|
2017-11-05 13:34:50 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not stat() %s: %s\n", mfi->path, strerror(errno));
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
|
|
|
goto error;
|
2017-11-05 13:34:50 -05:00
|
|
|
}
|
2023-01-27 17:13:46 -05:00
|
|
|
|
|
|
|
st->size = sb.st_size;
|
|
|
|
|
|
|
|
st->stream_size = st->size - offset;
|
|
|
|
if (end_offset > 0)
|
|
|
|
st->stream_size -= (st->size - end_offset);
|
|
|
|
|
|
|
|
st->start_offset = offset;
|
|
|
|
st->offset = offset;
|
|
|
|
st->end_offset = end_offset;
|
|
|
|
|
|
|
|
pos = lseek(st->fd, offset, SEEK_SET);
|
|
|
|
if (pos == (off_t) -1)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not seek into %s: %s\n", mfi->path, strerror(errno));
|
|
|
|
|
|
|
|
httpd_send_error(hreq, HTTP_BADREQUEST, "Bad Request");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return st;
|
|
|
|
|
|
|
|
error:
|
|
|
|
stream_free(st);
|
|
|
|
return NULL;
|
2017-11-05 13:34:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
stream_chunk_xcode_cb(int fd, short event, void *arg)
|
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
struct stream_ctx *st = arg;
|
2017-11-05 13:34:50 -05:00
|
|
|
int xcoded;
|
|
|
|
int ret;
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
xcoded = transcode(st->hreq->out_body, NULL, st->xcode, STREAM_CHUNK_SIZE);
|
2017-11-05 13:34:50 -05:00
|
|
|
if (xcoded <= 0)
|
|
|
|
{
|
|
|
|
if (xcoded == 0)
|
2017-11-10 17:18:07 -05:00
|
|
|
DPRINTF(E_INFO, L_HTTPD, "Done streaming transcoded file id %d\n", st->id);
|
2017-11-05 13:34:50 -05:00
|
|
|
else
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Transcoding error, file id %d\n", st->id);
|
|
|
|
|
2023-01-26 15:00:40 -05:00
|
|
|
stream_end(st);
|
2017-11-05 13:34:50 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Got %d bytes from transcode; streaming file id %d\n", xcoded, st->id);
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
// Consume transcoded data until we meet start_offset
|
2017-11-05 13:34:50 -05:00
|
|
|
if (st->start_offset > st->offset)
|
|
|
|
{
|
|
|
|
ret = st->start_offset - st->offset;
|
|
|
|
|
|
|
|
if (ret < xcoded)
|
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
evbuffer_drain(st->hreq->out_body, ret);
|
2017-11-05 13:34:50 -05:00
|
|
|
st->offset += ret;
|
|
|
|
|
|
|
|
ret = xcoded - ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
evbuffer_drain(st->hreq->out_body, xcoded);
|
2017-11-05 13:34:50 -05:00
|
|
|
st->offset += xcoded;
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
// Reschedule immediately - consume up to start_offset
|
|
|
|
event_active(st->ev, 0, 0);
|
|
|
|
return;
|
2017-11-05 13:34:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = xcoded;
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply_chunk(st->hreq, stream_chunk_resched_cb, st);
|
2017-11-05 13:34:50 -05:00
|
|
|
|
|
|
|
st->offset += ret;
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2016-09-02 15:52:45 -04:00
|
|
|
stream_end_register(st);
|
2009-05-01 09:33:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
stream_chunk_raw_cb(int fd, short event, void *arg)
|
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
struct stream_ctx *st = arg;
|
2009-12-08 15:42:48 -05:00
|
|
|
size_t chunk_size;
|
2009-05-01 09:33:48 -04:00
|
|
|
int ret;
|
|
|
|
|
2009-12-08 15:42:48 -05:00
|
|
|
if (st->end_offset && (st->offset > st->end_offset))
|
|
|
|
{
|
2023-01-26 15:00:40 -05:00
|
|
|
stream_end(st);
|
2009-12-08 15:42:48 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st->end_offset && ((st->offset + STREAM_CHUNK_SIZE) > (st->end_offset + 1)))
|
|
|
|
chunk_size = st->end_offset + 1 - st->offset;
|
|
|
|
else
|
|
|
|
chunk_size = STREAM_CHUNK_SIZE;
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
ret = evbuffer_read(st->hreq->out_body, st->fd, chunk_size);
|
2009-05-01 09:33:48 -04:00
|
|
|
if (ret <= 0)
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2009-05-01 09:33:48 -04:00
|
|
|
if (ret == 0)
|
2017-11-10 17:18:07 -05:00
|
|
|
DPRINTF(E_INFO, L_HTTPD, "Done streaming file id %d\n", st->id);
|
2009-05-01 09:33:48 -04:00
|
|
|
else
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Streaming error, file id %d\n", st->id);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2023-01-26 15:00:40 -05:00
|
|
|
stream_end(st);
|
2009-05-02 14:42:51 -04:00
|
|
|
return;
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2009-05-01 09:33:48 -04:00
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Read %d bytes; streaming file id %d\n", ret, st->id);
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply_chunk(st->hreq, stream_chunk_resched_cb, st);
|
2009-05-01 09:33:48 -04:00
|
|
|
|
|
|
|
st->offset += ret;
|
|
|
|
|
2016-09-02 15:52:45 -04:00
|
|
|
stream_end_register(st);
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2009-05-02 12:49:31 -04:00
|
|
|
static void
|
2023-02-08 12:29:48 -05:00
|
|
|
stream_fail_cb(void *arg)
|
2009-05-02 12:49:31 -04:00
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
struct stream_ctx *st = arg;
|
2009-05-02 12:49:31 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
stream_free(st);
|
|
|
|
}
|
2009-05-02 12:49:31 -04:00
|
|
|
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
/* ---------------------------- REQUEST CALLBACKS --------------------------- */
|
2009-05-02 12:49:31 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
// Worker thread, invoked by request_cb() below
|
|
|
|
static void
|
|
|
|
request_async_cb(void *arg)
|
|
|
|
{
|
|
|
|
struct httpd_request *hreq = *(struct httpd_request **)arg;
|
2009-05-02 12:49:31 -04:00
|
|
|
|
2023-08-29 17:50:19 -04:00
|
|
|
#if defined(HAVE_SYSCALL) && defined(HAVE_SYS_GETTID)
|
2023-01-27 17:13:46 -05:00
|
|
|
DPRINTF(E_DBG, hreq->module->logdomain, "%s request '%s' in worker thread %ld\n", hreq->module->name, hreq->uri, syscall(SYS_gettid));
|
|
|
|
#endif
|
2009-05-02 12:49:31 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
// Some handlers require an evbase to schedule events
|
|
|
|
hreq->evbase = worker_evbase_get();
|
|
|
|
hreq->module->request(hreq);
|
|
|
|
}
|
2017-11-05 13:34:50 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
// httpd thread
|
2017-11-05 13:34:50 -05:00
|
|
|
static void
|
2023-01-09 17:15:08 -05:00
|
|
|
request_cb(struct httpd_request *hreq, void *arg)
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
if (is_cors_preflight(hreq, httpd_allow_origin))
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply(hreq, HTTP_OK, "OK", HTTPD_SEND_NO_GZIP);
|
2023-01-09 17:15:08 -05:00
|
|
|
return;
|
2017-11-05 13:34:50 -05:00
|
|
|
}
|
2023-01-27 17:13:46 -05:00
|
|
|
else if (!hreq->uri || !hreq->uri_parsed)
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
DPRINTF(E_WARN, L_HTTPD, "Invalid URI in request: '%s'\n", hreq->uri);
|
|
|
|
httpd_redirect_to(hreq, "/");
|
2023-01-09 17:15:08 -05:00
|
|
|
return;
|
2017-11-05 13:34:50 -05:00
|
|
|
}
|
2022-12-21 13:11:03 -05:00
|
|
|
else if (!hreq->path)
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
DPRINTF(E_WARN, L_HTTPD, "Invalid path in request: '%s'\n", hreq->uri);
|
|
|
|
httpd_redirect_to(hreq, "/");
|
2023-01-09 17:15:08 -05:00
|
|
|
return;
|
2017-11-05 13:34:50 -05:00
|
|
|
}
|
|
|
|
|
2023-01-09 17:15:08 -05:00
|
|
|
httpd_request_handler_set(hreq);
|
2023-01-27 17:13:46 -05:00
|
|
|
if (hreq->module && hreq->is_async)
|
|
|
|
{
|
|
|
|
worker_execute(request_async_cb, &hreq, sizeof(struct httpd_request *), 0);
|
|
|
|
}
|
|
|
|
else if (hreq->module)
|
2018-02-24 03:59:51 -05:00
|
|
|
{
|
2023-01-26 15:00:40 -05:00
|
|
|
DPRINTF(E_DBG, hreq->module->logdomain, "%s request: '%s'\n", hreq->module->name, hreq->uri);
|
2023-01-27 17:13:46 -05:00
|
|
|
hreq->evbase = httpd_backend_evbase_get(hreq->backend);
|
2022-12-21 13:11:03 -05:00
|
|
|
hreq->module->request(hreq);
|
2018-02-24 03:59:51 -05:00
|
|
|
}
|
2022-12-21 13:11:03 -05:00
|
|
|
else
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
// Serve web interface files
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "HTTP request: '%s'\n", hreq->uri);
|
|
|
|
serve_file(hreq);
|
2017-11-05 13:34:50 -05:00
|
|
|
}
|
2023-01-27 17:13:46 -05:00
|
|
|
|
|
|
|
// Don't touch hreq here, if async it has been passed to a worker thread
|
2017-11-05 13:34:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------------------------- HTTPD API -------------------------------- */
|
|
|
|
|
2009-04-25 04:40:47 -04:00
|
|
|
void
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_stream_file(struct httpd_request *hreq, int id)
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
struct media_file_info *mfi = NULL;
|
|
|
|
struct stream_ctx *st = NULL;
|
2009-04-25 04:40:47 -04:00
|
|
|
const char *param;
|
2009-12-08 15:42:48 -05:00
|
|
|
const char *param_end;
|
2009-04-25 04:40:47 -04:00
|
|
|
char buf[64];
|
2023-01-27 17:13:46 -05:00
|
|
|
int64_t offset = 0;
|
|
|
|
int64_t end_offset = 0;
|
2009-04-25 04:40:47 -04:00
|
|
|
int transcode;
|
|
|
|
int ret;
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
param = httpd_header_find(hreq->in_headers, "Range");
|
2009-04-25 04:40:47 -04:00
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Found Range header: %s\n", param);
|
|
|
|
|
2009-12-08 15:42:48 -05:00
|
|
|
/* Start offset */
|
2010-02-02 15:02:24 -05:00
|
|
|
ret = safe_atoi64(param + strlen("bytes="), &offset);
|
2009-04-25 04:40:47 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2009-12-08 15:42:48 -05:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Invalid start offset, will stream whole file (%s)\n", param);
|
2009-04-25 04:40:47 -04:00
|
|
|
offset = 0;
|
|
|
|
}
|
2009-12-08 15:42:48 -05:00
|
|
|
/* End offset, if any */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
param_end = strchr(param, '-');
|
2014-02-03 16:02:20 -05:00
|
|
|
if (param_end && (strlen(param_end) > 1))
|
2009-12-08 15:42:48 -05:00
|
|
|
{
|
2010-02-02 15:02:24 -05:00
|
|
|
ret = safe_atoi64(param_end + 1, &end_offset);
|
2009-12-08 15:42:48 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Invalid end offset, will stream to end of file (%s)\n", param);
|
|
|
|
end_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end_offset < offset)
|
|
|
|
{
|
2010-02-02 15:02:24 -05:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "End offset < start offset, will stream to end of file (%" PRIi64 " < %" PRIi64 ")\n", end_offset, offset);
|
2009-12-08 15:42:48 -05:00
|
|
|
end_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
mfi = db_file_fetch_byid(id);
|
2009-04-25 04:40:47 -04:00
|
|
|
if (!mfi)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Item %d not found\n", id);
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_NOTFOUND, "Not Found");
|
2023-01-27 17:13:46 -05:00
|
|
|
goto error;
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2015-04-23 05:34:44 -04:00
|
|
|
if (mfi->data_kind != DATA_KIND_FILE)
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2020-04-15 10:00:18 -04:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not serve '%s' to client, not a file\n", mfi->path);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_error(hreq, HTTP_INTERNAL, "Cannot stream non-file content");
|
|
|
|
goto error;
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
param = httpd_header_find(hreq->in_headers, "Accept-Codecs");
|
2009-05-01 09:33:48 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
transcode = transcode_needed(hreq->user_agent, param, mfi->codectype);
|
2009-05-01 09:33:48 -04:00
|
|
|
if (transcode)
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2009-05-08 11:52:56 -04:00
|
|
|
DPRINTF(E_INFO, L_HTTPD, "Preparing to transcode %s\n", mfi->path);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
st = stream_new_transcode(mfi, hreq, offset, end_offset, stream_chunk_xcode_cb);
|
|
|
|
if (!st)
|
|
|
|
goto error;
|
2009-05-01 09:33:48 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
if (!httpd_header_find(hreq->out_headers, "Content-Type"))
|
|
|
|
httpd_header_add(hreq->out_headers, "Content-Type", "audio/wav");
|
2009-05-01 09:33:48 -04:00
|
|
|
}
|
|
|
|
else
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2009-05-08 11:52:56 -04:00
|
|
|
DPRINTF(E_INFO, L_HTTPD, "Preparing to stream %s\n", mfi->path);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
st = stream_new_raw(mfi, hreq, offset, end_offset, stream_chunk_raw_cb);
|
|
|
|
if (!st)
|
|
|
|
goto error;
|
2009-05-01 09:33:48 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
// Content-Type for video files is different than for audio files and
|
|
|
|
// overrides whatever may have been set previously, like
|
|
|
|
// application/x-dmap-tagged when we're speaking DAAP.
|
2009-12-08 15:04:30 -05:00
|
|
|
if (mfi->has_video)
|
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
// Front Row and others expect video/<type>
|
2009-12-08 15:04:30 -05:00
|
|
|
ret = snprintf(buf, sizeof(buf), "video/%s", mfi->type);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(buf)))
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Content-Type too large for buffer, dropping\n");
|
|
|
|
else
|
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_remove(hreq->out_headers, "Content-Type");
|
|
|
|
httpd_header_add(hreq->out_headers, "Content-Type", buf);
|
2009-12-08 15:04:30 -05:00
|
|
|
}
|
|
|
|
}
|
2023-01-27 17:13:46 -05:00
|
|
|
// If no Content-Type has been set and we're streaming audio, add a proper
|
|
|
|
// Content-Type for the file we're streaming. Remember DAAP streams audio
|
|
|
|
// with application/x-dmap-tagged as the Content-Type (ugh!).
|
2022-12-21 13:11:03 -05:00
|
|
|
else if (!httpd_header_find(hreq->out_headers, "Content-Type") && mfi->type)
|
2009-05-01 09:33:48 -04:00
|
|
|
{
|
|
|
|
ret = snprintf(buf, sizeof(buf), "audio/%s", mfi->type);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(buf)))
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Content-Type too large for buffer, dropping\n");
|
|
|
|
else
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Content-Type", buf);
|
2009-05-01 09:33:48 -04:00
|
|
|
}
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2009-12-08 15:42:48 -05:00
|
|
|
if ((offset == 0) && (end_offset == 0))
|
2009-12-31 10:47:24 -05:00
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
// If we are not decoding, send the Content-Length. We don't do that if we
|
|
|
|
// are decoding because we can only guesstimate the size in this case and
|
|
|
|
// the error margin is unknown and variable.
|
2010-02-02 15:05:04 -05:00
|
|
|
if (!transcode)
|
2009-12-31 10:47:24 -05:00
|
|
|
{
|
2010-02-02 15:02:24 -05:00
|
|
|
ret = snprintf(buf, sizeof(buf), "%" PRIi64, (int64_t)st->size);
|
2009-12-31 10:47:24 -05:00
|
|
|
if ((ret < 0) || (ret >= sizeof(buf)))
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Content-Length too large for buffer, dropping\n");
|
|
|
|
else
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Content-Length", buf);
|
2009-12-31 10:47:24 -05:00
|
|
|
}
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_reply_start(hreq, HTTP_OK, "OK");
|
2009-12-31 10:47:24 -05:00
|
|
|
}
|
2009-04-25 04:40:47 -04:00
|
|
|
else
|
|
|
|
{
|
2010-02-02 15:02:24 -05:00
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Stream request with range %" PRIi64 "-%" PRIi64 "\n", offset, end_offset);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2010-02-02 15:02:24 -05:00
|
|
|
ret = snprintf(buf, sizeof(buf), "bytes %" PRIi64 "-%" PRIi64 "/%" PRIi64,
|
2011-04-24 12:56:34 -04:00
|
|
|
offset, (end_offset) ? end_offset : (int64_t)st->size, (int64_t)st->size);
|
2009-04-25 04:40:47 -04:00
|
|
|
if ((ret < 0) || (ret >= sizeof(buf)))
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Content-Range too large for buffer, dropping\n");
|
|
|
|
else
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Content-Range", buf);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2011-04-24 12:56:34 -04:00
|
|
|
ret = snprintf(buf, sizeof(buf), "%" PRIi64, ((end_offset) ? end_offset + 1 : (int64_t)st->size) - offset);
|
2009-12-08 15:46:47 -05:00
|
|
|
if ((ret < 0) || (ret >= sizeof(buf)))
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Content-Length too large for buffer, dropping\n");
|
|
|
|
else
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Content-Length", buf);
|
2009-12-08 15:46:47 -05:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_reply_start(hreq, 206, "Partial Content");
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2010-02-02 14:59:46 -05:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
|
|
|
if (!transcode)
|
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
// Hint the OS
|
2022-01-19 18:14:07 -05:00
|
|
|
if ( (ret = posix_fadvise(st->fd, st->start_offset, st->stream_size, POSIX_FADV_WILLNEED)) != 0 ||
|
|
|
|
(ret = posix_fadvise(st->fd, st->start_offset, st->stream_size, POSIX_FADV_SEQUENTIAL)) != 0 ||
|
|
|
|
(ret = posix_fadvise(st->fd, st->start_offset, st->stream_size, POSIX_FADV_NOREUSE)) != 0 )
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "posix_fadvise() failed with error %d\n", ret);
|
2010-02-02 14:59:46 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_request_close_cb_set(hreq, stream_fail_cb, st);
|
2009-05-02 12:49:31 -04:00
|
|
|
|
2009-05-08 11:52:56 -04:00
|
|
|
DPRINTF(E_INFO, L_HTTPD, "Kicking off streaming for %s\n", mfi->path);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
free_mfi(mfi, 0);
|
2010-02-02 15:04:35 -05:00
|
|
|
return;
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
error:
|
|
|
|
stream_free(st);
|
2010-02-02 15:04:35 -05:00
|
|
|
free_mfi(mfi, 0);
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2016-10-17 13:35:37 -04:00
|
|
|
struct evbuffer *
|
|
|
|
httpd_gzip_deflate(struct evbuffer *in)
|
|
|
|
{
|
|
|
|
struct evbuffer *out;
|
|
|
|
struct evbuffer_iovec iovec[1];
|
|
|
|
z_stream strm;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
|
2022-01-19 18:14:07 -05:00
|
|
|
// Just to keep Coverity from complaining about uninitialized values
|
|
|
|
strm.total_in = 0;
|
|
|
|
strm.total_out = 0;
|
|
|
|
|
2016-10-17 13:35:37 -04:00
|
|
|
// Set up a gzip stream (the "+ 16" in 15 + 16), instead of a zlib stream (default)
|
|
|
|
ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
|
|
|
|
if (ret != Z_OK)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "zlib setup failed: %s\n", zError(ret));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strm.next_in = evbuffer_pullup(in, -1);
|
|
|
|
strm.avail_in = evbuffer_get_length(in);
|
|
|
|
|
|
|
|
out = evbuffer_new();
|
|
|
|
if (!out)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not allocate evbuffer for gzipped reply\n");
|
|
|
|
goto out_deflate_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use this to avoid a memcpy. The 512 is an arbitrary padding to make sure
|
|
|
|
// there is enough space, even if the compressed output should be slightly
|
|
|
|
// larger than input (could happen with small inputs).
|
|
|
|
ret = evbuffer_reserve_space(out, strm.avail_in + 512, iovec, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not reserve memory for gzipped reply\n");
|
|
|
|
goto out_evbuf_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
strm.next_out = iovec[0].iov_base;
|
|
|
|
strm.avail_out = iovec[0].iov_len;
|
|
|
|
|
|
|
|
ret = deflate(&strm, Z_FINISH);
|
|
|
|
if (ret != Z_STREAM_END)
|
|
|
|
goto out_evbuf_free;
|
|
|
|
|
|
|
|
iovec[0].iov_len -= strm.avail_out;
|
|
|
|
|
|
|
|
evbuffer_commit_space(out, iovec, 1);
|
|
|
|
deflateEnd(&strm);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
out_evbuf_free:
|
|
|
|
evbuffer_free(out);
|
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
out_deflate_end:
|
|
|
|
deflateEnd(&strm);
|
2017-08-12 13:37:07 -04:00
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
return NULL;
|
2009-04-22 15:25:41 -04:00
|
|
|
}
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
|
|
|
|
// The httpd_send functions below can be called from a worker thread (with
|
|
|
|
// hreq->is_async) or directly from the httpd thread. In the former case, they
|
|
|
|
// will command sending from the httpd thread, since it is not safe to access
|
|
|
|
// the backend (evhttp) from a worker thread. hreq will be freed (again,
|
|
|
|
// possibly async) if the type is either _COMPLETE or _END.
|
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
void
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply(struct httpd_request *hreq, int code, const char *reason, enum httpd_send_flags flags)
|
2009-04-22 15:25:41 -04:00
|
|
|
{
|
2017-11-05 13:34:50 -05:00
|
|
|
struct evbuffer *gzbuf;
|
2023-01-27 17:13:46 -05:00
|
|
|
struct evbuffer *save;
|
2017-11-05 13:34:50 -05:00
|
|
|
const char *param;
|
|
|
|
int do_gzip;
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
if (!hreq->backend)
|
2017-11-05 13:34:50 -05:00
|
|
|
return;
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
do_gzip = ( (!(flags & HTTPD_SEND_NO_GZIP)) &&
|
2023-01-27 17:13:46 -05:00
|
|
|
(evbuffer_get_length(hreq->out_body) > 512) &&
|
2022-12-21 13:11:03 -05:00
|
|
|
(param = httpd_header_find(hreq->in_headers, "Accept-Encoding")) &&
|
2017-11-05 13:34:50 -05:00
|
|
|
(strstr(param, "gzip") || strstr(param, "*"))
|
|
|
|
);
|
2016-10-18 17:04:51 -04:00
|
|
|
|
2023-01-17 11:14:04 -05:00
|
|
|
cors_headers_add(hreq, httpd_allow_origin);
|
2016-10-18 17:04:51 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
if (do_gzip && (gzbuf = httpd_gzip_deflate(hreq->out_body)))
|
2009-04-22 15:25:41 -04:00
|
|
|
{
|
2017-11-05 13:34:50 -05:00
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Gzipping response\n");
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Content-Encoding", "gzip");
|
2023-01-27 17:13:46 -05:00
|
|
|
save = hreq->out_body;
|
|
|
|
hreq->out_body = gzbuf;
|
|
|
|
evbuffer_free(save);
|
2009-04-24 09:42:20 -04:00
|
|
|
}
|
2023-01-17 11:14:04 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send(hreq, HTTPD_REPLY_COMPLETE, code, reason, NULL, NULL);
|
2009-04-22 15:25:41 -04:00
|
|
|
}
|
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
void
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_reply_start(struct httpd_request *hreq, int code, const char *reason)
|
2009-04-22 15:25:41 -04:00
|
|
|
{
|
2023-01-17 11:14:04 -05:00
|
|
|
cors_headers_add(hreq, httpd_allow_origin);
|
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send(hreq, HTTPD_REPLY_START, code, reason, NULL, NULL);
|
2022-12-21 13:11:03 -05:00
|
|
|
}
|
2009-06-07 12:58:02 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
void
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply_chunk(struct httpd_request *hreq, httpd_connection_chunkcb cb, void *arg)
|
2022-12-21 13:11:03 -05:00
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send(hreq, HTTPD_REPLY_CHUNK, 0, NULL, cb, arg);
|
2022-12-21 13:11:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
httpd_send_reply_end(struct httpd_request *hreq)
|
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send(hreq, HTTPD_REPLY_END, 0, NULL, NULL, NULL);
|
2022-12-21 13:11:03 -05:00
|
|
|
}
|
2009-06-07 12:58:02 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
// This is a modified version of evhttp_send_error (credit libevent)
|
|
|
|
void
|
|
|
|
httpd_send_error(struct httpd_request *hreq, int error, const char *reason)
|
|
|
|
{
|
2023-01-27 17:13:46 -05:00
|
|
|
evbuffer_drain(hreq->out_body, -1);
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_headers_clear(hreq->out_headers);
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2023-01-17 11:14:04 -05:00
|
|
|
cors_headers_add(hreq, httpd_allow_origin);
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Content-Type", "text/html");
|
|
|
|
httpd_header_add(hreq->out_headers, "Connection", "close");
|
2009-06-07 12:58:02 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
evbuffer_add_printf(hreq->out_body, ERR_PAGE, error, reason, reason);
|
2023-01-17 11:14:04 -05:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send(hreq, HTTPD_REPLY_COMPLETE, error, reason, NULL, NULL);
|
2009-04-22 15:25:41 -04:00
|
|
|
}
|
|
|
|
|
2017-11-11 15:17:42 -05:00
|
|
|
bool
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_admin_check_auth(struct httpd_request *hreq)
|
2017-11-13 19:50:06 -05:00
|
|
|
{
|
2017-11-15 17:13:20 -05:00
|
|
|
const char *passwd;
|
|
|
|
int ret;
|
2017-11-13 19:50:06 -05:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
if (net_peer_address_is_trusted(hreq->peer_address))
|
2017-11-05 13:34:50 -05:00
|
|
|
return true;
|
2009-05-02 11:20:33 -04:00
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
passwd = cfg_getstr(cfg_getsec(cfg, "general"), "admin_password");
|
2017-11-11 15:17:42 -05:00
|
|
|
if (!passwd)
|
2017-11-05 13:34:50 -05:00
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Web interface request to '%s' denied: No password set in the config\n", hreq->uri);
|
2009-05-02 11:20:33 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_error(hreq, HTTP_FORBIDDEN, "Forbidden");
|
2017-11-11 15:17:42 -05:00
|
|
|
return false;
|
2017-11-05 13:34:50 -05:00
|
|
|
}
|
2017-11-11 15:17:42 -05:00
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Checking web interface authentication\n");
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
ret = httpd_basic_auth(hreq, "admin", passwd, PACKAGE " web interface");
|
2017-11-11 15:17:42 -05:00
|
|
|
if (ret != 0)
|
2009-05-02 11:20:33 -04:00
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Web interface request to '%s' denied: Incorrect password\n", hreq->uri);
|
2009-05-02 11:20:33 -04:00
|
|
|
|
2017-11-11 15:17:42 -05:00
|
|
|
// httpd_basic_auth has sent a reply
|
|
|
|
return false;
|
2009-05-02 11:20:33 -04:00
|
|
|
}
|
|
|
|
|
2017-11-11 15:17:42 -05:00
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Authentication successful\n");
|
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
return true;
|
|
|
|
}
|
2009-05-01 14:58:15 -04:00
|
|
|
|
|
|
|
int
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_basic_auth(struct httpd_request *hreq, const char *user, const char *passwd, const char *realm)
|
2009-05-01 14:58:15 -04:00
|
|
|
{
|
2016-11-19 17:08:50 -05:00
|
|
|
char header[256];
|
2009-05-01 14:58:15 -04:00
|
|
|
const char *auth;
|
|
|
|
char *authuser;
|
|
|
|
char *authpwd;
|
|
|
|
int ret;
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
auth = httpd_header_find(hreq->in_headers, "Authorization");
|
2009-05-01 14:58:15 -04:00
|
|
|
if (!auth)
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "No Authorization header\n");
|
|
|
|
|
|
|
|
goto need_auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(auth, "Basic ", strlen("Basic ")) != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Bad Authentication header\n");
|
|
|
|
|
|
|
|
goto need_auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
auth += strlen("Basic ");
|
|
|
|
|
2019-09-06 15:51:53 -04:00
|
|
|
authuser = (char *)b64_decode(NULL, auth);
|
2009-05-01 14:58:15 -04:00
|
|
|
if (!authuser)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not decode Authentication header\n");
|
|
|
|
|
|
|
|
goto need_auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
authpwd = strchr(authuser, ':');
|
|
|
|
if (!authpwd)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Malformed Authentication header\n");
|
|
|
|
|
|
|
|
free(authuser);
|
|
|
|
goto need_auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
*authpwd = '\0';
|
|
|
|
authpwd++;
|
|
|
|
|
|
|
|
if (user)
|
|
|
|
{
|
|
|
|
if (strcmp(user, authuser) != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Username mismatch\n");
|
|
|
|
|
|
|
|
free(authuser);
|
|
|
|
goto need_auth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(passwd, authpwd) != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Bad password\n");
|
|
|
|
|
|
|
|
free(authuser);
|
|
|
|
goto need_auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(authuser);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
need_auth:
|
2016-11-19 17:08:50 -05:00
|
|
|
ret = snprintf(header, sizeof(header), "Basic realm=\"%s\"", realm);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(header)))
|
2009-05-01 14:58:15 -04:00
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_SERVUNAVAIL, "Internal Server Error");
|
2009-05-01 14:58:15 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "WWW-Authenticate", header);
|
2016-10-17 16:14:10 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
evbuffer_add_printf(hreq->out_body, ERR_PAGE, HTTP_UNAUTHORIZED, "Unauthorized", "Authorization required");
|
2009-05-01 14:58:15 -04:00
|
|
|
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply(hreq, HTTP_UNAUTHORIZED, "Unauthorized", HTTPD_SEND_NO_GZIP);
|
2009-05-01 14:58:15 -04:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-01-21 18:33:54 -05:00
|
|
|
static int
|
|
|
|
bind_test(short unsigned port)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = net_bind(&port, SOCK_STREAM, "httpd init");
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:14:04 -05:00
|
|
|
static void
|
|
|
|
thread_init_cb(struct evthr *thr, void *shared)
|
|
|
|
{
|
2023-01-21 18:33:54 -05:00
|
|
|
struct event_base *evbase;
|
|
|
|
httpd_server *server;
|
2023-01-17 11:14:04 -05:00
|
|
|
|
|
|
|
thread_setname(pthread_self(), "httpd");
|
|
|
|
|
2023-01-21 18:33:54 -05:00
|
|
|
CHECK_ERR(L_HTTPD, db_perthread_init());
|
|
|
|
CHECK_NULL(L_HTTPD, evbase = evthr_get_base(thr));
|
|
|
|
CHECK_NULL(L_HTTPD, server = httpd_server_new(evbase, httpd_port, request_cb, NULL));
|
2023-01-17 11:14:04 -05:00
|
|
|
|
|
|
|
// For CORS headers
|
2023-01-21 18:33:54 -05:00
|
|
|
httpd_server_allow_origin_set(server, httpd_allow_origin);
|
|
|
|
|
|
|
|
evthr_set_aux(thr, server);
|
2023-01-17 11:14:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
thread_exit_cb(struct evthr *thr, void *shared)
|
|
|
|
{
|
2023-01-21 18:33:54 -05:00
|
|
|
httpd_server *server;
|
|
|
|
|
|
|
|
server = evthr_get_aux(thr);
|
|
|
|
httpd_server_free(server);
|
2023-01-17 11:14:04 -05:00
|
|
|
|
|
|
|
db_perthread_deinit();
|
|
|
|
}
|
|
|
|
|
2009-04-22 15:25:41 -04:00
|
|
|
/* Thread: main */
|
|
|
|
int
|
2017-12-03 03:21:17 -05:00
|
|
|
httpd_init(const char *webroot)
|
2009-04-22 15:25:41 -04:00
|
|
|
{
|
2017-12-03 03:21:17 -05:00
|
|
|
struct stat sb;
|
2009-04-22 15:25:41 -04:00
|
|
|
int ret;
|
|
|
|
|
2017-12-03 03:21:17 -05:00
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Starting web server with root directory '%s'\n", webroot);
|
2018-08-18 06:34:03 -04:00
|
|
|
ret = stat(webroot, &sb);
|
2017-12-03 03:21:17 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-08-18 06:34:03 -04:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not stat() web root directory '%s': %s\n", webroot, strerror(errno));
|
2017-12-03 03:21:17 -05:00
|
|
|
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;
|
|
|
|
}
|
2021-07-04 05:41:39 -04:00
|
|
|
if (!realpath(webroot, webroot_directory))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Web root directory '%s' could not be dereferenced: %s\n", webroot, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2017-12-03 03:21:17 -05:00
|
|
|
|
2023-01-17 11:14:04 -05:00
|
|
|
// Read config
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_port = cfg_getint(cfg_getsec(cfg, "library"), "port");
|
|
|
|
httpd_allow_origin = cfg_getstr(cfg_getsec(cfg, "general"), "allow_origin");
|
|
|
|
if (strlen(httpd_allow_origin) == 0)
|
|
|
|
httpd_allow_origin = NULL;
|
|
|
|
|
2023-01-21 18:33:54 -05:00
|
|
|
// Test that the port is free. We do it here because we can make a nicer exit
|
|
|
|
// than we can in thread_init_cb(), where the actual binding takes place.
|
|
|
|
ret = bind_test(httpd_port);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Could not create HTTP server on port %d (server already running?)\n", httpd_port);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
// Prepare modules, e.g. httpd_daap
|
2023-01-17 11:14:04 -05:00
|
|
|
ret = modules_init();
|
2022-12-21 13:11:03 -05:00
|
|
|
if (ret < 0)
|
2016-10-19 15:26:13 -04:00
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Modules init failed\n");
|
2023-01-17 11:14:04 -05:00
|
|
|
goto error;
|
2016-10-19 15:22:14 -04:00
|
|
|
}
|
2016-10-17 16:03:32 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
#ifdef HAVE_LIBWEBSOCKETS
|
|
|
|
ret = websocket_init();
|
2017-09-07 15:44:12 -04:00
|
|
|
if (ret < 0)
|
2015-12-21 15:52:21 -05:00
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Websocket init failed\n");
|
2023-01-17 11:14:04 -05:00
|
|
|
goto error;
|
2009-04-22 15:25:41 -04:00
|
|
|
}
|
2022-12-21 13:11:03 -05:00
|
|
|
#endif
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2023-01-17 11:14:04 -05:00
|
|
|
httpd_threadpool = evthr_pool_wexit_new(THREADPOOL_NTHREADS, thread_init_cb, thread_exit_cb, NULL);
|
|
|
|
if (!httpd_threadpool)
|
2009-04-22 15:25:41 -04:00
|
|
|
{
|
2023-01-17 11:14:04 -05:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not create httpd thread pool\n");
|
|
|
|
goto error;
|
2009-04-22 15:25:41 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:14:04 -05:00
|
|
|
ret = evthr_pool_start(httpd_threadpool);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not spawn worker threads\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2016-03-05 07:19:55 -05:00
|
|
|
|
2009-04-22 15:25:41 -04:00
|
|
|
return 0;
|
|
|
|
|
2023-01-17 11:14:04 -05:00
|
|
|
error:
|
|
|
|
httpd_deinit();
|
2009-04-22 15:25:41 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread: main */
|
|
|
|
void
|
|
|
|
httpd_deinit(void)
|
|
|
|
{
|
2023-01-17 11:14:04 -05:00
|
|
|
// Give modules a chance to hang up connections nicely
|
2022-12-20 16:59:36 -05:00
|
|
|
modules_deinit();
|
|
|
|
|
2017-09-15 18:05:50 -04:00
|
|
|
#ifdef HAVE_LIBWEBSOCKETS
|
2017-08-12 14:01:57 -04:00
|
|
|
websocket_deinit();
|
|
|
|
#endif
|
2009-04-24 09:42:20 -04:00
|
|
|
|
2023-01-17 11:14:04 -05:00
|
|
|
evthr_pool_stop(httpd_threadpool);
|
|
|
|
evthr_pool_free(httpd_threadpool);
|
2009-04-22 15:25:41 -04:00
|
|
|
}
|