view StatusCodes.inc.php @ 134:b6b4a58c7625

Using .inc.php rather than just .inc for include files.
author Tom Fredrik Blenning <bfg@bfgconsult.no>
date Sun, 22 Jan 2023 19:22:00 +0100
parents StatusCodes.inc@ff5fc61aa5ea
children
line wrap: on
line source

<?php
/**
 * StatusCodes provides named constants for
 * HTTP protocol status codes. Written for the
 * Recess Framework (http://www.recessframework.com/)
 *
 * @author Kris Jordan
 * @author Tom Fredrik Blenning Klaussen
 * @copyright MIT
 */
class StatusCodes
{
  // [Informational 1xx]
  /**
   * <a href="http://httpstatus.es/100">HTTP_CONTINUE</a>
   */
  const HTTP_CONTINUE = 100;
  /**
   * <a href="http://httpstatus.es/101">HTTP_SWITCHING_PROTOCOLS</a>
   */
  const HTTP_SWITCHING_PROTOCOLS = 101;

  // [Successful 2xx]
  /**
   * <a href="http://httpstatus.es/200">HTTP_OK</a>
   */
  const HTTP_OK = 200;
  /**
   * <a href="http://httpstatus.es/201">HTTP_CREATED</a>
   */
  const HTTP_CREATED = 201;
  /**
   * <a href="http://httpstatus.es/202">HTTP_ACCEPTED</a>
   */
  const HTTP_ACCEPTED = 202;
  /**
   * <a href="http://httpstatus.es/203">HTTP_ACCEPTED</a>
   */
  const HTTP_NONAUTHORITATIVE_INFORMATION = 203;
  /**
   * <a href="http://httpstatus.es/204">HTTP_NO_CONTENT</a>
   */
  const HTTP_NO_CONTENT = 204;
  /**
   * <a href="http://httpstatus.es/205">HTTP_RESET_CONTENT</a>
   */
  const HTTP_RESET_CONTENT = 205;
  /**
   * <a href="http://httpstatus.es/206">HTTP_PARTIAL_CONTENT</a>
   */
  const HTTP_PARTIAL_CONTENT = 206;

  // [Redirection 3xx]
  /**
   * <a href="http://httpstatus.es/300">HTTP_MULTIPLE_CHOICES</a>
   */
  const HTTP_MULTIPLE_CHOICES = 300;
  /**
   * <a href="http://httpstatus.es/301">HTTP_MOVED_PERMANENTLY</a>
   */
  const HTTP_MOVED_PERMANENTLY = 301;
  /**
   * <a href="http://httpstatus.es/302">HTTP_FOUND</a>
   */
  const HTTP_FOUND = 302;
  /**
   * <a href="http://httpstatus.es/303">HTTP_SEE_OTHER</a>
   */
  const HTTP_SEE_OTHER = 303;
  /**
   * <a href="http://httpstatus.es/304">HTTP_NOT_MODIFIED</a>
   */
  const HTTP_NOT_MODIFIED = 304;
  /**
   * <a href="http://httpstatus.es/305">HTTP_USE_PROXY</a>
   */
  const HTTP_USE_PROXY = 305;
  /**
   * <a href="http://httpstatus.es/306">HTTP_UNUSED</a>
   */
  const HTTP_UNUSED = 306;
  /**
   * <a href="http://httpstatus.es/307">HTTP_TEMPORARY_REDIRECT</a>
   */
  const HTTP_TEMPORARY_REDIRECT = 307;

  // [Client Error 4xx]
  /**
   * Defines the beginning of errorCodes
   * @private
   */
  const errorCodesBeginAt = 400;
  /**
   * <a href="http://httpstatus.es/400">HTTP_BAD_REQUEST</a>
   */
  const HTTP_BAD_REQUEST = 400;
  /**
   * <a href="http://httpstatus.es/401">HTTP_UNAUTHORIZED</a>
   */
  const HTTP_UNAUTHORIZED = 401;
  /**
   * <a href="http://httpstatus.es/402">HTTP_PAYMENT_REQUIRED</a>
   */
  const HTTP_PAYMENT_REQUIRED = 402;
  /**
   * <a href="http://httpstatus.es/403">HTTP_FORBIDDEN</a>
   */
  const HTTP_FORBIDDEN = 403;
  /**
   * <a href="http://httpstatus.es/404">HTTP_NOT_FOUND</a>
   */
  const HTTP_NOT_FOUND = 404;
  /**
   * <a href="http://httpstatus.es/405">HTTP_METHOD_NOT_ALLOWED</a>
   */
  const HTTP_METHOD_NOT_ALLOWED = 405;
  /**
   * <a href="http://httpstatus.es/406">HTTP_NOT_ACCEPTABLE</a>
   */
  const HTTP_NOT_ACCEPTABLE = 406;
  /**
   * <a href="http://httpstatus.es/407">HTTP_PROXY_AUTHENTICATION_REQUIRED</a>
   */
  const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
  /**
   * <a href="http://httpstatus.es/408">HTTP_REQUEST_TIMEOUT</a>
   */
  const HTTP_REQUEST_TIMEOUT = 408;
  /**
   * <a href="http://httpstatus.es/409">HTTP_CONFLICT</a>
   */
  const HTTP_CONFLICT = 409;
  /**
   * <a href="http://httpstatus.es/410">HTTP_GONE</a>
   */
  const HTTP_GONE = 410;
  /**
   * <a href="http://httpstatus.es/411">HTTP_LENGTH_REQUIRED</a>
   */
  const HTTP_LENGTH_REQUIRED = 411;
  /**
   * <a href="http://httpstatus.es/412">HTTP_PRECONDITION_FAILED</a>
   */
  const HTTP_PRECONDITION_FAILED = 412;
  /**
   * <a href="http://httpstatus.es/413">HTTP_REQUEST_ENTITY_TOO_LARGE</a>
   */
  const HTTP_REQUEST_ENTITY_TOO_LARGE = 413;
  /**
   * <a href="http://httpstatus.es/414">HTTP_REQUEST_URI_TOO_LONG</a>
   */
  const HTTP_REQUEST_URI_TOO_LONG = 414;
  /**
   * <a href="http://httpstatus.es/415">HTTP_UNSUPPORTED_MEDIA_TYPE</a>
   */
  const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
  /**
   * <a href="http://httpstatus.es/416">HTTP_REQUESTED_RANGE_NOT_SATISFIABLE</a>
   */
  const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
  /**
   * <a href="http://httpstatus.es/417">HTTP_EXPECTATION_FAILED</a>
   */
  const HTTP_EXPECTATION_FAILED = 417;

