home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / SREFPRC1 / DOPUT.SRF < prev    next >
Text File  |  1997-06-30  |  8KB  |  219 lines

  1. /*------------------------------------*/
  2. /* Do the "put" or "delete" method (place the body of a request into the
  3. file designated by the url -- but only if PUT (or DELETE) is one of the
  4. permissions in the ACCESS_FILE.
  5. */
  6. /*------------------------------------*/
  7. sref_do_put:
  8. parse arg which,ddir,action,enmadd,transaction,accopts,tempfile,homedir, ,
  9.      servername,host_nickname,accept_range,do_htaccess,htaccess_file
  10.  
  11. os2e='os2environment'
  12.  
  13. if which="DELETE" then signal dodelete
  14.  
  15. /* is there a PUT option associated with this url (typically, as specified
  16. by a wildcarded entry */
  17.  
  18.     if wordpos('PUT',upper(accopts))=0 then do
  19.           dog=response1('notallowed', 'you can not PUT to this URL:'||action)
  20.           dog
  21.           return 'put not allowed '
  22.     end
  23.  
  24.     file=strip(sref_do_virtual(ddir,action,enmadd,0,transaction,homedir,host_Nickname))
  25.     if file=0 then do
  26.           dog=response1('badreq', 'could not process PUT request')
  27.           dog
  28.           return 'could not process '
  29.     end
  30.     if do_htaccess=1 then do
  31.        tmp1=sref_htaccess(action,file,htaccess_file,,,,,ddir,,TEMPFILE,0)
  32.        if tmp1=-1 then  return 'PUT: Access denied '
  33.     end
  34.  
  35.  ndo=0
  36.  
  37.  'read body var awords'                    /* get the incoming data */
  38.   filen=length(awords)
  39.  
  40.  ranges=reqfield('range:')
  41.  
  42.  if ranges<>"" then do
  43.      if accept_range<>1 then do
  44.        dog=response1('notimplemented','server does not accept range requests')
  45.        dog
  46.        return 'unallowed ranges'
  47.      end
  48.      parse var ranges atype '=' vlist
  49.      if upper(strip(atype))<>"BYTES" then do
  50.          dog=response1('notimplemented','server does not accept non-byte range requests')
  51.          dog
  52.          return 'not supported range type:' atype
  53.      end
  54.      do until vlist=""
  55.         parse var vlist aterm ',' vlist
  56.         parse var aterm  t1 '-' t2
  57.         if t1<>"" then
  58.            if datatype(t1)<>'NUM' then iterate
  59.         if t2<>"" then
  60.              if datatype(t2)<>'NUM' then iterate
  61.         if t1="" & t2="" then iterate
  62.         if t2="" then t2=filen
  63.         if t1="" then do
  64.            t1=filen-t2
  65.            t2=filen
  66.         end
  67.         if t1<0 then t1=0
  68.         if t2>(filen-1) then t2=filen-1
  69.         if t2<t1 then iterate             /* bad request */
  70.  
  71.         ndo=ndo+1
  72.         r1.ndo=t1  ; r2.ndo=t2
  73.      end
  74.  
  75.      if ndo=0 then do
  76.         dog=response1('badreq',' No acceptable ranges ')
  77.         dog
  78.         return 'no acceptable ranges'
  79.      end
  80.   end
  81.  
  82. /* can file be open ? */
  83.  a=upper(stream(file,'c','open write'))
  84.   if abbrev(a,'READY')=0 then do
  85.           dog=response1('badreq', 'PUT request to an inaccessible file ')
  86.           dog
  87.           return 'could not access file '
  88.   end
  89.  
  90.   if ndo=0 then do
  91.         doit=awords
  92.         totdo=length(awords)
  93.   end
  94.   else do
  95.        doit="" ; totdo=0
  96.        do mm=1 to ndo
  97.           t1=r1.mm ; t2=r2.mm
  98.           nget=(1+t2)-t1 ; totdo=totdo+nget
  99.           bobo=substr(awords,t1+1,nget)
  100.           doit=doit||bobo
  101.        end
  102.    end
  103.  
  104.   foo=charout(file,doit,1)
  105.   if foo>0 then do
  106.          dog=response1('badreq', 'PUT request failed to write all bytes ')
  107.          dog
  108.          return 'could not write all bytes '
  109.   end
  110.   foo=stream(file,'c','close')
  111.   call lineout tempfile, '<!doctype html public "-//IETF//DTD HTML 2.0//EN">'
  112.   call lineout tempfile, "<html><head><title>PUT request success</title></head>"
  113.   call lineout tempfile, "<body><h2>PUT request success ...</h2>"
  114.   call lineout tempfile, "<p>PUT of the URL was successful: " action
  115.   call lineout tempfile,' <br> Bytes written: ' totdo
  116.   call lineout tempfile, "</body></html>"
  117.   call lineout tempfile  /* close */
  118.   'FILE ERASE TYPE text/html NAME' tempfile
  119.   return ' success (bytes= ' totdo
  120.  
  121.  
  122. /* Jump here if DELETE method */
  123. dodelete:
  124. /*------------------------------------*/
  125. /* Do the "delete" verb (delete a file  represented by the url from the server)
  126.  -- but only if DELETE is one of the accopts.
  127. */
  128. /*------------------------------------*/
  129.  
  130. /* is there a DELETE option associated with this url (typically, as specified
  131. by a wildcarded entry */
  132.  
  133.     if wordpos('DELETE',upper(accopts))=0 then do
  134.           dog=response1('notallowed', 'you can not DELETE URL:'||action)
  135.           dog
  136.           return 'put not allowed '
  137.     end
  138.  
  139.     file=strip(sref_do_virtual(ddir,action,enmadd,0,transaction,homedir,host_nickname))
  140.     if file=0 then do
  141.           dog=response1('badreq', 'could not process DELETE request')
  142.           dog
  143.           return 'could not process '
  144.     end
  145.  
  146.     if do_htaccess=1 then do
  147.        tmp1=sref_htaccess(action,file,htaccess_file,,,,,ddir,,TEMPFILE,0)
  148.        if tmp1=-1 then  return 'DELETE: Access denied '
  149.     end
  150.  
  151.  
  152.  
  153. /* can file be open ? */
  154.  a=stream(file,'c','query exists')
  155.  if a="" then do
  156.           dog=response1('badreq', 'DELETED non-existent file ')
  157.           dog
  158.           return 'file does not exist '
  159.   end
  160.  
  161.  a=sysfiledelete(file)
  162.  if a<>0 then do
  163.           dog=response1('badreq', 'Could not delete file ')
  164.           dog
  165.           return 'problem deleting file: ' a
  166.   end
  167.  
  168.   call lineout tempfile, '<!doctype html public "-//IETF//DTD HTML 2.0//EN">'
  169.   call lineout tempfile, "<html><head><title>DELETE request success</title></head>"
  170.   call lineout tempfile, "<body><h2>DELETE request success ...</h2>"
  171.   call lineout tempfile, "<p>DELETE of the URL was successful: " action
  172.   call lineout tempfile, "</body></html>"
  173.   call lineout tempfile  /* close */
  174.   'FILE ERASE TYPE text/html NAME' tempfile
  175.   return ' success '
  176.  
  177.  
  178.  
  179.  
  180. /* ----------------------------------------------------------------------- */
  181. /* RESPONSE1: Standard [mostly error] responses.                            */
  182. /* ----------------------------------------------------------------------- */
  183. /* This routine should stay in the main filter program.                    */
  184. /* Arguments are: response type and extended message information.          */
  185. /* It returns the GoServe command to handle the result file.               */
  186. response1: procedure expose tempfile  servername
  187.   parse arg request, message
  188.   select
  189.     when request='badreq'   then use='400 Bad request syntax'
  190.     when request='notfound' then use='404 Not found'
  191.     when request='forbid'   then use='403 Forbidden'
  192.     when request='unauth'   then use='401 Unauthorized'
  193.     when request='notallowed' then use='405 Method not allowed'
  194.     when request='notimplemented' then use='501 Not implemented'
  195.     otherwise do
  196.         use='406 Not acceptable'
  197.         call pmprintf_sref('weird response '|| request||' '|| message)
  198.       end
  199.     end  /* Add others to this list as needed */
  200.  
  201.  
  202.   /* Now set the response and build the response file */
  203.   'RESPONSE HTTP/1.0' use     /* Set HTTP response line */
  204.   parse var use code text
  205.   if request='notallowed' then do
  206.      'HEADER ADD Allow:HEAD '
  207.   end
  208.   call lineout tempfile, '<!doctype html public "-//IETF//DTD HTML 2.0//EN">'
  209.   call lineout tempfile, "<html><head><title>"text"</title></head>"
  210.   call lineout tempfile, "<body><h2>Sorry...</h2>"
  211.   call lineout tempfile, "<p>The request from your Web client: " message"."
  212.   call lineout tempfile, "<hr><em>HTTP response code:</em>" code '['text']'
  213.   call lineout tempfile, "<br><em>From server at:</em>" servername
  214.   call lineout tempfile, "<br><em>Running:</em>" server()
  215.   call lineout tempfile, "</body></html>"
  216.   call lineout tempfile  /* close */
  217.   return 'FILE ERASE TYPE text/html NAME' tempfile
  218.  
  219.