| 1 | package Net::DAV::Lock; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 31 | ▶ | use Net::DAV::UUID; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 31 | our $MAX_LOCK_TIMEOUT = 15 * 60; | |||||||||||||
| 6 | 31 | our $DEFAULT_LOCK_TIMEOUT = $MAX_LOCK_TIMEOUT; | |||||||||||||
| 7 | 31 | our $DEFAULT_DEPTH = 'infinity'; # as per RFC 4918, section 9.10.3, paragraph 5 | |||||||||||||
| 8 | 31 | our $DEFAULT_SCOPE = 'exclusive'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 31 | our $VERSION = '1.304'; | |||||||||||||
| 11 | 31 | $VERSION = eval $VERSION; | |||||||||||||
| 12 | |||||||||||||||
| 13 | sub new { | ||||||||||||||
| 14 | 110 | ▶ | my ($class, $hash) = @_; | ||||||||||||
|
Subroutine
new: called CC=18 SCAR=28.9
Pod
undocumented
| |||||||||||||||
| 15 | 110 | my $obj = {}; | |||||||||||||
| 16 | |||||||||||||||
| 17 | 110 | my $now = time(); | |||||||||||||
| 18 | |||||||||||||||
| 19 | 110 | ▶ | die('Missing path value') unless defined $hash->{'path'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 20 | 108 | $obj->{'path'} = $hash->{'path'}; | |||||||||||||
| 21 | |||||||||||||||
| 22 | 108 | ▶ | die('Missing creator value') unless defined $hash->{'creator'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | 107 | ▶ | die('Owner contains invalid characters') unless $hash->{'creator'} =~ /^[a-z_.][-a-z0-9_.]*$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | 106 | $obj->{'creator'} = $hash->{'creator'}; | |||||||||||||
| 25 | 106 | ▶ | die('Missing owner value') unless defined $hash->{'owner'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 105 | $obj->{'owner'} = $hash->{'owner'}; | |||||||||||||
| 27 | |||||||||||||||
| 28 | 105 | ▶ | if (defined $hash->{'expiry'}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 29 | 28 | ▶ | die('Lock expiry is a date in the past') if $hash->{'expiry'} < $now; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 27 | ▶ | if ($hash->{'expiry'} - $now > $MAX_LOCK_TIMEOUT) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | 9 | $obj->{'expiry'} = $now + $MAX_LOCK_TIMEOUT; | |||||||||||||
| 32 | } | ||||||||||||||
| 33 | else { | ||||||||||||||
| 34 | 18 | $obj->{'expiry'} = $hash->{'expiry'}; | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | } elsif (defined $hash->{'timeout'}) { | ||||||||||||||
| 37 | 2 | ▶ | if ($hash->{'timeout'} > $MAX_LOCK_TIMEOUT) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | 1 | $obj->{'expiry'} = $now + $MAX_LOCK_TIMEOUT; | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | else { | ||||||||||||||
| 41 | 1 | $obj->{'expiry'} = $now + $hash->{'timeout'}; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | } else { | ||||||||||||||
| 44 | 75 | $obj->{'expiry'} = $now + $DEFAULT_LOCK_TIMEOUT; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 104 | ▶ | if (defined $hash->{'depth'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | 52 | ▶ | die('Depth is a non-RFC 4918 value') unless $hash->{'depth'} =~ /^(?:0|infinity)$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 51 | $obj->{'depth'} = $hash->{'depth'}; | |||||||||||||
| 50 | } else { | ||||||||||||||
| 51 | 52 | $obj->{'depth'} = $DEFAULT_DEPTH; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 103 | ▶ | if (defined $hash->{'scope'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 47 | ▶ | die('Scope is an unsupported value') unless $hash->{'scope'} eq 'exclusive'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 46 | $obj->{'scope'} = $hash->{'scope'}; | |||||||||||||
| 57 | } else { | ||||||||||||||
| 58 | 56 | $obj->{'scope'} = $DEFAULT_SCOPE; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 102 | $obj->{'uri'} = $hash->{'uri'}; | |||||||||||||
| 62 | |||||||||||||||
| 63 | # | ||||||||||||||
| 64 | # Calculate and store a new UUID based on the path and owner | ||||||||||||||
| 65 | # specified, if none is present. | ||||||||||||||
| 66 | # | ||||||||||||||
| 67 | 102 | ▶ | if ($hash->{'uuid'}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 68 | 2 | ▶ | unless ($hash->{'uuid'} =~ /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 1 | die('UUID is of an invalid format'); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 1 | $obj->{'uuid'} = $hash->{'uuid'}; | |||||||||||||
| 73 | } elsif ($hash->{'token'}) { | ||||||||||||||
| 74 | 3 | ▶ | unless ($hash->{'token'} =~ /^opaquelocktoken:[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | 2 | die('Token is not a UUID prefixed with the opaquelocktoken: URI namespace'); | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 1 | my $uuid = $hash->{'token'}; | |||||||||||||
| 79 | 1 | $uuid =~ s/^opaquelocktoken://; | |||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | $obj->{'uuid'} = $uuid; | |||||||||||||
| 82 | } else { | ||||||||||||||
| 83 | 97 | $obj->{'uuid'} = Net::DAV::UUID::generate(@{$hash}{qw/path owner/}); | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 99 | return bless $obj, $class; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | # | ||||||||||||||
| 90 | # Provide a separate constructor for reanimating values from the database, | ||||||||||||||
| 91 | # especially when the validation within the normal constructor would be | ||||||||||||||
| 92 | # considered undesired behavior. | ||||||||||||||
| 93 | # | ||||||||||||||
| 94 | # Results in a simple copy of the database row into a blessed object. | ||||||||||||||
| 95 | # | ||||||||||||||
| 96 | sub reanimate { | ||||||||||||||
| 97 | 10 | ▶ | my ($class, $row) = @_; | ||||||||||||
|
Subroutine
reanimate: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 98 | |||||||||||||||
| 99 | 10 | bless { %$row }, $class; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | 125 | ▶ | sub expiry { shift->{'expiry'} }; | ||||||||||||
|
Subroutine
expiry: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 103 | 61 | ▶ | sub creator { shift->{'creator'} }; | ||||||||||||
|
Subroutine
creator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 104 | 13 | ▶ | sub owner { shift->{'owner'} }; | ||||||||||||
|
Subroutine
owner: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 105 | 88 | ▶ | sub depth { shift->{'depth'} }; | ||||||||||||
|
Subroutine
depth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 106 | 14 | ▶ | sub scope { shift->{'scope'} }; | ||||||||||||
|
Subroutine
scope: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 107 | 501 | ▶ | sub path { shift->{'path'} }; | ||||||||||||
|
Subroutine
path: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 108 | 137 | ▶ | sub uuid { shift->{'uuid'} }; | ||||||||||||
|
Subroutine
uuid: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 109 | |||||||||||||||
| 110 | # | ||||||||||||||
| 111 | # Return the number of seconds remaining for which this lock is | ||||||||||||||
| 112 | # valid, relative to the current system time. | ||||||||||||||
| 113 | # | ||||||||||||||
| 114 | sub timeout { | ||||||||||||||
| 115 | 37 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
timeout: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||
| 116 | |||||||||||||||
| 117 | 37 | my $left = $self->{'expiry'} - time(); | |||||||||||||
| 118 | |||||||||||||||
| 119 | 37 | ▶ | return $left >= 0? $left: 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | # | ||||||||||||||
| 123 | # Provide a wrapper method to return a token URI based on the UUID | ||||||||||||||
| 124 | # stored in the current object. | ||||||||||||||
| 125 | # | ||||||||||||||
| 126 | sub token { | ||||||||||||||
| 127 | 125 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
token: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 128 | |||||||||||||||
| 129 | 125 | return 'opaquelocktoken:' . $self->uuid; | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | # | ||||||||||||||
| 133 | # Update the expiration date of this lock. Throw an error if the update | ||||||||||||||
| 134 | # is not for any time in the future. | ||||||||||||||
| 135 | # | ||||||||||||||
| 136 | # The rationale for providing this method as a means of setting a new | ||||||||||||||
| 137 | # value for the lock expiration date is that without it, the immutable | ||||||||||||||
| 138 | # nature of this class forces the creation of a new lock object, which | ||||||||||||||
| 139 | # would be undesirable as the existing UUID should be preserved. | ||||||||||||||
| 140 | # | ||||||||||||||
| 141 | sub renew { | ||||||||||||||
| 142 | 7 | ▶ | my ($self, $expiry) = @_; | ||||||||||||
|
Subroutine
renew: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 143 | |||||||||||||||
| 144 | 7 | ▶ | die('New lock expiration date is not in the future') unless $expiry > time(); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | |||||||||||||||
| 146 | 6 | $self->{'expiry'} = $expiry; | |||||||||||||
| 147 | |||||||||||||||
| 148 | 6 | return $self; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 31 | 1; | |||||||||||||
| 152 | |||||||||||||||
| 153 | __END__ | ||||||||||||||