| 1 |
#BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net) |
| 2 |
#BSD All rights reserved. |
| 3 |
#BSD |
| 4 |
#BSD Redistribution and use in source and binary forms, with or without |
| 5 |
#BSD modification, are permitted provided that the following conditions |
| 6 |
#BSD are met: |
| 7 |
#BSD |
| 8 |
#BSD - Redistributions of source code must retain the above copyright |
| 9 |
#BSD notice, this list of conditions and the following disclaimer. |
| 10 |
#BSD |
| 11 |
#BSD - Redistributions in binary form must reproduce the above copyright |
| 12 |
#BSD notice, this list of conditions and the following disclaimer in the |
| 13 |
#BSD documentation and/or other materials provided with the distribution. |
| 14 |
#BSD |
| 15 |
#BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 |
#BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 |
#BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 |
#BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 |
#BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 |
#BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 |
#BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 |
#BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 |
#BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 |
#BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
#BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
#BSD POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
######################################################################## |
| 29 |
|
| 30 |
=item B<get_page> |
| 31 |
|
| 32 |
Params: $url [, \%request] |
| 33 |
|
| 34 |
Return: $code, $data ($code will be set to undef on error, $data will |
| 35 |
contain error message) |
| 36 |
|
| 37 |
This function will fetch the page at the given URL, and return the HTTP response code |
| 38 |
and page contents. Use this in the form of: |
| 39 |
($code,$html)=LW2::get_page("http://host.com/page.html") |
| 40 |
|
| 41 |
The optional %request will be used if supplied. This allows you to set |
| 42 |
headers and other parameters. |
| 43 |
|
| 44 |
=cut |
| 45 |
|
| 46 |
sub get_page { |
| 47 |
my ( $URL, $hr ) = ( shift, shift ); |
| 48 |
return ( undef, 'No URL supplied' ) if ( length($URL) == 0 ); |
| 49 |
|
| 50 |
my ( %req, %resp ); |
| 51 |
my $rptr; |
| 52 |
|
| 53 |
if ( defined $hr && ref($hr) ) { |
| 54 |
$rptr = $hr; |
| 55 |
} |
| 56 |
else { |
| 57 |
$rptr = \%req; |
| 58 |
http_init_request( \%req ); |
| 59 |
} |
| 60 |
|
| 61 |
my @u = uri_split( $URL, $rptr ); |
| 62 |
return ( undef, 'Non-HTTP URL supplied' ) |
| 63 |
if ( $u[1] ne 'http' && $u[1] ne 'https' ); |
| 64 |
http_fixup_request($rptr); |
| 65 |
|
| 66 |
if ( http_do_request( $rptr, \%resp ) ) { |
| 67 |
return ( undef, $resp{'whisker'}->{'error'} ); |
| 68 |
} |
| 69 |
return ( $resp{'whisker'}->{'code'}, $resp{'whisker'}->{'data'} ); |
| 70 |
} |
| 71 |
|
| 72 |
######################################################################## |
| 73 |
|
| 74 |
=item B<get_page_hash> |
| 75 |
|
| 76 |
Params: $url [, \%request] |
| 77 |
|
| 78 |
Return: $hash_ref (undef on no URL) |
| 79 |
|
| 80 |
This function will fetch the page at the given URL, and return the whisker |
| 81 |
HTTP response hash. The return code of the function is set to |
| 82 |
$hash_ref->{whisker}->{get_page_hash}, and uses the http_do_request() |
| 83 |
return values. |
| 84 |
|
| 85 |
Note: undef is returned if no URL is supplied |
| 86 |
|
| 87 |
=cut |
| 88 |
|
| 89 |
sub get_page_hash { |
| 90 |
my ( $URL, $hr ) = ( shift, shift ); |
| 91 |
return undef if ( length($URL) == 0 ); |
| 92 |
|
| 93 |
my ( %req, %resp ); |
| 94 |
my $rptr; |
| 95 |
|
| 96 |
if ( defined $hr && ref($hr) ) { |
| 97 |
$rptr = $hr; |
| 98 |
} |
| 99 |
else { |
| 100 |
$rptr = \%req; |
| 101 |
http_init_request( \%req ); |
| 102 |
} |
| 103 |
|
| 104 |
my @u = uri_split( $URL, $rptr ); # this is newer >=1.1 syntax |
| 105 |
return undef if ( $u[1] ne 'http' && $u[1] ne 'https' ); |
| 106 |
http_fixup_request($rptr); |
| 107 |
|
| 108 |
my $r = http_do_request( $rptr, \%resp ); |
| 109 |
$resp{whisker}->{get_page_hash} = $r; |
| 110 |
return \%resp; |
| 111 |
} |
| 112 |
|
| 113 |
######################################################################## |
| 114 |
|
| 115 |
=item B<get_page_to_file> |
| 116 |
|
| 117 |
Params: $url, $filepath [, \%request] |
| 118 |
|
| 119 |
Return: $code ($code will be set to undef on error) |
| 120 |
|
| 121 |
This function will fetch the page at the given URL, place the resulting HTML |
| 122 |
in the file specified, and return the HTTP response code. The optional |
| 123 |
%request hash sets the default parameters to be used in the request. |
| 124 |
|
| 125 |
NOTE: libwhisker does not do any file checking; libwhisker will open the |
| 126 |
supplied filepath for writing, overwriting any previously-existing files. |
| 127 |
Libwhisker does not differentiate between a bad request, and a bad file |
| 128 |
open. If you're having troubles making this function work, make sure |
| 129 |
that your $filepath is legal and valid, and that you have appropriate |
| 130 |
write permissions to create/overwrite that file. |
| 131 |
|
| 132 |
=cut |
| 133 |
|
| 134 |
sub get_page_to_file { |
| 135 |
my ( $URL, $filepath, $hr ) = @_; |
| 136 |
|
| 137 |
return undef if ( length($URL) == 0 ); |
| 138 |
return undef if ( length($filepath) == 0 ); |
| 139 |
|
| 140 |
my ( %req, %resp ); |
| 141 |
my $rptr; |
| 142 |
|
| 143 |
if ( defined $hr && ref($hr) ) { |
| 144 |
$rptr = $hr; |
| 145 |
} |
| 146 |
else { |
| 147 |
$rptr = \%req; |
| 148 |
http_init_request( \%req ); |
| 149 |
} |
| 150 |
|
| 151 |
my @u = uri_split( $URL, $rptr ); # this is newer >=1.1 syntax |
| 152 |
return undef if ( $u[1] ne 'http' && $u[1] ne 'https' ); |
| 153 |
http_fixup_request($rptr); |
| 154 |
return undef if ( http_do_request( $rptr, \%resp ) ); |
| 155 |
|
| 156 |
open( OUT, ">$filepath" ) || return undef; |
| 157 |
binmode(OUT); # stupid Windows |
| 158 |
print OUT $resp{'whisker'}->{'data'}; |
| 159 |
close(OUT); |
| 160 |
|
| 161 |
return $resp{'whisker'}->{'code'}; |
| 162 |
} |
| 163 |
|