00001
00002
00003
00004
00005
00006
00007
00008
00010
00011 #include "soapclient.h"
00012
00013 #include "mythcontext.h"
00014 #include "httprequest.h"
00015
00017
00019
00020 SOAPClient::SOAPClient( const QUrl &url,
00021 const QString &sNamespace,
00022 const QString &sControlPath )
00023 {
00024 m_url = url;
00025 m_sNamespace = sNamespace;
00026 m_sControlPath = sControlPath;
00027 }
00028
00030
00032
00033 SOAPClient::~SOAPClient()
00034 {
00035 }
00036
00038
00040
00041 QDomNode SOAPClient::FindNode( const QString &sName, QDomNode &baseNode )
00042 {
00043 QStringList parts = QStringList::split( "/", sName );
00044
00045 return FindNode( parts, baseNode );
00046
00047 }
00048
00050
00052
00053 QDomNode SOAPClient::FindNode( QStringList &sParts, QDomNode &curNode )
00054 {
00055 if (sParts.empty())
00056 return curNode;
00057
00058 QString sName = sParts.front();
00059 sParts.pop_front();
00060
00061 QDomNode child = curNode.namedItem( sName );
00062
00063 if (child.isNull() )
00064 sParts.clear();
00065
00066 return FindNode( sParts, child );
00067 }
00068
00070
00072
00073 int SOAPClient::GetNodeValue( QDomNode &node, const QString &sName, int nDefault )
00074 {
00075 QString sValue = GetNodeValue( node, sName, QString::number( nDefault ) );
00076
00077 return sValue.toInt();
00078 }
00079
00081
00083
00084 bool SOAPClient::GetNodeValue( QDomNode &node, const QString &sName, bool bDefault )
00085 {
00086 QString sDefault = (bDefault) ? "true" : "false";
00087 QString sValue = GetNodeValue( node, sName, sDefault );
00088
00089 if (sValue.startsWith( "T" , false ) ||
00090 sValue.startsWith( "Y" , false ) ||
00091 sValue.startsWith( "1" , false ) )
00092 {
00093 return true;
00094 }
00095
00096 if (sValue.startsWith( "F" , false ) ||
00097 sValue.startsWith( "N" , false ) ||
00098 sValue.startsWith( "0" , false ) )
00099 {
00100 return false;
00101 }
00102
00103 return bDefault;
00104 }
00105
00107
00109
00110 QString SOAPClient::GetNodeValue( QDomNode &node, const QString &sName, const QString &sDefault )
00111 {
00112 if (node.isNull())
00113 return sDefault;
00114
00115 QString sValue = "";
00116 QDomNode valNode = FindNode( sName, node );
00117
00118 if (!valNode.isNull())
00119 {
00120
00121
00122 QDomText oText = valNode.firstChild().toText();
00123
00124 if (!oText.isNull())
00125 sValue = oText.nodeValue();
00126
00127 QUrl::decode( sValue );
00128
00129 return sValue;
00130 }
00131
00132 return sDefault;
00133 }
00134
00136
00138
00139 bool SOAPClient::SendSOAPRequest( const QString &sMethod,
00140 QStringMap &list,
00141 int &nErrCode,
00142 QString &sErrDesc,
00143 bool bInQtThread )
00144 {
00145 QUrl url( m_url );
00146
00147 url.setPath( m_sControlPath );
00148
00149
00150
00151
00152
00153 QHttpRequestHeader header;
00154
00155 header.setValue("CONTENT-TYPE", "text/xml; charset=\"utf-8\"" );
00156 header.setValue("SOAPACTION" , QString( "\"%1#GetConnectionInfo\"" )
00157 .arg( m_sNamespace ));
00158
00159
00160
00161
00162
00163 QByteArray aBuffer;
00164 QTextStream os( aBuffer, IO_WriteOnly );
00165
00166 os << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
00167 os << "<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n";
00168 os << " <s:Body>\r\n";
00169 os << " <u:" << sMethod << " xmlns:u=\"" << m_sNamespace << "\">\r\n";
00170
00171
00172
00173
00174
00175 for ( QStringMap::iterator it = list.begin();
00176 it != list.end();
00177 ++it )
00178 {
00179 os << " <" << it.key() << ">";
00180 os << HTTPRequest::Encode( it.data() );
00181 os << "</" << it.key() << ">\r\n";
00182 }
00183
00184 os << " </u:" << sMethod << ">\r\n";
00185 os << " </s:Body>\r\n";
00186 os << "</s:Envelope>\r\n";
00187
00188
00189
00190
00191
00192 QBuffer buff( aBuffer );
00193
00194 QString sXml = HttpComms::postHttp( url,
00195 &header,
00196 &buff,
00197 10000,
00198 3,
00199 0,
00200 false,
00201 NULL,
00202 bInQtThread );
00203
00204
00205
00206
00207
00208 list.clear();
00209
00210 QDomDocument doc;
00211
00212 if ( !doc.setContent( sXml, true, &sErrDesc, &nErrCode ))
00213 {
00214 VERBOSE( VB_UPNP, QString( "MythXMLClient::SendSOAPRequest( %1 ) - Invalid response from %2" )
00215 .arg( sMethod )
00216 .arg( url.toString() ));
00217 return false;
00218 }
00219
00220
00221
00222
00223
00224 QString sResponseName = sMethod + "Response";
00225 QDomNodeList oNodeList = doc.elementsByTagNameNS( m_sNamespace, sResponseName );
00226
00227 if (oNodeList.count() > 0)
00228 {
00229 QDomNode oMethod = oNodeList.item(0);
00230
00231 if (!oMethod.isNull())
00232 {
00233
00234 for ( QDomNode oNode = oMethod.firstChild(); !oNode.isNull();
00235 oNode = oNode.nextSibling() )
00236 {
00237 QDomElement e = oNode.toElement();
00238
00239 if (!e.isNull())
00240 {
00241 QString sName = e.tagName();
00242 QString sValue = "";
00243
00244 QDomText oText = oNode.firstChild().toText();
00245
00246 if (!oText.isNull())
00247 sValue = oText.nodeValue();
00248
00249 QUrl::decode( sName );
00250 QUrl::decode( sValue );
00251
00252 list.insert( sName, sValue );
00253 }
00254 }
00255 }
00256
00257 return true;
00258 }
00259
00260
00261
00262
00263
00264 nErrCode = GetNodeValue( doc, "Envelope/Body/Fault/detail/UPnPResult/errorCode" , 500 );
00265 sErrDesc = GetNodeValue( doc, "Envelope/Body/Fault/detail/UPnPResult/errorDescription", QString( "Unknown" ));
00266
00267 return false;
00268 }