3 * http://sourceforge.net/projects/minidlna/
5 * MiniDLNA media server
6 * Copyright (C) 2008-2009 Justin Maggard
8 * This file is part of MiniDLNA.
10 * MiniDLNA is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * MiniDLNA is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
22 * Portions of the code from the MiniUPnP project:
24 * Copyright (c) 2006-2007, Thomas Bernard
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions are met:
29 * * Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * * Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * * The name of the author may not be used to endorse or promote products
35 * derived from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
41 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 * POSSIBILITY OF SUCH DAMAGE.
52 #include <sys/socket.h>
56 #include <sys/types.h>
57 #include <arpa/inet.h>
58 #include <netinet/in.h>
63 #include "upnpglobalvars.h"
67 #include "upnpreplyparse.h"
68 #include "getifaddr.h"
74 BuildSendAndCloseSoapResp(struct upnphttp
* h
,
75 const char * body
, int bodylen
)
77 static const char beforebody
[] =
78 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
79 "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
80 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
83 static const char afterbody
[] =
87 BuildHeader_upnphttp(h
, 200, "OK", sizeof(beforebody
) - 1
88 + sizeof(afterbody
) - 1 + bodylen
);
90 memcpy(h
->res_buf
+ h
->res_buflen
, beforebody
, sizeof(beforebody
) - 1);
91 h
->res_buflen
+= sizeof(beforebody
) - 1;
93 memcpy(h
->res_buf
+ h
->res_buflen
, body
, bodylen
);
94 h
->res_buflen
+= bodylen
;
96 memcpy(h
->res_buf
+ h
->res_buflen
, afterbody
, sizeof(afterbody
) - 1);
97 h
->res_buflen
+= sizeof(afterbody
) - 1;
100 CloseSocket_upnphttp(h
);
104 GetSystemUpdateID(struct upnphttp
* h
, const char * action
)
106 static const char resp
[] =
115 bodylen
= snprintf(body
, sizeof(body
), resp
,
116 action
, "urn:schemas-upnp-org:service:ContentDirectory:1",
118 BuildSendAndCloseSoapResp(h
, body
, bodylen
);
122 IsAuthorizedValidated(struct upnphttp
* h
, const char * action
)
124 static const char resp
[] =
127 "<Result>%d</Result>"
133 bodylen
= snprintf(body
, sizeof(body
), resp
,
134 action
, "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
136 BuildSendAndCloseSoapResp(h
, body
, bodylen
);
140 GetProtocolInfo(struct upnphttp
* h
, const char * action
)
142 static const char resp
[] =
146 RESOURCE_PROTOCOL_INFO_VALUES
154 bodylen
= asprintf(&body
, resp
,
155 action
, "urn:schemas-upnp-org:service:ConnectionManager:1",
157 BuildSendAndCloseSoapResp(h
, body
, bodylen
);
162 GetSortCapabilities(struct upnphttp
* h
, const char * action
)
164 static const char resp
[] =
171 "upnp:originalTrackNumber"
178 bodylen
= snprintf(body
, sizeof(body
), resp
,
179 action
, "urn:schemas-upnp-org:service:ContentDirectory:1",
181 BuildSendAndCloseSoapResp(h
, body
, bodylen
);
185 GetSearchCapabilities(struct upnphttp
* h
, const char * action
)
187 static const char resp
[] =
188 "<u:%sResponse xmlns:u=\"%s\">"
204 bodylen
= snprintf(body
, sizeof(body
), resp
,
205 action
, "urn:schemas-upnp-org:service:ContentDirectory:1",
207 BuildSendAndCloseSoapResp(h
, body
, bodylen
);
211 GetCurrentConnectionIDs(struct upnphttp
* h
, const char * action
)
213 /* TODO: Use real data. - JM */
214 static const char resp
[] =
217 "<ConnectionIDs>0</ConnectionIDs>"
223 bodylen
= snprintf(body
, sizeof(body
), resp
,
224 action
, "urn:schemas-upnp-org:service:ConnectionManager:1",
226 BuildSendAndCloseSoapResp(h
, body
, bodylen
);
230 GetCurrentConnectionInfo(struct upnphttp
* h
, const char * action
)
232 /* TODO: Use real data. - JM */
233 static const char resp
[] =
237 "<AVTransportID>-1</AVTransportID>"
238 "<ProtocolInfo></ProtocolInfo>"
239 "<PeerConnectionManager></PeerConnectionManager>"
240 "<PeerConnectionID>-1</PeerConnectionID>"
241 "<Direction>Output</Direction>"
242 "<Status>Unknown</Status>"
245 char body
[sizeof(resp
)+128];
248 bodylen
= snprintf(body
, sizeof(body
), resp
,
249 action
, "urn:schemas-upnp-org:service:ConnectionManager:1",
251 BuildSendAndCloseSoapResp(h
, body
, bodylen
);
255 mime_to_ext(const char * mime
, char * buf
)
259 /* Audio extensions */
261 if( strcmp(mime
+6, "mpeg") == 0 )
263 else if( strcmp(mime
+6, "mp4") == 0 )
265 else if( strcmp(mime
+6, "x-ms-wma") == 0 )
267 else if( strcmp(mime
+6, "x-flac") == 0 )
269 else if( strcmp(mime
+6, "flac") == 0 )
271 else if( strcmp(mime
+6, "x-wav") == 0 )
273 else if( strncmp(mime
+6, "L16", 3) == 0 )
275 else if( strcmp(mime
+6, "3gpp") == 0 )
277 else if( strcmp(mime
, "application/ogg") == 0 )
283 if( strcmp(mime
+6, "avi") == 0 )
285 else if( strcmp(mime
+6, "divx") == 0 )
287 else if( strcmp(mime
+6, "x-msvideo") == 0 )
289 else if( strcmp(mime
+6, "mpeg") == 0 )
291 else if( strcmp(mime
+6, "mp4") == 0 )
293 else if( strcmp(mime
+6, "x-ms-wmv") == 0 )
295 else if( strcmp(mime
+6, "x-matroska") == 0 )
297 else if( strcmp(mime
+6, "x-mkv") == 0 )
299 else if( strcmp(mime
+6, "x-flv") == 0 )
301 else if( strcmp(mime
+6, "vnd.dlna.mpeg-tts") == 0 )
303 else if( strcmp(mime
+6, "quicktime") == 0 )
305 else if( strcmp(mime
+6, "3gpp") == 0 )
307 else if( strcmp(mime
+6, "x-tivo-mpeg") == 0 )
313 if( strcmp(mime
+6, "jpeg") == 0 )
315 else if( strcmp(mime
+6, "png") == 0 )
326 #define FILTER_CHILDCOUNT 0x00000001
327 #define FILTER_DC_CREATOR 0x00000002
328 #define FILTER_DC_DATE 0x00000004
329 #define FILTER_DC_DESCRIPTION 0x00000008
330 #define FILTER_DLNA_NAMESPACE 0x00000010
331 #define FILTER_REFID 0x00000020
332 #define FILTER_RES 0x00000040
333 #define FILTER_RES_BITRATE 0x00000080
334 #define FILTER_RES_DURATION 0x00000100
335 #define FILTER_RES_NRAUDIOCHANNELS 0x00000200
336 #define FILTER_RES_RESOLUTION 0x00000400
337 #define FILTER_RES_SAMPLEFREQUENCY 0x00000800
338 #define FILTER_RES_SIZE 0x00001000
339 #define FILTER_UPNP_ACTOR 0x00002000
340 #define FILTER_UPNP_ALBUM 0x00004000
341 #define FILTER_UPNP_ALBUMARTURI 0x00008000
342 #define FILTER_UPNP_ALBUMARTURI_DLNA_PROFILEID 0x00010000
343 #define FILTER_UPNP_ARTIST 0x00020000
344 #define FILTER_UPNP_GENRE 0x00040000
345 #define FILTER_UPNP_ORIGINALTRACKNUMBER 0x00080000
346 #define FILTER_UPNP_SEARCHCLASS 0x00100000
349 set_filter_flags(char * filter
, enum client_types client
)
351 char *item
, *saveptr
= NULL
;
354 if( !filter
|| (strlen(filter
) <= 1) )
356 if( client
== ESamsungTV
)
357 flags
|= FILTER_DLNA_NAMESPACE
;
358 item
= strtok_r(filter
, ",", &saveptr
);
359 while( item
!= NULL
)
363 while( isspace(*item
) )
365 if( strcmp(item
, "@childCount") == 0 )
367 flags
|= FILTER_CHILDCOUNT
;
369 else if( strcmp(item
, "dc:creator") == 0 )
371 flags
|= FILTER_DC_CREATOR
;
373 else if( strcmp(item
, "dc:date") == 0 )
375 flags
|= FILTER_DC_DATE
;
377 else if( strcmp(item
, "dc:description") == 0 )
379 flags
|= FILTER_DC_DESCRIPTION
;
381 else if( strcmp(item
, "dlna") == 0 )
383 flags
|= FILTER_DLNA_NAMESPACE
;
385 else if( strcmp(item
, "@refID") == 0 )
387 flags
|= FILTER_REFID
;
389 else if( strcmp(item
, "upnp:album") == 0 )
391 flags
|= FILTER_UPNP_ALBUM
;
393 else if( strcmp(item
, "upnp:albumArtURI") == 0 )
395 flags
|= FILTER_UPNP_ALBUMARTURI
;
396 if( client
== ESamsungTV
)
397 flags
|= FILTER_UPNP_ALBUMARTURI_DLNA_PROFILEID
;
399 else if( strcmp(item
, "upnp:albumArtURI@dlna:profileID") == 0 )
401 flags
|= FILTER_UPNP_ALBUMARTURI
;
402 flags
|= FILTER_UPNP_ALBUMARTURI_DLNA_PROFILEID
;
404 else if( strcmp(item
, "upnp:artist") == 0 )
406 flags
|= FILTER_UPNP_ARTIST
;
408 else if( strcmp(item
, "upnp:actor") == 0 )
410 flags
|= FILTER_UPNP_ACTOR
;
412 else if( strcmp(item
, "upnp:genre") == 0 )
414 flags
|= FILTER_UPNP_GENRE
;
416 else if( strcmp(item
, "upnp:originalTrackNumber") == 0 )
418 flags
|= FILTER_UPNP_ORIGINALTRACKNUMBER
;
420 else if( strcmp(item
, "upnp:searchClass") == 0 )
422 flags
|= FILTER_UPNP_SEARCHCLASS
;
424 else if( strcmp(item
, "res") == 0 )
428 else if( (strcmp(item
, "res@bitrate") == 0) ||
429 (strcmp(item
, "@bitrate") == 0) ||
430 ((strcmp(item
, "bitrate") == 0) && (flags
& FILTER_RES
)) )
433 flags
|= FILTER_RES_BITRATE
;
435 else if( (strcmp(item
, "res@duration") == 0) ||
436 (strcmp(item
, "@duration") == 0) ||
437 ((strcmp(item
, "duration") == 0) && (flags
& FILTER_RES
)) )
440 flags
|= FILTER_RES_DURATION
;
442 else if( (strcmp(item
, "res@nrAudioChannels") == 0) ||
443 (strcmp(item
, "@nrAudioChannels") == 0) ||
444 ((strcmp(item
, "nrAudioChannels") == 0) && (flags
& FILTER_RES
)) )
447 flags
|= FILTER_RES_NRAUDIOCHANNELS
;
449 else if( (strcmp(item
, "res@resolution") == 0) ||
450 (strcmp(item
, "@resolution") == 0) ||
451 ((strcmp(item
, "resolution") == 0) && (flags
& FILTER_RES
)) )
454 flags
|= FILTER_RES_RESOLUTION
;
456 else if( (strcmp(item
, "res@sampleFrequency") == 0) ||
457 (strcmp(item
, "@sampleFrequency") == 0) ||
458 ((strcmp(item
, "sampleFrequency") == 0) && (flags
& FILTER_RES
)) )
461 flags
|= FILTER_RES_SAMPLEFREQUENCY
;
463 else if( (strcmp(item
, "res@size") == 0) ||
464 (strcmp(item
, "@size") == 0) ||
465 (strcmp(item
, "size") == 0) )
468 flags
|= FILTER_RES_SIZE
;
470 item
= strtok_r(NULL
, ",", &saveptr
);
477 parse_sort_criteria(char * sortCriteria
, int * error
)
480 char *item
, *saveptr
;
481 int i
, ret
, reverse
, title_sorted
= 0;
487 if( (item
= strtok_r(sortCriteria
, ",", &saveptr
)) )
489 order
= malloc(4096);
490 strcpy(order
, "order by ");
492 for( i
=0; item
!= NULL
; i
++ )
501 else if( *item
== '-' )
506 if( strcasecmp(item
, "upnp:class") == 0 )
508 strcat(order
, "o.CLASS");
510 else if( strcasecmp(item
, "dc:title") == 0 )
512 strcat(order
, "d.TITLE");
515 else if( strcasecmp(item
, "dc:date") == 0 )
517 strcat(order
, "d.DATE");
519 else if( strcasecmp(item
, "upnp:originalTrackNumber") == 0 )
521 strcat(order
, "d.DISC, d.TRACK");
525 printf("Unhandled SortCriteria [%s]\n", item
);
533 goto unhandled_order
;
537 strcat(order
, " DESC");
539 item
= strtok_r(NULL
, ",", &saveptr
);
546 /* Add a "tiebreaker" sort order */
548 strcat(order
, ", TITLE ASC");
554 add_resized_res(int srcw
, int srch
, int reqw
, int reqh
, char *dlna_pn
,
555 char *detailID
, struct Response
*passed_args
)
560 if( passed_args
->flags
& FLAG_NO_RESIZE
)
563 strcatf(passed_args
->str
, "<res ");
564 if( passed_args
->filter
& FILTER_RES_RESOLUTION
)
567 dsth
= ((((reqw
<<10)/srcw
)*srch
)>>10);
570 dstw
= (((reqh
<<10)/srch
) * srcw
>>10);
572 strcatf(passed_args
->str
, "resolution=\"%dx%d\" ", dstw
, dsth
);
574 strcatf(passed_args
->str
, "protocolInfo=\"http-get:*:image/jpeg:DLNA.ORG_PN=%s;DLNA.ORG_CI=1\">"
575 "http://%s:%d/Resized/%s.jpg?width=%d,height=%d"
577 dlna_pn
, lan_addr
[0].str
, runtime_vars
.port
, detailID
, dstw
, dsth
);
581 add_res(char *size
, char *duration
, char *bitrate
, char *sampleFrequency
,
582 char *nrAudioChannels
, char *resolution
, char *dlna_pn
, char *mime
,
583 char *detailID
, char *ext
, struct Response
*passed_args
)
585 strcatf(passed_args
->str
, "<res ");
586 if( size
&& (passed_args
->filter
& FILTER_RES_SIZE
) ) {
587 strcatf(passed_args
->str
, "size=\"%s\" ", size
);
589 if( duration
&& (passed_args
->filter
& FILTER_RES_DURATION
) ) {
590 strcatf(passed_args
->str
, "duration=\"%s\" ", duration
);
592 if( bitrate
&& (passed_args
->filter
& FILTER_RES_BITRATE
) ) {
593 strcatf(passed_args
->str
, "bitrate=\"%s\" ", bitrate
);
595 if( sampleFrequency
&& (passed_args
->filter
& FILTER_RES_SAMPLEFREQUENCY
) ) {
596 strcatf(passed_args
->str
, "sampleFrequency=\"%s\" ", sampleFrequency
);
598 if( nrAudioChannels
&& (passed_args
->filter
& FILTER_RES_NRAUDIOCHANNELS
) ) {
599 strcatf(passed_args
->str
, "nrAudioChannels=\"%s\" ", nrAudioChannels
);
601 if( resolution
&& (passed_args
->filter
& FILTER_RES_RESOLUTION
) ) {
602 strcatf(passed_args
->str
, "resolution=\"%s\" ", resolution
);
604 strcatf(passed_args
->str
, "protocolInfo=\"http-get:*:%s:%s\">"
605 "http://%s:%d/MediaItems/%s.%s"
607 mime
, dlna_pn
, lan_addr
[0].str
,
608 runtime_vars
.port
, detailID
, ext
);
611 #define SELECT_COLUMNS "SELECT o.OBJECT_ID, o.PARENT_ID, o.REF_ID, o.DETAIL_ID, o.CLASS," \
612 " d.SIZE, d.TITLE, d.DURATION, d.BITRATE, d.SAMPLERATE, d.ARTIST," \
613 " d.ALBUM, d.GENRE, d.COMMENT, d.CHANNELS, d.TRACK, d.DATE, d.RESOLUTION," \
614 " d.THUMBNAIL, d.CREATOR, d.DLNA_PN, d.MIME, d.ALBUM_ART, d.DISC "
617 callback(void *args
, int argc
, char **argv
, char **azColName
)
619 struct Response
*passed_args
= (struct Response
*)args
;
620 char *id
= argv
[0], *parent
= argv
[1], *refID
= argv
[2], *detailID
= argv
[3], *class = argv
[4], *size
= argv
[5], *title
= argv
[6],
621 *duration
= argv
[7], *bitrate
= argv
[8], *sampleFrequency
= argv
[9], *artist
= argv
[10], *album
= argv
[11],
622 *genre
= argv
[12], *comment
= argv
[13], *nrAudioChannels
= argv
[14], *track
= argv
[15], *date
= argv
[16], *resolution
= argv
[17],
623 *tn
= argv
[18], *creator
= argv
[19], *dlna_pn
= argv
[20], *mime
= argv
[21], *album_art
= argv
[22];
626 struct string_s
*str
= passed_args
->str
;
629 /* Make sure we have at least 8KB left of allocated memory to finish the response. */
630 if( str
->off
> (str
->size
- 8192) )
632 #if MAX_RESPONSE_SIZE > 0
633 if( (str
->size
+DEFAULT_RESP_SIZE
) <= MAX_RESPONSE_SIZE
)
636 str
->data
= realloc(str
->data
, (str
->off
+DEFAULT_RESP_SIZE
));
639 str
->size
+= DEFAULT_RESP_SIZE
;
640 DPRINTF(E_DEBUG
, L_HTTP
, "UPnP SOAP response enlarged to %d. [%d results so far]\n",
641 str
->size
, passed_args
->returned
);
645 DPRINTF(E_ERROR
, L_HTTP
, "UPnP SOAP response was too big, and realloc failed!\n");
648 #if MAX_RESPONSE_SIZE > 0
652 DPRINTF(E_ERROR
, L_HTTP
, "UPnP SOAP response cut short, to not exceed the max response size [%lld]!\n", (long long int)MAX_RESPONSE_SIZE
);
657 passed_args
->returned
++;
660 sprintf(dlna_buf
, "DLNA.ORG_PN=%s", dlna_pn
);
661 else if( passed_args
->flags
& FLAG_DLNA
)
662 strcpy(dlna_buf
, dlna_no_conv
);
664 strcpy(dlna_buf
, "*");
666 if( strncmp(class, "item", 4) == 0 )
668 /* We may need special handling for certain MIME types */
671 if( passed_args
->flags
& FLAG_MIME_AVI_DIVX
)
673 if( strcmp(mime
, "video/x-msvideo") == 0 )
676 strcpy(mime
+6, "divx");
678 strcpy(mime
+6, "avi");
681 else if( passed_args
->flags
& FLAG_MIME_AVI_AVI
)
683 if( strcmp(mime
, "video/x-msvideo") == 0 )
685 strcpy(mime
+6, "avi");
688 if( !(passed_args
->flags
& FLAG_DLNA
) )
690 if( strcmp(mime
+6, "vnd.dlna.mpeg-tts") == 0 )
692 strcpy(mime
+6, "mpeg");
695 /* From what I read, Samsung TV's expect a [wrong] MIME type of x-mkv. */
696 if( passed_args
->client
== ESamsungTV
)
698 if( strcmp(mime
+6, "x-matroska") == 0 )
700 strcpy(mime
+8, "mkv");
704 else if( *mime
== 'a' )
706 if( strcmp(mime
+6, "x-flac") == 0 )
708 if( passed_args
->flags
& FLAG_MIME_FLAC_FLAC
)
710 strcpy(mime
+6, "flac");
715 ret
= strcatf(str
, "<item id=\"%s\" parentID=\"%s\" restricted=\"1\"", id
, parent
);
716 if( refID
&& (passed_args
->filter
& FILTER_REFID
) ) {
717 ret
= strcatf(str
, " refID=\"%s\"", refID
);
719 ret
= strcatf(str
, ">"
720 "<dc:title>%s</dc:title>"
721 "<upnp:class>object.%s</upnp:class>",
723 if( comment
&& (passed_args
->filter
& FILTER_DC_DESCRIPTION
) ) {
724 ret
= strcatf(str
, "<dc:description>%.384s</dc:description>", comment
);
726 if( creator
&& (passed_args
->filter
& FILTER_DC_CREATOR
) ) {
727 ret
= strcatf(str
, "<dc:creator>%s</dc:creator>", creator
);
729 if( date
&& (passed_args
->filter
& FILTER_DC_DATE
) ) {
730 ret
= strcatf(str
, "<dc:date>%s</dc:date>", date
);
733 if( (*mime
== 'v') && (passed_args
->filter
& FILTER_UPNP_ACTOR
) ) {
734 ret
= strcatf(str
, "<upnp:actor>%s</upnp:actor>", artist
);
736 if( passed_args
->filter
& FILTER_UPNP_ARTIST
) {
737 ret
= strcatf(str
, "<upnp:artist>%s</upnp:artist>", artist
);
740 if( album
&& (passed_args
->filter
& FILTER_UPNP_ALBUM
) ) {
741 ret
= strcatf(str
, "<upnp:album>%s</upnp:album>", album
);
743 if( genre
&& (passed_args
->filter
& FILTER_UPNP_GENRE
) ) {
744 ret
= strcatf(str
, "<upnp:genre>%s</upnp:genre>", genre
);
746 if( strncmp(id
, MUSIC_PLIST_ID
, strlen(MUSIC_PLIST_ID
)) == 0 ) {
747 track
= strrchr(id
, '$')+1;
749 if( track
&& atoi(track
) && (passed_args
->filter
& FILTER_UPNP_ORIGINALTRACKNUMBER
) ) {
750 ret
= strcatf(str
, "<upnp:originalTrackNumber>%s</upnp:originalTrackNumber>", track
);
752 if( album_art
&& atoi(album_art
) )
754 /* Video and audio album art is handled differently */
755 if( *mime
== 'v' && (passed_args
->filter
& FILTER_RES
) && !(passed_args
->flags
& FLAG_MS_PFS
) ) {
756 ret
= strcatf(str
, "<res protocolInfo=\"http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_TN\">"
757 "http://%s:%d/AlbumArt/%s-%s.jpg"
759 lan_addr
[0].str
, runtime_vars
.port
, album_art
, detailID
);
760 } else if( passed_args
->filter
& FILTER_UPNP_ALBUMARTURI
) {
761 ret
= strcatf(str
, "<upnp:albumArtURI");
762 if( passed_args
->filter
& FILTER_UPNP_ALBUMARTURI_DLNA_PROFILEID
) {
763 ret
= strcatf(str
, " dlna:profileID=\"JPEG_TN\" xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\"");
765 ret
= strcatf(str
, ">http://%s:%d/AlbumArt/%s-%s.jpg</upnp:albumArtURI>",
766 lan_addr
[0].str
, runtime_vars
.port
, album_art
, detailID
);
770 if( (passed_args
->flags
& FLAG_MS_PFS
) && *mime
== 'i' ) {
771 ret
= strcatf(str
, "<upnp:album>%s</upnp:album>", "[No Keywords]");
773 if( tn
&& atoi(tn
) ) {
774 ret
= strcatf(str
, "<upnp:albumArtURI>"
775 "http://%s:%d/Thumbnails/%s.jpg"
776 "</upnp:albumArtURI>",
777 lan_addr
[0].str
, runtime_vars
.port
, detailID
);
779 ret
= strcatf(str
, "<upnp:albumArtURI>"
780 "http://%s:%d/Resized/%s.jpg?width=160,height=160"
781 "</upnp:albumArtURI>",
782 lan_addr
[0].str
, runtime_vars
.port
, detailID
);
786 if( passed_args
->filter
& FILTER_RES
) {
787 mime_to_ext(mime
, ext
);
788 if( (passed_args
->client
== EFreeBox
) && tn
&& atoi(tn
) ) {
789 ret
= strcatf(str
, "<res protocolInfo=\"http-get:*:%s:%s\">"
790 "http://%s:%d/Thumbnails/%s.jpg"
792 mime
, "DLNA.ORG_PN=JPEG_TN", lan_addr
[0].str
,
793 runtime_vars
.port
, detailID
);
795 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
796 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
797 if( (*mime
== 'i') && (passed_args
->client
!= EFreeBox
) ) {
799 int srcw
= atoi(strsep(&resolution
, "x"));
800 int srch
= atoi(resolution
);
802 add_resized_res(srcw
, srch
, 4096, 4096, "JPEG_LRG", detailID
, passed_args
);
804 if( !dlna_pn
|| !strncmp(dlna_pn
, "JPEG_L", 6) || !strncmp(dlna_pn
, "JPEG_M", 6) ) {
805 add_resized_res(srcw
, srch
, 640, 480, "JPEG_SM", detailID
, passed_args
);
808 if( tn
&& atoi(tn
) ) {
809 ret
= strcatf(str
, "<res protocolInfo=\"http-get:*:%s:%s\">"
810 "http://%s:%d/Thumbnails/%s.jpg"
812 mime
, "DLNA.ORG_PN=JPEG_TN", lan_addr
[0].str
,
813 runtime_vars
.port
, detailID
);
816 else if( *mime
== 'v' ) {
817 switch( passed_args
->client
) {
820 (strncmp(dlna_pn
, "MPEG_TS_HD_NA", 13) == 0 ||
821 strncmp(dlna_pn
, "MPEG_TS_SD_NA", 13) == 0 ||
822 strncmp(dlna_pn
, "AVC_TS_MP_HD_AC3", 16) == 0 ||
823 strncmp(dlna_pn
, "AVC_TS_HP_HD_AC3", 16) == 0))
825 sprintf(dlna_buf
, "DLNA.ORG_PN=MPEG_PS_NTSC;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
826 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
827 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
832 (strncmp(dlna_pn
, "AVC_TS", 6) == 0 ||
833 strncmp(dlna_pn
, "MPEG_TS", 7) == 0) )
835 if( strncmp(dlna_pn
, "MPEG_TS_SD_NA", 13) != 0 )
837 sprintf(dlna_buf
, "DLNA.ORG_PN=MPEG_TS_SD_NA;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
838 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
839 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
841 if( strncmp(dlna_pn
, "MPEG_TS_SD_EU", 13) != 0 )
843 sprintf(dlna_buf
, "DLNA.ORG_PN=MPEG_TS_SD_EU;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
844 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
845 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
849 (strncmp(dlna_pn
, "AVC_MP4", 7) == 0 ||
850 strncmp(dlna_pn
, "MPEG4_P2_MP4", 12) == 0)) ||
851 strcmp(mime
+6, "x-matroska") == 0 ||
852 strcmp(mime
+6, "x-msvideo") == 0 ||
853 strcmp(mime
+6, "mpeg") == 0 )
855 strcpy(mime
+6, "avi");
856 if( !dlna_pn
|| strncmp(dlna_pn
, "MPEG_PS_NTSC", 12) != 0 )
858 sprintf(dlna_buf
, "DLNA.ORG_PN=MPEG_PS_NTSC;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
859 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
860 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
862 if( !dlna_pn
|| strncmp(dlna_pn
, "MPEG_PS_PAL", 11) != 0 )
864 sprintf(dlna_buf
, "DLNA.ORG_PN=MPEG_PS_PAL;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
865 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
866 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
871 /* BRAVIA KDL-##*X### series TVs do natively support AVC/AC3 in TS, but
872 require profile to be renamed (applies to _T and _ISO variants also) */
874 (strncmp(dlna_pn
, "AVC_TS_MP_SD_AC3", 16) == 0 ||
875 strncmp(dlna_pn
, "AVC_TS_MP_HD_AC3", 16) == 0 ||
876 strncmp(dlna_pn
, "AVC_TS_HP_HD_AC3", 16) == 0))
878 sprintf(dlna_buf
, "DLNA.ORG_PN=AVC_TS_HD_50_AC3;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
879 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
880 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
884 if( sql_get_int_field(db
, "SELECT ID from CAPTIONS where ID = '%s'", detailID
) > 0 )
886 ret
= strcatf(str
, "<res protocolInfo=\"http-get:*:text/srt:*\">"
887 "http://%s:%d/Captions/%s.srt"
889 lan_addr
[0].str
, runtime_vars
.port
, detailID
);
897 ret
= strcatf(str
, "</item>");
899 else if( strncmp(class, "container", 9) == 0 )
901 ret
= strcatf(str
, "<container id=\"%s\" parentID=\"%s\" restricted=\"1\" ", id
, parent
);
902 if( passed_args
->filter
& FILTER_CHILDCOUNT
)
905 ret
= sql_get_int_field(db
, "SELECT count(*) from OBJECTS where PARENT_ID = '%s';", id
);
906 children
= (ret
> 0) ? ret
: 0;
907 ret
= strcatf(str
, "childCount=\"%d\"", children
);
909 /* If the client calls for BrowseMetadata on root, we have to include our "upnp:searchClass"'s, unless they're filtered out */
910 if( (passed_args
->requested
== 1) && (strcmp(id
, "0") == 0) )
912 if( passed_args
->filter
& FILTER_UPNP_SEARCHCLASS
)
914 ret
= strcatf(str
, ">"
915 "<upnp:searchClass includeDerived=\"1\">object.item.audioItem</upnp:searchClass>"
916 "<upnp:searchClass includeDerived=\"1\">object.item.imageItem</upnp:searchClass>"
917 "<upnp:searchClass includeDerived=\"1\">object.item.videoItem</upnp:searchClass");
920 ret
= strcatf(str
, ">"
921 "<dc:title>%s</dc:title>"
922 "<upnp:class>object.%s</upnp:class>",
924 if( creator
&& (passed_args
->filter
& FILTER_DC_CREATOR
) ) {
925 ret
= strcatf(str
, "<dc:creator>%s</dc:creator>", creator
);
927 if( genre
&& (passed_args
->filter
& FILTER_UPNP_GENRE
) ) {
928 ret
= strcatf(str
, "<upnp:genre>%s</upnp:genre>", genre
);
930 if( artist
&& (passed_args
->filter
& FILTER_UPNP_ARTIST
) ) {
931 ret
= strcatf(str
, "<upnp:artist>%s</upnp:artist>", artist
);
933 if( album_art
&& atoi(album_art
) && (passed_args
->filter
& FILTER_UPNP_ALBUMARTURI
) ) {
934 ret
= strcatf(str
, "<upnp:albumArtURI ");
935 if( passed_args
->filter
& FILTER_UPNP_ALBUMARTURI_DLNA_PROFILEID
) {
936 ret
= strcatf(str
, "dlna:profileID=\"JPEG_TN\" xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\"");
938 ret
= strcatf(str
, ">http://%s:%d/AlbumArt/%s-%s.jpg</upnp:albumArtURI>",
939 lan_addr
[0].str
, runtime_vars
.port
, album_art
, detailID
);
941 ret
= strcatf(str
, "</container>");
948 BrowseContentDirectory(struct upnphttp
* h
, const char * action
)
950 static const char resp0
[] =
952 "xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\">"
955 CONTENT_DIRECTORY_SCHEMAS
;
959 struct Response args
;
962 struct NameValueParserData data
;
964 ParseNameValue(h
->req_buf
+ h
->req_contentoff
, h
->req_contentlen
, &data
);
965 char * ObjectId
= GetValueFromNameValueList(&data
, "ObjectID");
966 char * Filter
= GetValueFromNameValueList(&data
, "Filter");
967 char * BrowseFlag
= GetValueFromNameValueList(&data
, "BrowseFlag");
968 char * SortCriteria
= GetValueFromNameValueList(&data
, "SortCriteria");
969 char * orderBy
= NULL
;
970 int RequestedCount
= 0;
971 int StartingIndex
= 0;
972 if( (ptr
= GetValueFromNameValueList(&data
, "RequestedCount")) )
973 RequestedCount
= atoi(ptr
);
974 if( !RequestedCount
)
976 if( (ptr
= GetValueFromNameValueList(&data
, "StartingIndex")) )
977 StartingIndex
= atoi(ptr
);
978 if( !BrowseFlag
|| (strcmp(BrowseFlag
, "BrowseDirectChildren") && strcmp(BrowseFlag
, "BrowseMetadata")) )
980 SoapError(h
, 402, "Invalid Args");
981 if( h
->reqflags
& FLAG_MS_PFS
)
982 ObjectId
= sqlite3_malloc(1);
985 if( !ObjectId
&& !(ObjectId
= GetValueFromNameValueList(&data
, "ContainerID")) )
987 SoapError(h
, 701, "No such object error");
988 if( h
->reqflags
& FLAG_MS_PFS
)
989 ObjectId
= sqlite3_malloc(1);
992 memset(&args
, 0, sizeof(args
));
993 memset(&str
, 0, sizeof(str
));
995 str
.data
= malloc(DEFAULT_RESP_SIZE
);
996 str
.size
= DEFAULT_RESP_SIZE
;
997 str
.off
= sprintf(str
.data
, "%s", resp0
);
999 /* See if we need to include DLNA namespace reference */
1000 args
.filter
= set_filter_flags(Filter
, h
->req_client
);
1001 if( args
.filter
& FILTER_DLNA_NAMESPACE
)
1003 ret
= strcatf(&str
, DLNA_NAMESPACE
);
1005 strcatf(&str
, ">\n");
1008 args
.requested
= RequestedCount
;
1009 args
.client
= h
->req_client
;
1010 args
.flags
= h
->reqflags
;
1011 if( args
.flags
& FLAG_MS_PFS
)
1013 if( !strchr(ObjectId
, '$') && (strcmp(ObjectId
, "0") != 0) )
1015 ptr
= sql_get_text_field(db
, "SELECT OBJECT_ID from OBJECTS"
1016 " where OBJECT_ID in "
1017 "('"MUSIC_ID
"$%s', '"VIDEO_ID
"$%s', '"IMAGE_ID
"$%s')",
1018 ObjectId
, ObjectId
, ObjectId
);
1022 args
.flags
|= FLAG_FREE_OBJECT_ID
;
1026 DPRINTF(E_DEBUG
, L_HTTP
, "Browsing ContentDirectory:\n"
1029 " * StartingIndex: %d\n"
1030 " * BrowseFlag: %s\n"
1032 " * SortCriteria: %s\n",
1033 ObjectId
, RequestedCount
, StartingIndex
,
1034 BrowseFlag
, Filter
, SortCriteria
);
1036 if( (args
.flags
& FLAG_AUDIO_ONLY
) && (strcmp(ObjectId
, "0") == 0) )
1038 ObjectId
= sqlite3_mprintf("%s", MUSIC_ID
);
1039 args
.flags
|= FLAG_FREE_OBJECT_ID
;
1042 if( strcmp(BrowseFlag
+6, "Metadata") == 0 )
1045 sql
= sqlite3_mprintf( SELECT_COLUMNS
1046 "from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)"
1047 " where OBJECT_ID = '%s';"
1049 ret
= sqlite3_exec(db
, sql
, callback
, (void *) &args
, &zErrMsg
);
1050 totalMatches
= args
.returned
;
1054 ret
= sql_get_int_field(db
, "SELECT count(*) from OBJECTS where PARENT_ID = '%s'", ObjectId
);
1055 totalMatches
= (ret
> 0) ? ret
: 0;
1059 #ifdef __sparc__ /* Sorting takes too long on slow processors with very large containers */
1060 if( totalMatches
< 10000 )
1062 orderBy
= parse_sort_criteria(SortCriteria
, &ret
);
1066 if( strncmp(ObjectId
, MUSIC_PLIST_ID
, strlen(MUSIC_PLIST_ID
)) == 0 )
1068 if( strcmp(ObjectId
, MUSIC_PLIST_ID
) == 0 )
1069 asprintf(&orderBy
, "order by d.TITLE");
1071 asprintf(&orderBy
, "order by length(OBJECT_ID), OBJECT_ID");
1073 else if( args
.client
== ERokuSoundBridge
)
1076 if( totalMatches
< 10000 )
1078 asprintf(&orderBy
, "order by o.CLASS, d.DISC, d.TRACK, d.TITLE");
1081 /* If it's a DLNA client, return an error for bad sort criteria */
1082 if( (args
.flags
& FLAG_DLNA
) && ret
)
1084 SoapError(h
, 709, "Unsupported or invalid sort criteria");
1088 sql
= sqlite3_mprintf( SELECT_COLUMNS
1089 "from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)"
1090 " where PARENT_ID = '%s' %s limit %d, %d;",
1091 ObjectId
, orderBy
, StartingIndex
, RequestedCount
);
1092 DPRINTF(E_DEBUG
, L_HTTP
, "Browse SQL: %s\n", sql
);
1093 ret
= sqlite3_exec(db
, sql
, callback
, (void *) &args
, &zErrMsg
);
1096 if( (ret
!= SQLITE_OK
) && (zErrMsg
!= NULL
) )
1098 DPRINTF(E_WARN
, L_HTTP
, "SQL error: %s\nBAD SQL: %s\n", zErrMsg
, sql
);
1099 sqlite3_free(zErrMsg
);
1101 /* Does the object even exist? */
1104 ret
= sql_get_int_field(db
, "SELECT count(*) from OBJECTS where OBJECT_ID = '%s'", ObjectId
);
1107 SoapError(h
, 701, "No such object error");
1111 ret
= strcatf(&str
, "</DIDL-Lite></Result>\n"
1112 "<NumberReturned>%u</NumberReturned>\n"
1113 "<TotalMatches>%u</TotalMatches>\n"
1114 "<UpdateID>%u</UpdateID>"
1115 "</u:BrowseResponse>",
1116 args
.returned
, totalMatches
, updateID
);
1117 BuildSendAndCloseSoapResp(h
, str
.data
, str
.off
);
1119 ClearNameValueList(&data
);
1120 if( args
.flags
& FLAG_FREE_OBJECT_ID
)
1121 sqlite3_free(ObjectId
);
1127 SearchContentDirectory(struct upnphttp
* h
, const char * action
)
1129 static const char resp0
[] =
1130 "<u:SearchResponse "
1131 "xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\">"
1134 CONTENT_DIRECTORY_SCHEMAS
;
1138 struct Response args
;
1139 struct string_s str
;
1140 int totalMatches
= 0;
1143 struct NameValueParserData data
;
1144 ParseNameValue(h
->req_buf
+ h
->req_contentoff
, h
->req_contentlen
, &data
);
1145 char * ContainerID
= GetValueFromNameValueList(&data
, "ContainerID");
1146 char * Filter
= GetValueFromNameValueList(&data
, "Filter");
1147 char * SearchCriteria
= GetValueFromNameValueList(&data
, "SearchCriteria");
1148 char * SortCriteria
= GetValueFromNameValueList(&data
, "SortCriteria");
1149 char * newSearchCriteria
= NULL
;
1150 char * orderBy
= NULL
;
1151 char groupBy
[] = "group by DETAIL_ID";
1152 int RequestedCount
= 0;
1153 int StartingIndex
= 0;
1154 if( (ptr
= GetValueFromNameValueList(&data
, "RequestedCount")) )
1155 RequestedCount
= atoi(ptr
);
1156 if( !RequestedCount
)
1157 RequestedCount
= -1;
1158 if( (ptr
= GetValueFromNameValueList(&data
, "StartingIndex")) )
1159 StartingIndex
= atoi(ptr
);
1162 if( !(ContainerID
= GetValueFromNameValueList(&data
, "ObjectID")) )
1164 SoapError(h
, 701, "No such object error");
1165 if( h
->reqflags
& FLAG_MS_PFS
)
1166 ContainerID
= sqlite3_malloc(1);
1170 memset(&args
, 0, sizeof(args
));
1171 memset(&str
, 0, sizeof(str
));
1173 str
.data
= malloc(DEFAULT_RESP_SIZE
);
1174 str
.size
= DEFAULT_RESP_SIZE
;
1175 str
.off
= sprintf(str
.data
, "%s", resp0
);
1176 /* See if we need to include DLNA namespace reference */
1177 args
.filter
= set_filter_flags(Filter
, h
->req_client
);
1178 if( args
.filter
& FILTER_DLNA_NAMESPACE
)
1180 ret
= strcatf(&str
, DLNA_NAMESPACE
);
1182 ret
= strcatf(&str
, ">\n");
1185 args
.requested
= RequestedCount
;
1186 args
.client
= h
->req_client
;
1187 args
.flags
= h
->reqflags
;
1189 if( h
->reqflags
& FLAG_MS_PFS
)
1191 if( strchr(ContainerID
, '$') || (strcmp(ContainerID
, "0") == 0) )
1193 ContainerID
= sqlite3_mprintf("%s", ContainerID
);
1197 ptr
= sql_get_text_field(db
, "SELECT OBJECT_ID from OBJECTS"
1198 " where OBJECT_ID in "
1199 "('"MUSIC_ID
"$%s', '"VIDEO_ID
"$%s', '"IMAGE_ID
"$%s')",
1200 ContainerID
, ContainerID
, ContainerID
);
1204 ContainerID
= sqlite3_mprintf("%s", ContainerID
);
1206 #if 0 // Looks like the 360 already does this
1207 /* Sort by track number for some containers */
1209 ((strncmp(ContainerID
, MUSIC_GENRE_ID
, 3) == 0) ||
1210 (strncmp(ContainerID
, MUSIC_ARTIST_ID
, 3) == 0) ||
1211 (strncmp(ContainerID
, MUSIC_ALBUM_ID
, 3) == 0)) )
1213 DPRINTF(E_DEBUG
, L_HTTP
, "Old sort order: %s\n", orderBy
);
1214 sprintf(str_buf
, "d.TRACK, ");
1215 memmove(orderBy
+18, orderBy
+9, strlen(orderBy
)+1);
1216 memmove(orderBy
+9, &str_buf
, 9);
1217 DPRINTF(E_DEBUG
, L_HTTP
, "New sort order: %s\n", orderBy
);
1221 DPRINTF(E_DEBUG
, L_HTTP
, "Searching ContentDirectory:\n"
1224 " * StartingIndex: %d\n"
1225 " * SearchCriteria: %s\n"
1227 " * SortCriteria: %s\n",
1228 ContainerID
, RequestedCount
, StartingIndex
,
1229 SearchCriteria
, Filter
, SortCriteria
);
1231 if( strcmp(ContainerID
, "0") == 0 )
1233 else if( strcmp(ContainerID
, MUSIC_ALL_ID
) == 0 )
1235 if( !SearchCriteria
)
1237 newSearchCriteria
= strdup("1 = 1");
1238 SearchCriteria
= newSearchCriteria
;
1242 SearchCriteria
= modifyString(SearchCriteria
, """, "\"", 0);
1243 SearchCriteria
= modifyString(SearchCriteria
, "'", "'", 0);
1244 SearchCriteria
= modifyString(SearchCriteria
, "\\\"", "\"\"", 0);
1245 SearchCriteria
= modifyString(SearchCriteria
, "object.", "", 0);
1246 SearchCriteria
= modifyString(SearchCriteria
, "derivedfrom", "like", 1);
1247 SearchCriteria
= modifyString(SearchCriteria
, "contains", "like", 2);
1248 SearchCriteria
= modifyString(SearchCriteria
, "dc:title", "d.TITLE", 0);
1249 SearchCriteria
= modifyString(SearchCriteria
, "dc:creator", "d.CREATOR", 0);
1250 SearchCriteria
= modifyString(SearchCriteria
, "upnp:class", "o.CLASS", 0);
1251 SearchCriteria
= modifyString(SearchCriteria
, "upnp:actor", "d.ARTIST", 0);
1252 SearchCriteria
= modifyString(SearchCriteria
, "upnp:artist", "d.ARTIST", 0);
1253 SearchCriteria
= modifyString(SearchCriteria
, "upnp:album", "d.ALBUM", 0);
1254 SearchCriteria
= modifyString(SearchCriteria
, "upnp:genre", "d.GENRE", 0);
1255 SearchCriteria
= modifyString(SearchCriteria
, "exists true", "is not NULL", 0);
1256 SearchCriteria
= modifyString(SearchCriteria
, "exists false", "is NULL", 0);
1257 SearchCriteria
= modifyString(SearchCriteria
, "@refID", "REF_ID", 0);
1258 if( strstr(SearchCriteria
, "@id") )
1260 newSearchCriteria
= strdup(SearchCriteria
);
1261 SearchCriteria
= newSearchCriteria
= modifyString(newSearchCriteria
, "@id", "OBJECT_ID", 0);
1263 if( strstr(SearchCriteria
, "res is ") )
1265 if( !newSearchCriteria
)
1266 newSearchCriteria
= strdup(SearchCriteria
);
1267 SearchCriteria
= newSearchCriteria
= modifyString(newSearchCriteria
, "res is ", "MIME is ", 0);
1269 #if 0 // Does 360 need this?
1270 if( strstr(SearchCriteria
, "&") )
1272 if( newSearchCriteria
)
1273 newSearchCriteria
= modifyString(newSearchCriteria
, "&", "&amp;", 0);
1275 newSearchCriteria
= modifyString(strdup(SearchCriteria
), "&", "&amp;", 0);
1276 SearchCriteria
= newSearchCriteria
;
1280 DPRINTF(E_DEBUG
, L_HTTP
, "Translated SearchCriteria: %s\n", SearchCriteria
);
1282 sql
= sqlite3_mprintf("SELECT (select count(distinct DETAIL_ID)"
1283 " from OBJECTS o left join DETAILS d on (o.DETAIL_ID = d.ID)"
1284 " where (OBJECT_ID glob '%s$*') and (%s))"
1286 "(select count(*) from OBJECTS o left join DETAILS d on (o.DETAIL_ID = d.ID)"
1287 " where (OBJECT_ID = '%s') and (%s))",
1288 ContainerID
, SearchCriteria
, ContainerID
, SearchCriteria
);
1289 //DEBUG DPRINTF(E_DEBUG, L_HTTP, "Count SQL: %s\n", sql);
1290 ret
= sql_get_table(db
, sql
, &result
, NULL
, NULL
);
1292 if( ret
== SQLITE_OK
)
1294 totalMatches
= atoi(result
[1]);
1295 sqlite3_free_table(result
);
1299 /* Must be invalid SQL, so most likely bad or unhandled search criteria. */
1300 SoapError(h
, 708, "Unsupported or invalid search criteria");
1303 /* Does the object even exist? */
1306 ret
= sql_get_int_field(db
, "SELECT count(*) from OBJECTS where OBJECT_ID = '%q'",
1307 !strcmp(ContainerID
, "*")?"0":ContainerID
);
1310 SoapError(h
, 710, "No such container");
1314 #ifdef __sparc__ /* Sorting takes too long on slow processors with very large containers */
1316 if( totalMatches
< 10000 )
1318 orderBy
= parse_sort_criteria(SortCriteria
, &ret
);
1319 /* If it's a DLNA client, return an error for bad sort criteria */
1320 if( (args
.flags
& FLAG_DLNA
) && ret
)
1322 SoapError(h
, 709, "Unsupported or invalid sort criteria");
1326 sql
= sqlite3_mprintf( SELECT_COLUMNS
1327 "from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)"
1328 " where OBJECT_ID glob '%s$*' and (%s) %s "
1331 ContainerID
, SearchCriteria
, groupBy
,
1332 (*ContainerID
== '*') ? NULL
:
1333 sqlite3_mprintf("UNION ALL " SELECT_COLUMNS
1334 "from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)"
1335 " where OBJECT_ID = '%s' and (%s) ", ContainerID
, SearchCriteria
),
1336 orderBy
, StartingIndex
, RequestedCount
);
1337 DPRINTF(E_DEBUG
, L_HTTP
, "Search SQL: %s\n", sql
);
1338 ret
= sqlite3_exec(db
, sql
, callback
, (void *) &args
, &zErrMsg
);
1339 if( (ret
!= SQLITE_OK
) && (zErrMsg
!= NULL
) )
1341 DPRINTF(E_WARN
, L_HTTP
, "SQL error: %s\nBAD SQL: %s\n", zErrMsg
, sql
);
1342 sqlite3_free(zErrMsg
);
1345 ret
= strcatf(&str
, "</DIDL-Lite></Result>\n"
1346 "<NumberReturned>%u</NumberReturned>\n"
1347 "<TotalMatches>%u</TotalMatches>\n"
1348 "<UpdateID>%u</UpdateID>"
1349 "</u:SearchResponse>",
1350 args
.returned
, totalMatches
, updateID
);
1351 BuildSendAndCloseSoapResp(h
, str
.data
, str
.off
);
1353 ClearNameValueList(&data
);
1354 if( h
->reqflags
& FLAG_MS_PFS
)
1355 sqlite3_free(ContainerID
);
1357 free(newSearchCriteria
);
1362 If a control point calls QueryStateVariable on a state variable that is not
1363 buffered in memory within (or otherwise available from) the service,
1364 the service must return a SOAP fault with an errorCode of 404 Invalid Var.
1366 QueryStateVariable remains useful as a limited test tool but may not be
1367 part of some future versions of UPnP.
1370 QueryStateVariable(struct upnphttp
* h
, const char * action
)
1372 static const char resp
[] =
1375 "<return>%s</return>"
1380 struct NameValueParserData data
;
1381 const char * var_name
;
1383 ParseNameValue(h
->req_buf
+ h
->req_contentoff
, h
->req_contentlen
, &data
);
1384 /*var_name = GetValueFromNameValueList(&data, "QueryStateVariable"); */
1385 /*var_name = GetValueFromNameValueListIgnoreNS(&data, "varName");*/
1386 var_name
= GetValueFromNameValueList(&data
, "varName");
1388 DPRINTF(E_INFO
, L_HTTP
, "QueryStateVariable(%.40s)\n", var_name
);
1392 SoapError(h
, 402, "Invalid Args");
1394 else if(strcmp(var_name
, "ConnectionStatus") == 0)
1396 bodylen
= snprintf(body
, sizeof(body
), resp
,
1397 action
, "urn:schemas-upnp-org:control-1-0",
1398 "Connected", action
);
1399 BuildSendAndCloseSoapResp(h
, body
, bodylen
);
1403 DPRINTF(E_WARN
, L_HTTP
, "%s: Unknown: %s\n", action
, var_name
?var_name
:"");
1404 SoapError(h
, 404, "Invalid Var");
1407 ClearNameValueList(&data
);
1411 SamsungGetFeatureList(struct upnphttp
* h
, const char * action
)
1413 static const char resp
[] =
1414 "<u:X_GetFeatureListResponse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\">"
1416 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
1417 "<Features xmlns=\"urn:schemas-upnp-org:av:avs\""
1418 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
1419 " xsi:schemaLocation=\"urn:schemas-upnp-org:av:avs http://www.upnp.org/schemas/av/avs.xsd\">"
1420 "<Feature name=\"samsung.com_BASICVIEW\" version=\"1\">"
1421 "<container id=\"1\" type=\"object.item.audioItem\"/>"
1422 "<container id=\"2\" type=\"object.item.videoItem\"/>"
1423 "<container id=\"3\" type=\"object.item.imageItem\"/>"
1425 "</FeatureList></u:X_GetFeatureListResponse>";
1427 BuildSendAndCloseSoapResp(h
, resp
, sizeof(resp
));
1432 const char * methodName
;
1433 void (*methodImpl
)(struct upnphttp
*, const char *);
1437 { "QueryStateVariable", QueryStateVariable
},
1438 { "Browse", BrowseContentDirectory
},
1439 { "Search", SearchContentDirectory
},
1440 { "GetSearchCapabilities", GetSearchCapabilities
},
1441 { "GetSortCapabilities", GetSortCapabilities
},
1442 { "GetSystemUpdateID", GetSystemUpdateID
},
1443 { "GetProtocolInfo", GetProtocolInfo
},
1444 { "GetCurrentConnectionIDs", GetCurrentConnectionIDs
},
1445 { "GetCurrentConnectionInfo", GetCurrentConnectionInfo
},
1446 { "IsAuthorized", IsAuthorizedValidated
},
1447 { "IsValidated", IsAuthorizedValidated
},
1448 { "X_GetFeatureList", SamsungGetFeatureList
},
1453 ExecuteSoapAction(struct upnphttp
* h
, const char * action
, int n
)
1457 p
= strchr(action
, '#');
1465 p2
= strchr(p
, '"');
1469 methodlen
= n
- (p
- action
);
1470 DPRINTF(E_DEBUG
, L_HTTP
, "SoapMethod: %.*s\n", methodlen
, p
);
1471 while(soapMethods
[i
].methodName
)
1473 len
= strlen(soapMethods
[i
].methodName
);
1474 if(strncmp(p
, soapMethods
[i
].methodName
, len
) == 0)
1476 soapMethods
[i
].methodImpl(h
, soapMethods
[i
].methodName
);
1482 DPRINTF(E_WARN
, L_HTTP
, "SoapMethod: Unknown: %.*s\n", methodlen
, p
);
1485 SoapError(h
, 401, "Invalid Action");
1490 * errorCode errorDescription Description
1491 * -------- ---------------- -----------
1492 * 401 Invalid Action No action by that name at this service.
1493 * 402 Invalid Args Could be any of the following: not enough in args,
1494 * too many in args, no in arg by that name,
1495 * one or more in args are of the wrong data type.
1496 * 403 Out of Sync Out of synchronization.
1497 * 501 Action Failed May be returned in current state of service
1498 * prevents invoking that action.
1499 * 600-699 TBD Common action errors. Defined by UPnP Forum
1500 * Technical Committee.
1501 * 700-799 TBD Action-specific errors for standard actions.
1502 * Defined by UPnP Forum working committee.
1503 * 800-899 TBD Action-specific errors for non-standard actions.
1504 * Defined by UPnP vendor.
1507 SoapError(struct upnphttp
* h
, int errCode
, const char * errDesc
)
1509 static const char resp
[] =
1511 "xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
1512 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
1515 "<faultcode>s:Client</faultcode>"
1516 "<faultstring>UPnPError</faultstring>"
1518 "<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">"
1519 "<errorCode>%d</errorCode>"
1520 "<errorDescription>%s</errorDescription>"
1530 DPRINTF(E_WARN
, L_HTTP
, "Returning UPnPError %d: %s\n", errCode
, errDesc
);
1531 bodylen
= snprintf(body
, sizeof(body
), resp
, errCode
, errDesc
);
1532 BuildResp2_upnphttp(h
, 500, "Internal Server Error", body
, bodylen
);
1533 SendResp_upnphttp(h
);
1534 CloseSocket_upnphttp(h
);