package Requester::realhttp;

use strict;
use Requester::Requester;

@Requester::realhttp::ISA = qw(Requester::Requester);

use MicroTime;
use LWP::UserAgent;

my $realhttp = new Requester::realhttp();

sub new
{
   my( $this, $args ) = @_;
   my $class = ref($this) || $this;
   my $self = {};
   bless( $self, $class );
   $self->register("realhttp");
   return( $self );
}


sub getinfo {
    my( $self, $vars, $indexes ) = @_;

    # we cannot walk the realhttp domain
    return undef unless( $indexes );
    my $info = {};

    my $env = $self->{"env"};
    my $url = $env->expand_value( "url" );
    my $timeout = $env->expand_value( "timeout" );
    my $user = $env->expand_value( "user" );
    my $pass = $env->expand_value( "password" );
    my $greppos = $env->expand_value( "match" );
    my $grepneg = $env->expand_value( "notmatch" );
    my $maxsize = $env->expand_value( "maxsize" );
    $maxsize = 1024 unless( $maxsize );
    my $noredirect = $env->{args}->{noredirect};
    if ( !defined($noredirect) || $noredirect=="" ) {
	$noredirect=1;
    }
    $noredirect = ! ($noredirect =~ /^(false|f|0|no)$/);
    my $extraheaders = $env->{args}->{extraheaders};
    my $extraheadersvalue = $env->{args}->{extraheadersvalue};

    foreach my $test (@$vars) {
        unless( $test =~ /^(time|status|message|log)\.(\d+)$/ ) {
	    print "   realhttp cannot handle $test\n" if( $BigSister::common::dl > 4 );
	    next;
	}
	my $id = $2;
	next if( defined $info->{$id} );
	$info->{$id} = {};

	my $request;
        if( $user || $extraheaders ) {
            my $header = new HTTP::Headers();
            if( $user ) {
                $header->authorization_basic( $user, $pass );
            }
            if( $extraheaders && $extraheadersvalue) {
                $header->header($extraheaders => $extraheadersvalue);  # set
            }
	    $request = new HTTP::Request( GET => $url, $header );
	}
	else {
	    $request = new HTTP::Request( GET => $url );
	}

	my $agent = new LWP::UserAgent();
	$agent->timeout( $timeout ) if( $timeout );

	if ($noredirect) {
		my @toto;
		$agent->requests_redirectable( \@toto );
	}

	my $start = MicroTime::gettimeofday();
	my $result;
	eval {
	    $result = $agent->request( $request );
	};
	my $responsetime = int( MicroTime::difftime( $start, MicroTime::gettimeofday() ) * 100 + 0.5 ) / 100;

	my $status;
	my $text;
	my $head;
	$result && ($head = $result->headers->as_string());
	#if( $result && $result->is_success ) {
	if( $result && ($result->is_success || ( $noredirect && $result->is_redirect)) ) {
	    my $content = $result->content();
	    eval {
		if( $greppos && ! ($content =~ /$greppos/) ) {
		    $status = 0;
		    $text = "FAILED: result did not match";
		}
		elsif( $grepneg && ($content =~ /$grepneg/) ) {
		    $status = 0;
		    $text = "FAILED: found notmatch string";
		}
		else {
		    $status = 3;
		    $text = "OK";
		}
	    };
	}

	else {
	    $status = 0;
	    if( $timeout && ($responsetime >= $timeout) ) {
		$text = "TIMED OUT";
	     }
	     else {
		 $responsetime = -1;
		 $text = "FAILED: could not retrieve";
	     }
	}

	($info->{$id}->{"log"} = $head) if( $head );
	$info->{$id}->{"time"} = $responsetime;
	$info->{$id}->{"message"} = $text;
	$info->{$id}->{"status"} = $status;
    }
    return $info;
}

1;
