# Register the routine with the plugins registry
$PLUGINS{'http'} = \&check_http;

# Http is a little special in that we also check the return code.  No
# connection and 5xx codes are red, but 4xx return codes are just yellow.
# Also we go through a list of documents that the have been provided and check
# each one of them to make sure the web server is behaving correctly

sub check_http {
   my( $host ) = @_;
   my( @http_files ) = ( @{$HTTPDOCS{"ALL"}}, @{$HTTPDOCS{$host}} );
   my( $http_port ) = $HTTPPORT{$host} || $HTTPPORT{"ALL"} || 80;

   # Find list of URL's to check
   my( @http_urls ) = @{$HTTPURLS{$host}}; 
   @http_urls = @{$HTTPURLS{'DEFAULT'}}  if( ! @http_urls );
   @http_urls = @{$HOSTS{$host}->{'http_urls'}} if( ! @http_urls );
   @http_urls = @{$HOSTS_DEFAULTS{'http_urls'}} if( ! @http_urls );

   # Append mandatory urls to check
   @http_urls = ( @http_urls, @{$HOSTS_ALL{'http_urls'}} ); 

   my( $file, $tmessage ) = ( "", "" );
   my( $color, $summary ) = ( "green", "" );

   # If HTTP URLS are defined use the URL check
   if ( @http_urls ) {
      foreach my $url (@http_urls) {
         # Parse the URL into it's components
         $url =~ s|^http://||;  # Remove the protocol id if present
         my($hpart,$urlpath) = ( $url =~ m|^([^/]+)(/.*)| );
         my($hname,$port) = split(/:/,$hpart);
         $port = 80 if ! $port;
         #$hname = $host if ( ! $hname || $hname eq '_HOST_' );
         if ( ! $hname ) {
            $hname = $host;
         } elsif ( $hname eq '_HOST_' ) {
            $hname = $host;
            $url =~ s/_HOST_/$host/;
         }
         
         my $message = 
	    &check_tcp( $hname, $port,
               "HEAD $urlpath HTTP/1.1\r\nHost: $hname:$port\r\n\r\n", 10 );

         if( $message =~ /HTTP\S+\s+(\d\d\d)\s.*$/m ) {
            my $code = $1;
	 
	    if( $code >= 500 ) {
	       $color = "red"; $summary = "http error - $code - $url";
               # Treat a 401 (authorization Required) code as a green
	    } elsif( $code >= 400 && $code != 401) {
	       if( $color ne "red" ) {
	          $color = "yellow"; $summary = "http warning - $code - $url"; }
	    } else { 
	       if( $color ne "red" && $color ne "yellow" ) {
	          $color = "green"; $summary = "http ok - $code"; }
	    }
         } elsif( $message !~ /HTTP/m ) { 
	    $color = "red"; $summary = "no response from http server";
         } else {
	    if( $color ne "red" ) {
	       $color = "yellow"; $summary = "can't determine status code";}
         }
         $tmessage .= "->HEAD $urlpath HTTP/1.1\nHost: $hname:$port\n$message\n\n";
      }

   } else {

      foreach $file ( @http_files ) {
         my $message = 
	    &check_tcp( $host, $http_port, "HEAD $file HTTP/1.0\r\n\r\n", 10 );
      
         if( $message =~ /HTTP\S+\s+(\d\d\d)\s.*$/m ) {
	    my $code = $1;
	 
	    if( $code >= 500 ) {
	       $color = "red"; $summary = "http error - $code - $file";
               # Treat a 401 (authorization Required) code as a green
	    } elsif( $code >= 400 && $code != 401) {
	       if( $color ne "red" ) {
	          $color = "yellow"; $summary = "http warning - $code - $file";
	       }
	    } else { 
	       if( $color ne "red" && $color ne "yellow" ) {
	          $color = "green"; $summary = "http ok - $code"; }
	    }
         } elsif( $message !~ /HTTP/m ) { 
	    $color = "red"; $summary = "no response from http server";
         } else {
	    if( $color ne "red" ) {
	       $color = "yellow"; $summary = "can't determine status code";}
         }
         $tmessage .= "->HEAD $file HTTP/1.0\n$message\n\n";
      }
   }
   
   &debug( "http - $host - $color, $summary" );
   return( $color, $summary, $tmessage );
}

1;
