home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Spezial
/
SPEZIAL2_97.zip
/
SPEZIAL2_97.iso
/
ANWEND
/
ONLINE
/
SREFPRC1
/
GETREMOT.SRF
< prev
next >
Wrap
Text File
|
1997-07-23
|
3KB
|
105 lines
/* --------- */
/* get a remote url, return 0 if can't get, or content/type crlf body */
/* note that redirection is automatically taken care of here, perhaps
even multiple redirections */
sref_get_remote:
parse arg theurl,verbose,UPWD
/* This requires that the RxSock.DLL be in your LIBPATH (it is */
/* usually in your \TCPIP\DLL directory. It was shipped with the */
/* August 1994 CSD for the TCP/IP base (UN64092). */
if RxFuncQuery("SockLoadFuncs") then do /* already there */
call RxFuncAdd "SockLoadFuncs","rxSock","SockLoadFuncs"
call SockLoadFuncs
end
crlf ='0d0a'x /* constants */
family ='AF_INET'
httpport=80
/* parse out server, port, and request from theurl */
if abbrev(upper(theurl),'HTTP://')=0 then do
say " improper call to get_Remote "
return 0
end
hop0: /* hop here if 301 or 302 */
theurl=delstr(theurl,1,7)
ii=pos('/',theurl)
if ii=0 then do
server=theurl
request=' '
end
else do
server=substr(theurl,1,ii-1)
request=substr(theurl,ii+1)
end
icc=pos(':',server)
if icc>0 then do
httpport=substr(server,icc+1)
server=substr(server,1,icc-1)
end
rc=sockgethostbyname(server, "serv.0") /* get dotaddress of server */
if rc=0 then do; call pmprintf_sref('Unable to resolve "'server'"'); return 0; end
dotserver=serv.0addr /* .. */
gosaddr.0family=family /* set up address */
gosaddr.0port =httpport
gosaddr.0addr =dotserver
gosock = SockSocket(family, "SOCK_STREAM", "IPPROTO_TCP")
message='GET /'request' HTTP/1.0'crlf
IF UPWD<>' ' THEN
message=message||'Authorization: Basic '||upwd||crlf
message=message||crlf
got=''
rc = SockConnect(gosock,"gosaddr.0")
if rc<0 then do; call pmprintf_sref('Unable to connect to "'server'"' rc); return 0; end
rc = SockSend(gosock, message)
/* Now wait for the response */
do r=1 by 1
rc = SockRecv(gosock, "response", 1000)
got=got||response
if rc<=0 then leave
end r
rc = SockClose(gosock)
/* 200 = gotit, 301 or 302 = moved, otherwise couldn't find file */
parse var got oof status .
status=strip(status)
select
when status=301 | status=302 then do
foo=pos('LOCATION:',upper(got))
if foo=0 then
return 0
foo2=pos(crlf,got,foo)
theurl=strip(substr(got,foo+9,foo2-(foo+9)))
call pmprintf_sref(" moved to " theurl)
signal hop0
end
when status<>200 then do
if VERBOSE>0 then call pmprintF_sref(" Unable to obtain remote URL: " status)
return 0
end
otherwise
nop
end
/* extract header and body */
findit=crlf||crlf
foo=pos(findit,got)
ahead=substr(got,1,foo)
abody=substr(got,foo+length(findit))
al=length(abody)
parse upper var ahead ee 'CONTENT-TYPE:' atype (crlf) .
atype=lower(strip(atype))
return atype||crlf||abody