File Coverage

blib/lib/Net/DirectConnect/http.pm
Criterion Covered Total %
statement 18 58 31.0
branch 0 22 0.0
condition 0 24 0.0
subroutine 6 10 60.0
pod 0 1 0.0
total 24 115 20.8


line stmt bran cond sub pod time code
1             #$Id: http.pm 998 2013-08-14 12:21:20Z pro $ $URL: svn://svn.setun.net/dcppp/trunk/lib/Net/DirectConnect/http.pm $
2             package #hide from cpan
3             Net::DirectConnect::http;
4 1     1   7 use Data::Dumper; #dev only
  1         2  
  1         55  
5             #$Data::Dumper::Sortkeys = 1;
6             #$Data::Dumper::Sortkeys = $Data::Dumper::Useqq = $Data::Dumper::Indent = 1;
7 1     1   7 use Net::DirectConnect;
  1         1  
  1         23  
8             #use Net::DirectConnect::hubcli;
9 1     1   6 use strict;
  1         1  
  1         38  
10 1     1   6 no warnings qw(uninitialized);
  1         2  
  1         47  
11 1     1   6 no if $] >= 5.017011, warnings => 'experimental::smartmatch';
  1         2  
  1         7  
12             our $VERSION = ( split( ' ', '$Revision: 998 $' ) )[1];
13             #our @ISA = ('Net::DirectConnect');
14 1     1   147 use base 'Net::DirectConnect';
  1         2  
  1         1456  
15              
16             sub init {
17 0     0 0   my $self = shift;
18             #$self->log( 'dev', 'httpcli init' );
19             #%$self = ( %$self,
20 0           local %_ = (
21             #'incomingclass' => 'Net:DirectConnect::httpcli',
22             'auto_connect' => 0,
23             'auto_listen' => 0,
24             'protocol' => 'http',
25             #); $self->{$_} = $_{$_} for keys %_;
26             # local %_ = (
27             #'myport' => 80,
28             #'myport' => 443,
29             #modules => [],
30             #'myport_base' => 8000,
31             #'myport_random' => 99,
32             #'myport_tries' => 5,
33             'myport_inc' => 1,
34             #'HubName' => 'Net::DirectConnect test hub',
35             'allow' => '127.0.0.1',
36             'cmd_bef' => undef,
37             'cmd_aft' => "\n",
38             );
39 0   0       $self->{$_} //= $_{$_} for keys %_;
40 0           local %_ = @_;
41             #warn "$_=$_{$_}";
42 0           $self->{$_} = $_{$_} for keys %_;
43             #$self->{$_} ||= $self->{'parent'}{$_} ||= {} for qw(peers peers_sid peers_cid want share_full share_tth);
44 0   0       $self->{$_} ||= $self->{'parent'}{$_} for qw(http_download http_control http_allow); #allow
45             #$self->baseinit();
46             #$self->{'parse'} ||= $self->{'parent'}{'parse'};
47             #$self->{'cmd'} ||= $self->{'parent'}{'cmd'};
48 0 0         $self->{'allow'} = $self->{http_allow} if defined $self->{http_allow};
49             $self->{'handler_int'}{'unknown'} ||= sub {
50 0 0   0     my $self = shift if ref $_[0];
51             #$self->log( 'dev', "unknown1", Dumper \@_ );
52 0           ( $self->{http_headers}{ $_[0] } = $_[1] ) =~ s/^: |\s+$//g;
53             #};
54 0   0       };
55             #$self->{'handler'}{ 'unknown'}||=sub {
56             #my $self = shift if ref $_[0];
57             #$self->log( 'dev', "unknown2", @_ );
58             #};
59             #};
60             #'GET' => sub {
61             $self->{'parse'} ||= {
62             'DELLLLLLLnoGET' => sub {
63 0 0   0     my $self = shift if ref $_[0];
64 0           my ( $url, $prot ) = split /\s/, $_[0];
65 0           $self->log( 'dev', "get $url : $prot" );
66 0           $self->{'http_geturl'} = $url;
67             },
68             "\x0D" => sub { #$self->log('dev', 'can send'); },
69             #'' => sub {
70 0 0   0     my $self = shift if ref $_[0];
71             #$self->log( 'dev', 'can send2', Dumper $self->{'handler_int'}, $self->{http_headers} );
72 0           ( $self->{'http_geturl'} ) = split ' ', $self->{http_headers}{GET};
73 0           ( $self->{'http_getfile'} ) = $self->{'http_geturl'} =~ m{^/(.+)};
74 0           my $c = "HTTP/1.1 200 OK" . "\nContent-Type: text/html; charset=utf-8\n\n";
75 0 0 0       if ( $self->{'http_control'} and $self->{'http_geturl'} eq '/' ) {
    0 0        
    0 0        
76 0 0         $c .= "" . "clients:
" . (
77             join ', ',
78             map {
79 0           "$_($self->{clients}{$_}{status}"
80             . (
81             !$self->{clients}{$_}{'filebytes'} ? () : ":$self->{clients}{$_}{'filebytes'}/$self->{clients}{$_}{'filetotal'}" )
82             . ")"
83 0           } sort keys %{ $self->{clients} }
84             )
85             . "
peers:
"
86             . (
87             join '
',
88             map {
89 0           join ' ', $_->{INF}{NI}, $_->{INF}{SS}, $_->{INF}{I4},
90             } sort {
91 0           $b->{INF}{SS} <=> $a->{INF}{SS}
92 0           } values %{ $self->{peers_cid} }
93             )
94             . "
"    #.Dumper($self->{peers}) 
95             #. "
" . Dumper($self) 
96             . "";
97             } elsif ( $self->{'http_download'} and my $full = $self->{"share_full"}{ $self->{'http_getfile'} } ) {
98 0           my ($name) = $full =~ m{([^/]+)$};
99 0           my $size = -s $full;
100 0           my $sizep = $size + 1;
101 0           my ( $from, $to ) = $self->{http_headers}{Range} =~ /^bytes=(\d+)\-(\d*)/;
102 0 0 0       $to ||= $size if $from;
103 0   0       my $type = { xml => 'text/xml' }->{ lc +( $name =~ m/\.(\w+)/ )[0] } || 'binary/octet-stream';
104 0 0         $c =
    0          
105             "HTTP/1.1 "
106             . ( $from ? "206 Partial Content" : "200 OK" )
107             . "\nContent-Type: $type"
108             . "\nContent-Length: $size"
109             . "\nAccept-Ranges: bytes\n"
110             . ( !$from ? () : "Content-Range: bytes $from-$to/$sizep\n" )
111             . "Content-Disposition: attachment; filename=$name\n\n";
112             #$self->log( 'dev', "hdr[$c]" );
113 0           $self->send( Encode::encode 'utf8', $c, Encode::FB_WARN );
114 0           $self->file_send( $self->{"share_full"}{ $self->{'http_getfile'} }, $from, $to );
115 0           return;
116             #$c .= "gettii[$self->{'http_getfile'}]";
117             } elsif ( $self->{'http_control'} and $self->{'http_geturl'} =~ m{^/dl/(.+)$} ) {
118 0           $self->{parent}{parent}->download($1);
119 0           $c .= "try dl [$1]";
120             }
121             #$c .= "
" . Dumper( $self->{http_headers} ); 
122 0           $self->send( Encode::encode 'utf8', $c, Encode::FB_WARN );
123 0           $self->destroy();
124             },
125 0   0       };
126             #$self->{'parser'} = sub { my $self = shift;$self->log('dev', 'myparser', Dumper @_); };
127             #$self->log( 'dev', 'httpcli inited' );
128             }
129             1;