Raised This Month: $7 Target: $400
 1% 

Check if file exists in a webserver


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 05-05-2019 , 11:59   Check if file exists in a webserver
Reply With Quote #1

The answer to this may already out there somewhere but I couldn't find anything related to this.
Is it possible to check through AMXX whether a file exists in a website/webserver?

EDIT: I found Bugsy's HTTP include, but that downloads the file, any way to check without downloading?
__________________

Last edited by edon1337; 05-05-2019 at 12:01.
edon1337 is offline
HLM
Senior Member
Join Date: Apr 2008
Location: C:\WINDOWS\System32
Old 05-05-2019 , 14:20   Re: Check if file exists in a webserver
Reply With Quote #2

Use this: https://forums.alliedmods.net/showthread.php?t=41913

Perform a GET request for your file, read the response stream to find out if it exists (200), wasnt found (404), or some issue exists (500)

Although I think some of those functions in the example may be deprecated now.. reference the funcwiki.. http://amxmodx.org/api/sockets/__functions
__________________
+|- KARMA Respectively

HLM is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-05-2019 , 14:39   Re: Check if file exists in a webserver
Reply With Quote #3

Quote:
Originally Posted by HLM View Post
Use this: https://forums.alliedmods.net/showthread.php?t=41913

Perform a GET request for your file, read the response stream to find out if it exists (200), wasnt found (404), or some issue exists (500)

Although I think some of those functions in the example may be deprecated now.. reference the funcwiki.. http://amxmodx.org/api/sockets/__functions
Would be better to do a HEAD request than a GET, to keep the response short.
__________________
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 05-05-2019 , 15:13   Re: Check if file exists in a webserver
Reply With Quote #4

Quote:
Originally Posted by HamletEagle View Post
Would be better to do a HEAD request than a GET, to keep the response short.
So just replace GET with HEAD everywhere in the tut?
__________________
edon1337 is offline
HLM
Senior Member
Join Date: Apr 2008
Location: C:\WINDOWS\System32
Old 05-05-2019 , 15:20   Re: Check if file exists in a webserver
Reply With Quote #5

Yes, use HEAD instead of GET if you just need to confirm the file exists (or doesn't)

You should also use socket_is_readable as socket_change is deprecated
__________________
+|- KARMA Respectively

HLM is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 05-05-2019 , 15:36   Re: Check if file exists in a webserver
Reply With Quote #6

What value is this?
PHP Code:
            if( equalszLineVariable"some_value" ) )
            {
                
server_print("Value is %s"line_value)
            } 
PHP Code:
#include < amxmodx >
#include < sockets >

new g_iSocketsWeb;

#define SCRIPT_NAME "/myfile.txt"
#define REMOTE_HOST "website.com"

public plugin_init( )
{
    
ConnectToWeb( );
}

public 
ConnectToWeb( )
{
    new 
iErrorszString512 ];

    
g_iSocketsWeb socket_openREMOTE_HOST80SOCKET_TCPiError );
    
    if( 
g_iSocketsWeb )
    {
        
formatszStringcharsmaxszString ),"HEAD %s HTTP/1.1^nHost: %s^n^n"SCRIPT_NAMEREMOTE_HOST );
        
WriteWebszString );
        
ReadWeb( );
    }
    
    else
    {
        switch( 
iError )
        {
        case 
1
            { 
                
server_print"Error creating socket" ); 
            }
        case 
2
            { 
                
server_print"Error resolving remote hostname" ); 
            }
        case 
3
            { 
                
server_print"Error connecting socket" ); 
            }
        }
        return 
PLUGIN_CONTINUE;
    }
}

public 
ReadWeb()
{
    new 
szLineVariable64 ], szLineValue64 ];

    if( 
socket_is_readableg_iSocketsWeb100 ) )
    {
        new 
szBuf512 ], szLines30 ][ 100 ], iCount;
        
        
socket_recvg_iSocketsWebszBufcharsmaxszBuf ) );
        
        
iCount ExplodeStringszLines50119szBuf13 );
        
        for( new 
iiCounti++ )
        {
            
parseszLines], szLineVariablecharsmaxszLineVariable ), szLineValuecharsmaxszLineValue ) );
            
            if( 
equalszLineVariable"some_value" ) )
            {
                
server_print("Value is %s"line_value)
            }
        }   

        if( 
g_iSocketsWeb != )
        {
            
set_task0.5"ReadWeb" );
        }
        
        else
        {
            
DisconnectWeb( );
        }
    }
}

public 
WriteWeb( const szText[ ] )
{
    
socket_sendg_iSocketsWebszTextcharsmaxszText ) );
}

public 
DisconnectWeb( )
{
    
server_print"Socket disconnected" );
}

stock ExplodeStringp_szOutput[][], p_nMaxp_nSizep_szInput[], p_szDelimiter ) { // Function by xeroblood
    
new nIdx 0strlen(p_szInput)
    new 
nLen = (copycp_szOutput[nIdx], p_nSizep_szInputp_szDelimiter ))
    while( (
nLen l) && (++nIdx p_nMax) )
    
nLen += (copycp_szOutput[nIdx], p_nSizep_szInput[nLen], p_szDelimiter ))
    return 
nIdx

__________________
edon1337 is offline
HLM
Senior Member
Join Date: Apr 2008
Location: C:\WINDOWS\System32
Old 05-05-2019 , 15:43   Re: Check if file exists in a webserver
Reply With Quote #7

PHP Code:
    for(new i=0;i<count;i++)
    {
        
parse(lines[i], line_variableSIZEline_valueSIZE)
        if (
equal(line_variable"some_value"))
        {
            
server_print("Value is %s"line_value)
        }
    } 
PHP Code:
        for( new iiCounti++ )
        {
            
parseszLines], szLineVariablecharsmaxszLineVariable ), szLineValuecharsmaxszLineValue ) );
            
            if( 
equalszLineVariable"HTTP/1.1" ) )
            {
                
server_print("Value is %s"szLineValue)
            }
        } 
__________________
+|- KARMA Respectively

HLM is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 05-05-2019 , 15:45   Re: Check if file exists in a webserver
Reply With Quote #8

Oh, the protocol?
Should I use TCP if I'm checking whether a .txt file exists?
__________________

Last edited by edon1337; 05-05-2019 at 15:45.
edon1337 is offline
thEsp
BANNED
Join Date: Aug 2017
Old 05-05-2019 , 16:36   Re: Check if file exists in a webserver
Reply With Quote #9

Quote:
Originally Posted by edon1337 View Post
Oh, the protocol?
Should I use TCP if I'm checking whether a .txt file exists?
No, use UDP since is faster,but less complex/reliable.
Edit: Nope, as Hamlet said. Most of HTTP use TCP, not only that. I realized that you ask for FILE existence checking, in this case a FTP connection.
So TCP seems what you need.
But try both, compare them and chose what you'll use.

Last edited by thEsp; 05-05-2019 at 17:11.
thEsp is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-05-2019 , 16:43   Re: Check if file exists in a webserver
Reply With Quote #10

You should firstly print the entire server response to understand wjat you are gettingn back(the http server) and then decide what value you are looking for.
__________________
HamletEagle is offline
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 05:24.


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