1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| # Listing 1. Prestuff.pl
use Win32::API::Prototype;
%CACHE_ENTRY = (
COOKIE => 0x00100000,
NORMAL => 0x00000001,
STICKY => 0x00000004,
TRACK_OFFLINE => 0x00000010,
TRACK_ONLINE => 0x00000020,
URLHISTORY => 0x00200000,
SPARSE => 0x00010000
);
my $Url = shift @ARGV || die;
my $UrlFile = shift @ARGV;
my( $UrlExtension ) = ( $Url =~ /\.([^\/]*?)$/ );
my $UrlData;
# BEGIN CALLOUT A
my $Path = NewString( 256 );
# END CALLOUT A
ApiLink( "wininet", "BOOL CreateUrlCacheEntry(
LPCTSTR lpszUrlName,
DWORD dwExpectedFileSize,
LPCTSTR lpszFileExtension,
LPTSTR lpszFileName,
DWORD dwReserved )" );
ApiLink( "wininet", "BOOL CommitUrlCacheEntry(
LPCTSTR lpszUrlName,
LPCTSTR lpszLocalFileName,
DWORD ExpireTimeLow,
DWORD ExpireTimeHigh,
DWORD LastModifiedTimeLow,
DWORD LastModifiedTimeHigh,
DWORD CacheEntryType,
LPCTSTR lpHeaderInfo,
DWORD dwHeaderSize,
LPCTSTR lpszFileExtension,
LPCTSTR lpszOriginalUrl )" );
# BEGIN CALLOUT B
if( "" ne $UrlFile )
{
open( URLFILE, "< $UrlFile" ) || die "Cannot open $UrlFile";
bindmode( URLFILE );
# Locally clear the input record separator variable ($/)
# so that you can read the content of large files.
local( $/ );
$UrlData = join( "", <URLFILE> );
close( URLFILE );
}
else
{
# Add code here to download the file.
}
# END CALLOUT B
if( CreateUrlCacheEntry( $Url, length( $UrlData ), $UrlExtension, $Path, 0 ) )
{
# BEGIN CALLOUT C
open( CACHEFILE, "> $Path" ) || die "Cannot write to '$Path'";
binmode( CACHEFILE );
print CACHEFILE $UrlData;
close( CACHEFILE );
# END CALLOUT C
if( CommitUrlCacheEntry( $Url, $Path, 0, 0, 0, 0, $CACHE_ENTRY{NORMAL},undef, 0, $UrlExtension, $Url ) )
{
print "Url was successfully cached.\n";
}
else
{
print "Failed to prestuff.\n";
}
} |
Partager