2009-04-28 11:48:57 -04:00
|
|
|
/*
|
2018-09-30 16:39:53 -04:00
|
|
|
* Copyright (C) 2016-2018 Espen Jürgensen <espenjurgensen@gmail.com>
|
2011-03-31 12:05:41 -04:00
|
|
|
* Copyright (C) 2009-2011 Julien BLACHE <jb@jblache.org>
|
2010-08-14 07:57:49 -04:00
|
|
|
* Copyright (C) 2010 Kai Elwert <elwertk@googlemail.com>
|
2009-04-28 11:48:57 -04:00
|
|
|
*
|
|
|
|
* Adapted from mt-daapd:
|
|
|
|
* Copyright (C) 2003-2007 Ron Pedde <ron@pedde.com>
|
|
|
|
*
|
|
|
|
* 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>
|
2017-10-26 17:01:07 -04:00
|
|
|
#include <stdbool.h>
|
2009-04-28 11:48:57 -04:00
|
|
|
#include <string.h>
|
2009-04-28 16:51:52 -04:00
|
|
|
#include <errno.h>
|
2009-04-28 11:48:57 -04:00
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdint.h>
|
2010-01-04 12:00:05 -05:00
|
|
|
#include <inttypes.h>
|
2014-08-23 19:23:23 -04:00
|
|
|
#include <time.h>
|
2010-08-14 07:57:49 -04:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include <uninorm.h>
|
2014-08-23 19:23:23 -04:00
|
|
|
#include <unistd.h>
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
#include <event2/event.h>
|
2020-01-10 13:22:42 -05:00
|
|
|
#include <event2/bufferevent.h>
|
2017-11-04 16:24:42 -04:00
|
|
|
|
|
|
|
#include "httpd_daap.h"
|
2009-05-08 11:46:32 -04:00
|
|
|
#include "logger.h"
|
2009-06-07 12:58:02 -04:00
|
|
|
#include "db.h"
|
2009-04-28 11:48:57 -04:00
|
|
|
#include "conffile.h"
|
2009-04-30 08:25:52 -04:00
|
|
|
#include "misc.h"
|
2009-05-01 09:33:48 -04:00
|
|
|
#include "transcode.h"
|
2010-03-07 05:11:09 -05:00
|
|
|
#include "artwork.h"
|
2009-06-02 09:51:38 -04:00
|
|
|
#include "daap_query.h"
|
2011-03-08 13:22:16 -05:00
|
|
|
#include "dmap_common.h"
|
2014-11-10 16:53:08 -05:00
|
|
|
#include "cache.h"
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2014-05-29 17:22:00 -04:00
|
|
|
|
2010-03-06 04:30:59 -05:00
|
|
|
/* httpd event base, from httpd.c */
|
|
|
|
extern struct event_base *evbase_httpd;
|
|
|
|
|
2015-10-03 04:00:42 -04:00
|
|
|
/* Max number of sessions and session timeout
|
|
|
|
* Many clients (including iTunes) don't seem to respect the timeout capability
|
|
|
|
* that we announce, and just keep using the same session. Therefore we take a
|
|
|
|
* lenient approach to actually timing out: We wait an entire week, and to
|
|
|
|
* avoid running a timer for that long, we only check for expiration when adding
|
|
|
|
* new sessions - see daap_session_cleanup().
|
|
|
|
*/
|
|
|
|
#define DAAP_SESSION_MAX 200
|
|
|
|
#define DAAP_SESSION_TIMEOUT 604800 // One week in seconds
|
2012-01-02 02:15:18 -05:00
|
|
|
/* We announce this timeout to the client when returning server capabilities */
|
2015-10-03 04:00:42 -04:00
|
|
|
#define DAAP_SESSION_TIMEOUT_CAPABILITY 1800 // 30 minutes
|
2011-09-10 12:37:16 -04:00
|
|
|
/* Update requests refresh interval in seconds */
|
2012-01-02 02:15:18 -05:00
|
|
|
#define DAAP_UPDATE_REFRESH 0
|
2010-03-06 04:30:59 -05:00
|
|
|
|
2015-03-30 15:10:59 -04:00
|
|
|
/* Database number for the Radio item */
|
2015-03-16 18:33:42 -04:00
|
|
|
#define DAAP_DB_RADIO 2
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
/* Errors that the reply handlers may return */
|
|
|
|
enum daap_reply_result
|
|
|
|
{
|
|
|
|
DAAP_REPLY_LOGOUT = 4,
|
|
|
|
DAAP_REPLY_NONE = 3,
|
|
|
|
DAAP_REPLY_NO_CONTENT = 2,
|
|
|
|
DAAP_REPLY_OK_NO_GZIP = 1,
|
|
|
|
DAAP_REPLY_OK = 0,
|
|
|
|
DAAP_REPLY_NO_CONNECTION = -1,
|
|
|
|
DAAP_REPLY_ERROR = -2,
|
|
|
|
DAAP_REPLY_FORBIDDEN = -3,
|
|
|
|
DAAP_REPLY_BAD_REQUEST = -4,
|
|
|
|
DAAP_REPLY_SERVUNAVAIL = -5,
|
2009-04-28 11:48:57 -04:00
|
|
|
};
|
|
|
|
|
2010-01-20 12:24:52 -05:00
|
|
|
struct daap_session {
|
|
|
|
int id;
|
2015-10-03 04:00:42 -04:00
|
|
|
time_t mtime;
|
2017-10-26 17:01:07 -04:00
|
|
|
bool is_remote;
|
2015-02-24 16:58:47 -05:00
|
|
|
|
|
|
|
struct daap_session *next;
|
2010-01-20 12:24:52 -05:00
|
|
|
};
|
|
|
|
|
2010-01-28 13:20:00 -05:00
|
|
|
struct daap_update_request {
|
|
|
|
struct evhttp_request *req;
|
|
|
|
|
2011-09-10 12:37:16 -04:00
|
|
|
/* Refresh tiemout */
|
2015-03-06 15:22:05 -05:00
|
|
|
struct event *timeout;
|
2011-09-10 12:37:16 -04:00
|
|
|
|
2010-01-28 13:20:00 -05:00
|
|
|
struct daap_update_request *next;
|
|
|
|
};
|
|
|
|
|
2010-08-14 07:57:49 -04:00
|
|
|
struct sort_ctx {
|
|
|
|
struct evbuffer *headerlist;
|
|
|
|
int16_t mshc;
|
|
|
|
uint32_t mshi;
|
|
|
|
uint32_t mshn;
|
|
|
|
uint32_t misc_mshn;
|
|
|
|
};
|
|
|
|
|
2010-02-08 13:02:58 -05:00
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
/* Default meta tags if not provided in the query */
|
|
|
|
static char *default_meta_plsongs = "dmap.itemkind,dmap.itemid,dmap.itemname,dmap.containeritemid,dmap.parentcontainerid";
|
|
|
|
static char *default_meta_pl = "dmap.itemid,dmap.itemname,dmap.persistentid,com.apple.itunes.smart-playlist";
|
2010-01-10 07:04:57 -05:00
|
|
|
static char *default_meta_group = "dmap.itemname,dmap.persistentid,daap.songalbumartist";
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2010-01-20 12:24:52 -05:00
|
|
|
/* DAAP session tracking */
|
2015-02-24 16:58:47 -05:00
|
|
|
static struct daap_session *daap_sessions;
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2010-01-28 13:20:00 -05:00
|
|
|
/* Update requests */
|
2011-09-10 12:37:16 -04:00
|
|
|
static int current_rev;
|
2010-01-28 13:20:00 -05:00
|
|
|
static struct daap_update_request *update_requests;
|
2015-03-06 15:22:05 -05:00
|
|
|
static struct timeval daap_update_refresh_tv = { DAAP_UPDATE_REFRESH, 0 };
|
2010-01-28 13:20:00 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
|
|
|
|
/* -------------------------- SESSION HANDLING ------------------------------ */
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
static void
|
|
|
|
daap_session_free(struct daap_session *s)
|
2010-01-20 12:24:52 -05:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
if (!s)
|
|
|
|
return;
|
2010-01-20 12:24:52 -05:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
free(s);
|
2010-01-20 12:24:52 -05:00
|
|
|
}
|
|
|
|
|
2010-03-06 04:29:53 -05:00
|
|
|
static void
|
2015-02-24 16:58:47 -05:00
|
|
|
daap_session_remove(struct daap_session *s)
|
2010-03-06 04:29:53 -05:00
|
|
|
{
|
2015-02-24 16:58:47 -05:00
|
|
|
struct daap_session *ptr;
|
|
|
|
struct daap_session *prev;
|
2010-07-30 15:52:04 -04:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
prev = NULL;
|
|
|
|
for (ptr = daap_sessions; ptr; ptr = ptr->next)
|
|
|
|
{
|
|
|
|
if (ptr == s)
|
|
|
|
break;
|
2010-07-30 15:52:04 -04:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
prev = ptr;
|
|
|
|
}
|
2014-03-11 18:20:29 -04:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
if (!ptr)
|
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Error: Request to remove non-existent or ad-hoc session. BUG!\n");
|
2015-02-24 16:58:47 -05:00
|
|
|
return;
|
|
|
|
}
|
2014-03-11 18:20:29 -04:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
if (!prev)
|
|
|
|
daap_sessions = s->next;
|
|
|
|
else
|
|
|
|
prev->next = s->next;
|
2010-03-06 04:30:59 -05:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
daap_session_free(s);
|
2010-03-06 04:29:53 -05:00
|
|
|
}
|
|
|
|
|
2010-01-20 12:24:52 -05:00
|
|
|
static struct daap_session *
|
2015-02-24 16:58:47 -05:00
|
|
|
daap_session_get(int id)
|
|
|
|
{
|
|
|
|
struct daap_session *s;
|
|
|
|
|
|
|
|
for (s = daap_sessions; s; s = s->next)
|
|
|
|
{
|
|
|
|
if (id == s->id)
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-03 04:00:42 -04:00
|
|
|
/* Removes stale sessions and also drops the oldest sessions if DAAP_SESSION_MAX
|
|
|
|
* will otherwise be exceeded
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
daap_session_cleanup(void)
|
|
|
|
{
|
|
|
|
struct daap_session *s;
|
|
|
|
struct daap_session *next;
|
|
|
|
time_t now;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
now = time(NULL);
|
|
|
|
|
|
|
|
for (s = daap_sessions; s; s = next)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
next = s->next;
|
|
|
|
|
|
|
|
if ((difftime(now, s->mtime) > DAAP_SESSION_TIMEOUT) || (count > DAAP_SESSION_MAX))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Cleaning up DAAP session (id %d)\n", s->id);
|
|
|
|
|
|
|
|
daap_session_remove(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
static struct daap_session *
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_session_add(bool is_remote, int request_session_id)
|
2010-01-20 12:24:52 -05:00
|
|
|
{
|
|
|
|
struct daap_session *s;
|
|
|
|
|
2015-10-03 04:00:42 -04:00
|
|
|
daap_session_cleanup();
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_NULL(L_DAAP, s = calloc(1, sizeof(struct daap_session)));
|
2010-01-20 12:24:52 -05:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
if (request_session_id)
|
2014-04-01 16:47:04 -04:00
|
|
|
{
|
2015-02-24 16:58:47 -05:00
|
|
|
if (daap_session_get(request_session_id))
|
2014-04-01 16:47:04 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Session id requested in login (%d) is not available\n", request_session_id);
|
2017-10-05 16:13:01 -04:00
|
|
|
free(s);
|
2014-04-01 16:47:04 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-11-11 15:17:42 -05:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
s->id = request_session_id;
|
|
|
|
}
|
2014-04-01 16:47:04 -04:00
|
|
|
else
|
|
|
|
{
|
2015-02-24 16:58:47 -05:00
|
|
|
while ( (s->id = rand() + 100) && daap_session_get(s->id) );
|
2014-04-01 16:47:04 -04:00
|
|
|
}
|
2010-01-20 12:24:52 -05:00
|
|
|
|
2015-10-03 04:00:42 -04:00
|
|
|
s->mtime = time(NULL);
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
s->is_remote = is_remote;
|
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
if (daap_sessions)
|
|
|
|
s->next = daap_sessions;
|
2010-01-20 12:24:52 -05:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
daap_sessions = s;
|
2014-03-11 18:20:29 -04:00
|
|
|
|
2010-01-20 12:24:52 -05:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
/* ---------------------- UPDATE REQUESTS HANDLERS -------------------------- */
|
2010-01-29 16:36:12 -05:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
static void
|
|
|
|
update_free(struct daap_update_request *ur)
|
|
|
|
{
|
2015-03-06 15:22:05 -05:00
|
|
|
if (ur->timeout)
|
|
|
|
event_free(ur->timeout);
|
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
free(ur);
|
|
|
|
}
|
|
|
|
|
2010-01-28 13:20:00 -05:00
|
|
|
static void
|
2011-09-10 12:37:16 -04:00
|
|
|
update_remove(struct daap_update_request *ur)
|
2010-01-28 13:20:00 -05:00
|
|
|
{
|
|
|
|
struct daap_update_request *p;
|
|
|
|
|
|
|
|
if (ur == update_requests)
|
|
|
|
update_requests = ur->next;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (p = update_requests; p && (p->next != ur); p = p->next)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
{
|
2010-01-29 16:37:05 -05:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "WARNING: struct daap_update_request not found in list; BUG!\n");
|
2010-01-28 13:20:00 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->next = ur->next;
|
|
|
|
}
|
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
update_free(ur);
|
2010-01-28 13:20:00 -05:00
|
|
|
}
|
|
|
|
|
2011-09-10 12:37:16 -04:00
|
|
|
static void
|
|
|
|
update_refresh_cb(int fd, short event, void *arg)
|
|
|
|
{
|
|
|
|
struct daap_update_request *ur;
|
2014-05-29 17:22:00 -04:00
|
|
|
struct evhttp_connection *evcon;
|
2017-10-26 17:01:07 -04:00
|
|
|
struct evbuffer *reply;
|
2011-09-10 12:37:16 -04:00
|
|
|
|
|
|
|
ur = (struct daap_update_request *)arg;
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_NULL(L_DAAP, reply = evbuffer_new());
|
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(reply, 32));
|
2011-09-10 12:37:16 -04:00
|
|
|
|
2017-10-30 15:05:35 -04:00
|
|
|
current_rev++;
|
|
|
|
|
2011-09-10 12:37:16 -04:00
|
|
|
/* Send back current revision */
|
2017-10-26 17:01:07 -04:00
|
|
|
dmap_add_container(reply, "mupd", 24);
|
|
|
|
dmap_add_int(reply, "mstt", 200); /* 12 */
|
|
|
|
dmap_add_int(reply, "musr", current_rev); /* 12 */
|
2011-09-10 12:37:16 -04:00
|
|
|
|
2014-05-29 17:22:00 -04:00
|
|
|
evcon = evhttp_request_get_connection(ur->req);
|
|
|
|
evhttp_connection_set_closecb(evcon, NULL, NULL);
|
2011-09-10 12:37:16 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
httpd_send_reply(ur->req, HTTP_OK, "OK", reply, 0);
|
2011-09-10 12:37:16 -04:00
|
|
|
|
|
|
|
update_remove(ur);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
update_fail_cb(struct evhttp_connection *evcon, void *arg)
|
|
|
|
{
|
2014-05-29 17:22:00 -04:00
|
|
|
struct evhttp_connection *evc;
|
2011-09-10 12:37:16 -04:00
|
|
|
struct daap_update_request *ur;
|
|
|
|
|
|
|
|
ur = (struct daap_update_request *)arg;
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "Update request: client closed connection\n");
|
|
|
|
|
2014-05-29 17:22:00 -04:00
|
|
|
evc = evhttp_request_get_connection(ur->req);
|
|
|
|
if (evc)
|
|
|
|
evhttp_connection_set_closecb(evc, NULL, NULL);
|
2011-09-10 12:37:16 -04:00
|
|
|
|
2020-01-02 17:30:34 -05:00
|
|
|
evhttp_request_free(ur->req);
|
2011-09-10 12:37:16 -04:00
|
|
|
update_remove(ur);
|
|
|
|
}
|
|
|
|
|
2010-01-20 12:24:52 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
/* ------------------------- SORT HEADERS HELPERS --------------------------- */
|
|
|
|
|
2010-08-14 07:57:49 -04:00
|
|
|
static struct sort_ctx *
|
|
|
|
daap_sort_context_new(void)
|
|
|
|
{
|
|
|
|
struct sort_ctx *ctx;
|
|
|
|
int ret;
|
|
|
|
|
2017-10-05 16:13:01 -04:00
|
|
|
ctx = calloc(1, sizeof(struct sort_ctx));
|
2010-08-14 07:57:49 -04:00
|
|
|
if (!ctx)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Out of memory for sorting context\n");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(ctx, 0, sizeof(struct sort_ctx));
|
|
|
|
|
|
|
|
ctx->headerlist = evbuffer_new();
|
|
|
|
if (!ctx->headerlist)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not create evbuffer for DAAP sort headers list\n");
|
|
|
|
|
|
|
|
free(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = evbuffer_expand(ctx->headerlist, 512);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not expand evbuffer for DAAP sort headers list\n");
|
|
|
|
|
|
|
|
evbuffer_free(ctx->headerlist);
|
|
|
|
free(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->mshc = -1;
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
daap_sort_context_free(struct sort_ctx *ctx)
|
|
|
|
{
|
|
|
|
evbuffer_free(ctx->headerlist);
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
daap_sort_build(struct sort_ctx *ctx, char *str)
|
|
|
|
{
|
|
|
|
uint8_t *ret;
|
|
|
|
size_t len;
|
|
|
|
char fl;
|
|
|
|
|
|
|
|
len = strlen(str);
|
2013-12-12 17:27:50 -05:00
|
|
|
if (len > 0)
|
2010-08-14 07:57:49 -04:00
|
|
|
{
|
2015-06-06 08:47:54 -04:00
|
|
|
ret = u8_normalize(UNINORM_NFD, (uint8_t *)str, len + 1, NULL, &len);
|
2013-12-12 17:27:50 -05:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not normalize string for sort header\n");
|
2010-08-14 07:57:49 -04:00
|
|
|
|
2013-12-12 17:27:50 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2010-08-14 07:57:49 -04:00
|
|
|
|
2013-12-12 17:27:50 -05:00
|
|
|
fl = ret[0];
|
|
|
|
free(ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fl = 0;
|
2010-08-14 07:57:49 -04:00
|
|
|
|
|
|
|
if (isascii(fl) && isalpha(fl))
|
|
|
|
{
|
|
|
|
fl = toupper(fl);
|
|
|
|
|
|
|
|
/* Init */
|
|
|
|
if (ctx->mshc == -1)
|
|
|
|
ctx->mshc = fl;
|
|
|
|
|
|
|
|
if (fl == ctx->mshc)
|
|
|
|
ctx->mshn++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dmap_add_container(ctx->headerlist, "mlit", 34);
|
|
|
|
dmap_add_short(ctx->headerlist, "mshc", ctx->mshc); /* 10 */
|
|
|
|
dmap_add_int(ctx->headerlist, "mshi", ctx->mshi); /* 12 */
|
|
|
|
dmap_add_int(ctx->headerlist, "mshn", ctx->mshn); /* 12 */
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "Added sort header: mshc = %c, mshi = %u, mshn = %u fl %c\n", ctx->mshc, ctx->mshi, ctx->mshn, fl);
|
|
|
|
|
|
|
|
ctx->mshi = ctx->mshi + ctx->mshn;
|
|
|
|
ctx->mshn = 1;
|
|
|
|
ctx->mshc = fl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Non-ASCII, goes to misc category */
|
|
|
|
ctx->misc_mshn++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-10 06:35:24 -05:00
|
|
|
static void
|
|
|
|
daap_sort_finalize(struct sort_ctx *ctx)
|
2010-08-14 07:57:49 -04:00
|
|
|
{
|
|
|
|
/* Add current entry, if any */
|
|
|
|
if (ctx->mshc != -1)
|
|
|
|
{
|
|
|
|
dmap_add_container(ctx->headerlist, "mlit", 34);
|
|
|
|
dmap_add_short(ctx->headerlist, "mshc", ctx->mshc); /* 10 */
|
|
|
|
dmap_add_int(ctx->headerlist, "mshi", ctx->mshi); /* 12 */
|
|
|
|
dmap_add_int(ctx->headerlist, "mshn", ctx->mshn); /* 12 */
|
|
|
|
|
|
|
|
ctx->mshi = ctx->mshi + ctx->mshn;
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "Added sort header: mshc = %c, mshi = %u, mshn = %u (final)\n", ctx->mshc, ctx->mshi, ctx->mshn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add misc category */
|
|
|
|
dmap_add_container(ctx->headerlist, "mlit", 34);
|
|
|
|
dmap_add_short(ctx->headerlist, "mshc", '0'); /* 10 */
|
|
|
|
dmap_add_int(ctx->headerlist, "mshi", ctx->mshi); /* 12 */
|
|
|
|
dmap_add_int(ctx->headerlist, "mshn", ctx->misc_mshn); /* 12 */
|
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
|
|
|
|
/* ----------------------------- OTHER HELPERS ------------------------------ */
|
|
|
|
|
|
|
|
/* We try not to return items that the client cannot play (like Spotify and
|
|
|
|
* internet streams in iTunes), or which are inappropriate (like internet streams
|
2017-11-04 16:24:42 -04:00
|
|
|
* in the album tab of remotes). Note that the function must never append a
|
|
|
|
* filter if the SELECT is not from the files table.
|
2017-10-26 17:01:07 -04:00
|
|
|
*/
|
|
|
|
static void
|
2017-11-07 18:32:03 -05:00
|
|
|
user_agent_filter(struct query_params *qp, struct httpd_request *hreq)
|
2017-10-26 17:01:07 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
struct daap_session *s = hreq->extra_data;
|
2017-10-26 17:01:07 -04:00
|
|
|
char *filter;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
if (s->is_remote)
|
2017-10-26 17:01:07 -04:00
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
// This makes sure 1) the SELECT is from files, 2) that the Remote query
|
|
|
|
// contained extended_media_kind:1, which characterise the queries we want
|
|
|
|
// to filter. TODO: Not a really nice way of doing this, but best I could
|
|
|
|
// think of.
|
2020-04-04 15:25:14 -04:00
|
|
|
if (!qp->filter || !strstr(qp->filter, "f.media_kind = 1"))
|
2017-11-04 16:24:42 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
filter = safe_asprintf("%s AND (f.data_kind <> %d)", qp->filter, DATA_KIND_HTTP);
|
2017-10-26 17:01:07 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
if (qp->type != Q_ITEMS)
|
|
|
|
return;
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
if (qp->filter)
|
|
|
|
filter = safe_asprintf("%s AND (f.data_kind = %d)", qp->filter, DATA_KIND_FILE);
|
|
|
|
else
|
|
|
|
filter = safe_asprintf("(f.data_kind = %d)", DATA_KIND_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(qp->filter);
|
|
|
|
qp->filter = filter;
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "SQL filter w/client mod: %s\n", qp->filter);
|
|
|
|
}
|
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
static void
|
2017-11-07 18:32:03 -05:00
|
|
|
query_params_set(struct query_params *qp, int *sort_headers, struct httpd_request *hreq, enum query_type type)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
|
|
|
const char *param;
|
|
|
|
char *ptr;
|
2009-06-07 12:58:02 -04:00
|
|
|
int low;
|
|
|
|
int high;
|
2009-04-28 11:48:57 -04:00
|
|
|
int ret;
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
low = 0;
|
|
|
|
high = -1; /* No limit */
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
memset(qp, 0, sizeof(struct query_params));
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "index");
|
2009-04-28 11:48:57 -04:00
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
if (param[0] == '-') /* -n, last n entries */
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Unsupported index range: %s\n", param);
|
|
|
|
else
|
|
|
|
{
|
2010-02-02 15:02:24 -05:00
|
|
|
ret = safe_atoi32(param, &low);
|
2009-04-28 11:48:57 -04:00
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not parse index range: %s\n", param);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = strchr(param, '-');
|
|
|
|
if (!ptr) /* single item */
|
2009-06-07 12:58:02 -04:00
|
|
|
high = low;
|
2009-04-28 11:48:57 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
if (*ptr != '\0') /* low-high */
|
|
|
|
{
|
2010-02-02 15:02:24 -05:00
|
|
|
ret = safe_atoi32(ptr, &high);
|
2009-04-28 11:48:57 -04:00
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not parse high index in range: %s\n", param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
DPRINTF(E_DBG, L_DAAP, "Index range %s: low %d, high %d (offset %d, limit %d)\n", param, low, high, qp->offset, qp->limit);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
if (high < low)
|
|
|
|
high = -1; /* No limit */
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
qp->offset = low;
|
|
|
|
if (high < 0)
|
|
|
|
qp->limit = -1; /* No limit */
|
|
|
|
else
|
|
|
|
qp->limit = (high - low) + 1;
|
|
|
|
|
2016-04-09 18:11:33 -04:00
|
|
|
if (qp->limit == -1 && qp->offset == 0)
|
|
|
|
qp->idx_type = I_NONE;
|
|
|
|
else
|
|
|
|
qp->idx_type = I_SUB;
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2010-04-24 04:20:26 -04:00
|
|
|
qp->sort = S_NONE;
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "sort");
|
2010-08-03 17:28:46 -04:00
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
if (strcmp(param, "name") == 0)
|
|
|
|
qp->sort = S_NAME;
|
2018-01-20 18:22:21 -05:00
|
|
|
else if (strcmp(param, "album") == 0 && (type != Q_BROWSE_ALBUMS)) // Only set if non-default sort requested
|
2010-08-03 17:28:46 -04:00
|
|
|
qp->sort = S_ALBUM;
|
2018-01-20 18:22:21 -05:00
|
|
|
else if (strcmp(param, "artist") == 0 && (type != Q_BROWSE_ARTISTS)) // Only set if non-default sort requested
|
2010-10-09 09:57:34 -04:00
|
|
|
qp->sort = S_ARTIST;
|
2013-12-04 15:29:58 -05:00
|
|
|
else if (strcmp(param, "releasedate") == 0)
|
2020-02-22 18:54:28 -05:00
|
|
|
qp->sort = S_DATE_RELEASED;
|
2010-08-03 17:28:46 -04:00
|
|
|
else
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "Unknown sort param: %s\n", param);
|
|
|
|
|
|
|
|
if (qp->sort != S_NONE)
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "Sorting songlist by %s\n", param);
|
|
|
|
}
|
2010-04-24 04:20:26 -04:00
|
|
|
|
2010-08-14 06:13:41 -04:00
|
|
|
if (sort_headers)
|
|
|
|
{
|
|
|
|
*sort_headers = 0;
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "include-sort-headers");
|
2017-03-05 03:45:34 -05:00
|
|
|
if (param && (strcmp(param, "1") == 0))
|
2010-08-14 06:13:41 -04:00
|
|
|
{
|
2017-03-05 03:45:34 -05:00
|
|
|
*sort_headers = 1;
|
|
|
|
DPRINTF(E_SPAM, L_DAAP, "Sort headers requested\n");
|
2010-08-14 06:13:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "query");
|
2009-06-02 10:32:09 -04:00
|
|
|
if (!param)
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "filter");
|
2009-06-02 10:32:09 -04:00
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "DAAP browse query filter: %s\n", param);
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
qp->filter = daap_query_parse_sql(param);
|
|
|
|
if (!qp->filter)
|
2015-04-22 15:54:31 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Ignoring improper DAAP query: %s\n", param);
|
2015-05-05 15:46:56 -04:00
|
|
|
|
|
|
|
/* iTunes seems to default to this when there is a query (which there is for audiobooks, but not for normal playlists) */
|
2018-01-20 18:22:21 -05:00
|
|
|
if (!qp->sort && !(type & Q_F_BROWSE))
|
2015-05-05 15:46:56 -04:00
|
|
|
qp->sort = S_ALBUM;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
2017-10-26 17:01:07 -04:00
|
|
|
|
|
|
|
qp->type = type;
|
2017-10-29 05:09:53 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
user_agent_filter(qp, hreq);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2011-04-01 13:01:35 -04:00
|
|
|
static int
|
2017-10-26 17:01:07 -04:00
|
|
|
parse_meta(const struct dmap_field ***out_meta, const char *param)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2011-04-01 13:01:35 -04:00
|
|
|
const struct dmap_field **meta;
|
2009-04-28 11:48:57 -04:00
|
|
|
char *ptr;
|
2011-04-01 13:01:35 -04:00
|
|
|
char *field;
|
2009-04-28 11:48:57 -04:00
|
|
|
char *metastr;
|
|
|
|
int nmeta;
|
2009-04-28 16:51:52 -04:00
|
|
|
int i;
|
2013-12-16 16:09:18 -05:00
|
|
|
int n;
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_NULL(L_DAAP, metastr = strdup(param));
|
2009-04-28 11:48:57 -04:00
|
|
|
|
|
|
|
nmeta = 1;
|
|
|
|
ptr = metastr;
|
2013-12-02 14:55:16 -05:00
|
|
|
while ((ptr = strchr(ptr + 1, ',')) && (strlen(ptr) > 1))
|
2009-04-28 11:48:57 -04:00
|
|
|
nmeta++;
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "Asking for %d meta tags\n", nmeta);
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_NULL(L_DAAP, meta = calloc(nmeta, sizeof(const struct dmap_field *)));
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2011-04-01 13:01:35 -04:00
|
|
|
field = strtok_r(metastr, ",", &ptr);
|
2009-04-28 16:51:52 -04:00
|
|
|
for (i = 0; i < nmeta; i++)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2013-12-16 16:09:18 -05:00
|
|
|
for (n = 0; (n < i) && (strcmp(field, meta[n]->desc) != 0); n++);
|
2011-04-01 13:01:35 -04:00
|
|
|
|
2013-12-16 16:09:18 -05:00
|
|
|
if (n == i)
|
2011-04-01 13:01:35 -04:00
|
|
|
{
|
2017-08-25 13:27:41 -04:00
|
|
|
meta[i] = dmap_find_field_wrapper(field, strlen(field));
|
2013-12-16 16:09:18 -05:00
|
|
|
|
|
|
|
if (!meta[i])
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_DAAP, "Could not find requested meta field '%s'\n", field);
|
|
|
|
|
|
|
|
i--;
|
|
|
|
nmeta--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_DAAP, "Parser will ignore duplicate occurrence of meta field '%s'\n", field);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2011-04-01 13:01:35 -04:00
|
|
|
i--;
|
|
|
|
nmeta--;
|
|
|
|
}
|
|
|
|
|
|
|
|
field = strtok_r(NULL, ",", &ptr);
|
|
|
|
if (!field)
|
2009-04-28 16:51:52 -04:00
|
|
|
break;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
free(metastr);
|
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
DPRINTF(E_DBG, L_DAAP, "Found %d meta tags\n", nmeta);
|
|
|
|
|
2011-04-01 13:01:35 -04:00
|
|
|
*out_meta = meta;
|
2009-04-28 16:51:52 -04:00
|
|
|
|
2011-04-01 13:01:35 -04:00
|
|
|
return nmeta;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static void
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_send(struct httpd_request *hreq, enum daap_reply_result result)
|
2017-10-26 17:01:07 -04:00
|
|
|
{
|
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
case DAAP_REPLY_LOGOUT:
|
2017-11-07 18:32:03 -05:00
|
|
|
httpd_send_reply(hreq->req, 204, "Logout Successful", hreq->reply, 0);
|
2017-10-26 17:01:07 -04:00
|
|
|
break;
|
|
|
|
case DAAP_REPLY_NO_CONTENT:
|
2017-11-07 18:32:03 -05:00
|
|
|
httpd_send_reply(hreq->req, HTTP_NOCONTENT, "No Content", hreq->reply, HTTPD_SEND_NO_GZIP);
|
2017-10-26 17:01:07 -04:00
|
|
|
break;
|
|
|
|
case DAAP_REPLY_OK:
|
2017-11-07 18:32:03 -05:00
|
|
|
httpd_send_reply(hreq->req, HTTP_OK, "OK", hreq->reply, 0);
|
2017-10-26 17:01:07 -04:00
|
|
|
break;
|
|
|
|
case DAAP_REPLY_OK_NO_GZIP:
|
|
|
|
case DAAP_REPLY_ERROR:
|
2017-11-07 18:32:03 -05:00
|
|
|
httpd_send_reply(hreq->req, HTTP_OK, "OK", hreq->reply, HTTPD_SEND_NO_GZIP);
|
2017-10-26 17:01:07 -04:00
|
|
|
break;
|
|
|
|
case DAAP_REPLY_FORBIDDEN:
|
2017-11-07 18:32:03 -05:00
|
|
|
httpd_send_error(hreq->req, 403, "Forbidden");
|
2017-10-26 17:01:07 -04:00
|
|
|
break;
|
|
|
|
case DAAP_REPLY_BAD_REQUEST:
|
2017-11-07 18:32:03 -05:00
|
|
|
httpd_send_error(hreq->req, HTTP_BADREQUEST, "Bad Request");
|
2017-10-26 17:01:07 -04:00
|
|
|
break;
|
|
|
|
case DAAP_REPLY_SERVUNAVAIL:
|
2017-11-07 18:32:03 -05:00
|
|
|
httpd_send_error(hreq->req, HTTP_SERVUNAVAIL, "Internal Server Error");
|
2017-10-26 17:01:07 -04:00
|
|
|
break;
|
|
|
|
case DAAP_REPLY_NO_CONNECTION:
|
|
|
|
case DAAP_REPLY_NONE:
|
|
|
|
// Send nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static int
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_request_authorize(struct httpd_request *hreq)
|
2017-10-26 17:01:07 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
struct daap_session *session = hreq->extra_data;
|
2017-10-26 17:01:07 -04:00
|
|
|
const char *param;
|
|
|
|
char *passwd;
|
|
|
|
int ret;
|
|
|
|
|
2017-11-15 17:13:20 -05:00
|
|
|
if (peer_address_is_trusted(hreq->peer_address))
|
2017-10-28 18:05:20 -04:00
|
|
|
return 0;
|
|
|
|
|
2018-09-30 16:39:53 -04:00
|
|
|
// Regular DAAP clients like iTunes will login with /login, and we will reply
|
|
|
|
// with httpd_basic_auth() if a library password is set. Remote clients will
|
|
|
|
// also call /login, but they should not get a httpd_basic_auth(), instead
|
|
|
|
// daap_reply_login() will take care of auth.
|
|
|
|
if (session->is_remote && (strcmp(hreq->uri_parsed->path, "/login") == 0))
|
|
|
|
return 0;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "session-id");
|
2017-10-26 17:01:07 -04:00
|
|
|
if (param)
|
|
|
|
{
|
2018-09-30 16:39:53 -04:00
|
|
|
if (session->id == 0)
|
2017-10-26 17:01:07 -04:00
|
|
|
{
|
2018-09-07 11:01:32 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Unauthorized request from '%s', DAAP session not found: '%s'\n", hreq->peer_address, hreq->uri_parsed->uri);
|
2018-10-04 16:33:30 -04:00
|
|
|
|
2018-10-04 16:39:46 -04:00
|
|
|
httpd_send_error(hreq->req, 401, "Unauthorized");
|
2017-10-26 17:01:07 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
session->mtime = time(NULL);
|
2017-10-26 17:01:07 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
passwd = cfg_getstr(cfg_getsec(cfg, "library"), "password");
|
|
|
|
if (!passwd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// If no valid session then we may need to authenticate
|
2017-11-07 18:32:03 -05:00
|
|
|
if ((strcmp(hreq->uri_parsed->path, "/server-info") == 0)
|
|
|
|
|| (strcmp(hreq->uri_parsed->path, "/logout") == 0)
|
|
|
|
|| (strcmp(hreq->uri_parsed->path, "/content-codes") == 0)
|
|
|
|
|| (strncmp(hreq->uri_parsed->path, "/databases/1/items/", strlen("/databases/1/items/")) == 0))
|
2017-10-26 17:01:07 -04:00
|
|
|
return 0; // No authentication
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "Checking authentication for library\n");
|
|
|
|
|
|
|
|
// We don't care about the username
|
2017-11-07 18:32:03 -05:00
|
|
|
ret = httpd_basic_auth(hreq->req, NULL, passwd, cfg_getstr(cfg_getsec(cfg, "library"), "name"));
|
2017-10-26 17:01:07 -04:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2018-09-07 11:01:32 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Unsuccessful library authorization attempt from '%s'\n", hreq->peer_address);
|
2017-10-26 17:01:07 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------- REPLY HANDLERS ------------------------------- */
|
|
|
|
/* Note that some handlers can be called without a connection (needed for */
|
|
|
|
/* cache regeneration), while others cannot. Those that cannot should check */
|
2017-11-07 18:32:03 -05:00
|
|
|
/* that hreq->req is not a null pointer. */
|
2017-10-26 17:01:07 -04:00
|
|
|
|
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_server_info(struct httpd_request *hreq)
|
2017-10-26 17:01:07 -04:00
|
|
|
{
|
|
|
|
struct evbuffer *content;
|
|
|
|
struct evkeyvalq *headers;
|
|
|
|
char *name;
|
|
|
|
char *passwd;
|
|
|
|
const char *clientver;
|
|
|
|
size_t len;
|
|
|
|
int mpro;
|
|
|
|
int apro;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
if (!hreq->req)
|
2017-10-26 17:01:07 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Bug! daap_reply_server_info() cannot be called without an actual connection\n");
|
|
|
|
return DAAP_REPLY_NO_CONNECTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
passwd = cfg_getstr(cfg_getsec(cfg, "library"), "password");
|
|
|
|
name = cfg_getstr(cfg_getsec(cfg, "library"), "name");
|
|
|
|
|
|
|
|
CHECK_NULL(L_DAAP, content = evbuffer_new());
|
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(content, 512));
|
|
|
|
|
|
|
|
mpro = 2 << 16 | 10;
|
|
|
|
apro = 3 << 16 | 12;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
headers = evhttp_request_get_input_headers(hreq->req);
|
2017-10-26 17:01:07 -04:00
|
|
|
if (headers && (clientver = evhttp_find_header(headers, "Client-DAAP-Version")))
|
|
|
|
{
|
|
|
|
if (strcmp(clientver, "1.0") == 0)
|
|
|
|
{
|
|
|
|
mpro = 1 << 16;
|
|
|
|
apro = 1 << 16;
|
|
|
|
}
|
|
|
|
else if (strcmp(clientver, "2.0") == 0)
|
|
|
|
{
|
|
|
|
mpro = 1 << 16;
|
|
|
|
apro = 2 << 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dmap_add_int(content, "mstt", 200);
|
|
|
|
dmap_add_int(content, "mpro", mpro); // dmap.protocolversion
|
|
|
|
dmap_add_string(content, "minm", name); // dmap.itemname (server name)
|
|
|
|
|
|
|
|
dmap_add_int(content, "apro", apro); // daap.protocolversion
|
|
|
|
dmap_add_int(content, "aeSV", apro); // com.apple.itunes.music-sharing-version (determines if itunes shows share types)
|
|
|
|
|
|
|
|
dmap_add_short(content, "ated", 7); // daap.supportsextradata
|
|
|
|
|
2019-10-26 16:51:15 -04:00
|
|
|
// Sub-optimal user-agent sniffing to solve the problem that iTunes 12.1 and
|
|
|
|
// Apple Music do not work if we announce support for groups
|
2017-11-07 18:32:03 -05:00
|
|
|
if (hreq->user_agent && (strncmp(hreq->user_agent, "iTunes", strlen("iTunes")) == 0))
|
2017-10-26 17:01:07 -04:00
|
|
|
dmap_add_short(content, "asgr", 0); // daap.supportsgroups (1=artists, 2=albums, 3=both)
|
2019-10-26 16:51:15 -04:00
|
|
|
else if (hreq->user_agent && (strncmp(hreq->user_agent, "Music", strlen("Music")) == 0))
|
|
|
|
dmap_add_short(content, "asgr", 0); // daap.supportsgroups (1=artists, 2=albums, 3=both)
|
2017-10-26 17:01:07 -04:00
|
|
|
else
|
|
|
|
dmap_add_short(content, "asgr", 3); // daap.supportsgroups (1=artists, 2=albums, 3=both)
|
|
|
|
|
|
|
|
// dmap_add_long(content, "asse", 0x80000); // unknown - used by iTunes
|
|
|
|
|
|
|
|
dmap_add_char(content, "aeMQ", 1); // unknown - used by iTunes
|
|
|
|
|
|
|
|
// dmap_add_long(content, "mscu", ); // unknown - used by iTunes
|
|
|
|
// dmap_add_char(content, "aeFR", ); // unknown - used by iTunes
|
|
|
|
|
|
|
|
dmap_add_char(content, "aeTr", 1); // unknown - used by iTunes
|
|
|
|
dmap_add_char(content, "aeSL", 1); // unknown - used by iTunes
|
|
|
|
dmap_add_char(content, "aeSR", 1); // unknown - used by iTunes
|
|
|
|
// dmap_add_char(content, "aeFP", 2); // triggers FairPlay request
|
|
|
|
// dmap_add_long(content, "aeSX", ); // unknown - used by iTunes
|
|
|
|
|
|
|
|
// dmap_add_int(content, "ppro", ); // dpap.protocolversion
|
|
|
|
|
|
|
|
dmap_add_char(content, "msed", 0); // dmap.supportsedit? - we don't support playlist editing
|
|
|
|
|
|
|
|
dmap_add_char(content, "mslr", 1); // dmap.loginrequired
|
|
|
|
dmap_add_int(content, "mstm", DAAP_SESSION_TIMEOUT_CAPABILITY); // dmap.timeoutinterval
|
|
|
|
dmap_add_char(content, "msal", 1); // dmap.supportsautologout
|
|
|
|
// dmap_add_char(content, "msas", 3); // dmap.authenticationschemes
|
|
|
|
dmap_add_char(content, "msau", (passwd) ? 2 : 0); // dmap.authenticationmethod
|
|
|
|
|
|
|
|
dmap_add_char(content, "msup", 1); // dmap.supportsupdate
|
|
|
|
dmap_add_char(content, "mspi", 1); // dmap.supportspersistentids
|
2014-03-26 18:59:28 -04:00
|
|
|
dmap_add_char(content, "msex", 1); // dmap.supportsextensions
|
|
|
|
dmap_add_char(content, "msbr", 1); // dmap.supportsbrowse
|
|
|
|
dmap_add_char(content, "msqy", 1); // dmap.supportsquery
|
|
|
|
dmap_add_char(content, "msix", 1); // dmap.supportsindex
|
|
|
|
// dmap_add_char(content, "msrs", 1); // dmap.supportsresolve
|
2012-01-02 02:15:18 -05:00
|
|
|
|
2014-05-29 03:29:23 -04:00
|
|
|
dmap_add_int(content, "msdc", 2); // dmap.databasescount
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2015-02-06 16:14:26 -05:00
|
|
|
// dmap_add_int(content, "mstc", ); // dmap.utctime
|
|
|
|
// dmap_add_int(content, "msto", ); // dmap.utcoffset
|
|
|
|
|
2014-03-26 18:59:28 -04:00
|
|
|
// Create container
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(content);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "msrv", len);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(hreq->reply, content));
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
evbuffer_free(content);
|
|
|
|
|
|
|
|
return DAAP_REPLY_OK;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_content_codes(struct httpd_request *hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2011-04-02 08:40:48 -04:00
|
|
|
const struct dmap_field *dmap_fields;
|
2015-07-29 17:05:47 -04:00
|
|
|
size_t len;
|
2011-04-02 08:40:48 -04:00
|
|
|
int nfields;
|
2009-04-28 11:48:57 -04:00
|
|
|
int i;
|
|
|
|
|
2011-04-02 08:40:48 -04:00
|
|
|
dmap_fields = dmap_get_fields_table(&nfields);
|
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
len = 12;
|
2011-04-02 08:40:48 -04:00
|
|
|
for (i = 0; i < nfields; i++)
|
2011-04-01 13:01:35 -04:00
|
|
|
len += 8 + 12 + 10 + 8 + strlen(dmap_fields[i].desc);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(hreq->reply, len + 8));
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "mccr", len);
|
|
|
|
dmap_add_int(hreq->reply, "mstt", 200);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2011-04-02 08:40:48 -04:00
|
|
|
for (i = 0; i < nfields; i++)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2011-04-01 13:01:35 -04:00
|
|
|
len = 12 + 10 + 8 + strlen(dmap_fields[i].desc);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "mdcl", len);
|
|
|
|
dmap_add_string(hreq->reply, "mcnm", dmap_fields[i].tag); /* 12 */
|
|
|
|
dmap_add_string(hreq->reply, "mcna", dmap_fields[i].desc); /* 8 + strlen(desc) */
|
|
|
|
dmap_add_short(hreq->reply, "mcty", dmap_fields[i].type); /* 10 */
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_OK;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_login(struct httpd_request *hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
struct daap_session *adhoc = hreq->extra_data;
|
|
|
|
struct daap_session *session;
|
2010-01-30 10:50:00 -05:00
|
|
|
struct pairing_info pi;
|
2014-04-01 16:47:04 -04:00
|
|
|
const char *param;
|
|
|
|
int request_session_id;
|
2009-04-28 11:48:57 -04:00
|
|
|
int ret;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(hreq->reply, 32));
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "pairing-guid");
|
2017-11-15 17:13:20 -05:00
|
|
|
if (param && !peer_address_is_trusted(hreq->peer_address))
|
2010-01-30 10:50:00 -05:00
|
|
|
{
|
2017-10-26 17:01:07 -04:00
|
|
|
if (strlen(param) < 3)
|
2010-01-30 10:50:00 -05:00
|
|
|
{
|
2017-11-15 17:13:20 -05:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Login attempt from %s with invalid pairing-guid: %s\n", hreq->peer_address, param);
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_FORBIDDEN;
|
2010-01-30 10:50:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&pi, 0, sizeof(struct pairing_info));
|
2014-04-01 16:47:04 -04:00
|
|
|
pi.guid = strdup(param + 2); /* Skip leading 0X */
|
2010-01-30 10:50:00 -05:00
|
|
|
|
|
|
|
ret = db_pairing_fetch_byguid(&pi);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-11-15 17:13:20 -05:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Login attempt from %s with invalid pairing-guid: %s\n", hreq->peer_address, param);
|
2010-01-30 10:50:00 -05:00
|
|
|
free_pi(&pi, 1);
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_FORBIDDEN;
|
2010-01-30 10:50:00 -05:00
|
|
|
}
|
|
|
|
|
2017-11-15 17:13:20 -05:00
|
|
|
DPRINTF(E_INFO, L_DAAP, "Remote '%s' (%s) logging in with GUID %s\n", pi.name, hreq->peer_address, pi.guid);
|
2010-01-30 10:50:00 -05:00
|
|
|
free_pi(&pi, 1);
|
|
|
|
}
|
2017-10-26 17:01:07 -04:00
|
|
|
else
|
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
if (hreq->user_agent)
|
2017-11-15 17:13:20 -05:00
|
|
|
DPRINTF(E_INFO, L_DAAP, "Client '%s' logging in from %s\n", hreq->user_agent, hreq->peer_address);
|
2017-10-26 17:01:07 -04:00
|
|
|
else
|
2017-11-15 17:13:20 -05:00
|
|
|
DPRINTF(E_INFO, L_DAAP, "Client (unknown user-agent) logging in from %s\n", hreq->peer_address);
|
2017-10-26 17:01:07 -04:00
|
|
|
}
|
2010-01-30 10:50:00 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "request-session-id");
|
2014-04-01 16:47:04 -04:00
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
ret = safe_atoi32(param, &request_session_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Login request where request-session-id is not an integer\n");
|
|
|
|
request_session_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
request_session_id = 0;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
session = daap_session_add(adhoc->is_remote, request_session_id);
|
|
|
|
if (!session)
|
2010-01-20 12:24:52 -05:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "mlog", "Could not start session");
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2010-01-20 12:24:52 -05:00
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "mlog", 24);
|
|
|
|
dmap_add_int(hreq->reply, "mstt", 200); /* 12 */
|
|
|
|
dmap_add_int(hreq->reply, "mlid", session->id); /* 12 */
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_OK;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_logout(struct httpd_request *hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
if (!hreq->extra_data)
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_FORBIDDEN;
|
2010-01-20 12:24:52 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_session_remove(hreq->extra_data);
|
2010-01-20 12:24:52 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
hreq->extra_data = NULL;
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_LOGOUT;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_update(struct httpd_request *hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2010-01-28 13:20:00 -05:00
|
|
|
struct daap_update_request *ur;
|
2014-05-29 17:22:00 -04:00
|
|
|
struct evhttp_connection *evcon;
|
2020-01-10 13:22:42 -05:00
|
|
|
struct bufferevent *bufev;
|
2010-01-28 13:20:00 -05:00
|
|
|
const char *param;
|
|
|
|
int reqd_rev;
|
2009-04-28 11:48:57 -04:00
|
|
|
int ret;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
if (!hreq->req)
|
2017-10-26 17:01:07 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Bug! daap_reply_update() cannot be called without an actual connection\n");
|
|
|
|
return DAAP_REPLY_NO_CONNECTION;
|
|
|
|
}
|
2010-01-30 06:57:50 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "revision-number");
|
2010-01-28 13:20:00 -05:00
|
|
|
if (!param)
|
|
|
|
{
|
2014-12-16 18:33:16 -05:00
|
|
|
DPRINTF(E_DBG, L_DAAP, "Missing revision-number in client update request\n");
|
2012-01-02 04:22:39 -05:00
|
|
|
/* Some players (Amarok, Banshee) don't supply a revision number.
|
|
|
|
They get a standard update of everything. */
|
2018-09-30 16:39:53 -04:00
|
|
|
param = "1"; /* Default to "1" will ensure an update */
|
2010-01-28 13:20:00 -05:00
|
|
|
}
|
|
|
|
|
2010-02-02 15:02:24 -05:00
|
|
|
ret = safe_atoi32(param, &reqd_rev);
|
2009-04-28 11:48:57 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2010-01-28 13:20:00 -05:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Parameter revision-number not an integer\n");
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "mupd", "Invalid request");
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2010-01-28 13:20:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (reqd_rev == 1) /* Or revision is not valid */
|
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(hreq->reply, 32));
|
2010-01-28 13:20:00 -05:00
|
|
|
|
|
|
|
/* Send back current revision */
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "mupd", 24);
|
|
|
|
dmap_add_int(hreq->reply, "mstt", 200); /* 12 */
|
|
|
|
dmap_add_int(hreq->reply, "musr", current_rev); /* 12 */
|
2010-01-28 13:20:00 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_OK;
|
2010-01-28 13:20:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Else, just let the request hang until we have changes to push back */
|
2017-10-05 16:13:01 -04:00
|
|
|
ur = calloc(1, sizeof(struct daap_update_request));
|
2010-01-28 13:20:00 -05:00
|
|
|
if (!ur)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Out of memory for update request\n");
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "mupd", "Out of memory");
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
2011-09-10 12:37:16 -04:00
|
|
|
|
2015-03-06 15:22:05 -05:00
|
|
|
if (DAAP_UPDATE_REFRESH > 0)
|
|
|
|
{
|
|
|
|
ur->timeout = evtimer_new(evbase_httpd, update_refresh_cb, ur);
|
|
|
|
if (ur->timeout)
|
|
|
|
ret = evtimer_add(ur->timeout, &daap_update_refresh_tv);
|
|
|
|
else
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Out of memory for update request event\n");
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "mupd", "Could not register timer");
|
2015-03-06 15:22:05 -05:00
|
|
|
update_free(ur);
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2015-03-06 15:22:05 -05:00
|
|
|
}
|
|
|
|
}
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2010-01-28 13:20:00 -05:00
|
|
|
/* NOTE: we may need to keep reqd_rev in there too */
|
2017-11-07 18:32:03 -05:00
|
|
|
ur->req = hreq->req;
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2010-01-28 13:20:00 -05:00
|
|
|
ur->next = update_requests;
|
|
|
|
update_requests = ur;
|
|
|
|
|
2010-02-14 03:34:29 -05:00
|
|
|
/* If the connection fails before we have an update to push out
|
|
|
|
* to the client, we need to know.
|
2010-01-28 13:20:00 -05:00
|
|
|
*/
|
2017-11-07 18:32:03 -05:00
|
|
|
evcon = evhttp_request_get_connection(hreq->req);
|
2014-05-29 17:22:00 -04:00
|
|
|
if (evcon)
|
2020-01-10 13:22:42 -05:00
|
|
|
{
|
|
|
|
evhttp_connection_set_closecb(evcon, update_fail_cb, ur);
|
|
|
|
|
|
|
|
// This is a workaround for some versions of libevent (2.0, but possibly
|
|
|
|
// also 2.1) that don't detect if the client hangs up, and thus don't
|
|
|
|
// clean up and never call update_fail_cb(). See github issue #870 and
|
|
|
|
// https://github.com/libevent/libevent/issues/666. It should probably be
|
|
|
|
// removed again in the future. The workaround is also present in dacp.c
|
|
|
|
bufev = evhttp_connection_get_bufferevent(evcon);
|
|
|
|
if (bufev)
|
|
|
|
bufferevent_enable(bufev, EV_READ);
|
|
|
|
}
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_NONE;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_activity(struct httpd_request *hreq)
|
2009-12-20 08:43:28 -05:00
|
|
|
{
|
|
|
|
/* That's so nice, thanks for letting us know */
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_NO_CONTENT;
|
2009-12-20 08:43:28 -05:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_dblist(struct httpd_request *hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2014-03-26 18:59:28 -04:00
|
|
|
struct evbuffer *content;
|
2014-05-29 03:29:23 -04:00
|
|
|
struct evbuffer *item;
|
2009-04-28 11:48:57 -04:00
|
|
|
char *name;
|
2015-03-30 15:10:59 -04:00
|
|
|
char *name_radio;
|
2015-07-29 17:05:47 -04:00
|
|
|
size_t len;
|
2019-05-28 17:40:41 -04:00
|
|
|
uint32_t count = 0;
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
name = cfg_getstr(cfg_getsec(cfg, "library"), "name");
|
|
|
|
name_radio = cfg_getstr(cfg_getsec(cfg, "library"), "name_radio");
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_NULL(L_DAAP, content = evbuffer_new());
|
|
|
|
CHECK_NULL(L_DAAP, item = evbuffer_new());
|
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(item, 512));
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(hreq->reply, 1024));
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2014-05-29 03:29:23 -04:00
|
|
|
// Add db entry for library with dbid = 1
|
|
|
|
dmap_add_int(item, "miid", 1);
|
|
|
|
dmap_add_long(item, "mper", 1);
|
|
|
|
dmap_add_int(item, "mdbk", 1);
|
|
|
|
dmap_add_int(item, "aeCs", 1);
|
|
|
|
dmap_add_string(item, "minm", name);
|
2019-05-28 17:40:41 -04:00
|
|
|
db_files_get_count(&count, NULL, NULL);
|
|
|
|
dmap_add_int(item, "mimc", (int)count);
|
|
|
|
db_pl_get_count(&count); // TODO Don't count empty smart playlists, because they get excluded in aply
|
|
|
|
dmap_add_int(item, "mctc", (int)count);
|
2014-03-28 14:25:15 -04:00
|
|
|
// dmap_add_int(content, "aeMk", 0x405); // com.apple.itunes.extended-media-kind (OR of all in library)
|
2014-05-29 03:29:23 -04:00
|
|
|
dmap_add_int(item, "meds", 3);
|
|
|
|
|
|
|
|
// Create container for library db
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(item);
|
|
|
|
dmap_add_container(content, "mlit", len);
|
2014-05-29 03:29:23 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(content, item));
|
2014-05-29 03:29:23 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
// Add second db entry for radio with dbid = DAAP_DB_RADIO
|
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(item, 512));
|
2015-03-15 11:26:06 -04:00
|
|
|
|
2015-03-16 18:33:42 -04:00
|
|
|
dmap_add_int(item, "miid", DAAP_DB_RADIO);
|
|
|
|
dmap_add_long(item, "mper", DAAP_DB_RADIO);
|
2014-05-29 03:29:23 -04:00
|
|
|
dmap_add_int(item, "mdbk", 0x64);
|
|
|
|
dmap_add_int(item, "aeCs", 0);
|
2015-03-30 15:10:59 -04:00
|
|
|
dmap_add_string(item, "minm", name_radio);
|
2019-05-28 17:40:41 -04:00
|
|
|
db_pl_get_count(&count); // TODO This counts too much, should only include stream playlists
|
|
|
|
dmap_add_int(item, "mimc", (int)count);
|
2014-05-29 03:29:23 -04:00
|
|
|
dmap_add_int(item, "mctc", 0);
|
|
|
|
dmap_add_int(item, "aeMk", 1); // com.apple.itunes.extended-media-kind (OR of all in library)
|
|
|
|
dmap_add_int(item, "meds", 3);
|
|
|
|
|
|
|
|
// Create container for radio db
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(item);
|
|
|
|
dmap_add_container(content, "mlit", len);
|
2017-10-26 17:01:07 -04:00
|
|
|
|
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(content, item));
|
2014-03-26 18:59:28 -04:00
|
|
|
|
|
|
|
// Create container
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(content);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "avdb", len + 53);
|
|
|
|
dmap_add_int(hreq->reply, "mstt", 200); /* 12 */
|
|
|
|
dmap_add_char(hreq->reply, "muty", 0); /* 9 */
|
|
|
|
dmap_add_int(hreq->reply, "mtco", 2); /* 12 */
|
|
|
|
dmap_add_int(hreq->reply, "mrco", 2); /* 12 */
|
|
|
|
dmap_add_container(hreq->reply, "mlcl", len); /* 8 */
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(hreq->reply, content));
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
evbuffer_free(item);
|
|
|
|
evbuffer_free(content);
|
|
|
|
|
|
|
|
return DAAP_REPLY_OK;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_songlist_generic(struct httpd_request *hreq, int playlist)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2009-06-07 12:58:02 -04:00
|
|
|
struct query_params qp;
|
|
|
|
struct db_media_file_info dbmfi;
|
2009-04-28 11:48:57 -04:00
|
|
|
struct evbuffer *song;
|
|
|
|
struct evbuffer *songlist;
|
2014-05-29 17:22:00 -04:00
|
|
|
struct evkeyvalq *headers;
|
2017-11-07 18:32:03 -05:00
|
|
|
struct daap_session *s;
|
2011-04-01 13:01:35 -04:00
|
|
|
const struct dmap_field **meta;
|
2010-08-30 10:59:48 -04:00
|
|
|
struct sort_ctx *sctx;
|
2009-04-28 11:48:57 -04:00
|
|
|
const char *param;
|
2014-08-22 18:02:01 -04:00
|
|
|
const char *client_codecs;
|
2017-10-26 17:01:07 -04:00
|
|
|
const char *tag;
|
2014-12-28 17:37:12 -05:00
|
|
|
char *last_codectype;
|
2015-07-29 17:05:47 -04:00
|
|
|
size_t len;
|
2009-04-28 11:48:57 -04:00
|
|
|
int nmeta;
|
2010-08-30 10:59:48 -04:00
|
|
|
int sort_headers;
|
2009-04-28 11:48:57 -04:00
|
|
|
int nsongs;
|
|
|
|
int transcode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "Fetching song list for playlist %d\n", playlist);
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
s = hreq->extra_data;
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Bug! daap_reply_songlist_generic() called with NULL session (playlist %d)\n", playlist);
|
|
|
|
return DAAP_REPLY_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
if (playlist != -1)
|
|
|
|
{
|
2017-10-26 17:01:07 -04:00
|
|
|
// Songs in playlist
|
|
|
|
tag = "apso";
|
2017-11-07 18:32:03 -05:00
|
|
|
query_params_set(&qp, &sort_headers, hreq, Q_PLITEMS);
|
2017-10-26 17:01:07 -04:00
|
|
|
qp.id = playlist;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
2017-10-26 17:01:07 -04:00
|
|
|
else
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2017-10-26 17:01:07 -04:00
|
|
|
// Songs in database
|
|
|
|
tag = "adbs";
|
2017-11-07 18:32:03 -05:00
|
|
|
query_params_set(&qp, &sort_headers, hreq, Q_ITEMS);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_NULL(L_DAAP, songlist = evbuffer_new());
|
|
|
|
CHECK_NULL(L_DAAP, song = evbuffer_new());
|
|
|
|
CHECK_NULL(L_DAAP, sctx = daap_sort_context_new());
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(hreq->reply, 61));
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(songlist, 4096));
|
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(song, 512));
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "meta");
|
2009-04-28 11:48:57 -04:00
|
|
|
if (!param)
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_DAAP, "No meta parameter in query, using default\n");
|
|
|
|
|
|
|
|
if (playlist != -1)
|
|
|
|
param = default_meta_plsongs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param)
|
|
|
|
{
|
2017-10-26 17:01:07 -04:00
|
|
|
nmeta = parse_meta(&meta, param);
|
2009-04-28 11:48:57 -04:00
|
|
|
if (nmeta < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Failed to parse meta parameter in DAAP query\n");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-04-28 16:51:52 -04:00
|
|
|
{
|
|
|
|
meta = NULL;
|
|
|
|
nmeta = 0;
|
|
|
|
}
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
ret = db_query_start(&qp);
|
|
|
|
if (ret < 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2009-06-07 12:58:02 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not start query\n");
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
free(meta);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, tag, "Could not start query");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2014-12-28 17:37:12 -05:00
|
|
|
client_codecs = NULL;
|
2017-11-07 18:32:03 -05:00
|
|
|
if (!s->is_remote && hreq->req)
|
2014-12-28 17:37:12 -05:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
headers = evhttp_request_get_input_headers(hreq->req);
|
2014-12-28 17:37:12 -05:00
|
|
|
client_codecs = evhttp_find_header(headers, "Accept-Codecs");
|
|
|
|
}
|
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
nsongs = 0;
|
2014-12-28 17:37:12 -05:00
|
|
|
last_codectype = NULL;
|
2009-06-07 12:58:02 -04:00
|
|
|
while (((ret = db_query_fetch_file(&qp, &dbmfi)) == 0) && (dbmfi.id))
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
|
|
|
nsongs++;
|
|
|
|
|
2014-12-28 17:37:12 -05:00
|
|
|
if (!dbmfi.codectype)
|
2014-08-22 18:02:01 -04:00
|
|
|
{
|
2014-12-28 17:37:12 -05:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Cannot transcode '%s', codec type is unknown\n", dbmfi.fname);
|
|
|
|
|
|
|
|
transcode = 0;
|
2014-08-22 18:02:01 -04:00
|
|
|
}
|
2017-11-07 18:32:03 -05:00
|
|
|
else if (s->is_remote)
|
2014-12-28 17:37:12 -05:00
|
|
|
{
|
|
|
|
transcode = 1;
|
|
|
|
}
|
|
|
|
else if (!last_codectype || (strcmp(last_codectype, dbmfi.codectype) != 0))
|
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
transcode = transcode_needed(hreq->user_agent, client_codecs, dbmfi.codectype);
|
2014-12-28 17:37:12 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
free(last_codectype);
|
2014-12-28 17:37:12 -05:00
|
|
|
last_codectype = strdup(dbmfi.codectype);
|
|
|
|
}
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2013-12-25 17:06:41 -05:00
|
|
|
ret = dmap_encode_file_metadata(songlist, song, &dbmfi, meta, nmeta, sort_headers, transcode);
|
2011-04-02 09:07:26 -04:00
|
|
|
if (ret < 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2011-04-02 09:07:26 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Failed to encode song metadata\n");
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2011-04-02 09:07:26 -04:00
|
|
|
ret = -100;
|
|
|
|
break;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2010-08-30 10:59:48 -04:00
|
|
|
if (sort_headers)
|
|
|
|
{
|
2011-03-15 16:24:24 -04:00
|
|
|
ret = daap_sort_build(sctx, dbmfi.title_sort);
|
2010-08-30 10:59:48 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not add sort header to DAAP song list reply\n");
|
|
|
|
|
|
|
|
ret = -100;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:51:13 -04:00
|
|
|
DPRINTF(E_SPAM, L_DAAP, "Done with song\n");
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2009-04-28 16:51:52 -04:00
|
|
|
DPRINTF(E_DBG, L_DAAP, "Done with song list, %d songs\n", nsongs);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
free(last_codectype);
|
|
|
|
free(meta);
|
|
|
|
db_query_end(&qp);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
if (ret == -100)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, tag, "Out of memory");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Error fetching results\n");
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, tag, "Error fetching query results");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add header to evbuf, add songlist to evbuf */
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(songlist);
|
2010-08-30 10:59:48 -04:00
|
|
|
if (sort_headers)
|
2013-11-10 06:35:24 -05:00
|
|
|
{
|
|
|
|
daap_sort_finalize(sctx);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, tag, len + evbuffer_get_length(sctx->headerlist) + 61);
|
2013-11-10 06:35:24 -05:00
|
|
|
}
|
2010-08-30 10:59:48 -04:00
|
|
|
else
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, tag, len + 53);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_int(hreq->reply, "mstt", 200); /* 12 */
|
|
|
|
dmap_add_char(hreq->reply, "muty", 0); /* 9 */
|
|
|
|
dmap_add_int(hreq->reply, "mtco", qp.results); /* 12 */
|
|
|
|
dmap_add_int(hreq->reply, "mrco", nsongs); /* 12 */
|
|
|
|
dmap_add_container(hreq->reply, "mlcl", len); /* 8 */
|
2010-08-30 10:59:48 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(hreq->reply, songlist));
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2010-08-30 10:59:48 -04:00
|
|
|
if (sort_headers)
|
|
|
|
{
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(sctx->headerlist);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "mshl", len); /* 8 */
|
2010-08-30 10:59:48 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(hreq->reply, sctx->headerlist));
|
2010-08-30 10:59:48 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
daap_sort_context_free(sctx);
|
|
|
|
evbuffer_free(song);
|
|
|
|
evbuffer_free(songlist);
|
|
|
|
free_query_params(&qp, 1);
|
2010-01-10 08:34:48 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_OK;
|
2010-01-10 08:34:48 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
error:
|
|
|
|
daap_sort_context_free(sctx);
|
2010-01-10 08:34:48 -05:00
|
|
|
evbuffer_free(song);
|
|
|
|
evbuffer_free(songlist);
|
2017-10-26 17:01:07 -04:00
|
|
|
free_query_params(&qp, 1);
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_dbsonglist(struct httpd_request *hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
return daap_reply_songlist_generic(hreq, -1);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_plsonglist(struct httpd_request *hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
|
|
|
int playlist;
|
|
|
|
int ret;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
ret = safe_atoi32(hreq->uri_parsed->path_parts[3], &playlist);
|
2009-04-28 11:48:57 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "apso", "Invalid playlist ID");
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2018-03-11 17:40:07 -04:00
|
|
|
// This is a work-around for Remote for iTunes that for unknown reasons
|
|
|
|
// sometimes requests playlist 0
|
|
|
|
if (playlist == 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Client '%s' made invalid request for playlist 0, returning playlist 1\n", hreq->user_agent);
|
|
|
|
|
|
|
|
playlist = 1;
|
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
return daap_reply_songlist_generic(hreq, playlist);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_playlists(struct httpd_request *hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2009-06-07 12:58:02 -04:00
|
|
|
struct query_params qp;
|
|
|
|
struct db_playlist_info dbpli;
|
2009-04-28 11:48:57 -04:00
|
|
|
struct evbuffer *playlistlist;
|
|
|
|
struct evbuffer *playlist;
|
2011-04-01 13:01:35 -04:00
|
|
|
const struct dmap_field_map *dfm;
|
|
|
|
const struct dmap_field *df;
|
|
|
|
const struct dmap_field **meta;
|
2009-04-28 11:48:57 -04:00
|
|
|
const char *param;
|
|
|
|
char **strval;
|
2015-07-29 17:05:47 -04:00
|
|
|
size_t len;
|
2015-03-16 18:33:42 -04:00
|
|
|
int database;
|
2015-03-30 15:10:59 -04:00
|
|
|
int cfg_radiopl;
|
2009-04-28 11:48:57 -04:00
|
|
|
int nmeta;
|
|
|
|
int npls;
|
2013-12-17 17:27:37 -05:00
|
|
|
int32_t plid;
|
|
|
|
int32_t pltype;
|
|
|
|
int32_t plitems;
|
2015-03-16 18:33:42 -04:00
|
|
|
int32_t plstreams;
|
2015-03-14 17:00:57 -04:00
|
|
|
int32_t plparent;
|
2009-04-28 16:51:52 -04:00
|
|
|
int i;
|
2009-04-28 11:48:57 -04:00
|
|
|
int ret;
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
cfg_radiopl = cfg_getbool(cfg_getsec(cfg, "library"), "radio_playlists");
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
ret = safe_atoi32(hreq->uri_parsed->path_parts[1], &database);
|
2009-04-28 11:48:57 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "aply", "Invalid database ID");
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
query_params_set(&qp, NULL, hreq, Q_PL);
|
2019-08-13 15:35:07 -04:00
|
|
|
qp.sort = S_PLAYLIST; // Only S_PLAYLIST (and S_NONE) works for Q_PL
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_NULL(L_DAAP, playlistlist = evbuffer_new());
|
|
|
|
CHECK_NULL(L_DAAP, playlist = evbuffer_new());
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(hreq->reply, 61));
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(playlistlist, 1024));
|
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(playlist, 128));
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "meta");
|
2009-04-28 11:48:57 -04:00
|
|
|
if (!param)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "No meta parameter in query, using default\n");
|
|
|
|
|
|
|
|
param = default_meta_pl;
|
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
nmeta = parse_meta(&meta, param);
|
2009-04-28 11:48:57 -04:00
|
|
|
if (nmeta < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Failed to parse meta parameter in DAAP query\n");
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "aply", "Failed to parse query");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
ret = db_query_start(&qp);
|
|
|
|
if (ret < 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2009-06-07 12:58:02 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not start query\n");
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
free(meta);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "aply", "Could not start query");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
npls = 0;
|
2019-05-28 17:40:41 -04:00
|
|
|
while (((ret = db_query_fetch_pl(&qp, &dbpli)) == 0) && (dbpli.id))
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2013-12-17 17:27:37 -05:00
|
|
|
plid = 1;
|
|
|
|
if (safe_atoi32(dbpli.id, &plid) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pltype = 0;
|
|
|
|
if (safe_atoi32(dbpli.type, &pltype) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
plitems = 0;
|
|
|
|
if (safe_atoi32(dbpli.items, &plitems) != 0)
|
|
|
|
continue;
|
|
|
|
|
2015-03-16 18:33:42 -04:00
|
|
|
plstreams = 0;
|
|
|
|
if (safe_atoi32(dbpli.streams, &plstreams) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Database DAAP_DB_RADIO is radio, so for that db skip playlists without
|
|
|
|
* streams and for other databases skip playlists which are just streams
|
|
|
|
*/
|
|
|
|
if ((database == DAAP_DB_RADIO) && (plstreams == 0))
|
|
|
|
continue;
|
2015-03-30 15:10:59 -04:00
|
|
|
if (!cfg_radiopl && (database != DAAP_DB_RADIO) && (plstreams > 0) && (plstreams == plitems))
|
2015-03-16 18:33:42 -04:00
|
|
|
continue;
|
|
|
|
|
2015-04-11 10:55:13 -04:00
|
|
|
/* Don't add empty Special playlists */
|
2015-04-21 13:11:48 -04:00
|
|
|
if ((plid > 1) && (plitems == 0) && (pltype == PL_SPECIAL))
|
2013-12-17 17:27:37 -05:00
|
|
|
continue;
|
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
npls++;
|
|
|
|
|
2009-04-28 16:51:52 -04:00
|
|
|
for (i = 0; i < nmeta; i++)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2011-04-01 13:01:35 -04:00
|
|
|
df = meta[i];
|
|
|
|
dfm = df->dfm;
|
2010-02-08 13:28:42 -05:00
|
|
|
|
2009-04-28 16:51:52 -04:00
|
|
|
/* dmap.itemcount - always added */
|
2011-04-01 13:01:35 -04:00
|
|
|
if (dfm == &dfm_dmap_mimc)
|
2009-04-28 11:48:57 -04:00
|
|
|
continue;
|
|
|
|
|
2015-04-11 10:55:13 -04:00
|
|
|
/* Add field "com.apple.itunes.smart-playlist" for special and smart playlists
|
|
|
|
(excluding the special playlist for "library" with id = 1) */
|
2011-04-01 13:01:35 -04:00
|
|
|
if (dfm == &dfm_dmap_aeSP)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2015-04-11 10:55:13 -04:00
|
|
|
if ((pltype == PL_SMART) || ((pltype == PL_SPECIAL) && (plid != 1)))
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2013-12-17 17:27:37 -05:00
|
|
|
dmap_add_char(playlist, "aeSP", 1);
|
2015-04-11 10:55:13 -04:00
|
|
|
}
|
2010-01-21 11:56:44 -05:00
|
|
|
|
2015-04-11 10:55:13 -04:00
|
|
|
/* Add field "com.apple.itunes.special-playlist" for special playlists
|
|
|
|
(excluding the special playlist for "library" with id = 1) */
|
|
|
|
if ((pltype == PL_SPECIAL) && (plid != 1))
|
|
|
|
{
|
|
|
|
int32_t aePS = 0;
|
2013-12-17 17:27:37 -05:00
|
|
|
ret = safe_atoi32(dbpli.special_id, &aePS);
|
|
|
|
if ((ret == 0) && (aePS > 0))
|
|
|
|
dmap_add_char(playlist, "aePS", aePS);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-04-28 16:51:52 -04:00
|
|
|
/* Not in struct playlist_info */
|
|
|
|
if (dfm->pli_offset < 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
continue;
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
strval = (char **) ((char *)&dbpli + dfm->pli_offset);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2009-04-28 16:51:52 -04:00
|
|
|
if (!(*strval) || (**strval == '\0'))
|
2009-04-28 11:48:57 -04:00
|
|
|
continue;
|
|
|
|
|
2011-04-01 13:01:35 -04:00
|
|
|
dmap_add_field(playlist, df, *strval, 0);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2013-11-14 17:08:20 -05:00
|
|
|
DPRINTF(E_SPAM, L_DAAP, "Done with meta tag %s (%s)\n", df->desc, *strval);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Item count (mimc) */
|
2015-03-29 17:34:36 -04:00
|
|
|
dmap_add_int(playlist, "mimc", plitems);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2010-07-31 04:40:39 -04:00
|
|
|
/* Container ID (mpco) */
|
2015-03-14 17:00:57 -04:00
|
|
|
ret = safe_atoi32(dbpli.parent_id, &plparent);
|
|
|
|
if (ret == 0)
|
|
|
|
dmap_add_int(playlist, "mpco", plparent);
|
|
|
|
else
|
|
|
|
dmap_add_int(playlist, "mpco", 0);
|
2010-07-31 04:40:39 -04:00
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
/* Base playlist (abpl), id = 1 */
|
2013-12-17 17:27:37 -05:00
|
|
|
if (plid == 1)
|
2009-04-28 11:48:57 -04:00
|
|
|
dmap_add_char(playlist, "abpl", 1);
|
|
|
|
|
2017-11-05 13:34:50 -05:00
|
|
|
DPRINTF(E_SPAM, L_DAAP, "Done with playlist\n");
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(playlist);
|
|
|
|
dmap_add_container(playlistlist, "mlit", len);
|
2009-04-28 11:48:57 -04:00
|
|
|
ret = evbuffer_add_buffer(playlistlist, playlist);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2010-08-29 05:51:00 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not add playlist to playlist list for DAAP playlists reply\n");
|
|
|
|
|
|
|
|
ret = -100;
|
2009-04-28 11:48:57 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
db_query_end(&qp);
|
2009-04-28 11:48:57 -04:00
|
|
|
free(meta);
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
DPRINTF(E_DBG, L_DAAP, "Done with playlist list, %d playlists\n", npls);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
if (ret == -100)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "aply", "Out of memory");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Error fetching results\n");
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "aply", "Error fetching query results");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add header to evbuf, add playlistlist to evbuf */
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(playlistlist);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "aply", len + 53);
|
|
|
|
dmap_add_int(hreq->reply, "mstt", 200); /* 12 */
|
|
|
|
dmap_add_char(hreq->reply, "muty", 0); /* 9 */
|
|
|
|
dmap_add_int(hreq->reply, "mtco", qp.results); /* 12 */
|
|
|
|
dmap_add_int(hreq->reply,"mrco", npls); /* 12 */
|
|
|
|
dmap_add_container(hreq->reply, "mlcl", len);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(hreq->reply, playlistlist));
|
2009-06-07 12:58:02 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
evbuffer_free(playlist);
|
2009-04-28 11:48:57 -04:00
|
|
|
evbuffer_free(playlistlist);
|
2017-10-26 17:01:07 -04:00
|
|
|
free_query_params(&qp, 1);
|
2010-01-10 08:40:03 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_OK;
|
2010-01-10 08:40:03 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
error:
|
2010-01-10 08:40:03 -05:00
|
|
|
evbuffer_free(playlist);
|
|
|
|
evbuffer_free(playlistlist);
|
2017-10-26 17:01:07 -04:00
|
|
|
free_query_params(&qp, 1);
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_groups(struct httpd_request *hreq)
|
2010-01-10 07:04:57 -05:00
|
|
|
{
|
|
|
|
struct query_params qp;
|
|
|
|
struct db_group_info dbgri;
|
|
|
|
struct evbuffer *group;
|
|
|
|
struct evbuffer *grouplist;
|
2011-04-01 13:01:35 -04:00
|
|
|
const struct dmap_field_map *dfm;
|
|
|
|
const struct dmap_field *df;
|
|
|
|
const struct dmap_field **meta;
|
2010-08-29 05:59:17 -04:00
|
|
|
struct sort_ctx *sctx;
|
2015-12-12 17:28:48 -05:00
|
|
|
cfg_t *lib;
|
2010-01-10 07:04:57 -05:00
|
|
|
const char *param;
|
|
|
|
char **strval;
|
2015-07-29 17:05:47 -04:00
|
|
|
char *tag;
|
|
|
|
size_t len;
|
2010-01-10 07:04:57 -05:00
|
|
|
int nmeta;
|
2010-08-29 05:59:17 -04:00
|
|
|
int sort_headers;
|
2010-01-10 07:04:57 -05:00
|
|
|
int ngrp;
|
2010-02-02 15:02:24 -05:00
|
|
|
int32_t val;
|
2010-01-10 07:04:57 -05:00
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "group-type");
|
2013-08-29 16:00:37 -04:00
|
|
|
if (strcmp(param, "artists") == 0)
|
|
|
|
{
|
2017-03-05 03:45:34 -05:00
|
|
|
// Request from Remote may have the form:
|
|
|
|
// groups?meta=dmap.xxx,dma...&type=music&group-type=artists&sort=album&include-sort-headers=1&query=('...')&session-id=...
|
|
|
|
// Note: Since grouping by artist and sorting by album is crazy we override
|
2013-08-29 16:00:37 -04:00
|
|
|
tag = "agar";
|
2017-11-07 18:32:03 -05:00
|
|
|
query_params_set(&qp, &sort_headers, hreq, Q_GROUP_ARTISTS);
|
2013-12-03 17:35:36 -05:00
|
|
|
qp.sort = S_ARTIST;
|
2013-08-29 16:00:37 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-05 03:45:34 -05:00
|
|
|
// Request from Remote may have the form:
|
|
|
|
// groups?meta=dmap.xxx,dma...&type=music&group-type=albums&sort=artist&include-sort-headers=0&query=('...'))&session-id=...
|
|
|
|
// Sort may also be 'album'
|
2013-08-29 16:00:37 -04:00
|
|
|
tag = "agal";
|
2017-11-07 18:32:03 -05:00
|
|
|
query_params_set(&qp, &sort_headers, hreq, Q_GROUP_ALBUMS);
|
2017-03-05 03:45:34 -05:00
|
|
|
if (qp.sort == S_NONE)
|
|
|
|
qp.sort = S_ALBUM;
|
2013-08-29 16:00:37 -04:00
|
|
|
}
|
2010-01-10 07:04:57 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_NULL(L_DAAP, grouplist = evbuffer_new());
|
|
|
|
CHECK_NULL(L_DAAP, group = evbuffer_new());
|
|
|
|
CHECK_NULL(L_DAAP, sctx = daap_sort_context_new());
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(hreq->reply, 61));
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(grouplist, 1024));
|
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(group, 128));
|
2010-01-10 07:04:57 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "meta");
|
2010-01-10 07:04:57 -05:00
|
|
|
if (!param)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "No meta parameter in query, using default\n");
|
|
|
|
|
|
|
|
param = default_meta_group;
|
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
nmeta = parse_meta(&meta, param);
|
2010-01-10 07:04:57 -05:00
|
|
|
if (nmeta < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Failed to parse meta parameter in DAAP query\n");
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, tag, "Failed to parse query");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2010-08-29 05:59:17 -04:00
|
|
|
}
|
|
|
|
|
2010-01-10 07:04:57 -05:00
|
|
|
ret = db_query_start(&qp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not start query\n");
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
free(meta);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, tag, "Could not start query");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2010-01-10 07:04:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ngrp = 0;
|
|
|
|
while ((ret = db_query_fetch_group(&qp, &dbgri)) == 0)
|
|
|
|
{
|
2013-12-12 17:27:50 -05:00
|
|
|
/* Don't add item if no name (eg blank album name) */
|
|
|
|
if (strlen(dbgri.itemname) == 0)
|
|
|
|
continue;
|
|
|
|
|
2015-12-12 17:28:48 -05:00
|
|
|
/* Don't add single item albums/artists if configured to hide */
|
|
|
|
lib = cfg_getsec(cfg, "library");
|
|
|
|
if (cfg_getbool(lib, "hide_singles") && (strcmp(dbgri.itemcount, "1") == 0))
|
|
|
|
continue;
|
|
|
|
|
2010-01-10 07:04:57 -05:00
|
|
|
ngrp++;
|
|
|
|
|
|
|
|
for (i = 0; i < nmeta; i++)
|
|
|
|
{
|
2011-04-01 13:01:35 -04:00
|
|
|
df = meta[i];
|
2018-12-19 02:41:45 -05:00
|
|
|
if (!df)
|
|
|
|
continue;
|
|
|
|
|
2011-04-01 13:01:35 -04:00
|
|
|
dfm = df->dfm;
|
2010-01-10 07:04:57 -05:00
|
|
|
|
2010-02-08 13:28:42 -05:00
|
|
|
/* dmap.itemcount - always added */
|
2011-04-01 13:01:35 -04:00
|
|
|
if (dfm == &dfm_dmap_mimc)
|
2010-02-08 13:28:42 -05:00
|
|
|
continue;
|
|
|
|
|
2010-01-10 07:04:57 -05:00
|
|
|
/* Not in struct group_info */
|
|
|
|
if (dfm->gri_offset < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strval = (char **) ((char *)&dbgri + dfm->gri_offset);
|
|
|
|
|
|
|
|
if (!(*strval) || (**strval == '\0'))
|
|
|
|
continue;
|
|
|
|
|
2011-04-01 13:01:35 -04:00
|
|
|
dmap_add_field(group, df, *strval, 0);
|
2010-01-10 07:04:57 -05:00
|
|
|
|
2013-11-16 16:52:53 -05:00
|
|
|
DPRINTF(E_SPAM, L_DAAP, "Done with meta tag %s (%s)\n", df->desc, *strval);
|
2010-01-10 07:04:57 -05:00
|
|
|
}
|
|
|
|
|
2010-08-29 05:59:17 -04:00
|
|
|
if (sort_headers)
|
|
|
|
{
|
2013-12-18 13:14:39 -05:00
|
|
|
ret = daap_sort_build(sctx, dbgri.itemname_sort);
|
2010-08-29 05:59:17 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not add sort header to DAAP groups reply\n");
|
|
|
|
|
|
|
|
ret = -100;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-10 07:04:57 -05:00
|
|
|
/* Item count, always added (mimc) */
|
|
|
|
val = 0;
|
2010-02-02 15:02:24 -05:00
|
|
|
ret = safe_atoi32(dbgri.itemcount, &val);
|
2010-01-10 07:04:57 -05:00
|
|
|
if ((ret == 0) && (val > 0))
|
|
|
|
dmap_add_int(group, "mimc", val);
|
|
|
|
|
2013-08-29 16:00:37 -04:00
|
|
|
/* Song album artist (asaa), always added if group-type is albums */
|
2013-12-03 17:16:07 -05:00
|
|
|
if (qp.type == Q_GROUP_ALBUMS)
|
2013-08-29 16:00:37 -04:00
|
|
|
dmap_add_string(group, "asaa", dbgri.songalbumartist);
|
2010-01-10 07:04:57 -05:00
|
|
|
|
|
|
|
/* Item id (miid) */
|
2010-03-06 12:56:30 -05:00
|
|
|
val = 0;
|
|
|
|
ret = safe_atoi32(dbgri.id, &val);
|
|
|
|
if ((ret == 0) && (val > 0))
|
|
|
|
dmap_add_int(group, "miid", val);
|
2010-01-10 07:04:57 -05:00
|
|
|
|
2015-03-05 15:24:27 -05:00
|
|
|
DPRINTF(E_SPAM, L_DAAP, "Done with group\n");
|
2010-01-10 07:04:57 -05:00
|
|
|
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(group);
|
|
|
|
dmap_add_container(grouplist, "mlit", len);
|
2010-01-10 07:04:57 -05:00
|
|
|
ret = evbuffer_add_buffer(grouplist, group);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2010-08-29 05:51:00 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not add group to group list for DAAP groups reply\n");
|
|
|
|
|
|
|
|
ret = -100;
|
2010-01-10 07:04:57 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
db_query_end(&qp);
|
2010-01-10 07:04:57 -05:00
|
|
|
free(meta);
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
DPRINTF(E_DBG, L_DAAP, "Done with group list, %d groups\n", ngrp);
|
2010-08-29 05:59:17 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
if (ret == -100)
|
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, tag, "Out of memory");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Error fetching results\n");
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, tag, "Error fetching query results");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2010-01-10 07:04:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add header to evbuf, add grouplist to evbuf */
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(grouplist);
|
2010-08-29 05:59:17 -04:00
|
|
|
if (sort_headers)
|
2013-11-10 06:35:24 -05:00
|
|
|
{
|
|
|
|
daap_sort_finalize(sctx);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, tag, len + evbuffer_get_length(sctx->headerlist) + 61);
|
2013-11-10 06:35:24 -05:00
|
|
|
}
|
2010-08-29 05:59:17 -04:00
|
|
|
else
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, tag, len + 53);
|
2010-01-10 07:04:57 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_int(hreq->reply, "mstt", 200); /* 12 */
|
|
|
|
dmap_add_char(hreq->reply, "muty", 0); /* 9 */
|
|
|
|
dmap_add_int(hreq->reply, "mtco", qp.results); /* 12 */
|
|
|
|
dmap_add_int(hreq->reply,"mrco", ngrp); /* 12 */
|
|
|
|
dmap_add_container(hreq->reply, "mlcl", len); /* 8 */
|
2010-08-29 05:59:17 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(hreq->reply, grouplist));
|
2010-01-10 07:04:57 -05:00
|
|
|
|
2010-08-29 05:59:17 -04:00
|
|
|
if (sort_headers)
|
|
|
|
{
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(sctx->headerlist);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "mshl", len); /* 8 */
|
2010-08-29 05:59:17 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(hreq->reply, sctx->headerlist));
|
2010-08-29 05:59:17 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
daap_sort_context_free(sctx);
|
|
|
|
evbuffer_free(group);
|
|
|
|
evbuffer_free(grouplist);
|
|
|
|
free_query_params(&qp, 1);
|
2010-01-10 07:04:57 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_OK;
|
2010-01-10 07:04:57 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
error:
|
|
|
|
daap_sort_context_free(sctx);
|
2010-01-10 07:04:57 -05:00
|
|
|
evbuffer_free(group);
|
|
|
|
evbuffer_free(grouplist);
|
2017-10-26 17:01:07 -04:00
|
|
|
free_query_params(&qp, 1);
|
2013-12-03 17:16:07 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2010-01-10 07:04:57 -05:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_browse(struct httpd_request *hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2009-06-07 12:58:02 -04:00
|
|
|
struct query_params qp;
|
2009-04-28 11:48:57 -04:00
|
|
|
struct evbuffer *itemlist;
|
2010-08-14 07:58:03 -04:00
|
|
|
struct sort_ctx *sctx;
|
2009-06-07 12:58:02 -04:00
|
|
|
char *browse_item;
|
2010-12-05 10:28:33 -05:00
|
|
|
char *sort_item;
|
2009-04-28 11:48:57 -04:00
|
|
|
char *tag;
|
2015-07-29 17:05:47 -04:00
|
|
|
size_t len;
|
2010-08-14 07:58:03 -04:00
|
|
|
int sort_headers;
|
2009-04-28 11:48:57 -04:00
|
|
|
int nitems;
|
|
|
|
int ret;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
if (strcmp(hreq->uri_parsed->path_parts[3], "artists") == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
|
|
|
tag = "abar";
|
2017-11-07 18:32:03 -05:00
|
|
|
query_params_set(&qp, &sort_headers, hreq, Q_BROWSE_ARTISTS);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
2017-11-07 18:32:03 -05:00
|
|
|
else if (strcmp(hreq->uri_parsed->path_parts[3], "albums") == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
|
|
|
tag = "abal";
|
2017-11-07 18:32:03 -05:00
|
|
|
query_params_set(&qp, &sort_headers, hreq, Q_BROWSE_ALBUMS);
|
2013-12-03 17:16:07 -05:00
|
|
|
}
|
2017-11-07 18:32:03 -05:00
|
|
|
else if (strcmp(hreq->uri_parsed->path_parts[3], "genres") == 0)
|
2013-12-03 17:16:07 -05:00
|
|
|
{
|
|
|
|
tag = "abgn";
|
2017-11-07 18:32:03 -05:00
|
|
|
query_params_set(&qp, &sort_headers, hreq, Q_BROWSE_GENRES);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
2017-11-07 18:32:03 -05:00
|
|
|
else if (strcmp(hreq->uri_parsed->path_parts[3], "composers") == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
|
|
|
tag = "abcp";
|
2017-11-07 18:32:03 -05:00
|
|
|
query_params_set(&qp, &sort_headers, hreq, Q_BROWSE_COMPOSERS);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Invalid DAAP browse request type '%s'\n", hreq->uri_parsed->path_parts[3]);
|
|
|
|
dmap_error_make(hreq->reply, "abro", "Invalid browse type");
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_NULL(L_DAAP, itemlist = evbuffer_new());
|
|
|
|
CHECK_NULL(L_DAAP, sctx = daap_sort_context_new());
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(hreq->reply, 52));
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_expand(itemlist, 1024)); // Just a starting alloc, it'll expand as needed
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
ret = db_query_start(&qp);
|
|
|
|
if (ret < 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2009-06-07 12:58:02 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not start query\n");
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "abro", "Could not start query");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
nitems = 0;
|
2010-12-05 10:28:33 -05:00
|
|
|
while (((ret = db_query_fetch_string_sort(&qp, &browse_item, &sort_item)) == 0) && (browse_item))
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
|
|
|
nitems++;
|
|
|
|
|
2010-08-14 07:58:03 -04:00
|
|
|
if (sort_headers)
|
|
|
|
{
|
2010-12-05 10:28:33 -05:00
|
|
|
ret = daap_sort_build(sctx, sort_item);
|
2010-08-14 07:58:03 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not add sort header to DAAP browse reply\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
dmap_add_string(itemlist, "mlit", browse_item);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
db_query_end(&qp);
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
if (ret < 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2010-08-14 07:58:03 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Error fetching/building results\n");
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, "abro", "Error fetching/building query results");
|
2017-10-26 17:01:07 -04:00
|
|
|
goto error;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(itemlist);
|
2010-08-14 07:58:03 -04:00
|
|
|
if (sort_headers)
|
2013-11-10 06:35:24 -05:00
|
|
|
{
|
|
|
|
daap_sort_finalize(sctx);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "abro", len + evbuffer_get_length(sctx->headerlist) + 52);
|
2013-11-10 06:35:24 -05:00
|
|
|
}
|
2010-08-14 07:58:03 -04:00
|
|
|
else
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "abro", len + 44);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_int(hreq->reply, "mstt", 200); /* 12 */
|
|
|
|
dmap_add_int(hreq->reply, "mtco", qp.results); /* 12 */
|
|
|
|
dmap_add_int(hreq->reply, "mrco", nitems); /* 12 */
|
|
|
|
dmap_add_container(hreq->reply, tag, len); /* 8 */
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(hreq->reply, itemlist));
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2010-08-14 07:58:03 -04:00
|
|
|
if (sort_headers)
|
|
|
|
{
|
2015-07-29 17:05:47 -04:00
|
|
|
len = evbuffer_get_length(sctx->headerlist);
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, "mshl", len); /* 8 */
|
2013-12-03 17:16:07 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERR(L_DAAP, evbuffer_add_buffer(hreq->reply, sctx->headerlist));
|
2010-08-14 07:58:03 -04:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
daap_sort_context_free(sctx);
|
|
|
|
evbuffer_free(itemlist);
|
|
|
|
free_query_params(&qp, 1);
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_OK;
|
2013-12-03 17:16:07 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
error:
|
|
|
|
daap_sort_context_free(sctx);
|
2013-12-03 17:16:07 -05:00
|
|
|
evbuffer_free(itemlist);
|
2017-10-26 17:01:07 -04:00
|
|
|
free_query_params(&qp, 1);
|
2013-12-03 17:16:07 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
2010-03-07 05:11:09 -05:00
|
|
|
/* NOTE: We only handle artwork at the moment */
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_extra_data(struct httpd_request *hreq)
|
2010-01-03 12:11:16 -05:00
|
|
|
{
|
2014-05-29 17:22:00 -04:00
|
|
|
struct evkeyvalq *headers;
|
2017-10-26 17:01:07 -04:00
|
|
|
char clen[32];
|
2010-03-07 05:11:09 -05:00
|
|
|
const char *param;
|
2011-03-30 13:35:13 -04:00
|
|
|
char *ctype;
|
2015-07-29 17:05:47 -04:00
|
|
|
size_t len;
|
2010-03-07 05:11:09 -05:00
|
|
|
int id;
|
|
|
|
int max_w;
|
|
|
|
int max_h;
|
|
|
|
int ret;
|
2010-01-20 12:24:52 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
if (!hreq->req)
|
2017-10-26 17:01:07 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Bug! daap_reply_extra_data() cannot be called without an actual connection\n");
|
|
|
|
return DAAP_REPLY_NO_CONNECTION;
|
|
|
|
}
|
2010-01-20 12:24:52 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
ret = safe_atoi32(hreq->uri_parsed->path_parts[3], &id);
|
2010-03-07 05:11:09 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not convert id parameter to integer: '%s'\n", hreq->uri_parsed->path_parts[3]);
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_BAD_REQUEST;
|
2010-03-07 05:11:09 -05:00
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
if (evhttp_find_header(hreq->query, "mw") && evhttp_find_header(hreq->query, "mh"))
|
2010-03-07 05:11:09 -05:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "mw");
|
2013-12-25 17:06:41 -05:00
|
|
|
ret = safe_atoi32(param, &max_w);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-26 17:01:07 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not convert mw parameter to integer: '%s'\n", param);
|
|
|
|
return DAAP_REPLY_BAD_REQUEST;
|
2013-12-25 17:06:41 -05:00
|
|
|
}
|
2010-03-07 05:11:09 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
param = evhttp_find_header(hreq->query, "mh");
|
2013-12-25 17:06:41 -05:00
|
|
|
ret = safe_atoi32(param, &max_h);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-26 17:01:07 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not convert mh parameter to integer: '%s'\n", param);
|
|
|
|
return DAAP_REPLY_BAD_REQUEST;
|
2013-12-25 17:06:41 -05:00
|
|
|
}
|
2010-03-07 05:11:09 -05:00
|
|
|
}
|
2013-12-25 17:06:41 -05:00
|
|
|
else
|
2010-03-07 05:11:09 -05:00
|
|
|
{
|
2017-10-26 17:01:07 -04:00
|
|
|
DPRINTF(E_DBG, L_DAAP, "Request for artwork without mw or mh parameter\n");
|
2010-03-07 05:11:09 -05:00
|
|
|
|
2013-12-25 17:06:41 -05:00
|
|
|
max_w = 0;
|
|
|
|
max_h = 0;
|
2010-03-07 05:11:09 -05:00
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
if (strcmp(hreq->uri_parsed->path_parts[2], "groups") == 0)
|
|
|
|
ret = artwork_get_group(hreq->reply, id, max_w, max_h);
|
|
|
|
else if (strcmp(hreq->uri_parsed->path_parts[2], "items") == 0)
|
|
|
|
ret = artwork_get_item(hreq->reply, id, max_w, max_h);
|
2010-03-07 05:11:09 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
len = evbuffer_get_length(hreq->reply);
|
2015-07-29 17:05:47 -04:00
|
|
|
|
2011-03-30 13:35:13 -04:00
|
|
|
switch (ret)
|
2010-03-07 05:11:09 -05:00
|
|
|
{
|
2011-03-30 13:35:13 -04:00
|
|
|
case ART_FMT_PNG:
|
|
|
|
ctype = "image/png";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ART_FMT_JPEG:
|
|
|
|
ctype = "image/jpeg";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-07-29 17:05:47 -04:00
|
|
|
if (len > 0)
|
2017-11-07 18:32:03 -05:00
|
|
|
evbuffer_drain(hreq->reply, len);
|
2010-03-07 05:11:09 -05:00
|
|
|
|
2011-03-30 13:35:13 -04:00
|
|
|
goto no_artwork;
|
2010-03-07 05:11:09 -05:00
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
headers = evhttp_request_get_output_headers(hreq->req);
|
2014-05-29 17:22:00 -04:00
|
|
|
evhttp_remove_header(headers, "Content-Type");
|
|
|
|
evhttp_add_header(headers, "Content-Type", ctype);
|
2015-07-29 17:05:47 -04:00
|
|
|
snprintf(clen, sizeof(clen), "%ld", (long)len);
|
2014-05-29 17:22:00 -04:00
|
|
|
evhttp_add_header(headers, "Content-Length", clen);
|
2010-03-07 05:11:09 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_OK_NO_GZIP;
|
2010-03-07 05:11:09 -05:00
|
|
|
|
|
|
|
no_artwork:
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_NO_CONTENT;
|
2010-01-03 12:11:16 -05:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_stream(struct httpd_request *hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
|
|
|
int id;
|
|
|
|
int ret;
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
if (!hreq->req)
|
2015-02-06 16:14:26 -05:00
|
|
|
{
|
2017-10-26 17:01:07 -04:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Bug! daap_stream() cannot be called without an actual connection\n");
|
|
|
|
return DAAP_REPLY_NO_CONNECTION;
|
2015-02-06 16:14:26 -05:00
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
ret = safe_atoi32(hreq->uri_parsed->path_parts[3], &id);
|
2017-10-26 17:01:07 -04:00
|
|
|
if (ret < 0)
|
|
|
|
return DAAP_REPLY_BAD_REQUEST;
|
2009-11-13 15:53:47 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
httpd_stream_file(hreq->req, id);
|
2009-11-13 15:53:47 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_NONE;
|
2009-11-13 15:53:47 -05:00
|
|
|
}
|
|
|
|
|
2010-02-08 13:58:50 -05:00
|
|
|
#ifdef DMAP_TEST
|
2011-04-01 13:01:35 -04:00
|
|
|
static const struct dmap_field dmap_TEST = { "test.container", "TEST", NULL, DMAP_TYPE_LIST };
|
|
|
|
static const struct dmap_field dmap_TST1 = { "test.ubyte", "TST1", NULL, DMAP_TYPE_UBYTE };
|
|
|
|
static const struct dmap_field dmap_TST2 = { "test.byte", "TST2", NULL, DMAP_TYPE_BYTE };
|
|
|
|
static const struct dmap_field dmap_TST3 = { "test.ushort", "TST3", NULL, DMAP_TYPE_USHORT };
|
|
|
|
static const struct dmap_field dmap_TST4 = { "test.short", "TST4", NULL, DMAP_TYPE_SHORT };
|
|
|
|
static const struct dmap_field dmap_TST5 = { "test.uint", "TST5", NULL, DMAP_TYPE_UINT };
|
|
|
|
static const struct dmap_field dmap_TST6 = { "test.int", "TST6", NULL, DMAP_TYPE_INT };
|
|
|
|
static const struct dmap_field dmap_TST7 = { "test.ulong", "TST7", NULL, DMAP_TYPE_ULONG };
|
|
|
|
static const struct dmap_field dmap_TST8 = { "test.long", "TST8", NULL, DMAP_TYPE_LONG };
|
|
|
|
static const struct dmap_field dmap_TST9 = { "test.string", "TST9", NULL, DMAP_TYPE_STRING };
|
2010-02-08 13:58:50 -05:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
static enum daap_reply_result
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_dmap_test(struct httpd_request *hreq)
|
2010-02-08 13:58:50 -05:00
|
|
|
{
|
|
|
|
struct evbuffer *test;
|
2017-10-26 17:01:07 -04:00
|
|
|
char buf[64];
|
2010-02-08 13:58:50 -05:00
|
|
|
int ret;
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
CHECK_NULL(L_DAAP, test = evbuffer_new());
|
2010-02-08 13:58:50 -05:00
|
|
|
|
|
|
|
/* UBYTE */
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIu8, UINT8_MAX);
|
|
|
|
dmap_add_field(test, &dmap_TST1, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
|
|
|
|
/* BYTE */
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIi8, INT8_MIN);
|
|
|
|
dmap_add_field(test, &dmap_TST2, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIi8, INT8_MAX);
|
|
|
|
dmap_add_field(test, &dmap_TST2, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
|
|
|
|
/* USHORT */
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIu16, UINT16_MAX);
|
|
|
|
dmap_add_field(test, &dmap_TST3, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
|
|
|
|
/* SHORT */
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIi16, INT16_MIN);
|
|
|
|
dmap_add_field(test, &dmap_TST4, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIi16, INT16_MAX);
|
|
|
|
dmap_add_field(test, &dmap_TST4, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
|
|
|
|
/* UINT */
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIu32, UINT32_MAX);
|
|
|
|
dmap_add_field(test, &dmap_TST5, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
|
|
|
|
/* INT */
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIi32, INT32_MIN);
|
|
|
|
dmap_add_field(test, &dmap_TST6, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIi32, INT32_MAX);
|
|
|
|
dmap_add_field(test, &dmap_TST6, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
|
|
|
|
/* ULONG */
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIu64, UINT64_MAX);
|
|
|
|
dmap_add_field(test, &dmap_TST7, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
|
|
|
|
/* LONG */
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIi64, INT64_MIN);
|
|
|
|
dmap_add_field(test, &dmap_TST8, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
snprintf(buf, sizeof(buf), "%" PRIi64, INT64_MAX);
|
|
|
|
dmap_add_field(test, &dmap_TST8, buf, 0);
|
|
|
|
dmap_add_field(test, &dmap_TST9, buf, 0);
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_add_container(hreq->reply, dmap_TEST.tag, evbuffer_get_length(test));
|
2010-02-08 13:58:50 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
ret = evbuffer_add_buffer(hreq->reply, test);
|
2010-02-08 13:58:50 -05:00
|
|
|
evbuffer_free(test);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Could not add test results to DMAP test reply\n");
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
dmap_error_make(hreq->reply, dmap_TEST.tag, "Out of memory");
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_ERROR;
|
2010-02-08 13:58:50 -05:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return DAAP_REPLY_OK_NO_GZIP;
|
2010-02-08 13:58:50 -05:00
|
|
|
}
|
|
|
|
#endif /* DMAP_TEST */
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
static struct httpd_uri_map daap_handlers[] =
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
|
|
|
{
|
|
|
|
.regexp = "^/server-info$",
|
|
|
|
.handler = daap_reply_server_info
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.regexp = "^/content-codes$",
|
|
|
|
.handler = daap_reply_content_codes
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.regexp = "^/login$",
|
|
|
|
.handler = daap_reply_login
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.regexp = "^/logout$",
|
|
|
|
.handler = daap_reply_logout
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.regexp = "^/update$",
|
|
|
|
.handler = daap_reply_update
|
|
|
|
},
|
2009-12-20 08:43:28 -05:00
|
|
|
{
|
|
|
|
.regexp = "^/activity$",
|
|
|
|
.handler = daap_reply_activity
|
|
|
|
},
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
|
|
|
.regexp = "^/databases$",
|
|
|
|
.handler = daap_reply_dblist
|
|
|
|
},
|
|
|
|
{
|
2009-11-01 06:39:11 -05:00
|
|
|
.regexp = "^/databases/[[:digit:]]+/browse/[^/]+$",
|
2009-04-28 11:48:57 -04:00
|
|
|
.handler = daap_reply_browse
|
|
|
|
},
|
|
|
|
{
|
2009-11-01 06:39:11 -05:00
|
|
|
.regexp = "^/databases/[[:digit:]]+/items$",
|
2009-04-28 11:48:57 -04:00
|
|
|
.handler = daap_reply_dbsonglist
|
|
|
|
},
|
|
|
|
{
|
2009-11-01 06:39:11 -05:00
|
|
|
.regexp = "^/databases/[[:digit:]]+/items/[[:digit:]]+[.][^/]+$",
|
2009-04-28 11:48:57 -04:00
|
|
|
.handler = daap_stream
|
|
|
|
},
|
2010-01-03 12:11:16 -05:00
|
|
|
{
|
|
|
|
.regexp = "^/databases/[[:digit:]]+/items/[[:digit:]]+/extra_data/artwork$",
|
|
|
|
.handler = daap_reply_extra_data
|
|
|
|
},
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2009-11-01 06:39:11 -05:00
|
|
|
.regexp = "^/databases/[[:digit:]]+/containers$",
|
2009-04-28 11:48:57 -04:00
|
|
|
.handler = daap_reply_playlists
|
|
|
|
},
|
|
|
|
{
|
2009-11-01 06:39:11 -05:00
|
|
|
.regexp = "^/databases/[[:digit:]]+/containers/[[:digit:]]+/items$",
|
2009-04-28 11:48:57 -04:00
|
|
|
.handler = daap_reply_plsonglist
|
|
|
|
},
|
2010-01-10 07:04:57 -05:00
|
|
|
{
|
|
|
|
.regexp = "^/databases/[[:digit:]]+/groups$",
|
|
|
|
.handler = daap_reply_groups
|
|
|
|
},
|
2010-01-03 12:11:16 -05:00
|
|
|
{
|
|
|
|
.regexp = "^/databases/[[:digit:]]+/groups/[[:digit:]]+/extra_data/artwork$",
|
|
|
|
.handler = daap_reply_extra_data
|
|
|
|
},
|
2010-02-08 13:58:50 -05:00
|
|
|
#ifdef DMAP_TEST
|
|
|
|
{
|
|
|
|
.regexp = "^/dmap-test$",
|
|
|
|
.handler = daap_reply_dmap_test
|
|
|
|
},
|
|
|
|
#endif /* DMAP_TEST */
|
2017-11-11 15:17:42 -05:00
|
|
|
{
|
2009-04-28 11:48:57 -04:00
|
|
|
.regexp = NULL,
|
|
|
|
.handler = NULL
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
/* ------------------------------- DAAP API --------------------------------- */
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
/* iTunes 9 gives us an absolute request-uri like
|
|
|
|
* daap://10.1.1.20:3689/server-info
|
|
|
|
* iTunes 12.1 gives us an absolute request-uri for streaming like
|
|
|
|
* http://10.1.1.20:3689/databases/1/items/1.mp3
|
|
|
|
*/
|
2009-04-28 11:48:57 -04:00
|
|
|
void
|
2017-11-04 16:24:42 -04:00
|
|
|
daap_request(struct evhttp_request *req, struct httpd_uri_parsed *uri_parsed)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
struct httpd_request *hreq;
|
2014-05-29 17:22:00 -04:00
|
|
|
struct evkeyvalq *headers;
|
2014-08-23 19:23:23 -04:00
|
|
|
struct timespec start;
|
|
|
|
struct timespec end;
|
2017-11-07 18:32:03 -05:00
|
|
|
struct daap_session session;
|
|
|
|
const char *param;
|
|
|
|
int32_t id;
|
2009-04-28 11:48:57 -04:00
|
|
|
int ret;
|
2017-10-26 17:01:07 -04:00
|
|
|
int msec;
|
2009-11-13 15:53:47 -05:00
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
DPRINTF(E_DBG, L_DAAP, "DAAP request: '%s'\n", uri_parsed->uri);
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
hreq = httpd_request_parse(req, uri_parsed, NULL, daap_handlers);
|
|
|
|
if (!hreq)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Unrecognized path '%s' in DAAP request: '%s'\n", uri_parsed->path, uri_parsed->uri);
|
|
|
|
|
2016-10-18 16:45:22 -04:00
|
|
|
httpd_send_error(req, HTTP_BADREQUEST, "Bad Request");
|
2009-04-28 11:48:57 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
// Check if we have a session and point hreq->extra_data to it
|
|
|
|
param = evhttp_find_header(hreq->query, "session-id");
|
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
ret = safe_atoi32(param, &id);
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_DAAP, "Ignoring non-numeric session id in DAAP request: '%s'\n", uri_parsed->uri);
|
|
|
|
else
|
|
|
|
hreq->extra_data = daap_session_get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an ad-hoc session, which is a way of passing is_remote to the handler, even though no real session exists
|
|
|
|
if (!hreq->extra_data)
|
|
|
|
{
|
|
|
|
memset(&session, 0, sizeof(struct daap_session));
|
|
|
|
session.is_remote = (evhttp_find_header(hreq->query, "pairing-guid") != NULL);
|
|
|
|
hreq->extra_data = &session;
|
|
|
|
}
|
|
|
|
|
2018-09-30 16:39:53 -04:00
|
|
|
ret = daap_request_authorize(hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
free(hreq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-21 04:01:47 -04:00
|
|
|
// Set reply headers
|
|
|
|
headers = evhttp_request_get_output_headers(req);
|
|
|
|
evhttp_add_header(headers, "Accept-Ranges", "bytes");
|
|
|
|
evhttp_add_header(headers, "DAAP-Server", "forked-daapd/" VERSION);
|
2017-11-07 18:32:03 -05:00
|
|
|
// Content-Type for all replies, even the actual audio streaming. Note that
|
|
|
|
// video streaming will override this Content-Type with a more appropriate
|
|
|
|
// video/<type> Content-Type as expected by clients like Front Row.
|
2014-08-21 04:01:47 -04:00
|
|
|
evhttp_add_header(headers, "Content-Type", "application/x-dmap-tagged");
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
// Now we create the actual reply
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_NULL(L_DAAP, hreq->reply = evbuffer_new());
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2014-11-10 16:53:08 -05:00
|
|
|
// Try the cache
|
2017-11-07 18:32:03 -05:00
|
|
|
ret = cache_daap_get(hreq->reply, uri_parsed->uri);
|
2014-11-10 16:53:08 -05:00
|
|
|
if (ret == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2016-10-17 13:35:37 -04:00
|
|
|
// The cache will return the data gzipped, so httpd_send_reply won't need to do it
|
|
|
|
evhttp_add_header(headers, "Content-Encoding", "gzip");
|
2017-11-07 18:32:03 -05:00
|
|
|
httpd_send_reply(req, HTTP_OK, "OK", hreq->reply, HTTPD_SEND_NO_GZIP); // TODO not all want this reply
|
2009-04-28 11:48:57 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
evbuffer_free(hreq->reply);
|
|
|
|
free(hreq);
|
2009-04-28 11:48:57 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
// No dice, let's call the handler so it can construct a reply and then send it (note that the reply may be an error)
|
2014-08-23 19:23:23 -04:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
2017-11-07 18:32:03 -05:00
|
|
|
|
|
|
|
ret = hreq->handler(hreq);
|
2017-11-11 15:17:42 -05:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_send(hreq, ret);
|
|
|
|
|
2014-08-23 19:23:23 -04:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
|
|
|
msec = (end.tv_sec * 1000 + end.tv_nsec / 1000000) - (start.tv_sec * 1000 + start.tv_nsec / 1000000);
|
|
|
|
|
2015-01-06 17:26:29 -05:00
|
|
|
DPRINTF(E_DBG, L_DAAP, "DAAP request handled in %d milliseconds\n", msec);
|
2014-08-23 19:23:23 -04:00
|
|
|
|
2018-08-24 10:31:36 -04:00
|
|
|
if (ret == DAAP_REPLY_OK && msec > cache_daap_threshold() && hreq->user_agent)
|
2017-11-07 18:32:03 -05:00
|
|
|
cache_daap_add(uri_parsed->uri, hreq->user_agent, ((struct daap_session *)hreq->extra_data)->is_remote, msec);
|
2014-08-23 19:23:23 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
evbuffer_free(hreq->reply);
|
|
|
|
free(hreq);
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-11-04 16:24:42 -04:00
|
|
|
daap_is_request(const char *path)
|
2009-04-28 11:48:57 -04:00
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strncmp(path, "/databases/", strlen("/databases/")) == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
return 1;
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strcmp(path, "/databases") == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
return 1;
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strcmp(path, "/server-info") == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
return 1;
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strcmp(path, "/content-codes") == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
return 1;
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strcmp(path, "/login") == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
return 1;
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strcmp(path, "/update") == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
return 1;
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strcmp(path, "/activity") == 0)
|
2009-12-20 08:43:28 -05:00
|
|
|
return 1;
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strcmp(path, "/logout") == 0)
|
2009-04-28 11:48:57 -04:00
|
|
|
return 1;
|
|
|
|
|
2010-02-08 13:58:50 -05:00
|
|
|
#ifdef DMAP_TEST
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strcmp(path, "/dmap-test") == 0)
|
2010-02-08 13:58:50 -05:00
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
int
|
|
|
|
daap_session_is_valid(int id)
|
2014-08-19 18:21:48 -04:00
|
|
|
{
|
2017-10-26 17:01:07 -04:00
|
|
|
struct daap_session *session;
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
session = daap_session_get(id);
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
if (session)
|
|
|
|
session->mtime = time(NULL);
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return session ? 1 : 0;
|
|
|
|
}
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
// Thread: Cache
|
2017-10-26 17:01:07 -04:00
|
|
|
struct evbuffer *
|
2017-11-07 18:32:03 -05:00
|
|
|
daap_reply_build(const char *uri, const char *user_agent, int is_remote)
|
2017-10-26 17:01:07 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
struct httpd_request *hreq;
|
2017-11-04 16:24:42 -04:00
|
|
|
struct httpd_uri_parsed *uri_parsed;
|
2017-10-26 17:01:07 -04:00
|
|
|
struct evbuffer *reply;
|
2017-11-07 18:32:03 -05:00
|
|
|
struct daap_session session;
|
2017-10-26 17:01:07 -04:00
|
|
|
int ret;
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
DPRINTF(E_DBG, L_DAAP, "Building reply for DAAP request: '%s'\n", uri);
|
2014-08-21 04:01:47 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
reply = NULL;
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
uri_parsed = httpd_uri_parse(uri);
|
|
|
|
if (!uri_parsed)
|
|
|
|
return NULL;
|
2014-08-19 18:21:48 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
hreq = httpd_request_parse(NULL, uri_parsed, user_agent, daap_handlers);
|
|
|
|
if (!hreq)
|
2014-08-21 04:01:47 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
DPRINTF(E_LOG, L_DAAP, "Cannot build reply, unrecognized path '%s' in request: '%s'\n", uri_parsed->path, uri_parsed->uri);
|
|
|
|
goto out_free_uri;
|
2014-08-21 04:01:47 -04:00
|
|
|
}
|
2014-08-19 18:21:48 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
memset(&session, 0, sizeof(struct daap_session));
|
|
|
|
session.is_remote = (bool)is_remote;
|
|
|
|
|
|
|
|
hreq->extra_data = &session;
|
|
|
|
|
|
|
|
CHECK_NULL(L_DAAP, hreq->reply = evbuffer_new());
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
ret = hreq->handler(hreq);
|
2014-08-21 04:01:47 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
evbuffer_free(hreq->reply);
|
|
|
|
goto out_free_hreq;
|
2014-08-21 04:01:47 -04:00
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
reply = hreq->reply;
|
|
|
|
|
|
|
|
out_free_hreq:
|
|
|
|
free(hreq);
|
|
|
|
out_free_uri:
|
2017-11-04 16:24:42 -04:00
|
|
|
httpd_uri_free(uri_parsed);
|
2014-08-19 18:21:48 -04:00
|
|
|
|
2017-10-26 17:01:07 -04:00
|
|
|
return reply;
|
2014-08-19 18:21:48 -04:00
|
|
|
}
|
|
|
|
|
2009-04-28 11:48:57 -04:00
|
|
|
int
|
|
|
|
daap_init(void)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
srand((unsigned)time(NULL));
|
2011-09-10 12:37:16 -04:00
|
|
|
current_rev = 2;
|
2010-01-28 13:20:00 -05:00
|
|
|
update_requests = NULL;
|
2009-04-28 11:48:57 -04:00
|
|
|
|
|
|
|
for (i = 0; daap_handlers[i].handler; i++)
|
|
|
|
{
|
|
|
|
ret = regcomp(&daap_handlers[i].preg, daap_handlers[i].regexp, REG_EXTENDED | REG_NOSUB);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
regerror(ret, &daap_handlers[i].preg, buf, sizeof(buf));
|
|
|
|
|
|
|
|
DPRINTF(E_FATAL, L_DAAP, "DAAP init failed; regexp error: %s\n", buf);
|
2011-03-31 12:05:41 -04:00
|
|
|
return -1;
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
daap_deinit(void)
|
|
|
|
{
|
2015-02-24 16:58:47 -05:00
|
|
|
struct daap_session *s;
|
2010-01-28 13:20:00 -05:00
|
|
|
struct daap_update_request *ur;
|
2014-05-29 17:22:00 -04:00
|
|
|
struct evhttp_connection *evcon;
|
2009-04-28 11:48:57 -04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; daap_handlers[i].handler; i++)
|
|
|
|
regfree(&daap_handlers[i].preg);
|
2009-04-28 16:51:52 -04:00
|
|
|
|
2015-02-24 16:58:47 -05:00
|
|
|
for (s = daap_sessions; daap_sessions; s = daap_sessions)
|
|
|
|
{
|
|
|
|
daap_sessions = s->next;
|
|
|
|
daap_session_free(s);
|
|
|
|
}
|
2010-01-28 13:20:00 -05:00
|
|
|
|
|
|
|
for (ur = update_requests; update_requests; ur = update_requests)
|
|
|
|
{
|
|
|
|
update_requests = ur->next;
|
|
|
|
|
2014-05-29 17:22:00 -04:00
|
|
|
evcon = evhttp_request_get_connection(ur->req);
|
|
|
|
if (evcon)
|
2010-07-30 15:42:53 -04:00
|
|
|
{
|
2014-05-29 17:22:00 -04:00
|
|
|
evhttp_connection_set_closecb(evcon, NULL, NULL);
|
|
|
|
evhttp_connection_free(evcon);
|
2010-07-30 15:42:53 -04:00
|
|
|
}
|
2010-07-23 12:15:18 -04:00
|
|
|
|
2011-09-10 12:37:16 -04:00
|
|
|
update_free(ur);
|
2010-01-28 13:20:00 -05:00
|
|
|
}
|
2009-04-28 11:48:57 -04:00
|
|
|
}
|