Set HTTP status codes with http_​response_​code()

This commit is contained in:
Alexei Sorokin
2020-08-04 14:12:17 +03:00
parent ab4120721f
commit e206995268
14 changed files with 526 additions and 525 deletions

View File

@@ -150,11 +150,9 @@ class action extends HTMLOutputter // lawsuit
$code = 400;
}
$status_string = ClientErrorAction::$status[$code];
switch ($format) {
case 'xml':
header("HTTP/1.1 {$code} {$status_string}");
http_response_code($code);
$this->initDocument('xml');
$this->elementStart('hash');
$this->element('error', null, $msg);
@@ -164,7 +162,7 @@ class action extends HTMLOutputter // lawsuit
break;
case 'json':
if (!isset($this->callback)) {
header("HTTP/1.1 {$code} {$status_string}");
http_response_code($code);
}
$this->initDocument('json');
$error_array = ['error' => $msg, 'request' => $_SERVER['REQUEST_URI']];
@@ -172,7 +170,7 @@ class action extends HTMLOutputter // lawsuit
$this->endDocument('json');
break;
case 'text':
header("HTTP/1.1 {$code} {$status_string}");
http_response_code($code);
header('Content-Type: text/plain; charset=utf-8');
echo $msg;
break;
@@ -411,7 +409,7 @@ class action extends HTMLOutputter // lawsuit
// If this check fails, ignore the if-modified-since below.
$checked = true;
if ($this->_hasEtag($etag, $if_none_match)) {
header('HTTP/1.1 304 Not Modified');
http_response_code(304);
// Better way to do this?
exit(0);
}
@@ -422,7 +420,7 @@ class action extends HTMLOutputter // lawsuit
$if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
$ims = strtotime($if_modified_since);
if ($lm <= $ims) {
header('HTTP/1.1 304 Not Modified');
http_response_code(304);
// Better way to do this?
exit(0);
}
@@ -505,11 +503,9 @@ class action extends HTMLOutputter // lawsuit
$code = 500;
}
$status_string = ServerErrorAction::$status[$code];
switch ($format) {
case 'xml':
header("HTTP/1.1 {$code} {$status_string}");
http_response_code($code);
$this->initDocument('xml');
$this->elementStart('hash');
$this->element('error', null, $msg);
@@ -519,7 +515,7 @@ class action extends HTMLOutputter // lawsuit
break;
case 'json':
if (!isset($this->callback)) {
header("HTTP/1.1 {$code} {$status_string}");
http_response_code($code);
}
$this->initDocument('json');
$error_array = ['error' => $msg, 'request' => $_SERVER['REQUEST_URI']];
@@ -1743,9 +1739,14 @@ class action extends HTMLOutputter // lawsuit
*
* @return void
*/
public function menuItem(string $url, $text, ?string $title = null, bool $is_selected = false,
?string $id = null, $class = null): void
{
public function menuItem(
string $url,
$text,
?string $title = null,
bool $is_selected = false,
?string $id = null,
$class = null
): void {
// Added @id to li for some control.
// XXX: We might want to move this to htmloutputter.php
$lattrs = [];

View File

@@ -1,37 +1,31 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* Server error action.
*
* PHP version 5
*
* @category Action
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2008, 2009, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* @category Action
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @copyright 2008, 2009 StatusNet, Inc.
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
if (!defined('GNUSOCIAL')) {
exit(1);
}
defined('GNUSOCIAL') || die();
/**
* Class for displaying HTTP server errors
@@ -44,22 +38,23 @@ if (!defined('GNUSOCIAL')) {
*
* See: http://tools.ietf.org/html/rfc2616#section-10
*
* @category Action
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
* @category Action
* @package GNUsocial
* @author Zach Copley <zach@status.net>
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class ServerErrorAction extends ErrorAction
{
static $status = [500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'];
public static $status = [
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
];
function __construct($message = 'Error', $code = 500, $ex = null)
public function __construct($message = 'Error', $code = 500, $ex = null)
{
parent::__construct($message, $code);
@@ -88,10 +83,9 @@ class ServerErrorAction extends ErrorAction
*
* @return void
*/
function extraHeaders()
public function extraHeaders()
{
$status_string = self::$status[$this->code];
header('HTTP/1.1 ' . $this->code . ' ' . $status_string);
http_response_code($this->code);
}
/**
@@ -100,9 +94,8 @@ class ServerErrorAction extends ErrorAction
* @return string page title
*/
function title()
public function title()
{
return self::$status[$this->code];
}
}