#GPL #GPL libwhisker copyright 2000-2004 by rfp.labs #GPL #GPL This program is free software; you can redistribute it and/or #GPL modify it under the terms of the GNU General Public License #GPL as published by the Free Software Foundation; either version 2 #GPL of the License, or (at your option) any later version. #GPL #GPL This program is distributed in the hope that it will be useful, #GPL but WITHOUT ANY WARRANTY; without even the implied warranty of #GPL MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #GPL GNU General Public License for more details. #GPL ######################################################################## =item B Params: $url [, \%request] Return: $code, $data ($code will be set to undef on error, $data will contain error message) This function will fetch the page at the given URL, and return the HTTP response code and page contents. Use this in the form of: ($code,$html)=LW2::get_page("http://host.com/page.html") The optional %request will be used if supplied. This allows you to set headers and other parameters. =cut sub get_page { my ($URL,$hr)=(shift,shift); return (undef,'No URL supplied') if(length($URL)==0); my (%req,%resp); my $rptr; if(defined $hr && ref($hr)){ $rptr=$hr; } else { $rptr=\%req; http_init_request(\%req); } my @u=uri_split($URL,$rptr); return (undef,'Non-HTTP URL supplied') if($u[1] ne 'http' && $u[1] ne 'https'); http_fixup_request($rptr); if(http_do_request($rptr,\%resp)){ return (undef,$resp{'whisker'}->{'error'}); } return ($resp{'whisker'}->{'code'}, $resp{'whisker'}->{'data'}); } ######################################################################## =item B Params: $url [, \%request] Return: $hash_ref (undef on no URL) This function will fetch the page at the given URL, and return the whisker HTTP response hash. The return code of the function is set to $hash_ref->{whisker}->{get_page_hash}, and uses the http_do_request() return values. Note: undef is returned if no URL is supplied =cut sub get_page_hash { my ($URL,$hr)=(shift,shift); return undef if(length($URL)==0); my (%req,%resp); my $rptr; if(defined $hr && ref($hr)){ $rptr=$hr; } else { $rptr=\%req; http_init_request(\%req); } my @u=uri_split($URL,$rptr); # this is newer >=1.1 syntax return undef if($u[1] ne 'http' && $u[1] ne 'https'); http_fixup_request($rptr); my $r=http_do_request($rptr,\%resp); $resp{whisker}->{get_page_hash}=$r; return \%resp; } ######################################################################## =item B Params: $url, $filepath [, \%request] Return: $code ($code will be set to undef on error) This function will fetch the page at the given URL, place the resulting HTML in the file specified, and return the HTTP response code. The optional %request hash sets the default parameters to be used in the request. NOTE: libwhisker does not do any file checking; libwhisker will open the supplied filepath for writing, overwriting any previously-existing files. Libwhisker does not differentiate between a bad request, and a bad file open. If you're having troubles making this function work, make sure that your $filepath is legal and valid, and that you have appropriate write permissions to create/overwrite that file. =cut sub get_page_to_file { my ($URL, $filepath, $hr)=@_; return undef if(length($URL)==0); return undef if(length($filepath)==0); my (%req,%resp); my $rptr; if(defined $hr && ref($hr)){ $rptr=$hr; } else { $rptr=\%req; http_init_request(\%req); } my @u=uri_split($URL,$rptr); # this is newer >=1.1 syntax return undef if($u[1] ne 'http' && $u[1] ne 'https'); http_fixup_request($rptr); return undef if(http_do_request($rptr,\%resp)); open(OUT,">$filepath") || return undef; binmode(OUT); # stupid Windows print OUT $resp{'whisker'}->{'data'}; close(OUT); return $resp{'whisker'}->{'code'}; }