Changeset 1240

Show
Ignore:
Timestamp:
11/05/06 01:01:13 (2 years ago)
Author:
david
Message:

added HTTP PUT and HTTP DELETE support to WebRequest? (default method names 'create' and 'remove'), this is all we need to support REST, so this closes #332. PUT submitted content is accessible via the normal file handling stuff, default file identifier is 'put_file', can be configured, of course (parameter name 'PUT_file_name')

Location:
trunk/src/request
Files:
1 removed
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/request/AgaviWebRequest.class.php

    r1239 r1240  
    6767 
    6868  /** 
     69   * @var        bool Indicates whether or not PUT was used to upload a file. 
     70   */ 
     71  protected $isHttpPutFile = false; 
     72 
     73  /** 
    6974   * Retrieve the scheme part of a request URL, typically the protocol. 
    7075   * Example: "http". 
     
    461466    parent::initialize($context, $parameters); 
    462467     
    463     $methods = array('GET' => 'read', 'POST' => 'write'); 
     468    $methods = array('GET' => 'read', 'POST' => 'write', 'PUT' => 'create', 'DELETE' => 'remove'); 
    464469    if(isset($parameters['method_names'])) { 
    465470      $methods = array_merge($methods, (array) $parameters['method_names']); 
     
    470475        $this->setMethod($methods['POST']); 
    471476        break; 
     477      case 'PUT': 
     478        $this->setMethod($methods['PUT']); 
     479        break; 
     480      case 'DELETE': 
     481        $this->setMethod($methods['DELETE']); 
     482        break; 
    472483      default: 
    473484        $this->setMethod($methods['GET']); 
    474485    } 
    475  
     486     
     487    if($this->getMethod() == $methods['PUT']) { 
     488      // PUT. We now gotta set a flag for that and populate $_FILES manually 
     489      $this->isHttpPutFile = true; 
     490       
     491      $putfile = tmpfile(); 
     492       
     493      stream_copy_to_stream(fopen("php://input", "rb"), $putFile); 
     494       
     495      // for temp file name and size 
     496      $putFileInfo = array( 
     497        'stat' => fstat($putFile), 
     498        'meta_data' => stream_get_meta_data($putFile) 
     499      ); 
     500       
     501      $putFileName = isset($parameters['PUT_file_name']) ? $parameters['PUT_file_name'] : 'put_file'; 
     502       
     503      $_FILES = array( 
     504        $putFileName => array( 
     505          'name' => $putFileName, 
     506          'type' => 'application/octet-stream', 
     507          'size' => $putFileInfo['stat']['size'], 
     508          'tmp_name' => $putFileInfo['meta_data']['uri'], 
     509          'error' => UPLOAD_ERR_OK 
     510        ) 
     511      ); 
     512    } 
     513     
    476514    $this->urlScheme = 'http' . (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 's' : ''); 
    477515 
     
    516554    // merge POST parameters 
    517555    $this->setParameters($_POST); 
     556  } 
     557   
     558  /** 
     559   * Wrapper method for either move_uplaoded_file or HTTP PUT input handling. 
     560   * 
     561   * @param      string The name of the input file. 
     562   * @param      string The name of the destination file. 
     563   * 
     564   * @return     bool Whether or not the operation was successful. 
     565   * 
     566   * @author     David Zuelke <dz@bitxtender.com> 
     567   * @since      0.11.0 
     568   */ 
     569  protected function moveUploadedFile($source, $destination) 
     570  { 
     571    if($this->isHttpPutFile) { 
     572      return @rename($source, $destination); 
     573    } else { 
     574      return @move_uploaded_file($source, $destination); 
     575    } 
    518576  } 
    519577 
     
    568626      } 
    569627 
    570       if(@move_uploaded_file($_FILES[$name]['tmp_name'], $file)) { 
     628      if($this->moveUploadedFile($_FILES[$name]['tmp_name'], $file)) { 
    571629        // chmod our file 
    572630        @chmod($file, $fileMode);