Changeset 2444

Show
Ignore:
Timestamp:
04/21/2008 07:32:12 AM
Author:
tof
Message:

Fixed #770

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/HISTORY

    r2441 r2444  
    4141ENH: Ticket#745 - TWizard action MoveTo - allow specify step ID's (Christophe) 
    4242ENH: Ticket#757 - TDateFormat and TNumberFormat now implement IDataRenderer (Qiang) 
     43EHH: Ticket#770 - THttpResponse send HTTP 1.1 Status code to the client (Christophe) 
    4344ENH: Ticket#783 - Added date/time type support to soap implementation (Hongliang) 
    4445ENH: Ticket#800,#833 - Added database connection charset for MySql and PgSql (donkee) 
  • trunk/framework/Web/THttpResponse.php

    r2301 r2444  
    4343 * or {@link TGlobalization::setCharset() TGlobalization.Charset} is set. 
    4444 * 
     45 * Since 3.1.2, HTTP status code can be set with the {@link setStatusCode StatusCode} property. 
     46 *  
     47 * Note: Some HTTP Status codes can require additional header or body information. So, if you use {@link setStatusCode StatusCode} 
     48 * in your application, be sure to add theses informations. 
     49 * E.g : to make an http authentication : 
     50 * <code> 
     51 *  public function clickAuth ($sender, $param) 
     52 *  { 
     53 *     $response=$this->getResponse(); 
     54 *     $response->setStatusCode(401); 
     55 *     $response->appendHeader('WWW-Authenticate: Basic realm="Test"'); 
     56 *  }  
     57 * </code> 
     58 *     
     59 * This event handler will sent the 401 status code (Unauthorized) to the browser, with the WWW-Authenticate header field. This 
     60 * will force the browser to ask for a username and a password. 
     61 *   
    4562 * @author Qiang Xue <qiang.xue@gmail.com> 
    4663 * @version $Id$ 
     
    5168{ 
    5269        /** 
     70         * @var The differents defined status code by RFC 2616 {@link http://www.faqs.org/rfcs/rfc2616} 
     71         */ 
     72        private static $HTTP_STATUS_CODES = array( 
     73                100 => 'Continue', 101 => 'Switching Protocols',  
     74                200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content',  
     75                300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',  
     76                400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed',  
     77                500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported' 
     78        ); 
     79 
     80        /** 
    5381         * @var boolean whether to buffer output 
    5482         */ 
     
    6694         */ 
    6795        private $_status=200; 
     96        /** 
     97         * @var string reason correspond to status code 
     98         */ 
     99        private $_reason='OK'; 
    68100        /** 
    69101         * @var string HTML writer type 
     
    225257 
    226258        /** 
     259         * Set the HTTP status code for the response. 
     260         *  
    227261         * @param integer HTTP status code 
    228262         */ 
    229         public function setStatusCode($status) 
    230         { 
    231                 $this->_status=TPropertyValue::ensureInteger($status); 
     263        public function setStatusCode($status, $reason=null) 
     264        { 
     265                $status=TPropertyValue::ensureInteger($status); 
     266                if(isset(self::$HTTP_STATUS_CODES[$status])) { 
     267                        $this->_reason=self::$HTTP_STATUS_CODES[$status]; 
     268                }else{ 
     269                        if($reason===null || $reason==='') { 
     270                                throw new TInvalidDataValueException("response_status_reason_missing"); 
     271                        } 
     272                        $reason=TPropertyValue::ensureString($reason); 
     273                        if(strpos($reason, "\r")!=false || strpos($reason, "\n")!=false) { 
     274                                throw new TInvalidDataValueException("response_status_reason_barchars"); 
     275                        } 
     276                        $this->_reason=$reason; 
     277                } 
     278                $this->_status=$status; 
     279        } 
     280 
     281        /** 
     282         * @param string HTTP status reason 
     283         */ 
     284        public function getStatusReason() { 
     285                return $this->_reason; 
    232286        } 
    233287 
     
    288342                } 
    289343                $fn=basename($fileName); 
     344                header("HTTP/1.1 {$this->_status} {$this->_reason}", true, $this->_status); 
    290345                if(is_array($headers)) 
    291346                { 
     
    367422        { 
    368423                Prado::trace("Flushing output",'System.Web.THttpResponse'); 
     424                $this->sendHttpHeader(); 
    369425                $this->sendContentTypeHeader(); 
    370426                if($this->_bufferOutput) 
    371427                        ob_flush(); 
     428        } 
     429         
     430        /** 
     431         * Send the HTTP header with the status code (defaults to 200) and status reason (defaults to OK) 
     432         */ 
     433        protected function sendHttpHeader () 
     434        { 
     435                header("HTTP/1.1 {$this->_status} {$this->_reason}", true, $this->_status); 
    372436        } 
    373437