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>
|
|
|
|
#include <pthread.h>
|
2009-04-25 04:40:47 -04:00
|
|
|
#include <time.h>
|
2010-01-09 07:42:23 -05:00
|
|
|
#include <sys/param.h>
|
2009-04-25 04:40:47 -04:00
|
|
|
#include <sys/queue.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
|
|
|
|
2010-02-04 12:52:13 -05:00
|
|
|
#if defined(HAVE_SYS_EVENTFD_H) && defined(HAVE_EVENTFD)
|
|
|
|
# define USE_EVENTFD
|
|
|
|
# include <sys/eventfd.h>
|
|
|
|
#endif
|
|
|
|
|
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"
|
2009-04-22 15:25:41 -04:00
|
|
|
#include "httpd.h"
|
2009-04-24 09:42:20 -04:00
|
|
|
#include "httpd_rsp.h"
|
2009-04-28 11:52:11 -04:00
|
|
|
#include "httpd_daap.h"
|
2010-01-29 16:43:39 -05:00
|
|
|
#include "httpd_dacp.h"
|
2009-05-01 09:33:48 -04:00
|
|
|
#include "transcode.h"
|
2009-04-22 15:25:41 -04:00
|
|
|
|
|
|
|
|
2009-04-25 05:09:25 -04:00
|
|
|
/*
|
|
|
|
* HTTP client quirks by User-Agent, from mt-daapd
|
|
|
|
*
|
|
|
|
* - iTunes:
|
|
|
|
* + Connection: Keep-Alive on HTTP error 401
|
|
|
|
* - Hifidelio:
|
|
|
|
* + Connection: Keep-Alive for streaming (Connection: close not honoured)
|
|
|
|
*
|
|
|
|
* These quirks are not implemented. Implement as needed.
|
2009-05-02 11:20:33 -04:00
|
|
|
*
|
|
|
|
* Implemented quirks:
|
|
|
|
*
|
|
|
|
* - Roku:
|
|
|
|
* + Does not encode space as + in query string
|
|
|
|
* - iTunes:
|
|
|
|
* + Does not encode space as + in query string
|
2009-04-25 05:09:25 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-02-02 15:06:13 -05:00
|
|
|
#define STREAM_CHUNK_SIZE (64 * 1024)
|
2009-06-12 05:09:58 -04:00
|
|
|
#define WEBFACE_ROOT DATADIR "/webface/"
|
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 {
|
2009-04-25 04:40:47 -04:00
|
|
|
struct evhttp_request *req;
|
2010-02-02 15:03:29 -05:00
|
|
|
uint8_t *buf;
|
2009-04-25 04:40:47 -04:00
|
|
|
struct evbuffer *evbuf;
|
2009-05-02 12:49:31 -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
|
|
|
};
|
|
|
|
|
2009-04-22 15:25:41 -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 }
|
|
|
|
};
|
|
|
|
|
2010-03-06 04:27:39 -05:00
|
|
|
struct event_base *evbase_httpd;
|
|
|
|
|
2010-02-04 12:52:13 -05:00
|
|
|
#ifdef USE_EVENTFD
|
|
|
|
static int exit_efd;
|
|
|
|
#else
|
2009-04-22 15:25:41 -04:00
|
|
|
static int exit_pipe[2];
|
2010-02-04 12:52:13 -05:00
|
|
|
#endif
|
2009-04-22 15:25:41 -04:00
|
|
|
static int httpd_exit;
|
|
|
|
static struct event exitev;
|
|
|
|
static struct evhttp *evhttpd;
|
|
|
|
static pthread_t tid_httpd;
|
|
|
|
|
|
|
|
|
2009-05-02 14:42:51 -04:00
|
|
|
static void
|
2009-12-08 13:52:51 -05:00
|
|
|
stream_end(struct stream_ctx *st, int failed)
|
2009-05-02 14:42:51 -04:00
|
|
|
{
|
2010-07-23 12:15:18 -04:00
|
|
|
if (st->req->evcon)
|
|
|
|
evhttp_connection_set_closecb(st->req->evcon, NULL, NULL);
|
2009-05-02 14:42:51 -04:00
|
|
|
|
2009-12-08 13:52:51 -05:00
|
|
|
if (!failed)
|
|
|
|
evhttp_send_reply_end(st->req);
|
2009-05-02 14:42:51 -04:00
|
|
|
|
|
|
|
evbuffer_free(st->evbuf);
|
2009-12-08 13:52:51 -05:00
|
|
|
|
2009-05-02 14:42:51 -04:00
|
|
|
if (st->xcode)
|
|
|
|
transcode_cleanup(st->xcode);
|
2009-12-08 13:53:53 -05:00
|
|
|
else
|
2010-02-02 15:03:29 -05:00
|
|
|
{
|
|
|
|
free(st->buf);
|
|
|
|
close(st->fd);
|
|
|
|
}
|
2009-12-08 13:53:53 -05:00
|
|
|
|
2009-05-02 14:42:51 -04:00
|
|
|
free(st);
|
|
|
|
}
|
|
|
|
|
2010-09-08 13:37:41 -04:00
|
|
|
static void
|
|
|
|
stream_up_playcount(struct stream_ctx *st)
|
|
|
|
{
|
|
|
|
if (!st->marked
|
|
|
|
&& (st->stream_size > ((st->size * 50) / 100))
|
|
|
|
&& (st->offset > ((st->size * 80) / 100)))
|
|
|
|
{
|
|
|
|
st->marked = 1;
|
|
|
|
db_file_inc_playcount(st->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-02 14:42:51 -04:00
|
|
|
static void
|
|
|
|
stream_chunk_resched_cb(struct evhttp_connection *evcon, void *arg)
|
|
|
|
{
|
2009-05-03 06:19:04 -04:00
|
|
|
struct stream_ctx *st;
|
2009-05-02 14:42:51 -04:00
|
|
|
struct timeval tv;
|
|
|
|
int ret;
|
|
|
|
|
2009-05-03 06:19:04 -04:00
|
|
|
st = (struct stream_ctx *)arg;
|
2009-05-02 14:42:51 -04:00
|
|
|
|
|
|
|
evutil_timerclear(&tv);
|
|
|
|
ret = event_add(&st->ev, &tv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not re-add one-shot event for streaming\n");
|
|
|
|
|
2009-12-08 13:52:51 -05:00
|
|
|
stream_end(st, 0);
|
2009-05-02 14:42:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 04:40:47 -04:00
|
|
|
static void
|
2009-05-01 09:33:48 -04:00
|
|
|
stream_chunk_xcode_cb(int fd, short event, void *arg)
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2009-05-03 06:19:04 -04:00
|
|
|
struct stream_ctx *st;
|
2009-04-25 04:40:47 -04:00
|
|
|
struct timeval tv;
|
2009-05-01 09:33:48 -04:00
|
|
|
int xcoded;
|
2009-04-25 04:40:47 -04:00
|
|
|
int ret;
|
|
|
|
|
2009-05-03 06:19:04 -04:00
|
|
|
st = (struct stream_ctx *)arg;
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2009-05-01 09:33:48 -04:00
|
|
|
xcoded = transcode(st->xcode, st->evbuf, STREAM_CHUNK_SIZE);
|
|
|
|
if (xcoded <= 0)
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2009-05-01 09:33:48 -04:00
|
|
|
if (xcoded == 0)
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Done streaming transcoded file id %d\n", st->id);
|
|
|
|
else
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Transcoding error, file id %d\n", st->id);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2009-12-08 13:52:51 -05:00
|
|
|
stream_end(st, 0);
|
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, "Got %d bytes from transcode; streaming file id %d\n", xcoded, st->id);
|
|
|
|
|
|
|
|
/* Consume transcoded data until we meet start_offset */
|
|
|
|
if (st->start_offset > st->offset)
|
|
|
|
{
|
|
|
|
ret = st->start_offset - st->offset;
|
|
|
|
|
|
|
|
if (ret < xcoded)
|
|
|
|
{
|
|
|
|
evbuffer_drain(st->evbuf, ret);
|
|
|
|
st->offset += ret;
|
|
|
|
|
|
|
|
ret = xcoded - ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
evbuffer_drain(st->evbuf, xcoded);
|
|
|
|
st->offset += xcoded;
|
|
|
|
|
2009-05-02 14:42:51 -04:00
|
|
|
goto consume;
|
2009-05-01 09:33:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = xcoded;
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2009-05-02 14:42:51 -04:00
|
|
|
evhttp_send_reply_chunk_with_cb(st->req, st->evbuf, stream_chunk_resched_cb, st);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
|
|
|
st->offset += ret;
|
|
|
|
|
2010-09-08 13:37:41 -04:00
|
|
|
stream_up_playcount(st);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2009-05-02 14:42:51 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
consume: /* reschedule immediately - consume up to start_offset */
|
2009-05-01 09:33:48 -04:00
|
|
|
evutil_timerclear(&tv);
|
2009-05-02 12:49:31 -04:00
|
|
|
ret = event_add(&st->ev, &tv);
|
2009-05-01 09:33:48 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2009-05-02 14:42:51 -04:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not re-add one-shot event for streaming (xcode)\n");
|
2009-05-01 09:33:48 -04:00
|
|
|
|
2009-12-08 13:52:51 -05:00
|
|
|
stream_end(st, 0);
|
2009-05-02 14:42:51 -04:00
|
|
|
return;
|
2009-05-01 09:33:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
stream_chunk_raw_cb(int fd, short event, void *arg)
|
|
|
|
{
|
2009-05-03 06:19:04 -04:00
|
|
|
struct stream_ctx *st;
|
2009-12-08 15:42:48 -05:00
|
|
|
size_t chunk_size;
|
2009-05-01 09:33:48 -04:00
|
|
|
int ret;
|
|
|
|
|
2009-05-03 06:19:04 -04:00
|
|
|
st = (struct stream_ctx *)arg;
|
2009-05-01 09:33:48 -04:00
|
|
|
|
2009-12-08 15:42:48 -05:00
|
|
|
if (st->end_offset && (st->offset > st->end_offset))
|
|
|
|
{
|
|
|
|
stream_end(st, 0);
|
|
|
|
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;
|
|
|
|
|
2010-02-02 15:03:29 -05:00
|
|
|
ret = read(st->fd, st->buf, 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)
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Done streaming file id %d\n", st->id);
|
|
|
|
else
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Streaming error, file id %d\n", st->id);
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2009-12-08 13:52:51 -05:00
|
|
|
stream_end(st, 0);
|
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);
|
|
|
|
|
2010-02-02 15:03:29 -05:00
|
|
|
evbuffer_add(st->evbuf, st->buf, ret);
|
|
|
|
|
2009-05-02 14:42:51 -04:00
|
|
|
evhttp_send_reply_chunk_with_cb(st->req, st->evbuf, stream_chunk_resched_cb, st);
|
2009-05-01 09:33:48 -04:00
|
|
|
|
|
|
|
st->offset += ret;
|
|
|
|
|
2010-09-08 13:37:41 -04:00
|
|
|
stream_up_playcount(st);
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2009-05-02 12:49:31 -04:00
|
|
|
static void
|
2010-02-14 03:34:29 -05:00
|
|
|
stream_fail_cb(struct evhttp_connection *evcon, void *arg)
|
2009-05-02 12:49:31 -04:00
|
|
|
{
|
2009-05-03 06:19:04 -04:00
|
|
|
struct stream_ctx *st;
|
2009-05-02 12:49:31 -04:00
|
|
|
|
2009-05-03 06:19:04 -04:00
|
|
|
st = (struct stream_ctx *)arg;
|
2009-05-02 12:49:31 -04:00
|
|
|
|
2014-02-03 17:12:19 -05:00
|
|
|
DPRINTF(E_WARN, L_HTTPD, "Connection failed; stopping streaming of file ID %d\n", st->id);
|
2009-05-02 12:49:31 -04:00
|
|
|
|
|
|
|
/* Stop streaming */
|
|
|
|
event_del(&st->ev);
|
|
|
|
|
2009-12-08 13:52:51 -05:00
|
|
|
stream_end(st, 1);
|
2009-05-02 12:49:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-25 04:40:47 -04:00
|
|
|
/* Thread: httpd */
|
|
|
|
void
|
|
|
|
httpd_stream_file(struct evhttp_request *req, int id)
|
|
|
|
{
|
|
|
|
struct media_file_info *mfi;
|
2009-05-03 06:19:04 -04:00
|
|
|
struct stream_ctx *st;
|
2009-05-01 09:33:48 -04:00
|
|
|
void (*stream_cb)(int fd, short event, void *arg);
|
2009-04-25 04:40:47 -04:00
|
|
|
struct stat sb;
|
|
|
|
struct timeval tv;
|
|
|
|
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];
|
2010-02-02 15:02:24 -05:00
|
|
|
int64_t offset;
|
|
|
|
int64_t end_offset;
|
2010-02-10 12:19:32 -05:00
|
|
|
off_t pos;
|
2009-04-25 04:40:47 -04:00
|
|
|
int transcode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
offset = 0;
|
2009-12-08 15:42:48 -05:00
|
|
|
end_offset = 0;
|
2009-04-25 04:40:47 -04:00
|
|
|
param = evhttp_find_header(req->input_headers, "Range");
|
|
|
|
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);
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mfi->data_kind != 0)
|
|
|
|
{
|
|
|
|
evhttp_send_error(req, 500, "Cannot stream radio station");
|
|
|
|
|
2010-02-02 15:04:35 -05:00
|
|
|
goto out_free_mfi;
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2009-05-03 06:19:04 -04:00
|
|
|
st = (struct stream_ctx *)malloc(sizeof(struct stream_ctx));
|
2009-04-25 04:40:47 -04:00
|
|
|
if (!st)
|
|
|
|
{
|
2009-05-03 06:19:04 -04:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Out of memory for struct stream_ctx\n");
|
2009-04-25 04:40:47 -04:00
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal Server Error");
|
|
|
|
|
2010-02-02 15:04:35 -05:00
|
|
|
goto out_free_mfi;
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
2009-05-03 06:19:04 -04:00
|
|
|
memset(st, 0, sizeof(struct stream_ctx));
|
2010-02-02 15:04:35 -05:00
|
|
|
st->fd = -1;
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2009-05-01 09:33:48 -04:00
|
|
|
transcode = transcode_needed(req->input_headers, mfi->codectype);
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-05-01 09:33:48 -04:00
|
|
|
stream_cb = stream_chunk_xcode_cb;
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2014-03-11 18:20:29 -04:00
|
|
|
ret = transcode_setup(&st->xcode, mfi, &st->size, 1);
|
|
|
|
if (ret < 0)
|
2009-05-01 09:33:48 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_HTTPD, "Transcoding setup failed, aborting streaming\n");
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2009-05-01 09:33:48 -04:00
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal Server Error");
|
|
|
|
|
2010-02-02 15:04:35 -05:00
|
|
|
goto out_free_st;
|
2009-05-01 09:33:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!evhttp_find_header(req->output_headers, "Content-Type"))
|
|
|
|
evhttp_add_header(req->output_headers, "Content-Type", "audio/wav");
|
|
|
|
}
|
|
|
|
else
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2009-05-01 09:33:48 -04:00
|
|
|
/* Stream the raw file */
|
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
|
|
|
|
2010-02-02 15:03:29 -05:00
|
|
|
st->buf = (uint8_t *)malloc(STREAM_CHUNK_SIZE);
|
|
|
|
if (!st->buf)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Out of memory for raw streaming buffer\n");
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal Server Error");
|
|
|
|
|
2010-02-02 15:04:35 -05:00
|
|
|
goto out_free_st;
|
2010-02-02 15:03:29 -05:00
|
|
|
}
|
|
|
|
|
2009-05-01 09:33:48 -04:00
|
|
|
stream_cb = stream_chunk_raw_cb;
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2009-05-01 09:33:48 -04: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));
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
|
|
|
|
|
2010-02-02 15:04:35 -05:00
|
|
|
goto out_cleanup;
|
2009-05-01 09:33:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = stat(mfi->path, &sb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not stat() %s: %s\n", mfi->path, strerror(errno));
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
|
|
|
|
|
2010-02-02 15:04:35 -05:00
|
|
|
goto out_cleanup;
|
2009-05-01 09:33:48 -04:00
|
|
|
}
|
|
|
|
st->size = sb.st_size;
|
|
|
|
|
2010-02-10 12:19:32 -05:00
|
|
|
pos = lseek(st->fd, offset, SEEK_SET);
|
|
|
|
if (pos == (off_t) -1)
|
2009-05-01 09:33:48 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not seek into %s: %s\n", mfi->path, strerror(errno));
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_BADREQUEST, "Bad Request");
|
|
|
|
|
2010-02-02 15:04:35 -05:00
|
|
|
goto out_cleanup;
|
2009-05-01 09:33:48 -04:00
|
|
|
}
|
|
|
|
st->offset = offset;
|
2009-12-08 15:42:48 -05:00
|
|
|
st->end_offset = end_offset;
|
2009-05-01 09:33:48 -04:00
|
|
|
|
2009-12-08 15:04:30 -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.
|
|
|
|
*/
|
|
|
|
if (mfi->has_video)
|
|
|
|
{
|
|
|
|
/* Front Row and others expect video/<type> */
|
|
|
|
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
|
|
|
|
{
|
|
|
|
evhttp_remove_header(req->output_headers, "Content-Type");
|
|
|
|
evhttp_add_header(req->output_headers, "Content-Type", buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* 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!).
|
|
|
|
*/
|
|
|
|
else if (!evhttp_find_header(req->output_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
|
|
|
|
evhttp_add_header(req->output_headers, "Content-Type", buf);
|
|
|
|
}
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2009-05-01 09:33:48 -04:00
|
|
|
st->evbuf = evbuffer_new();
|
|
|
|
if (!st->evbuf)
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2009-05-01 09:33:48 -04:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not allocate an evbuffer for streaming\n");
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2009-05-01 09:33:48 -04:00
|
|
|
evhttp_clear_headers(req->output_headers);
|
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal Server Error");
|
2009-04-25 04:40:47 -04:00
|
|
|
|
2010-02-02 15:04:35 -05:00
|
|
|
goto out_cleanup;
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2009-05-01 09:33:48 -04:00
|
|
|
ret = evbuffer_expand(st->evbuf, STREAM_CHUNK_SIZE);
|
|
|
|
if (ret != 0)
|
2009-04-25 04:40:47 -04:00
|
|
|
{
|
2009-05-01 09:33:48 -04:00
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not expand evbuffer for streaming\n");
|
2009-04-25 04:40:47 -04:00
|
|
|
|
|
|
|
evhttp_clear_headers(req->output_headers);
|
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal Server Error");
|
|
|
|
|
2010-02-02 15:04:35 -05:00
|
|
|
goto out_cleanup;
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
evutil_timerclear(&tv);
|
2009-05-02 12:49:31 -04:00
|
|
|
event_set(&st->ev, -1, EV_TIMEOUT, stream_cb, st);
|
|
|
|
event_base_set(evbase_httpd, &st->ev);
|
|
|
|
ret = event_add(&st->ev, &tv);
|
2009-04-25 04:40:47 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not add one-shot event for streaming\n");
|
|
|
|
|
|
|
|
evhttp_clear_headers(req->output_headers);
|
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal Server Error");
|
|
|
|
|
2010-02-02 15:04:35 -05:00
|
|
|
goto out_cleanup;
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
st->id = mfi->id;
|
2009-05-01 09:33:48 -04:00
|
|
|
st->start_offset = offset;
|
2009-12-26 03:57:37 -05:00
|
|
|
st->stream_size = st->size;
|
2009-04-25 04:40:47 -04:00
|
|
|
st->req = req;
|
|
|
|
|
2009-12-08 15:42:48 -05:00
|
|
|
if ((offset == 0) && (end_offset == 0))
|
2009-12-31 10:47:24 -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
|
|
|
|
evhttp_add_header(req->output_headers, "Content-Length", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
evhttp_send_reply_start(req, HTTP_OK, "OK");
|
|
|
|
}
|
2009-04-25 04:40:47 -04:00
|
|
|
else
|
|
|
|
{
|
2009-12-26 03:57:37 -05:00
|
|
|
if (offset > 0)
|
|
|
|
st->stream_size -= offset;
|
|
|
|
if (end_offset > 0)
|
|
|
|
st->stream_size -= (st->size - end_offset);
|
|
|
|
|
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
|
|
|
|
evhttp_add_header(req->output_headers, "Content-Range", buf);
|
|
|
|
|
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
|
|
|
|
evhttp_add_header(req->output_headers, "Content-Length", buf);
|
|
|
|
|
2009-04-25 04:40:47 -04:00
|
|
|
evhttp_send_reply_start(req, 206, "Partial Content");
|
|
|
|
}
|
|
|
|
|
2010-02-02 14:59:46 -05:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
|
|
|
if (!transcode)
|
|
|
|
{
|
|
|
|
/* Hint the OS */
|
|
|
|
posix_fadvise(st->fd, st->start_offset, st->stream_size, POSIX_FADV_WILLNEED);
|
|
|
|
posix_fadvise(st->fd, st->start_offset, st->stream_size, POSIX_FADV_SEQUENTIAL);
|
|
|
|
posix_fadvise(st->fd, st->start_offset, st->stream_size, POSIX_FADV_NOREUSE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-14 03:34:29 -05:00
|
|
|
evhttp_connection_set_closecb(req->evcon, 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;
|
|
|
|
|
|
|
|
out_cleanup:
|
|
|
|
if (st->evbuf)
|
|
|
|
evbuffer_free(st->evbuf);
|
|
|
|
if (st->xcode)
|
|
|
|
transcode_cleanup(st->xcode);
|
|
|
|
if (st->buf)
|
|
|
|
free(st->buf);
|
|
|
|
if (st->fd > 0)
|
|
|
|
close(st->fd);
|
|
|
|
out_free_st:
|
|
|
|
free(st);
|
|
|
|
out_free_mfi:
|
|
|
|
free_mfi(mfi, 0);
|
2009-04-25 04:40:47 -04:00
|
|
|
}
|
|
|
|
|
2010-05-03 12:19:41 -04:00
|
|
|
/* Thread: httpd */
|
|
|
|
void
|
|
|
|
httpd_send_reply(struct evhttp_request *req, int code, const char *reason, struct evbuffer *evbuf)
|
|
|
|
{
|
|
|
|
unsigned char outbuf[128 * 1024];
|
|
|
|
z_stream strm;
|
|
|
|
struct evbuffer *gzbuf;
|
|
|
|
const char *param;
|
|
|
|
int flush;
|
|
|
|
int zret;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!evbuf || (EVBUFFER_LENGTH(evbuf) == 0))
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Not gzipping body-less reply\n");
|
|
|
|
|
|
|
|
goto no_gzip;
|
|
|
|
}
|
|
|
|
|
|
|
|
param = evhttp_find_header(req->input_headers, "Accept-Encoding");
|
|
|
|
if (!param)
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Not gzipping; no Accept-Encoding header\n");
|
|
|
|
|
|
|
|
goto no_gzip;
|
|
|
|
}
|
|
|
|
else if (!strstr(param, "gzip") && !strstr(param, "*"))
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Not gzipping; gzip not in Accept-Encoding (%s)\n", param);
|
|
|
|
|
|
|
|
goto no_gzip;
|
|
|
|
}
|
|
|
|
|
|
|
|
gzbuf = evbuffer_new();
|
|
|
|
if (!gzbuf)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not allocate evbuffer for gzipped reply\n");
|
|
|
|
|
|
|
|
goto no_gzip;
|
|
|
|
}
|
|
|
|
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
|
|
|
|
/* Set up a gzip stream (the "+ 16" in 15 + 16), instead of a zlib stream (default) */
|
|
|
|
zret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
|
|
|
|
if (zret != Z_OK)
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "zlib setup failed: %s\n", zError(zret));
|
|
|
|
|
|
|
|
goto out_fail_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
strm.next_in = EVBUFFER_DATA(evbuf);
|
|
|
|
strm.avail_in = EVBUFFER_LENGTH(evbuf);
|
|
|
|
|
|
|
|
flush = Z_NO_FLUSH;
|
|
|
|
|
|
|
|
/* 2 iterations: Z_NO_FLUSH until input is consumed, then Z_FINISH */
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
strm.next_out = outbuf;
|
|
|
|
strm.avail_out = sizeof(outbuf);
|
|
|
|
|
|
|
|
zret = deflate(&strm, flush);
|
|
|
|
if (zret == Z_STREAM_ERROR)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not deflate data: %s\n", strm.msg);
|
|
|
|
|
|
|
|
goto out_fail_gz;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = evbuffer_add(gzbuf, outbuf, sizeof(outbuf) - strm.avail_out);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Out of memory adding gzipped data to evbuffer\n");
|
|
|
|
|
|
|
|
goto out_fail_gz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (strm.avail_out == 0);
|
|
|
|
|
|
|
|
if (flush == Z_FINISH)
|
|
|
|
break;
|
|
|
|
|
|
|
|
flush = Z_FINISH;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zret != Z_STREAM_END)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Compressed data not finalized!\n");
|
|
|
|
|
|
|
|
goto out_fail_gz;
|
|
|
|
}
|
|
|
|
|
|
|
|
deflateEnd(&strm);
|
|
|
|
|
|
|
|
evhttp_add_header(req->output_headers, "Content-Encoding", "gzip");
|
|
|
|
evhttp_send_reply(req, code, reason, gzbuf);
|
|
|
|
|
|
|
|
evbuffer_free(gzbuf);
|
|
|
|
|
|
|
|
/* Drain original buffer, as would be after evhttp_send_reply() */
|
|
|
|
evbuffer_drain(evbuf, EVBUFFER_LENGTH(evbuf));
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
out_fail_gz:
|
|
|
|
deflateEnd(&strm);
|
|
|
|
out_fail_init:
|
|
|
|
evbuffer_free(gzbuf);
|
|
|
|
no_gzip:
|
|
|
|
evhttp_send_reply(req, code, reason, evbuf);
|
|
|
|
}
|
|
|
|
|
2009-04-22 15:25:41 -04:00
|
|
|
/* Thread: httpd */
|
|
|
|
static int
|
|
|
|
path_is_legal(char *path)
|
|
|
|
{
|
|
|
|
return strncmp(WEBFACE_ROOT, path, strlen(WEBFACE_ROOT));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread: httpd */
|
|
|
|
static void
|
|
|
|
redirect_to_index(struct evhttp_request *req, char *uri)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
int slashed;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
slashed = (uri[strlen(uri) - 1] == '/');
|
|
|
|
|
|
|
|
ret = snprintf(buf, sizeof(buf), "%s%sindex.html", uri, (slashed) ? "" : "/");
|
|
|
|
if ((ret < 0) || (ret >= sizeof(buf)))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Redirection URL exceeds buffer length\n");
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
evhttp_add_header(req->output_headers, "Location", buf);
|
|
|
|
evhttp_send_reply(req, HTTP_MOVETEMP, "Moved", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread: httpd */
|
|
|
|
static void
|
|
|
|
serve_file(struct evhttp_request *req, char *uri)
|
|
|
|
{
|
|
|
|
char *ext;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char *deref;
|
|
|
|
char *ctype;
|
2009-05-01 14:59:32 -04:00
|
|
|
char *passwd;
|
2009-04-22 15:25:41 -04:00
|
|
|
struct evbuffer *evbuf;
|
|
|
|
struct stat sb;
|
|
|
|
int fd;
|
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
2009-05-01 14:59:32 -04:00
|
|
|
/* Check authentication */
|
|
|
|
passwd = cfg_getstr(cfg_getsec(cfg, "general"), "admin_password");
|
|
|
|
if (passwd)
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Checking web interface authentication\n");
|
|
|
|
|
|
|
|
ret = httpd_basic_auth(req, "admin", passwd, PACKAGE " web interface");
|
|
|
|
if (ret != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Authentication successful\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-08 13:41:17 -04:00
|
|
|
if ((strcmp(req->remote_host, "::1") != 0)
|
|
|
|
&& (strcmp(req->remote_host, "127.0.0.1") != 0))
|
2009-05-01 14:59:32 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Remote web interface request denied; no password set\n");
|
|
|
|
|
|
|
|
evhttp_send_error(req, 403, "Forbidden");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-22 15:25:41 -04:00
|
|
|
ret = snprintf(path, sizeof(path), "%s%s", WEBFACE_ROOT, uri + 1); /* skip starting '/' */
|
|
|
|
if ((ret < 0) || (ret >= sizeof(path)))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Request exceeds PATH_MAX: %s\n", uri);
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = lstat(path, &sb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not lstat() %s: %s\n", path, strerror(errno));
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(sb.st_mode))
|
|
|
|
{
|
|
|
|
redirect_to_index(req, uri);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (S_ISLNK(sb.st_mode))
|
|
|
|
{
|
2010-01-09 07:44:10 -05:00
|
|
|
deref = m_realpath(path);
|
2009-04-22 15:25:41 -04:00
|
|
|
if (!deref)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not dereference %s: %s\n", path, strerror(errno));
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(deref) + 1 > PATH_MAX)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Dereferenced path exceeds PATH_MAX: %s\n", path);
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
|
|
|
|
|
|
|
|
free(deref);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(path, deref);
|
|
|
|
free(deref);
|
|
|
|
|
|
|
|
ret = stat(path, &sb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not stat() %s: %s\n", path, strerror(errno));
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(sb.st_mode))
|
|
|
|
{
|
|
|
|
redirect_to_index(req, uri);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path_is_legal(path) != 0)
|
|
|
|
{
|
|
|
|
evhttp_send_error(req, 403, "Forbidden");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
evbuf = evbuffer_new();
|
|
|
|
if (!evbuf)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not create evbuffer\n");
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not open %s: %s\n", path, strerror(errno));
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_NOTFOUND, "Not Found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-02 15:05:32 -05:00
|
|
|
/* FIXME: this is broken, if we ever need to serve files here,
|
|
|
|
* this must be fixed.
|
|
|
|
*/
|
2009-04-22 15:25:41 -04:00
|
|
|
ret = evbuffer_read(evbuf, fd, sb.st_size);
|
|
|
|
close(fd);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Could not read file into evbuffer\n");
|
|
|
|
|
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
evhttp_add_header(req->output_headers, "Content-Type", ctype);
|
|
|
|
|
|
|
|
evhttp_send_reply(req, HTTP_OK, "OK", evbuf);
|
|
|
|
|
|
|
|
evbuffer_free(evbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread: httpd */
|
|
|
|
static void
|
2009-05-03 06:19:46 -04:00
|
|
|
httpd_gen_cb(struct evhttp_request *req, void *arg)
|
2009-04-22 15:25:41 -04:00
|
|
|
{
|
|
|
|
const char *req_uri;
|
|
|
|
char *uri;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
req_uri = evhttp_request_uri(req);
|
|
|
|
if (!req_uri)
|
|
|
|
{
|
|
|
|
redirect_to_index(req, "/");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uri = strdup(req_uri);
|
|
|
|
ptr = strchr(uri, '?');
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_HTTPD, "Found query string\n");
|
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = uri;
|
|
|
|
uri = evhttp_decode_uri(uri);
|
|
|
|
free(ptr);
|
|
|
|
|
2009-04-24 09:42:20 -04:00
|
|
|
/* Dispatch protocol-specific URIs */
|
|
|
|
if (rsp_is_request(req, uri))
|
|
|
|
{
|
|
|
|
rsp_request(req);
|
|
|
|
|
2010-01-29 16:33:32 -05:00
|
|
|
goto out;
|
2009-04-28 11:52:11 -04:00
|
|
|
}
|
|
|
|
else if (daap_is_request(req, uri))
|
|
|
|
{
|
|
|
|
daap_request(req);
|
|
|
|
|
2010-01-29 16:43:39 -05:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (dacp_is_request(req, uri))
|
|
|
|
{
|
|
|
|
dacp_request(req);
|
|
|
|
|
2010-01-29 16:33:32 -05:00
|
|
|
goto out;
|
2009-04-24 09:42:20 -04:00
|
|
|
}
|
|
|
|
|
2010-01-26 12:15:08 -05:00
|
|
|
DPRINTF(E_DBG, L_HTTPD, "HTTP request: %s\n", uri);
|
|
|
|
|
2009-04-24 09:42:20 -04:00
|
|
|
/* Serve web interface files */
|
2009-04-22 15:25:41 -04:00
|
|
|
serve_file(req, uri);
|
|
|
|
|
2010-01-29 16:33:32 -05:00
|
|
|
out:
|
2009-04-22 15:25:41 -04:00
|
|
|
free(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread: httpd */
|
|
|
|
static void *
|
|
|
|
httpd(void *arg)
|
|
|
|
{
|
2009-06-07 12:58:02 -04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = db_perthread_init();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_HTTPD, "Error: DB init failed\n");
|
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2009-04-22 15:25:41 -04:00
|
|
|
event_base_dispatch(evbase_httpd);
|
|
|
|
|
|
|
|
if (!httpd_exit)
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "HTTPd event loop terminated ahead of time!\n");
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
db_perthread_deinit();
|
|
|
|
|
2009-04-22 15:25:41 -04:00
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread: httpd */
|
|
|
|
static void
|
|
|
|
exit_cb(int fd, short event, void *arg)
|
|
|
|
{
|
|
|
|
event_base_loopbreak(evbase_httpd);
|
|
|
|
|
|
|
|
httpd_exit = 1;
|
|
|
|
}
|
|
|
|
|
2009-05-02 11:20:33 -04:00
|
|
|
char *
|
|
|
|
httpd_fixup_uri(struct evhttp_request *req)
|
|
|
|
{
|
|
|
|
const char *ua;
|
|
|
|
const char *uri;
|
|
|
|
const char *u;
|
|
|
|
const char *q;
|
|
|
|
char *fixed;
|
|
|
|
char *f;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
uri = evhttp_request_uri(req);
|
|
|
|
if (!uri)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* No query string, nothing to do */
|
|
|
|
q = strchr(uri, '?');
|
|
|
|
if (!q)
|
|
|
|
return strdup(uri);
|
|
|
|
|
|
|
|
ua = evhttp_find_header(req->input_headers, "User-Agent");
|
|
|
|
if (!ua)
|
|
|
|
return strdup(uri);
|
|
|
|
|
|
|
|
if ((strncmp(ua, "iTunes", strlen("iTunes")) != 0)
|
2009-12-30 12:49:52 -05:00
|
|
|
&& (strncmp(ua, "Remote", strlen("Remote")) != 0)
|
2009-05-02 11:20:33 -04:00
|
|
|
&& (strncmp(ua, "Roku", strlen("Roku")) != 0))
|
|
|
|
return strdup(uri);
|
|
|
|
|
|
|
|
/* Reencode + as %2B and space as + in the query,
|
|
|
|
which iTunes and Roku devices don't do */
|
|
|
|
len = strlen(uri);
|
|
|
|
|
|
|
|
u = q;
|
|
|
|
while (*u)
|
|
|
|
{
|
|
|
|
if (*u == '+')
|
|
|
|
len += 2;
|
|
|
|
|
|
|
|
u++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fixed = (char *)malloc(len + 1);
|
|
|
|
if (!fixed)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
strncpy(fixed, uri, q - uri);
|
|
|
|
|
|
|
|
f = fixed + (q - uri);
|
|
|
|
while (*q)
|
|
|
|
{
|
|
|
|
switch (*q)
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
*f = '%';
|
|
|
|
f++;
|
|
|
|
*f = '2';
|
|
|
|
f++;
|
|
|
|
*f = 'B';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
*f = '+';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*f = *q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
q++;
|
|
|
|
f++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*f = '\0';
|
|
|
|
|
|
|
|
return fixed;
|
|
|
|
}
|
|
|
|
|
2010-09-08 13:22:26 -04:00
|
|
|
static const char *http_reply_401 = "<html><head><title>401 Unauthorized</title></head><body>Authorization required</body></html>";
|
2009-05-01 14:58:15 -04:00
|
|
|
|
|
|
|
int
|
|
|
|
httpd_basic_auth(struct evhttp_request *req, char *user, char *passwd, char *realm)
|
|
|
|
{
|
|
|
|
struct evbuffer *evbuf;
|
|
|
|
char *header;
|
|
|
|
const char *auth;
|
|
|
|
char *authuser;
|
|
|
|
char *authpwd;
|
|
|
|
int len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
auth = evhttp_find_header(req->input_headers, "Authorization");
|
|
|
|
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 ");
|
|
|
|
|
|
|
|
authuser = b64_decode(auth);
|
|
|
|
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:
|
|
|
|
len = strlen(realm) + strlen("Basic realm=") + 3;
|
|
|
|
header = (char *)malloc(len);
|
|
|
|
if (!header)
|
|
|
|
{
|
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal Server Error");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = snprintf(header, len, "Basic realm=\"%s\"", realm);
|
|
|
|
if ((ret < 0) || (ret >= len))
|
|
|
|
{
|
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal Server Error");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
evbuf = evbuffer_new();
|
|
|
|
if (!evbuf)
|
|
|
|
{
|
|
|
|
evhttp_send_error(req, HTTP_SERVUNAVAIL, "Internal Server Error");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
evhttp_add_header(req->output_headers, "WWW-Authenticate", header);
|
|
|
|
evbuffer_add(evbuf, http_reply_401, strlen(http_reply_401));
|
|
|
|
evhttp_send_reply(req, 401, "Unauthorized", evbuf);
|
|
|
|
|
|
|
|
free(header);
|
|
|
|
evbuffer_free(evbuf);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-04-22 15:25:41 -04:00
|
|
|
/* Thread: main */
|
|
|
|
int
|
|
|
|
httpd_init(void)
|
|
|
|
{
|
|
|
|
unsigned short port;
|
2011-03-20 07:19:39 -04:00
|
|
|
int v6enabled;
|
2009-04-22 15:25:41 -04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
httpd_exit = 0;
|
|
|
|
|
2011-03-20 07:19:39 -04:00
|
|
|
v6enabled = cfg_getbool(cfg_getsec(cfg, "general"), "ipv6");
|
|
|
|
|
2010-04-24 06:59:29 -04:00
|
|
|
evbase_httpd = event_base_new();
|
|
|
|
if (!evbase_httpd)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Could not create an event base\n");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-04-24 09:42:20 -04:00
|
|
|
ret = rsp_init();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "RSP protocol init failed\n");
|
|
|
|
|
2010-04-24 06:59:29 -04:00
|
|
|
goto rsp_fail;
|
2009-04-24 09:42:20 -04:00
|
|
|
}
|
|
|
|
|
2009-04-28 11:52:11 -04:00
|
|
|
ret = daap_init();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "DAAP protocol init failed\n");
|
|
|
|
|
|
|
|
goto daap_fail;
|
|
|
|
}
|
|
|
|
|
2010-01-29 16:43:39 -05:00
|
|
|
ret = dacp_init();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "DACP protocol init failed\n");
|
|
|
|
|
|
|
|
goto dacp_fail;
|
|
|
|
}
|
|
|
|
|
2010-02-04 12:52:13 -05:00
|
|
|
#ifdef USE_EVENTFD
|
|
|
|
exit_efd = eventfd(0, EFD_CLOEXEC);
|
|
|
|
if (exit_efd < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Could not create eventfd: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
goto pipe_fail;
|
|
|
|
}
|
2010-01-09 07:41:14 -05:00
|
|
|
#else
|
2010-02-04 12:52:13 -05:00
|
|
|
# if defined(__linux__)
|
|
|
|
ret = pipe2(exit_pipe, O_CLOEXEC);
|
|
|
|
# else
|
2010-01-09 07:41:14 -05:00
|
|
|
ret = pipe(exit_pipe);
|
2010-02-04 12:52:13 -05:00
|
|
|
# endif
|
2009-04-22 15:25:41 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Could not create pipe: %s\n", strerror(errno));
|
|
|
|
|
2009-04-24 09:42:20 -04:00
|
|
|
goto pipe_fail;
|
2009-04-22 15:25:41 -04:00
|
|
|
}
|
2010-02-04 12:52:13 -05:00
|
|
|
#endif /* USE_EVENTFD */
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2010-02-04 12:52:13 -05:00
|
|
|
#ifdef USE_EVENTFD
|
|
|
|
event_set(&exitev, exit_efd, EV_READ, exit_cb, NULL);
|
|
|
|
#else
|
2009-04-22 15:25:41 -04:00
|
|
|
event_set(&exitev, exit_pipe[0], EV_READ, exit_cb, NULL);
|
2010-02-04 12:52:13 -05:00
|
|
|
#endif
|
2009-04-22 15:25:41 -04:00
|
|
|
event_base_set(evbase_httpd, &exitev);
|
|
|
|
event_add(&exitev, NULL);
|
|
|
|
|
|
|
|
evhttpd = evhttp_new(evbase_httpd);
|
|
|
|
if (!evhttpd)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Could not create HTTP server\n");
|
|
|
|
|
|
|
|
goto evhttp_fail;
|
|
|
|
}
|
|
|
|
|
2010-03-19 14:06:47 -04:00
|
|
|
port = cfg_getint(cfg_getsec(cfg, "library"), "port");
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2010-05-14 11:36:58 -04:00
|
|
|
/* We are binding v6 and v4 separately, and we allow v6 to fail
|
|
|
|
* as IPv6 might not be supported on the system.
|
|
|
|
* We still warn about the failure, in case there's another issue.
|
|
|
|
*/
|
2009-04-22 15:25:41 -04:00
|
|
|
ret = evhttp_bind_socket(evhttpd, "0.0.0.0", port);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2010-05-14 11:36:58 -04:00
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Could not bind INADDR_ANY:%d\n", port);
|
2009-04-22 15:25:41 -04:00
|
|
|
|
2010-05-14 11:36:58 -04:00
|
|
|
goto bind_fail;
|
2009-04-22 15:25:41 -04:00
|
|
|
}
|
|
|
|
|
2011-03-20 07:19:39 -04:00
|
|
|
if (v6enabled)
|
|
|
|
{
|
|
|
|
ret = evhttp_bind_socket(evhttpd, "::", port);
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_WARN, L_HTTPD, "Could not bind IN6ADDR_ANY:%d (that's OK)\n", port);
|
|
|
|
}
|
2010-09-08 13:47:28 -04:00
|
|
|
|
2009-05-03 06:19:46 -04:00
|
|
|
evhttp_set_gencb(evhttpd, httpd_gen_cb, NULL);
|
2009-04-22 15:25:41 -04:00
|
|
|
|
|
|
|
ret = pthread_create(&tid_httpd, NULL, httpd, NULL);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Could not spawn HTTPd thread: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
goto thread_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
thread_fail:
|
|
|
|
bind_fail:
|
|
|
|
evhttp_free(evhttpd);
|
|
|
|
evhttp_fail:
|
2010-02-04 12:52:13 -05:00
|
|
|
#ifdef USE_EVENTFD
|
|
|
|
close(exit_efd);
|
|
|
|
#else
|
2009-04-22 15:25:41 -04:00
|
|
|
close(exit_pipe[0]);
|
|
|
|
close(exit_pipe[1]);
|
2010-02-04 12:52:13 -05:00
|
|
|
#endif
|
2009-04-24 09:42:20 -04:00
|
|
|
pipe_fail:
|
2010-01-29 16:43:39 -05:00
|
|
|
dacp_deinit();
|
|
|
|
dacp_fail:
|
2009-04-28 11:52:11 -04:00
|
|
|
daap_deinit();
|
|
|
|
daap_fail:
|
2009-04-24 09:42:20 -04:00
|
|
|
rsp_deinit();
|
2010-04-24 06:59:29 -04:00
|
|
|
rsp_fail:
|
|
|
|
event_base_free(evbase_httpd);
|
2009-04-22 15:25:41 -04:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread: main */
|
|
|
|
void
|
|
|
|
httpd_deinit(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2010-02-04 12:52:13 -05:00
|
|
|
#ifdef USE_EVENTFD
|
|
|
|
ret = eventfd_write(exit_efd, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Could not send exit event: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int dummy = 42;
|
|
|
|
|
2009-04-22 15:25:41 -04:00
|
|
|
ret = write(exit_pipe[1], &dummy, sizeof(dummy));
|
|
|
|
if (ret != sizeof(dummy))
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Could not write to exit fd: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2010-02-04 12:52:13 -05:00
|
|
|
#endif
|
2009-04-22 15:25:41 -04:00
|
|
|
|
|
|
|
ret = pthread_join(tid_httpd, NULL);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_HTTPD, "Could not join HTTPd thread: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-24 09:42:20 -04:00
|
|
|
rsp_deinit();
|
2010-01-29 16:43:39 -05:00
|
|
|
dacp_deinit();
|
2009-04-28 11:52:11 -04:00
|
|
|
daap_deinit();
|
2009-04-24 09:42:20 -04:00
|
|
|
|
2010-02-04 12:52:13 -05:00
|
|
|
#ifdef USE_EVENTFD
|
|
|
|
close(exit_efd);
|
|
|
|
#else
|
2009-04-22 15:25:41 -04:00
|
|
|
close(exit_pipe[0]);
|
|
|
|
close(exit_pipe[1]);
|
2010-02-04 12:52:13 -05:00
|
|
|
#endif
|
2009-04-22 15:25:41 -04:00
|
|
|
evhttp_free(evhttpd);
|
|
|
|
event_base_free(evbase_httpd);
|
|
|
|
}
|