| 1 | # -*- perl -*- | ||
| 2 | ##---------------------------------------------------------------------------- | ||
| 3 | ## REST API Framework - ~/lib/Net/API/REST/Request.pm | ||
| 4 | ## Version v1.1.0 | ||
| 5 | ## Copyright(c) 2023 DEGUEST Pte. Ltd. | ||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 7 | ## Created 2019/09/01 | ||
| 8 | ## Modified 2023/11/19 | ||
| 9 | ## All rights reserved | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package Net::API::REST::Request; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use parent qw( Apache2::API::Request ); | ||
| 20 | use vars qw( $ERROR $VERSION $SERVER_VERSION ); | ||
| 21 | use common::sense; | ||
| 22 | use utf8 (); | ||
| 23 | use version; | ||
| 24 | use Net::API::REST::Cookies; | ||
| 25 | use Net::API::REST::DateTime; | ||
| 26 | use Net::API::REST::Query; | ||
| 27 | use Net::API::REST::Status; | ||
| 28 | our $VERSION = 'v1.1.0'; | ||
| 29 | our( $SERVER_VERSION, $ERROR ); | ||
| 30 | }; | ||
| 31 | |||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | |||
| 35 | # init() is inherited | ||
| 36 | |||
| 37 | sub reply | ||
| 38 | { | ||
| 39 | my $self = shift( @_ ); | ||
| 40 | my $code = shift( @_ ); | ||
| 41 | my $ref = shift( @_ ); | ||
| 42 | my $r = $self->request; | ||
| 43 | my( $call_pack, $call_file, $call_line ) = caller; | ||
| 44 | my $call_sub = ( caller(1) )[3]; | ||
| 45 | if( $code !~ /^[0-9]+$/ ) | ||
| 46 | { | ||
| 47 | #$r->custom_response( Apache2::Const::SERVER_ERROR, "Was expecting an organisation id" ); | ||
| 48 | $r->status( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR ); | ||
| 49 | $r->rflush; | ||
| 50 | # $r->send_http_header; | ||
| 51 | $r->print( $self->json->encode({ 'error' => 'An unexpected server error occured', 'code' => 500 }) ); | ||
| 52 | $self->error( "http code to be used '$code' is invalid. It should be only integers." ); | ||
| 53 | return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR ); | ||
| 54 | } | ||
| 55 | if( ref( $ref ) ne 'HASH' ) | ||
| 56 | { | ||
| 57 | $r->status( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR ); | ||
| 58 | $r->rflush; | ||
| 59 | # $r->send_http_header; | ||
| 60 | $r->print( $self->json->encode({ 'error' => 'An unexpected server error occured', 'code' => 500 }) ); | ||
| 61 | $self->error( "Data provided to send is not an hash ref." ); | ||
| 62 | return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR ); | ||
| 63 | } | ||
| 64 | my $msg = CORE::exists( $ref->{success} ) | ||
| 65 | ? $ref->{ 'success' } | ||
| 66 | : CORE::exists( $ref->{error} ) | ||
| 67 | ? $ref->{ 'error' } | ||
| 68 | : undef(); | ||
| 69 | $r->status( $code ); | ||
| 70 | if( defined( $msg ) ) | ||
| 71 | { | ||
| 72 | $r->custom_response( $code, $msg ); | ||
| 73 | } | ||
| 74 | else | ||
| 75 | { | ||
| 76 | $r->status( $code ); | ||
| 77 | } | ||
| 78 | $r->rflush; | ||
| 79 | $ref->{code} = $code if( !CORE::exists( $ref->{code} ) ); | ||
| 80 | # try-catch | ||
| 81 | local $@; | ||
| 82 | my $json = eval | ||
| 83 | { | ||
| 84 | $self->json->encode( $ref ); | ||
| 85 | }; | ||
| 86 | if( $@ ) | ||
| 87 | { | ||
| 88 | $self->error( "An error occurred while encoding data into JSON: $@" ); | ||
| 89 | return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR ); | ||
| 90 | } | ||
| 91 | |||
| 92 | # try-catch | ||
| 93 | eval | ||
| 94 | { | ||
| 95 | $r->print( $json ); | ||
| 96 | }; | ||
| 97 | if( $@ ) | ||
| 98 | { | ||
| 99 | $self->error( "An error occurred while calling Apache Request method \"print\": $@" ); | ||
| 100 | return( Apache2::Const::HTTP_INTERNAL_SERVER_ERROR ); | ||
| 101 | } | ||
| 102 | return( $code ); | ||
| 103 | } | ||
| 104 | |||
| 105 | # sub variables { return( shift->_set_get_object_without_init( 'variables', 'Net::API::REST::Endpoint::Variables', @_ ) ); } | ||
| 106 | sub variables { return( shift->_set_get_hash_as_mix_object( 'variables', @_ ) ); } | ||
| 107 | |||
| 108 | # Taken from http://www.perlmonks.org/bare/?node_id=319761 | ||
| 109 | # This will do a split on a semi-colon, but being mindful if before it there is an escaped backslash | ||
| 110 | # For example, this would not be skipped: something\;here | ||
| 111 | # But this would be split: something\\;here resulting in something\ and here after unescaping | ||
| 112 | sub _split_str | ||
| 113 | { | ||
| 114 | my $self = shift( @_ ); | ||
| 115 | my $s = shift( @_ ); | ||
| 116 | return( {} ) if( !CORE::length( $s ) ); | ||
| 117 | my $sep = @_ ? shift( @_ ) : ';'; | ||
| 118 | my @parts = (); | ||
| 119 | my $i = 0; | ||
| 120 | foreach( split( /(\\.)|$sep/, $s ) ) | ||
| 121 | { | ||
| 122 | defined( $_ ) ? do{ $parts[$i] .= $_ } : do{ $i++ }; | ||
| 123 | } | ||
| 124 | my $header_val = shift( @parts ); | ||
| 125 | my $param = {}; | ||
| 126 | foreach my $frag ( @parts ) | ||
| 127 | { | ||
| 128 | $frag =~ s/^[[:blank:]]+|[[:blank:]]+$//g; | ||
| 129 | my( $attribute, $value ) = split( /[[:blank:]]*\=[[:blank:]]*/, $frag, 2 ); | ||
| 130 | $value =~ s/^\"|\"$//g; | ||
| 131 | ## Check character string and length. Should not be more than 255 characters | ||
| 132 | ## http://tools.ietf.org/html/rfc1341 | ||
| 133 | ## http://www.iana.org/assignments/media-types/media-types.xhtml | ||
| 134 | ## Won't complain if this does not meet our requirement, but will discard it silently | ||
| 135 | if( $attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ && CORE::length( $attribute ) <= 255 ) | ||
| 136 | { | ||
| 137 | if( $value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ && CORE::length( $value ) <= 255 ) | ||
| 138 | { | ||
| 139 | $param->{ lc( $attribute ) } = $value; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | return( { 'value' => $header_val, 'param' => $param } ); | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | # NOTE: POD | ||
| 148 | __END__ | ||
| 149 | |||
| 150 | =encoding utf8 | ||
| 151 | |||
| 152 | =head1 NAME | ||
| 153 | |||
| 154 | Net::API::REST::Request - Apache2 Incoming Request Access and Manipulation | ||
| 155 | |||
| 156 | =head1 SYNOPSIS | ||
| 157 | |||
| 158 | use Net::API::REST::Request; | ||
| 159 | ## $r is the Apache2::RequestRec object | ||
| 160 | my $req = Net::API::REST::Request->new( request => $r, debug => 1 ); | ||
| 161 | ## or, to test it outside of a modperl environment: | ||
| 162 | my $req = Net::API::REST::Request->new( request => $r, debug => 1, checkonly => 1 ); | ||
| 163 | |||
| 164 | =head1 VERSION | ||
| 165 | |||
| 166 | v1.1.0 | ||
| 167 | |||
| 168 | =head1 DESCRIPTION | ||
| 169 | |||
| 170 | The purpose of this module is to provide an easy access to various method to process and manipulate incoming request. | ||
| 171 | |||
| 172 | This module inherits all of its methods from L<Apache2::API::Request>. Please check its documentation directly. | ||
| 173 | |||
| 174 | For its alter ego to manipulate outgoing http response, use the L<Net::API::REST::Response> module. | ||
| 175 | |||
| 176 | =for Pod::Coverage reply | ||
| 177 | |||
| 178 | =for Pod::Coverage variables | ||
| 179 | |||
| 180 | =head1 AUTHOR | ||
| 181 | |||
| 182 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 183 | |||
| 184 | L<Apache2::API::Request>, L<Apache2::API::Response>, L<Apache2::API> | ||
| 185 | |||
| 186 | =head1 COPYRIGHT & LICENSE | ||
| 187 | |||
| 188 | Copyright (c) 2018-2023 DEGUEST Pte. Ltd. | ||
| 189 | |||
| 190 | You can use, copy, modify and redistribute this package and associated | ||
| 191 | files under the same terms as Perl itself. | ||
| 192 | |||
| 193 | =cut |