File: 99_mire_test.t

package info (click to toggle)
libpoe-component-sslify-perl 1.012-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 656 kB
  • sloc: perl: 2,560; xml: 27; makefile: 2
file content (271 lines) | stat: -rw-r--r-- 7,278 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/perl
#
# This file is part of POE-Component-SSLify
#
# This software is copyright (c) 2014 by Apocalypse.
#
# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
#
use strict; use warnings;

# Test contributed by mire@irc which used this to hit the WRITE_WANTS_READ case
# mire created package Ub because he couldn't reproduce it with poco-cl-http :(
# tweaked slightly to turn it into a real testcase ( not done yet )

BEGIN {
#  sub POE::Kernel::ASSERT_DEFAULT () { 1 }
#  sub POE::Kernel::TRACE_STATISTICS () { 0 } # makes POE hang, it's been removed in git but not in 1.299 heh
#  sub POE::Kernel::TRACE_DEFAULT () { 1 }
#  sub POE::Kernel::CATCH_EXCEPTIONS () { 0 } # make sure we die right away so it's easier to debug
}

use Test::More;
BEGIN {
	plan skip_all => "AUTHOR TEST";
}

use strict;
use warnings;
use POE;
use Test::FailWarnings;

our $DEBUG=0;

package Ub;
use strict;
use warnings;
use POE qw( Component::Client::TCP Filter::Stream );
use POE::Component::SSLify qw( Client_SSLify );

# non-core deps
BEGIN {
	eval "use POE::Filter::HTTPChunk; use HTTP::Parser; use HTTP::Response;";
	if ( $@ ) {
		use Test::More;
		plan skip_all => "Unable to load deps: $@";
	}
}

sub new {
    my $this = shift;
    my %p = @_;

    my $class = ref($this) || $this;
    my $self = {};
    bless $self, $class;

    $self->{'_p'} = \%p;

    return $self;
}

