// Link Checker
import java.net.*;
import java.io.*;
import java.util.*;

public class LinkCk {

    static int lastStatus=0;

    public static boolean check(String URL,String host,String root) 
      throws IOException {
	String theHost=host;
	String dir="";
	int n,n1,port=80;
	// interpret URL
	n=root.lastIndexOf('/');
	if (n>0) dir=root.substring(0,n+1);
	if (dir.length()!=0 && dir.charAt(0)!='/') dir="/"+dir;
	// check for protocol string other than http:, forget it
	n=URL.indexOf(':');
	n1=URL.indexOf('/');
	if (n!=-1 && (n1>n || n1==-1)) {
	    if (URL.substring(0,5).compareToIgnoreCase("http:")!=0) 
		return true;
	    n=URL.indexOf('/',7);
	    if (n==-1) {
		theHost=URL;
		URL="/";
	    } else {
		theHost=URL.substring(0,n);
		URL=URL.substring(n);
	    }
	    theHost=theHost.substring(7); // remove http://
	}
	else {
	    if (URL.charAt(0)!='/') URL=dir+URL;
	}

	n=theHost.indexOf('@');  // userid/password?
	if (n!=-1) theHost=theHost.substring(n+1);
	n=theHost.lastIndexOf(':');
	try {
	    if (n!=-1) {
		port=Integer.parseInt(theHost.substring(n+1));
		theHost=theHost.substring(0,n);
	    }
	    
	    // try to guess if this is a directory
	    // this is an imperfect guess
	    if (URL.indexOf('.')==-1&&URL.charAt(URL.length()-1)!='/') 
		URL=URL+"/"; 
	    // make the request and see if it works
	    String cmd="HEAD " + URL + " HTTP/1.1\r\n",line;
	    Socket sock = request(theHost,port,cmd);
	    BufferedReader rdr=new BufferedReader(
			  new InputStreamReader(sock.getInputStream()));
	    line=rdr.readLine();
	    if (line==null) return false;
	    return getStatus(line)==200;
	}
	catch (Exception e) {
	    System.out.println(e);
	    return false;
	}
    }


    // Pick out the status
    public static int getStatus(String response) {
	StringTokenizer token = new StringTokenizer(response);
	token.nextToken();
	lastStatus=Integer.parseInt(token.nextToken());
	return lastStatus;
    }

    // Make a Version 1.1. request
    public static Socket request(String host,int port,String cmd) 
	throws UnknownHostException, IOException {
	Socket skt = new Socket(host,port);
	cmd+="host: " + host +"\r\nconnection: close\r\n\r\n";
	skt.getOutputStream().write(cmd.getBytes());
	return skt;
    }


    public static void main(String[] args) throws Exception {
	String line;
	// make a 1.0 request here so we avoid the chunked response encoding
	String cmd="GET /" + args[1]+" HTTP/1.0\r\n";
	Socket skt = request(args[0],80,cmd);



	BufferedReader rdr=new BufferedReader(
			      new InputStreamReader(skt.getInputStream()));
	boolean headers=true;
	try {
	    // check status code
	    line=rdr.readLine();
	    if (getStatus(line)!=200) {
		System.out.println(line);
		System.exit(0);
	    }
	    do {
		line=rdr.readLine();
		if (line!=null) {
		    if (line.equals("")) {
			headers=false;
			continue;
		    }
		    // skip headers
		    if (headers) continue;
		    String Uline = line.toUpperCase();
		    int n=0,n1;
		    // pick out hyperlinks
		    // don't try to exclude those commented out
		    do {
			n=Uline.indexOf("HREF=",n);
			if (n!=-1) {
			    n+=5;
			    if (line.charAt(n)=='"') {
				n1=line.indexOf('"',++n);
			    }
			    else if (line.charAt(n)=='\'') {
				n1=line.indexOf('\'',++n);
			    }
			    else {
				int n2;
				n1=line.indexOf(' ',n);
				n2=line.indexOf('>',n);
				if (n1==-1||n2<n1) n1=n2;
			    }
			    // detect empty tags (why do people do this?)
			    if (n==n1) continue;
			    String link=line.substring(n,n1);
			    if (link.charAt(0)=='#') continue; // local
			    System.out.print(link + "... ");
			    System.out.println(
				      check(link,
					args[0],args[1])?"OK":("*BAD* " + lastStatus));
			    n=n1;
			}
		    } while (n!=-1); // try another line
		} 
		
	    }  while (line!=null);
	}
	catch (IOException e) {	}
    }

}



