#include "http_client.h" #include "system.h" #include "md5.h" #include "converter.h" #include #include extern std::ofstream frostLogFile; extern bool frostLogging; namespace http_client { std::wstring client::get_server(const std::wstring& url) { if(frostLogging) { frostLogFile << "get_server(" << utils::utf16_to_ascii(url.c_str()) << ")" << std::endl; } if((url.size() > 7) && (0 == _wcsicmp(url.substr(0, 7).c_str(), L"http://"))) { size_t i = 7; while(((i+1) < url.size()) && (L'/' != url[i])) ++i; if(frostLogging) { frostLogFile << "get_server() ok" << std::endl; } return url.substr(7, i - 7); } if(frostLogging) { frostLogFile << "get_server() fail" << std::endl; } return L""; } std::wstring client::get_path(const std::wstring& url) { if(frostLogging) { frostLogFile << "get_path(" << utils::utf16_to_ascii(url.c_str()) << ")" << std::endl; } if((url.size() > 7) && (0 == _wcsicmp(url.substr(0, 7).c_str(), L"http://"))) { size_t i = 7; while(((i+1) < url.size()) && (L'/' != url[i])) ++i; while((i < url.size()) && (L'/' == url[i])) ++i; if(frostLogging) { frostLogFile << "get_path() ok" << std::endl; } return url.substr(i); } if(frostLogging) { frostLogFile << "get_path() fail" << std::endl; } return L""; } struct download_info { download_info(const std::wstring& src, const std::wstring& dest, const std::wstring& crc, unsigned __int64 size): m_src(src), m_dest(L""), m_crc(crc), m_size(size) { if(frostLogging) { frostLogFile << "download_info::download_info(" << utils::utf16_to_ascii(src.c_str()) << ", " << utils::utf16_to_ascii(dest.c_str()) << ", " << utils::utf16_to_ascii(crc.c_str()) << ", " << size << ")" << std::endl; } bool isSep = false; for(size_t i=0; i size) { oldSize = 0; } if(0 == oldSize) { utils::delete_file(dest); } if(oldSize && firstTime) { unsigned __int64 allSize = 0; while(allSize < oldSize) { unsigned delta = (oldSize < (allSize + 10000000)) ? (oldSize - allSize) : 10000000; allSize += delta; } } unsigned triesCount = 0; m_error = 0; if(actualSize) *actualSize = 0; if(!m_socket.connect()) { while (true) { std::string range; if(size) { if(oldSize == size) { break; } range += "Range: bytes="; range += utils::int2str(oldSize); range += "-"; range += utils::int2str(size); range += "\r\n"; } m_error = m_socket.send(di.get_url(), range); unsigned __int64 newSize = 0; if(!m_error) { di.save(); m_error = m_socket.recv(size - oldSize, dest, actualSize); if(!m_error) { newSize = di.get_downloaded_size(); } } if(!errors.empty()) { errors += "; "; } errors += m_socket.get_errors(); if(((newSize < size) || m_error) && (triesCount < 10)) { ++triesCount; oldSize = di.get_downloaded_size(); if(!size) { size = m_socket.get_conntent_size(); } Sleep(500); } else { utils::delete_file(di.get_info_file()); break; } } } else { m_error = m_socket.m_error; errors += m_socket.get_errors(); } m_currentFile = L""; if(frostLogging) { frostLogFile << "client::download() = " << m_error << std::endl; } return m_error; } }