Apache Zeta Components Manual :: File Source for http.php

Source for file http.php

Documentation is available at http.php

  1. <?php
  2. /**
  3.  * File containing the ezcMvcHttpResponseWriter class
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23.  * @version //autogentag//
  24.  * @filesource
  25.  * @package MvcTools
  26.  */
  27.  
  28. /**
  29.  * Request parser that uses HTTP headers to populate an ezcMvcRequest object.
  30.  *
  31.  * @package MvcTools
  32.  * @version //autogentag//
  33.  * @mainclass
  34.  */
  35. {
  36.     /**
  37.      * Contains the response struct
  38.      *
  39.      * @var ezcMvcResponse 
  40.      */
  41.     protected $response;
  42.  
  43.     /**
  44.      * Contains an array of header name to value mappings
  45.      *
  46.      * @var array(string=>string) 
  47.      */
  48.     public $headers;
  49.  
  50.     /**
  51.      * Creates a new ezcMvcHttpResponseWriter class to write $response
  52.      *
  53.      * @param ezcMvcResponse $response 
  54.      */
  55.     public function __constructezcMvcResponse $response )
  56.     {
  57.         $this->response = $response;
  58.         $this->headers = array();
  59.     }
  60.  
  61.     /**
  62.      * Takes the raw protocol depending response body, and the protocol
  63.      * abstract response headers and forges a response to the client. Then it sends
  64.      * the assembled response to the client.
  65.      */
  66.     public function handleResponse()
  67.     {
  68.         // process all headers
  69.         $this->processStandardHeaders();
  70.         if $this->response->cache instanceof ezcMvcResultCache )
  71.         {
  72.             $this->processCacheHeaders();
  73.         }
  74.         if $this->response->content instanceof ezcMvcResultContent )
  75.         {
  76.             $this->processContentHeaders();
  77.         }
  78.  
  79.         // process the status headers through objects
  80.         if $this->response->status instanceof ezcMvcResultStatusObject )
  81.         {
  82.             $this->response->status->process$this );
  83.         }
  84.  
  85.         // automatically add content-length header
  86.         $this->headers['Content-Length'strlen$this->response->body );
  87.  
  88.         // write output
  89.         foreach $this->headers as $header => $value )
  90.         {
  91.             header"$header$value);
  92.         }
  93.         // do cookies
  94.         foreach $this->response->cookies as $cookie )
  95.         {
  96.             $this->processCookie$cookie );
  97.         }
  98.         echo $this->response->body;
  99.     }
  100.  
  101.     /**
  102.      * Takes a $cookie and uses PHP's setcookie() function to add cookies to the output stream.
  103.      *
  104.      * @param ezcMvcResultCookie $cookie 
  105.      */
  106.     private function processCookieezcMvcResultCookie $cookie )
  107.     {
  108.         $args array();
  109.         $args[$cookie->name;
  110.         $args[$cookie->value;
  111.         if $cookie->expire instanceof DateTime )
  112.         {
  113.             $args[$cookie->expire->format'U' );
  114.         }
  115.         else
  116.         {
  117.             $args[null;
  118.         }
  119.         $args[$cookie->domain;
  120.         $args[$cookie->path;
  121.         $args[$cookie->secure;
  122.         $args[$cookie->httpOnly;
  123.         call_user_func_array'setcookie'$args );
  124.     }
  125.  
  126.     /**
  127.      * Checks whether there is a DateTime object in $obj->$prop and sets a header accordingly.
  128.      *
  129.      * @param Object $obj 
  130.      * @param string $prop 
  131.      * @param string $headerName 
  132.      * @param bool   $default 
  133.      */
  134.     private function doDate$obj$prop$headerName$default false )
  135.     {
  136.         if $obj->$prop instanceof DateTime )
  137.         {
  138.             $headerDate clone $obj->$prop;
  139.             $headerDate->setTimezonenew DateTimeZone"UTC" ) );
  140.             $this->headers[$headerName$headerDate->format'D, d M Y H:i:s \G\M\T' );
  141.             return;
  142.         }
  143.  
  144.         if $default )
  145.         {
  146.             $headerDate new DateTime"UTC" );
  147.             $this->headers[$headerName$headerDate->format'D, d M Y H:i:s \G\M\T' );
  148.         }
  149.     }
  150.  
  151.     /**
  152.      * Processes the standard headers that are not subdivided into other structs.
  153.      */
  154.     protected function processStandardHeaders()
  155.     {
  156.         $res $this->response;
  157.  
  158.         // generator
  159.         $this->headers['X-Powered-By'$res->generator !== ''
  160.             ? $res->generator
  161.             : "eZ Components MvcTools";
  162.  
  163.         $this->doDate$res'date''Date'true );
  164.     }
  165.  
  166.     /**
  167.      * Processes the caching related headers.
  168.      */
  169.     protected function processCacheHeaders()
  170.     {
  171.         $cache $this->response->cache;
  172.  
  173.         if $cache->vary )
  174.         {
  175.             $this->headers['Vary'$cache->vary;
  176.         }
  177.         $this->doDate$cache'expire''Expires' );
  178.         if count$cache->controls ) )
  179.         {
  180.             $this->headers['Cache-Control'join', '$cache->controls );
  181.         }
  182.         if $cache->pragma )
  183.         {
  184.             $this->headers['Pragma'$cache->pragma;
  185.         }
  186.         $this->doDate$cache'lastModified''Last-Modified' );
  187.     }
  188.  
  189.     /**
  190.      * Processes the content type related headers.
  191.      */
  192.     protected function processContentHeaders()
  193.     {
  194.         $content $this->response->content;
  195.         $defaultContentType 'text/html';
  196.  
  197.         if $content->language )
  198.         {
  199.             $this->headers['Content-Language'$content->language;
  200.         }
  201.         if $content->type || $content->charset )
  202.         {
  203.             $contentType $content->type $content->type $defaultContentType;
  204.             if $content->charset )
  205.             {
  206.                 $contentType .= '; charset=' $content->charset;
  207.             }
  208.             $this->headers['Content-Type'$contentType;
  209.         }
  210.         if $content->encoding )
  211.         {
  212.             $this->headers['Content-Encoding'$content->encoding;
  213.         }
  214.  
  215.         if $content->disposition instanceof ezcMvcResultContentDisposition )
  216.         {
  217.             $this->processContentDispositionHeaders$content->disposition );
  218.         }
  219.     }
  220.  
  221.     /**
  222.      * Processed the content disposition related headers.
  223.      *
  224.      * See http://tools.ietf.org/html/rfc2183#section-2, but implemented with limitations.
  225.      *
  226.      * @param ezcMvcResultContentDisposition $disp 
  227.      */
  228.     protected function processContentDispositionHeadersezcMvcResultContentDisposition $disp )
  229.     {
  230.         // type
  231.         $value $disp->type;
  232.  
  233.         // filename
  234.         if $disp->filename !== null )
  235.         {
  236.             $value .= "; filename";
  237.             if strpbrk$disp->filename,
  238.                 "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff" === false )
  239.             {
  240.                 // case 1: ASCII characters only
  241.                 if strpbrk$disp->filename'\(\)<>@,;:\\"/\[\]?= ' === false )
  242.                 {
  243.                     // case 1a: no tspecials
  244.                     $value .= '=' $disp->filename;
  245.                 }
  246.                 else
  247.                 {
  248.                     // case 1b: with tspecials
  249.                     $value .= '="' str_replace'"''\"'$disp->filename '"';
  250.                 }
  251.             }
  252.             else
  253.             {
  254.                 // case 2: non-ASCII characters (and thus UTF-8 encoded)
  255.                 $value .= "*=utf-8''" urlencode$disp->filename );
  256.             }
  257.         }
  258.  
  259.         // dates
  260.         if $disp->creationDate !== null )
  261.         {
  262.             $value .= '; creation-date="' $disp->creationDate->formatDateTime::RFC2822 '"';
  263.         }
  264.         if $disp->modificationDate !== null )
  265.         {
  266.             $value .= '; modification-date="' $disp->modificationDate->formatDateTime::RFC2822 '"';
  267.         }
  268.         if $disp->readDate !== null )
  269.         {
  270.             $value .= '; read-date="' $disp->readDate->formatDateTime::RFC2822 '"';
  271.         }
  272.  
  273.         // size
  274.         if $disp->size !== null )
  275.         {
  276.             $value .= "; size=" . (int) $disp->size;
  277.         }
  278.  
  279.         $this->headers['Content-Disposition'$value;
  280.     }
  281. }
  282. ?>
Documentation generated by phpDocumentor 1.4.3