IsHttpFile()

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

IsHttpFile()

Post by infratec »

Hi,

since we need to check if our images on the webserver are available I wrote a procedure which does not download them :mrgreen:

Code: Select all

Procedure.i IsHttpFile(URL$)
  
  Protected.i Result, Connection, Size, Pos, Status
  Protected Head$
  
  
  Connection = OpenNetworkConnection(GetURLPart(URL$, #PB_URL_Site), 80)
  If Connection
    
    Head$ = "HEAD /" + GetURLPart(URL$, #PB_URL_Path) + " HTTP/1.1" + #CRLF$
    Head$ + "Host: " + GetURLPart(URL$, #PB_URL_Site) + #CRLF$
    Head$ + #CRLF$
    
    ;Debug Head$
    
    SendNetworkString(Connection, Head$)
    
    TimeOutCounter = 100
    Repeat
      If NetworkClientEvent(Connection) = #PB_NetworkEvent_Data
        
        Buffer$ = Space(1024)
        Size = ReceiveNetworkData(Connection, @Buffer$, Len(Buffer$))
        If Size
          Buffer$ = PeekS(@Buffer$, Size, #PB_UTF8)
          ;Debug Buffer$
          Pos = FindString(Buffer$, " ")
          If Pos
            Status = Val(Mid(Buffer$, Pos + 1, 3))
            If Status > 199 And Status < 300
              Result = #True
            EndIf
          EndIf
        EndIf
        
        Break
      EndIf
      Delay(10)
      TimeOutCounter - 1
    Until TimeOutCounter = 0
    
    CloseNetworkConnection(Connection)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure
Example:

Code: Select all

Debug IsHttpFile("http://www.purebasic.fr/english/index.php")
Debug IsHttpFile("http://www.purebasic.fr/english/index.ppp")
Bernd
User avatar
J. Baker
Addict
Addict
Posts: 2185
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: IsHttpFile()

Post by J. Baker »

Don't forget InitNetwork(). ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: IsHttpFile()

Post by Shield »

Shortened it "a bit". :)

Code: Select all

EnableExplicit

InitNetwork()

Procedure.i IsHttpFile(url.s)
	Protected header.s
	Protected status.i
	
	header = GetHTTPHeader(url)
	status = Val(StringField(header, 2, " "))
	
	If status < 200 Or status >= 300
		ProcedureReturn #False
	EndIf
	
	ProcedureReturn #True
EndProcedure


Debug IsHttpFile("http://www.purebasic.fr/english/index.php")
Debug IsHttpFile("http://www.purebasic.fr/english/index.ppp")
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: IsHttpFile()

Post by infratec »

:oops: :oops: :oops:

What a m... :!:

I should read the complete help file every month :mrgreen:

Bernd
Post Reply