test if file exists on http-server

Jan Schenkel janschenkel at yahoo.com
Sun Sep 2 16:12:50 EDT 2007


--- "R. Hillen" <mail at richard-hillen.de> wrote:
> Hello list,
> 
> I have to check some hundred files, if they are
> available on  http- 
> server; as they are big, is there another way than
> to try to download  
> them?
> 
> Thank you!
> Richard.
> 

Hi Richard,

That's what the 'HEAD' command is for, but since that
doesn't seem to be in libURL, here's a helper function
that I cooked up.

##
on mouseUp
  answer
qrtHTTP_FileExists("http://www.runrev.com/papasmurf.htm")
end mouseUp

function qrtHTTP_FileExists pURL
  breakpoint
  local tSocket, tPath, tHeaders, tCommand
  set the itemDelimiter to "/"
  -- extract host IP address and file path from the
URL
  put item 3 of pURL into tHost
  if tHost contains ":" then 
    put tHost into tSocket
  else 
    put tHost & ":80" into tSocket
  end if
  put "/" & item 4 to -1 of pURL into tPath
  -- build the command
  put "HEAD" && tPath && "HTTP/1.1" into tCommand
  -- build the http headers
  put "Connection: Close" & CRLF & \
      "Host:" && tHost & CRLF & \
      "User-Agent: Revolution (" & the version & ")"
into tHeaders
  -- open the socket, write he request and read the
response
  open socket tSocket
  write tCommand & CRLF & tHeaders & CRLF & CRLF to
socket tSocket
  read from socket tSocket until CRLF
  put word 2 of it into tResultCode
  close socket tSocket
  -- return our interpretation of the result code
  return (tResultCode = 200)
end qrtHTTP_FileExists
##

This function does not take into account the
possibility that the file was moved and there's a
redirect to the new location. If you don't intend on
moving the file, the above should work just fine.

Hope this helped,

Jan Schenkel.

Quartam Reports & PDF Library for Revolution
<http://www.quartam.com>

=====
"As we grow older, we grow both wiser and more foolish at the same time."  (La Rochefoucauld)


       
____________________________________________________________________________________
Got a little couch potato? 
Check out fun summer activities for kids.
http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities+for+kids&cs=bz 



More information about the use-livecode mailing list