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];
}
}

View File

@@ -1,34 +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/>.
/**
* StatusNet, the distributed open-source microblogging tool
*
* Base class for RSS 1.0 feed actions
*
* PHP version 5
*
* LICENCE: 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 Mail
* @package StatusNet
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @author Earle Martin <earle@downlode.org>
* @copyright 2008-9 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://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();
define('DEFAULT_RSS_LIMIT', 48);
@@ -36,10 +33,10 @@ class Rss10Action extends ManagedAction
{
// This will contain the details of each feed item's author and be used to generate SIOC data.
var $creators = array();
var $limit = DEFAULT_RSS_LIMIT;
var $notices = null;
var $tags_already_output = array();
public $creators = [];
public $limit = DEFAULT_RSS_LIMIT;
public $notices = null;
public $tags_already_output = [];
public function isReadOnly($args)
{
@@ -88,9 +85,9 @@ class Rss10Action extends ManagedAction
// for example if we need to set $this->target or something
}
function show_basic_auth_error()
public function show_basic_auth_error()
{
header('HTTP/1.1 401 Unauthorized');
http_response_code(401);
header('Content-Type: application/xml; charset=utf-8');
$this->startXML();
$this->elementStart('hash');
@@ -119,20 +116,22 @@ class Rss10Action extends ManagedAction
* @return array
*/
function getChannel()
public function getChannel()
{
return array('url' => '',
'title' => '',
'link' => '',
'description' => '');
return [
'url' => '',
'title' => '',
'link' => '',
'description' => '',
];
}
function getImage()
public function getImage()
{
return null;
}
function showPage()
public function showPage()
{
$this->initRss();
$this->showChannel();
@@ -154,9 +153,8 @@ class Rss10Action extends ManagedAction
$this->endRss();
}
function showChannel()
public function showChannel()
{
$channel = $this->getChannel();
$image = $this->getImage();
@@ -164,7 +162,9 @@ class Rss10Action extends ManagedAction
$this->element('title', null, $channel['title']);
$this->element('link', null, $channel['link']);
$this->element('description', null, $channel['description']);
$this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
$this->element('cc:licence', [
'rdf:resource' => common_config('license', 'url'),
]);
if ($image) {
$this->element('image', array('rdf:resource' => $image));
@@ -185,7 +185,7 @@ class Rss10Action extends ManagedAction
$this->elementEnd('channel');
}
function showImage()
public function showImage()
{
$image = $this->getImage();
if ($image) {
@@ -198,7 +198,7 @@ class Rss10Action extends ManagedAction
}
}
function showItem($notice)
public function showItem($notice)
{
$profile = $notice->getProfile();
$nurl = common_local_url('shownotice', array('notice' => $notice->id));
@@ -237,13 +237,17 @@ class Rss10Action extends ManagedAction
$this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
}
if (!empty($notice->conversation)) {
$conversationurl = common_local_url('conversation',
array('id' => $notice->conversation));
$this->element('sioc:has_discussion', array('rdf:resource' => $conversationurl));
$conversationurl = common_local_url(
'conversation',
['id' => $notice->conversation]
);
$this->element('sioc:has_discussion', [
'rdf:resource' => $conversationurl,
]);
}
$attachments = $notice->attachments();
if($attachments){
foreach($attachments as $attachment){
if ($attachments) {
foreach ($attachments as $attachment) {
try {
$enclosure = $attachment->getEnclosure();
$attribs = array('rdf:resource' => $enclosure->url);
@@ -274,7 +278,7 @@ class Rss10Action extends ManagedAction
while ($tag->fetch()) {
$tagpage = common_local_url('tag', array('tag' => $tag->tag));
if ( in_array($tag, $this->tags_already_output) ) {
if (in_array($tag, $this->tags_already_output)) {
$this->element('ctag:tagged', array('rdf:resource'=>$tagpage.'#concept'));
continue;
}
@@ -294,7 +298,7 @@ class Rss10Action extends ManagedAction
$this->creators[$creator_uri] = $profile;
}
function showCreators()
public function showCreators()
{
foreach ($this->creators as $uri => $profile) {
$id = $profile->id;
@@ -311,7 +315,7 @@ class Rss10Action extends ManagedAction
}
}
function initRss()
public function initRss()
{
$channel = $this->getChannel();
header('Content-Type: application/rdf+xml');
@@ -351,7 +355,7 @@ class Rss10Action extends ManagedAction
$this->elementEnd('sioc:Site');
}
function endRss()
public function endRss()
{
$this->elementEnd('rdf:RDF');
}
@@ -361,7 +365,7 @@ class Rss10Action extends ManagedAction
*
*/
function lastModified()
public function lastModified()
{
if (empty($this->notices)) {
return null;
@@ -376,4 +380,3 @@ class Rss10Action extends ManagedAction
return strtotime($this->notices[0]->created);
}
}

View File

@@ -82,7 +82,7 @@ class ClosechannelAction extends Action
{
$this->channel->decrement();
header('HTTP/1.1 204 No Content');
http_response_code(204);
return;
}

View File

@@ -1,48 +1,39 @@
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2011, StatusNet, Inc.
*
* action periodically pinged by a page to keep a channel alive
*
* PHP version 5
*
* 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 Realtime
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
// 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/>.
/**
* Action periodically pinged by a page to keep a channel alive
*
* @category Realtime
* @package StatusNet
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
defined('GNUSOCIAL') || die();
/**
* Action periodically pinged by a page to keep a channel alive
*
* @category Realtime
* @package GNUsocial
* @author Evan Prodromou <evan@status.net>
* @copyright 2011 StatusNet, Inc.
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class KeepalivechannelAction extends Action
{
@@ -92,7 +83,7 @@ class KeepalivechannelAction extends Action
{
$this->channel->touch();
header('HTTP/1.1 204 No Content');
http_response_code(204);
return;
}

View File

@@ -1,66 +1,64 @@
<?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/>.
/**
* Client 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-2010 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-2010 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 client errors
*
* @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 ClientErrorAction extends ErrorAction
{
static $status = array(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 Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed');
public static $status = [
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 Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed'
];
function __construct($message='Error', $code=400)
public function __construct($message = 'Error', $code = 400)
{
parent::__construct($message, $code);
$this->default = 400;
@@ -78,10 +76,9 @@ class ClientErrorAction 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);
}
/**
@@ -90,7 +87,7 @@ class ClientErrorAction extends ErrorAction
* @return page title
*/
function title()
public function title()
{
return @self::$status[$this->code];
}

View File

@@ -1659,15 +1659,11 @@ function common_sql_weight($column, $dropoff)
return "SUM(EXP({$expr} / {$dropoff}))";
}
function common_redirect($url, $code=307)
function common_redirect(string $url, int $code = 307): void
{
static $status = [301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
307 => "Temporary Redirect"];
header('HTTP/1.1 '.$code.' '.$status[$code]);
header("Location: $url");
assert(in_array($code, [301, 302, 303, 307]));
http_response_code($code);
header("Location: {$url}");
header("Connection: close");
$xo = new XMLOutputter();
@@ -1678,7 +1674,7 @@ function common_redirect($url, $code=307)
);
$xo->element('a', ['href' => $url], $url);
$xo->endXML();
exit;
die();
}
// Stick the notice on the queue