Example #1
0
void AssetDownloader::append_data (std::string &data, BinaryFileStream &temp_file)
{

    // Search for HTTP header
    if (_total_size == 0) {
    
        std::size_t header_pos = data.find("\r\n\r\n");
        if (header_pos != std::string::npos) {
        
            // Parse the header
            std::string header = data.substr(0,header_pos);
            std::vector<std::string> header_items = MoreStrings::split(header, "\r\n");
            
            std::map<std::string,std::string> header_items_map;

            // Find the content length
            for (std::size_t i = 0; i < header_items.size(); ++i) {
                std::vector<std::string> header_item = MoreStrings::split(header_items[i],":");

                // Validate entry
                if (header_item.size() != 2)
                    continue;

                std::string param = MoreStrings::trim(header_item[0]);
                std::string value = MoreStrings::trim(header_item[1]);
                
                LOG_MESSAGE << "Header: " << param << " " << value;
                
                header_items_map[param] = value;
            }
            
            auto i = header_items_map.find("Content-Length");
            if (i != header_items_map.end())
                _total_size = MoreStrings::cast_from_string<DTsize>(i->second);
        
            // Strip off header
            data = data.substr(header_pos+4);
        }
        
    // Append data
    }
    
    if (_total_size > 0) {
        
        // Write out data to a file
        temp_file.write_raw( (DTubyte*) &(data[0]), data.size());
        
        // Update sizes
        _current_size += data.size();
        data.clear();
    }

    // Update progress
    update_status (STATUS_DOWNLOADING, _current_size, _total_size);
}
Example #2
0
void AssetDownloader::finalize_data (std::string &data, BinaryFileStream &temp_file)
{
    // Write out data to a file
    temp_file.write_raw( (DTubyte*) &(data[0]), data.size());

    // Update sizes
    _current_size += data.size();
    data.clear();

    // Update progress
    update_status (STATUS_DOWNLOADING, _current_size, _total_size);
}