Raised This Month: $12 Target: $400
 3% 

[INC] HTTP


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-20-2011 , 20:55   [INC] HTTP
Reply With Quote #1

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

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
    • Initial release
Attached Files
File Type: inc http.inc (6.0 KB, 607 views)
File Type: sma Get Plugin or Get Source (http_test.sma - 1130 views - 802 Bytes)
__________________

Last edited by Bugsy; 10-01-2016 at 13:09. Reason: New version
Bugsy is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 09-20-2011 , 21:08   Re: [INC] HTTP
Reply With Quote #2

Nice job!

Quick question:
How often does the forward get called? Just twice? Once for download start and one for download end? Or is it constantly called until the download is finished?

EDIT: Nvrm, I see it's 0.1 seconds.
__________________
Quote:
Originally Posted by DarkGod View Post
nikhilgupta generates his plugins using sheer awesome.
If you like my work, please
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-20-2011 , 21:10   Re: [INC] HTTP
Reply With Quote #3

Constantly called from download start to download end.
__________________
Bugsy is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 09-20-2011 , 21:12   Re: [INC] HTTP
Reply With Quote #4

K. Well, good job again. Will be useful to a lot of people.
__________________
Quote:
Originally Posted by DarkGod View Post
nikhilgupta generates his plugins using sheer awesome.
If you like my work, please
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
deadman909
Veteran Member
Join Date: Oct 2008
Old 09-21-2011 , 03:19   Re: [INC] HTTP
Reply With Quote #5

How exactly does this work. Do players have to type HTTP_DownloadFile muttley.gif in console? Because I type that in console but noting happens.
__________________

deadman909 is offline
Send a message via MSN to deadman909 Send a message via Yahoo to deadman909
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 09-21-2011 , 04:22   Re: [INC] HTTP
Reply With Quote #6

That's not a client command but a function to be used in a plugin.
__________________
Arkshine is offline
Fedde
BANNED
Join Date: Sep 2011
Old 09-21-2011 , 06:06   Re: [INC] HTTP
Reply With Quote #7

:orgasm:

Thanks you Bugsy ^^

[Edit]
How can I download from two webs and put the data into 1 file?
I think I must make two diferents files,read one of the files get the data,read it,and write the data to the other file.

Sorry for my bad english.

Last edited by Fedde; 09-21-2011 at 06:19.
Fedde is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-21-2011 , 07:53   Re: [INC] HTTP
Reply With Quote #8

Quote:
Originally Posted by Fedde View Post
rgasm:

Thanks you Bugsy ^^

[Edit]
How can I download from two webs and put the data into 1 file?
I think I must make two diferents files,read one of the files get the data,read it,and write the data to the other file.

Sorry for my bad english.
This can be done but is there a reason why you want to do this other than faster downloading of the same file?
__________________
Bugsy is offline
Fedde
BANNED
Join Date: Sep 2011
Old 09-21-2011 , 08:15   Re: [INC] HTTP
Reply With Quote #9

Quote:
Originally Posted by Bugsy View Post
This can be done but is there a reason why you want to do this other than faster downloading of the same file?
Yes,I want the records from cosy-climbing and xtreme-jumps to use with a plugin about world records.
Fedde is offline
deadman909
Veteran Member
Join Date: Oct 2008
Old 09-21-2011 , 09:55   Re: [INC] HTTP
Reply With Quote #10

I am still sort of confused as to what this plugin would help one do. I put it in but I checked my files and it didnt download the image when I connected to server.
__________________

deadman909 is offline
Send a message via MSN to deadman909 Send a message via Yahoo to deadman909
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:19.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode