use xmlinterface in UPNP

This commit is contained in:
Jacek Jendrzej
2015-04-27 19:40:38 +02:00
parent 9041bff59c
commit 497f05b730
7 changed files with 122 additions and 118 deletions

View File

@@ -3,6 +3,8 @@ noinst_LIBRARIES = libtuxbox-upnpclient.a
AM_CPPFLAGS = -fno-rtti
AM_CPPFLAGS += \
-I$(top_builddir) \
-I$(top_srcdir) \
-I$(top_srcdir)/lib/xmltree
libtuxbox_upnpclient_a_SOURCES = UPNPSocket.cpp UPNPDevice.cpp UPNPService.cpp

View File

@@ -18,12 +18,15 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
***********************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string.h>
#include <xmltree.h>
#include <xmlinterface.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -133,7 +136,7 @@ CUPnPDevice::CUPnPDevice(std::string url)
std::string result, head, body, charset, urlbase;
std::string curl, eurl, name, mimetype, iurl, rcode;
std::string::size_type pos;
XMLTreeNode *root, *device, *service, *node, *snode, *icon;
xmlNodePtr root, device, service, node, snode, icon;
int width = 0;
int height = 0;
int depth = 0;
@@ -165,76 +168,76 @@ CUPnPDevice::CUPnPDevice(std::string url)
if (rcode != "200")
throw std::runtime_error(std::string("description url returned ") + rcode);
XMLTreeParser parser(charset.c_str());
parser.Parse(body.c_str(), body.size(), 1);
root = parser.RootNode();
xmlDocPtr parser = parseXml(body.c_str(),charset.c_str());
root = xmlDocGetRootElement(parser);
if (!root)
throw std::runtime_error(std::string("XML: no root node"));
if (strcmp(root->GetType(),"root"))
if (strcmp(xmlGetName(root),"root"))
throw std::runtime_error(std::string("XML: no root"));
for (node = root->GetChild(); node; node=node->GetNext())
for (node = root->xmlChildrenNode; node; node=node->xmlNextNode)
{
if (!strcmp(node->GetType(),"URLBase"))
if (!strcmp(xmlGetName(node),"URLBase"))
{
urlbase = std::string(node->GetData());
urlbase = std::string(xmlGetData(node));
if ((!urlbase.empty() ) && (urlbase[urlbase.length()-1] == '/'))
urlbase.erase(urlbase.length()-1);
}
}
node = root->GetChild();
node = root->xmlChildrenNode;
if (!node)
throw std::runtime_error(std::string("XML: no root child"));
while (strcmp(node->GetType(),"device"))
while (strcmp(xmlGetName(node),"device"))
{
node = node->GetNext();
node = node->xmlNextNode;
if (!node)
throw std::runtime_error(std::string("XML: no device"));
}
device = node;
for (node=device->GetChild(); node; node=node->GetNext())
for (node=device->xmlChildrenNode; node; node=node->xmlNextNode)
{
if (!strcmp(node->GetType(),"deviceType"))
devicetype = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"deviceType"))
devicetype = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"friendlyName"))
friendlyname = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"friendlyName"))
friendlyname = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"manufacturer"))
manufacturer = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"manufacturer"))
manufacturer = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"manufacturerURL"))
manufacturerurl = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"manufacturerURL"))
manufacturerurl = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"modelDescription"))
modeldescription = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"modelDescription"))
modeldescription = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"modelName"))
modelname = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"modelName"))
modelname = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"modelNumber"))
modelnumber = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"modelNumber"))
modelnumber = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"modelURL"))
modelurl = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"modelURL"))
modelurl = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"serialNumber"))
serialnumber = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"serialNumber"))
serialnumber = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"UDN"))
udn = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"UDN"))
udn = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"UPC"))
upc = std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"UPC"))
upc = std::string(xmlGetData(node)?xmlGetData(node):"");
if (!strcmp(node->GetType(),"iconList"))
if (!strcmp(xmlGetName(node),"iconList"))
{
for (icon=node->GetChild(); icon; icon=icon->GetNext())
for (icon=node->xmlChildrenNode; icon; icon=icon->xmlNextNode)
{
bool foundm = false;
bool foundw = false;
@@ -242,33 +245,33 @@ CUPnPDevice::CUPnPDevice(std::string url)
bool foundd = false;
bool foundu = false;
if (strcmp(icon->GetType(),"icon"))
if (strcmp(xmlGetName(icon),"icon"))
throw std::runtime_error(std::string("XML: no icon"));
for (snode=icon->GetChild(); snode; snode=snode->GetNext())
for (snode=icon->xmlChildrenNode; snode; snode=snode->xmlNextNode)
{
if (!strcmp(snode->GetType(),"mimetype"))
if (!strcmp(xmlGetName(snode),"mimetype"))
{
mimetype=std::string(snode->GetData()?snode->GetData():"");
mimetype=std::string(xmlGetData(snode)?xmlGetData(snode):"");
foundm = true;
}
if (!strcmp(snode->GetType(),"width"))
if (!strcmp(xmlGetName(snode),"width"))
{
width=snode->GetData()?atoi(snode->GetData()):0;
width=xmlGetData(snode)?atoi(xmlGetData(snode)):0;
foundw = true;
}
if (!strcmp(snode->GetType(),"height"))
if (!strcmp(xmlGetName(snode),"height"))
{
height=snode->GetData()?atoi(snode->GetData()):0;
height=xmlGetData(snode)?atoi(xmlGetData(snode)):0;
foundh = true;
}
if (!strcmp(snode->GetType(),"depth"))
if (!strcmp(xmlGetName(snode),"depth"))
{
depth=snode->GetData()?atoi(snode->GetData()):0;
depth=xmlGetData(snode)?atoi(xmlGetData(snode)):0;
foundd = true;
}
if (!strcmp(snode->GetType(),"url"))
if (!strcmp(xmlGetName(snode),"url"))
{
url=std::string(snode->GetData()?snode->GetData():"");
url=std::string(xmlGetData(snode)?xmlGetData(snode):"");
foundu = true;
}
}
@@ -286,28 +289,27 @@ CUPnPDevice::CUPnPDevice(std::string url)
icons.push_back(e);
}
}
if (!strcmp(node->GetType(),"serviceList"))
if (!strcmp(xmlGetName(node),"serviceList"))
{
servicefound = true;
for (service=node->GetChild(); service; service=service->GetNext())
for (service=node->xmlChildrenNode; service; service=service->xmlNextNode)
{
bool foundc = false;
bool founde = false;
bool foundn = false;
if (strcmp(service->GetType(),"service"))
if (strcmp(xmlGetName(service),"service"))
throw std::runtime_error(std::string("XML: no service"));
for (snode=service->GetChild(); snode; snode=snode->GetNext())
for (snode=service->xmlChildrenNode; snode; snode=snode->xmlNextNode)
{
if (!strcmp(snode->GetType(),"serviceType"))
if (!strcmp(xmlGetName(snode),"serviceType"))
{
name=std::string(snode->GetData()?snode->GetData():"");
name=std::string(xmlGetData(snode)?xmlGetData(snode):"");
foundn = true;
}
if (!strcmp(snode->GetType(),"eventSubURL"))
if (!strcmp(xmlGetName(snode),"eventSubURL"))
{
char *p;
p = snode->GetData();
const char *p = xmlGetData(snode);
if (!p)
eurl=urlbase + "/";
else if (p[0]=='/')
@@ -316,10 +318,9 @@ CUPnPDevice::CUPnPDevice(std::string url)
eurl=urlbase + "/" + std::string(p);
founde = true;
}
if (!strcmp(snode->GetType(),"controlURL"))
if (!strcmp(xmlGetName(snode),"controlURL"))
{
char *p;
p = snode->GetData();
const char *p = xmlGetData(snode);
if (!p)
curl=urlbase + "/";
else if (p[0]=='/')
@@ -348,6 +349,7 @@ CUPnPDevice::CUPnPDevice(std::string url)
}
if (!servicefound)
throw std::runtime_error(std::string("XML: no service list"));
xmlFreeDoc(parser);
}
CUPnPDevice::~CUPnPDevice()

View File

@@ -18,12 +18,15 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
***********************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string.h>
#include <xmltree.h>
#include <xmlinterface.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -44,6 +47,7 @@ CUPnPService::CUPnPService(CUPnPDevice* dev, std::string curl, std::string eurl,
CUPnPService::~CUPnPService()
{
}
#include <stdio.h>
std::list<UPnPAttribute> CUPnPService::SendSOAP(std::string action, std::list<UPnPAttribute> attribs)
{
@@ -52,7 +56,7 @@ std::list<UPnPAttribute> CUPnPService::SendSOAP(std::string action, std::list<UP
std::string result, head, body, charset, envelope, soapbody, soapresponse, rcode;
std::list<UPnPAttribute>::iterator i;
std::list<UPnPAttribute> results;
XMLTreeNode *root, *node, *snode;
xmlNodePtr root, node, snode;
std::string::size_type pos;
post << "<?xml version=\"1.0\"?>\r\n"
@@ -95,13 +99,12 @@ std::list<UPnPAttribute> CUPnPService::SendSOAP(std::string action, std::list<UP
if (!device->check_response(head, charset, rcode))
throw std::runtime_error(std::string("protocol error"));
XMLTreeParser parser(charset.c_str());
parser.Parse(body.c_str(), body.size(), 1);
root = parser.RootNode();
xmlDocPtr parser = parseXml(body.c_str(),charset.c_str());
root = xmlDocGetRootElement(parser);
if (!root)
throw std::runtime_error(std::string("XML: no root node"));
envelope=std::string(root->GetType());
envelope=std::string(xmlGetName(root));
pos = envelope.find(":");
if (pos !=std::string::npos)
envelope.erase(0,pos+1);
@@ -109,11 +112,11 @@ std::list<UPnPAttribute> CUPnPService::SendSOAP(std::string action, std::list<UP
if (envelope != "Envelope")
throw std::runtime_error(std::string("XML: no envelope"));
node = root->GetChild();
node = root->xmlChildrenNode;
if (!node)
throw std::runtime_error(std::string("XML: no envelope child"));
soapbody=std::string(node->GetType());
soapbody=std::string(xmlGetName(node));
pos = soapbody.find(":");
if (pos !=std::string::npos)
soapbody.erase(0,pos+1);
@@ -121,11 +124,11 @@ std::list<UPnPAttribute> CUPnPService::SendSOAP(std::string action, std::list<UP
if (soapbody != "Body")
throw std::runtime_error(std::string("XML: no soap body"));
node = node->GetChild();
node = node->xmlChildrenNode;
if (!node)
throw std::runtime_error(std::string("XML: no soap body child"));
soapresponse=std::string(node->GetType());
soapresponse=std::string(xmlGetName(node));
pos = soapresponse.find(":");
if (pos !=std::string::npos)
soapresponse.erase(0,pos+1);
@@ -135,27 +138,27 @@ std::list<UPnPAttribute> CUPnPService::SendSOAP(std::string action, std::list<UP
std::string faultstring, upnpcode, upnpdesc, errstr;
if (soapresponse != "Fault")
throw std::runtime_error(std::string("XML: http error without soap fault: ")+rcode);
for (node=node->GetChild(); node; node=node->GetNext())
for (node=node->xmlChildrenNode; node; node=node->xmlNextNode)
{
if (!strcmp(node->GetType(),"detail"))
if (!strcmp(xmlGetName(node),"detail"))
{
snode=node->GetChild();
snode=node->xmlChildrenNode;
if (snode)
for (snode=snode->GetChild(); snode; snode=snode->GetNext())
for (snode=snode->xmlChildrenNode; snode; snode=snode->xmlNextNode)
{
errstr=snode->GetType();
errstr=xmlGetName(snode);
pos = errstr.find(":");
if (pos !=std::string::npos)
errstr.erase(0,pos+1);
if (errstr=="errorCode")
upnpcode=std::string(snode->GetData()?snode->GetData():"");
upnpcode=std::string(xmlGetData(snode)?xmlGetData(snode):"");
if (errstr=="errorDescription")
upnpdesc=std::string(snode->GetData()?snode->GetData():"");
upnpdesc=std::string(xmlGetData(snode)?xmlGetData(snode):"");
}
}
if (!strcmp(node->GetType(),"faultstring"))
faultstring=std::string(node->GetData()?node->GetData():"");
if (!strcmp(xmlGetName(node),"faultstring"))
faultstring=std::string(xmlGetData(node)?xmlGetData(node):"");
}
if (!faultstring.empty())
throw std::runtime_error(faultstring + " " + upnpcode + " " + upnpdesc);
@@ -165,8 +168,9 @@ std::list<UPnPAttribute> CUPnPService::SendSOAP(std::string action, std::list<UP
if (soapresponse != action + "Response")
throw std::runtime_error(std::string("XML: no soap response"));
for (node=node->GetChild(); node; node=node->GetNext())
results.push_back(UPnPAttribute(node->GetType(), node->GetData()));
for (node=node->xmlChildrenNode; node; node=node->xmlNextNode)
results.push_back(UPnPAttribute(xmlGetName(node), xmlGetData(node)));
xmlFreeDoc(parser);
return results;
}

View File

@@ -23,7 +23,6 @@
#include <sstream>
#include <stdexcept>
#include <string.h>
#include <xmltree.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>