aboutsummaryrefslogtreecommitdiffstats
path: root/src/db/upnp/Device.cxx
blob: b5174236baf500c502c24f7bdbe929cdf70009a5 (plain) (blame)
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
/*
 * Copyright (C) 2003-2014 The Music Player Daemon Project
 * 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 "Device.hxx"
#include "Util.hxx"
#include "Expat.hxx"
#include "Log.hxx"
#include "util/Error.hxx"

#include <stdlib.h>

#include <string.h>

/**
 * An XML parser which constructs an UPnP device object from the
 * device descriptor.
 */
class UPnPDeviceParser final : public CommonExpatParser {
	UPnPDevice &m_device;
	std::vector<std::string> m_path;
	UPnPService m_tservice;

public:
	UPnPDeviceParser(UPnPDevice& device)
		:m_device(device) {}

protected:
	virtual void StartElement(const XML_Char *name, const XML_Char **) {
		m_path.push_back(name);
	}

	virtual void EndElement(const XML_Char *name) {
		if (!strcmp(name, "service")) {
			m_device.services.push_back(m_tservice);
			m_tservice.clear();
		}

		m_path.pop_back();
	}

	virtual void CharacterData(const XML_Char *s, int len) {
		std::string str = trimstring(s, len);
		switch (m_path.back()[0]) {
		case 'c':
			if (!m_path.back().compare("controlURL"))
				m_tservice.controlURL = std::move(str);
			break;
		case 'd':
			if (!m_path.back().compare("deviceType"))
				m_device.deviceType = std::move(str);
			break;
		case 'e':
			if (!m_path.back().compare("eventSubURL"))
				m_tservice.eventSubURL = std::move(str);
			break;
		case 'f':
			if (!m_path.back().compare("friendlyName"))
				m_device.friendlyName = std::move(str);
			break;
		case 'm':
			if (!m_path.back().compare("manufacturer"))
				m_device.manufacturer = std::move(str);
			else if (!m_path.back().compare("modelName"))
				m_device.modelName = std::move(str);
			break;
		case 's':
			if (!m_path.back().compare("serviceType"))
				m_tservice.serviceType = std::move(str);
			else if (!m_path.back().compare("serviceId"))
				m_tservice.serviceId = std::move(str);
		case 'S':
			if (!m_path.back().compare("SCPDURL"))
				m_tservice.SCPDURL = std::move(str);
			break;
		case 'U':
			if (!m_path.back().compare("UDN"))
				m_device.UDN = std::move(str);
			else if (!m_path.back().compare("URLBase"))
				m_device.URLBase = std::move(str);
			break;
		}
	}
};

UPnPDevice::UPnPDevice(const std::string &url, const char *description)
	:ok(false)
{
	UPnPDeviceParser mparser(*this);
	Error error;
	if (!mparser.Parse(description, strlen(description), true,
			   error)) {
		// TODO: pass Error to caller
		LogError(error);
		return;
	}

	if (URLBase.empty()) {
		// The standard says that if the URLBase value is empty, we should use
		// the url the description was retrieved from. However this is
		// sometimes something like http://host/desc.xml, sometimes something
		// like http://host/

		if (url.size() < 8) {
			// ???
			URLBase = url;
		} else {
			auto hostslash = url.find_first_of("/", 7);
			if (hostslash == std::string::npos || hostslash == url.size()-1) {
				URLBase = url;
			} else {
				URLBase = path_getfather(url);
			}
		}
	}
	ok = true;
}