home *** CD-ROM | disk | FTP | other *** search
- /*
- http.rexx - A very simple example of an http downloader.
- usage: http <url>
-
- (Hummmm url ? :)
- if output redirected via >file, prints errors and info to stderr
- */
-
- signal on break_c
-
- l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit
-
- prg=ProgramName("NOEXT")
-
- if AddLibrary("rexxsupport.library","rxsocket.library")~=0 then
- call err "can't find" result,1
-
- if ~RMH_ReadArgs("URL/A,RANGE/K") then do
- call PrintFault(IoErr(),prg)
- exit
- end
-
- url=parm.0.value
-
- if upper(left(url,7))="HTTP://" then parse var url +7 url
- p=pos("/",url)
- if p=0 then do
- host=url
- file="/"
- end
- else do
- p=p-1
- parse var url host +p file
- end
-
- p=pos(":",url)
- if p=0 then port=80
- else do
- p=p-1
- parse var host host +p dummy +1 port
- end
-
- if ~Open("STDERR","CONSOLE:","W") then SDTERR="STDOUT"
-
- call info prg"/1.0 © alfie"
- call info "Host:" host
- call info "Port:" port
- call info "File:" file
-
- call info "resolving host addr..."
- sin.addraddr=resolve(host)
- if sin.addraddr=-1 then call err "host <"host"> not found",1
- sin.addrport=port
-
- sock=socket("INET","STREAM")
- if sock=-1 then call err "can't create socket"
-
- call info "connecting..."
- if connect(sock,"SIN")<0 then call err "can't connect"
-
- fin="D0A"x
- request="GET" space(file) "HTTP/1.0"fin
- if parm.1.flag then request=request"Range: bytes" parm1.value||fin
- request=request||fin
-
- call info "sending request..."
- if send(sock,request)<0 then call err "error sending"
-
- call info "receiving results..."
- if recvline(sock,"BUF",256)<0 then call err "error receiving"
- if buf="" then call err "empty answer",1
- parse var buf http code
- if word(code,1)~=200 then call err "error from server" code,1
-
- call info "receiving head..."
- do while buf~="D0A"x
- if recvline(sock,"BUF",256)<0 then call err "error receiving"
- end
-
- call info "receiving file..."
- res=recv(sock,"BUF",256)
- do while res>0
- call writech("STDOUT",buf)
- res=recv(sock,"BUF",256)
- end
- if res<0 then call err "error receiving"
- call info "done."
- exit
-
- err:
- parse arg msg,ntdoerr
- if ntdoerr~=1 then msg=msg "("errorstring(errno())")"
- call info(msg)
- exit
-
- info:
- parse arg msg
- call writeln(STDERR,msg)
- return
-
- break_c:
- call info "user break."
- exit
-