| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Net::OAuth2::Scheme::Counter; | ||
| 5 | BEGIN { | ||
| 6 | $Net::OAuth2::Scheme::Counter::VERSION = '0.020002_099'; | ||
| 7 | } | ||
| 8 | # ABSTRACT: a host-wide counter | ||
| 9 | use Carp; | ||
| 10 | use Thread::IID 'interpreter_id'; | ||
| 11 | |||
| 12 | my %current = (); | ||
| 13 | my %refs = (); # how many objects have been created for a given tag | ||
| 14 | my $p_id = -1; | ||
| 15 | my $i_id; | ||
| 16 | my $start; | ||
| 17 | |||
| 18 | # This counter rolls over every 200 days | ||
| 19 | # (token lifetimes should not be ANYWHERE NEAR this long) | ||
| 20 | sub _mk_start { | ||
| 21 | $start = pack "Cw2a3", 0x3f, ($i_id = interpreter_id), ($p_id = $$), pack('V',time()); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub suffix { | ||
| 25 | my $value = shift; | ||
| 26 | return (unpack 'w3a3a*', (ord($value)&0x40) ? chr(0xc0)^$value : $value)[4]; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub next { | ||
| 30 | my $self = shift; | ||
| 31 | my $tag = $$self; | ||
| 32 | |||
| 33 | # check for fork() | ||
| 34 | ref($self)->CLONE unless $$ == $p_id; | ||
| 35 | |||
| 36 | my $s0 = ord($current{$tag}); | ||
| 37 | my ($n,$s) = unpack 'wa*', | ||
| 38 | ((ord($current{$tag}) & 0x40) ? chr(0x80)^$current{$tag} : $current{$tag}); | ||
| 39 | my $ns = pack 'w', $n+1; | ||
| 40 | return $current{$tag} = | ||
| 41 | ( | ||
| 42 | !(ord($ns) & 0x40) ? $ns : | ||
| 43 | (ord($ns) & 0x80) ? chr(0x80).$ns : | ||
| 44 | chr(0x80)^$ns | ||
| 45 | # ((ord($ns) & 0xc0)==0x80 ? chr(0xc0)^$ns : | ||
| 46 | # (ord($ns) & 0x40) ? chr(0x40).$ns : | ||
| 47 | # $ns | ||
| 48 | ).$s; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub new { | ||
| 52 | my $class = shift; | ||
| 53 | my $tag = shift || ''; | ||
| 54 | |||
| 55 | # check for fork() | ||
| 56 | $class->CLONE unless $$ == $p_id; | ||
| 57 | |||
| 58 | $current{$tag} = $start | ||
| 59 | unless ($current{$tag}); | ||
| 60 | |||
| 61 | ++$refs{$tag}; | ||
| 62 | return bless \( $tag ), $class; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub DESTROY { | ||
| 66 | my $self = shift; | ||
| 67 | --$refs{$$self}; | ||
| 68 | # this routine only exists for the sake of being able to detect | ||
| 69 | # unused tags upon interpreter clone or process fork. | ||
| 70 | # | ||
| 71 | # once a counter for a given tag is created, it's best to keep it | ||
| 72 | # around; we risk repeats if we get rid of a tag and recreate it | ||
| 73 | # within a second of its original creation in the same process/thread | ||
| 74 | } | ||
| 75 | |||
| 76 | sub CLONE { | ||
| 77 | my $class = shift; | ||
| 78 | return if $p_id == $$ && $i_id == interpreter_id; | ||
| 79 | _mk_start(); | ||
| 80 | for my $tag (keys %refs) { | ||
| 81 | if ($refs{$tag} <= 0) { | ||
| 82 | # nobody is currently using it | ||
| 83 | # therefore it has not been used yet in this thread | ||
| 84 | # therefore we can safely ignore it | ||
| 85 | delete $refs{$tag}; | ||
| 86 | delete $current{$tag}; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | $current{$tag} = $start; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; | ||
| 95 | |||
| 96 | |||
| 97 | __END__ |