Apache2-API

 view release on metacpan or  search on metacpan

lib/Apache2/API/Status.pm  view on Meta::CPAN

##----------------------------------------------------------------------------
## Apache2 API Framework - ~/lib/Apache2/API/Status.pm
## Version v0.2.1
## Copyright(c) 2025 DEGUEST Pte. Ltd.
## Author: Jacques Deguest <jack@deguest.jp>
## Created 2023/05/30
## Modified 2026/03/22
## All rights reserved
## 
## 
## This program is free software; you can redistribute  it  and/or  modify  it
## under the same terms as Perl itself.
##----------------------------------------------------------------------------
package Apache2::API::Status;
BEGIN
{
    use strict;
    use warnings;
    warnings::register_categories( 'Apache2::API' );
    use parent qw( Module::Generic );
    use vars qw( $CODES $HTTP_CODES $MAP_LANG_SHORT $STATUS_TO_TYPE );
    use Apache2::Const -compile => qw( :http );
    use constant 
    {
        HTTP_CONTINUE                           => 100,
        HTTP_SWITCHING_PROTOCOLS                => 101,
        HTTP_PROCESSING                         => 102,
        HTTP_EARLY_HINTS                        => 103,
        HTTP_OK                                 => 200,
        HTTP_CREATED                            => 201,
        HTTP_ACCEPTED                           => 202,
        HTTP_NON_AUTHORITATIVE                  => 203,
        HTTP_NO_CONTENT                         => 204,
        HTTP_RESET_CONTENT                      => 205,
        HTTP_PARTIAL_CONTENT                    => 206,
        HTTP_MULTI_STATUS                       => 207,
        HTTP_ALREADY_REPORTED                   => 208,
        HTTP_IM_USED                            => 226,
        HTTP_MULTIPLE_CHOICES                   => 300,
        HTTP_MOVED_PERMANENTLY                  => 301,
        HTTP_MOVED_TEMPORARILY                  => 302,
        HTTP_SEE_OTHER                          => 303,
        HTTP_NOT_MODIFIED                       => 304,
        HTTP_USE_PROXY                          => 305,
        HTTP_TEMPORARY_REDIRECT                 => 307,
        HTTP_PERMANENT_REDIRECT                 => 308,
        HTTP_BAD_REQUEST                        => 400,
        HTTP_UNAUTHORIZED                       => 401,
        HTTP_PAYMENT_REQUIRED                   => 402,
        HTTP_FORBIDDEN                          => 403,
        HTTP_NOT_FOUND                          => 404,
        HTTP_METHOD_NOT_ALLOWED                 => 405,
        HTTP_NOT_ACCEPTABLE                     => 406,
        HTTP_PROXY_AUTHENTICATION_REQUIRED      => 407,
        HTTP_REQUEST_TIME_OUT                   => 408,
        HTTP_CONFLICT                           => 409,
        HTTP_GONE                               => 410,
        HTTP_LENGTH_REQUIRED                    => 411,
        HTTP_PRECONDITION_FAILED                => 412,
        HTTP_REQUEST_ENTITY_TOO_LARGE           => 413,
        # Compatibility with HTTP::Status
        HTTP_PAYLOAD_TOO_LARGE                  => 413,
        HTTP_REQUEST_URI_TOO_LARGE              => 414,
        HTTP_URI_TOO_LONG                       => 414,
        HTTP_UNSUPPORTED_MEDIA_TYPE             => 415,
        HTTP_RANGE_NOT_SATISFIABLE              => 416,
        # Compatibility with HTTP::Status
        HTTP_REQUEST_RANGE_NOT_SATISFIABLE      => 416,
        HTTP_EXPECTATION_FAILED                 => 417,
        HTTP_I_AM_A_TEA_POT                     => 418,
        # Compatibility with HTTP::Status
        HTTP_I_AM_A_TEAPOT                      => 418,
        HTTP_MISDIRECTED_REQUEST                => 421,
        HTTP_UNPROCESSABLE_ENTITY               => 422,
        HTTP_LOCKED                             => 423,
        HTTP_FAILED_DEPENDENCY                  => 424,
        HTTP_TOO_EARLY                          => 425,
        # Compatibility with HTTP::Status
        HTTP_NO_CODE                            => 425,
        # Compatibility with HTTP::Status
        HTTP_UNORDERED_COLLECTION               => 425,
        HTTP_UPGRADE_REQUIRED                   => 426,
        HTTP_PRECONDITION_REQUIRED              => 428,
        HTTP_TOO_MANY_REQUESTS                  => 429,
        HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE    => 431,
        HTTP_CONNECTION_CLOSED_WITHOUT_RESPONSE => 444,
        HTTP_UNAVAILABLE_FOR_LEGAL_REASONS      => 451,
        HTTP_CLIENT_CLOSED_REQUEST              => 499,
        HTTP_INTERNAL_SERVER_ERROR              => 500,
        HTTP_NOT_IMPLEMENTED                    => 501,
        HTTP_BAD_GATEWAY                        => 502,
        HTTP_SERVICE_UNAVAILABLE                => 503,
        HTTP_GATEWAY_TIME_OUT                   => 504,
        HTTP_VERSION_NOT_SUPPORTED              => 505,
        HTTP_VARIANT_ALSO_VARIES                => 506,
        HTTP_INSUFFICIENT_STORAGE               => 507,
        HTTP_LOOP_DETECTED                      => 508,
        HTTP_BANDWIDTH_LIMIT_EXCEEDED           => 509,
        HTTP_NOT_EXTENDED                       => 510,
        HTTP_NETWORK_AUTHENTICATION_REQUIRED    => 511,
        HTTP_NETWORK_CONNECT_TIMEOUT_ERROR      => 599,
    };
    our @EXPORT_OK = qw(
        HTTP_ACCEPTED HTTP_ALREADY_REPORTED HTTP_BAD_GATEWAY HTTP_BAD_REQUEST
        HTTP_BANDWIDTH_LIMIT_EXCEEDED HTTP_CLIENT_CLOSED_REQUEST HTTP_CONFLICT
        HTTP_CONNECTION_CLOSED_WITHOUT_RESPONSE HTTP_CONTINUE HTTP_CREATED
        HTTP_EARLY_HINTS HTTP_EXPECTATION_FAILED HTTP_FAILED_DEPENDENCY
        HTTP_FORBIDDEN HTTP_GATEWAY_TIME_OUT HTTP_GONE HTTP_IM_USED
        HTTP_INSUFFICIENT_STORAGE HTTP_INTERNAL_SERVER_ERROR
        HTTP_I_AM_A_TEAPOT HTTP_I_AM_A_TEA_POT HTTP_LENGTH_REQUIRED
        HTTP_LOCKED HTTP_LOOP_DETECTED HTTP_METHOD_NOT_ALLOWED
        HTTP_MISDIRECTED_REQUEST HTTP_MOVED_PERMANENTLY HTTP_MOVED_TEMPORARILY
        HTTP_MULTIPLE_CHOICES HTTP_MULTI_STATUS
        HTTP_NETWORK_AUTHENTICATION_REQUIRED
        HTTP_NETWORK_CONNECT_TIMEOUT_ERROR HTTP_NON_AUTHORITATIVE
        HTTP_NOT_ACCEPTABLE HTTP_NOT_EXTENDED HTTP_NOT_FOUND
        HTTP_NOT_IMPLEMENTED HTTP_NOT_MODIFIED HTTP_NO_CODE HTTP_NO_CONTENT
        HTTP_OK HTTP_PARTIAL_CONTENT HTTP_PAYLOAD_TOO_LARGE
        HTTP_PAYMENT_REQUIRED HTTP_PERMANENT_REDIRECT HTTP_PRECONDITION_FAILED
        HTTP_PRECONDITION_REQUIRED HTTP_PROCESSING
        HTTP_PROXY_AUTHENTICATION_REQUIRED HTTP_RANGE_NOT_SATISFIABLE
        HTTP_REQUEST_ENTITY_TOO_LARGE HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
        HTTP_REQUEST_RANGE_NOT_SATISFIABLE HTTP_REQUEST_TIME_OUT
        HTTP_REQUEST_URI_TOO_LARGE HTTP_RESET_CONTENT HTTP_SEE_OTHER
        HTTP_SERVICE_UNAVAILABLE HTTP_SWITCHING_PROTOCOLS
        HTTP_TEMPORARY_REDIRECT HTTP_TOO_EARLY HTTP_TOO_MANY_REQUESTS
        HTTP_UNAUTHORIZED HTTP_UNAVAILABLE_FOR_LEGAL_REASONS
        HTTP_UNORDERED_COLLECTION HTTP_UNPROCESSABLE_ENTITY
        HTTP_UNSUPPORTED_MEDIA_TYPE HTTP_UPGRADE_REQUIRED HTTP_URI_TOO_LONG
        HTTP_USE_PROXY HTTP_VARIANT_ALSO_VARIES HTTP_VERSION_NOT_SUPPORTED
    );
    our %EXPORT_TAGS = (
        all => [@EXPORT_OK], 
        common  => [qw( HTTP_NETWORK_AUTHENTICATION_REQUIRED HTTP_FORBIDDEN HTTP_NOT_FOUND HTTP_OK HTTP_TEMPORARY_REDIRECT HTTP_INTERNAL_SERVER_ERROR )],
    );
    our $VERSION = 'v0.2.1';
};

