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
, struct upnphttp
*h
)
351 char *item
, *saveptr
= NULL
;
354 if( !filter
|| (strlen(filter
) <= 1) )
356 if( h
->reqflags
& FLAG_SAMSUNG
)
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( h
->reqflags
& FLAG_SAMSUNG
)
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
*args
)
560 if( args
->flags
& FLAG_NO_RESIZE
)
563 strcatf(args
->str
, "<res ");
564 if( args
->filter
& FILTER_RES_RESOLUTION
)
567 dsth
= ((((reqw
<<10)/srcw
)*srch
)>>10);
570 dstw
= (((reqh
<<10)/srch
) * srcw
>>10);
572 strcatf(args
->str
, "resolution=\"%dx%d\" ", dstw
, dsth
);
574 strcatf(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
[args
->iface
].str
, runtime_vars
.port
,
578 detailID
, dstw
, dsth
);
582 add_res(char *size
, char *duration
, char *bitrate
, char *sampleFrequency
,
583 char *nrAudioChannels
, char *resolution
, char *dlna_pn
, char *mime
,
584 char *detailID
, char *ext
, struct Response
*args
)
586 strcatf(args
->str
, "<res ");
587 if( size
&& (args
->filter
& FILTER_RES_SIZE
) ) {
588 strcatf(args
->str
, "size=\"%s\" ", size
);
590 if( duration
&& (args
->filter
& FILTER_RES_DURATION
) ) {
591 strcatf(args
->str
, "duration=\"%s\" ", duration
);
593 if( bitrate
&& (args
->filter
& FILTER_RES_BITRATE
) ) {
594 strcatf(args
->str
, "bitrate=\"%s\" ", bitrate
);
596 if( sampleFrequency
&& (args
->filter
& FILTER_RES_SAMPLEFREQUENCY
) ) {
597 strcatf(args
->str
, "sampleFrequency=\"%s\" ", sampleFrequency
);
599 if( nrAudioChannels
&& (args
->filter
& FILTER_RES_NRAUDIOCHANNELS
) ) {
600 strcatf(args
->str
, "nrAudioChannels=\"%s\" ", nrAudioChannels
);
602 if( resolution
&& (args
->filter
& FILTER_RES_RESOLUTION
) ) {
603 strcatf(args
->str
, "resolution=\"%s\" ", resolution
);
605 strcatf(args
->str
, "protocolInfo=\"http-get:*:%s:%s\">"
606 "http://%s:%d/MediaItems/%s.%s"
608 mime
, dlna_pn
, lan_addr
[args
->iface
].str
,
609 runtime_vars
.port
, detailID
, ext
);
612 #define SELECT_COLUMNS "SELECT o.OBJECT_ID, o.PARENT_ID, o.REF_ID, o.DETAIL_ID, o.CLASS," \
613 " d.SIZE, d.TITLE, d.DURATION, d.BITRATE, d.SAMPLERATE, d.ARTIST," \
614 " d.ALBUM, d.GENRE, d.COMMENT, d.CHANNELS, d.TRACK, d.DATE, d.RESOLUTION," \
615 " d.THUMBNAIL, d.CREATOR, d.DLNA_PN, d.MIME, d.ALBUM_ART, d.DISC "
618 callback(void *args
, int argc
, char **argv
, char **azColName
)
620 struct Response
*passed_args
= (struct Response
*)args
;
621 char *id
= argv
[0], *parent
= argv
[1], *refID
= argv
[2], *detailID
= argv
[3], *class = argv
[4], *size
= argv
[5], *title
= argv
[6],
622 *duration
= argv
[7], *bitrate
= argv
[8], *sampleFrequency
= argv
[9], *artist
= argv
[10], *album
= argv
[11],
623 *genre
= argv
[12], *comment
= argv
[13], *nrAudioChannels
= argv
[14], *track
= argv
[15], *date
= argv
[16], *resolution
= argv
[17],
624 *tn
= argv
[18], *creator
= argv
[19], *dlna_pn
= argv
[20], *mime
= argv
[21], *album_art
= argv
[22];
627 struct string_s
*str
= passed_args
->str
;
630 /* Make sure we have at least 8KB left of allocated memory to finish the response. */
631 if( str
->off
> (str
->size
- 8192) )
633 #if MAX_RESPONSE_SIZE > 0
634 if( (str
->size
+DEFAULT_RESP_SIZE
) <= MAX_RESPONSE_SIZE
)
637 str
->data
= realloc(str
->data
, (str
->off
+DEFAULT_RESP_SIZE
));
640 str
->size
+= DEFAULT_RESP_SIZE
;
641 DPRINTF(E_DEBUG
, L_HTTP
, "UPnP SOAP response enlarged to %d. [%d results so far]\n",
642 str
->size
, passed_args
->returned
);
646 DPRINTF(E_ERROR
, L_HTTP
, "UPnP SOAP response was too big, and realloc failed!\n");
649 #if MAX_RESPONSE_SIZE > 0
653 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
);
658 passed_args
->returned
++;
661 sprintf(dlna_buf
, "DLNA.ORG_PN=%s", dlna_pn
);
662 else if( passed_args
->flags
& FLAG_DLNA
)
663 strcpy(dlna_buf
, dlna_no_conv
);
665 strcpy(dlna_buf
, "*");
667 if( strncmp(class, "item", 4) == 0 )
669 /* We may need special handling for certain MIME types */
672 if( passed_args
->flags
& FLAG_MIME_AVI_DIVX
)
674 if( strcmp(mime
, "video/x-msvideo") == 0 )
677 strcpy(mime
+6, "divx");
679 strcpy(mime
+6, "avi");
682 else if( passed_args
->flags
& FLAG_MIME_AVI_AVI
)
684 if( strcmp(mime
, "video/x-msvideo") == 0 )
686 strcpy(mime
+6, "avi");
689 if( !(passed_args
->flags
& FLAG_DLNA
) )
691 if( strcmp(mime
+6, "vnd.dlna.mpeg-tts") == 0 )
693 strcpy(mime
+6, "mpeg");
696 /* From what I read, Samsung TV's expect a [wrong] MIME type of x-mkv. */
697 if( passed_args
->flags
& FLAG_SAMSUNG
)
699 if( strcmp(mime
+6, "x-matroska") == 0 )
701 strcpy(mime
+8, "mkv");
705 else if( *mime
== 'a' )
707 if( strcmp(mime
+6, "x-flac") == 0 )
709 if( passed_args
->flags
& FLAG_MIME_FLAC_FLAC
)
711 strcpy(mime
+6, "flac");
716 ret
= strcatf(str
, "<item id=\"%s\" parentID=\"%s\" restricted=\"1\"", id
, parent
);
717 if( refID
&& (passed_args
->filter
& FILTER_REFID
) ) {
718 ret
= strcatf(str
, " refID=\"%s\"", refID
);
720 ret
= strcatf(str
, ">"
721 "<dc:title>%s</dc:title>"
722 "<upnp:class>object.%s</upnp:class>",
724 if( comment
&& (passed_args
->filter
& FILTER_DC_DESCRIPTION
) ) {
725 ret
= strcatf(str
, "<dc:description>%.384s</dc:description>", comment
);
727 if( creator
&& (passed_args
->filter
& FILTER_DC_CREATOR
) ) {
728 ret
= strcatf(str
, "<dc:creator>%s</dc:creator>", creator
);
730 if( date
&& (passed_args
->filter
& FILTER_DC_DATE
) ) {
731 ret
= strcatf(str
, "<dc:date>%s</dc:date>", date
);
734 if( (*mime
== 'v') && (passed_args
->filter
& FILTER_UPNP_ACTOR
) ) {
735 ret
= strcatf(str
, "<upnp:actor>%s</upnp:actor>", artist
);
737 if( passed_args
->filter
& FILTER_UPNP_ARTIST
) {
738 ret
= strcatf(str
, "<upnp:artist>%s</upnp:artist>", artist
);
741 if( album
&& (passed_args
->filter
& FILTER_UPNP_ALBUM
) ) {
742 ret
= strcatf(str
, "<upnp:album>%s</upnp:album>", album
);
744 if( genre
&& (passed_args
->filter
& FILTER_UPNP_GENRE
) ) {
745 ret
= strcatf(str
, "<upnp:genre>%s</upnp:genre>", genre
);
747 if( strncmp(id
, MUSIC_PLIST_ID
, strlen(MUSIC_PLIST_ID
)) == 0 ) {
748 track
= strrchr(id
, '$')+1;
750 if( track
&& atoi(track
) && (passed_args
->filter
& FILTER_UPNP_ORIGINALTRACKNUMBER
) ) {
751 ret
= strcatf(str
, "<upnp:originalTrackNumber>%s</upnp:originalTrackNumber>", track
);
753 if( album_art
&& atoi(album_art
) )
755 /* Video and audio album art is handled differently */
756 if( *mime
== 'v' && (passed_args
->filter
& FILTER_RES
) && !(passed_args
->flags
& FLAG_MS_PFS
) ) {
757 ret
= strcatf(str
, "<res protocolInfo=\"http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_TN\">"
758 "http://%s:%d/AlbumArt/%s-%s.jpg"
760 lan_addr
[passed_args
->iface
].str
, runtime_vars
.port
, album_art
, detailID
);
761 } else if( passed_args
->filter
& FILTER_UPNP_ALBUMARTURI
) {
762 ret
= strcatf(str
, "<upnp:albumArtURI");
763 if( passed_args
->filter
& FILTER_UPNP_ALBUMARTURI_DLNA_PROFILEID
) {
764 ret
= strcatf(str
, " dlna:profileID=\"JPEG_TN\" xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\"");
766 ret
= strcatf(str
, ">http://%s:%d/AlbumArt/%s-%s.jpg</upnp:albumArtURI>",
767 lan_addr
[passed_args
->iface
].str
, runtime_vars
.port
, album_art
, detailID
);
770 if( (passed_args
->flags
& FLAG_MS_PFS
) && *mime
== 'i' ) {
771 if( passed_args
->client
== EMediaRoom
&& !album
)
772 ret
= strcatf(str
, "<upnp:album>%s</upnp:album>", "[No Keywords]");
774 /* EVA2000 doesn't seem to handle embedded thumbnails */
775 if( passed_args
->client
!= ENetgearEVA2000
&& tn
&& atoi(tn
) ) {
776 ret
= strcatf(str
, "<upnp:albumArtURI>"
777 "http://%s:%d/Thumbnails/%s.jpg"
778 "</upnp:albumArtURI>",
779 lan_addr
[passed_args
->iface
].str
, runtime_vars
.port
, detailID
);
781 ret
= strcatf(str
, "<upnp:albumArtURI>"
782 "http://%s:%d/Resized/%s.jpg?width=160,height=160"
783 "</upnp:albumArtURI>",
784 lan_addr
[passed_args
->iface
].str
, runtime_vars
.port
, detailID
);
787 if( passed_args
->filter
& FILTER_RES
) {
788 mime_to_ext(mime
, ext
);
789 if( (passed_args
->client
== EFreeBox
) && tn
&& atoi(tn
) ) {
790 ret
= strcatf(str
, "<res protocolInfo=\"http-get:*:%s:%s\">"
791 "http://%s:%d/Thumbnails/%s.jpg"
793 mime
, "DLNA.ORG_PN=JPEG_TN", lan_addr
[passed_args
->iface
].str
,
794 runtime_vars
.port
, detailID
);
796 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
797 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
798 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
);
807 if( tn
&& atoi(tn
) ) {
808 ret
= strcatf(str
, "<res protocolInfo=\"http-get:*:%s:%s\">"
809 "http://%s:%d/Thumbnails/%s.jpg"
811 mime
, "DLNA.ORG_PN=JPEG_TN", lan_addr
[passed_args
->iface
].str
,
812 runtime_vars
.port
, detailID
);
815 else if( *mime
== 'v' ) {
816 switch( passed_args
->client
) {
819 (strncmp(dlna_pn
, "MPEG_TS_HD_NA", 13) == 0 ||
820 strncmp(dlna_pn
, "MPEG_TS_SD_NA", 13) == 0 ||
821 strncmp(dlna_pn
, "AVC_TS_MP_HD_AC3", 16) == 0 ||
822 strncmp(dlna_pn
, "AVC_TS_HP_HD_AC3", 16) == 0))
824 sprintf(dlna_buf
, "DLNA.ORG_PN=MPEG_PS_NTSC;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
825 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
826 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
831 (strncmp(dlna_pn
, "AVC_TS", 6) == 0 ||
832 strncmp(dlna_pn
, "MPEG_TS", 7) == 0) )
834 if( strncmp(dlna_pn
, "MPEG_TS_SD_NA", 13) != 0 )
836 sprintf(dlna_buf
, "DLNA.ORG_PN=MPEG_TS_SD_NA;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
837 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
838 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
840 if( strncmp(dlna_pn
, "MPEG_TS_SD_EU", 13) != 0 )
842 sprintf(dlna_buf
, "DLNA.ORG_PN=MPEG_TS_SD_EU;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
843 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
844 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
848 (strncmp(dlna_pn
, "AVC_MP4", 7) == 0 ||
849 strncmp(dlna_pn
, "MPEG4_P2_MP4", 12) == 0)) ||
850 strcmp(mime
+6, "x-matroska") == 0 ||
851 strcmp(mime
+6, "x-msvideo") == 0 ||
852 strcmp(mime
+6, "mpeg") == 0 )
854 strcpy(mime
+6, "avi");
855 if( !dlna_pn
|| strncmp(dlna_pn
, "MPEG_PS_NTSC", 12) != 0 )
857 sprintf(dlna_buf
, "DLNA.ORG_PN=MPEG_PS_NTSC;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
858 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
859 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
861 if( !dlna_pn
|| strncmp(dlna_pn
, "MPEG_PS_PAL", 11) != 0 )
863 sprintf(dlna_buf
, "DLNA.ORG_PN=MPEG_PS_PAL;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
864 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
865 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
870 /* BRAVIA KDL-##*X### series TVs do natively support AVC/AC3 in TS, but
871 require profile to be renamed (applies to _T and _ISO variants also) */
873 (strncmp(dlna_pn
, "AVC_TS_MP_SD_AC3", 16) == 0 ||
874 strncmp(dlna_pn
, "AVC_TS_MP_HD_AC3", 16) == 0 ||
875 strncmp(dlna_pn
, "AVC_TS_HP_HD_AC3", 16) == 0))
877 sprintf(dlna_buf
, "DLNA.ORG_PN=AVC_TS_HD_50_AC3;DLNA.ORG_OP=01;DLNA.ORG_CI=0");
878 add_res(size
, duration
, bitrate
, sampleFrequency
, nrAudioChannels
,
879 resolution
, dlna_buf
, mime
, detailID
, ext
, passed_args
);
883 if( sql_get_int_field(db
, "SELECT ID from CAPTIONS where ID = '%s'", detailID
) > 0 )
885 ret
= strcatf(str
, "<res protocolInfo=\"http-get:*:text/srt:*\">"
886 "http://%s:%d/Captions/%s.srt"
888 lan_addr
[passed_args
->iface
].str
, runtime_vars
.port
, detailID
);
896 ret
= strcatf(str
, "</item>");
898 else if( strncmp(class, "container", 9) == 0 )
900 ret
= strcatf(str
, "<container id=\"%s\" parentID=\"%s\" restricted=\"1\" ", id
, parent
);
901 if( passed_args
->filter
& FILTER_CHILDCOUNT
)
904 ret
= sql_get_int_field(db
, "SELECT count(*) from OBJECTS where PARENT_ID = '%s';", id
);
905 children
= (ret
> 0) ? ret
: 0;
906 ret
= strcatf(str
, "childCount=\"%d\"", children
);
908 /* If the client calls for BrowseMetadata on root, we have to include our "upnp:searchClass"'s, unless they're filtered out */
909 if( (passed_args
->requested
== 1) && (strcmp(id
, "0") == 0) )
911 if( passed_args
->filter
& FILTER_UPNP_SEARCHCLASS
)
913 ret
= strcatf(str
, ">"
914 "<upnp:searchClass includeDerived=\"1\">object.item.audioItem</upnp:searchClass>"
915 "<upnp:searchClass includeDerived=\"1\">object.item.imageItem</upnp:searchClass>"
916 "<upnp:searchClass includeDerived=\"1\">object.item.videoItem</upnp:searchClass");
919 ret
= strcatf(str
, ">"
920 "<dc:title>%s</dc:title>"
921 "<upnp:class>object.%s</upnp:class>",
923 if( creator
&& (passed_args
->filter
& FILTER_DC_CREATOR
) ) {
924 ret
= strcatf(str
, "<dc:creator>%s</dc:creator>", creator
);
926 if( genre
&& (passed_args
->filter
& FILTER_UPNP_GENRE
) ) {
927 ret
= strcatf(str
, "<upnp:genre>%s</upnp:genre>", genre
);
929 if( artist
&& (passed_args
->filter
& FILTER_UPNP_ARTIST
) ) {
930 ret
= strcatf(str
, "<upnp:artist>%s</upnp:artist>", artist
);
932 if( album_art
&& atoi(album_art
) && (passed_args
->filter
& FILTER_UPNP_ALBUMARTURI
) ) {
933 ret
= strcatf(str
, "<upnp:albumArtURI ");
934 if( passed_args
->filter
& FILTER_UPNP_ALBUMARTURI_DLNA_PROFILEID
) {
935 ret
= strcatf(str
, "dlna:profileID=\"JPEG_TN\" xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\"");
937 ret
= strcatf(str
, ">http://%s:%d/AlbumArt/%s-%s.jpg</upnp:albumArtURI>",
938 lan_addr
[passed_args
->iface
].str
, runtime_vars
.port
, album_art
, detailID
);
940 ret
= strcatf(str
, "</container>");
947 BrowseContentDirectory(struct upnphttp
* h
, const char * action
)
949 static const char resp0
[] =
951 "xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\">"
954 CONTENT_DIRECTORY_SCHEMAS
;
958 struct Response args
;
961 struct NameValueParserData data
;
963 ParseNameValue(h
->req_buf
+ h
->req_contentoff
, h
->req_contentlen
, &data
);
964 char * ObjectID
= GetValueFromNameValueList(&data
, "ObjectID");
965 char * Filter
= GetValueFromNameValueList(&data
, "Filter");
966 char * BrowseFlag
= GetValueFromNameValueList(&data
, "BrowseFlag");
967 char * SortCriteria
= GetValueFromNameValueList(&data
, "SortCriteria");
968 char * orderBy
= NULL
;
969 int RequestedCount
= 0;
970 int StartingIndex
= 0;
971 if( (ptr
= GetValueFromNameValueList(&data
, "RequestedCount")) )
972 RequestedCount
= atoi(ptr
);
973 if( !RequestedCount
)
975 if( (ptr
= GetValueFromNameValueList(&data
, "StartingIndex")) )
976 StartingIndex
= atoi(ptr
);
977 if( !BrowseFlag
|| (strcmp(BrowseFlag
, "BrowseDirectChildren") && strcmp(BrowseFlag
, "BrowseMetadata")) )
979 SoapError(h
, 402, "Invalid Args");
982 if( !ObjectID
&& !(ObjectID
= GetValueFromNameValueList(&data
, "ContainerID")) )
984 SoapError(h
, 701, "No such object error");
987 memset(&args
, 0, sizeof(args
));
988 memset(&str
, 0, sizeof(str
));
990 str
.data
= malloc(DEFAULT_RESP_SIZE
);
991 str
.size
= DEFAULT_RESP_SIZE
;
992 str
.off
= sprintf(str
.data
, "%s", resp0
);
993 /* See if we need to include DLNA namespace reference */
994 args
.iface
= h
->iface
;
995 args
.filter
= set_filter_flags(Filter
, h
);
996 if( args
.filter
& FILTER_DLNA_NAMESPACE
)
998 ret
= strcatf(&str
, DLNA_NAMESPACE
);
1000 strcatf(&str
, ">\n");
1003 args
.requested
= RequestedCount
;
1004 args
.client
= h
->req_client
;
1005 args
.flags
= h
->reqflags
;
1007 if( args
.flags
& FLAG_MS_PFS
)
1009 if( !strchr(ObjectID
, '$') && (strcmp(ObjectID
, "0") != 0) )
1011 ptr
= sql_get_text_field(db
, "SELECT OBJECT_ID from OBJECTS"
1012 " where OBJECT_ID in "
1013 "('"MUSIC_ID
"$%s', '"VIDEO_ID
"$%s', '"IMAGE_ID
"$%s')",
1014 ObjectID
, ObjectID
, ObjectID
);
1018 args
.flags
|= FLAG_FREE_OBJECT_ID
;
1022 DPRINTF(E_DEBUG
, L_HTTP
, "Browsing ContentDirectory:\n"
1025 " * StartingIndex: %d\n"
1026 " * BrowseFlag: %s\n"
1028 " * SortCriteria: %s\n",
1029 ObjectID
, RequestedCount
, StartingIndex
,
1030 BrowseFlag
, Filter
, SortCriteria
);
1032 if( (args
.flags
& FLAG_AUDIO_ONLY
) && (strcmp(ObjectID
, "0") == 0) )
1034 ObjectID
= sqlite3_mprintf("%s", MUSIC_ID
);
1035 args
.flags
|= FLAG_FREE_OBJECT_ID
;
1038 if( strcmp(BrowseFlag
+6, "Metadata") == 0 )
1041 sql
= sqlite3_mprintf( SELECT_COLUMNS
1042 "from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)"
1043 " where OBJECT_ID = '%s';"
1045 ret
= sqlite3_exec(db
, sql
, callback
, (void *) &args
, &zErrMsg
);
1046 totalMatches
= args
.returned
;
1050 ret
= sql_get_int_field(db
, "SELECT count(*) from OBJECTS where PARENT_ID = '%s'", ObjectID
);
1051 totalMatches
= (ret
> 0) ? ret
: 0;
1055 #ifdef __sparc__ /* Sorting takes too long on slow processors with very large containers */
1056 if( totalMatches
< 10000 )
1058 orderBy
= parse_sort_criteria(SortCriteria
, &ret
);
1062 if( strncmp(ObjectID
, MUSIC_PLIST_ID
, strlen(MUSIC_PLIST_ID
)) == 0 )
1064 if( strcmp(ObjectID
, MUSIC_PLIST_ID
) == 0 )
1065 asprintf(&orderBy
, "order by d.TITLE");
1067 asprintf(&orderBy
, "order by length(OBJECT_ID), OBJECT_ID");
1069 else if( args
.client
== ERokuSoundBridge
)
1072 if( totalMatches
< 10000 )
1074 asprintf(&orderBy
, "order by o.CLASS, d.DISC, d.TRACK, d.TITLE");
1077 /* If it's a DLNA client, return an error for bad sort criteria */
1078 if( (args
.flags
& FLAG_DLNA
) && ret
)
1080 SoapError(h
, 709, "Unsupported or invalid sort criteria");
1084 sql
= sqlite3_mprintf( SELECT_COLUMNS
1085 "from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)"
1086 " where PARENT_ID = '%s' %s limit %d, %d;",
1087 ObjectID
, orderBy
, StartingIndex
, RequestedCount
);
1088 DPRINTF(E_DEBUG
, L_HTTP
, "Browse SQL: %s\n", sql
);
1089 ret
= sqlite3_exec(db
, sql
, callback
, (void *) &args
, &zErrMsg
);
1092 if( (ret
!= SQLITE_OK
) && (zErrMsg
!= NULL
) )
1094 DPRINTF(E_WARN
, L_HTTP
, "SQL error: %s\nBAD SQL: %s\n", zErrMsg
, sql
);
1095 sqlite3_free(zErrMsg
);
1097 /* Does the object even exist? */
1100 ret
= sql_get_int_field(db
, "SELECT count(*) from OBJECTS where OBJECT_ID = '%s'", ObjectID
);
1103 SoapError(h
, 701, "No such object error");
1107 ret
= strcatf(&str
, "</DIDL-Lite></Result>\n"
1108 "<NumberReturned>%u</NumberReturned>\n"
1109 "<TotalMatches>%u</TotalMatches>\n"
1110 "<UpdateID>%u</UpdateID>"
1111 "</u:BrowseResponse>",
1112 args
.returned
, totalMatches
, updateID
);
1113 BuildSendAndCloseSoapResp(h
, str
.data
, str
.off
);
1115 ClearNameValueList(&data
);
1116 if( args
.flags
& FLAG_FREE_OBJECT_ID
)
1117 sqlite3_free(ObjectID
);
1123 SearchContentDirectory(struct upnphttp
* h
, const char * action
)
1125 static const char resp0
[] =
1126 "<u:SearchResponse "
1127 "xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\">"
1130 CONTENT_DIRECTORY_SCHEMAS
;
1134 struct Response args
;
1135 struct string_s str
;
1136 int totalMatches
= 0;
1139 struct NameValueParserData data
;
1140 ParseNameValue(h
->req_buf
+ h
->req_contentoff
, h
->req_contentlen
, &data
);
1141 char * ContainerID
= GetValueFromNameValueList(&data
, "ContainerID");
1142 char * Filter
= GetValueFromNameValueList(&data
, "Filter");
1143 char * SearchCriteria
= GetValueFromNameValueList(&data
, "SearchCriteria");
1144 char * SortCriteria
= GetValueFromNameValueList(&data
, "SortCriteria");
1145 char * newSearchCriteria
= NULL
;
1146 char * orderBy
= NULL
;
1147 char groupBy
[] = "group by DETAIL_ID";
1148 int RequestedCount
= 0;
1149 int StartingIndex
= 0;
1150 if( (ptr
= GetValueFromNameValueList(&data
, "RequestedCount")) )
1151 RequestedCount
= atoi(ptr
);
1152 if( !RequestedCount
)
1153 RequestedCount
= -1;
1154 if( (ptr
= GetValueFromNameValueList(&data
, "StartingIndex")) )
1155 StartingIndex
= atoi(ptr
);
1158 if( !(ContainerID
= GetValueFromNameValueList(&data
, "ObjectID")) )
1160 SoapError(h
, 701, "No such object error");
1164 memset(&args
, 0, sizeof(args
));
1165 memset(&str
, 0, sizeof(str
));
1167 str
.data
= malloc(DEFAULT_RESP_SIZE
);
1168 str
.size
= DEFAULT_RESP_SIZE
;
1169 str
.off
= sprintf(str
.data
, "%s", resp0
);
1170 /* See if we need to include DLNA namespace reference */
1171 args
.iface
= h
->iface
;
1172 args
.filter
= set_filter_flags(Filter
, h
);
1173 if( args
.filter
& FILTER_DLNA_NAMESPACE
)
1175 ret
= strcatf(&str
, DLNA_NAMESPACE
);
1177 strcatf(&str
, ">\n");
1180 args
.requested
= RequestedCount
;
1181 args
.client
= h
->req_client
;
1182 args
.flags
= h
->reqflags
;
1184 if( args
.flags
& FLAG_MS_PFS
)
1186 if( !strchr(ContainerID
, '$') && (strcmp(ContainerID
, "0") != 0) )
1188 ptr
= sql_get_text_field(db
, "SELECT OBJECT_ID from OBJECTS"
1189 " where OBJECT_ID in "
1190 "('"MUSIC_ID
"$%s', '"VIDEO_ID
"$%s', '"IMAGE_ID
"$%s')",
1191 ContainerID
, ContainerID
, ContainerID
);
1195 args
.flags
|= FLAG_FREE_OBJECT_ID
;
1198 #if 0 // Looks like the 360 already does this
1199 /* Sort by track number for some containers */
1201 ((strncmp(ContainerID
, MUSIC_GENRE_ID
, 3) == 0) ||
1202 (strncmp(ContainerID
, MUSIC_ARTIST_ID
, 3) == 0) ||
1203 (strncmp(ContainerID
, MUSIC_ALBUM_ID
, 3) == 0)) )
1205 DPRINTF(E_DEBUG
, L_HTTP
, "Old sort order: %s\n", orderBy
);
1206 sprintf(str_buf
, "d.TRACK, ");
1207 memmove(orderBy
+18, orderBy
+9, strlen(orderBy
)+1);
1208 memmove(orderBy
+9, &str_buf
, 9);
1209 DPRINTF(E_DEBUG
, L_HTTP
, "New sort order: %s\n", orderBy
);
1213 DPRINTF(E_DEBUG
, L_HTTP
, "Searching ContentDirectory:\n"
1216 " * StartingIndex: %d\n"
1217 " * SearchCriteria: %s\n"
1219 " * SortCriteria: %s\n",
1220 ContainerID
, RequestedCount
, StartingIndex
,
1221 SearchCriteria
, Filter
, SortCriteria
);
1223 if( strcmp(ContainerID
, "0") == 0 )
1225 else if( strcmp(ContainerID
, MUSIC_ALL_ID
) == 0 )
1227 if( !SearchCriteria
)
1229 newSearchCriteria
= strdup("1 = 1");
1230 SearchCriteria
= newSearchCriteria
;
1234 SearchCriteria
= modifyString(SearchCriteria
, """, "\"", 0);
1235 SearchCriteria
= modifyString(SearchCriteria
, "'", "'", 0);
1236 SearchCriteria
= modifyString(SearchCriteria
, "\\\"", "\"\"", 0);
1237 SearchCriteria
= modifyString(SearchCriteria
, "object.", "", 0);
1238 SearchCriteria
= modifyString(SearchCriteria
, "derivedfrom", "like", 1);
1239 SearchCriteria
= modifyString(SearchCriteria
, "contains", "like", 2);
1240 SearchCriteria
= modifyString(SearchCriteria
, "dc:title", "d.TITLE", 0);
1241 SearchCriteria
= modifyString(SearchCriteria
, "dc:creator", "d.CREATOR", 0);
1242 SearchCriteria
= modifyString(SearchCriteria
, "upnp:class", "o.CLASS", 0);
1243 SearchCriteria
= modifyString(SearchCriteria
, "upnp:actor", "d.ARTIST", 0);
1244 SearchCriteria
= modifyString(SearchCriteria
, "upnp:artist", "d.ARTIST", 0);
1245 SearchCriteria
= modifyString(SearchCriteria
, "upnp:album", "d.ALBUM", 0);
1246 SearchCriteria
= modifyString(SearchCriteria
, "upnp:genre", "d.GENRE", 0);
1247 SearchCriteria
= modifyString(SearchCriteria
, "exists true", "is not NULL", 0);
1248 SearchCriteria
= modifyString(SearchCriteria
, "exists false", "is NULL", 0);
1249 SearchCriteria
= modifyString(SearchCriteria
, "@refID", "REF_ID", 0);
1250 if( strstr(SearchCriteria
, "@id") )
1252 newSearchCriteria
= strdup(SearchCriteria
);
1253 SearchCriteria
= newSearchCriteria
= modifyString(newSearchCriteria
, "@id", "OBJECT_ID", 0);
1255 if( strstr(SearchCriteria
, "res is ") )
1257 if( !newSearchCriteria
)
1258 newSearchCriteria
= strdup(SearchCriteria
);
1259 SearchCriteria
= newSearchCriteria
= modifyString(newSearchCriteria
, "res is ", "MIME is ", 0);
1261 #if 0 // Does 360 need this?
1262 if( strstr(SearchCriteria
, "&") )
1264 if( newSearchCriteria
)
1265 newSearchCriteria
= modifyString(newSearchCriteria
, "&", "&amp;", 0);
1267 newSearchCriteria
= modifyString(strdup(SearchCriteria
), "&", "&amp;", 0);
1268 SearchCriteria
= newSearchCriteria
;
1272 DPRINTF(E_DEBUG
, L_HTTP
, "Translated SearchCriteria: %s\n", SearchCriteria
);
1274 sql
= sqlite3_mprintf("SELECT (select count(distinct DETAIL_ID)"
1275 " from OBJECTS o left join DETAILS d on (o.DETAIL_ID = d.ID)"
1276 " where (OBJECT_ID glob '%s$*') and (%s))"
1278 "(select count(*) from OBJECTS o left join DETAILS d on (o.DETAIL_ID = d.ID)"
1279 " where (OBJECT_ID = '%s') and (%s))",
1280 ContainerID
, SearchCriteria
, ContainerID
, SearchCriteria
);
1281 //DEBUG DPRINTF(E_DEBUG, L_HTTP, "Count SQL: %s\n", sql);
1282 ret
= sql_get_table(db
, sql
, &result
, NULL
, NULL
);
1284 if( ret
== SQLITE_OK
)
1286 totalMatches
= atoi(result
[1]);
1287 sqlite3_free_table(result
);
1291 /* Must be invalid SQL, so most likely bad or unhandled search criteria. */
1292 SoapError(h
, 708, "Unsupported or invalid search criteria");
1295 /* Does the object even exist? */
1298 ret
= sql_get_int_field(db
, "SELECT count(*) from OBJECTS where OBJECT_ID = '%q'",
1299 !strcmp(ContainerID
, "*")?"0":ContainerID
);
1302 SoapError(h
, 710, "No such container");
1306 #ifdef __sparc__ /* Sorting takes too long on slow processors with very large containers */
1308 if( totalMatches
< 10000 )
1310 orderBy
= parse_sort_criteria(SortCriteria
, &ret
);
1311 /* If it's a DLNA client, return an error for bad sort criteria */
1312 if( (args
.flags
& FLAG_DLNA
) && ret
)
1314 SoapError(h
, 709, "Unsupported or invalid sort criteria");
1318 sql
= sqlite3_mprintf( SELECT_COLUMNS
1319 "from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)"
1320 " where OBJECT_ID glob '%s$*' and (%s) %s "
1323 ContainerID
, SearchCriteria
, groupBy
,
1324 (*ContainerID
== '*') ? NULL
:
1325 sqlite3_mprintf("UNION ALL " SELECT_COLUMNS
1326 "from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)"
1327 " where OBJECT_ID = '%s' and (%s) ", ContainerID
, SearchCriteria
),
1328 orderBy
, StartingIndex
, RequestedCount
);
1329 DPRINTF(E_DEBUG
, L_HTTP
, "Search SQL: %s\n", sql
);
1330 ret
= sqlite3_exec(db
, sql
, callback
, (void *) &args
, &zErrMsg
);
1331 if( (ret
!= SQLITE_OK
) && (zErrMsg
!= NULL
) )
1333 DPRINTF(E_WARN
, L_HTTP
, "SQL error: %s\nBAD SQL: %s\n", zErrMsg
, sql
);
1334 sqlite3_free(zErrMsg
);
1337 ret
= strcatf(&str
, "</DIDL-Lite></Result>\n"
1338 "<NumberReturned>%u</NumberReturned>\n"
1339 "<TotalMatches>%u</TotalMatches>\n"
1340 "<UpdateID>%u</UpdateID>"
1341 "</u:SearchResponse>",
1342 args
.returned
, totalMatches
, updateID
);
1343 BuildSendAndCloseSoapResp(h
, str
.data
, str
.off
);
1345 ClearNameValueList(&data
);
1346 if( args
.flags
& FLAG_FREE_OBJECT_ID
)
1347 sqlite3_free(ContainerID
);
1349 free(newSearchCriteria
);
1354 If a control point calls QueryStateVariable on a state variable that is not
1355 buffered in memory within (or otherwise available from) the service,
1356 the service must return a SOAP fault with an errorCode of 404 Invalid Var.
1358 QueryStateVariable remains useful as a limited test tool but may not be
1359 part of some future versions of UPnP.
1362 QueryStateVariable(struct upnphttp
* h
, const char * action
)
1364 static const char resp
[] =
1367 "<return>%s</return>"
1372 struct NameValueParserData data
;
1373 const char * var_name
;
1375 ParseNameValue(h
->req_buf
+ h
->req_contentoff
, h
->req_contentlen
, &data
);
1376 /*var_name = GetValueFromNameValueList(&data, "QueryStateVariable"); */
1377 /*var_name = GetValueFromNameValueListIgnoreNS(&data, "varName");*/
1378 var_name
= GetValueFromNameValueList(&data
, "varName");
1380 DPRINTF(E_INFO
, L_HTTP
, "QueryStateVariable(%.40s)\n", var_name
);
1384 SoapError(h
, 402, "Invalid Args");
1386 else if(strcmp(var_name
, "ConnectionStatus") == 0)
1388 bodylen
= snprintf(body
, sizeof(body
), resp
,
1389 action
, "urn:schemas-upnp-org:control-1-0",
1390 "Connected", action
);
1391 BuildSendAndCloseSoapResp(h
, body
, bodylen
);
1395 DPRINTF(E_WARN
, L_HTTP
, "%s: Unknown: %s\n", action
, var_name
?var_name
:"");
1396 SoapError(h
, 404, "Invalid Var");
1399 ClearNameValueList(&data
);
1403 SamsungGetFeatureList(struct upnphttp
* h
, const char * action
)
1405 static const char resp
[] =
1406 "<u:X_GetFeatureListResponse xmlns:u=\"urn:schemas-upnp-org:service:ContentDirectory:1\">"
1408 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
1409 "<Features xmlns=\"urn:schemas-upnp-org:av:avs\""
1410 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
1411 " xsi:schemaLocation=\"urn:schemas-upnp-org:av:avs http://www.upnp.org/schemas/av/avs.xsd\">"
1412 "<Feature name=\"samsung.com_BASICVIEW\" version=\"1\">"
1413 "<container id=\"1\" type=\"object.item.audioItem\"/>"
1414 "<container id=\"2\" type=\"object.item.videoItem\"/>"
1415 "<container id=\"3\" type=\"object.item.imageItem\"/>"
1417 "</FeatureList></u:X_GetFeatureListResponse>";
1419 BuildSendAndCloseSoapResp(h
, resp
, sizeof(resp
));
1424 const char * methodName
;
1425 void (*methodImpl
)(struct upnphttp
*, const char *);
1429 { "QueryStateVariable", QueryStateVariable
},
1430 { "Browse", BrowseContentDirectory
},
1431 { "Search", SearchContentDirectory
},
1432 { "GetSearchCapabilities", GetSearchCapabilities
},
1433 { "GetSortCapabilities", GetSortCapabilities
},
1434 { "GetSystemUpdateID", GetSystemUpdateID
},
1435 { "GetProtocolInfo", GetProtocolInfo
},
1436 { "GetCurrentConnectionIDs", GetCurrentConnectionIDs
},
1437 { "GetCurrentConnectionInfo", GetCurrentConnectionInfo
},
1438 { "IsAuthorized", IsAuthorizedValidated
},
1439 { "IsValidated", IsAuthorizedValidated
},
1440 { "X_GetFeatureList", SamsungGetFeatureList
},
1445 ExecuteSoapAction(struct upnphttp
* h
, const char * action
, int n
)
1449 p
= strchr(action
, '#');
1457 p2
= strchr(p
, '"');
1461 methodlen
= n
- (p
- action
);
1462 DPRINTF(E_DEBUG
, L_HTTP
, "SoapMethod: %.*s\n", methodlen
, p
);
1463 while(soapMethods
[i
].methodName
)
1465 len
= strlen(soapMethods
[i
].methodName
);
1466 if(strncmp(p
, soapMethods
[i
].methodName
, len
) == 0)
1468 soapMethods
[i
].methodImpl(h
, soapMethods
[i
].methodName
);
1474 DPRINTF(E_WARN
, L_HTTP
, "SoapMethod: Unknown: %.*s\n", methodlen
, p
);
1477 SoapError(h
, 401, "Invalid Action");
1482 * errorCode errorDescription Description
1483 * -------- ---------------- -----------
1484 * 401 Invalid Action No action by that name at this service.
1485 * 402 Invalid Args Could be any of the following: not enough in args,
1486 * too many in args, no in arg by that name,
1487 * one or more in args are of the wrong data type.
1488 * 403 Out of Sync Out of synchronization.
1489 * 501 Action Failed May be returned in current state of service
1490 * prevents invoking that action.
1491 * 600-699 TBD Common action errors. Defined by UPnP Forum
1492 * Technical Committee.
1493 * 700-799 TBD Action-specific errors for standard actions.
1494 * Defined by UPnP Forum working committee.
1495 * 800-899 TBD Action-specific errors for non-standard actions.
1496 * Defined by UPnP vendor.
1499 SoapError(struct upnphttp
* h
, int errCode
, const char * errDesc
)
1501 static const char resp
[] =
1503 "xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
1504 "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
1507 "<faultcode>s:Client</faultcode>"
1508 "<faultstring>UPnPError</faultstring>"
1510 "<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\">"
1511 "<errorCode>%d</errorCode>"
1512 "<errorDescription>%s</errorDescription>"
1522 DPRINTF(E_WARN
, L_HTTP
, "Returning UPnPError %d: %s\n", errCode
, errDesc
);
1523 bodylen
= snprintf(body
, sizeof(body
), resp
, errCode
, errDesc
);
1524 BuildResp2_upnphttp(h
, 500, "Internal Server Error", body
, bodylen
);
1525 SendResp_upnphttp(h
);
1526 CloseSocket_upnphttp(h
);