Device.cxx 3.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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"
23
#include "lib/expat/ExpatParser.hxx"
24 25 26 27 28 29
#include "util/Error.hxx"

#include <stdlib.h>

#include <string.h>

30 31 32 33 34
UPnPDevice::~UPnPDevice()
{
	/* this destructor exists here just so it won't get inlined */
}

35 36 37 38 39 40
/**
 * An XML parser which constructs an UPnP device object from the
 * device descriptor.
 */
class UPnPDeviceParser final : public CommonExpatParser {
	UPnPDevice &m_device;
41 42 43

	std::string *value;

44 45 46 47
	UPnPService m_tservice;

public:
	UPnPDeviceParser(UPnPDevice& device)
48 49
		:m_device(device),
		 value(nullptr) {}
50 51 52

protected:
	virtual void StartElement(const XML_Char *name, const XML_Char **) {
53 54
		value = nullptr;

55
		switch (name[0]) {
56
		case 'c':
57 58
			if (strcmp(name, "controlURL") == 0)
				value = &m_tservice.controlURL;
59 60
			break;
		case 'd':
61 62
			if (strcmp(name, "deviceType") == 0)
				value = &m_device.deviceType;
63 64
			break;
		case 'f':
65 66
			if (strcmp(name, "friendlyName") == 0)
				value = &m_device.friendlyName;
67 68
			break;
		case 'm':
69 70 71 72
			if (strcmp(name, "manufacturer") == 0)
				value = &m_device.manufacturer;
			else if (strcmp(name, "modelName") == 0)
				value = &m_device.modelName;
73 74
			break;
		case 's':
75 76
			if (strcmp(name, "serviceType") == 0)
				value = &m_tservice.serviceType;
77 78
			break;
		case 'U':
79 80 81 82
			if (strcmp(name, "UDN") == 0)
				value = &m_device.UDN;
			else if (strcmp(name, "URLBase") == 0)
				value = &m_device.URLBase;
83 84 85
			break;
		}
	}
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

	virtual void EndElement(const XML_Char *name) {
		if (value != nullptr) {
			trimstring(*value);
			value = nullptr;
		} else if (!strcmp(name, "service")) {
			m_device.services.emplace_back(std::move(m_tservice));
			m_tservice.clear();
		}
	}

	virtual void CharacterData(const XML_Char *s, int len) {
		if (value != nullptr)
			value->append(s, len);
	}
101 102
};

103 104 105
bool
UPnPDevice::Parse(const std::string &url, const char *description,
		  Error &error)
106
{
107 108 109 110 111
	{
		UPnPDeviceParser mparser(*this);
		if (!mparser.Parse(description, strlen(description),
				   true, error))
			return false;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	}

	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);
			}
		}
	}
132 133

	return true;
134
}