<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$
 */

  uses(
    'org.apache.HttpScriptlet',
    'org.webdav.xml.WebdavPropFindRequest',
    'org.webdav.xml.WebdavPropFindResponse'
  );
  
  define('WEBDAV_METHOD_PROPFIND',  'PROPFIND');
  define('WEBDAV_METHOD_PROPPATCH', 'PROPPATCH');
  
  /**
   * Webdav
   * 
   * <code>
   *   $s= &new WebdavScriptlet(array(
   *    '/webdav/' => new DavFileImpl('/path/to/files/you/want/do/provide/')
   *   ));
   *   try(); {
   *     $s->init();
   *     $response= &$s->process();
   *   } if (catch('HttpScriptletException', $e)) {
   *     // Retreive standard "Internal Server Error"-Document
   *     $response= &$e->getResponse(); 
   *   }
   *   
   *   $response->sendHeaders();
   *   $response->sendContent();
   *   $s->finalize();  
   * </code>
   *
   * Note: Needs PHP patched to work!
   *
   * @see      http://sitten-polizei.de/php/webdav.patch
   * @see      http://www.webdav.org/
   * @see      http://www.webdav.org/cadaver/
   * @see      rfc://rfc2518 (WebDAV)
   * @see      rfc://rfc2616 (HTTP/1.1)
   * @see      rfc://rfc3253 (DeltaV)
   * @purpose  Provide the base for Webdav Services
   */
  class WebdavScriptlet extends HttpScriptlet {
    var
      $impl         = array(),
      $handlingImpl = NULL;
      
    /**
     * Constructor
     *
     * @access  public
     * @param   array impl (associative array of pathmatch => org.webdav.impl.DavImpl)
     */  
    function __construct($impl) {
      $this->impl= $impl;
      parent::__construct();
    }

    /**
     * Handle OPTIONS
     *
     * @see     xp://org.apache.scriptlet.HttpScriptlet#doGet
     *@access  private
     * @return  bool processed
     * @public  request org.apache.HttpScriptletRequest
     * @access  response org.apache.HttpScriptletResponse
     * @throws  Exception to indicate failure
     */
    function doOptions(&$request, &$response) {
      $response->setHeader('MS-Author-Via', 'DAV'); // MS-clients
      $response->setHeader('Allow', implode(', ', array(
        HTTP_METHOD_OPTIONS,
        HTTP_METHOD_GET,
        HTTP_METHOD_PUT,
        HTTP_METHOD_DELETE,
        WEBDAV_METHOD_PROPFIND,
        WEBDAV_METHOD_PROPPATCH
      )));
      $response->setHeader('DAV', '1, 2');
    }
    
    /**
     * Receives an PROPPATCH request from the <pre>process()</pre> method
     * and handles it.
     *
     * @see     xp://org.apache.scriptlet.HttpScriptlet#doGet
     *@access  private
     * @return  bool processed
     * @public  request org.apache.HttpScriptletRequest
     * @access  response org.apache.HttpScriptletResponse
     * @throws  Exception to indicate failure
     */
    function doPropFind(&$request, &$response) {
      try(); {
        $multistatus= &$this->handlingImpl->propfind(
          new WebdavPropFindRequest($request),
          new WebdavPropFindResponse()
        );
      } if (catch('Exception', $e)) {
        return throw(new HttpScriptletException($e->message));
      }
      
      // Send "HTTP/1.1 207 Multi-Status" response header
      $response->setStatus(207);
      $response->setHeader('Content-Type', 'text/xml');
      
      $response->setContent($multistatus->getSource(0));
    }

    /**
     * Receives an PROPPATCH request from the <pre>process()</pre> method
     * and handles it.
     *
     * @see     xp://org.apache.scriptlet.HttpScriptlet#doGet
     *@access  private
     * @return  bool processed
     * @public  request org.apache.HttpScriptletRequest
     * @access  response org.apache.HttpScriptletResponse
     * @throws  Exception to indicate failure
     */
    function doPropPatch(&$request, &$response) {
    }
  
    /**
     * Handle methods
     *
     * @access  private
     * @return  string class method (one of doGet, doPost, doHead)
     * @param   string method Request-Method
     * @see     http://www.webdav.org/
     */
    function _handleMethod($method) {
      $l= &Logger::getInstance();
      $c= &$l->getCategory();
      
      // Read input if we have a 
      if (
        (NULL !== ($len= $this->request->getHeader('Content-Length'))) &&
        (FALSE !== ($fd= fopen('php://input', 'r')))
      ) {
        $data= fread($fd, $len);
        $c->debug($method, $len, $data);
        fclose($fd);
      }
      
      // Select implementation
      $this->handlingImpl= NULL;
      foreach (array_keys($this->impl) as $pattern) {
        if (0 !== strpos($this->request->uri['path'], $pattern)) continue;
        
        $this->request->uri['path_root']= $pattern;
        $this->request->uri['path_translated']= (string)substr(
          $this->request->uri['path'], 
          strlen($pattern)
        );
        $this->handlingImpl= &$this->impl[$pattern];
        break;
      }
      
      // Implementation not found
      if (NULL === $this->handlingImpl) {
        trigger_error('No pattern match ['.implode(', ', array_keys($this->impl)).']', E_USER_NOTICE);
        return throw(new HttpScriptlet('Cannot handle requests to '.$request->uri['path']));
      }

      switch ($method) {
        case HTTP_METHOD_OPTIONS:
          $this->request->setData(getenv('QUERY_STRING'));
          $this->request->setParams(array_change_key_case($_REQUEST, CASE_LOWER));
          $this->_method= 'doOptions';
          break;
          
        case WEBDAV_METHOD_PROPFIND:
          $this->request->setData($data);
          $this->request->setParams(array_change_key_case($_REQUEST, CASE_LOWER));
          $this->_method= 'doPropFind';
          break;

        case WEBDAV_METHOD_PROPPATCH:
          $this->request->setData($data);
          $this->request->setParams(array_change_key_case($_REQUEST, CASE_LOWER));
          $this->_method= 'doPropPatch';
          break;
          
        // TBD: COPY MOVE OPTIONS...
          
        default:
          $this->_method= parent::_handleMethod($method);
      }
      
      return $this->_method;
    }
  }
