2013-08-23, 17:35
I was wondering if someone can help me convert this method to something more XBMC friendly. I need to drop CURL for XBMC built-in/exposed functions as CURL is causing XBMC to crash on certain platforms/versions. I'm a beginner at CPP programming so any help/guidance is greatly appreciated.
Thanks to whomever replies.
Thanks to whomever replies.
Code:
static size_t data_write(void* buf, size_t size, size_t nmemb, void* userp)
{
if(userp)
{
std::ostream& os = *static_cast<std::ostream*>(userp);
std::streamsize len = size * nmemb;
if(os.write(static_cast<char*>(buf), len))
return len;
}
return 0;
}
CURLcode cPVRClient::GetAuthenticatedURL(const CStdString &url, ostringstream &strContent, long timeout)
{
CURLcode code(CURLE_FAILED_INIT);
CURL *curl = curl_easy_init();
char *buffer;
char *errorBuffer = 0;
string strSuccess = "Login Complete";
if (curl)
{
if (CURLE_OK == (curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_URL, url.c_str()))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_HEADER, 1))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &data_write))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FILE, &strContent))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "")))
{
code = curl_easy_perform(curl);
m_strUrl = strContent.str();
size_t found = m_strUrl.find(strSuccess.c_str());
if (found == std::string::npos)
{
XBMC->Log(LOG_ERROR, "Authentication Failure.");
return CURLE_URL_MALFORMAT;
}
istringstream ss(m_strUrl);
string line;
size_t cc;
while (getline(ss, line))
{
cc = line.find("Set-Cookie:");
if (cc != std::string::npos)
{
cc = line.find("PHPSESSID=");
if (cc != std::string::npos)
{
m_strPHPSessId = line.substr(line.find("PHPSESSID="), line.length() - 1);
}
else
{
m_strToken = line.substr(line.find("token"), line.length() - 1);
}
}
}
char params[100];
char chans[256];
strContent.clear();
strContent.str("");
string strId = m_strToken.substr(0, m_strToken.find_first_of(";"));
sprintf(chans, "http://mydomain.myhost.com?%s", strId.c_str());
if (CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_COOKIE, m_strToken.c_str()))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_COOKIE, m_strPHPSessId.c_str()))
&& CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_URL, chans)))
{
code = curl_easy_perform(curl);
}
}
curl_easy_cleanup(curl);
}
return code;
}