REST/Request.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« REST/Query.pm ↑ Summary REST/Response.pm »
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 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ use parent qw( Apache2::API::Request );
Subroutine BEGIN: called CC=1 SCAR=0.0
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__
« REST/Query.pm ↑ Summary REST/Response.pm »