blib/lib/Bot/Cobalt/Plugin/WWW.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Bot/Cobalt/Plugin/Rehash.pm ↑ Summary blib/lib/Bot/Cobalt/DB.pm »
1 package Bot::Cobalt::Plugin::WWW;
2 $Bot::Cobalt::Plugin::WWW::VERSION = '0.021003';
3 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Bot::Cobalt;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Bot::Cobalt::Common;
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 use POE qw/
10 Component::Client::HTTP
11 Component::Client::Keepalive
12 /;
13  
14  
15 sub opts {
16 my $opts = core->get_plugin_cfg($_[0])->{Opts};
17 return +{} unless ref $opts and reftype $opts eq 'HASH';
18 $opts
19 }
20  
21 sub bindaddr {
22 $_[0]->opts->{BindAddr}
23 }
24  
25 sub proxy {
26 $_[0]->opts->{Proxy}
27 }
28  
29 sub timeout {
30 $_[0]->opts->{Timeout} || 90
31 }
32  
33 sub max_per_host {
34 $_[0]->opts->{MaxPerHost} || 5
35 }
36  
37 sub max_workers {
38 $_[0]->opts->{MaxWorkers} || 25
39 }
40  
41 sub Requests {
42 return($_[0]->{REQS}//={})
43 }
44  
45 sub new { bless {}, shift }
46  
47 sub Cobalt_register {
48 my ($self, $core) = splice @_, 0, 2;
49  
50 register( $self, 'SERVER',
51 'www_request',
52 );
53
54 POE::Session->create(
55 object_states => [
56 $self => [
57 '_start',
58 'ht_response',
59 'ht_post_request',
60 ],
61 ],
62 );
63  
64 logger->info("Loaded WWW interface");
65  
66 return PLUGIN_EAT_NONE
67 }
68  
69 sub Cobalt_unregister {
70 my ($self, $core) = splice @_, 0, 2;
71  
72 delete $core->Provided->{www_request};
73  
74 my $ht_alias = 'ht_'.$core->get_plugin_alias($self);
75 $poe_kernel->call( $ht_alias, 'shutdown' );
76  
77 my $sess_alias = 'www_'.$core->get_plugin_alias($self);
78 $poe_kernel->alias_remove( $sess_alias );
79  
80 logger->info("Unregistered");
81  
82 return PLUGIN_EAT_NONE
83 }
84  
85 sub Bot_www_request {
86 my ($self, $core) = splice @_, 0, 2;
87 my $request = ${ $_[0] };
88 my $event = defined $_[1] ? ${$_[1]} : undef ;
89 my $args = defined $_[2] ? ${$_[2]} : undef ;
90  
91 unless ($request && $request->isa('HTTP::Request')) {
92 logger->warn(
93 "www_request received but no request at "
94 .join ' ', (caller)[0,2]
95 );
96 }
97
98 unless ($event) {
99 ## no event at all is legitimate
100 $event = 'www_not_handled';
101 }
102
103 $args = [] unless $args;
104 my @p = ( 'a' .. 'f', 1 .. 9 );
105 my $tag = join '', map { $p[rand@p] } 1 .. 5;
106 $tag .= $p[rand@p] while exists $self->Requests->{$tag};
107  
108 $self->Requests->{$tag} = {
109 Event => $event,
110 Args => $args,
111 Request => $request,
112 Time => time(),
113 };
114  
115 logger->debug("www_request issue $tag -> $event");
116
117 my $sess_alias = 'www_'.$core->get_plugin_alias($self);
118 $poe_kernel->call( $sess_alias,
119 'ht_post_request',
120 $request, $tag
121 );
122  
123 return PLUGIN_EAT_ALL
124 }
125  
126 sub ht_post_request {
127 ## Bridge to make sure response gets delivered to correct session
128 my ($self, $kernel) = @_[OBJECT, KERNEL];
129 my ($request, $tag) = @_[ARG0, ARG1];
130 ## Post the ::Request
131 my $ht_alias = 'ht_'. core()->get_plugin_alias($self);
132 $kernel->post( $ht_alias,
133 'request', 'ht_response',
134 $request, $tag
135 );
136 }
137  
138 sub ht_response {
139 my ($self, $kernel) = @_[OBJECT, KERNEL];
140 my ($req_pk, $resp_pk) = @_[ARG0, ARG1];
141  
142 my $response = $resp_pk->[0];
143 my $tag = $req_pk->[1];
144  
145 my $this_req = delete $self->Requests->{$tag};
146 return unless $this_req;
147
148 my $event = $this_req->{Event};
149 my $args = $this_req->{Args};
150
151 core->log->debug("ht_response dispatch: $event ($tag)");
152  
153 my $content = $response->is_success ?
154 $response->decoded_content
155 : $response->message;
156  
157 broadcast($event, $content, $response, $args);
158 }
159  
160 sub _start {
161 my ($self, $kernel) = @_[OBJECT, KERNEL];
162  
163 my $sess_alias = 'www_'. core()->get_plugin_alias($self);
164 $kernel->alias_set( $sess_alias );
165  
166 my %opts;
167 $opts{BindAddr} = $self->bindaddr if $self->bindaddr;
168 $opts{Proxy} = $self->proxy if $self->proxy;
169 $opts{Timeout} = $self->timeout;
170  
171 ## Create "ht_${plugin_alias}" session
172 POE::Component::Client::HTTP->spawn(
173  
174 FollowRedirects => 5,
175  
176 Agent => __PACKAGE__,
177  
178 Alias => 'ht_'. core()->get_plugin_alias($self),
179  
180 ConnectionManager => POE::Component::Client::Keepalive->new(
181 keep_alive => 1,
182 max_per_host => $self->max_per_host,
183 max_open => $self->max_workers,
184 timeout => $self->timeout,
185 ),
186
187 %opts,
188  
189 );
190
191 core()->Provided->{www_request} = __PACKAGE__ ;
192 }
193  
194 1;
195 __END__
« blib/lib/Bot/Cobalt/Plugin/Rehash.pm ↑ Summary blib/lib/Bot/Cobalt/DB.pm »