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
end mouseUp

function qrtHTTP_FileExists pURL
  local tSocket, tPath, tHeaders, tCommand
  set the itemDelimiter to "/"
  -- extract host IP address and file path from the
  put item 3 of pURL into tHost
  if tHost contains ":" then 
    put tHost into tSocket
    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
  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

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

More information about the use-livecode mailing list