?>
<commit_msg>- Correct RFC linkage<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$
 */

  uses(
    'org.apache.HttpScriptlet',
    'org.webdav.xml.WebdavPropFindRequest',
    'org.webdav.xml.WebdavPropFindResponse'
  );
  
  define('WEBDAV_METHOD_PROPFIND',  'PROPFIND');
  define('WEBDAV_METHOD_PROPPATCH', 'PROPPATCH');
  
  /**
   * Webdav
   * 
   * <code>
   *   $s= &new WebdavScriptlet(array(
   *    '/webdav/' => new DavFileImpl('/path/to/files/you/want/do/provide/')
   *   ));
   *   try(); {
   *     $s->init();
   *     $response= &$s->process();
   *   } if (catch('HttpScriptletException', $e)) {
   *     // Retreive standard "Internal Server Error"-Document
   *     $response= &$e->getResponse(); 
   *   }
   *   
   *   $response->sendHeaders();
   *   $response->sendContent();
   *   $s->finalize();  
   * </code>
   *
   * Note: Needs PHP patched to work!
   *
   * @see      http://sitten-polizei.de/php/webdav.patch
   * @see      http://www.webdav.org/
   * @see      http://www.webdav.org/cadaver/
   * @see      rfc://2518 (WebDAV)
   * @see      rfc://2616 (HTTP/1.1)
   * @see      rfc://3253 (DeltaV)
   * @purpose  Provide the base for Webdav Services
   */
  class WebdavScriptlet extends HttpScriptlet {
    var
      $impl         = array(),
      $handlingImpl = NULL;
      
    /**
     * Constructor
     *
     * @access  public
     * @param   array impl (associative array of pathmatch => org.webdav.impl.DavImpl)
     */  
    function __construct($impl) {
      $this->impl= $impl;
      parent::__construct();
    }

    /**
     * Handle OPTIONS
     *
     * @see     xp://org.apache.scriptlet.HttpScriptlet#doGet
     *@access  private
     * @return  bool processed
     * @public  request org.apache.HttpScriptletRequest
     * @access  response org.apache.HttpScriptletResponse
     * @throws  Exception to indicate failure
     */
    function doOptions(&$request, &$response) {
      $response->setHeader('MS-Author-Via', 'DAV'); // MS-clients
      $response->setHeader('Allow', implode(', ', array(
        HTTP_METHOD_OPTIONS,
        HTTP_METHOD_GET,
        HTTP_METHOD_PUT,
        HTTP_METHOD_DELETE,
        WEBDAV_METHOD_PROPFIND,
        WEBDAV_METHOD_PROPPATCH
      )));
      $response->setHeader('DAV', '1, 2');
    }
    
    /**
     * Receives an PROPPATCH request from the <pre>process()</pre> method
     * and handles it.
     *
     * @see     xp://org.apache.scriptlet.HttpScriptlet#doGet
     *@access  private
     * @return  bool processed
     * @public  request org.apache.HttpScriptletRequest
     * @access  response org.apache.HttpScriptletResponse
     * @throws  Exception to indicate failure
     */
    function doPropFind(&$request, &$response) {
      try(); {
        $multistatus= &$this->handlingImpl->propfind(
          new WebdavPropFindRequest($request),
          new WebdavPropFindResponse()
        );
      } if (catch('Exception', $e)) {
        return throw(new HttpScriptletException($e->message));
      }
      
      // Send "HTTP/1.1 207 Multi-Status" response header
      $response->setStatus(207);
      $response->setHeader('Content-Type', 'text/xml');
      
      $response->setContent($multistatus->getSource(0));
    }

    /**
     * Receives an PROPPATCH request from the <pre>process()</pre> method
     * and handles it.
     *
     * @see     xp://org.apache.scriptlet.HttpScriptlet#doGet
     *@access  private
     * @return  bool processed
     * @public  request org.apache.HttpScriptletRequest
     * @access  response org.apache.HttpScriptletResponse
     * @throws  Exception to indicate failure
     */
    function doPropPatch(&$request, &$response) {
    }
  
    /**
     * Handle methods
     *
     * @access  private
     * @return  string class method (one of doGet, doPost, doHead)
     * @param   string method Request-Method
     * @see     http://www.webdav.org/
     */
    function _handleMethod($method) {
      $l= &Logger::getInstance();
      $c= &$l->getCategory();
      
      // Read input if we have a 
      if (
        (NULL !== ($len= $this->request->getHeader('Content-Length'))) &&
        (FALSE !== ($fd= fopen('php://input', 'r')))
      ) {
        $data= fread($fd, $len);
        $c->debug($method, $len, $data);
        fclose($fd);
      }
      
      // Select implementation
      $this->handlingImpl= NULL;
      foreach (array_keys($this->impl) as $pattern) {
        if (0 !== strpos($this->request->uri['path'], $pattern)) continue;
        
        $this->request->uri['path_root']= $pattern;
        $this->request->uri['path_translated']= (string)substr(
          $this->request->uri['path'], 
          strlen($pattern)
        );
        $this->handlingImpl= &$this->impl[$pattern];
        break;
      }
      
      // Implementation not found
      if (NULL === $this->handlingImpl) {
        trigger_error('No pattern match ['.implode(', ', array_keys($this->impl)).']', E_USER_NOTICE);
        return throw(new HttpScriptlet('Cannot handle requests to '.$request->uri['path']));
      }

      switch ($method) {
        case HTTP_METHOD_OPTIONS:
          $this->request->setData(getenv('QUERY_STRING'));
          $this->request->setParams(array_change_key_case($_REQUEST, CASE_LOWER));
          $this->_method= 'doOptions';
          break;
          
        case WEBDAV_METHOD_PROPFIND:
          $this->request->setData($data);
          $this->request->setParams(array_change_key_case($_REQUEST, CASE_LOWER));
          $this->_method= 'doPropFind';
          break;

        case WEBDAV_METHOD_PROPPATCH:
          $this->request->setData($data);
          $this->request->setParams(array_change_key_case($_REQUEST, CASE_LOWER));
          $this->_method= 'doPropPatch';
          break;
          
        // TBD: COPY MOVE OPTIONS...
          
        default:
          $this->_method= parent::_handleMethod($method);
      }
      
      return $this->_method;
    }
  }
?>

{'fraction_non_alphanumeric': 0.15111519505471557, 'fraction_numerical': 0.004343830924734776, 'mean_word_length': 2.231309041835358, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5705116', 'n_tokens_mistral': 3889, 'n_tokens_neox': 3758, 'n_words': 691}