use strict;
use warnings;

use utf8;
# Ref:
# <https://datatracker.ietf.org/doc/html/rfc7231#section-8.2>
# <http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml>
our $CODES =
{
# Info 1xx
100 => Apache2::Const::HTTP_CONTINUE,
101 => Apache2::Const::HTTP_SWITCHING_PROTOCOLS,
102 => Apache2::Const::HTTP_PROCESSING,
# Success 2xx
200 => Apache2::Const::HTTP_OK,
201 => Apache2::Const::HTTP_CREATED,
202 => Apache2::Const::HTTP_ACCEPTED,
203 => Apache2::Const::HTTP_NON_AUTHORITATIVE,
204 => Apache2::Const::HTTP_NO_CONTENT,
205 => Apache2::Const::HTTP_RESET_CONTENT,
206 => Apache2::Const::HTTP_PARTIAL_CONTENT,
207 => Apache2::Const::HTTP_MULTI_STATUS,
208 => Apache2::Const::HTTP_ALREADY_REPORTED,
226 => Apache2::Const::HTTP_IM_USED,
# Redirect 3xx
300 => Apache2::Const::HTTP_MULTIPLE_CHOICES,
301 => Apache2::Const::HTTP_MOVED_PERMANENTLY,
302 => Apache2::Const::HTTP_MOVED_TEMPORARILY,
303 => Apache2::Const::HTTP_SEE_OTHER,
304 => Apache2::Const::HTTP_NOT_MODIFIED,
305 => Apache2::Const::HTTP_USE_PROXY,
307 => Apache2::Const::HTTP_TEMPORARY_REDIRECT,
308 => Apache2::Const::HTTP_PERMANENT_REDIRECT,
# Client error 4xx
400 => Apache2::Const::HTTP_BAD_REQUEST,
401 => Apache2::Const::HTTP_UNAUTHORIZED,
402 => Apache2::Const::HTTP_PAYMENT_REQUIRED,
403 => Apache2::Const::HTTP_FORBIDDEN,
404 => Apache2::Const::HTTP_NOT_FOUND,
405 => Apache2::Const::HTTP_METHOD_NOT_ALLOWED,
406 => Apache2::Const::HTTP_NOT_ACCEPTABLE,
407 => Apache2::Const::HTTP_PROXY_AUTHENTICATION_REQUIRED,
408 => Apache2::Const::HTTP_REQUEST_TIME_OUT,
409 => Apache2::Const::HTTP_CONFLICT,
410 => Apache2::Const::HTTP_GONE,
411 => Apache2::Const::HTTP_LENGTH_REQUIRED,
412 => Apache2::Const::HTTP_PRECONDITION_FAILED,
413 => Apache2::Const::HTTP_REQUEST_ENTITY_TOO_LARGE,
414 => Apache2::Const::HTTP_REQUEST_URI_TOO_LARGE,
415 => Apache2::Const::HTTP_UNSUPPORTED_MEDIA_TYPE,
416 => Apache2::Const::HTTP_RANGE_NOT_SATISFIABLE,
417 => Apache2::Const::HTTP_EXPECTATION_FAILED,
# 421 => Apache2::Const::HTTP_MISDIRECTED_REQUEST,
#W WebDAV
422 => Apache2::Const::HTTP_UNPROCESSABLE_ENTITY,
# WebDAV
423 => Apache2::Const::HTTP_LOCKED,
# WebDAV
424 => Apache2::Const::HTTP_FAILED_DEPENDENCY,
426 => Apache2::Const::HTTP_UPGRADE_REQUIRED,
428 => Apache2::Const::HTTP_PRECONDITION_REQUIRED,
429 => Apache2::Const::HTTP_TOO_MANY_REQUESTS,
431 => Apache2::Const::HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE,
# 451 => Apache2::Const::HTTP_UNAVAILABLE_FOR_LEGAL_REASONS,
# Server error 5xx
500 => Apache2::Const::HTTP_INTERNAL_SERVER_ERROR,
501 => Apache2::Const::HTTP_NOT_IMPLEMENTED,
502 => Apache2::Const::HTTP_BAD_GATEWAY,
503 => Apache2::Const::HTTP_SERVICE_UNAVAILABLE,
504 => Apache2::Const::HTTP_GATEWAY_TIME_OUT,
506 => Apache2::Const::HTTP_VARIANT_ALSO_VARIES,
# WebDAV
507 => Apache2::Const::HTTP_INSUFFICIENT_STORAGE,
508 => Apache2::Const::HTTP_LOOP_DETECTED,
510 => Apache2::Const::HTTP_NOT_EXTENDED,
511 => Apache2::Const::HTTP_NETWORK_AUTHENTICATION_REQUIRED,
};

