Esempio n. 1
0
 /**
  * 
  * Prepares $this->_headers, $this->_cookies, and $this->content for the
  * request.
  * 
  * @return array A sequential array where element 0 is a URI object,
  * element 1 is string of headers (including cookies), and element 2 is a 
  * string of content.
  * 
  * @todo Only generate $content on POST and PUT?
  * 
  */
 protected function _prepareRequest()
 {
     // get the URI
     if (!$this->_uri) {
         throw $this->_exception('ERR_NO_URI');
     } else {
         $uri = clone $this->_uri;
     }
     // what kind of request is this?
     $is_get = $this->_method == Solar_Http_Request::METHOD_GET;
     $is_post = $this->_method == Solar_Http_Request::METHOD_POST;
     $is_put = $this->_method == Solar_Http_Request::METHOD_PUT;
     // do we have any body content?
     if (is_array($this->content) && ($is_post || $is_put)) {
         // is a POST or PUT with a data array.
         // convert from array and force the content-type.
         $content = http_build_query($this->content);
         $content_type = 'application/x-www-form-urlencoded';
     } elseif (is_array($this->content) && $is_get) {
         // is a GET with a data array.
         // merge the content array to the cloned uri query params.
         $uri->query = array_merge($uri->query, $this->content);
         // now clear out the content
         $content = null;
         $content_type = null;
     } elseif (is_string($this->content)) {
         // honor as set by the user
         $content = $this->content;
         $content_type = $this->_content_type;
     } else {
         // no recognizable content
         $content = null;
         $content_type = null;
     }
     // get a list of the headers as they are now
     $list = $this->_headers;
     // force the content-type header if needed
     if ($content_type) {
         if ($this->_charset) {
             $content_type .= "; charset={$this->_charset}";
         }
         $list['Content-Type'] = $content_type;
     }
     // auto-set the content-length
     if ($this->_config['auto_set_length']) {
         if ($content) {
             $list['Content-Length'] = strlen($content);
         } else {
             unset($list['Content-Length']);
         }
     }
     // force the user-agent header if needed
     if ($this->_user_agent) {
         $list['User-Agent'] = $this->_user_agent;
     }
     // force the referer if needed
     if ($this->_referer) {
         $list['Referer'] = $this->_referer->get(true);
     }
     // convert the list of all header values to a sequential array
     $headers = array();
     foreach ($list as $key => $set) {
         settype($set, 'array');
         foreach ($set as $val) {
             $headers[] = Solar_Mime::headerLine($key, $val);
         }
     }
     // create additional cookies in the headers array
     if ($this->_cookies) {
         $val = array();
         foreach ($this->_cookies as $name => $data) {
             $val[] = "{$name}=" . urlencode($data);
         }
         $headers[] = Solar_Mime::headerLine('Cookie', implode(';', $val));
     }
     // done!
     return array($uri, $headers, $content);
 }