Example #1
0
 /**
  * 出力
  * @param array $headers ヘッダー(別途Header::getHeaders()で指定すること)
  * @param int $status ステータスコード
  * @param string $body 内容
  * @return void
  */
 public static function writeResponse($headers, $status = Response::STATUS_CODE_200, $body = '')
 {
     global $_string;
     // なぜかこの行を出力しないと503エラーが起きる
     echo "";
     // レスポンスをコンストラクト
     $response = new Response();
     if (!empty($body)) {
         if ($status == Response::STATUS_CODE_200 && isset($headers['If-None-Match']) && !isset($headers['ETag'])) {
             // Modifiedヘッダーが出力されてない場合、出力内容からETagを生成
             // 負荷対策にはならないが転送量を抑えることができる
             $hash = md5($body);
             if (preg_match('/' . $hash . '/', $headers['If-None-Match'])) {
                 $status = Response::STATUS_CODE_304;
             }
             $headers['Etag'] = $hash;
         } else {
             if ($status == Response::STATUS_CODE_401) {
                 // レスポンスコードが401の場合、認証画面を出力
                 $headers['WWW-Authenticate'] = Auth::getAuthHeader();
             }
         }
         // 内容が存在する場合容量をContent-Lengthヘッダーに出力
         //	if (!isset($headers['Content-Length'])){
         $headers['Content-Length'] = strlen($body);
         //	}
         // レスポンスに内容を追加
         $response->setContent($body);
     }
     // ajaxで送信した時に、net::ERR_CONTENT_LENGTH_MISMATCHエラーが発生するので、
     // その場合、Content Lengthヘッダーを出力しない
     if (IS_AJAX) {
         unset($headers['Content-Length']);
     }
     // ステータスコードを出力
     $response->setStatusCode($status);
     // ヘッダーをソート
     ksort($headers);
     // ヘッダーを指定
     $response->getHeaders()->addHeaders($headers);
     // ヘッダー出力をチェック
     if (headers_sent($file, $line)) {
         die(sprintf('Header::writeResponse(): ' . $_string['header_sent'], Utility::htmlsc($file), $line));
         exit;
     }
     // ステータスコードを出力
     header($response->renderStatusLine());
     // ヘッダーを出力
     foreach ($response->getHeaders() as $_header) {
         header($_header->toString());
     }
     if (!empty($body)) {
         // 内容を出力
         echo $response->getBody();
     }
     // 出力バッファをフラッシュ
     flush();
     // 終了
     exit;
 }