summaryrefslogtreecommitdiffabout
path: root/lib/SlackBuild/URI.pm
blob: 410a9ca29115efe2c262b9108b73f940f293793b (plain)
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
package SlackBuild::URI;
use strict;
use warnings;
use URI;
use Carp;
use LWP::UserAgent;
use File::Basename;
use File::Spec;
# use LWP::Protocol::https;
# use LWP::Protocol::file;
# use LWP::Protocol::ftp;

our $AUTOLOAD;

sub AUTOLOAD {
    my $self = shift;

    # The Perl interpreter places the name of the
    # message in a variable called $AUTOLOAD.

    # DESTROY messages should never be propagated.
    return if $AUTOLOAD =~ /::DESTROY$/;

    # Remove the package name.
    $AUTOLOAD =~ s/^.*:://;
    
    # Pass the message to the delegate.
    return $self->{_uri}->$AUTOLOAD(@_);
}

sub _valid_scheme {
    my $scheme = shift->scheme;
    return eval { require "LWP/Protocol/$scheme.pm" }; 
}

sub new {
    my $class = shift;
    my $uri = new URI(@_);
    $uri->scheme('file') unless $uri->scheme;
    my $self = bless { _uri => $uri }, $class;
    croak "$self: unsupported scheme"
    	unless $self->_valid_scheme;
    return $self;
}

sub clone {
    my $self = shift;
    return new SlackBuild::URI($self);
}

sub new_abs {
    my ($self, $arg) = @_;
    my $s = $self->clone;
    $s->path(File::Spec->catfile($self->path, $arg));
    return $s;
}

use overload '""' => sub { shift->as_string };

sub download {
    my $self = shift;
    my $dst = shift || basename($self->path);
    my $scheme = $self->scheme;
    require "LWP/Protocol/$scheme.pm";
    print "downloading $self\n";
    my $ua = LWP::UserAgent->new();
    $ua->agent('Slackbuilder/0.90');
    my $response = $ua->get($self->as_string, ':content_file' => $dst);
    $self->download_response($response);
    return $response->is_success;
}

sub download_response {
    my $self = shift;
    croak "too many arguments" if (@_ > 1);
    if (my $v = shift) {
	$self->{_response} = $v;
    }
    return $self->{_response};
}

sub download_success {
    my $self = shift;
    return $self->download_response->is_success;
}

sub download_status {
    my $self = shift;
    return $self->download_response->status_line;
}

sub downloaded_html {
    my $self = shift;
    return $self->download_response->content_type =~ m{^(?:application/xhtml\+xml|text/(?:css|html))$};
}

1;

Return to:

Send suggestions and report system problems to the System administrator.