| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||
| 2 | ## Apache2 Server Side Include Parser - ~/lib/Apache2/SSI/Notes.pm | ||||||||||||||||||||
| 3 | ## Version v0.1.3 | ||||||||||||||||||||
| 4 | ## Copyright(c) 2024 DEGUEST Pte. Ltd. | ||||||||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||||||||
| 6 | ## Created 2021/01/18 | ||||||||||||||||||||
| 7 | ## Modified 2025/03/22 | ||||||||||||||||||||
| 8 | ## All rights reserved | ||||||||||||||||||||
| 9 | ## | ||||||||||||||||||||
| 10 | ## This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 11 | ## under the same terms as Perl itself. | ||||||||||||||||||||
| 12 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||
| 13 | package Apache2::SSI::Notes; | ||||||||||||||||||||
| 14 | BEGIN | ||||||||||||||||||||
| 15 | 9 | ▶ | { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 9 | ▶ | use warnings::register; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 9 | ▶ | use parent qw( Module::Generic ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 9 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | # 512Kb | ||||||||||||||||||||
| 22 | 9 | ▶ | use constant MAX_SIZE => 524288; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 9 | ▶ | use Apache2::SSI::SharedMem ':all'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 9 | our $VERSION = 'v0.1.3'; | |||||||||||||||||||
| 25 | }; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | sub init | ||||||||||||||||||||
| 31 | 32 | ▶ | { | ||||||||||||||||||
|
Subroutine
init: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||
| 32 | 32 | my $self = shift( @_ ); | |||||||||||||||||||
| 33 | 32 | $self->{key} = 'ap2_ssi_notes'; | |||||||||||||||||||
| 34 | 32 | $self->{size} = MAX_SIZE; | |||||||||||||||||||
| 35 | 32 | $self->{_init_strict_use_sub} = 1; | |||||||||||||||||||
| 36 | 32 | $self->SUPER::init( @_ ); | |||||||||||||||||||
| 37 | 32 | ▶ | return( $self->error( "Notes under this system $^O are unsupported." ) ) if( !Apache2::SSI::SharedMem->supported ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | my $mem = Apache2::SSI::SharedMem->new( | ||||||||||||||||||||
| 39 | key => ( length( $self->{key} ) ? $self->{key} : 'ap2_ssi_notes' ), | ||||||||||||||||||||
| 40 | # 512 Kb max | ||||||||||||||||||||
| 41 | size => $self->{size}, | ||||||||||||||||||||
| 42 | # Create if necessary | ||||||||||||||||||||
| 43 | 32 | ▶ | create => 1, | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
'Apache2::SSI::SharedMem'->new('key', length $self->{'key'} ? $self->{'key'} : 'ap2_ssi_notes', 'size', $self->{'size'}, 'create', 1, 'debug', $self->debug) || (return $self->pass_error('Apache2::SSI::SharedMem'->error))
MC/DC0%0/1
'Apache2::SSI::SharedMem'->new('key', length $self->{'key'} ? $self->{'key'} : 'ap2_ssi_notes', 'size', $self->{'size'}, 'create', 1, 'debug', $self->debug) || (return $self->pass_error('Apache2::SSI::SharedMem'->error))
'Apache2::SSI::SharedMem'->new('key', length $self->{'key'} ? $self->{'key'} : 'ap2_ssi_notes', 'size', $self->{'size'}, 'create', 1, 'debug', $self->debug)
Truth table50%1/2
A
A= 'Apache2::SSI::SharedMem'->new('key', length $self->{'key'} ? $self->{'key'} : 'ap2_ssi_notes', 'size', $self->{'size'}, 'create', 1, 'debug', $self->debug)
| |||||||||||||||||||||
| 44 | debug => $self->debug, | ||||||||||||||||||||
| 45 | ) || return( $self->pass_error( Apache2::SSI::SharedMem->error ) ); | ||||||||||||||||||||
| 46 | 32 | ▶ | my $shem = $mem->open || return( $self->pass_error( $mem->error ) ); | ||||||||||||||||||
|
Condition50%1/2
$mem->open || (return $self->pass_error($mem->error))
MC/DC0%0/1
$mem->open || (return $self->pass_error($mem->error))
$mem->open
Truth table50%1/2
A
A= $mem->open
| |||||||||||||||||||||
| 47 | 32 | $self->shem( $shem ); | |||||||||||||||||||
| 48 | 32 | return( $self ); | |||||||||||||||||||
| 49 | }; | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | ▶ | sub add { return( shift->set( @_ ) ); } | ||||||||||||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | sub clear | ||||||||||||||||||||
| 54 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
clear: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 55 | 0 | my $self = shift( @_ ); | |||||||||||||||||||
| 56 | 0 | my $data = {}; | |||||||||||||||||||
| 57 | 0 | ▶ | $self->write_mem( $data ) || return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 0 | return( $self ); | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | sub do | ||||||||||||||||||||
| 62 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
do: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 63 | 0 | my $self = shift( @_ ); | |||||||||||||||||||
| 64 | 0 | my $code = shift( @_ ); | |||||||||||||||||||
| 65 | 0 | my @keys = @_; | |||||||||||||||||||
| 66 | 0 | ▶ | return( $self->error( "Code provided ($code) is not actually a code reference." ) ) if( ref( $code ) ne 'CODE' ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | ▶ | my $data = $self->read_mem || return; | ||||||||||||||||||
|
Condition0%0/2
$self->read_mem || (return)
MC/DC0%0/1
$self->read_mem || (return)
$self->read_mem
Truth table0%0/2
A
A= $self->read_mem
| |||||||||||||||||||||
| 68 | 0 | ▶ | @keys = sort( keys( %$data ) ) unless( scalar( @keys ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | local $@; | |||||||||||||||||||
| 70 | 0 | foreach my $k ( @keys ) | |||||||||||||||||||
| 71 | { | ||||||||||||||||||||
| 72 | 0 | my $k_orig = $k; | |||||||||||||||||||
| 73 | 0 | my $v = $data->{ $k }; | |||||||||||||||||||
| 74 | # try-catch | ||||||||||||||||||||
| 75 | eval | ||||||||||||||||||||
| 76 | 0 | { | |||||||||||||||||||
| 77 | # Code can modify values in-place like: | ||||||||||||||||||||
| 78 | # sub | ||||||||||||||||||||
| 79 | # { | ||||||||||||||||||||
| 80 | # $_[1] = 'new value' if( $_[0] eq 'some_key_name' ); | ||||||||||||||||||||
| 81 | # } | ||||||||||||||||||||
| 82 | 0 | $code->( $k, $v ); | |||||||||||||||||||
| 83 | # Store possibly updated value | ||||||||||||||||||||
| 84 | 0 | $data->{ $k_orig } = $v; | |||||||||||||||||||
| 85 | }; | ||||||||||||||||||||
| 86 | 0 | ▶ | if( $@ ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | { | ||||||||||||||||||||
| 88 | 0 | return( $self->error( "Callback died with error: $@" ) ); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | # No need to bother if there was no keys in the first place | ||||||||||||||||||||
| 92 | 0 | ▶ | if( scalar( @keys ) ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | { | ||||||||||||||||||||
| 94 | 0 | ▶ | $self->write_mem( $data ) || return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | 0 | return( $self ); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | sub get | ||||||||||||||||||||
| 100 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
get: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 101 | 0 | my $self = shift( @_ ); | |||||||||||||||||||
| 102 | 0 | my $key; | |||||||||||||||||||
| 103 | 0 | ▶ | if( @_ ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | { | ||||||||||||||||||||
| 105 | 0 | $key = shift( @_ ); | |||||||||||||||||||
| 106 | 0 | ▶ | return( $self->error( "Key provided to retrieve is empty." ) ) if( !length( $key ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | 0 | ▶ | my $data = $self->read_mem || return; | ||||||||||||||||||
|
Condition0%0/2
$self->read_mem || (return)
MC/DC0%0/1
$self->read_mem || (return)
$self->read_mem
Truth table0%0/2
A
A= $self->read_mem
| |||||||||||||||||||||
| 109 | # As it is the case for the first time, before any write | ||||||||||||||||||||
| 110 | 0 | ▶ | $data = {} if( !ref( $data ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 0 | ▶ | return( $data ) if( !defined( $key ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 0 | return( $data->{ $key } ); | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 0 | ▶ | sub key { return( shift->_set_get_scalar( 'key', @_ ) ); } | ||||||||||||||||||
|
Subroutine
key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | sub read_mem | ||||||||||||||||||||
| 118 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
read_mem: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 119 | 0 | my $self = shift( @_ ); | |||||||||||||||||||
| 120 | 0 | ▶ | my $shem = $self->shem || | ||||||||||||||||||
|
Condition0%0/2
$self->shem || (return $self->error('Oh no, the shared memory object is gone! That should not happen.'))
MC/DC0%0/1
$self->shem || (return $self->error('Oh no, the shared memory object is gone! That should not happen.'))
$self->shem
Truth table0%0/2
A
A= $self->shem
| |||||||||||||||||||||
| 121 | return( $self->error( "Oh no, the shared memory object is gone! That should not happen." ) ); | ||||||||||||||||||||
| 122 | 0 | my $data; | |||||||||||||||||||
| 123 | 0 | my $len = $shem->read( $data ); | |||||||||||||||||||
| 124 | 0 | ▶ | return( $self->pass_error( $shem->error ) ) if( !defined( $len ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 0 | ▶ | $data = {} unless( ref( $data ) eq 'HASH' ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 0 | return( $data ); | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | sub remove | ||||||||||||||||||||
| 130 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
remove: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 131 | 0 | my $self = shift( @_ ); | |||||||||||||||||||
| 132 | 0 | ▶ | my $shem = $self->shem || | ||||||||||||||||||
|
Condition0%0/2
$self->shem || (return $self->error('Oh no, the shared memory object is gone! That should not happen.'))
MC/DC0%0/1
$self->shem || (return $self->error('Oh no, the shared memory object is gone! That should not happen.'))
$self->shem
Truth table0%0/2
A
A= $self->shem
| |||||||||||||||||||||
| 133 | return( $self->error( "Oh no, the shared memory object is gone! That should not happen." ) ); | ||||||||||||||||||||
| 134 | 0 | my $rv; | |||||||||||||||||||
| 135 | 0 | ▶ | if( !defined( $rv = $shem->remove ) ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | { | ||||||||||||||||||||
| 137 | 0 | return( $self->pass_error( $shem->error ) ); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | 0 | return( $rv ); | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | sub set | ||||||||||||||||||||
| 143 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
set: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 144 | 0 | my $self = shift( @_ ); | |||||||||||||||||||
| 145 | 0 | ▶ | my $data = $self->read_mem || return; | ||||||||||||||||||
|
Condition0%0/2
$self->read_mem || (return)
MC/DC0%0/1
$self->read_mem || (return)
$self->read_mem
Truth table0%0/2
A
A= $self->read_mem
| |||||||||||||||||||||
| 146 | 0 | my @callinfo = caller; | |||||||||||||||||||
| 147 | 0 | my( $key, $value ) = @_; | |||||||||||||||||||
| 148 | 0 | ▶ | return( $self->error( "Key provided to set value is empty." ) ) if( !length( $key ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | 0 | $data->{ $key } = $value; | |||||||||||||||||||
| 150 | 0 | ▶ | $self->write_mem( $data ) || return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 0 | return( $self ); | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 32 | ▶ | sub shem { return( shift->_set_get_object_without_init( 'shem', 'Apache2::SSI::SharedMem', @_ ) ); } | ||||||||||||||||||
|
Subroutine
shem: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 0 | ▶ | sub size { return( shift->_set_get_scalar( 'size', @_ ) ); } | ||||||||||||||||||
|
Subroutine
size: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 32 | ▶ | sub supported { return( Apache2::SSI::SharedMem->supported ); } | ||||||||||||||||||
|
Subroutine
supported: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | sub unset | ||||||||||||||||||||
| 161 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
unset: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 162 | 0 | my $self = shift( @_ ); | |||||||||||||||||||
| 163 | 0 | my $key = shift( @_ ); | |||||||||||||||||||
| 164 | 0 | ▶ | return( $self->error( "Key provided to unset value is empty." ) ) if( !length( $key ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | 0 | ▶ | my $data = $self->read_mem || return; | ||||||||||||||||||
|
Condition0%0/2
$self->read_mem || (return)
MC/DC0%0/1
$self->read_mem || (return)
$self->read_mem
Truth table0%0/2
A
A= $self->read_mem
| |||||||||||||||||||||
| 166 | 0 | delete( $data->{ $key } ); | |||||||||||||||||||
| 167 | 0 | ▶ | $self->write_mem( $data ) || return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | 0 | return( $self ); | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | sub write_mem | ||||||||||||||||||||
| 172 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
write_mem: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 173 | 0 | my $self = shift( @_ ); | |||||||||||||||||||
| 174 | 0 | ▶ | my $shem = $self->shem || | ||||||||||||||||||
|
Condition0%0/2
$self->shem || (return $self->error('Oh no, the shared memory object is gone! That should not happen.'))
MC/DC0%0/1
$self->shem || (return $self->error('Oh no, the shared memory object is gone! That should not happen.'))
$self->shem
Truth table0%0/2
A
A= $self->shem
| |||||||||||||||||||||
| 175 | return( $self->error( "Oh no, the shared memory object is gone! That should not happen." ) ); | ||||||||||||||||||||
| 176 | 0 | my $data = shift( @_ ); | |||||||||||||||||||
| 177 | 0 | ▶ | return( $self->error( "I was expecting an hash reference and got instead '$data'" ) ) if( ref( $data ) ne 'HASH' ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | 0 | ▶ | if( !defined( $shem->lock( ( LOCK_EX | LOCK_NB ) ) ) ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | { | ||||||||||||||||||||
| 180 | 0 | return( $self->pass_error( $shem->error ) ); | |||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | 0 | my $rc = $shem->write( $data ); | |||||||||||||||||||
| 183 | 0 | $shem->unlock; | |||||||||||||||||||
| 184 | 0 | ▶ | return( $self->pass_error( $shem->error ) ) if( !defined( $rc ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | 0 | return( $self ); | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 9 | 1; | |||||||||||||||||||
| 189 | # NOTE: POD | ||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||