Parent: [78e8e1] (diff)

Child: [40859d] (diff)

Download this file

contentdirectory.cxx    563 lines (502 with data), 17.3 kB

  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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/* Copyright (C) 2016 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "contentdirectory.hxx"
#include <upnp/upnp.h>
#include <functional>
#include <iostream>
#include <map>
#include <utility>
#include <unordered_map>
#include <sstream>
#include "libupnpp/log.hxx"
#include "libupnpp/soaphelp.hxx"
#include "libupnpp/device/device.hxx"
#include "pathut.h"
#include "smallut.h"
#include "upmpdutils.hxx"
#include "main.hxx"
#include "cdplugins/plgwithslave.hxx"
#include "conftree.h"
using namespace std;
using namespace std::placeholders;
using namespace UPnPProvider;
class ContentDirectory::Internal {
public:
Internal (ContentDirectory *sv, MediaServer *dv)
: service(sv), msdev(dv), updateID("1") { }
~Internal() {
for (auto& it : plugins) {
delete it.second;
}
}
// Start plugins which have long init so that the user has less to
// wait on first access
void maybeStartSomePlugins(bool enabled);
CDPlugin *pluginFactory(const string& appname) {
LOGDEB("ContentDirectory::pluginFactory: for " << appname << endl);
if (host.empty()) {
UpnpDevice *dev;
if (!service || !(dev = service->getDevice())) {
LOGERR("ContentDirectory::Internal: no service or dev ??\n");
return nullptr;
}
unsigned short usport;
if (!dev->ipv4(&host, &usport)) {
LOGERR("ContentDirectory::Internal: can't get server IP\n");
return nullptr;
}
port = usport;
LOGDEB("ContentDirectory: host "<< host<< " port " << port << endl);
}
return new PlgWithSlave(appname, service);
}
CDPlugin *pluginForApp(const string& appname) {
auto it = plugins.find(appname);
if (it != plugins.end()) {
return it->second;
} else {
CDPlugin *plug = pluginFactory(appname);
if (plug) {
plugins[appname] = plug;
}
return plug;
}
}
ContentDirectory *service;
MediaServer *msdev;
unordered_map<string, CDPlugin *> plugins;
string host;
int port;
string updateID;
};
static const string
sTpContentDirectory("urn:schemas-upnp-org:service:ContentDirectory:1");
static const string
sIdContentDirectory("urn:upnp-org:serviceId:ContentDirectory");
ContentDirectory::ContentDirectory(MediaServer *dev, bool enabled)
: UpnpService(sTpContentDirectory, sIdContentDirectory, dev),
m(new Internal(this, dev))
{
dev->addActionMapping(
this, "GetSearchCapabilities",
bind(&ContentDirectory::actGetSearchCapabilities, this, _1, _2));
dev->addActionMapping(
this, "GetSortCapabilities",
bind(&ContentDirectory::actGetSortCapabilities, this, _1, _2));
dev->addActionMapping(
this, "GetSystemUpdateID",
bind(&ContentDirectory::actGetSystemUpdateID, this, _1, _2));
dev->addActionMapping(
this, "Browse",
bind(&ContentDirectory::actBrowse, this, _1, _2));
dev->addActionMapping(
this, "Search",
bind(&ContentDirectory::actSearch, this, _1, _2));
m->maybeStartSomePlugins(enabled);
}
ContentDirectory::~ContentDirectory()
{
delete m;
}
int ContentDirectory::actGetSearchCapabilities(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("ContentDirectory::actGetSearchCapabilities: " << endl);
std::string out_SearchCaps(
"upnp:class,upnp:artist,dc:creator,upnp:album,dc:title");
data.addarg("SearchCaps", out_SearchCaps);
return UPNP_E_SUCCESS;
}
int ContentDirectory::actGetSortCapabilities(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("ContentDirectory::actGetSortCapabilities: " << endl);
std::string out_SortCaps;
data.addarg("SortCaps", out_SortCaps);
return UPNP_E_SUCCESS;
}
int ContentDirectory::actGetSystemUpdateID(const SoapIncoming& sc, SoapOutgoing& data)
{
LOGDEB("ContentDirectory::actGetSystemUpdateID: " << endl);
std::string out_Id = m->updateID;
data.addarg("Id", out_Id);
return UPNP_E_SUCCESS;
}
static vector<UpSong> rootdir;
static bool makerootdir()
{
rootdir.clear();
string pathplg = path_cat(g_datadir, "cdplugins");
string reason;
set<string> entries;
if (!readdir(pathplg, reason, entries)) {
LOGERR("ContentDirectory::makerootdir: can't read " << pathplg <<
" : " << reason << endl);
return false;
}
for (const auto& entry : entries) {
if (!entry.compare("pycommon")) {
continue;
}
string userkey = entry + "user";
string autostartkey = entry + "autostart";
if (!g_config->hasNameAnywhere(userkey) &&
!g_config->hasNameAnywhere(autostartkey)) {
LOGINF("ContentDirectory: not creating entry for " << entry <<
" because neither " << userkey << " nor " << autostartkey <<
" are defined in the configuration\n");
continue;
}
// If the title parameter is not defined in the configuration,
// we compute a title (to be displayed in the root directory)
// from the plugin name.
string title;
if (!g_config->get(entry + "title", title)) {
title = stringtoupper((const string&)entry.substr(0,1)) +
entry.substr(1, entry.size()-1);
}
rootdir.push_back(UpSong::container("0$" + entry + "$", "0", title));
}
if (rootdir.empty()) {
// This should not happen, as we only start the CD if services
// are configured !
rootdir.push_back(UpSong::item("0$none$", "0", "No services found"));
return false;
} else {
return true;
}
}
bool ContentDirectory::mediaServerNeeded()
{
return makerootdir();
}
// Returns totalmatches
static size_t readroot(int offs, int cnt, vector<UpSong>& out)
{
//LOGDEB("readroot: offs " << offs << " cnt " << cnt << endl);
if (rootdir.empty()) {
makerootdir();
}
out.clear();
if (cnt <= 0)
cnt = rootdir.size();
if (offs < 0 || cnt <= 0) {
return rootdir.size();
}
for (int i = 0; i < cnt; i++) {
if (size_t(offs + i) < rootdir.size()) {
out.push_back(rootdir[offs + i]);
} else {
break;
}
}
//LOGDEB("readroot: returning " << out.size() << " entries\n");
return rootdir.size();
}
static string appForId(const string& id)
{
string app;
string::size_type dol0 = id.find_first_of("$");
if (dol0 == string::npos) {
LOGERR("ContentDirectory::appForId: bad id [" << id << "]\n");
return string();
}
string::size_type dol1 = id.find_first_of("$", dol0 + 1);
if (dol1 == string::npos) {
LOGERR("ContentDirectory::appForId: bad id [" << id << "]\n");
return string();
}
return id.substr(dol0 + 1, dol1 - dol0 -1);
}
void ContentDirectory::Internal::maybeStartSomePlugins(bool enabled)
{
// If enabled is not set, no service is locally enabled, we are
// working for ohcredentials. Explicitely start the microhttpd
// daemon in this case, as we'll need it before any plugin is
// created.
if (!enabled) {
PlgWithSlave::maybeStartMHD(this->service);
}
for (auto& entry : rootdir) {
string app = appForId(entry.id);
string sas;
if (g_config->get(app + "autostart", sas) && stringToBool(sas)) {
LOGDEB0("ContentDirectory::Internal::maybeStartSomePlugins: "
"starting " << app << endl);
CDPlugin *p = pluginForApp(app);
if (p) {
p->startInit();
}
}
}
}
// Really preposterous: bubble (and maybe others) searches in root,
// but we can't do this. So memorize the last browsed object ID and
// use this as a replacement when root search is requested. Forget
// about multiaccess, god forbid multithreading etc. Will work in most
// cases though :)
static string last_objid;
int ContentDirectory::actBrowse(const SoapIncoming& sc, SoapOutgoing& data)
{
bool ok = false;
std::string in_ObjectID;
ok = sc.get("ObjectID", &in_ObjectID);
if (!ok) {
LOGERR("ContentDirectory::actBrowse: no ObjectID in params\n");
return UPNP_E_INVALID_PARAM;
}
std::string in_BrowseFlag;
ok = sc.get("BrowseFlag", &in_BrowseFlag);
if (!ok) {
LOGERR("ContentDirectory::actBrowse: no BrowseFlag in params\n");
return UPNP_E_INVALID_PARAM;
}
std::string in_Filter;
ok = sc.get("Filter", &in_Filter);
if (!ok) {
LOGERR("ContentDirectory::actBrowse: no Filter in params\n");
return UPNP_E_INVALID_PARAM;
}
int in_StartingIndex;
ok = sc.get("StartingIndex", &in_StartingIndex);
if (!ok) {
LOGERR("ContentDirectory::actBrowse: no StartingIndex in params\n");
return UPNP_E_INVALID_PARAM;
}
int in_RequestedCount;
ok = sc.get("RequestedCount", &in_RequestedCount);
if (!ok) {
LOGERR("ContentDirectory::actBrowse: no RequestedCount in params\n");
return UPNP_E_INVALID_PARAM;
}
std::string in_SortCriteria;
ok = sc.get("SortCriteria", &in_SortCriteria);
if (!ok) {
LOGERR("ContentDirectory::actBrowse: no SortCriteria in params\n");
return UPNP_E_INVALID_PARAM;
}
LOGDEB("ContentDirectory::actBrowse: " << " ObjectID " << in_ObjectID <<
" BrowseFlag " << in_BrowseFlag << " Filter " << in_Filter <<
" StartingIndex " << in_StartingIndex <<
" RequestedCount " << in_RequestedCount <<
" SortCriteria " << in_SortCriteria << endl);
last_objid = in_ObjectID;
vector<string> sortcrits;
stringToStrings(in_SortCriteria, sortcrits);
CDPlugin::BrowseFlag bf;
if (!in_BrowseFlag.compare("BrowseMetadata")) {
bf = CDPlugin::BFMeta;
} else {
bf = CDPlugin::BFChildren;
}
std::string out_Result;
std::string out_NumberReturned;
std::string out_TotalMatches;
std::string out_UpdateID;
// Go fetch
vector<UpSong> entries;
size_t totalmatches = 0;
if (!in_ObjectID.compare("0")) {
// Root directory: we do this ourselves
totalmatches = readroot(in_StartingIndex, in_RequestedCount, entries);
} else {
// Pass off request to appropriate app, defined by 1st elt in id
string app = appForId(in_ObjectID);
CDPlugin *plg = m->pluginForApp(app);
if (plg) {
totalmatches = plg->browse(in_ObjectID, in_StartingIndex,
in_RequestedCount, entries,
sortcrits, bf);
} else {
LOGERR("ContentDirectory::Browse: unknown app: [" << app << "]\n");
return UPNP_E_INVALID_PARAM;
}
}
// Process and send out result
out_NumberReturned = ulltodecstr(entries.size());
out_TotalMatches = ulltodecstr(totalmatches);
out_UpdateID = m->updateID;
out_Result = headDIDL();
for (unsigned int i = 0; i < entries.size(); i++) {
out_Result += entries[i].didl();
}
out_Result += tailDIDL();
LOGDEB1("ContentDirectory::Browse: didl: " << out_Result << endl);
data.addarg("Result", out_Result);
LOGDEB1("ContentDirectory::actBrowse: result [" << out_Result << "]\n");
data.addarg("NumberReturned", out_NumberReturned);
data.addarg("TotalMatches", out_TotalMatches);
data.addarg("UpdateID", out_UpdateID);
return UPNP_E_SUCCESS;
}
int ContentDirectory::actSearch(const SoapIncoming& sc, SoapOutgoing& data)
{
bool ok = false;
std::string in_ContainerID;
ok = sc.get("ContainerID", &in_ContainerID);
if (!ok) {
LOGERR("ContentDirectory::actSearch: no ContainerID in params\n");
return UPNP_E_INVALID_PARAM;
}
std::string in_SearchCriteria;
ok = sc.get("SearchCriteria", &in_SearchCriteria);
if (!ok) {
LOGERR("ContentDirectory::actSearch: no SearchCriteria in params\n");
return UPNP_E_INVALID_PARAM;
}
std::string in_Filter;
ok = sc.get("Filter", &in_Filter);
if (!ok) {
LOGERR("ContentDirectory::actSearch: no Filter in params\n");
return UPNP_E_INVALID_PARAM;
}
int in_StartingIndex;
ok = sc.get("StartingIndex", &in_StartingIndex);
if (!ok) {
LOGERR("ContentDirectory::actSearch: no StartingIndex in params\n");
return UPNP_E_INVALID_PARAM;
}
int in_RequestedCount;
ok = sc.get("RequestedCount", &in_RequestedCount);
if (!ok) {
LOGERR("ContentDirectory::actSearch: no RequestedCount in params\n");
return UPNP_E_INVALID_PARAM;
}
std::string in_SortCriteria;
ok = sc.get("SortCriteria", &in_SortCriteria);
if (!ok) {
LOGERR("ContentDirectory::actSearch: no SortCriteria in params\n");
return UPNP_E_INVALID_PARAM;
}
LOGDEB("ContentDirectory::actSearch: " <<
" ContainerID " << in_ContainerID <<
" SearchCriteria " << in_SearchCriteria <<
" Filter " << in_Filter << " StartingIndex " << in_StartingIndex <<
" RequestedCount " << in_RequestedCount <<
" SortCriteria " << in_SortCriteria << endl);
vector<string> sortcrits;
stringToStrings(in_SortCriteria, sortcrits);
std::string out_Result;
std::string out_NumberReturned = "0";
std::string out_TotalMatches = "0";
std::string out_UpdateID;
// Go fetch
vector<UpSong> entries;
size_t totalmatches = 0;
if (!in_ContainerID.compare("0")) {
// Root directory: can't search in there: we don't know what
// plugin to pass the search to. Substitute last browsed. Yes
// it does break in multiuser mode, and yes it's preposterous.
LOGERR("ContentDirectory::actSearch: Can't search in root. "
"Substituting last browsed container\n");
in_ContainerID = last_objid;
}
// Pass off request to appropriate app, defined by 1st elt in id
string app = appForId(in_ContainerID);
CDPlugin *plg = m->pluginForApp(app);
if (plg) {
totalmatches = plg->search(in_ContainerID, in_StartingIndex,
in_RequestedCount, in_SearchCriteria,
entries, sortcrits);
} else {
LOGERR("ContentDirectory::Search: unknown app: [" << app << "]\n");
return UPNP_E_INVALID_PARAM;
}
// Process and send out result
out_NumberReturned = ulltodecstr(entries.size());
out_TotalMatches = ulltodecstr(totalmatches);
out_UpdateID = m->updateID;
out_Result = headDIDL();
for (unsigned int i = 0; i < entries.size(); i++) {
out_Result += entries[i].didl();
}
out_Result += tailDIDL();
data.addarg("Result", out_Result);
data.addarg("NumberReturned", out_NumberReturned);
data.addarg("TotalMatches", out_TotalMatches);
data.addarg("UpdateID", out_UpdateID);
return UPNP_E_SUCCESS;
}
static string firstpathelt(const string& path)
{
// The parameter is normally a path, but make this work with an URL too
string::size_type pos = path.find("://");
if (pos != string::npos) {
pos += 3;
pos = path.find("/", pos);
} else {
pos = 0;
}
pos = path.find_first_not_of("/", pos);
if (pos == string::npos) {
return string();
}
string::size_type epos = path.find_first_of("/", pos);
if (epos != string::npos) {
return path.substr(pos, epos -pos);
} else {
return path.substr(pos);
}
}
CDPlugin *ContentDirectory::getpluginforpath(const string& path)
{
string app = firstpathelt(path);
return m->pluginForApp(app);
}
std::string ContentDirectory::getupnpaddr(CDPlugin *)
{
return m->host;
}
int ContentDirectory::getupnpport(CDPlugin *)
{
return m->port;
}
bool ContentDirectory::setfileops(CDPlugin *plg, const std::string& path,
UPnPProvider::VirtualDir::FileOps ops)
{
VirtualDir *dir = VirtualDir::getVirtualDir();
if (dir == nullptr) {
LOGERR("ContentDirectory::setfileops: getVirtualDir() failed\n");
return false;
}
string prefix = getpathprefix(plg);
if (path.find(prefix) != 0) {
LOGERR("ContentDirectory::setfileops: dir path should begin with: " <<
prefix);
return false;
}
dir->addVDir(path, ops);
return true;
}
std::string ContentDirectory::getfname()
{
return m->msdev->getfname();
}
bool CDPluginServices::config_get(const string& nm, string& val)
{
if (nullptr == g_config) {
return false;
}
return g_config->get(nm, val);
}
int CDPluginServices::microhttpport()
{
int port = 49149;
string sport;
if (g_config->get("plgmicrohttpport", sport)) {
port = atoi(sport.c_str());
}
return port;
}