# $Id: HTTP 11769 2010-08-08 12:26:33Z sparky $
# Direct::HTTP - File getter plugin for rsget.pl
#
# 2010 (c) Przemysław Iskra <sparky@pld-linux.org>
#		This program is free software,
# you may distribute it under GPL v2 or newer.

name: HTTP
short: Direct::HTTP
web: "http://rsget.pl/getters/Direct/HTTP/"
uri: qr{http://.*[^/]}
uri: qr{https://.*[^/]}
slots: max
status: OK 2010-07-12

start:
	GET( $-{_uri}, headonly => 1 );

	if ( /^Location:\s*\s+/mi ) {
		LINK( $-{_referer} );
	}
	ERROR( "file not found" ) unless m{^HTTP/[\d\.]+ 200}m;

	my @type = /^Content-Type:\s*(\S+?)(?:;.*?)?\r?$/mig;
	! my $type = pop @type;
	my ( $len ) = /^Content-Length:\s*(\d+)\r?$/mi;
	$len ||= -1;

	if ( $type =~ m#^text/# ) {
		if ( /^Refresh:\s*.*?url=("|'|)(.*?)\1\s*\r$/mi ) {
			require URI;
			LINK( URI->new( $2 )->abs( $-{_referer} )->as_string );
		}
		ERROR( "file is text ($type)" );
	}
	ERROR( "file is a small image ($type, $len)" )
		if $type =~ m#^image/# and $len < 32 * 1024;

	my ( $name ) = $-{_referer} =~ m{.*/(.*)$};
	if ( /^Content-Disposition:(.+?)\r?$/mi ) {
		$name = RSGet::Curl::content_filename( $1 );
	}

	INFO( name => $name, size => $len );

	DOWNLOAD( $-{_uri} );

# vim: filetype=perl:ts=4:sw=4
