writing a proxy ..



Hi ppl ...
I am writing a simple proxy server ...

I redirect my browser to the local server and re-route the HTTP
request to the host.
I get a reply back from the HOST but unfortunately this is not shown in
my browser ( html appears in my console ) .

I am putting the code here .. ( Its a thread ) pls run with the Server
Class specified below it ..

package com.webproxy.fthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class fthread extends Thread {

Socket orig;

Socket dest;

public fthread(Socket s) {
this.orig = s;

}

public void run() {
String line;
try {

BufferedReader r = new BufferedReader(new InputStreamReader(orig
.getInputStream()));

PrintWriter w = new PrintWriter(new OutputStreamWriter(orig
.getOutputStream()));

BufferedReader r2 = null;

PrintWriter w2 = null;

while(true){
line = r.readLine();
if(line.equals(null) || line.equals(""))
break;
System.out.println( line );
if (line.indexOf("GET") > -1) {
String deststr = line.split(" ")[1].split("/")[2]; //
System.out.println(deststr);
dest = new Socket(deststr, 80);

w2 = new PrintWriter(new OutputStreamWriter(dest
.getOutputStream()));

r2 = new BufferedReader(new InputStreamReader(dest
.getInputStream()));
}

w2.write(line + "\n");
w2.flush();
}

w2.write("\n\n");
w2.flush();

System.out.println("\nNow Reading...\n\n" );

while ((line = r2.readLine()) != null) {
System.out.println(line);
w.write(line);
w.flush();
}

w.close();
r2.close();
r.close();
w2.close();
} catch (IOException e) { // TODO Auto-generated catch
e.printStackTrace();
}

}
}


package webproxy.com.webproxy.main;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import webproxy.com.webproxy.fthread.fthread;

public class ServerClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ServerSocket ss = new ServerSocket(8080);
Socket s = new Socket("localhost", 8080);
Socket s2 = null;
(new PrintWriter(new OutputStreamWriter(s.getOutputStream())))
.write("GET http://www.google.com/ig HTTP/1.0");

s2 = ss.accept();
fthread t = new fthread(s2);
t.start();
t.join();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

.



Relevant Pages

  • Re: Link Checers and the "www"
    ... I find it intensely annoying when a browser re-interprets ... it's the name of the host. ... For servers that have specific purposes, particular the ones being exposed to the public, they'll typically assign server names that denote the ... You can also assign multiple names to the same server, ...
    (comp.infosystems.www.authoring.html)
  • gdm hangs
    ... gdm will hang 9 of 10 times when logging out. ... with or without the client having been connected to the Server. ... # Timed login, useful for kiosks. ... Must output the chosen host on stdout, ...
    (Debian-User)
  • problem with sendmail in solaris 9
    ... names that should be exposed as from this host, ... # save Unix-style "From_" lines at top of header? ... # work recipient factor ... # SMTP STARTTLS server options ...
    (SunManagers)
  • Re: Add new cluster and use existing LUNs?
    ... Storport driver and Powerpath on all of our SAN host servers so we are trying ... In the end I think that I may play it cautious and create a new RAID group, ... > varied activity (DBMSes, Messaging Server, File Server, Web Servers, ... Some of the physical spindle limitations can be addressed through the SAN ...
    (microsoft.public.sqlserver.clustering)
  • Re: How current are web pages?
    ... was last updated on the *server*. ... It is running locally on your own host, ... being checked is the one that was just rendered by the browser. ... Post replies to the newsgroup. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)