db/upnp: free IXML_Document *request manually

This commit is contained in:
Max Kellermann 2014-01-18 14:38:52 +01:00
parent 22dd3c8048
commit 1a4940bbda
1 changed files with 40 additions and 38 deletions

View File

@ -49,13 +49,11 @@ ContentDirectoryService::ContentDirectoryService(const UPnPDevice &device,
class DirBResFree { class DirBResFree {
public: public:
IXML_Document **rqpp, **rspp; IXML_Document **rspp;
DirBResFree(IXML_Document** _rqpp, IXML_Document **_rspp) DirBResFree(IXML_Document **_rspp)
:rqpp(_rqpp), rspp(_rspp) {} :rspp(_rspp) {}
~DirBResFree() ~DirBResFree()
{ {
if (*rqpp)
ixmlDocument_free(*rqpp);
if (*rspp) if (*rspp)
ixmlDocument_free(*rspp); ixmlDocument_free(*rspp);
} }
@ -68,31 +66,30 @@ ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
int *didreadp, int *totalp, int *didreadp, int *totalp,
Error &error) Error &error)
{ {
IXML_Document *request(0);
IXML_Document *response(0);
DirBResFree cleaner(&request, &response);
// Create request // Create request
char ofbuf[100], cntbuf[100]; char ofbuf[100], cntbuf[100];
sprintf(ofbuf, "%d", offset); sprintf(ofbuf, "%d", offset);
sprintf(cntbuf, "%d", count); sprintf(cntbuf, "%d", count);
int argcnt = 6; int argcnt = 6;
// Some devices require an empty SortCriteria, else bad params // Some devices require an empty SortCriteria, else bad params
request = UpnpMakeAction("Browse", m_serviceType.c_str(), argcnt, IXML_Document *request =
"ObjectID", objectId, UpnpMakeAction("Browse", m_serviceType.c_str(), argcnt,
"BrowseFlag", "BrowseDirectChildren", "ObjectID", objectId,
"Filter", "*", "BrowseFlag", "BrowseDirectChildren",
"SortCriteria", "", "Filter", "*",
"StartingIndex", ofbuf, "SortCriteria", "",
"RequestedCount", cntbuf, "StartingIndex", ofbuf,
nullptr, nullptr); "RequestedCount", cntbuf,
nullptr, nullptr);
if (request == nullptr) { if (request == nullptr) {
error.Set(upnp_domain, "UpnpMakeAction() failed"); error.Set(upnp_domain, "UpnpMakeAction() failed");
return false; return false;
} }
IXML_Document *response;
int code = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(), int code = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, &response); 0 /*devUDN*/, request, &response);
ixmlDocument_free(request);
if (code != UPNP_E_SUCCESS) { if (code != UPNP_E_SUCCESS) {
error.Format(upnp_domain, code, error.Format(upnp_domain, code,
"UpnpSendAction() failed: %s", "UpnpSendAction() failed: %s",
@ -100,6 +97,8 @@ ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
return false; return false;
} }
DirBResFree cleaner(&response);
int didread = -1; int didread = -1;
std::string tbuf = ixmlwrap::getFirstElementValue(response, "NumberReturned"); std::string tbuf = ixmlwrap::getFirstElementValue(response, "NumberReturned");
if (!tbuf.empty()) if (!tbuf.empty())
@ -152,34 +151,34 @@ ContentDirectoryService::search(UpnpClient_Handle hdl,
UPnPDirContent &dirbuf, UPnPDirContent &dirbuf,
Error &error) Error &error)
{ {
IXML_Document *request(0);
IXML_Document *response(0);
int offset = 0; int offset = 0;
int total = 1000;// Updated on first read. int total = 1000;// Updated on first read.
while (offset < total) { while (offset < total) {
DirBResFree cleaner(&request, &response);
char ofbuf[100]; char ofbuf[100];
sprintf(ofbuf, "%d", offset); sprintf(ofbuf, "%d", offset);
// Create request // Create request
int argcnt = 6; int argcnt = 6;
request = UpnpMakeAction("Search", m_serviceType.c_str(), argcnt,
"ContainerID", objectId, IXML_Document *request =
"SearchCriteria", ss, UpnpMakeAction("Search", m_serviceType.c_str(), argcnt,
"Filter", "*", "ContainerID", objectId,
"SortCriteria", "", "SearchCriteria", ss,
"StartingIndex", ofbuf, "Filter", "*",
"RequestedCount", "0", // Setting a value here gets twonky into fits "SortCriteria", "",
nullptr, nullptr); "StartingIndex", ofbuf,
"RequestedCount", "0", // Setting a value here gets twonky into fits
nullptr, nullptr);
if (request == 0) { if (request == 0) {
error.Set(upnp_domain, "UpnpMakeAction() failed"); error.Set(upnp_domain, "UpnpMakeAction() failed");
return false; return false;
} }
IXML_Document *response;
auto code = UpnpSendAction(hdl, m_actionURL.c_str(), auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
m_serviceType.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, &response); 0 /*devUDN*/, request, &response);
ixmlDocument_free(request);
if (code != UPNP_E_SUCCESS) { if (code != UPNP_E_SUCCESS) {
error.Format(upnp_domain, code, error.Format(upnp_domain, code,
"UpnpSendAction() failed: %s", "UpnpSendAction() failed: %s",
@ -187,6 +186,8 @@ ContentDirectoryService::search(UpnpClient_Handle hdl,
return false; return false;
} }
DirBResFree cleaner(&response);
int count = -1; int count = -1;
std::string tbuf = std::string tbuf =
ixmlwrap::getFirstElementValue(response, "NumberReturned"); ixmlwrap::getFirstElementValue(response, "NumberReturned");
@ -219,20 +220,20 @@ ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl,
std::set<std::string> &result, std::set<std::string> &result,
Error &error) Error &error)
{ {
IXML_Document *request(0); IXML_Document *request =
IXML_Document *response(0); UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(),
0,
request = UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(), nullptr, nullptr);
0,
nullptr, nullptr);
if (request == 0) { if (request == 0) {
error.Set(upnp_domain, "UpnpMakeAction() failed"); error.Set(upnp_domain, "UpnpMakeAction() failed");
return false; return false;
} }
IXML_Document *response;
auto code = UpnpSendAction(hdl, m_actionURL.c_str(), auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
m_serviceType.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, &response); 0 /*devUDN*/, request, &response);
ixmlDocument_free(request);
if (code != UPNP_E_SUCCESS) { if (code != UPNP_E_SUCCESS) {
error.Format(upnp_domain, code, error.Format(upnp_domain, code,
"UpnpSendAction() failed: %s", "UpnpSendAction() failed: %s",
@ -241,6 +242,7 @@ ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl,
} }
std::string tbuf = ixmlwrap::getFirstElementValue(response, "SearchCaps"); std::string tbuf = ixmlwrap::getFirstElementValue(response, "SearchCaps");
ixmlDocument_free(response);
result.clear(); result.clear();
if (!tbuf.compare("*")) { if (!tbuf.compare("*")) {
@ -261,8 +263,6 @@ ContentDirectoryService::getMetadata(UpnpClient_Handle hdl,
UPnPDirContent &dirbuf, UPnPDirContent &dirbuf,
Error &error) Error &error)
{ {
IXML_Document *response(0);
// Create request // Create request
int argcnt = 6; int argcnt = 6;
IXML_Document *request = IXML_Document *request =
@ -274,15 +274,16 @@ ContentDirectoryService::getMetadata(UpnpClient_Handle hdl,
"StartingIndex", "0", "StartingIndex", "0",
"RequestedCount", "1", "RequestedCount", "1",
nullptr, nullptr); nullptr, nullptr);
DirBResFree cleaner(&request, &response);
if (request == nullptr) { if (request == nullptr) {
error.Set(upnp_domain, "UpnpMakeAction() failed"); error.Set(upnp_domain, "UpnpMakeAction() failed");
return false; return false;
} }
IXML_Document *response;
auto code = UpnpSendAction(hdl, m_actionURL.c_str(), auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
m_serviceType.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, &response); 0 /*devUDN*/, request, &response);
ixmlDocument_free(request);
if (code != UPNP_E_SUCCESS) { if (code != UPNP_E_SUCCESS) {
error.Format(upnp_domain, code, error.Format(upnp_domain, code,
"UpnpSendAction() failed: %s", "UpnpSendAction() failed: %s",
@ -291,5 +292,6 @@ ContentDirectoryService::getMetadata(UpnpClient_Handle hdl,
} }
std::string tbuf = ixmlwrap::getFirstElementValue(response, "Result"); std::string tbuf = ixmlwrap::getFirstElementValue(response, "Result");
ixmlDocument_free(response);
return dirbuf.parse(tbuf, error); return dirbuf.parse(tbuf, error);
} }