Example #1
0
TEST_F(TestWebServer, CanGetJsonRpcResponse)
{
  // initialized JSON-RPC
  JSONRPC::CJSONRPC::Initialize();

  std::string result;
  CCurlFile curl;
  curl.SetMimeType("application/json");
  ASSERT_TRUE(curl.Post(GetUrl(TEST_URL_JSONRPC), "{ \"jsonrpc\": \"2.0\", \"method\": \"JSONRPC.Version\", \"id\": 1 }", result));
  ASSERT_FALSE(result.empty());

  // parse the JSON-RPC response
  CVariant resultObj = CJSONVariantParser::Parse(reinterpret_cast<const unsigned char*>(result.c_str()), result.size());
  // make sure it's an object
  ASSERT_TRUE(resultObj.isObject());

  // get the HTTP header details
  const CHttpHeader& httpHeader = curl.GetHttpHeader();

  // Content-Type must be "application/json"
  EXPECT_STREQ("application/json", httpHeader.GetMimeType().c_str());
  // Accept-Ranges must be "none"
  EXPECT_STREQ("none", httpHeader.GetValue(MHD_HTTP_HEADER_ACCEPT_RANGES).c_str());

  // Cache-Control must contain "mag-age=0" and "no-cache"
  std::string cacheControl = httpHeader.GetValue(MHD_HTTP_HEADER_CACHE_CONTROL);
  EXPECT_TRUE(cacheControl.find("max-age=0") != std::string::npos);
  EXPECT_TRUE(cacheControl.find("no-cache") != std::string::npos);

  // uninitialize JSON-RPC
  JSONRPC::CJSONRPC::Cleanup();
}
Example #2
0
/*
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>method</methodName>
<params>
<param><value><string>user</string></value></param>
<param><value><string>challenge</string></value></param>
<param><value><string>auth</string></value></param>
<param><value><string>artist</string></value></param>
<param><value><string>title</string></value></param>
</params>
</methodCall>
*/
bool CLastFmManager::CallXmlRpc(const CStdString& action, const CStdString& artist, const CStdString& title)
{
  CStdString strUserName = g_guiSettings.GetString("scrobbler.lastfmusername");
  CStdString strPassword = g_guiSettings.GetString("scrobbler.lastfmpass");
  if (strUserName.IsEmpty() || strPassword.IsEmpty())
  {
    CLog::Log(LOGERROR, "Last.fm CallXmlRpc no username or password set.");
    return false;
  }
  if (artist.IsEmpty())
  {
    CLog::Log(LOGERROR, "Last.fm CallXmlRpc no artistname provided.");
    return false;
  }
  if (title.IsEmpty())
  {
    CLog::Log(LOGERROR, "Last.fm CallXmlRpc no tracktitle provided.");
    return false;
  }

  char ti[20];
  time_t rawtime;
  time ( &rawtime );
  struct tm *now = gmtime(&rawtime);
  strftime(ti, sizeof(ti), "%Y-%m-%d %H:%M:%S", now);
  CStdString strChallenge = ti;

  CStdString strAuth(strPassword);
  strAuth.ToLower();
  strAuth.append(strChallenge);
  CreateMD5Hash(strAuth, strAuth);

  //create request xml
  CXBMCTinyXML doc;
  TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "" );
  doc.LinkEndChild( decl );

  TiXmlElement * elMethodCall = new TiXmlElement( "methodCall" );
  doc.LinkEndChild( elMethodCall );

  TiXmlElement * elMethodName = new TiXmlElement( "methodName" );
  elMethodCall->LinkEndChild( elMethodName );
  TiXmlText * txtAction = new TiXmlText( action );
  elMethodName->LinkEndChild( txtAction );

  TiXmlElement * elParams = new TiXmlElement( "params" );
  elMethodCall->LinkEndChild( elParams );

  TiXmlElement * elParam = new TiXmlElement( "param" );
  elParams->LinkEndChild( elParam );
  TiXmlElement * elValue = new TiXmlElement( "value" );
  elParam->LinkEndChild( elValue );
  TiXmlElement * elString = new TiXmlElement( "string" );
  elValue->LinkEndChild( elString );
  TiXmlText * txtParam = new TiXmlText( strUserName );
  elString->LinkEndChild( txtParam );

  elParam = new TiXmlElement( "param" );
  elParams->LinkEndChild( elParam );
  elValue = new TiXmlElement( "value" );
  elParam->LinkEndChild( elValue );
  elString = new TiXmlElement( "string" );
  elValue->LinkEndChild( elString );
  txtParam = new TiXmlText( strChallenge );
  elString->LinkEndChild( txtParam );

  elParam = new TiXmlElement( "param" );
  elParams->LinkEndChild( elParam );
  elValue = new TiXmlElement( "value" );
  elParam->LinkEndChild( elValue );
  elString = new TiXmlElement( "string" );
  elValue->LinkEndChild( elString );
  txtParam = new TiXmlText( strAuth );
  elString->LinkEndChild( txtParam );

  elParam = new TiXmlElement( "param" );
  elParams->LinkEndChild( elParam );
  elValue = new TiXmlElement( "value" );
  elParam->LinkEndChild( elValue );
  elString = new TiXmlElement( "string" );
  elValue->LinkEndChild( elString );
  txtParam = new TiXmlText( artist );
  elString->LinkEndChild( txtParam );

  elParam = new TiXmlElement( "param" );
  elParams->LinkEndChild( elParam );
  elValue = new TiXmlElement( "value" );
  elParam->LinkEndChild( elValue );
  elString = new TiXmlElement( "string" );
  elValue->LinkEndChild( elString );
  txtParam = new TiXmlText( title );
  elString->LinkEndChild( txtParam );

  CStdString strBody;
  strBody << doc;

  CCurlFile http;
  CStdString html;
  CStdString url = "http://ws.audioscrobbler.com/1.0/rw/xmlrpc.php";
  http.SetMimeType("text/xml");
  if (!http.Post(url, strBody, html))
  {
    CLog::Log(LOGERROR, "Last.fm action %s failed.", action.c_str());
    return false;
  }

  if (html.Find("fault") >= 0)
  {
    CLog::Log(LOGERROR, "Last.fm return failed response: %s", html.c_str());
    return false;
  }
  return true;
}