home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / srev13h.zip / DOHEAD.CMD < prev    next >
OS/2 REXX Batch file  |  1999-04-07  |  5KB  |  178 lines

  1. /* dohead -- head's a resource from an HTTP server                 */
  2. /* ------------------------------------------------------------------- */
  3. /* Call as: dohead [serveraddress [requeststring]]                   */
  4. /* ------------------------------------------------------------------- */
  5. /* This program requires that the RxSock.DLL be in your LIBPATH (it is */
  6. /* usually in your \TCPIP\DLL directory.  It was shipped with the      */
  7. /* August 1994 CSD for the TCP/IP base (UN64092).                      */
  8.  
  9. call load /* load functions if necessary */
  10.  
  11. say " Issue a HEAD method request to an HTTP server, and display complete response "
  12. parse arg server request .
  13. mehost=get_hostname()
  14. crlf    ='0d0a'x                        /* constants */
  15. opts="" ;upwd=""
  16.  
  17. if server="" then do 
  18.     mehost=get_hostname()
  19.     say " Please enter server address (ENTER= " mehost ": "
  20.     parse pull server
  21.     if server="" then server=mehost
  22. end  /* Do */
  23. if request="" then  do
  24.   say " Please enter url to HEAD: "
  25.   parse pull request
  26.   say " Enter a (space seperated) USERNAME PASSWORD (ENTER=None):"
  27.   parse pull upwd
  28.   if upwd<>' ' then do
  29.     upwd=space(strip(upwd))
  30.     upwd=mk_base64(translate(upwd,':',' '))
  31.   end
  32.   say " Enter optional request headers (?=examples, ENTER=no more)"
  33.   opts=""
  34.   aopt=0
  35.   do until aopt=""
  36.       call charout," : "
  37.       parse pull aopt
  38.       aopt=strip(aopt)
  39.       if aopt="" then leave
  40.       if aopt="?" then do
  41.               say " Examples: "
  42.               say "    Connection:keep-alive"
  43.               say "    Range:bytes=0-50,200-400"
  44.               say " "
  45.               iterate
  46.       end  /* Do */
  47.       opts=opts||aopt||crlf
  48.   end /* do */
  49. end
  50.  
  51.  
  52. family  ='AF_INET'
  53. httpport=80
  54.  
  55. rc=sockgethostbyname(server, "serv.0")  /* get dotaddress of server */
  56. if rc=0 then do; say 'Unable to resolve "'server'"'; exit; end
  57. dotserver=serv.0addr                    /* .. */
  58.  
  59. gosaddr.0family=family                  /* set up address */
  60. gosaddr.0port  =httpport
  61. gosaddr.0addr  =dotserver
  62.  
  63. gosock = SockSocket(family, "SOCK_STREAM", "IPPROTO_TCP")
  64.  
  65. request=strip(request,'l','/')
  66. message='HEAD /'request' HTTP/1.1'crlf'HOST:'server||crlf
  67.  
  68. /*Note: uncomment these lines, and changes the "bytes=" ranges,
  69. if you only want a segment(s) of the URL*/
  70. message=message||'Referer:do_head@'||mehost||crlf
  71. if upwd<>' ' then
  72.   message=message||'Authorization: Basic '||upwd||crlf
  73. if opts<>"" then message=message||opts
  74.  
  75. message=message||crlf
  76. say message
  77.  
  78.  
  79. got=''
  80. rc = SockConnect(gosock,"gosaddr.0")
  81. if rc<0 then do; say 'Unable to connect to "'server'"'; exit; end
  82. rc = SockSend(gosock, message)
  83. say " rc " rc
  84. /* Now wait for the response */
  85. do r=1 by 1
  86.   rc = SockRecv(gosock, "response", 1000)
  87.   got=got||response
  88. say length(got)
  89.   /* say '>'rc'>' response */
  90.   if rc<=0 then leave
  91.   end r
  92. rc = SockClose(gosock)
  93.  
  94. say 'Got' length(got) 'bytes of response:'
  95.  
  96. findit=crlf||crlf
  97. foo=pos(findit,got)
  98. t1=substr(got,1,foo)
  99. say t1
  100. say " =--- writing results to dohead.lst "
  101. t2=substr(got,foo+length(findit))
  102. tt='dohead.lst'
  103. foo=sysfiledelete(tt)
  104. eek=charout(tt,t2,1)
  105. say eek
  106.  
  107. exit
  108.  
  109. /* --- Load the function library, if necessary --- */
  110. load:
  111. if \RxFuncQuery("SockLoadFuncs") then return      /* already there */
  112. call RxFuncAdd "SockLoadFuncs","rxSock","SockLoadFuncs"
  113. call SockLoadFuncs
  114.  
  115. /* Load up advanced REXX functions */
  116. foo=rxfuncquery('sysloadfuncs')
  117. if foo=1 then do
  118.   call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  119.   call SysLoadFuncs
  120. end
  121.  
  122.  
  123. return
  124.  
  125.  
  126. /* get the hostname (aa.bb.cc) for this machine
  127.    Developed by Timur Kazimirov  */
  128.  
  129. get_hostname: 
  130. if \RxFuncQuery("SockLoadFuncs")
  131.   then
  132.     nop
  133.   else
  134.     do
  135.       call RxFuncAdd "SockLoadFuncs","rxSock","SockLoadFuncs"
  136.       call SockLoadFuncs
  137.     end
  138. dot_addr = SockGetHostId()
  139. rc = SockGetHostByAddr(dot_addr, "host.")
  140. return host.name
  141.  
  142.  
  143. /************/
  144. /* create a base64 packing of a message */
  145. mk_base64:procedure
  146.  
  147. do mm=0 to 25           /* set base 64 encoding keys */
  148.    a.mm=d2c(65+mm)
  149. end /* do */
  150. do mm=26 to 51
  151.    a.mm=d2c(97+mm-26)
  152. end /* do */
  153. do mm=52 to 61
  154.    a.mm=d2c(48+mm-52)
  155. end /* do */
  156. a.62='+'
  157. a.63='/'
  158.  
  159. parse arg mess
  160. s2=x2b(c2x(mess))
  161. ith=0
  162. do forever
  163.    ith=ith+1
  164.    a1=substr(s2,1,6,0)
  165.    ms.ith=x2d(b2x(a1))
  166.    if length(s2)<7 then leave
  167.    s2=substr(s2,7)
  168. end /* do */
  169. pint=""
  170. do kk=1 to ith
  171.     oi=ms.kk ; pint=pint||a.oi
  172. end /* do */
  173. j1=length(pint)//4
  174. if j1<>0 then pint=pint||copies('=',4-j1)
  175. return pint
  176.  
  177.  
  178.