line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package POE::Component::WWW::Shorten; |
2
|
|
|
|
|
|
|
$POE::Component::WWW::Shorten::VERSION = '1.22'; |
3
|
|
|
|
|
|
|
#ABSTRACT: A non-blocking POE wrapper around WWW::Shorten. |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
559
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
33
|
|
7
|
1
|
|
|
1
|
|
4
|
use POE 0.38 qw(Wheel::Run Filter::Line Filter::Reference); |
|
1
|
|
|
|
|
38
|
|
|
1
|
|
|
|
|
7
|
|
8
|
1
|
|
|
1
|
|
23422
|
use Carp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
998
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub spawn { |
11
|
1
|
|
|
1
|
1
|
723
|
my $package = shift; |
12
|
1
|
50
|
|
|
|
5
|
croak "$package requires an even number of parameters" if @_ & 1; |
13
|
1
|
|
|
|
|
3
|
my %parms = @_; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
|
|
6
|
$parms{ lc $_ } = delete $parms{$_} for keys %parms; |
16
|
|
|
|
|
|
|
|
17
|
1
|
50
|
|
|
|
4
|
delete $parms{'options'} unless ref ( $parms{'options'} ) eq 'HASH'; |
18
|
1
|
|
50
|
|
|
10
|
my $type = delete $parms{'type'} || 'TinyURL'; |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
|
|
1
|
eval { |
21
|
1
|
|
|
|
|
204
|
require WWW::Shorten; |
22
|
0
|
|
|
|
|
0
|
import WWW::Shorten $type; |
23
|
|
|
|
|
|
|
}; |
24
|
1
|
50
|
|
|
|
113
|
die "Problem loading WWW::Shorten \'$type\', please check\n" if $@; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
my $self = bless \%parms, $package; |
27
|
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
$self->{session_id} = POE::Session->create( |
29
|
|
|
|
|
|
|
object_states => [ |
30
|
|
|
|
|
|
|
$self => { shorten => '_shorten', |
31
|
|
|
|
|
|
|
shutdown => '_shutdown', |
32
|
|
|
|
|
|
|
}, |
33
|
|
|
|
|
|
|
$self => [ qw(_child_error _child_closed _child_stdout _child_stderr _sig_chld _start _stop) ], |
34
|
|
|
|
|
|
|
], |
35
|
|
|
|
|
|
|
( defined ( $parms{'options'} ) ? ( options => $parms{'options'} ) : () ), |
36
|
|
|
|
|
|
|
)->ID(); |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
return $self; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _start { |
42
|
0
|
|
|
0
|
|
|
my ($kernel,$self) = @_[KERNEL,OBJECT]; |
43
|
0
|
|
|
|
|
|
$self->{session_id} = $_[SESSION]->ID(); |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
|
|
|
|
if ( $self->{alias} ) { |
46
|
0
|
|
|
|
|
|
$kernel->alias_set( $self->{alias} ); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
else { |
49
|
0
|
|
|
|
|
|
$kernel->refcount_increment( $self->{session_id} => __PACKAGE__ ); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
$self->{wheel} = POE::Wheel::Run->new( |
53
|
|
|
|
|
|
|
Program => \&_shorten_wheel, |
54
|
|
|
|
|
|
|
ErrorEvent => '_child_error', |
55
|
|
|
|
|
|
|
CloseEvent => '_child_closed', |
56
|
|
|
|
|
|
|
StdoutEvent => '_child_stdout', |
57
|
|
|
|
|
|
|
StderrEvent => '_child_stderr', |
58
|
|
|
|
|
|
|
StdioFilter => POE::Filter::Reference->new(), |
59
|
|
|
|
|
|
|
StderrFilter => POE::Filter::Line->new(), |
60
|
|
|
|
|
|
|
( $^O eq 'MSWin32' ? ( CloseOnCall => 0 ) : ( CloseOnCall => 1 ) ), |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
0
|
0
|
|
|
|
|
$kernel->yield( 'shutdown' ) unless $self->{wheel}; |
64
|
0
|
|
|
|
|
|
$kernel->sig_child( $self->{wheel}->PID, '_sig_chld' ); |
65
|
0
|
|
|
|
|
|
undef; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _stop { |
69
|
0
|
|
|
0
|
|
|
return; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _sig_chld { |
73
|
0
|
|
|
0
|
|
|
$poe_kernel->sig_handled(); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub session_id { |
77
|
0
|
|
|
0
|
1
|
|
return $_[0]->{session_id}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub shorten { |
81
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
82
|
0
|
|
|
|
|
|
$poe_kernel->post( $self->{session_id} => 'shorten' => @_ ); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _shorten { |
86
|
0
|
|
|
0
|
|
|
my ($kernel,$self) = @_[KERNEL,OBJECT]; |
87
|
0
|
|
|
|
|
|
my $sender = $_[SENDER]->ID(); |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
return if $self->{shutdown}; |
90
|
0
|
|
|
|
|
|
my $args; |
91
|
0
|
0
|
|
|
|
|
if ( ref( $_[ARG0] ) eq 'HASH' ) { |
92
|
0
|
|
|
|
|
|
$args = { %{ $_[ARG0] } }; |
|
0
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
} else { |
94
|
0
|
|
|
|
|
|
warn "first parameter must be a hashref, trying to adjust. " |
95
|
|
|
|
|
|
|
."(fix this to get rid of this message)"; |
96
|
0
|
|
|
|
|
|
$args = { @_[ARG0..$#_] }; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
$args->{lc $_} = delete $args->{$_} for grep { $_ !~ /^_/ } keys %{ $args }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
|
unless ( $args->{event} ) { |
102
|
0
|
|
|
|
|
|
warn "where am i supposed to send the output?"; |
103
|
0
|
|
|
|
|
|
return; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
0
|
0
|
|
|
|
|
unless ( $args->{url} ) { |
107
|
0
|
|
|
|
|
|
warn "No 'url' specified"; |
108
|
0
|
|
|
|
|
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
0
|
0
|
|
|
|
|
if ( $args->{session} ) { |
112
|
0
|
0
|
|
|
|
|
if ( my $ref = $kernel->alias_resolve( $args->{session} ) ) { |
113
|
0
|
|
|
|
|
|
$args->{sender} = $ref->ID(); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
else { |
116
|
0
|
|
|
|
|
|
warn "Could not resolve 'session' to a valid POE session\n"; |
117
|
0
|
|
|
|
|
|
return; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else { |
121
|
0
|
|
|
|
|
|
$args->{sender} = $sender; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
0
|
|
|
|
|
$args->{params} = $self->{params} ? $self->{params} : []; |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
$kernel->refcount_increment( $args->{sender} => __PACKAGE__ ); |
127
|
0
|
|
|
|
|
|
$self->{wheel}->put( $args ); |
128
|
0
|
|
|
|
|
|
undef; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub shutdown { |
132
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
133
|
0
|
|
|
|
|
|
$poe_kernel->call( $self->{session_id} => 'shutdown' => @_ ); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub _shutdown { |
137
|
0
|
|
|
0
|
|
|
my ($kernel,$self) = @_[KERNEL,OBJECT]; |
138
|
0
|
|
|
|
|
|
$kernel->alarm_remove_all(); |
139
|
0
|
|
|
|
|
|
$kernel->alias_remove( $_ ) for $kernel->alias_list(); |
140
|
0
|
0
|
|
|
|
|
$kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias}; |
141
|
0
|
|
|
|
|
|
$self->{shutdown} = 1; |
142
|
0
|
0
|
|
|
|
|
$self->{wheel}->shutdown_stdin if $self->{wheel}; |
143
|
0
|
|
|
|
|
|
undef; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub _child_closed { |
147
|
0
|
|
|
0
|
|
|
my ($kernel,$self) = @_[KERNEL,OBJECT]; |
148
|
0
|
|
|
|
|
|
delete $self->{wheel}; |
149
|
0
|
0
|
|
|
|
|
$kernel->yield( 'shutdown' ) unless $self->{shutdown}; |
150
|
0
|
|
|
|
|
|
undef; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub _child_error { |
154
|
0
|
|
|
0
|
|
|
my ($kernel,$self) = @_[KERNEL,OBJECT]; |
155
|
0
|
|
|
|
|
|
delete $self->{wheel}; |
156
|
0
|
0
|
|
|
|
|
$kernel->yield( 'shutdown' ) unless $self->{shutdown}; |
157
|
0
|
|
|
|
|
|
undef; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub _child_stderr { |
161
|
0
|
|
|
0
|
|
|
my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0]; |
162
|
0
|
0
|
|
|
|
|
warn "$input\n" if $self->{debug}; |
163
|
0
|
|
|
|
|
|
undef; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub _child_stdout { |
167
|
0
|
|
|
0
|
|
|
my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0]; |
168
|
0
|
|
|
|
|
|
my $session = delete $input->{sender}; |
169
|
0
|
|
|
|
|
|
my $event = delete $input->{event}; |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
|
$kernel->post( $session, $event, $input ); |
172
|
0
|
|
|
|
|
|
$kernel->refcount_decrement( $session => __PACKAGE__ ); |
173
|
0
|
|
|
|
|
|
undef; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub _shorten_wheel { |
177
|
0
|
0
|
|
0
|
|
|
if ( $^O eq 'MSWin32' ) { |
178
|
0
|
|
|
|
|
|
binmode(STDIN); binmode(STDOUT); |
|
0
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
} |
180
|
0
|
|
|
|
|
|
my $raw; |
181
|
0
|
|
|
|
|
|
my $size = 4096; |
182
|
0
|
|
|
|
|
|
my $filter = POE::Filter::Reference->new(); |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
while ( sysread ( STDIN, $raw, $size ) ) { |
185
|
0
|
|
|
|
|
|
my $requests = $filter->get( [ $raw ] ); |
186
|
0
|
|
|
|
|
|
foreach my $req ( @{ $requests } ) { |
|
0
|
|
|
|
|
|
|
187
|
0
|
|
|
|
|
|
$req->{short} = makeashorterlink( $req->{url}, @{$req->{params}} ); |
|
0
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
my $response = $filter->put( [ $req ] ); |
189
|
0
|
|
|
|
|
|
print STDOUT @$response; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
'snip'; |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
__END__ |