summaryrefslogtreecommitdiffstats
path: root/lib/libUPnP/Platinum/Source/Devices/MediaServer/PltFileMediaServer.cpp
blob: 421d0c204271eb2e9e8bd7182f4ec9c0010ea860 (plain)
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
39
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
241
242
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
371
372
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
418
419
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*****************************************************************
|
|   Platinum - File Media Server Delegate
|
| Copyright (c) 2004-2010, Plutinosoft, LLC.
| All rights reserved.
| http://www.plutinosoft.com
|
| This program is free software; you can redistribute it and/or
| modify it under the terms of the GNU General Public License
| as published by the Free Software Foundation; either version 2
| of the License, or (at your option) any later version.
|
| OEMs, ISVs, VARs and other distributors that combine and 
| distribute commercially licensed software with Platinum software
| and do not wish to distribute the source code for the commercially
| licensed software under version 2, or (at your option) any later
| version, of the GNU General Public License (the "GPL") must enter
| into a commercial license agreement with Plutinosoft, LLC.
| licensing@plutinosoft.com
|  
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program; see the file LICENSE.txt. If not, write to
| the Free Software Foundation, Inc., 
| 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
| http://www.gnu.org/licenses/gpl-2.0.html
|
****************************************************************/

/*----------------------------------------------------------------------
|   includes
+---------------------------------------------------------------------*/
#include "PltUPnP.h"
#include "PltFileMediaServer.h"
#include "PltMediaItem.h"
#include "PltService.h"
#include "PltTaskManager.h"
#include "PltHttpServer.h"
#include "PltDidl.h"
#include "PltVersion.h"
#include "PltMimeType.h"

