daap-share.c 36.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Implmentation of DAAP (e.g., iTunes Music) sharing
 *
 * Copyright (C) 2005 Charles Schmidt <cschmidt2@emich.edu>
 *
 * Modifications Copyright (C) 2008 W. Michael Petullo <mike@flyn.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"

#include <time.h>
#include <string.h>
#include <stdlib.h>

#include <glib.h>

#include <libsoup/soup.h>
#include <libsoup/soup-address.h>
#include <libsoup/soup-message.h>
#include <libsoup/soup-uri.h>
#include <libsoup/soup-server.h>

39
#include <libdmapsharing/dmap.h>
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <libdmapsharing/dmap-structure.h>

#ifdef HAVE_GSTREAMERAPP
#include <libdmapsharing/g-gst-mp3-input-stream.h>
#include <libdmapsharing/g-gst-wav-input-stream.h>
#endif /* HAVE_GSTREAMERAPP */

static void daap_share_set_property  (GObject *object,
					 guint prop_id,
					 const GValue *value,
					 GParamSpec *pspec);
static void daap_share_get_property  (GObject *object,
					 guint prop_id,
					 GValue *value,
				 	 GParamSpec *pspec);
static void daap_share_dispose	(GObject *object);
guint daap_share_get_desired_port (DMAPShare *share);
const char *daap_share_get_type_of_service (DMAPShare *share);
void daap_share_server_info (DMAPShare         *share,
			     SoupServer        *server,
	  		     SoupMessage       *message,
			     const char        *path,
			     GHashTable        *query,
			     SoupClientContext *context);
void daap_share_databases (DMAPShare         *share,
			     SoupServer        *server,
	  		     SoupMessage       *message,
			     const char        *path,
			     GHashTable        *query,
			     SoupClientContext *context);
void daap_share_message_add_standard_headers (SoupMessage *message);

#define DAAP_TYPE_OF_SERVICE "_daap._tcp"
#define DAAP_PORT 3689

struct DAAPSharePrivate {
	/* db things */
	DMAPDb *db;
	DMAPContainerDb *container_db;

	/* FIXME: eventually, this should be determined dynamically, based
	 * on what client has connected and its supported mimetypes.
	 */
	gchar *transcode_mimetype;
};

#define DAAP_SHARE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_DAAP_SHARE, DAAPSharePrivate))

enum {
	PROP_0,
	PROP_DB,
	PROP_CONTAINER_DB,
	PROP_TRANSCODE_MIMETYPE
};

/* Provide two items as user_data to callback. */
typedef struct ChunkData {
	SoupServer *server;
	GInputStream *stream;
} ChunkData;


G_DEFINE_TYPE (DAAPShare, daap_share, TYPE_DMAP_SHARE)

/* FIXME: get rid of this global: */
static gchar *transcode_format = NULL;

