// Show cookie headers
import java.io.*;
import java.net.*;

public class CookieProxy extends HttpProxy {
    static private StringBuffer line[];
    public CookieProxy(Socket s) {
        super(s);
        line=new StringBuffer[2];
        line[0]=line[1]=null;
    }
    public void writeLog(int c, boolean browser) throws IOException {
        int index=browser?1:0;
        if (line[index]==null) line[index]=new StringBuffer();
        if (c=='\r' || c=='\n') {
            // Heuristic guesses when it sees a cookie header
            // not perfect, but good enough
            if (line[index]==null) return; // nothing
            if (line[index].length()<(browser?7:11)) return;

            if (line[index].substring(0,browser?6:10).
              compareToIgnoreCase(browser?"cookie":"set-cookie")==0) {
                System.out.print(browser?"Sent ":"Received ");
                System.out.println(line[index]);
            }
            line[index]=null;
            return;
        }
        line[index].append((char)c);
    }
    public String processHostName(String url, String host, int port, 
      Socket sock) {
// do nothing
       return host;
    }
// Very simple test main
    static public void main(String args[]) {
      System.out.println("Starting Cookieproxy on port 808<BR>");
      HttpProxy.log=System.out;
      HttpProxy.logging=true;
      HttpProxy.startProxy(808,CookieProxy.class);
      }


}