NPT_SET_LOCAL_LOGGER("platinum.media.server.file.delegate")

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::PLT_FileMediaServerDelegate
+---------------------------------------------------------------------*/
PLT_FileMediaServerDelegate::PLT_FileMediaServerDelegate(const char* url_root, 
                                                         const char* file_root,
                                                         bool        use_cache) :
    m_UrlRoot(url_root),
    m_FileRoot(file_root),
    m_FilterUnknownOut(false),
    m_UseCache(use_cache)
{
    /* Trim excess separators */
    m_FileRoot.TrimRight("/\\");
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::~PLT_FileMediaServerDelegate
+---------------------------------------------------------------------*/
PLT_FileMediaServerDelegate::~PLT_FileMediaServerDelegate()
{
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::ProcessFileRequest
+---------------------------------------------------------------------*/
NPT_Result 
PLT_FileMediaServerDelegate::ProcessFileRequest(NPT_HttpRequest&              request, 
                                                const NPT_HttpRequestContext& context,
                                                NPT_HttpResponse&             response)
{
    NPT_HttpUrlQuery query(request.GetUrl().GetQuery());
    
    PLT_LOG_HTTP_REQUEST(NPT_LOG_LEVEL_FINE, "PLT_FileMediaServerDelegate::ProcessFileRequest:", &request);
    
    if (request.GetMethod().Compare("GET") && request.GetMethod().Compare("HEAD")) {
        response.SetStatus(500, "Internal Server Error");
        return NPT_SUCCESS;
    }
    
    /* Extract file path from url */
    NPT_String file_path;
    NPT_CHECK_LABEL_WARNING(ExtractResourcePath(request.GetUrl(), file_path), failure);
    
    /* Serve file */
    NPT_CHECK_WARNING(ServeFile(request, context, response, NPT_FilePath::Create(m_FileRoot, file_path)));
    return NPT_SUCCESS;
    
failure:
    response.SetStatus(404, "File Not Found");
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::ServeFile
+---------------------------------------------------------------------*/
NPT_Result 
PLT_FileMediaServerDelegate::ServeFile(const NPT_HttpRequest&        request, 
                                       const NPT_HttpRequestContext& context,
                                       NPT_HttpResponse&             response,
                                       const NPT_String&             file_path)
{
    NPT_CHECK_WARNING(PLT_HttpServer::ServeFile(request, context, response, file_path));
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::OnBrowseMetadata
+---------------------------------------------------------------------*/
NPT_Result
PLT_FileMediaServerDelegate::OnBrowseMetadata(PLT_ActionReference&          action, 
                                              const char*                   object_id, 
                                              const char*                   filter,
                                              NPT_UInt32                    starting_index,
                                              NPT_UInt32                    requested_count,
                                              const char*                   sort_criteria,
                                              const PLT_HttpRequestContext& context)
{
    NPT_COMPILER_UNUSED(sort_criteria);
    NPT_COMPILER_UNUSED(requested_count);
    NPT_COMPILER_UNUSED(starting_index);
    
    NPT_String didl;
    PLT_MediaObjectReference item;
    
    /* locate the file from the object ID */
    NPT_String filepath;
    if (NPT_FAILED(GetFilePath(object_id, filepath))) {
        /* error */
        NPT_LOG_WARNING("PLT_FileMediaServerDelegate::OnBrowse - ObjectID not found.");
        action->SetError(701, "No Such Object.");
        return NPT_FAILURE;
    }
    
    /* build the object didl */
    item = BuildFromFilePath(filepath, context, true, false, (NPT_String(filter).Find("ALLIP")!=-1));
    if (item.IsNull()) return NPT_FAILURE;
    NPT_String tmp;    
    NPT_CHECK_SEVERE(PLT_Didl::ToDidl(*item.AsPointer(), filter, tmp));
    
    /* add didl header and footer */
    didl = didl_header + tmp + didl_footer;
    
    NPT_CHECK_SEVERE(action->SetArgumentValue("Result", didl));
    NPT_CHECK_SEVERE(action->SetArgumentValue("NumberReturned", "1"));
    NPT_CHECK_SEVERE(action->SetArgumentValue("TotalMatches", "1"));
    
    /* update ID may be wrong here, it should be the one of the container?
       TODO: We need to keep track of the overall updateID of the CDS */
    NPT_CHECK_SEVERE(action->SetArgumentValue("UpdateId", "1"));
    
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::OnBrowseDirectChildren
+---------------------------------------------------------------------*/
NPT_Result
PLT_FileMediaServerDelegate::OnBrowseDirectChildren(PLT_ActionReference&          action, 
                                                    const char*                   object_id, 
                                                    const char*                   filter,
                                                    NPT_UInt32                    starting_index,
                                                    NPT_UInt32                    requested_count,
                                                    const char*                   sort_criteria,
                                                    const PLT_HttpRequestContext& context)
{
    NPT_COMPILER_UNUSED(sort_criteria);
    
    /* locate the file from the object ID */
    NPT_String dir;
    NPT_FileInfo info;
    if (NPT_FAILED(GetFilePath(object_id, dir)) || NPT_FAILED(NPT_File::GetInfo(dir, &info)) || info.m_Type != NPT_FileInfo::FILE_TYPE_DIRECTORY) {
        /* error */
        NPT_LOG_WARNING_1("ObjectID \'%s\' not found or not allowed", object_id);
        action->SetError(701, "No such Object");
        NPT_CHECK_WARNING(NPT_FAILURE);
    }
    
    /* get uuid from device via action reference */
    NPT_String uuid = action->GetActionDesc().GetService()->GetDevice()->GetUUID();
    
    /* Try to get list from cache if allowed */
    NPT_Result res;
    NPT_Reference<NPT_List<NPT_String> > entries;
    NPT_TimeStamp cached_entries_time;
    if (!m_UseCache || NPT_FAILED(m_DirCache.Get(uuid, dir, entries, &cached_entries_time)) || cached_entries_time < info.m_ModificationTime) {
        /* if not found in cache or if current dir has newer modified time fetch fresh new list from source */
        entries = new NPT_List<NPT_String>();
        res = NPT_File::ListDir(dir, *entries);
        if (NPT_FAILED(res)) {
            NPT_LOG_WARNING_1("PLT_FileMediaServerDelegate::OnBrowseDirectChildren - failed to open dir %s", (const char*) dir);
            NPT_CHECK_WARNING(res);
        }
        
        /* sort results according to modification date */
        res = entries->Sort(NPT_FileDateComparator(dir));
        if (NPT_FAILED(res)) {
            NPT_LOG_WARNING_1("PLT_FileMediaServerDelegate::OnBrowseDirectChildren - failed to open sort dir %s", (const char*) dir);
            NPT_CHECK_WARNING(res);
        }
        
        /* add new list to cache */
        if (m_UseCache) {
            m_DirCache.Put(uuid, dir, entries, &info.m_ModificationTime);
        }
    }
    
    unsigned long cur_index = 0;
    unsigned long num_returned = 0;
    unsigned long total_matches = 0;
    NPT_String didl = didl_header;
    bool allip = (NPT_String(filter).Find("ALLIP") != -1);
    
    PLT_MediaObjectReference item;
    for (NPT_List<NPT_String>::Iterator it = entries->GetFirstItem();
         it;
         ++it) {
        NPT_String filepath = NPT_FilePath::Create(dir, *it);
        
        /* verify we want to process this file first */
        if (!ProcessFile(filepath, filter)) continue;
        
        /* build item object from file path */
        item = BuildFromFilePath(filepath, 
                                 context,
                                 true,
                                 false,
                                 allip);
        
        /* generate didl if within range requested */
        if (!item.IsNull()) {
            if ((cur_index >= starting_index) && 
                ((num_returned < requested_count) || (requested_count == 0))) {
                NPT_String tmp;
                NPT_CHECK_SEVERE(PLT_Didl::ToDidl(*item.AsPointer(), filter, tmp));
                
                didl += tmp;
                ++num_returned;
            }
            ++cur_index;
            ++total_matches;        
        }
    };
    
    didl += didl_footer;
    
    NPT_LOG_FINE_6("BrowseDirectChildren from %s returning %d-%lu/%lu objects (%lu out of %d requested)",
                   (const char*)context.GetLocalAddress().GetIpAddress().ToString(),
                   starting_index, starting_index+num_returned, total_matches, num_returned, requested_count);
    
    NPT_CHECK_SEVERE(action->SetArgumentValue("Result", didl));
    NPT_CHECK_SEVERE(action->SetArgumentValue("NumberReturned", NPT_String::FromInteger(num_returned)));
    NPT_CHECK_SEVERE(action->SetArgumentValue("TotalMatches", NPT_String::FromInteger(total_matches))); // 0 means we don't know how many we have but most browsers don't like that!!
    NPT_CHECK_SEVERE(action->SetArgumentValue("UpdateId", "1"));
    
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::OnSearchContainer
+---------------------------------------------------------------------*/
NPT_Result
PLT_FileMediaServerDelegate::OnSearchContainer(PLT_ActionReference&          action, 
                                               const char*                   object_id, 
                                               const char*                   search_criteria,
                                               const char*                   /* filter */,
                                               NPT_UInt32                    /* starting_index */,
                                               NPT_UInt32                    /* requested_count */,
                                               const char*                   /* sort_criteria */,
                                               const PLT_HttpRequestContext& /* context */)
{
    /* parse search criteria */
    
    /* TODO: HACK TO PASS DLNA */
    if (search_criteria && NPT_StringsEqual(search_criteria, "Unknownfieldname")) {
        /* error */
        NPT_LOG_WARNING_1("Unsupported or invalid search criteria %s", search_criteria);
        action->SetError(708, "Unsupported or invalid search criteria");
        return NPT_FAILURE;
    }
    
    /* locate the file from the object ID */
    NPT_String dir;
    if (NPT_FAILED(GetFilePath(object_id, dir))) {
        /* error */
        NPT_LOG_WARNING("ObjectID not found.");
        action->SetError(710, "No Such Container.");
        return NPT_FAILURE;
    }
    
    /* retrieve the item type */
    NPT_FileInfo info;
    NPT_Result res = NPT_File::GetInfo(dir, &info);
    if (NPT_FAILED(res) || (info.m_Type != NPT_FileInfo::FILE_TYPE_DIRECTORY)) {
        /* error */
        NPT_LOG_WARNING("No such container");
        action->SetError(710, "No such container");
        return NPT_FAILURE;
    }
    
    return NPT_ERROR_NOT_IMPLEMENTED;
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::GetFilePath
+---------------------------------------------------------------------*/
NPT_Result
PLT_FileMediaServerDelegate::GetFilePath(const char* object_id, 
                                         NPT_String& filepath) 
{
    if (!object_id) return NPT_ERROR_INVALID_PARAMETERS;
    
    filepath = m_FileRoot;
    
    /* object id is formatted as 0/<filepath> */
    if (NPT_StringLength(object_id) >= 1) {
        filepath += (object_id + (object_id[0]=='0'?1:0));
    }
    
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::BuildSafeResourceUri
+---------------------------------------------------------------------*/
NPT_String
PLT_FileMediaServerDelegate::BuildSafeResourceUri(const NPT_HttpUrl& base_uri, 
                                                  const char*        host, 
                                                  const char*        file_path)
{
    NPT_String result;
    NPT_HttpUrl uri = base_uri;
    
    if (host) uri.SetHost(host);
    
    NPT_String uri_path = uri.GetPath();
    if (!uri_path.EndsWith("/")) uri_path += "/";

	/* some controllers (like WMP) will call us with an already urldecoded version.
       We're intentionally prepending a known urlencoded string
       to detect it when we receive the request urlencoded or already decoded to avoid double decoding*/
    uri_path += "%/";
    uri_path += file_path;
    
    /* set path */
    uri.SetPath(uri_path);
    
    /* 360 hack: force inclusion of port in case it's 80 */
    return uri.ToStringWithDefaultPort(0);
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::ExtractResourcePath
+---------------------------------------------------------------------*/
NPT_Result
PLT_FileMediaServerDelegate::ExtractResourcePath(const NPT_HttpUrl& url, 
                                                 NPT_String&        file_path)
{
    /* Extract non decoded path, we need to autodetect urlencoding */
    NPT_String uri_path = url.GetPath();
    NPT_String url_root_encode = NPT_Uri::PercentEncode(m_UrlRoot, NPT_Uri::PathCharsToEncode);
    
    NPT_Ordinal skip = 0;
    if (uri_path.StartsWith(m_UrlRoot)) {
        skip = m_UrlRoot.GetLength();
    } else if (uri_path.StartsWith(url_root_encode)) {
        skip = url_root_encode.GetLength();
    } else {
        return NPT_FAILURE;
    }
    
    /* account for extra slash */
    skip += ((m_UrlRoot=="/")?0:1);
    file_path = uri_path.SubString(skip);
    
    /* detect if client such as WMP sent a non urlencoded url */
    if (file_path.StartsWith("%/")) {
        NPT_LOG_FINE("Received a urldecoded version of our url!");
        file_path.Erase(0, 2);
    } else {
        /* remove our prepended string we used to detect urldecoded version */
        if (file_path.StartsWith("%25/")) file_path.Erase(0, 4);

        /* ok to urldecode */
        file_path = NPT_Uri::PercentDecode(file_path);
    }
    
    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::BuildResourceUri
+---------------------------------------------------------------------*/
NPT_String
PLT_FileMediaServerDelegate::BuildResourceUri(const NPT_HttpUrl& base_uri, 
                                              const char*        host, 
                                              const char*        file_path)
{
    return BuildSafeResourceUri(base_uri, host, file_path);
}

/*----------------------------------------------------------------------
|   PLT_FileMediaServerDelegate::BuildFromFilePath
+---------------------------------------------------------------------*/
PLT_MediaObject*
PLT_FileMediaServerDelegate::BuildFromFilePath(const NPT_String&             filepath, 
                                               const PLT_HttpRequestContext& context,
                                               bool                          with_count /* = true */,
                                               bool                          keep_extension_in_title /* = false */,
                                               bool                          allip /* = false */)
{
    NPT_String            root = m_FileRoot;
    PLT_MediaItemResource resource;
    PLT_MediaObject*      object = NULL;
    
    NPT_LOG_FINEST_1("Building didl for file '%s'", (const char*)filepath);
    
    /* retrieve the entry type (directory or file) */
    NPT_FileInfo info; 
    NPT_CHECK_LABEL_FATAL(NPT_File::GetInfo(filepath, &info), failure);
    
    if (info.m_Type == NPT_FileInfo::FILE_TYPE_REGULAR) {
        object = new PLT_MediaItem();
        
        /* Set the title using the filename for now */
        object->m_Title = NPT_FilePath::BaseName(filepath, keep_extension_in_title);
        if (object->m_Title.GetLength() == 0) goto failure;
        
        /* make sure we return something with a valid mimetype */
        if (m_FilterUnknownOut && 
            NPT_StringsEqual(PLT_MimeType::GetMimeType(filepath, &context), 
                             "application/octet-stream")) {
            goto failure;
        }
        
        /* Set the protocol Info from the extension */
        resource.m_ProtocolInfo = PLT_ProtocolInfo::GetProtocolInfo(filepath, true, &context);
        if (!resource.m_ProtocolInfo.IsValid())  goto failure;
        
        /* Set the resource file size */
        resource.m_Size = info.m_Size;
        
        /* format the resource URI */
        NPT_String url = filepath.SubString(root.GetLength()+1);
        
        // get list of ip addresses
        NPT_List<NPT_IpAddress> ips;
        NPT_CHECK_LABEL_SEVERE(PLT_UPnPMessageHelper::GetIPAddresses(ips), failure);
        
        /* if we're passed an interface where we received the request from
           move the ip to the top so that it is used for the first resource */
        if (context.GetLocalAddress().GetIpAddress().ToString() != "0.0.0.0") {
            ips.Remove(context.GetLocalAddress().GetIpAddress());
            ips.Insert(ips.GetFirstItem(), context.GetLocalAddress().GetIpAddress());
        } else if (!allip) {
            NPT_LOG_WARNING("Couldn't determine local interface IP so we might return an unreachable IP");
        }
        object->m_ObjectClass.type = PLT_MediaItem::GetUPnPClass(filepath, &context);
        
        /* add as many resources as we have interfaces s*/
        NPT_HttpUrl base_uri("127.0.0.1", context.GetLocalAddress().GetPort(), NPT_HttpUrl::PercentEncode(m_UrlRoot, NPT_Uri::PathCharsToEncode));
        NPT_List<NPT_IpAddress>::Iterator ip = ips.GetFirstItem();        
        while (ip) {
            resource.m_Uri = BuildResourceUri(base_uri, ip->ToString(), url);
            object->m_Resources.Add(resource);
            ++ip;
            
            /* if we only want the one resource reachable by client */
            if (!allip) break;
        }
    } else {
        object = new PLT_MediaContainer;
        
        /* Assign a title for this container */
        if (filepath.Compare(root, true) == 0) {
            object->m_Title = "Root";
        } else {
            object->m_Title = NPT_FilePath::BaseName(filepath, keep_extension_in_title);
            if (object->m_Title.GetLength() == 0) goto failure;
        }
        
        /* Get the number of children for this container */
        NPT_LargeSize count = 0;
        if (with_count && NPT_SUCCEEDED(NPT_File::GetSize(filepath, count))) {
            ((PLT_MediaContainer*)object)->m_ChildrenCount = (NPT_Int32)count;
        }
        
        object->m_ObjectClass.type = "object.container.storageFolder";
    }
    
    /* is it the root? */
    if (filepath.Compare(root, true) == 0) {
        object->m_ParentID = "-1";
        object->m_ObjectID = "0";
    } else {
        NPT_String directory = NPT_FilePath::DirName(filepath);
        /* is the parent path the root? */
        if (directory.GetLength() == root.GetLength()) {
            object->m_ParentID = "0";
        } else {
            object->m_ParentID = "0" + filepath.SubString(root.GetLength(), directory.GetLength() - root.GetLength());
        }
        object->m_ObjectID = "0" + filepath.SubString(root.GetLength());
    }
    
    return object;
    
failure:
    delete object;
    return NULL;
}