Esempio n. 1
0
const bool SM_Net::set_port ( const std::string &new_url )
{
	if( ! isSupported () )
		return false;

	unsigned int posA = new_url.find( "//" ) + 2;
	unsigned int posB = new_url.find( "/", posA );

	if( posB == std::string::npos )
	{
		if( isHTTP() )
			sm_port = 80;
		else if( isFTP() )
			sm_port = 21;
	}
	else
	{
		std::string temp = new_url.substr( posA, posB - posA );
		posA = temp.find( ":" );

		if( posA == std::string::npos )
		{
			if( isHTTP() )
				sm_port = 80;
			else if( isFTP() )
				sm_port = 21;
		}
		else
			sm_port = atoi( ( temp.substr( posA+1, posB - posA ) ).c_str() );
	}
	SM_Util::msg_out( "Port: " + SM_Util::itoa( sm_port ) );

	return true;
}
Esempio n. 2
0
const bool SM_Net::set_speedLimit ( const int &new_speedLimit )
{
	sm_speedLimit = new_speedLimit;

	if( isHTTP() && sm_httpSocket )
		sm_httpSocket->set_speedLimit( sm_speedLimit );
	else if( isFTP() && sm_ftpSocket )
		sm_ftpSocket->set_speedLimit( sm_speedLimit );

	return true;
}
Esempio n. 3
0
const bool SM_Net::stop ( void )
{
	if( isHTTP() )
	{
		if( sm_httpSocket )
			return sm_httpSocket->set_stop();
		else
			return false;	
	}
	else if ( isFTP() )
	{
		if( sm_ftpSocket )
			return sm_ftpSocket->set_stop();
		else
			return false;
	}
	return false;
}
Esempio n. 4
0
const bool SM_Net::set_totalSize ( const std::string &head_text )
{
		/* HTTP */
	if( isHTTP() )
	{
		int posA = head_text.find( "Length:" ) + 8;	
		int posB = head_text.find( "Keep-A" ) - 3;
		set_totalSize ( atoi( ( head_text.substr( posA, posB-posA+1 ) ).c_str() ) );

		SM_Util::msg_out( "TotalSize: " + head_text.substr( posA, posB-posA+1 ) );

		return true;
	}
		/* FTP */
	else if( isFTP() )
	{
		set_totalSize( 0 );
		return true;
	}
	
	set_totalSize( 0 );
	return false;
}
Esempio n. 5
0
void uHTTP::URI::setString(const std::string &value)
{
	uriStr = value;

	// Protocol
	size_t idx = uriStr.find(PROTOCOL_DELIM);
	if (idx != std::string::npos) {
		size_t protocolStrLen = strlen(PROTOCOL_DELIM);
		// Thanks for Jay Deen (03/26/04)
		protocol = uriStr.substr(0, idx/* + protocolStrLen*/);
		idx += protocolStrLen;
	}
	else
		idx = 0;

	// User (Password)
	size_t atIdx = uriStr.find(USER_DELIM, idx);
	if (atIdx != (int)std::string::npos) {
		std::string userPassStr = uriStr.substr(idx, atIdx - idx);
		size_t colonIdx = userPassStr.find(COLON_DELIM);
		if (colonIdx != std::string::npos) {
			user = userPassStr.substr(0, colonIdx);
			password = userPassStr.substr(colonIdx + 1, userPassStr.length() - colonIdx -1);
		}
		else
			user = userPassStr;
		idx = atIdx + 1;
	}

	// Host (Port)
	size_t shashIdx = uriStr.find(SLASH_DELIM, idx);
	if (shashIdx != std::string::npos)
		host = uriStr.substr(idx, shashIdx - idx);
	else
		host = uriStr.substr(idx, uriStr.length() - idx);
	size_t colonIdx = host.rfind(COLON_DELIM);
	size_t eblacketIdx = host.rfind(EBLACET_DELIM);
	if (colonIdx != std::string::npos && ((eblacketIdx == std::string::npos) || (eblacketIdx < colonIdx))) {
		std::string hostStr = host;
		host = hostStr.substr(0, colonIdx);
		if (0 < host.length()) {
			if (host.at(0) == '[' && host.at(host.length()-1) == ']')
				host = host.substr(1, colonIdx-2);
		}
		std::string portStr = hostStr.substr(colonIdx + 1, hostStr.length() - colonIdx -1);
		port = atoi(portStr.c_str());
	}
	else {
		port = URI_KNKOWN_PORT;
        if (isHTTP())
            port = HTTP_PORT;
        else if (isHTTPS())
            port = HTTPS_PORT;
    }
    
	if (shashIdx == (int)std::string::npos)
		return;
	idx = shashIdx/* + 1*/;

	// Path (Query/Fragment)
	path = uriStr.substr(idx, uriStr.length() - idx);
	size_t sharpIdx = path.find(SHARP_DELIM);
	if (sharpIdx != std::string::npos) {
		std::string pathStr = path;
		path = pathStr.substr(0, sharpIdx);
		fragment = pathStr.substr(sharpIdx + 1, pathStr.length() - sharpIdx -1);
	}
	size_t questionIdx = path.find(QUESTION_DELIM);
	if (questionIdx != std::string::npos) {
		std::string pathStr = path;
		path = pathStr.substr(0, questionIdx);
		query = pathStr.substr(questionIdx + 1, pathStr.length() - questionIdx -1);
	}
	
}
Esempio n. 6
0
const bool SM_Net::download_file ( void )
{
	if( ! define_info( sm_download->option == OPTION_NOT_ASKED ) )
		return false;
	if( sm_download->option == OPTION_NOT_ASKED )
	{
		sm_download->size      = get_totalSize();
		sm_download->completed = get_comSize();
		sm_download->option    = get_replace();
	}
	else
	{
		set_totalSize ( sm_download->size );
		set_replace   ( sm_download->option );
	}

	if( isHTTP() )
	{
		sm_httpSocket = new SM_HTTP ();
		if( ! sm_httpSocket )
		{
			SM_Util::msg_err( "Error: could not allocate http socket." );
			delete sm_httpSocket;
			sm_httpSocket = NULL;
			return false;
		}
		if( ! sm_httpSocket->configure( sm_address, sm_port ) )
		{
			SM_Util::msg_err( "Error: could not connect to server." );
			delete sm_httpSocket;
			sm_httpSocket = NULL;
			return false;
		}
		if( ! donwload )
		{
			running = true;

			sm_httpSocket->set_speedLimit( sm_speedLimit );

			bool get_return;
			if( replace && get_comSize() == 0 )
				get_return = sm_httpSocket->get_file( sm_download, sm_fileName, sm_path, get_totalSize(), replace );
			else
				get_return = sm_httpSocket->get_file( sm_download, sm_fileName, sm_path, get_totalSize() - sm_size, replace );

			running = false;
			delete sm_httpSocket;
			sm_httpSocket = NULL;

			if( get_return )
			{
				if( isCompleted() )
					sm_signal_completed( this );
				else
					sm_signal_paused( this );
				return true;
			}
			else
			{
				SM_Util::msg_err( "Error: could not get file." );
				return false;
			}
		}
		delete sm_httpSocket;
		sm_httpSocket = NULL;
		return true;
	}
	else if ( isFTP() )
	{
		sm_ftpSocket = new SM_FTP();

		if( ! sm_ftpSocket )
		{
			SM_Util::msg_err( "Error: could not allocate ftp socket." );
			delete sm_ftpSocket;
			sm_ftpSocket = NULL;
			return false;
		}
		if( ! sm_ftpSocket->configure( sm_address, sm_port ) )
		{
			SM_Util::msg_err( "Error: could not connect to server." );
			delete sm_ftpSocket;
			sm_ftpSocket = NULL;
			return false;
		}
		if( ! sm_ftpSocket->user_conf( sm_download->user, sm_download->pass ) )
		{
			SM_Util::msg_err( "Error: could not authenticate." );
			delete sm_ftpSocket;
			sm_ftpSocket = NULL;
			return false;
		}
		if( ! sm_ftpSocket->set_type( true ) )
		{
			SM_Util::msg_err( "Error: could not set connection type." );
			delete sm_ftpSocket;
			sm_ftpSocket = NULL;
			return false;
		}
		if( ! donwload )
		{
			running = true;

			sm_ftpSocket->set_speedLimit( sm_speedLimit );

			bool get_return;
			if( replace && get_comSize() == 0 )
				get_return = sm_ftpSocket->get_file( sm_download, sm_fileName, sm_path, 0, replace );
			else
				get_return = sm_ftpSocket->get_file( sm_download, sm_fileName, sm_path, sm_size, replace );

			running = false;

			if( get_return )
			{
				delete sm_ftpSocket;
				sm_ftpSocket = NULL;

				if( isCompleted() )
					sm_signal_completed( this );
				else
					sm_signal_paused( this );
				return true;
			}
			else
			{
				SM_Util::msg_err( "Error: could not make download." );

				if( ! sm_ftpSocket->quit() )
					SM_Util::msg_err( "Error: could not quit connection." );

				delete sm_ftpSocket;
				sm_ftpSocket = NULL;

				return false;
			}
		}
		delete sm_ftpSocket;
		sm_ftpSocket = NULL;
		return true;
	}
	else
	{
		SM_Util::msg_err( "Error: network protocol not supported." );
		return false;
	}	
}
Esempio n. 7
0
const bool SM_Net::define_info ( const bool &ask )
{
	if( isHTTP() )
	{
		if( ask )
		{
			do
			{
				sm_httpSocket = new SM_HTTP ();
				if( ! sm_httpSocket )
				{
					SM_Util::msg_err( "Error: could not allocate http socket." );
					show_error( "Memory Error", "Could not allocate\nhttp socket." , false );
					delete sm_httpSocket;
					sm_httpSocket = NULL;
					return false;
				}
				if( ! sm_httpSocket->configure( sm_address, sm_port ) )
				{
					SM_Util::msg_err( "Error: could not connect to server." );
					show_error( "Socket Error", "Could not connect\nto server." , false );
					delete sm_httpSocket;
					sm_httpSocket = NULL;
					return false;
				}
				const std::string head_text = sm_httpSocket->get_head( sm_fileName );
				if( head_text.compare( "" ) == 0 )
				{
					SM_Util::msg_err( "Error: could not get http file head." );
					show_error( "Socket Error", "Could not get\nhttp file head." , false );
					delete sm_httpSocket;
					sm_httpSocket = NULL;
					return false;
				}
				if( ! check_http_head( head_text ) )
				{
					delete sm_httpSocket;
					sm_httpSocket = NULL;
					return false;
				}

				set_totalSize ( head_text );

				replace = true;
				donwload = false;
					
				delete sm_httpSocket;
				sm_httpSocket = NULL;
			} while( changeURL );
		}
	}
	else if ( isFTP() )
	{
		if( ask )
		{
			sm_ftpSocket = new SM_FTP();

			if( ! sm_ftpSocket )
			{
				SM_Util::msg_err( "Error: could not allocate ftp socket." );
				delete sm_ftpSocket;
				sm_ftpSocket = NULL;
				return false;
			}
			if( ! sm_ftpSocket->configure( sm_address, sm_port ) )
			{
				SM_Util::msg_err( "Error: could not connect to server." );
				delete sm_ftpSocket;
				sm_ftpSocket = NULL;
				return false;
			}
			if( ! sm_ftpSocket->user_conf( sm_download->user, sm_download->pass ) )
			{
				SM_Util::msg_err( "Error: could not authenticate." );
				delete sm_ftpSocket;
				sm_ftpSocket = NULL;
				return false;
			}
			if( ! sm_ftpSocket->set_type( true ) )
			{
				SM_Util::msg_err( "Error: could not set connection type." );
				delete sm_ftpSocket;
				sm_ftpSocket = NULL;
				return false;
			}

			long size = sm_ftpSocket->get_size( sm_fileName );
			if( size == -1 )
			{
				SM_Util::msg_err( "Error: could not get file size." );
				delete sm_ftpSocket;
				sm_ftpSocket = NULL;
				return false;
			}
			set_totalSize( size );

			if( ! sm_ftpSocket->quit() )
			{
				SM_Util::msg_err( "Error: could not quit connection." );
				delete sm_ftpSocket;
				sm_ftpSocket = NULL;
				return false;
			}
			replace = true;
			donwload = false;
			delete sm_ftpSocket;
			sm_ftpSocket = NULL;
		}
	}
	else
	{
		SM_Util::msg_err( "Error: invalid network protocol." );
		return false;
	}
		/* file exists? */
	if( ( access( sm_path.c_str(), R_OK ) ) != -1 )
	{
		std::ifstream *inFile = new std::ifstream( sm_path.c_str(), std::ios::binary );
		sm_size = SM_Util::fileLen( inFile );

		inFile->close();
		delete inFile;

		if( ! ask )
		{
			set_comSize( sm_size );
					return true;
		}
		if( ( access( sm_path.c_str(), W_OK ) ) == -1 )
		{
			SM_Util::msg_err( "Error: file already exist, without write access." );
			show_error( "File Error", "The file already exist.\nCould not access." , false );
			donwload = true;
			return false;
		}
			/* resume file? */
		if( ! donwload && sm_size < get_totalSize() )
		{
			int answer = show_error( "File Alert", "The file already exist.\nWould you like to resume?" , true );

			if( answer )
				SM_Util::msg_out( "resume file: true" );
			else
				SM_Util::msg_out( "resume file: false" );

			if( answer == 0 )
			{
				set_comSize( 0 );
				replace = true;
			}
			else
			{
				set_comSize(  sm_size );
				replace = false;
			}
		}

			/* replace file? */
		else if( ! donwload )
		{
			int answer = show_error( "File Alert", "The file already exist.\nWould you like to replace?" , true );

			if( answer )
				SM_Util::msg_out( "replace file: true" );
			else
				SM_Util::msg_out( "replace file: false" );
		
			if( answer == 0 )
			{
				donwload = true;
				return false;
			}
			else
				replace = true;
			set_comSize( 0 );
		}
	}
	else
	{
		set_comSize( 0 );
		replace = true;
	}
	return true;
}