sub spawn {
my $self = shift;

    my $session_id =  POE::Session->create(
        inline_states => {
_child => sub {},
_start => sub {
    my ($kernel, $heap) = @_[KERNEL, HEAP];
    ($heap->{'self'})   = @_[ARG0..$#_];
    print 'INFO: ' . __PACKAGE__ . "_start\n"
        if $main::DEBUG;
    $kernel->alias_set( 'ub' );
    #$_[KERNEL]->refcount_increment($_[SESSION]->ID, 'ub');
},
_stop => sub {
    my ($kernel, $heap) = @_[KERNEL, HEAP];
    print 'INFO: ' . __PACKAGE__ . "_stop\n"
        if $main::DEBUG;
    $heap = {};
},
on_shutdown => sub {
    print 'INFO: ' . __PACKAGE__ . "on_shutdown\n"
        if $main::DEBUG;
},
_shutdown => sub {
    print 'INFO: ' . __PACKAGE__ . "_shutdown\n"
        if $main::DEBUG;
},
r => sub {
    my ($kernel, $heap, $ev_res, $cont_ref, $host, $port, $do_ssl) = @_[KERNEL, HEAP, ARG0..$#_];

    my $s_res = $_[SENDER]->ID;

    # TODO pravi alarm za ubijanje konekcije
    my $tcp_sid = POE::Component::Client::TCP->new(
    #SessionParams => [ options => { debug => 1, trace => 1 } ],
#    SessionParams => [ options => { debug => 1 } ],
    Args => [$s_res, $ev_res, $cont_ref, $do_ssl],
    Filter => "POE::Filter::Stream",
    RemoteAddress => $host,
    RemotePort    => $port,
    ConnectTimeout => 30,
    Started => sub {
        my ($kernel, $heap) = @_[KERNEL, HEAP];
        print "INFO: Started\n"
            if $main::DEBUG;
        (@$heap{qw|s_res ev_res cont_ref do_ssl|}) = @_[ARG0..$#_];
    },
    PreConnect => sub {
        my ($kernel, $heap) = @_[KERNEL, HEAP];

        print "INFO: PreConnect\n"
            if $main::DEBUG;

        $heap->{'parser'} = HTTP::Parser->new(response => 1);

        return $_[ARG0]
            unless $heap->{'do_ssl'};

        # Convert the socket into an SSL socket.
        my $socket = eval { Client_SSLify($_[ARG0]) };

        # Disconnect if SSL failed.
        if ($@) {
            warn $@ if $main::DEBUG;
            return;
        }
        # Return the SSL-ified socket.
        return $socket;
    },
    ConnectError => sub {
        my ($kernel, $heap) = @_[KERNEL, HEAP];
        #print Dumper $heap; exit;
        my ($operation, $error_number, $error_string) = @_[ARG0..ARG2];
        print "ERROR: ConnectError $operation error $error_number occurred: $error_string\n"
            if $main::DEBUG;
        my $dc = '';
        $kernel->post($heap->{'s_res'}, $heap->{'ev_res'}, {'error' => 1, 'error_type' => 'connect_error', 'content' => \$dc});
        $_[KERNEL]->yield('shutdown');
    },
    ServerError => sub {
        my ($kernel, $heap) = @_[KERNEL, HEAP];
        my ($operation, $error_number, $error_string) = @_[ARG0..ARG2];

        print "not informing master session, ERROR: ServerError $operation error $error_number occurred: $error_string\n"
            if $main::DEBUG;
        $kernel->yield('shutdown');
    },
    Connected     => sub {
        my ($kernel, $heap) = @_[KERNEL, HEAP];
        print "INFO: Connected\n"
            if $main::DEBUG;
        $heap->{server}->put(${$heap->{'cont_ref'}});
        # start timeout thing
        #   za pravu shutdown funkciju
        $heap->{'al_cest_id'} = $_[KERNEL]->alarm_set( shutdown => time + 60 );
    },
    ServerInput   => sub {
        my ($kernel, $heap) = @_[KERNEL, HEAP];
        print "INFO: ServerInput\n"
            if $main::DEBUG;
        my $input = $_[ARG0];
        #print 'from server: ' . Dumper $input;
        eval {
            $heap->{'parser'}->add($input);
        };
        # TODO error response
        $kernel->yield('shutdown')
            if $@;
    },
    ServerFlushed => sub {
        print "INFO: ServerFlushed\n"
            if $main::DEBUG;
    },
    Disconnected => sub {
        my ($kernel, $heap) = @_[KERNEL, HEAP];
        print "INFO: disconnected ;)\n"
            if $main::DEBUG;
        my $dc = '';
        $dc = $heap->{'parser'}->object->decoded_content
            if $heap->{'parser'}->object;
        $kernel->post($heap->{'s_res'}, $heap->{'ev_res'}, {'error' => 0, 'error_type' => '', 'content' => \$dc});
        $_[KERNEL]->alarm_remove(delete $heap->{'al_cest_id'})
            if (exists $heap->{'al_cest_id'} and $heap->{'al_cest_id'});
        $_[KERNEL]->yield('shutdown');
    },

    );
    print "tcp_sid: $tcp_sid\n"
        if $main::DEBUG;

},
},
    'args' => [$self],
)->ID;

    return $session_id;
}

1;

package main;

my $ub = Ub->new()->spawn();




    my $session_id_test =  POE::Session->create(
        inline_states => {
_start => sub {
    my ($kernel, $heap) = @_[KERNEL, HEAP];
    ($heap->{'self'})   = @_[ARG0..$#_];
    print 'INFO: ' . __PACKAGE__ . "_start\n"
        if $main::DEBUG;
    $kernel->yield('test');
},
_stop => sub {
    my ($kernel, $heap) = @_[KERNEL, HEAP];
    print 'INFO: ' . __PACKAGE__ . "_stop\n"
        if $main::DEBUG;
    $heap = {};
},
on_shutdown => sub {
    print 'INFO: ' . __PACKAGE__ . "on_shutdown\n"
        if $main::DEBUG;
},
_shutdown => sub {
    print 'INFO: ' . __PACKAGE__ . "_shutdown\n"
        if $main::DEBUG;
},
test => sub {
    my ($kernel, $heap) = @_[KERNEL, HEAP];
    my ($cont, $host, $port, $do_ssl);

    $kernel->refcount_increment($_[SESSION]->ID, 'test');

    if (1) {
        $host = '0ne.us';
        $port = 443;
        $do_ssl = 1;

        $cont = <<'EOF';
GET /get.php HTTP/1.1
Host: osadmin.com
User-Agent: proba 123
Connection: close


EOF

    }
    $kernel->post('ub', 'r', 'test_res', \$cont, $host, $port, $do_ssl);

},
test_res => sub {
    my ($kernel, $heap, $dat) = @_[KERNEL, HEAP, ARG0..$#_];
    $kernel->refcount_decrement($_[SESSION]->ID, 'test');
    my $cont = ${$dat->{'content'}};
    chomp $cont;
    warn $cont;
die "HIT BUG" if length $cont == 0;
    $kernel->yield('test');
    return;
},
},)->ID;

POE::Kernel->run();
done_testing;