blib/lib/Wx/Perl/FSHandler/LWP.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
↑ Summary
1 package Wx::Perl::FSHandler::LWP;
2  
3 =head1 NAME
4  
5 C<Wx::Perl::FSHandler::LWP> - file system handler based upon LWP
6  
7 =head1 SYNOPSIS
8  
9 my $ua = LWP::UserAgent->new;
10  
11 # customize the User Agent, set proxy, supported protocols, ...
12  
13 my $handler = Wx::Perl::FSHandler::LWP->new( $ua );
14  
15 Wx::FileSystem::AddHandler( $handler );
16  
17 =head1 DESCRIPTION
18  
19 The C<Wx::Perl::FSHandler::LWP> is a C<wxFileSystemHandler>
20 implementation based upon C<LWP::UserAgent>, and is meant as a
21 superior replacement for C<wxInternetFSHandler>.
22  
23 =cut
24  
25 0 ▶use Wx::FS;
Subroutine BEGIN: called CC=1 SCAR=2.6
26  
27 use strict;
28 use base 'Wx::PlFileSystemHandler';
29  
30 use LWP::UserAgent;
31 use IO::Scalar;
32  
33 our $VERSION = '0.03';
34  
35 =head2 new
36  
37 my $handler = Wx::Perl::FSHandler::LWP->new( $ua );
38  
39 Creates a new instance. C<$ua> must be an object of class
40 C<LWP::UserAgent>, which will be used to handle requests.
41  
42 =cut
43  
44 sub new {
45 my( $class, $ua ) = @_;
46 my $self = $class->SUPER::new;
47  
48 $self->{user_agent} = $ua;
49  
50 return $self;
51 }
52  
53 =head2 CanOpen
54  
55 Called internally by C<Wx::FileSystem>. Calls C<is_protocol_supported>
56 on the user agent to determine if the location can be opened.
57  
58 =cut
59  
60 sub CanOpen {
61 my( $self, $location ) = @_;
62 my $uri = URI->new( $location );
63  
64 return $self->user_agent->is_protocol_supported( $uri->scheme );
65 }
66  
67 =head2 OpenFile
68  
69 Called internally by C<Wx::FileSystem>. Uses the user agent to fetch
70 the URL and returns a C<Wx::FSFile> representing the result.
71  
72 =cut
73  
74 sub OpenFile {
75 my( $self, $fs, $location ) = @_;
76  
77 # work around bug in Wx::FileSystem: remove artificial '//'
78 if( index( $location, $fs->GetPath ) == 0
79 && substr( $location, length( $fs->GetPath ) - 1, 2 ) eq '//' ) {
80 substr $location, length( $fs->GetPath ) - 1, 2, '/';
81 }
82 my $uri = URI->new( $location );
83 my $request = HTTP::Request->new( 'GET', $uri );
84 my $response = $self->user_agent->request( $request, undef );
85  
86 return undef unless $response->is_success;
87  
88 my $value = $response->content;
89 my $fh = IO::Scalar->new( \$value );
90 my $file = Wx::FSFile->new( $fh, $response->base,
91 scalar $response->content_type,
92 $uri->fragment || '' );
93  
94 return $file;
95 }
96  
97 =head2 user_agent
98  
99 my $ua = $handler->user_agent;
100  
101 Returns the C<LWP::UserAgent> object used to handle requests.
102  
103 =cut
104  
105 sub user_agent { $_[0]->{user_agent} }
106  
107 =head1 ENVIRONMENTAL VARIABLES
108  
109 See L<LWP::UserAgent|LWP::UserAgent>.
110  
111 =head1 AUTHOR
112  
113 Mattia Barbon <mbarbon@cpan.org>
114  
115 =head1 LICENSE
116  
117 Copyright (c) 2003, 2006 Mattia Barbon.
118  
119 This package is free software; you can redistribute it and/or
120 modify it under the same terms as Perl itself.
121  
122 =head1 SEE ALSO
123  
124 L<LWP::UserAgent|LWP::UserAgent>
125  
126 wxFileSystem and wxFileSystemHandler in wxPerl documentation.
127  
128 =cut
129  
130 1;
↑ Summary