inline std::string getUserAgentString() { std::string uagent(std::string("SuperTuxKart/") + STK_VERSION); #ifdef WIN32 uagent += (std::string)" (Windows)"; #elif defined(__APPLE__) uagent += (std::string)" (Macintosh)"; #elif defined(__FreeBSD__) uagent += (std::string)" (FreeBSD)"; #elif defined(ANDROID) uagent += (std::string)" (Android)"; #elif defined(linux) uagent += (std::string)" (Linux)"; #else // Unknown system type #endif return uagent; }
/** The actual curl download happens here. */ void HTTPRequest::operation() { if (!m_curl_session) return; FILE *fout = NULL; if (m_filename.size() > 0) { fout = fopen((m_filename+".part").c_str(), "wb"); if (!fout) { Log::error("HTTPRequest", "Can't open '%s' for writing, ignored.", (m_filename+".part").c_str()); return; } curl_easy_setopt(m_curl_session, CURLOPT_WRITEDATA, fout ); curl_easy_setopt(m_curl_session, CURLOPT_WRITEFUNCTION, fwrite); } else { curl_easy_setopt(m_curl_session, CURLOPT_WRITEDATA, &m_string_buffer); curl_easy_setopt(m_curl_session, CURLOPT_WRITEFUNCTION, &HTTPRequest::writeCallback); } // All parameters added have a '&' added if (m_parameters.size() > 0) { m_parameters.erase(m_parameters.size()-1); } if (m_parameters.size() == 0) { Log::info("HTTPRequest", "Downloading %s", m_url.c_str()); } else if (Log::getLogLevel() <= Log::LL_INFO) { // Avoid printing the password or token, just replace them with *s std::string param = m_parameters; // List of strings whose values should not be printed. "" is the // end indicator. static std::string dont_print[] = { "&password="******"&token=", "¤t=", "&new1=", "&new2=", "&password_confirm=", ""}; unsigned int j = 0; while (dont_print[j].size() > 0) { // Get the string that should be replaced. std::size_t pos = param.find(dont_print[j]); if (pos != std::string::npos) { pos += dont_print[j].size(); while (pos < param.size() && param[pos] != '&') { param[pos] = '*'; pos++; } // while not end } // if string found j++; } // while dont_print[j].size()>0 Log::info("HTTPRequest", "Sending %s to %s", param.c_str(), m_url.c_str()); } // end log http request curl_easy_setopt(m_curl_session, CURLOPT_POSTFIELDS, m_parameters.c_str()); std::string uagent( std::string("SuperTuxKart/") + STK_VERSION ); #ifdef WIN32 uagent += (std::string)" (Windows)"; #elif defined(__APPLE__) uagent += (std::string)" (Macintosh)"; #elif defined(__FreeBSD__) uagent += (std::string)" (FreeBSD)"; #elif defined(linux) uagent += (std::string)" (Linux)"; #else // Unknown system type #endif curl_easy_setopt(m_curl_session, CURLOPT_USERAGENT, uagent.c_str()); m_curl_code = curl_easy_perform(m_curl_session); Request::operation(); if (fout) { fclose(fout); if (m_curl_code == CURLE_OK) { if(UserConfigParams::logAddons()) Log::info("HTTPRequest", "Download successful."); // The behaviour of rename is unspecified if the target // file should already exist - so remove it. bool ok = file_manager->removeFile(m_filename); if (!ok) { Log::error("addons", "Could not removed existing addons.xml file."); m_curl_code = CURLE_WRITE_ERROR; } int ret = rename((m_filename+".part").c_str(), m_filename.c_str() ); // In case of an error, set the status to indicate this if (ret != 0) { Log::error("addons", "Could not rename downloaded addons.xml file!"); m_curl_code = CURLE_WRITE_ERROR; } } // m_curl_code ==CURLE_OK } // if fout } // operation