  // [Server Error 5xx]
  /**
   * <a href="http://httpstatus.es/500">HTTP_INTERNAL_SERVER_ERROR</a>
   */
  const HTTP_INTERNAL_SERVER_ERROR = 500;
  /**
   * <a href="http://httpstatus.es/501">HTTP_NOT_IMPLEMENTED</a>
   */
  const HTTP_NOT_IMPLEMENTED = 501;
  /**
   * <a href="http://httpstatus.es/502">HTTP_BAD_GATEWAY</a>
   */
  const HTTP_BAD_GATEWAY = 502;
  /**
   * <a href="http://httpstatus.es/503">HTTP_SERVICE_UNAVAILABLE</a>
   */
  const HTTP_SERVICE_UNAVAILABLE = 503;
  /**
   * <a href="http://httpstatus.es/504">HTTP_GATEWAY_TIMEOUT</a>
   */
  const HTTP_GATEWAY_TIMEOUT = 504;
  /**
   * <a href="http://httpstatus.es/505">HTTP_VERSION_NOT_SUPPORTED</a>
   */
  const HTTP_VERSION_NOT_SUPPORTED = 505;

  private static $messages =
    array(
	  // [Informational 1xx]
	  100=>'100 Continue',
	  101=>'101 Switching Protocols',

	  // [Successful 2xx]
	  200=>'200 OK',
	  201=>'201 Created',
	  202=>'202 Accepted',
	  203=>'203 Non-Authoritative Information',
	  204=>'204 No Content',
	  205=>'205 Reset Content',
	  206=>'206 Partial Content',

	  // [Redirection 3xx]
	  300=>'300 Multiple Choices',
	  301=>'301 Moved Permanently',
	  302=>'302 Found',
	  303=>'303 See Other',
	  304=>'304 Not Modified',
	  305=>'305 Use Proxy',
	  306=>'306 (Unused)',
	  307=>'307 Temporary Redirect',

	  // [Client Error 4xx]
	  400=>'400 Bad Request',
	  401=>'401 Unauthorized',
	  402=>'402 Payment Required',
	  403=>'403 Forbidden',
	  404=>'404 Not Found',
	  405=>'405 Method Not Allowed',
	  406=>'406 Not Acceptable',
	  407=>'407 Proxy Authentication Required',
	  408=>'408 Request Timeout',
	  409=>'409 Conflict',
	  410=>'410 Gone',
	  411=>'411 Length Required',
	  412=>'412 Precondition Failed',
	  413=>'413 Request Entity Too Large',
	  414=>'414 Request-URI Too Long',
	  415=>'415 Unsupported Media Type',
	  416=>'416 Requested Range Not Satisfiable',
	  417=>'417 Expectation Failed',

	  // [Server Error 5xx]
	  500=>'500 Internal Server Error',
	  501=>'501 Not Implemented',
	  502=>'502 Bad Gateway',
	  503=>'503 Service Unavailable',
	  504=>'504 Gateway Timeout',
	  505=>'505 HTTP Version Not Supported'
	  );

  /**
   * Get the header for the specified code.
   *
   * @param $code Http status code
   * @return A textual representation of the header
   */
  public static function httpHeaderFor($code)
  {
    return 'HTTP/1.1 ' . self::$messages[$code];
  }

  /**
   * Get a canonical status message for the specified code
   *
   * @param $code Http status code
   * @return Text for the specified code
   */
  public static function getMessageForCode($code)
  {
    return self::$messages[$code];
  }

  /**
   * Checks if the specified code is an error code.
   *
   * @param $code Http status code
   * @return bool Answer
   */
  public static function isError($code)
  {
    return is_numeric($code) && $code >= self::HTTP_BAD_REQUEST;
  }

  /**
   * Can the specified status code have a body?
   *
   * @param $code Http status code
   * @return bool Answer
   */
  public static function canHaveBody($code)
  {
    return
      // True if not in 100s
      ($code < self::HTTP_CONTINUE || $code >= self::HTTP_OK)
      && // and not 204 NO CONTENT
      $code != self::HTTP_NO_CONTENT
      && // and not 304 NOT MODIFIED
      $code != self::HTTP_NOT_MODIFIED;
  }

  /**
   * Extract the numeric code from a header
   *
   * @param $header an http top header
   */
  public static function codeFromHeader($header)
  {
    $matches = array();
    preg_match('/HTTP\/\S+\s(\d+)/', $header, $matches);
    if (count($matches) < 1)
      throw new InvalidArgumentException("Not an http header");
    $n = $matches[1];
    return $n;
  }
}
?>