# https://issues.jboss.org/browse/JBCS-129

#!/usr/bin/perl

use Term::ANSIColor;
use HTTP::Request;
use HTTP::Request::Common qw(GET);
use URI::URL;      
use LWP;

my $ua = LWP::UserAgent->new; # LWP var to send requests
   $ua->agent('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.3) Gecko/20010801');
   $ua->timeout(15);
   $ua->protocols_allowed([qw( http https )]);  # Playing it safe.


Main();

sub Main {
  my $url = 'http://localhost/fake';
  my $url2 = 'http://localhost/example/index.html';
  
  # First Request
  for(my $i = 1; $i <= 3; $i++)
  {
    print color("bold green"), "[+] Sending HEAD request on : $url\n";
    my $head_response = send_request($url, 'HEAD', 'text/html; charset=UTF-8');
    push(@responses, "\nHEAD Request #$i : \n" . $head_response->content);
  }
  
  # Second Request
  print color("bold green"), "[+] Sending GET request on : $url2\n";
  my $get_response = send_request($url2, 'GET', 'text/html; charset=UTF-8');
  push(@responses, "\nGET Request #1 : \n" . $get_response->content);
  
  foreach my $result (@responses)
  {
    print color("bold green"), "$result\n";
  }
}

sub send_request {
  
  my($url, $method, $content_type) = @_;
  my $request;
  my $response;
  my $page_content;
  
  $request = new HTTP::Request $method, $url;
  $request->content_type($content_type);
  $request->header(Connection => 'Keep-Alive');
  $request->content($data);
  $response = $ua->request($request);
  
  return $response;
}