VFSsimple::Drv::Http - A VFSsimple implementation over http protocol


VFSsimple-Drv-Http documentation Contained in the VFSsimple-Drv-Http distribution.

Index


Code Index:

NAME

Top

VFSsimple::Drv::Http - A VFSsimple implementation over http protocol

DESCRIPTION

Top

This module provide access method for VFSsimple module to access to file on http server.

Access is provided using Net::Http module.

SEE ALSO

Top

VFSsimple

LICENSE AND COPYRIGHT

Top


VFSsimple-Drv-Http documentation Contained in the VFSsimple-Drv-Http distribution.
package VFSsimple::Drv::Http;

use strict;
use warnings;
use base qw(VFSsimple::Base);
use Net::HTTP ();
use URI ();
use File::Temp qw(tempfile);

our $VERSION = '0.03';

sub drv_new {
    my ($self) = @_;
    my $uri = URI->new($self->{root});
    my $http = Net::HTTP->new($uri->host());
    
    $self->{uri} = $uri;
    $self->{http} = $http;
    return $http ? $self : ();
}

sub drv_get {
    my ($self, $src) = @_;
    my (undef, $dest) = tempfile(UNLINK => 0);
    $self->drv_copy($src, $dest);
}

sub drv_copy {
    my ($self, $src, $dest) = @_;
    my $path = $self->{uri}->path() . '/' . $src;
    $self->{http}->write_request(GET => $path, 'User-Agent' => "Mozilla/5.0 VFSsimple::Http") or return;
    my $code = $self->{http}->read_response_headers;
    open(my $fh, '>', $dest) or return;
    while (1) {
        my $buf;
        my $n = $self->{http}->read_entity_body($buf, 1024);
        return() unless defined $n;
        last unless $n;
        print $fh $buf;
    }
    close($fh);
    return $dest;
}

sub drv_exists {
    my ($self, $file) = @_;
    my $path = $self->{uri}->path() . '/' . "$file";
    $self->{http}->write_request(
        HEAD => $path,
        'User-Agent' => "Mozilla/5.0 VFSsimple::Http"
    ) or return;
    my $code = $self->{http}->read_response_headers;
    return($code eq '200');
}

1;

__END__