Apache2-API
view release on metacpan or search on metacpan
lib/Apache2/API.pm view on Meta::CPAN
{
# $r->content_encoding( 'bzip2' );
$self->response->content_encoding( 'bzip2' );
$self->response->headers->set( 'Content-Encoding' => 'bzip2' );
$self->response->headers->merge( 'Vary' => 'Accept-Encoding' );
# $r->send_http_header;
$z->print( $json );
$z->close;
}
elsif( CORE::length( $json ) > $threshold &&
$self->request->accept_encoding =~ /\bdeflate\b/i &&
$self->_load_class( 'IO::Compress::Deflate' ) &&
( $z = IO::Compress::Deflate->new( '-' ) ) )
{
## $r->content_encoding( 'deflate' );
$self->response->content_encoding( 'deflate' );
$self->response->headers->set( 'Content-Encoding' => 'deflate' );
$self->response->headers->merge( 'Vary' => 'Accept-Encoding' );
# $r->send_http_header;
$z->print( $json );
$z->close;
}
else
{
$self->response->headers->unset( 'Content-Encoding' );
# $self->response->content_encoding( undef() );
# $r->send_http_header;
# $r->print( $json );
# $json = Encode::encode_utf8( $json ) if( utf8::is_utf8( $json ) );
# try-catch
local $@;
eval
{
my $bytes = $r->print( $json );
};
if( $@ )
{
}
}
# $r->rflush;
# Flush any buffered data to the client using Apache2::RequestIO
$self->response->rflush;
return( $self );
}
# push_handlers($hook_name => \&handler);
# push_handlers($hook_name => [\&handler, \&handler2]);
sub push_handlers { return( shift->_try( 'server', 'push_handlers', @_ ) ); }
# See also <https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/406>
sub reply
{
my $self = shift( @_ );
my( $code, $ref );
my $use_rfc_error = $self->{use_rfc_error} // $USE_RFC_ERROR;
# rfc9457 standard for REST API error response: <https://www.rfc-editor.org/rfc/rfc9457.html>
# Legacy JSON payload like Google, Twitter, Facebook
# Modern REST APIs now uses rfc9457 with a flattened payload.
# When the use_rfc_error object property is true, we use rfc9457 flattened error, this will produce something like:
# {
# error => 'not_found',
# status => 404,
# title => 'Not found!',
# detail => q{The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.},
# locale => 'en-US',
# type => 'https://api.example.com/problems/not-found',
# }
# otherwise, the legacy approach would be:
# {
# error =>
# {
# code => 404,
# message => q{The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.},
# },
# locale => 'en-US',
# }
# $self->reply( Apache2::Const::HTTP_OK, { message => "All is well" } );
if( scalar( @_ ) == 2 )
{
( $code, $ref ) = @_;
}
elsif( scalar( @_ ) == 1 &&
$self->_can( $_[0] => 'code' ) &&
$self->_can( $_[0] => 'message' ) )
{
my $ex = shift( @_ );
$code = $ex->code;
$ref =
{
message => $ex->message,
( $ex->can( 'public_message' ) ? ( public_message => $ex->public_message ) : () ),
( $ex->can( 'locale' ) ? ( locale => $ex->locale ) : () ),
};
}
# $self->reply({ code => Apache2::Const::HTTP_OK, message => "All is well" } );
elsif( ref( $_[0] ) eq 'HASH' )
{
$ref = shift( @_ );
$code = $ref->{code} if( length( $ref->{code} ) );
}
my $r = $self->apache_request;
my $req = $self->request;
my $resp = $self->response;
# Guardrails on inputs
if( !defined( $code ) || $code !~ /^[0-9]{3}$/ )
{
$resp->code( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR );
$resp->rflush;
$resp->print( $self->json->utf8->encode({ error => 'An unexpected server error occured', code => 500 }) );
$self->error( "http code to be used '", ( $code // 'undef' ), "' is invalid. It should be only integers." );
return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR );
}
if( ref( $ref ) ne 'HASH' )
{
$resp->code( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR );
$resp->rflush;
# $r->send_http_header;
$resp->print( $self->json->utf8->encode({ error => 'An unexpected server error occured', code => 500 }) );
$self->error( "Data provided to send is not a hash ref." );
return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR );
( run in 0.413 second using v1.01-cache-2.11-cpan-13bb782fe5a )