metacard  

client side http

Dave Cragg
Mon, 11 Sep 2000 07:32:03 -0700

Hi

I've been playing with the sockets features in order to try and 
implement some client side http. I've been doing this because 
Metacard's built-in http support won't work with some URLs (something 
to do with the way aliasing is set up on the server, I think).

The script below is a crude first attempt at this. It simply loads 
the data from the specified URL. (I've removed some stuff that was 
specific to my situation.) So far, it seems to work, even with 
previously unreachable URLs. Ideally, I'd like to be able to emulate 
the built-in http features of Metacard, but using sockets. (I've also 
had a go at the POST method, and it seems to work, but I need to make 
a more general solution than what I've done so far.)

The scripts may be useful to anyone else wanting to do the same 
thing. More importantly, I'd appreciate any feedback from the sockets 
gurus about whether I'm going in the right direction.

Two questions foremost on my mind:

In order to show progress of the download (progress bar or whatever), 
is it necessary to do reads in short steps (as in the script below) 
or is there another way?

How to handle http proxies? Is this just a question of opening the 
socket to the proxy and carrying on as before, or is it, as I fear, 
more torturous?

Any comments appreciated.

Cheers
Dave Cragg

---------------------------------------------------------------
local lcSocket, lcPath, lcData, lcHeader

on httpLoad pURL
   ## remove initial "http://" if present
   if "http://" is char 1 to 7 of pURL then
     delete char 1 to 7 of pURL
   end if
   put  offset("/",pURL) into tOff
   if tOff = 0 then
     put pURL into lcSocket
     put "" into lcPath
   else
     put char 1 to (tOff - 1) of pURL into lcSocket
     put char tOff to -1 of pURL into lcPath
   end if

   put  numToChar(13) & numToChar(10) into CRLF

   open socket  to lcSocket
 
   put "GET" && lcPath  &&  "HTTP/1.1" & CRLF  into tString
   put "host:" & lcSocket & CRLF after tString
   put CRLF after tString  ## blank line is necessary
 
   write tString to socket lcSocket
   read from socket lcSocket until CRLF ##line 1
   if word 2 of it = 200 then
     read from socket lcSocket until  CRLF &  CRLF ##get the header
     put it into lcHeader
     get lineOffset("Content-Length", lcHeader)
     if it <> 0 then
       put  it into tLineNum
       put word 2 line tLineNum of lcHeader into tLength
       put 1024 into tStep
     end if
     put empty into lcData
     put 0 into tCount
     put tLength div tStep into tTimes
    ## read in small steps in order to show a progress bar or other indication
     repeat for tTimes
       add 1 to tCount
       read from socket  lcSocket for tStep
       put it after lcData
      ## handle progress bar updating here
     end repeat
     read from socket  lcSocket until empty ## catch last few dribbles
     put it after lcData
     close socket lcSocket
     ## do something with lcData here (cache it, return it, or whatever)
   else
     ## crude error handling
     put word 2 of it
     close socket lcSocket
   end if
end httpLoad
-------------------------------
on socketError pSocket, pErr
   ##crude error handling
   put pSocket & ":" & pErr
end socketError
------------------------------
on socketTimeout pSocket
   ##crude error handling
   put "Timed out"
   close socket pSocket
end socketTimeout
------------------------------
_____________________________________________
The LACS Centre (Business English Training Resources)
mailto:[EMAIL PROTECTED]
http://www.lacscentre.co.uk
_____________________________________________

Archives: http://www.mail-archive.com/metacard%40lists.best.com/
Info: http://www.xworlds.com/metacard/mailinglist.htm
Please send bug reports to <[EMAIL PROTECTED]>, not this list.