2 # This class is the internal subclass that does various Async connects and
3 # retreivals of info. Typical uses (and specific support) include http get and
6 # This merely starts up a Msg handler (and no DXChannel) ($conn in other words)
7 # does the GET, parses out the result and the data and then (assuming a positive
8 # result and that the originating callsign is still online) punts out the data
11 # It isn't designed to be very clever.
13 # Copyright (c) 2013 - Dirk Koopman G1TLH
23 use vars qw(@ISA $deftimeout);
31 # standard http get handler
38 my $state = $conn->{state};
40 dbg("asyncmsg: $msg") if isdbg('async');
42 # no point in going on if there is no-one wanting the output anymore
43 my $dxchan = DXChannel::get($conn->{caller});
49 if ($state eq 'waitreply') {
50 # look at the reply code and decide whether it is a success
51 my ($http, $code, $ascii) = $msg =~ m|(HTTP/\d\.\d)\s+(\d+)\s+(.*)|;
54 $conn->{state} = 'waitblank';
55 } elsif ($code == 302) {
57 $conn->{state} = 'waitlocation';
59 $dxchan->send("$code $ascii");
62 } elsif ($state eq 'waitlocation') {
63 my ($path) = $msg =~ m|Location:\s*(.*)|;
65 my @uri = split m|/+|, $path;
66 if ($uri[0] eq 'http:') {
68 my $host = shift @uri;
69 my $newpath = '/' . join('/', @uri);
70 $newpath .= '/' if $path =~ m|/$|;
71 _getpost(ref $conn, $conn->{asyncsort}, $conn->{caller}, $host, 80, $newpath, @{$conn->{asyncargs}});
72 } elsif ($path =~ m|^/|) {
73 _getpost(ref $conn, $conn->{asyncsort}, $conn->{caller}, $conn->{peerhost}, $conn->{peerport}, $path,
74 @{$conn->{asyncargs}});
76 delete $conn->{on_disconnect};
79 } elsif ($state eq 'waitblank') {
81 $conn->{state} = 'indata';
83 } elsif ($conn->{state} eq 'indata') {
84 if (my $filter = $conn->{filter}) {
86 # this will crash if the command has been redefined and the filter is a
87 # function defined there whilst the request is in flight,
88 # but this isn't exactly likely in a production environment.
89 $filter->($conn, $msg, $dxchan);
91 my $prefix = $conn->{prefix} || '';
92 $dxchan->send("$prefix$msg");
100 # Just outputs everything
107 # no point in going on if there is no-one wanting the output anymore
108 my $dxchan = DXChannel::get($conn->{caller});
115 my $prefix = $conn->{prefix} || '';
116 $dxchan->send("$prefix$msg");
125 my $conn = $pkg->SUPER::new($handler);
126 $conn->{caller} = ref $call ? $call->call : $call;
129 $outstanding{$conn} = $conn;
134 # This does a http get on a path on a host and
135 # returns the result (through an optional filter)
137 # expects to be called something like from a cmd.pl file:
139 # AsyncMsg->get($self, <host>, <port>, <path>, [<key=>value>...]
141 # Standard key => value pairs are:
143 # filter => CODE ref (e.g. sub { ... })
144 # prefix => <string> prefix output with this string
146 # Anything else is taken and sent as (extra) http header stuff e.g:
148 # 'User-Agent' => qq{DXSpider;$main::version;$main::build;$^O}
149 # 'Content-Type' => q{text/xml; charset=utf-8}
150 # 'Content-Length' => $lth
152 # Host: is always set to the name of the host (unless overridden)
153 # User-Agent: is set to default above (unless overridden)
166 my $conn = $pkg->new($call, \&handle_get);
167 $conn->{asyncargs} = [@_];
168 $conn->{state} = 'waitreply';
169 $conn->{filter} = delete $args{filter} if exists $args{filter};
170 $conn->{prefix} = delete $args{prefix} if exists $args{prefix};
171 $conn->{on_disconnect} = delete $args{on_disc} || delete $args{on_disconnect};
172 $conn->{path} = $path;
173 $conn->{asyncsort} = $sort;
175 $r = $conn->connect($host, $port);
177 dbg("Sending '$sort $path HTTP/1.0'") if isdbg('async');
178 $conn->send_later("$sort $path HTTP/1.0\n");
180 my $h = delete $args{Host} || $host;
181 my $u = delete $args{'User-Agent'} || "DxSpider;$main::version;$main::build;$^O;$main::mycall";
182 my $d = delete $args{data};
184 $conn->send_later("Host: $h\n");
185 $conn->send_later("User-Agent: $u\n");
186 while (my ($k,$v) = each %args) {
187 $conn->send_later("$k: $v\n");
189 $conn->send_later("\n$d") if defined $d;
190 $conn->send_later("\n");
193 return $r ? $conn : undef;
199 _getpost($pkg, "GET", @_);
205 _getpost($pkg, "POST", @_);
208 # do a raw connection
210 # Async->raw($self, <host>, <port>, [handler => CODE ref], [prefix => <string>]);
212 # With no handler defined, everything sent by the connection will be sent to
215 # One can send stuff out on the connection by doing a standard "$conn->send_later(...)"
216 # inside the (custom) handler.
227 my $handler = delete $args{handler} || \&handle_raw;
228 my $conn = $pkg->new($call, $handler);
229 $conn->{prefix} = delete $args{prefix} if exists $args{prefix};
230 $r = $conn->connect($host, $port);
231 return $r ? $conn : undef;
241 my $r = $conn->SUPER::connect($host, $port);
243 dbg("AsyncMsg: Connected $conn->{cnum} to $host $port") if isdbg('async');
245 dbg("AsyncMsg: ***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('async');
255 if (my $ondisc = $conn->{on_disconnect}) {
256 my $dxchan = DXChannel::get($conn->{caller});
259 $ondisc->($conn, $dxchan)
262 delete $outstanding{$conn};
263 $conn->SUPER::disconnect;
269 delete $outstanding{$conn};
270 $conn->SUPER::DESTROY;