| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Enbld::HTTP; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 2632 | use strict; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 192 |  | 
| 4 | 4 |  |  | 4 |  | 24 | use warnings; | 
|  | 4 |  |  |  |  | 60 |  | 
|  | 4 |  |  |  |  | 138 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 4 |  |  | 4 |  | 23 | use File::Spec; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 133 |  | 
| 7 | 4 |  |  | 4 |  | 19 | use Carp; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 269 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 4 |  |  | 4 |  | 9182 | use HTTP::Tiny; | 
|  | 4 |  |  |  |  | 158194 |  | 
|  | 4 |  |  |  |  | 2622 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $ua; | 
| 12 |  |  |  |  |  |  | our $download_hook; | 
| 13 |  |  |  |  |  |  | our $get_hook; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | require Enbld::Message; | 
| 16 |  |  |  |  |  |  | require Enbld::Error; | 
| 17 |  |  |  |  |  |  | require Enbld::Exception; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub initialize_ua { | 
| 20 | 0 |  |  | 0 | 0 | 0 | $ua = HTTP::Tiny->new; | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub download { | 
| 24 | 54 |  |  | 54 | 0 | 154 | my ( $pkg, $url, $path ) = @_; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 54 |  |  |  |  | 1169 | my ( undef, undef, $file ) = File::Spec->splitpath( $path ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 54 | 100 |  |  |  | 1361 | if ( -e $path ) { | 
| 29 | 2 |  |  |  |  | 7 | my $msg = "--> Use file '$file' that is previously downloaded."; | 
| 30 | 2 |  |  |  |  | 13 | Enbld::Message->notify( $msg ); | 
| 31 | 2 |  |  |  |  | 4 | return $path; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | # for debug hook | 
| 35 | 52 | 50 |  |  |  | 204 | if ( $download_hook ) { | 
| 36 | 52 |  |  |  |  | 319 | $download_hook->( $pkg, $url, $path ); | 
| 37 | 52 |  |  |  |  | 20402 | return $path; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 |  |  |  |  | 0 | Enbld::Message->notify( "--> Download '$file' from '$url'." ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 0 |  |  |  |  | 0 | my $curl = `which curl`; | 
| 43 | 0 |  |  |  |  | 0 | chomp $curl; | 
| 44 | 0 | 0 |  |  |  | 0 | if ( $curl ) { | 
| 45 | 0 |  |  |  |  | 0 | system("$curl -L $url -o $path -s"); | 
| 46 |  |  |  |  |  |  | } else { | 
| 47 | 0 | 0 |  |  |  | 0 | initialize_ua() unless $ua; | 
| 48 | 0 |  |  |  |  | 0 | my $res = $ua->mirror( $url, $path ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 | 0 |  |  |  | 0 | if ( ! $res->{success} ) { | 
| 51 | 0 |  |  |  |  | 0 | Enbld::Error->throw( $res->{reason} ); | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  | 0 | return $path; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub download_archivefile { | 
| 59 | 29 |  |  | 29 | 0 | 59 | my ( $pkg, $url, $path ) = @_; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 29 |  |  |  |  | 111 | my $downloaded = Enbld::HTTP->download( $url, $path ); | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 29 |  |  |  |  | 613 | require Enbld::Archivefile; | 
| 64 | 29 |  |  |  |  | 449 | return Enbld::Archivefile->new( $downloaded ); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub get { | 
| 68 | 35 |  |  | 35 | 0 | 154 | my ( $pkg, $url ) = @_; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 35 | 50 |  |  |  | 154 | if ( $get_hook ) { | 
| 71 | 35 |  |  |  |  | 233 | return $get_hook->( $pkg, $url ); | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 | 0 |  |  |  | 0 | initialize_ua() unless $ua; | 
| 75 | 0 |  |  |  |  | 0 | my $res = $ua->get( $url ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 | 0 |  |  |  | 0 | if ( ! $res->{success} ) { | 
| 78 | 0 |  |  |  |  | 0 | Enbld::Error->throw( $res->{reason} ); | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 |  |  |  |  | 0 | return $res->{content}; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub get_html { | 
| 85 | 35 |  |  | 35 | 0 | 59 | my ( $pkg, $url ) = @_; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 35 |  |  |  |  | 166 | my $content = Enbld::HTTP->get( $url ); | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 35 |  |  |  |  | 1816 | require Enbld::HTML; | 
| 90 | 35 |  |  |  |  | 308 | return Enbld::HTML->new( $content ); | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # For debug methods. | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub register_get_hook { | 
| 96 | 3 |  |  | 3 | 0 | 1089 | my ( $pkg, $coderef ) = @_; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 3 | 50 |  |  |  | 18 | if ( ref( $coderef ) eq 'CODE' ) { | 
| 99 | 3 |  |  |  |  | 5 | $get_hook = $coderef; | 
| 100 | 3 |  |  |  |  | 8 | return $pkg; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 0 |  |  |  |  | 0 | my $err = "register get hook requires CODE reference parameter."; | 
| 104 | 0 |  |  |  |  | 0 | require Enbld::Exception; | 
| 105 | 0 |  |  |  |  | 0 | Enbld::Exception->throw( $err, $coderef ); | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | sub register_download_hook { | 
| 109 | 1 |  |  | 1 | 0 | 9 | my ( $pkg, $coderef ) = @_; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 1 | 50 |  |  |  | 3 | if ( ref( $coderef ) eq 'CODE' ) { | 
| 112 | 1 |  |  |  |  | 2 | $download_hook = $coderef; | 
| 113 | 1 |  |  |  |  | 2 | return $pkg; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 0 |  |  |  |  |  | my $err = "register download hook requires CODE reference parameter."; | 
| 117 | 0 |  |  |  |  |  | require Enbld::Exception; | 
| 118 | 0 |  |  |  |  |  | Enbld::Exception->throw( $err, $coderef ); | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | 1; |