| 1 | package Crop::Server::Handler; | ||
| 2 | use base qw/ Crop::Object /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Server::Handler | ||
| 6 | Handler does all the work to generate a correct response for a client request. | ||
| 7 | |||
| 8 | A handler consists of a callback routine and an input driver that checks a correctness | ||
| 9 | of client request parameters. | ||
| 10 | |||
| 11 | In the future a hadler will get an output driver that passthrogh only allowed data to the client. | ||
| 12 | |||
| 13 | May be is a good idea to isolate input driver to an extern class soon. | ||
| 14 | =cut | ||
| 15 | |||
| 16 | use v5.14; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | use Clone qw/ clone /; | ||
| 20 | |||
| 21 | use Crop::Error; | ||
| 22 | |||
| 23 | use Crop::Debug; | ||
| 24 | |||
| 25 | =begin nd | ||
| 26 | Variable: our %Attributes | ||
| 27 | Class attributes: | ||
| 28 | |||
| 29 | call - callback performs general work and place a result to the output flow | ||
| 30 | iflow - copy of input flow | ||
| 31 | input - declaration of client input rules; | ||
| 32 | item 'allow' describes allowed parameters of client request | ||
| 33 | k - current key of a current node in the input driver tree (the key of the 'offer' hash) | ||
| 34 | name - handler name than is unique for the script | ||
| 35 | offer - current hashref of the input | ||
| 36 | offer_stack - parsed input path as a stack of hashrefs from iflow to current, but current excluding; current hash is in the 'offer' attribute | ||
| 37 | page - template name without .ext; special name 'PAGELESS' means no output, only redirect or an error handler is allowed | ||
| 38 | rule - current array of allowed items | ||
| 39 | rule_stack - path of nested arrays of permissions; shrinks and grows synchronously with offer_stack (iflow presentation) | ||
| 40 | v - the value of the current node (value of the 'offer' attribute) | ||
| 41 | =cut | ||
| 42 | our %Attributes = ( | ||
| 43 | call => {mode => 'read', stable => 1}, | ||
| 44 | iflow => undef, | ||
| 45 | input => {stable => 1}, | ||
| 46 | k => undef, | ||
| 47 | name => {mode => 'read', stable => 1}, | ||
| 48 | offer => undef, | ||
| 49 | offer_stack => {default => []}, | ||
| 50 | page => {mode => 'read', stable => 1}, | ||
| 51 | rule => undef, | ||
| 52 | rule_stack => {default => []}, | ||
| 53 | v => undef, | ||
| 54 | ); | ||
| 55 | |||
| 56 | =begin nd | ||
| 57 | Method: checkin ($iflow) | ||
| 58 | Run input driver. | ||
| 59 | |||
| 60 | If input flow contains extra parameters not allowed by rules, than returns error. | ||
| 61 | The check not work if rules are empty. | ||
| 62 | |||
| 63 | Rules are in %{input=>allow}. | ||
| 64 | |||
| 65 | This method go trough two data tree: input flow and driver rules. They have distinct | ||
| 66 | structure. Leading structure is input flow, so bypass starts from it: each of input | ||
| 67 | flow param must must have corresponding item in driver rules. | ||
| 68 | |||
| 69 | Node is key-value pair in hash of input flow. Value can be either simple scalar value or hashref. | ||
| 70 | Is assumed that no empty hash exist in the input flow. | ||
| 71 | |||
| 72 | Parameters: | ||
| 73 | $iflow - input flow | ||
| 74 | |||
| 75 | Returns: | ||
| 76 | true - input flow satisfy rules | ||
| 77 | false - otherwise | ||
| 78 | =cut | ||
| 79 | sub checkin { | ||
| 80 | my ($self, $iflow) = @_; | ||
| 81 | |||
| 82 | # the script dosn't has input restriction, so check is success | ||
| 83 | exists $self->{input}{allow} or return 1; | ||
| 84 | |||
| 85 | $self->{iflow} = clone $iflow; | ||
| 86 | $self->{rule} = $self->{input}{allow}; | ||
| 87 | $self->{rule} = [$self->{rule}] unless ref $self->{rule} eq 'ARRAY'; # for simplified allow=>'param' | ||
| 88 | push @{$self->{rule_stack}}, $self->{rule}; | ||
| 89 | $self->{offer} = $self->{iflow}; | ||
| 90 | |||
| 91 | my $error; | ||
| 92 | NODE: | ||
| 93 | while (my ($k, $v) = $self->_next_node) { | ||
| 94 | # If contains the permissions array an item with current key of iflow? | ||
| 95 | # In no one exists, the check fails. | ||
| 96 | # Then check the value for this key. | ||
| 97 | my $key_index; | ||
| 98 | RULE: | ||
| 99 | while (my ($i, $allow) = each @{$self->{rule}}) { | ||
| 100 | next if ref $allow; | ||
| 101 | |||
| 102 | if ($k eq $allow) { | ||
| 103 | $key_index = $i; | ||
| 104 | keys @{$self->{rule}}; # reset each_iterator for it later use starts at first item | ||
| 105 | last RULE; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | unless (defined $key_index) { | ||
| 109 | $error = "CHECKIN|ERR: Incorrect params: k=$k; v=$v"; | ||
| 110 | last NODE; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $val_index = $key_index + 1; | ||
| 114 | |||
| 115 | if (ref $v eq 'HASH') { | ||
| 116 | # iflow contains inner hash. | ||
| 117 | # The key in last position of rules will arise an error, since simple value is expected but hashref found. | ||
| 118 | # Missing arrayref after the key will arise an error too. | ||
| 119 | if ($val_index == @{$self->{rule}}) { | ||
| 120 | $error = "CHECKIN|ERR: Hash found where value expected: k=$k; v=$v"; | ||
| 121 | last NODE; | ||
| 122 | } | ||
| 123 | |||
| 124 | my $next_rule = $self->{rule}[$val_index]; | ||
| 125 | my $ref_arr = ref $next_rule; | ||
| 126 | |||
| 127 | if (not defined $ref_arr or $ref_arr ne 'ARRAY') { | ||
| 128 | $error = "INPUT: Hash found where value expected: k=$k; v=$v"; | ||
| 129 | last NODE; | ||
| 130 | } | ||
| 131 | |||
| 132 | push @{$self->{rule_stack}}, $self->{rule}; | ||
| 133 | $self->{rule} = $next_rule; | ||
| 134 | next; | ||
| 135 | } elsif (ref $v eq 'ARRAY') { | ||
| 136 | next if $val_index == @{$self->{rule}} or not ref $self->{rule}[$val_index]; | ||
| 137 | |||
| 138 | $error = "INPUT: Value found where hash expected: k=$k; v=$v"; | ||
| 139 | last NODE; | ||
| 140 | |||
| 141 | } else { | ||
| 142 | next if $val_index == @{$self->{rule}} or not ref $self->{rule}[$val_index]; | ||
| 143 | |||
| 144 | $error = "INPUT: Value found where hash expected: k=$k; v=$v"; | ||
| 145 | last NODE; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | $self->Erase; | ||
| 149 | |||
| 150 | $error ? warn $error : 1; | ||
| 151 | } | ||
| 152 | |||
| 153 | =begin nd | ||
| 154 | Method: _next_node ( ) | ||
| 155 | Next step on passing a tree of iflow args. | ||
| 156 | |||
| 157 | Each step returns a key-value pair of current hash. | ||
| 158 | |||
| 159 | Returns empty list at end of iflow. | ||
| 160 | |||
| 161 | This method rely on absence of empty hasses in the iflow. | ||
| 162 | |||
| 163 | Returns: | ||
| 164 | ($k, $v) - item pair of current hash | ||
| 165 | emptylist - at and of iflow | ||
| 166 | =cut | ||
| 167 | sub _next_node { | ||
| 168 | my $self = shift; | ||
| 169 | |||
| 170 | if (ref $self->{v} eq 'HASH') { | ||
| 171 | push @{$self->{offer_stack}}, $self->{offer}; | ||
| 172 | $self->{offer} = $self->{v}; | ||
| 173 | return ($self->{k}, $self->{v}) = each %{$self->{offer}}; # current hash of iflow | ||
| 174 | } else { | ||
| 175 | if (my ($k, $v) = each %{$self->{offer}}) { | ||
| 176 | return ($self->{k}, $self->{v}) = ($k, $v); | ||
| 177 | } else { # end of current hsah | ||
| 178 | while ($self->{offer} = pop @{$self->{offer_stack}}) { | ||
| 179 | $self->{rule} = pop @{$self->{rule_stack}}; | ||
| 180 | if (my ($k, $v) = each %{$self->{offer}}) { | ||
| 181 | return ($self->{k}, $self->{v}) = ($k, $v); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | return (); # end of iflow, end of traverse | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | 1; |