LocateUri.cxx 3.25 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * http://www.musicpd.org
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "LocateUri.hxx"
#include "client/Client.hxx"
#include "fs/AllocatedPath.hxx"
#include "ls.hxx"
25
#include "util/ASCII.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "util/UriExtract.hxx"
27 28 29 30 31

#ifdef ENABLE_DATABASE
#include "storage/StorageInterface.hxx"
#endif

32 33
#include <stdexcept>

34
static LocatedUri
35
LocateFileUri(const char *uri, const Client *client
36
#ifdef ENABLE_DATABASE
37
	      , const Storage *storage
38
#endif
39
	      )
40
{
41
	auto path = AllocatedPath::FromUTF8Throw(uri);
42 43 44 45 46 47 48 49 50 51 52

#ifdef ENABLE_DATABASE
	if (storage != nullptr) {
		const char *suffix = storage->MapToRelativeUTF8(uri);
		if (suffix != nullptr)
			/* this path was relative to the music
			   directory */
			return LocatedUri(LocatedUri::Type::RELATIVE, suffix);
	}
#endif

53 54
	if (client != nullptr)
		client->AllowFile(path);
55 56 57 58 59

	return LocatedUri(LocatedUri::Type::PATH, uri, std::move(path));
}

static LocatedUri
60
LocateAbsoluteUri(UriPluginKind kind, const char *uri
61
#ifdef ENABLE_DATABASE
62
		  , const Storage *storage
63
#endif
64
		  )
65
{
66 67 68 69 70 71 72 73 74 75 76 77 78 79
	switch (kind) {
	case UriPluginKind::INPUT:
	case UriPluginKind::STORAGE: // TODO: separate check for storage plugins
		if (!uri_supported_scheme(uri))
			throw std::runtime_error("Unsupported URI scheme");
		break;

	case UriPluginKind::PLAYLIST:
		/* for now, no validation for playlist URIs; this is
		   more complicated because there are three ways to
		   identify which plugin to use: URI scheme, filename
		   suffix and MIME type */
		break;
	}
80 81 82 83 84 85 86 87 88 89 90 91 92

#ifdef ENABLE_DATABASE
	if (storage != nullptr) {
		const char *suffix = storage->MapToRelativeUTF8(uri);
		if (suffix != nullptr)
			return LocatedUri(LocatedUri::Type::RELATIVE, suffix);
	}
#endif

	return LocatedUri(LocatedUri::Type::ABSOLUTE, uri);
}

LocatedUri
93 94
LocateUri(UriPluginKind kind,
	  const char *uri, const Client *client
95
#ifdef ENABLE_DATABASE
96
	  , const Storage *storage
97
#endif
98
	  )
99 100
{
	/* skip the obsolete "file://" prefix */
101
	const char *path_utf8 = StringAfterPrefixCaseASCII(uri, "file://");
102
	if (path_utf8 != nullptr) {
103 104
		if (!PathTraitsUTF8::IsAbsolute(path_utf8))
			throw std::runtime_error("Malformed file:// URI");
105

106
		return LocateFileUri(path_utf8, client
107
#ifdef ENABLE_DATABASE
108
				     , storage
109
#endif
110
				     );
111
	} else if (PathTraitsUTF8::IsAbsolute(uri))
112
		return LocateFileUri(uri, client
113
#ifdef ENABLE_DATABASE
114
				     , storage
115
#endif
116
				     );
117
	else if (uri_has_scheme(uri))
118
		return LocateAbsoluteUri(kind, uri
119
#ifdef ENABLE_DATABASE
120
					 , storage
121
#endif
122
					 );
123 124 125
	else
		return LocatedUri(LocatedUri::Type::RELATIVE, uri);
}