bool CFileCurl::GetContent(const CURI &url, CStdString &content, CStdString useragent) { CFileCurl file; if (!useragent.IsEmpty()) file.SetUserAgent(useragent); if( file.Stat(url, NULL) == 0 ) { content = file.GetContent(); return true; } if (file.GetLastRetCode() > 400 ) { content = "text/html"; } else { content = ""; } return false; }
bool CShoutcastDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items) { CStdString strRoot = strPath; if (CUtil::HasSlashAtEnd(strRoot)) strRoot.Delete(strRoot.size() - 1); /* for old users wich doesn't have the full url */ if( strRoot.Equals("shout://www.shoutcast.com") ) strRoot = "shout://www.shoutcast.com/sbin/newxml.phtml"; if (g_directoryCache.GetDirectory(strRoot, items)) return true; CGUIDialogProgress* dlgProgress = (CGUIDialogProgress*)m_gWindowManager.GetWindow(WINDOW_DIALOG_PROGRESS); if (dlgProgress) { dlgProgress->ShowProgressBar(false); dlgProgress->SetHeading(260); dlgProgress->SetLine(0, 14003); dlgProgress->SetLine(1, ""); dlgProgress->SetLine(2, ""); dlgProgress->StartModal(); } CURL url(strRoot); CStdString protocol = url.GetProtocol(); url.SetProtocol("http"); CFileCurl http; //CURL doesn't seem to understand that data is encoded.. odd // opening as text for now //http.SetContentEncoding("deflate"); if( !http.Open(url, false) ) { CLog::Log(LOGERROR, "%s - Unable to get shoutcast dir", __FUNCTION__); if (dlgProgress) dlgProgress->Close(); return false; } /* restore protocol */ url.SetProtocol(protocol); CStdString content = http.GetContent(); if( !(content.Equals("text/html") || content.Equals("text/xml") || content.Equals("text/html;charset=utf-8") || content.Equals("text/xml;charset=utf-8") )) { CLog::Log(LOGERROR, "%s - Invalid content type %s", __FUNCTION__, content.c_str()); if (dlgProgress) dlgProgress->Close(); return false; } int size_read = 0; int size_total = (int)http.GetLength(); int data_size = 0; CStdString data; data.reserve(size_total); /* read response from server into string buffer */ char buffer[16384]; while( (size_read = http.Read(buffer, sizeof(buffer)-1)) > 0 ) { buffer[size_read] = 0; data += buffer; data_size += size_read; dlgProgress->Progress(); if (dlgProgress->IsCanceled()) { dlgProgress->Close(); return false; } } /* parse returned xml */ TiXmlDocument doc; doc.Parse(data.c_str()); TiXmlElement *root = doc.RootElement(); if(root == NULL) { CLog::Log(LOGERROR, "%s - Unable to parse xml", __FUNCTION__); CLog::Log(LOGDEBUG, "%s - Sample follows...\n%s", __FUNCTION__, data.c_str()); dlgProgress->Close(); return false; } /* clear data to keep memusage down, not needed anymore */ data.Empty(); bool result = false; if( strcmp(root->Value(), "genrelist") == 0 ) result = ParseGenres(root, items, url); else if( strcmp(root->Value(), "stationlist") == 0 ) result = ParseStations(root, items, url); else { CLog::Log(LOGERROR, "%s - Invalid root xml element for shoutcast",__FUNCTION__); CLog::Log(LOGDEBUG, "%s - Sample follows...\n%s", __FUNCTION__, data.c_str()); } CFileItemList vecCacheItems; g_directoryCache.ClearDirectory(strRoot); for( int i = 0; i <items.Size(); i++ ) { CFileItem* pItem=items[i]; if (!pItem->IsParentFolder()) vecCacheItems.Add(new CFileItem( *pItem )); } g_directoryCache.SetDirectory(strRoot, vecCacheItems); if (dlgProgress) dlgProgress->Close(); return result; }