| 1 | package POE::Component::CPANIDX; | ||
| 2 | |||
| 3 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 4 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 4 | ▶ | use POE qw(Component::Client::HTTP); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | use YAML::Tiny; | ||
| 8 | use HTTP::Request::Common; | ||
| 9 | use File::Spec::Unix; | ||
| 10 | use vars qw($VERSION); | ||
| 11 | |||
| 12 | $VERSION = '0.10'; | ||
| 13 | |||
| 14 | my $cmds = { | ||
| 15 | mod => 1, | ||
| 16 | dist => 1, | ||
| 17 | auth => 1, | ||
| 18 | corelist => 1, | ||
| 19 | dists => 1, | ||
| 20 | timestamp => 0, | ||
| 21 | topten => 0, | ||
| 22 | mirrors => 0, | ||
| 23 | }; | ||
| 24 | |||
| 25 | # Stolen from POE::Wheel. This is static data, shared by all | ||
| 26 | my $current_id = 0; | ||
| 27 | my %active_identifiers; | ||
| 28 | |||
| 29 | sub _allocate_identifier { | ||
| 30 | while (1) { | ||
| 31 | last unless exists $active_identifiers{ ++$current_id }; | ||
| 32 | } | ||
| 33 | return $active_identifiers{$current_id} = $current_id; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _free_identifier { | ||
| 37 | my $id = shift; | ||
| 38 | delete $active_identifiers{$id}; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub spawn { | ||
| 43 | my $package = shift; | ||
| 44 | my %opts = @_; | ||
| 45 | $opts{lc $_} = delete $opts{$_} for keys %opts; | ||
| 46 | my $options = delete $opts{options}; | ||
| 47 | my $self = bless \%opts, $package; | ||
| 48 | $self->{session_id} = POE::Session->create( | ||
| 49 | object_states => [ | ||
| 50 | $self => { shutdown => '_shutdown', | ||
| 51 | query_idx => '_query_idx', | ||
| 52 | }, | ||
| 53 | $self => [ qw(_start _query_idx _dispatch _http_request _http_response) ], | ||
| 54 | ], | ||
| 55 | heap => $self, | ||
| 56 | ( ref($options) eq 'HASH' ? ( options => $options ) : () ), | ||
| 57 | )->ID(); | ||
| 58 | return $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub session_id { | ||
| 62 | return $_[0]->{session_id}; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub shutdown { | ||
| 66 | my $self = shift; | ||
| 67 | $poe_kernel->call( $self->{session_id}, 'shutdown' ); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _start { | ||
| 71 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | ||
| 72 | $self->{session_id} = $_[SESSION]->ID(); | ||
| 73 | if ( $self->{alias} ) { | ||
| 74 | $kernel->alias_set( $self->{alias} ); | ||
| 75 | } | ||
| 76 | else { | ||
| 77 | $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ ); | ||
| 78 | } | ||
| 79 | $self->{_httpc} = 'httpc-' . $self->{session_id}; | ||
| 80 | POE::Component::Client::HTTP->spawn( | ||
| 81 | Alias => $self->{_httpc}, | ||
| 82 | FollowRedirects => 2, | ||
| 83 | ); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _shutdown { | ||
| 88 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | ||
| 89 | $kernel->alias_remove( $_ ) for $kernel->alias_list(); | ||
| 90 | $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias}; | ||
| 91 | $self->{_shutdown} = 1; | ||
| 92 | $kernel->post( $self->{_httpc}, 'shutdown' ); | ||
| 93 | undef; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub query_idx { | ||
| 97 | my $self = shift; | ||
| 98 | $poe_kernel->post( $self->{session_id}, '_query_idx', @_ ); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _query_idx { | ||
| 102 | my ($kernel,$self,$state) = @_[KERNEL,OBJECT,STATE]; | ||
| 103 | my $sender = $_[SENDER]->ID(); | ||
| 104 | return if $self->{_shutdown}; | ||
| 105 | my $args; | ||
| 106 | if ( ref( $_[ARG0] ) eq 'HASH' ) { | ||
| 107 | $args = { %{ $_[ARG0] } }; | ||
| 108 | } else { | ||
| 109 | $args = { @_[ARG0..$#_] }; | ||
| 110 | } | ||
| 111 | |||
| 112 | $args->{lc $_} = delete $args->{$_} for grep { $_ !~ /^_/ } keys %{ $args }; | ||
| 113 | |||
| 114 | unless ( $args->{event} ) { | ||
| 115 | warn "No 'event' specified for $state"; | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | croak | ||
| 120 | "You must provide a valid 'url' of a CPANIDX site" | ||
| 121 | unless $args->{url} and URI->new($args->{url}) and URI->new($args->{url})->scheme eq 'http'; | ||
| 122 | |||
| 123 | $args->{cmd} = 'timestamp' unless $args->{cmd}; | ||
| 124 | $args->{cmd} = lc $args->{cmd}; | ||
| 125 | |||
| 126 | my $arg = $cmds->{ $args->{cmd} }; | ||
| 127 | |||
| 128 | croak | ||
| 129 | "'cmd' that was specified is unknown" | ||
| 130 | unless defined $arg; | ||
| 131 | |||
| 132 | croak | ||
| 133 | "'cmd' requires that you specify a 'search' term" | ||
| 134 | if $arg and !$args->{search}; | ||
| 135 | |||
| 136 | $args->{sender} = $sender; | ||
| 137 | $kernel->refcount_increment( $sender => __PACKAGE__ ); | ||
| 138 | $kernel->yield( '_http_request', $args ); | ||
| 139 | |||
| 140 | return; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub _http_request { | ||
| 144 | my ($kernel,$self,$req) = @_[KERNEL,OBJECT,ARG0]; | ||
| 145 | my $url = URI->new( $req->{url} ); | ||
| 146 | |||
| 147 | $url->path( File::Spec::Unix->catfile( $url->path, 'yaml', $req->{cmd}, ( $req->{search} ? $req->{search} : () ) ) ); | ||
| 148 | |||
| 149 | my $id = _allocate_identifier(); | ||
| 150 | |||
| 151 | $kernel->post( | ||
| 152 | $self->{_httpc}, | ||
| 153 | 'request', | ||
| 154 | '_http_response', | ||
| 155 | GET( $url->as_string ), | ||
| 156 | "$id", | ||
| 157 | ); | ||
| 158 | |||
| 159 | $self->{_requests}->{ $id } = $req; | ||
| 160 | return; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub _http_response { | ||
| 164 | my ($kernel,$self,$request_packet,$response_packet) = @_[KERNEL,OBJECT,ARG0,ARG1]; | ||
| 165 | my $id = $request_packet->[1]; | ||
| 166 | my $req = delete $self->{_requests}->{ $id }; | ||
| 167 | _free_identifier( $id ); | ||
| 168 | my $resp = $response_packet->[0]; | ||
| 169 | if ( $resp->is_success ) { | ||
| 170 | my $data; | ||
| 171 | eval { $data = YAML::Tiny::Load( $resp->content ); }; | ||
| 172 | unless ( $data ) { | ||
| 173 | $req->{error} = 'No valid YAML data was found'; | ||
| 174 | $kernel->yield( '_dispatch', $req ); | ||
| 175 | return; | ||
| 176 | } | ||
| 177 | $req->{data} = $data; | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | $req->{error} = $resp->as_string; | ||
| 181 | } | ||
| 182 | |||
| 183 | $kernel->yield( '_dispatch', $req ); | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub _dispatch { | ||
| 188 | my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0]; | ||
| 189 | my $session = delete $input->{sender}; | ||
| 190 | my $event = delete $input->{event}; | ||
| 191 | $kernel->post( $session, $event, $input ); | ||
| 192 | $kernel->refcount_decrement( $session => __PACKAGE__ ); | ||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | qq[CAPTCH!]; | ||
| 197 | |||
| 198 | __END__ | ||