import java.io.*; import java.net.*; import java.util.*; public class Proxy extends Thread { Socket c=null; int port=0; String host=""; String rawUrl="", pname, fname; String CR="\r\n"; byte readIn[]=null; Cache mycache=null; URL url=null; String getfilename="GET.", postfilename="Post."; boolean isCachable=true; int bufferSize=4096; String connection=""; String cachePath="", extra="A."; //** Wait for new requests and open Connection class *// public Proxy(Socket c, int port, String rawUrl, Cache cache) { this.c=c; this.port=port; this.rawUrl=rawUrl; this.mycache=cache; cachePath=mycache.getcachePath(); try { url=new URL(rawUrl); this.host=url.getHost(); this.pname=url.getFile(); this.fname=pname.substring(pname.lastIndexOf('/')+1); System.out.println("host: "+host+" pname: "+pname+" fname: "+fname); } catch(Exception e) { System.err.println(e); } } public void run() { Socket s = null; //** Initiate communications stuff and start a Connection *// try { s = new Socket(host, port); System.out.println("Connection: " + s); System.out.println(CR+"rawUrl: "+rawUrl); // Read get stream c2p(c); System.out.println("Access c2p"+CR); // If it has cached, send it from proxy, else get from server if(mycache.IsCached(rawUrl)) { postfilename=mycache.getFilename(rawUrl); System.out.println("Get "+ postfilename+" from cache."); deleteFile(cachePath+getfilename); p2c(c); System.out.println("Access p2c"+CR); } else { p2s(s); System.out.println("Access p2s"+CR); deleteFile(cachePath+getfilename); s2p(s); System.out.println("Access s2p"+CR); if(isCachable) { p2c(c); System.out.println("Access p2c"+CR); } } // if(connection.equalsIgnoreCase("close")) // { closeConnection(s); closeConnection(c); // } }//try catch (Exception e) { System.out.println(e); } }//run //--- c2p --- Client to Proxy public synchronized void c2p(Socket s) { DataInputStream in = null; PrintWriter outFile=null; int i; String content="", method,path_name,version; StringTokenizer z, pn; String p; try { in=new DataInputStream(s.getInputStream()); content=in.readLine(); int string_length=content.length(); z=new StringTokenizer(content); method=z.nextToken(); //GET path_name=z.nextToken(); version=z.nextToken(); // Generate the filename getfilename+=host; if(path_name.equals("/")) { // Regenerate first line of header content=method + " "+ pname+" "+version; postfilename+=host+".html"; // System.out.println("path_name = /"); } else { rawUrl="http://"+ host+ path_name; pn=new StringTokenizer(path_name, "/"); while(pn.hasMoreTokens()) { p=pn.nextToken(); getfilename+= "."+ p; postfilename+="."+ p; } } getfilename+=".txt"; System.out.println("rawUrl: "+rawUrl); System.out.println("getfilename: "+getfilename+ " postfilename: "+postfilename+CR); // Do not save CGI and search file if(postfilename.indexOf('?')!=-1 ||postfilename.indexOf("cgi-bin")!=-1) { isCachable=false; } // Save get file outFile=new PrintWriter(new FileOutputStream(cachePath+getfilename)); while(string_length!=0 && content!=null) { System.out.println(content); saveFile_header(content,outFile); content=in.readLine(); string_length=content.length(); } outFile.println(); // Write a newline -- this is important, otherwise it will not be recognized outFile.close(); System.out.println("Saved to " + cachePath + getfilename); } catch(IOException e) { System.out.println(e);} catch (Exception e) { closeConnection(s); } }// end c2p public synchronized void deleteFile(String file) { File f=new File(file); f.delete(); } public synchronized void saveFile_header(String st,PrintWriter outFile) { outFile.println(st); outFile.flush(); } //--- p2s --- Proxy to Server public synchronized void p2s(Socket s) { DataInputStream inf=null; DataOutputStream out=null; readIn=new byte[bufferSize]; try { inf=new DataInputStream(new FileInputStream(cachePath+getfilename)); out=new DataOutputStream(s.getOutputStream()); // Open the get file and send to server int read=inf.read(readIn, 0, bufferSize); while(read!=-1) { sendFile(readIn,read,out,s); read=inf.read(readIn,0,bufferSize); } inf.close(); System.out.println(cachePath+getfilename+" send."); } catch(FileNotFoundException e) {e.printStackTrace();} catch(IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} }//end p2s public synchronized void sendFile(byte readIn[],int st, DataOutputStream out, Socket s) { try { out.write(readIn ,0, st); out.flush(); } catch (Exception e) { closeConnection(s); } } //--- s2p --- Server to Proxy public synchronized void s2p(Socket s) { DataInputStream inf=null, in=null; DataOutputStream outf=null; PrintWriter tableFile=null; String Token=""; StringTokenizer tk; readIn=new byte[bufferSize]; try { in=new DataInputStream(s.getInputStream()); // Read post stream header from server String reads=in.readLine(); int len=reads.length(); // System.out.println("length= "+len); String content_type=""; String[] header=new String[10]; int i=0, n=0; // Save header to an array while(len!=0) { header[i]=reads; System.out.println(header[i]); i++; reads=in.readLine(); len=reads.length(); } System.out.println("Read postfile header."); // Save the Content-Type and Connection n=i; for(i=0; i