Beispiel #1
0
int Web_Get( const char *url, const char *referer, const char *name, int resume, int max_downloading_time, int timeout, int ( *_progress )(double), int noreuse )
{
	CURLcode code;
	int fsize;

	// init/reinit curl
	Web_Init();

	code = curl_easy_setopt( curl, CURLOPT_URL, url );
	if( code != CURLE_OK )
	{
		Com_Printf( "Failed to set url\n" );;
		return 0;
	}

	if( referer )
	{
		code = curl_easy_setopt( curl, CURLOPT_REFERER, referer );
		if( code != CURLE_OK )
		{
			Com_Printf( "Failed to set Referer\n" );
			return 0;
		}
	}

	// connection timeout
	code = curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT, timeout );
	if( code != CURLE_OK )
	{
		Com_Printf( "Failed to set libcurl connection timeout\n" );
		return 0;
	}

	code = curl_easy_setopt( curl, CURLOPT_TIMEOUT, max_downloading_time );
	if( code != CURLE_OK )
	{
		Com_Printf( "Failed to set libcurl global timeout\n" );
		return 0;
	}

	if( noreuse )
	{
		code = curl_easy_setopt( curl, CURLOPT_FORBID_REUSE, 1 );
		if( code != CURLE_OK )
		{
			Com_Printf( "Failed to forbid reuse\n" );
			return 0;
		}
	}

	if( resume == 1 )
	{
		// test if file exist
		fsize = FS_FOpenAbsoluteFile( name, NULL, FS_READ );
		if( fsize < 0 )
			goto new_file; // file does not exist

		code = curl_easy_setopt( curl, CURLOPT_RESUME_FROM, fsize );
		if( code != CURLE_OK )
		{
			Com_Printf( "Failed to set file resume from length\n" );
			return 0;
		}
	}
	else
	{
		// we will append to the file so if it already exist we will have twice the data
		// so delete the file if it exist
		FS_RemoveAbsoluteFile( name );
	}

new_file:

	code = curl_easy_setopt( curl, CURLOPT_WRITEDATA, name );
	if( code != CURLE_OK )
	{
		Com_Printf( "Failed to set writer data\n" );
		return 0;
	}

	Com_DPrintf( "Downloading %s from %s\n", name, url );

	// set callback
	progress = _progress;
	code = curl_easy_perform( curl );
	if( code != CURLE_OK )
	{
		Com_Printf( "Failed to download %s from %s\n", name, url );
		Com_Printf( "Error: %s\n", curl_err );

		Web_Cleanup();
		return 0;
	}

	Web_Cleanup();
	return 1;
}
Beispiel #2
0
// url needs a / at the end
int Web_Get(const char *url, const char *referer, const char *name, int resume, int timeout, int (*_progress)(double))
{
	CURLcode code;
	FILE *f;
	unsigned int fsize;

	// init/reinit curl
	Web_Init();

	code=curl_easy_setopt(curl, CURLOPT_URL, url);
	if( code != CURLE_OK )
	{
		CG_Printf("Failed to set url\n");
		return 0;
	}		

	if( referer )
	{
		code=curl_easy_setopt(curl, CURLOPT_REFERER, referer);
		if( code != CURLE_OK )
		{
			CG_Printf("Failed to set Referer\n");
			return 0;
		}
	}

	// connection timeout
	code=curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout );
	if( code != CURLE_OK )
	{
		CG_Printf("Failed to set libcurl connection timeout\n");
		return 0;
	}

	code=curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout );
	if( code != CURLE_OK )
	{
		CG_Printf("Failed to set libcurl global timeout\n");
		return 0;
	}

	if(resume==1)
	{
		// test if file exist
		if((f=fopen(name,"r"))==NULL)
		{
			// file does not exist
			goto new_file;
		}
		// the file exist
		// get the size
		fsize=fseek(f,0,SEEK_END);
		fclose(f);

		code=curl_easy_setopt(curl, CURLOPT_RESUME_FROM, fsize); 
		if( code != CURLE_OK )
		{
			CG_Printf("Failed to set file resume from length\n");
			return 0;
		}		

		// file exist all good
	}

new_file:

	code=curl_easy_setopt(curl, CURLOPT_FILE, name); 
	//code=curl_easy_setopt(curl, CURLOPT_FILE, &f); 
	if( code != CURLE_OK )
	{
		CG_Printf("Failed to set writer data\n");
		return 0;
	}		

	CG_Printf("Downloading %s from %s\n",name,url);

	// set callback
	progress=_progress;
	code=curl_easy_perform(curl);
	if( code != CURLE_OK )
	{
		CG_Printf("Failed to download %s from %s\n",name,url);
		CG_Printf("Error: %s\n",curl_err);

		Web_Cleanup();
		return 0;
	}

	Web_Cleanup();
	return 1;
}