Codebase list libpoe-component-sslify-perl / 15e3d47
add mire test to be worked into a proper AUTHOR test Apocalypse 13 years ago
1 changed file(s) with 252 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 #!/usr/bin/perl
1
2 BEGIN {
3 # sub POE::Kernel::ASSERT_DEFAULT () { 1 }
4 # sub POE::Kernel::TRACE_STATISTICS () { 0 } # makes POE hang, it's been removed in git but not in 1.299 heh
5 # sub POE::Kernel::TRACE_DEFAULT () { 1 }
6 # sub POE::Kernel::CATCH_EXCEPTIONS () { 0 } # make sure we die right away so it's easier to debug
7 }
8
9 use strict;
10 use warnings;
11 use Data::Dumper;
12 use Time::HiRes qw|time sleep|;
13 use POE;
14
15 use Test::More;
16 plan skip_all => "AUTHOR TEST";
17
18 our $DEBUG=0;
19
20 package Ub;
21 use strict;
22 use warnings;
23 use Data::Dumper;
24 use HTTP::Parser;
25 use HTTP::Response;
26 use Time::HiRes qw|time sleep|;
27 use POE qw(Component::Client::TCP Filter::Stream Filter::HTTPChunk);
28 use POE::Component::SSLify qw( Client_SSLify );
29 sub new {
30 my $this = shift;
31 my %p = @_;
32
33 my $class = ref($this) || $this;
34 my $self = {};
35 bless $self, $class;
36
37 $self->{'_p'} = \%p;
38
39 return $self;
40 }
41
42 sub spawn {
43 my $self = shift;
44
45 my $session_id = POE::Session->create(
46 inline_states => {
47 _child => sub {},
48 _start => sub {
49 my ($kernel, $heap) = @_[KERNEL, HEAP];
50 ($heap->{'self'}) = @_[ARG0..$#_];
51 print 'INFO: ' . __PACKAGE__ . "_start\n"
52 if $main::DEBUG;
53 $kernel->alias_set( 'ub' );
54 #$_[KERNEL]->refcount_increment($_[SESSION]->ID, 'ub');
55 },
56 _stop => sub {
57 my ($kernel, $heap) = @_[KERNEL, HEAP];
58 print 'INFO: ' . __PACKAGE__ . "_stop\n"
59 if $main::DEBUG;
60 $heap = {};
61 },
62 on_shutdown => sub {
63 print 'INFO: ' . __PACKAGE__ . "on_shutdown\n"
64 if $main::DEBUG;
65 },
66 _shutdown => sub {
67 print 'INFO: ' . __PACKAGE__ . "_shutdown\n"
68 if $main::DEBUG;
69 },
70 r => sub {
71 my ($kernel, $heap, $ev_res, $cont_ref, $host, $port, $do_ssl) = @_[KERNEL, HEAP, ARG0..$#_];
72
73 my $s_res = $_[SENDER]->ID;
74
75 # TODO pravi alarm za ubijanje konekcije
76 my $tcp_sid = POE::Component::Client::TCP->new(
77 #SessionParams => [ options => { debug => 1, trace => 1 } ],
78 # SessionParams => [ options => { debug => 1 } ],
79 Args => [$s_res, $ev_res, $cont_ref, $do_ssl],
80 Filter => "POE::Filter::Stream",
81 RemoteAddress => $host,
82 RemotePort => $port,
83 ConnectTimeout => 30,
84 Started => sub {
85 my ($kernel, $heap) = @_[KERNEL, HEAP];
86 print "INFO: Started\n"
87 if $main::DEBUG;
88 (@$heap{qw|s_res ev_res cont_ref do_ssl|}) = @_[ARG0..$#_];
89 },
90 PreConnect => sub {
91 my ($kernel, $heap) = @_[KERNEL, HEAP];
92
93 print "INFO: PreConnect\n"
94 if $main::DEBUG;
95
96 $heap->{'parser'} = HTTP::Parser->new(response => 1);
97
98 return $_[ARG0]
99 unless $heap->{'do_ssl'};
100
101 # Convert the socket into an SSL socket.
102 my $socket = eval { Client_SSLify($_[ARG0]) };
103
104 # Disconnect if SSL failed.
105 if ($@) {
106 print Dumper [$@]
107 if $main::DEBUG;
108 return;
109 }
110 # Return the SSL-ified socket.
111 return $socket;
112 },
113 ConnectError => sub {
114 my ($kernel, $heap) = @_[KERNEL, HEAP];
115 #print Dumper $heap; exit;
116 my ($operation, $error_number, $error_string) = @_[ARG0..ARG2];
117 print "ERROR: ConnectError $operation error $error_number occurred: $error_string\n"
118 if $main::DEBUG;
119 my $dc = '';
120 $kernel->post($heap->{'s_res'}, $heap->{'ev_res'}, {'error' => 1, 'error_type' => 'connect_error', 'content' => \$dc});
121 $_[KERNEL]->yield('shutdown');
122 },
123 ServerError => sub {
124 my ($kernel, $heap) = @_[KERNEL, HEAP];
125 my ($operation, $error_number, $error_string) = @_[ARG0..ARG2];
126
127 print "not informing master session, ERROR: ServerError $operation error $error_number occurred: $error_string\n"
128 if $main::DEBUG;
129 $kernel->yield('shutdown');
130 },
131 Connected => sub {
132 my ($kernel, $heap) = @_[KERNEL, HEAP];
133 print "INFO: Connected\n"
134 if $main::DEBUG;
135 $heap->{server}->put(${$heap->{'cont_ref'}});
136 # start timeout thing
137 # za pravu shutdown funkciju
138 $heap->{'al_cest_id'} = $_[KERNEL]->alarm_set( shutdown => time + 60 );
139 },
140 ServerInput => sub {
141 my ($kernel, $heap) = @_[KERNEL, HEAP];
142 print "INFO: ServerInput\n"
143 if $main::DEBUG;
144 my $input = $_[ARG0];
145 #print 'from server: ' . Dumper $input;
146 eval {
147 $heap->{'parser'}->add($input);
148 };
149 # TODO error response
150 $kernel->yield('shutdown')
151 if $@;
152 },
153 ServerFlushed => sub {
154 print "INFO: ServerFlushed\n"
155 if $main::DEBUG;
156 },
157 Disconnected => sub {
158 my ($kernel, $heap) = @_[KERNEL, HEAP];
159 print "INFO: disconnected ;)\n"
160 if $main::DEBUG;
161 my $dc = '';
162 $dc = $heap->{'parser'}->object->decoded_content
163 if $heap->{'parser'}->object;
164 $kernel->post($heap->{'s_res'}, $heap->{'ev_res'}, {'error' => 0, 'error_type' => '', 'content' => \$dc});
165 $_[KERNEL]->alarm_remove(delete $heap->{'al_cest_id'})
166 if (exists $heap->{'al_cest_id'} and $heap->{'al_cest_id'});
167 $_[KERNEL]->yield('shutdown');
168 },
169
170 );
171 print "tcp_sid: $tcp_sid\n"
172 if $main::DEBUG;
173
174 },
175 },
176 'args' => [$self],
177 )->ID;
178
179 return $session_id;
180 }
181
182 1;
183
184 package main;
185
186 my $ub = Ub->new()->spawn();
187
188
189
190
191 my $session_id_test = POE::Session->create(
192 inline_states => {
193 _start => sub {
194 my ($kernel, $heap) = @_[KERNEL, HEAP];
195 ($heap->{'self'}) = @_[ARG0..$#_];
196 print 'INFO: ' . __PACKAGE__ . "_start\n"
197 if $main::DEBUG;
198 $kernel->yield('test');
199 },
200 _stop => sub {
201 my ($kernel, $heap) = @_[KERNEL, HEAP];
202 print 'INFO: ' . __PACKAGE__ . "_stop\n"
203 if $main::DEBUG;
204 $heap = {};
205 },
206 on_shutdown => sub {
207 print 'INFO: ' . __PACKAGE__ . "on_shutdown\n"
208 if $main::DEBUG;
209 },
210 _shutdown => sub {
211 print 'INFO: ' . __PACKAGE__ . "_shutdown\n"
212 if $main::DEBUG;
213 },
214 test => sub {
215 my ($kernel, $heap) = @_[KERNEL, HEAP];
216 my ($cont, $host, $port, $do_ssl);
217
218 $kernel->refcount_increment($_[SESSION]->ID, 'test');
219
220 if (1) {
221 $host = '0ne.us';
222 $port = 443;
223 $do_ssl = 1;
224
225 $cont = <<'EOE'
226 GET /get.php HTTP/1.1
227 Host: osadmin.com
228 User-Agent: proba 123
229 Connection: close
230
231
232 EOE
233 ;
234 }
235 $kernel->post('ub', 'r', 'test_res', \$cont, $host, $port, $do_ssl);
236
237 },
238 test_res => sub {
239 my ($kernel, $heap, $dat) = @_[KERNEL, HEAP, ARG0..$#_];
240 $kernel->refcount_decrement($_[SESSION]->ID, 'test');
241 my $cont = ${$dat->{'content'}};
242 chomp $cont;
243 print Dumper $cont;
244 die "HIT BUG" if length $cont == 0;
245 $kernel->yield('test'); return;
246 print "test_res has " . Dumper $dat;
247 },
248 },)->ID;
249
250 POE::Kernel->run();
251 exit;