our $HTTP_CODES =
{
    cs_CZ => {
        100 => "Pokračovat",
        101 => "Přepínání protokolů",
        102 => "Zpracovává se",
        103 => "Předběžné pokyny",
        200 => "OK",
        201 => "Vytvořeno",
        202 => "Přijato",
        203 => "Neautoritativní informace",
        204 => "Bez obsahu",
        205 => "Obnovit obsah",
        206 => "Částečný obsah",
        207 => "Více stavů",
        208 => "Již nahlášeno",
        226 => "IM použito",
        300 => "Více možností",
        301 => "Trvale přesunuto!",
        302 => "Dočasně přesunuto!",
        303 => "Viz jiné",
        304 => "Nezměněno",
        305 => "Použít proxy",
        307 => "Dočasné přesměrování!",

lib/Apache2/API/Status.pm  view on Meta::CPAN

See also L<Mozilla documentation on Authorization header|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization>

=head2 HTTP_PAYMENT_REQUIRED (402)

See L<rfc 7231, section 6.5.2|https://tools.ietf.org/html/rfc7231#section-6.5.2>

This was originally designed to inform the client that the resource could only be accessed once payment was made, but is now reserved and its current use is left at the discretion of the site implementing it.

=head2 HTTP_FORBIDDEN (403)

See L<rfc 7231, section 6.5.3|https://tools.ietf.org/html/rfc7231#section-6.5.3>

See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403>

This is returned to indicate the client is barred from accessing the resource.

This is different from C<405 Method Not Allowed>, which is used when the client has proper permission to access the resource, but is using a method not allowed, such as using C<PUT> instead of C<GET> method.

=head2 HTTP_NOT_FOUND (404)

See L<rfc 7231, section 6.5.4|https://tools.ietf.org/html/rfc7231#section-6.5.4>

See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404>

This is returned to indicate the resource does not exist anymore.

=head2 HTTP_METHOD_NOT_ALLOWED (405)

See L<rfc 7231, section 6.5.5|https://tools.ietf.org/html/rfc7231#section-6.5.5>

This is returned to indicate the client it used a L<method|https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods> not allowed, such as using C<PUT> instead of C<GET>. The server can point out the supported methods with the C<Allow> header, such a...

    HTTP/1.1 405 Method Not Allowed
    Content-Type: text/html
    Content-Length: 32
    Allow: GET, HEAD, OPTIONS, PUT

    <h1>405 Try another method!</h1>

=head2 HTTP_NOT_ACCEPTABLE (406)

See L<rfc 7231, section 6.5.6|https://tools.ietf.org/html/rfc7231#section-6.5.6>

This is returned to the client to indicate its requirements are not supported and thus not acceptable. This is in response to L<Accept|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept>, L<Accept-Charset|https://developer.mozilla.org/e...

For example:

    GET /foo HTTP/1.1
    Accept: application/json
    Accept-Language: fr-FR,en-GB;q=0.8,fr;q=0.6,en;q=0.4,ja;q=0.2

    HTTP/1.1 406 Not Acceptable
    Server: Apache/2.4
    Content-Type: text/html

    <h1>Je ne gère pas le type application/json</h1>


Then, the server would response something like:

=head2 HTTP_PROXY_AUTHENTICATION_REQUIRED (407)

See L<rfc 7235, section 3.2 on Authentication|https://tools.ietf.org/html/rfc7235#section-3.2>

This is returned to indicate the proxy used requires authentication. This is similar to the status code C<401 Unauthorized>.

=head2 HTTP_REQUEST_TIME_OUT (408)

See L<rfc 7231, section 6.5.7|https://tools.ietf.org/html/rfc7231#section-6.5.7>

This is returned to indicate the request took too long to be received and timed out. For example:

    HTTP/1.1 408 Request Timeout
    Connection: close
    Content-Type: text/plain
    Content-Length: 19

    Too slow! Try again

=head2 HTTP_CONFLICT (409)

See L<rfc 7231, section 6.5.8|https://tools.ietf.org/html/rfc7231#section-6.5.8>

This is returned to indicate a request conflict with the current state of the target resource, such as uploading with C<PUT> a file older than the remote one.

=head2 HTTP_GONE (410)

See L<rfc 7231, section 6.5.9|https://tools.ietf.org/html/rfc7231#section-6.5.9>

This is returned to indicate that the target resource is gone permanently. The subtle difference with the status code C<404> is that with C<404>, the resource may be only temporally unavailable whereas with C<410>, this is irremediable. For example:

    HTTP/1.1 410 Gone
    Server: Apache/2.4
    Content-Type: text/plain
    Content-Length: 30

    The resource has been removed.

=head2 HTTP_LENGTH_REQUIRED (411)

See L<rfc 7231, section 6.5.10|https://tools.ietf.org/html/rfc7231#section-6.5.10>

This is returned when the C<Content-Length> header was not provided by the client and the server requires it to be present. Most servers can do without.

=head2 HTTP_PRECONDITION_FAILED (412)

See L<rfc 7232 on Conditional Request|https://tools.ietf.org/html/rfc7232>

This is returned when some preconditions set by the client could not be met.

For example:

Issuing a C<PUT> request for a document if it does not already exist.

    PUT /foo/new-article.md HTTP/1.1
    Content-Type: text/markdown
    If-None-Match: *

Update a document if it has not changed since last time (etag)

    PUT /foo/old-article.md HTTP/1.1



( run in 0.455 second using v1.01-cache-2.11-cpan-483215c6ad5 )