All pastes #745871 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-10-22 19:14 UTC
rendered paste body
import java.io.*;
import java.net.*;
import java.util.*; 



public class Proxy {
	
	private ServerSocket server; // proxy's socket
	private Socket conn; // socket gia thn sundesh tou proxy me ton client(browser) 
	private Socket remote_server; // sockets tou remote server
	private int port; //  port sto opoio akouei o proxy
	private StringBuffer Request; //  to request pou mas dinei o client( browser)
	private StringBuffer Response; //  h apanthsh pou mas epistrefei o  remote server
	private String hostname;// pou tha periexei to zhtoumeno host px - > www.ceid.upatras.gr
	BufferedInputStream clientIn;
	BufferedOutputStream clientOut;
	String line1=null;//apo8ikeuoume thn prwth grammh 
    String line2=null;// apo8ikeuoume thn deuterh grammh
		

	public Proxy(int port) throws IOException { // costruct tou proxy gia na arxikopoiithei
		this.port = port;
		server = new ServerSocket(port);
		Request = new StringBuffer(""); 
		
	}

	public void startProxy() throws IOException {
		while(true) {
			//String host;
			conn = server.accept(); // o proxy akouei kainouries sundeseis pou ginontai sto port 
			
			// arxikopoioume ta streams
			clientIn = new BufferedInputStream(conn.getInputStream());
			clientOut = new BufferedOutputStream(conn.getOutputStream());

			//diabazoume prwta olh thn aithsh
		   getRequest(); 
		   parseFirstLine(line1); // Kanei tokenizing sthn prwth grammh  px -> GET http:/www.ceid.upatras.gr  HTTP/1.0
  	       parseLine(line2);
		    
		   // tipwse ta headers
		   System.out.println(Request.toString());



			// anoigoume socket ston host sto port 80
		
			//String host = "www.yale.edu";
			remote_server = new Socket(InetAddress.getByName(hostname), 80);
			BufferedInputStream fromServer = new BufferedInputStream(remote_server.getInputStream());
			BufferedOutputStream toServer = new BufferedOutputStream(remote_server.getOutputStream());

			// prowthoume ston server to request

			toServer.write(Request.toString().getBytes(), 0, Request.toString().getBytes().length);
			toServer.flush();

			int c;
			Response = new StringBuffer(""); //arxikopoiise ton buffer pou 8a apothikeusei thn apanthsh tou server
			
			// diavazoume tin apantisi - enan enan xaraktires 
			while ((c = fromServer.read()) != -1)
				Response.append((char)c);


			// steile ta pisw ston client
			clientOut.write(Response.toString().getBytes(), 0, Response.toString().getBytes().length);
			clientOut.flush();

			//kleise oles tis sindeseis
			clientIn.close();
			clientOut.close();
			toServer.close();
			fromServer.close();

			System.out.println("Done");



		}
	}

	// i main sinartisi tou programmatos
	public static void main(String [] args) {
		try {
			Proxy pr = new Proxy(Integer.parseInt(args[0])); // to port sto opoio akouei o proxy 
			pr.startProxy();//ksekina  h diadikasia tou Proxy
		} catch (IOException e) { System.out.println(e.toString()); }

	}

	private  void getRequest() throws IOException { //xrhsimopoiw flag gia na ksexwrizw gia poia grammh ths aithshs anaferomai
		String line;
		int flag=1;
	   while ((line = readLine(clientIn)) != null ) { 
			if (line.length() == 0)
			{
			Request.append("\r\n");
				break;
				}
				
			if (flag==1){	
		    line1=line;//bazoume thn 1h grammh
			flag=2;
			break;
			}
			else if (flag==2){
			line2=line; //bazoume thn 2h grammh
			flag=3;
			break;
			 }
			 else{
			 System.out.println("Diabasma ths upoloiphs aithshs!!");
			 break;
			 }
			 }// kleinei to while
			Request.append(line + "\r\n");//auto ginetai se olh thn aithsh
		   }

 private void parseFirstLine(String line1)// Kanei tokenizing sthn prwth grammh  px -> GET http:/www.ceid.upatras.gr/marianna HTTP/1.0
  	{
  		System.out.println("Command = "+line1);
  		StringTokenizer st = new StringTokenizer(line1);
   		String method =st.nextToken().toUpperCase(); //  periexei thn GET 
   		String url = st.nextToken();  //
   		String version = st.nextToken(); // periexei to HTTP 1.0
  	        }
			
private void parseLine(String line2){
      System.out.println(line2);
	  StringTokenizer st = new StringTokenizer(line2,":");
   		String  h=st.nextToken().toUpperCase(); //  periexei thn GET 
   		hostname = st.nextToken();
		System.out.println("congratulation!!!!!!");
		}
 
 
			
			// diavazei mia grammi kathe fora apo to stream 
	private String readLine(BufferedInputStream in) throws IOException {
		StringBuffer data = new StringBuffer(""); 
		int c;
		in.mark(1);
		if (in.read() == -1) 
			return null;
		else
			in.reset();

		while ((c = in.read()) >= 0) {
			if ((c == 0) || (c == 10) || (c == 13))
				break;
			else
				data.append((char)c);
		}
		if (c == 13) {
			in.mark(1);
			if (in.read() != '\n')
				in.reset();
		}
		return data.toString();
	}
}