[INC] HTTP

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

[INC] HTTP

Post by Archive-Poster »

.: 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.

.: Usage
Commands
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
engine
sockets

.: 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

Code: Select all

#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 );
    }
} 
Attachments
http.inc.zip
(1.94 KiB) Downloaded 3 times
http_test.sma.zip
(543 Bytes) Downloaded 4 times

Who is online

Users browsing this forum: No registered users and 1 guest