static void
daap_share_class_init (DAAPShareClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	DMAPShareClass *parent_class = DMAP_SHARE_CLASS (object_class);

	object_class->get_property = daap_share_get_property;
	object_class->set_property = daap_share_set_property;
	object_class->dispose = daap_share_dispose;

	parent_class->get_desired_port    = daap_share_get_desired_port;
	parent_class->get_type_of_service = daap_share_get_type_of_service;
	parent_class->message_add_standard_headers = daap_share_message_add_standard_headers;
	parent_class->server_info         = daap_share_server_info;
	parent_class->databases           = daap_share_databases;

	/* FIXME?: */
	g_object_class_install_property (object_class,
                                         PROP_DB,
                                         g_param_spec_pointer ("db",
                                                              "DB",
                                                              "DB object",
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	g_object_class_install_property (object_class,
                                         PROP_CONTAINER_DB,
                                         g_param_spec_pointer ("container-db",
                                                              "Container DB",
                                                              "Container DB object",
                                                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	/* FIXME?: */
	g_object_class_install_property (object_class,
                                         PROP_TRANSCODE_MIMETYPE,
                                         g_param_spec_string ("transcode-mimetype",
                                                             "Transcode mimetype",
							     "Set mimetype of stream after transcoding",
                                                             NULL,
                                                             G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	g_type_class_add_private (klass, sizeof (DAAPSharePrivate));
}

static void
daap_share_init (DAAPShare *share)
{
	share->priv = DAAP_SHARE_GET_PRIVATE (share);
	/* FIXME: do I need to manually call parent _init? */
}

static void
daap_share_set_property (GObject *object,
			    guint prop_id,
			    const GValue *value,
			    GParamSpec *pspec)
{
	DAAPShare *share = DAAP_SHARE (object);

	switch (prop_id) {
	/* FIXME: */
	case PROP_DB:
		share->priv->db = (DMAPDb *) g_value_get_pointer (value);
		break;
	case PROP_CONTAINER_DB:
		share->priv->container_db = (DMAPContainerDb *) g_value_get_pointer (value);
		break;
	case PROP_TRANSCODE_MIMETYPE:
		share->priv->transcode_mimetype = (gchar *) g_value_dup_string (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
daap_share_get_property (GObject *object,
			    guint prop_id,
			    GValue *value,
			    GParamSpec *pspec)
{
	DAAPShare *share = DAAP_SHARE (object);

	switch (prop_id) {
	case PROP_DB:
		g_value_set_pointer (value, share->priv->db);
		break;
	case PROP_CONTAINER_DB:
		g_value_set_pointer (value, share->priv->container_db);
		break;
	case PROP_TRANSCODE_MIMETYPE:
		g_value_set_string (value, share->priv->transcode_mimetype);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
daap_share_dispose (GObject *object)
{
	/* FIXME: implement in parent */
}

static gchar *mime_to_format (const gchar *transcode_mimetype)
{
	if (! transcode_mimetype) {
		return NULL;
	} else if (! strcmp (transcode_mimetype, "audio/wav")) {
		return g_strdup ("wav");
	} else if ( ! strcmp (transcode_mimetype, "audio/mp3")) {
		return g_strdup ("mp3");
	} else
		return NULL;
}

DAAPShare *
daap_share_new (const char *name,
		const char *password,
		DMAPDb *db,
		DMAPContainerDb *container_db,
		gchar *transcode_mimetype)
{
	DAAPShare *share;

	share = DAAP_SHARE (g_object_new (TYPE_DAAP_SHARE,
					     "name", name,
					     "password", password,
					     "db", db,
					     "container-db", container_db,
					     "transcode-mimetype", transcode_mimetype,
					     NULL));

241
242
	_dmap_share_server_start (DMAP_SHARE (share));
	_dmap_share_publish_start (DMAP_SHARE (share));
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

	transcode_format = mime_to_format (transcode_mimetype);

	return share;
}

void
daap_share_message_add_standard_headers (SoupMessage *message)
{
	soup_message_headers_append (message->response_headers, "DMAP-Server", "libdmapsharing" VERSION);
}

#define DAAP_STATUS_OK 200

#define DMAP_VERSION 2.0
#define DAAP_VERSION 3.0
#define DAAP_TIMEOUT 1800

guint
daap_share_get_desired_port (DMAPShare *share)
{
	return DAAP_PORT;
}

const char *
daap_share_get_type_of_service (DMAPShare *share)
{
	return DAAP_TYPE_OF_SERVICE;
}

void
daap_share_server_info (DMAPShare *share,
		SoupServer        *server,
	  	SoupMessage       *message,
		const char        *path,
		GHashTable        *query,
		SoupClientContext *context)
{
/* MSRV	server info response
 * 	MSTT status
 * 	MPRO daap version
 * 	APRO daap version
 * 	MINM name
 * 	MSAU authentication method
 * 	MSLR login required
 * 	MSTM timeout interval
 * 	MSAL supports auto logout
 * 	MSUP supports update
 * 	MSPI supports persistent ids
 * 	MSEX supports extensions
 * 	MSBR supports browse
 * 	MSQY supports query
 * 	MSIX supports index
 * 	MSRS supports resolve
 * 	MSDC databases count
 */
	gchar *nameprop;
	GNode *msrv;

	g_debug ("Path is %s.", path);

	g_object_get ((gpointer) share, "name", &nameprop, NULL);

	msrv = dmap_structure_add (NULL, DMAP_CC_MSRV);
	dmap_structure_add (msrv, DMAP_CC_MSTT, (gint32) DAAP_STATUS_OK);
	dmap_structure_add (msrv, DMAP_CC_MPRO, (gdouble) DAAP_VERSION);
	dmap_structure_add (msrv, DMAP_CC_APRO, (gdouble) DAAP_VERSION);
	/* 2/3 is for itunes 4.8 (at least).  its determined by the
	 * Client-DAAP-Version header sent, but if we decide not to support
	 * older versions..? anyway
	 *
	 * 1.0 is 1/1
	 * 2.0 is 1/2
	 * 3.0 is 2/3
	 */
	dmap_structure_add (msrv, DMAP_CC_MINM, nameprop);
319
	dmap_structure_add (msrv, DMAP_CC_MSAU, _dmap_share_get_auth_method (share));
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
	/* authentication method
	 * 0 is nothing
	 * 1 is name & password
	 * 2 is password only
	 */
	dmap_structure_add (msrv, DMAP_CC_MSLR, 0);
	dmap_structure_add (msrv, DMAP_CC_MSTM, (gint32) DAAP_TIMEOUT);
	dmap_structure_add (msrv, DMAP_CC_MSAL, (gchar) 0);
	dmap_structure_add (msrv, DMAP_CC_MSUP, (gchar) 0);
	dmap_structure_add (msrv, DMAP_CC_MSPI, (gchar) 0);
	dmap_structure_add (msrv, DMAP_CC_MSEX, (gchar) 0);
	dmap_structure_add (msrv, DMAP_CC_MSBR, (gchar) 0);
	dmap_structure_add (msrv, DMAP_CC_MSQY, (gchar) 0);
	dmap_structure_add (msrv, DMAP_CC_MSIX, (gchar) 0);
	dmap_structure_add (msrv, DMAP_CC_MSRS, (gchar) 0);
	dmap_structure_add (msrv, DMAP_CC_MSDC, (gint32) 1);

337
	_dmap_share_message_set_from_dmap_structure (share, message, msrv);
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
	dmap_structure_destroy (msrv);

	g_free (nameprop);
}

typedef enum {
	ITEM_ID = 0,
	ITEM_NAME,
	ITEM_KIND,
	PERSISTENT_ID,
	CONTAINER_ITEM_ID,
	SONG_ALBUM,
	SONG_GROUPING,
	SONG_ARTIST,
	SONG_BITRATE,
	SONG_BPM,
	SONG_COMMENT,
	SONG_COMPILATION,
	SONG_COMPOSER,
	SONG_DATA_KIND,
	SONG_DATA_URL,
	SONG_DATE_ADDED,
	SONG_DATE_MODIFIED,
	SONG_DISC_COUNT,
	SONG_DISC_NUMBER,
	SONG_DISABLED,
	SONG_EQ_PRESET,
	SONG_FORMAT,
	SONG_GENRE,
	SONG_DESCRIPTION,
	SONG_RELATIVE_VOLUME,
	SONG_SAMPLE_RATE,
	SONG_SIZE,
371
372
	SONG_SORT_ALBUM,
	SONG_SORT_ARTIST,
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
	SONG_START_TIME,
	SONG_STOP_TIME,
	SONG_TIME,
	SONG_TRACK_COUNT,
	SONG_TRACK_NUMBER,
	SONG_USER_RATING,
	SONG_YEAR,
	SONG_HAS_VIDEO
} DAAPMetaData;

static struct DMAPMetaDataMap meta_data_map[] = {
	{"dmap.itemid",			ITEM_ID},
    	{"dmap.itemname",		ITEM_NAME},
    	{"dmap.itemkind",		ITEM_KIND},
    	{"dmap.persistentid",		PERSISTENT_ID},
	{"dmap.containeritemid",	CONTAINER_ITEM_ID},
    	{"daap.songalbum",		SONG_ALBUM},
    	{"daap.songartist",		SONG_ARTIST},
    	{"daap.songbitrate",		SONG_BITRATE},
    	{"daap.songbeatsperminute",	SONG_BPM},
    	{"daap.songcomment",		SONG_COMMENT},
    	{"daap.songcompilation",	SONG_COMPILATION},
    	{"daap.songcomposer",		SONG_COMPOSER},
    	{"daap.songdatakind",		SONG_DATA_KIND},
    	{"daap.songdataurl",		SONG_DATA_URL},
    	{"daap.songdateadded",		SONG_DATE_ADDED},
    	{"daap.songdatemodified",	SONG_DATE_MODIFIED},
    	{"daap.songdescription",	SONG_DESCRIPTION},
    	{"daap.songdisabled",		SONG_DISABLED},
    	{"daap.songdisccount",		SONG_DISC_COUNT},
    	{"daap.songdiscnumber",		SONG_DISC_NUMBER},
    	{"daap.songeqpreset",		SONG_EQ_PRESET},
    	{"daap.songformat",		SONG_FORMAT},
    	{"daap.songgenre",		SONG_GENRE},
    	{"daap.songgrouping",		SONG_GROUPING},
    	{"daap.songrelativevolume",	SONG_RELATIVE_VOLUME},
    	{"daap.songsamplerate",		SONG_SAMPLE_RATE},
    	{"daap.songsize",		SONG_SIZE},
    	{"daap.songstarttime",		SONG_START_TIME},
    	{"daap.songstoptime",		SONG_STOP_TIME},
   	{"daap.songtime",		SONG_TIME},
    	{"daap.songtrackcount",		SONG_TRACK_COUNT},
    	{"daap.songtracknumber",	SONG_TRACK_NUMBER},
    	{"daap.songuserrating",		SONG_USER_RATING},
    	{"daap.songyear",		SONG_YEAR},
418
419
	{"daap.sortalbum",		SONG_SORT_ALBUM},
	{"daap.sortartist",		SONG_SORT_ARTIST},
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
	{"com.apple.itunes.has-video",	SONG_HAS_VIDEO}};

#define DAAP_ITEM_KIND_AUDIO 2
#define DAAP_SONG_DATA_KIND_NONE 0

static void
write_next_chunk (SoupMessage *message, ChunkData *cd)
{
	gssize read_size;
	GError *error = NULL;
	gchar *chunk = g_malloc (DMAP_SHARE_CHUNK_SIZE);

	read_size = g_input_stream_read (cd->stream,
					 chunk,
					 DMAP_SHARE_CHUNK_SIZE,
					 NULL,
					 &error);
	if (read_size > 0) {
		soup_message_body_append (message->response_body,
					  SOUP_MEMORY_TAKE,
					  chunk,
					  read_size);
	} else {
		if (error != NULL) {
			g_warning ("Error reading from input stream: %s",
				   error->message);
			g_error_free (error);
		}
		g_free (chunk);
		g_debug ("Wrote 0 bytes, sending message complete.");
		soup_message_body_complete (message->response_body);
	}
	soup_server_unpause_message (cd->server, message);
}

static void
chunked_message_finished (SoupMessage *message, ChunkData *cd)
{
	g_debug ("Finished sending chunked file.");
	g_input_stream_close (cd->stream, NULL, NULL);
	g_free (cd);
}

static void
send_chunked_file (SoupServer *server, SoupMessage *message, DAAPRecord *record, guint64 filesize, guint64 offset, gchar *transcode_mimetype)
{
	GInputStream *stream;
	gboolean has_video;
	const char *location;
	GError *error = NULL;
	ChunkData *cd = g_new (ChunkData, 1);

	g_object_get (record, "location", &location, NULL);

	/* FIXME: This crashes on powerpc-440fp-linux-gnu:
475
	 * g_debug ("Sending %s chunked from offset %" G_GUINT64_FORMAT ".", location, offset);
476
477
478
479
480
481
482
483
484
485
	 */

	cd->server = server;

	stream = G_INPUT_STREAM (daap_record_read (record, &error));

	if (error != NULL) {
		g_warning ("Couldn't open %s: %s.", location, error->message);
		g_error_free (error);
		soup_message_set_status (message, SOUP_STATUS_INTERNAL_SERVER_ERROR);
486
		g_free (cd);
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
		return;
	}

	if (transcode_format == NULL) {
		cd->stream = stream;
#ifdef HAVE_GSTREAMERAPP
	} else if (! strcmp ("mp3", transcode_format)) {
		cd->stream = G_INPUT_STREAM (g_gst_mp3_input_stream_new (stream));
	} else if (! strcmp ("wav", transcode_format)) {
		cd->stream = G_INPUT_STREAM (g_gst_wav_input_stream_new (stream));
#endif /* HAVE_GSTREAMERAPP */
	} else {
		g_warning ("Transcode format %s not supported", transcode_format);
		cd->stream = stream;
	}

503
504
505
506
507
508
	if (cd->stream == NULL) {
		g_warning ("Could not set up input stream");
		g_free (cd);
		return;
	}

509
510
511
512
513
	if (offset != 0) {
		if (g_seekable_seek (G_SEEKABLE (cd->stream), offset, G_SEEK_SET, NULL, &error) == FALSE) {
			g_warning ("Error seeking: %s.", error->message);
			g_input_stream_close (cd->stream, NULL, NULL);
			soup_message_set_status (message, SOUP_STATUS_INTERNAL_SERVER_ERROR);
514
			g_free (cd);
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
			return;
	 	}
		filesize -= offset;
	}

	/* Free memory after each chunk sent out over network. */
	soup_message_body_set_accumulate (message->response_body, FALSE);

	g_object_get (record, "has-video", &has_video, NULL);
	if (has_video
		/* NOTE: iTunes seems to require this or it stops reading 
		 * video data after about 2.5MB. Perhaps this is so iTunes
		 * knows how much data to buffer.
		 */
	    || transcode_format == NULL) {
	    	/* NOTE: iTunes 8 (and other versions?) will not seek
		 * properly without a Content-Length header.
		 */
		g_debug ("Using HTTP 1.1 content length encoding.");
		soup_message_headers_set_encoding (message->response_headers,
						  SOUP_ENCODING_CONTENT_LENGTH);
536
		g_debug ("Content length is %" G_GUINT64_FORMAT ".", filesize);
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
		soup_message_headers_set_content_length (
			message->response_headers, filesize);
	} else if (soup_message_get_http_version (message) == SOUP_HTTP_1_0) {
		/* NOTE: Roku clients support only HTTP 1.0. */
#ifdef HAVE_ENCODING_EOF
		g_debug ("Using HTTP 1.0 encoding.");
		soup_message_headers_set_encoding (message->response_headers,
						   SOUP_ENCODING_EOF);
#else
		g_warning ("Received HTTP 1.0 request, but not built with HTTP 1.0 support");
		soup_message_headers_set_encoding (message->response_headers,
						   SOUP_ENCODING_CHUNKED);
#endif
	} else {
		/* NOTE: Can not provide Content-Length when performing
		 * real-time transcoding.
		 */
		g_debug ("Using HTTP 1.1 chunked encoding.");
		soup_message_headers_set_encoding (message->response_headers,
						   SOUP_ENCODING_CHUNKED);
	}

	soup_message_headers_append (message->response_headers, "Connection", "Close");
	soup_message_headers_append (message->response_headers, "Content-Type", "application/x-daap-tagged");

	g_signal_connect (message, "wrote_headers", G_CALLBACK (write_next_chunk), cd);
	g_signal_connect (message, "wrote_chunk", G_CALLBACK (write_next_chunk), cd);
	g_signal_connect (message, "finished", G_CALLBACK (chunked_message_finished), cd);
565
	/* NOTE: cd g_free'd by chunked_message_finished(). */
566
567
568
569
570
571
572
573
574
575
576
}

static void
add_entry_to_mlcl (gpointer id, DMAPRecord *record, gpointer _mb)
{
	GNode *mlit;
	struct MLCL_Bits *mb;

	mb = (struct MLCL_Bits *) _mb;
	mlit = dmap_structure_add (mb->mlcl, DMAP_CC_MLIT);

577
	if (_dmap_share_client_requested (mb->bits, ITEM_KIND))
578
		dmap_structure_add (mlit, DMAP_CC_MIKD, (gchar) DAAP_ITEM_KIND_AUDIO);
579
	if (_dmap_share_client_requested (mb->bits, ITEM_ID))
580
		dmap_structure_add (mlit, DMAP_CC_MIID, GPOINTER_TO_UINT (id));
581
	if (_dmap_share_client_requested (mb->bits, ITEM_NAME)) {
582
583
584
585
		const gchar *title;
		g_object_get (record, "title", &title, NULL);
		dmap_structure_add (mlit, DMAP_CC_MINM, title);
	}
586
	if (_dmap_share_client_requested (mb->bits, PERSISTENT_ID))
587
		dmap_structure_add (mlit, DMAP_CC_MPER, GPOINTER_TO_UINT (id));
588
	if (_dmap_share_client_requested (mb->bits, CONTAINER_ITEM_ID))
589
		dmap_structure_add (mlit, DMAP_CC_MCTI, GPOINTER_TO_UINT (id));
590
	if (_dmap_share_client_requested (mb->bits, SONG_DATA_KIND))
591
592
		dmap_structure_add (mlit, DMAP_CC_ASDK, (gchar) DAAP_SONG_DATA_KIND_NONE);
	/* FIXME: Any use for this?
593
	if (_dmap_share_client_requested (mb->bits, SONG_DATA_URL))
594
595
		dmap_structure_add (mlit, DMAP_CC_ASUL, "daap://192.168.0.100:%u/databases/1/items/%d.%s?session-id=%s", data->port, *id, daap_record_get_format (DAAP_RECORD (record)), data->session_id);
	*/
596
	if (_dmap_share_client_requested (mb->bits, SONG_ALBUM)) {
597
598
599
600
		const gchar *album;
		g_object_get (record, "album", &album, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASAL, album);
	}
601
	if (_dmap_share_client_requested (mb->bits, SONG_GROUPING))
602
		dmap_structure_add (mlit, DMAP_CC_AGRP, "");
603
	if (_dmap_share_client_requested (mb->bits, SONG_ARTIST)) {
604
605
606
607
		const gchar *artist;
		g_object_get (record, "artist", &artist, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASAR, artist);
	}
608
	if (_dmap_share_client_requested (mb->bits, SONG_BITRATE)) {
609
610
611
612
613
		gulong bitrate;
		g_object_get (record, "bitrate", &bitrate, NULL);
		if (bitrate != 0)
			dmap_structure_add (mlit, DMAP_CC_ASBR, (gint32) bitrate);
	}
614
	if (_dmap_share_client_requested (mb->bits, SONG_BPM))
615
		dmap_structure_add (mlit, DMAP_CC_ASBT, (gint32) 0);
616
	if (_dmap_share_client_requested (mb->bits, SONG_COMMENT))
617
		dmap_structure_add (mlit, DMAP_CC_ASCM, "");
618
	if (_dmap_share_client_requested (mb->bits, SONG_COMPILATION))
619
		dmap_structure_add (mlit, DMAP_CC_ASCO, (gchar) FALSE);
620
	if (_dmap_share_client_requested (mb->bits, SONG_COMPOSER))
621
		dmap_structure_add (mlit, DMAP_CC_ASCP, "");
622
	if (_dmap_share_client_requested (mb->bits, SONG_DATE_ADDED)) {
623
624
625
626
		gint32 firstseen;
		g_object_get (record, "firstseen", &firstseen, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASDA, firstseen);
	}
627
	if (_dmap_share_client_requested (mb->bits, SONG_DATE_MODIFIED)) {
628
629
630
631
		gint32 mtime;
		g_object_get (record, "mtime", &mtime, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASDM, mtime);
	}
632
	if (_dmap_share_client_requested (mb->bits, SONG_DISC_COUNT))
633
		dmap_structure_add (mlit, DMAP_CC_ASDC, (gint32) 0);
634
	if (_dmap_share_client_requested (mb->bits, SONG_DISC_NUMBER)) {
635
636
637
638
		gint32 disc;
		g_object_get (record, "disc", &disc, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASDN, disc);
	}
639
	if (_dmap_share_client_requested (mb->bits, SONG_DISABLED))
640
		dmap_structure_add (mlit, DMAP_CC_ASDB, (gchar) FALSE);
641
	if (_dmap_share_client_requested (mb->bits, SONG_EQ_PRESET))
642
		dmap_structure_add (mlit, DMAP_CC_ASEQ, "");
643
	if (_dmap_share_client_requested (mb->bits, SONG_FORMAT)) {
644
645
646
647
648
649
650
		gchar *format;
		if (transcode_format)
			format = transcode_format;
		else
			g_object_get (record, "format", &format, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASFM, format);
	}
651
	if (_dmap_share_client_requested (mb->bits, SONG_GENRE)) {
652
653
654
655
		gchar *genre;
		g_object_get (record, "genre", &genre, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASGN, genre);
	}
656
	if (_dmap_share_client_requested (mb->bits, SONG_DESCRIPTION))
657
		dmap_structure_add (mlit, DMAP_CC_ASDT, ""); /* FIXME: e.g., wav audio file */
658
	if (_dmap_share_client_requested (mb->bits, SONG_RELATIVE_VOLUME))
659
		dmap_structure_add (mlit, DMAP_CC_ASRV, 0);
660
	if (_dmap_share_client_requested (mb->bits, SONG_SAMPLE_RATE))
661
		dmap_structure_add (mlit, DMAP_CC_ASSR, 0);
662
	if (_dmap_share_client_requested (mb->bits, SONG_SIZE)) {
663
664
665
666
		guint64 filesize;
		g_object_get (record, "filesize", &filesize, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASSZ, (gint32) filesize);
	}
667
	if (_dmap_share_client_requested (mb->bits, SONG_START_TIME))
668
		dmap_structure_add (mlit, DMAP_CC_ASST, 0);
669
	if (_dmap_share_client_requested (mb->bits, SONG_STOP_TIME))
670
		dmap_structure_add (mlit, DMAP_CC_ASSP, 0);
671
	if (_dmap_share_client_requested (mb->bits, SONG_TIME)) {
672
673
674
675
		gint32 duration;
		g_object_get (record, "duration", &duration, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASTM, (1000 * duration));
	}
676
	if (_dmap_share_client_requested (mb->bits, SONG_TRACK_COUNT))
677
		dmap_structure_add (mlit, DMAP_CC_ASTC, 0);
678
	if (_dmap_share_client_requested (mb->bits, SONG_TRACK_NUMBER)) {
679
680
681
682
		gint32 track;
		g_object_get (record, "track", &track, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASTN, track);
	}
683
	if (_dmap_share_client_requested (mb->bits, SONG_USER_RATING)) {
684
685
686
687
		gint32 rating;
		g_object_get (record, "rating", &rating, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASUR, rating);
	}
688
	if (_dmap_share_client_requested (mb->bits, SONG_YEAR)) {
689
690
691
692
		gint32 year;
		g_object_get (record, "year", &year, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASYR, year);
	}
693
	if (_dmap_share_client_requested (mb->bits, SONG_HAS_VIDEO)) {
694
695
696
697
		gboolean has_video;
		g_object_get (record, "has-video", &has_video, NULL);
		dmap_structure_add (mlit, DMAP_CC_AEHV, has_video);
	}
698
699
700
701
702
703
704
705
706
707
708
	if (_dmap_share_client_requested (mb->bits, SONG_SORT_ARTIST)) {
		gchar *sort_artist;
		g_object_get (record, "sort-artist", &sort_artist, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASSA, sort_artist);
	}
	if (_dmap_share_client_requested (mb->bits, SONG_SORT_ALBUM)) {
		gchar *sort_album;
		g_object_get (record, "sort-album", &sort_album, NULL);
		dmap_structure_add (mlit, DMAP_CC_ASSU, sort_album);
	}

709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796

	return;
}

static void
genre_tabulator (gpointer id, DMAPRecord *record, GHashTable *ht)
{
	const gchar *genre;
	g_object_get (record, "genre", &genre, NULL);
	if (! g_hash_table_lookup (ht, genre))
		g_hash_table_insert (ht, (gchar *) genre, NULL);
}

static void
artist_tabulator (gpointer id, DMAPRecord *record, GHashTable *ht)
{
	const gchar *artist;
	g_object_get (record, "artist", &artist, NULL);
	if (! g_hash_table_lookup (ht, artist))
		g_hash_table_insert (ht, (gchar *) artist, NULL);
}

static void
album_tabulator (gpointer id, DMAPRecord *record, GHashTable *ht)
{
	const gchar *album;
	g_object_get (record, "album", &album, NULL);
	if (! g_hash_table_lookup (ht, album))
		g_hash_table_insert (ht, (gchar *) album, NULL);
}

static gchar *
get_album (DAAPRecord *record)
{
	gchar *album;
	g_object_get (record, "album", &album, NULL);
	return album;
}

static gchar *
get_genre (DAAPRecord *record)
{
	gchar *genre;
	g_object_get (record, "genre", &genre, NULL);
	return genre;
}

static gchar *
get_artist (DAAPRecord *record)
{
	gchar *artist;
	g_object_get (record, "artist", &artist, NULL);
	return artist;
}

/* FIXME: Handle ('...') and share code with DPAPShare. */
static GSList *
build_filter (gchar *filterstr)
{
	/* Produces a list of lists, each being a filter definition that may
	 * be one or more filter criteria.
	 */

	/* A filter string looks like (iTunes):
	 * 'daap.songgenre:Other'+'daap.songartist:Band'.
	 * or (Roku):
	 * 'daap.songgenre:Other' 'daap.songartist:Band'.
	 * or
         * 'dmap.itemid:1000'
	 * or
         * 'dmap.itemid:1000','dmap:itemid:1001'
	 * or
	 * 'daap.songgenre:Foo'+'daap.songartist:Bar'+'daap.songalbum:Baz'
         */

	GSList *list = NULL;

	g_debug ("Filter string is %s.", filterstr);

	if (filterstr != NULL) {
		int i;
		gchar **t1 = g_strsplit (filterstr, ",", 0);

		for (i = 0; t1[i]; i++) {
			int j;
			GSList *filter = NULL;
			gchar **t2;

797
			t2 = _dmap_db_strsplit_using_quotes (t1[i]);
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884

			for (j = 0; t2[j]; j++) {
				FilterDefinition *def;
				gchar **t3;

				t3 = g_strsplit (t2[j], ":", 0);

				if (g_strcasecmp ("dmap.itemid", t3[0]) == 0) {
					def = g_new0 (FilterDefinition, 1);
					def->value = g_strdup (t3[1]);
					def->record_get_value = NULL;
				} else if (g_strcasecmp ("daap.songgenre", t3[0]) == 0) {
					def = g_new0 (FilterDefinition, 1);
					def->value = g_strdup (t3[1]);
					def->record_get_value = (RecordGetValueFunc) get_genre;
				} else if (g_strcasecmp ("daap.songartist", t3[0]) == 0) {
					def = g_new0 (FilterDefinition, 1);
					def->value = g_strdup (t3[1]);
					def->record_get_value = (RecordGetValueFunc) get_artist;
				} else if (g_strcasecmp ("daap.songalbum", t3[0]) == 0) {
					def = g_new0 (FilterDefinition, 1);
					def->value = g_strdup (t3[1]);
					def->record_get_value = (RecordGetValueFunc) get_album;
				} else {
					g_warning ("Unknown category: %s", t3[0]);
					def = NULL;
				}

				if (def != NULL)
					filter = g_slist_append (filter, def);

				g_strfreev (t3);
			}

			list = g_slist_append (list, filter);

			g_strfreev (t2);
		}
		g_strfreev (t1);
	}

        return list;
}

static void
free_filter (GSList *filter)
{
	GSList *ptr1, *ptr2;

	for (ptr1 = filter; ptr1 != NULL; ptr1 = ptr1->next) {
		for (ptr2 = ptr1->data; ptr2 != NULL; ptr2 = ptr2->next) {
			g_free (((FilterDefinition *) ptr2->data)->value);
			g_free (ptr2->data);
		}
	}
}

static void
add_to_category_listing (gpointer key, gpointer value, gpointer user_data)
{
	GNode *mlit;
	GNode *node = (GNode *) user_data;

	mlit = dmap_structure_add (node, DMAP_CC_MLIT);
	dmap_structure_add (mlit, DMAP_RAW, (char *) key);
}

static void
debug_param (gpointer key, gpointer val, gpointer user_data)
{
	g_debug ("%s %s", (char *) key, (char *) val);
}

void
daap_share_databases (DMAPShare *share,
	      SoupServer        *server,
	      SoupMessage       *message,
	      const char        *path,
	      GHashTable        *query,
	      SoupClientContext *context)

{
	const char *rest_of_path;

	g_debug ("Path is %s.", path);
	g_hash_table_foreach (query, debug_param, NULL);

885
	if (! _dmap_share_session_id_validate (share, context, message, query, NULL)) {
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
		soup_message_set_status (message, SOUP_STATUS_FORBIDDEN);
		return;
	}

	rest_of_path = strchr (path + 1, '/');

	if (rest_of_path == NULL) {
	/* AVDB server databases
	 * 	MSTT status
	 * 	MUTY update type
	 * 	MTCO specified total count
	 * 	MRCO returned count
	 * 	MLCL listing
	 * 		MLIT listing item
	 * 			MIID item id
	 * 			MPER persistent id
	 * 			MINM item name
	 * 			MIMC item count
	 * 			MCTC container count
	 */
		gchar *nameprop;
		GNode *avdb;
		GNode *mlcl;
		GNode *mlit;

		g_object_get ((gpointer) share, "name", &nameprop, NULL);

		avdb = dmap_structure_add (NULL, DMAP_CC_AVDB);
		dmap_structure_add (avdb, DMAP_CC_MSTT, (gint32) DAAP_STATUS_OK);
		dmap_structure_add (avdb, DMAP_CC_MUTY, 0);
		dmap_structure_add (avdb, DMAP_CC_MTCO, (gint32) 1);
		dmap_structure_add (avdb, DMAP_CC_MRCO, (gint32) 1);
		mlcl = dmap_structure_add (avdb, DMAP_CC_MLCL);
		mlit = dmap_structure_add (mlcl, DMAP_CC_MLIT);
		dmap_structure_add (mlit, DMAP_CC_MIID, (gint32) 1);
		dmap_structure_add (mlit, DMAP_CC_MPER, (gint64) 1);
		dmap_structure_add (mlit, DMAP_CC_MINM, nameprop);
		dmap_structure_add (mlit, DMAP_CC_MIMC, dmap_db_count (DAAP_SHARE (share)->priv->db));
		dmap_structure_add (mlit, DMAP_CC_MCTC, (gint32) 1);

926
		_dmap_share_message_set_from_dmap_structure (share, message, avdb);
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
		dmap_structure_destroy (avdb);

		g_free (nameprop);
	} else if (g_ascii_strcasecmp ("/1/items", rest_of_path) == 0) {
	/* ADBS database songs
	 * 	MSTT status
	 * 	MUTY update type
	 * 	MTCO specified total count
	 * 	MRCO returned count
	 * 	MLCL listing
	 * 		MLIT
	 * 			attrs
	 * 		MLIT
	 * 		...
	 */
		GNode *adbs;
		gchar *record_query;
		GHashTable *records = NULL;
		gint32 num_songs;
		struct MLCL_Bits mb = {NULL,0};

		record_query = g_hash_table_lookup (query, "query");
		if (record_query) {
			GSList *filter_def;
			filter_def = build_filter (record_query);
952
			records = _dmap_db_apply_filter (DMAP_DB (DAAP_SHARE (share)->priv->db), filter_def);
953
954
955
956
957
958
959
			g_debug ("Found %d records", g_hash_table_size (records));
			num_songs = g_hash_table_size (records);
			free_filter (filter_def);
		} else {
			num_songs = dmap_db_count (DAAP_SHARE (share)->priv->db);
		}

960
		mb.bits = _dmap_share_parse_meta (query, meta_data_map, G_N_ELEMENTS (meta_data_map));
961
962
963
964
965
966
967
968
969
970
971
972
973

		adbs = dmap_structure_add (NULL, DMAP_CC_ADBS);
		dmap_structure_add (adbs, DMAP_CC_MSTT, (gint32) DAAP_STATUS_OK);
		dmap_structure_add (adbs, DMAP_CC_MUTY, 0);
		dmap_structure_add (adbs, DMAP_CC_MTCO, (gint32) num_songs);
		dmap_structure_add (adbs, DMAP_CC_MRCO, (gint32) num_songs);
		mb.mlcl = dmap_structure_add (adbs, DMAP_CC_MLCL);

		if (record_query) {
			g_hash_table_foreach (records, (GHFunc) add_entry_to_mlcl, &mb);
			/* Free hash table but not data: */
			g_hash_table_destroy (records);
		} else {
974
			dmap_db_foreach (DAAP_SHARE (share)->priv->db, (GHFunc) add_entry_to_mlcl, &mb);
975
976
		}

977
		_dmap_share_message_set_from_dmap_structure (share, message, adbs);
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
		dmap_structure_destroy (adbs);
		adbs = NULL;
	} else if (g_ascii_strcasecmp ("/1/containers", rest_of_path) == 0) {
	/* APLY database playlists
	 * 	MSTT status
	 * 	MUTY update type
	 * 	MTCO specified total count
	 * 	MRCO returned count
	 * 	MLCL listing
	 * 		MLIT listing item
	 * 			MIID item id
	 * 			MPER persistent item id
	 * 			MINM item name
	 * 			MIMC item count
	 * 			ABPL baseplaylist (only for base)
	 * 		MLIT
	 * 		...
	 */
		gchar *nameprop;
		GNode *aply;
		GNode *mlcl;
		GNode *mlit;

		g_object_get ((gpointer) share, "name", &nameprop, NULL);

		aply = dmap_structure_add (NULL, DMAP_CC_APLY);
		dmap_structure_add (aply, DMAP_CC_MSTT, (gint32) DAAP_STATUS_OK);
		dmap_structure_add (aply, DMAP_CC_MUTY, 0);
		dmap_structure_add (aply, DMAP_CC_MTCO, (gint32) dmap_container_db_count (DAAP_SHARE (share)->priv->container_db) + 1);
		dmap_structure_add (aply, DMAP_CC_MRCO, (gint32) dmap_container_db_count (DAAP_SHARE (share)->priv->container_db) + 1);
		mlcl = dmap_structure_add (aply, DMAP_CC_MLCL);

		/* Base playlist: */
		mlit = dmap_structure_add (mlcl, DMAP_CC_MLIT);
		dmap_structure_add (mlit, DMAP_CC_MIID, (gint32) 1);
		dmap_structure_add (mlit, DMAP_CC_MPER, (gint64) 1);
		dmap_structure_add (mlit, DMAP_CC_MINM, nameprop);
		dmap_structure_add (mlit, DMAP_CC_MIMC, dmap_db_count (DAAP_SHARE (share)->priv->db));
		dmap_structure_add (mlit, DMAP_CC_ABPL, (gchar) 1);

1018
		dmap_container_db_foreach (DAAP_SHARE (share)->priv->container_db, (GHFunc) _dmap_share_add_playlist_to_mlcl, (gpointer) mlcl);
1019

1020
		_dmap_share_message_set_from_dmap_structure (share, message, aply);
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
		dmap_structure_destroy (aply);

		g_free (nameprop);
	} else if (g_ascii_strncasecmp ("/1/containers/", rest_of_path, 14) == 0) {
	/* APSO playlist songs
	 * 	MSTT status
	 * 	MUTY update type
	 * 	MTCO specified total count
	 * 	MRCO returned count
	 * 	MLCL listing
	 * 		MLIT listing item
	 * 			MIKD item kind
	 * 			MIID item id
	 * 			MCTI container item id
	 * 		MLIT
	 * 		...
	 */
		GNode *apso;
		struct MLCL_Bits mb = {NULL,0};
		gint pl_id = atoi (rest_of_path + 14);

1042
		mb.bits = _dmap_share_parse_meta (query, meta_data_map, G_N_ELEMENTS (meta_data_map));
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053

		apso = dmap_structure_add (NULL, DMAP_CC_APSO);
		dmap_structure_add (apso, DMAP_CC_MSTT, (gint32) DAAP_STATUS_OK);
		dmap_structure_add (apso, DMAP_CC_MUTY, 0);

		if (pl_id == 1) {
			gint32 num_songs = dmap_db_count (DAAP_SHARE (share)->priv->db);
			dmap_structure_add (apso, DMAP_CC_MTCO, (gint32) num_songs);
			dmap_structure_add (apso, DMAP_CC_MRCO, (gint32) num_songs);
			mb.mlcl = dmap_structure_add (apso, DMAP_CC_MLCL);

1054
			dmap_db_foreach (DAAP_SHARE (share)->priv->db, (GHFunc) add_entry_to_mlcl, &mb);
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
		} else {
			DMAPContainerRecord *record;
			const DMAPDb *entries;
			guint num_songs;
			
			record = dmap_container_db_lookup_by_id (DAAP_SHARE (share)->priv->container_db, pl_id);
			entries = dmap_container_record_get_entries (record);
			num_songs = dmap_db_count (entries);
			
			dmap_structure_add (apso, DMAP_CC_MTCO, (gint32) num_songs);
			dmap_structure_add (apso, DMAP_CC_MRCO, (gint32) num_songs);
			mb.mlcl = dmap_structure_add (apso, DMAP_CC_MLCL);

1068
			dmap_db_foreach (entries, (GHFunc) add_entry_to_mlcl, &mb);
1069
1070

			g_object_unref (record);
1071
1072
		}

1073
		_dmap_share_message_set_from_dmap_structure (share, message, apso);
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
		dmap_structure_destroy (apso);
	} else if (g_ascii_strncasecmp ("/1/browse/", rest_of_path, 9) == 0) {
	/* ABRO database browse
         *      MSTT status
         *      MUTY update type
         *      MTCO specified total count
         *      MRCO returned count
         *      ABGN genre listing
         *              MLIT listing item
         *              ...
         */
                GNode *abro, *node;
		gchar *filter;
		GSList *filter_def;
		GHashTable *filtered;
		guint num_genre;
		const gchar *browse_category;
		GHashTable *category_items;
		DMAPContentCode category_cc;

		browse_category = rest_of_path + 10;
		category_items = g_hash_table_new (g_str_hash, g_str_equal);

		filter = g_hash_table_lookup (query, "filter");
		filter_def = build_filter (filter);
1099
		filtered = _dmap_db_apply_filter (DMAP_DB (DAAP_SHARE (share)->priv->db), filter_def);
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129

		if (g_ascii_strcasecmp (browse_category, "genres") == 0) {
			g_hash_table_foreach (filtered, (GHFunc) genre_tabulator, category_items);
			category_cc = DMAP_CC_ABGN;
		} else if (g_ascii_strcasecmp (browse_category, "artists") == 0) {
			g_hash_table_foreach (filtered, (GHFunc) artist_tabulator, category_items);
			category_cc = DMAP_CC_ABAR;
		} else if (g_ascii_strcasecmp (browse_category, "albums") == 0) {
			g_hash_table_foreach (filtered, (GHFunc) album_tabulator, category_items);
			category_cc = DMAP_CC_ABAL;
		} else {
			g_warning ("Unsupported browse category: %s",
				   browse_category);
			goto _bad_category;
		}

		abro = dmap_structure_add (NULL, DMAP_CC_ABRO);
		dmap_structure_add (abro, DMAP_CC_MSTT, (gint32) DAAP_STATUS_OK);
		dmap_structure_add (abro, DMAP_CC_MUTY, 0);

		num_genre = g_hash_table_size (category_items);
		dmap_structure_add (abro, DMAP_CC_MTCO, (gint32) num_genre);
		dmap_structure_add (abro, DMAP_CC_MRCO, (gint32) num_genre);

		node = dmap_structure_add (abro, category_cc);

		g_hash_table_foreach (category_items,
				      add_to_category_listing,
				      node);

1130
		_dmap_share_message_set_from_dmap_structure (share, message, abro);
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
                dmap_structure_destroy (abro);
	_bad_category:
		free_filter (filter_def);
		/* Free's hash table but not data (points into real DB): */
		g_hash_table_destroy (filtered);
		g_hash_table_destroy (category_items);

	} else if (g_ascii_strncasecmp ("/1/items/", rest_of_path, 9) == 0) {
	/* just the file :) */
		const gchar *id_str;
		gint id;
		const gchar *location;
		const gchar *range_header;
		guint64 filesize;
		guint64 offset = 0;
		DAAPRecord *record;

		id_str = rest_of_path + 9;
		id = atoi (id_str);

		record = DAAP_RECORD (dmap_db_lookup_by_id (DAAP_SHARE (share)->priv->db, id));
		g_object_get (record, "location", &location, NULL);
		g_object_get (record, "filesize", &filesize, NULL);

		daap_share_message_add_standard_headers (message);
		soup_message_headers_append (message->response_headers, "Accept-Ranges", "bytes");

		range_header = soup_message_headers_get (message->request_headers, "Range");
		if (range_header) {
			const gchar *s;
			gchar *content_range;

			s = range_header + 6; /* bytes= */
			offset = atoll (s);

			content_range = g_strdup_printf ("bytes %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT, offset, filesize, filesize);
			soup_message_headers_append (message->response_headers, "Content-Range", content_range);
			g_debug ("Content range is %s.", content_range);
			g_free (content_range);
			soup_message_set_status (message, SOUP_STATUS_PARTIAL_CONTENT);
		} else {
			soup_message_set_status (message, SOUP_STATUS_OK);
		}
		send_chunked_file (server, message, record, filesize, offset, DAAP_SHARE (share)->priv->transcode_mimetype);
1175
1176
		
		g_object_unref (record);
1177
1178
1179
1180
	} else {
		g_warning ("Unhandled: %s\n", path);
	}
}