.: Description
This include provides the ability to download files via the HTTP protocol. Currently, the max simultaneous downloads is limited to 10. The title of the file may sound misleading since this include only has the ability to download files but I wanted to keep the title simple.
.: Commands- HTTP_DownloadFile - Download a file from a HTTP server.
- HTTP_AbortTransfer - Abort an active file transfer.
.: UsageCommands
- HTTP_DownloadFile( const szRemoteFile[] , const szLocalFile[] )
- szRemoteFile - The file to download, ie. "http://www.hello.net/mystuff/cars/ferrari.jpg"
- szLocalFile - Local file to save what was retrieved (default directory is mod-directory) ie. "ferarri.jpg"
- Return value: The function returns a unique integer value (download id) if successful or 0 on failure\all slots are full.
- HTTP_AbortTransfer( iDownloadID , bool:bDeleteLocalFile=true )
- iDownloadID - The unique download-id for the transfer. Returned by HTTP_DownloadFile and passed in download progress forward.
- bDeleteLocalFile - Whether or not the local file is deleted after the download is aborted. Default is true.
- Return value: 1 on success, 0 on failure.
Download Progress Forward
- HTTP_Download( const szFile[] , iDownloadID , iBytesReceived , iFileSize , bool:bTransferComplete )
- szFile - File as it is being saved on the local computer (local filename).
- iDownloadID - Download ID; this is the unique id\value that is returned from the HTTP_Download function.
- iBytesReceived - Bytes that have been downloaded so far.
- iFileSize - The size of the file being downloaded.
- bTransferComplete - Bool value for whether or not the download is complete.
.: Requirements
.: Comments
I realize a similar plugin\native already exists [
HTTP Downloader] but after reviewing the source I noticed it is not only inefficient but also guaranteed not to work on larger files. Many people in the thread have reported that it does not work so this will be a replacement for that. As always, comments and suggestions are welcome.
.: Example Plugin
PHP Code:
#include <amxmodx>
#include <http>
new const Version[] = "0.4";
public plugin_init()
{
register_plugin( "HTTP Test" , Version , "bugsy" );
register_concmd( "test" , "DownloadFile" );
}
public DownloadFile()
{
HTTP_DownloadFile( "www.colorado.edu/conflict/peace/download/peace_treatment.zip" , "testzip.zip" );
}
public HTTP_Download( const szFile[] , iDownloadID , iBytesRecv , iFileSize , bool:TransferComplete )
{
if ( TransferComplete )
{
server_print( "File=[%s] DownloadID=%d BytesTransferred=%d iSize=%d" , szFile , iDownloadID , iBytesRecv , iFileSize );
server_print( "%s download complete!" , szFile );
}
else
{
server_print( "File=[%s] DownloadID=%d BytesTransferred=%d iSize=%d" , szFile , iDownloadID , iBytesRecv , iFileSize );
}
}
.: Changelog- v0.4b
- Fixed issue where when a download was completing, the file was not getting closed.
- v0.4
- Changed socket_change() timeout from default of 100000 to 0 to avoid blocking.
- Added file size to download progress forward.
- Added FTP_AbortTransfer() function to stop an active file transfer.
- Fixed bug where if an error occurred opening\creating the local file the download would still proceed. Reported by dummy82.
- v0.3
- Changed CreateMultiForward() to CreateOneForward() to prevent the forward from being called in other plugins.
- Misc: Added entity-id check in think forward for safety. Removed trim-space flag on strtok()-per Emp, this is buggy, used trim() instead. Instead of calling set_fail_state() on an error, changed to log_amx().
- v0.2
- Changed download-id from a random number to a sequential number to eliminate the chance of duplicates.
- Fixed index out of bounds error when a download was attempted when all slots were already full.
- When all downloads are complete the forward and entity are destroyed. They are re-created when\if any subsequent downloads are called.
- The server-name and file are now parsed using strtok() instead of manually parsing with copy().
- EV_FL_nextthink is only set in the download function if we are creating the entity, not if a download/entity already exists.
- v0.1