Re: writing a proxy ..
- From: "shakah" <shakahshakah@xxxxxxxxx>
- Date: 16 Feb 2006 05:00:41 -0800
ebby83@xxxxxxxxx wrote:
shakah wrote:
ebby83@xxxxxxxxx wrote:
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();
}
}
}
I didn't try executing your code, but maybe you need to add CR/LF pairs
to the socket output so the browser interprets the HTTP response
header, e.g.:
while ((line = r2.readLine()) != null) {
System.out.println(line);
w.write(line);
w.write("\015\012") ;
w.flush();
}
?
thanks .. that helped me get this thing started but I still am having
problems loading the images ( which I think shud be the problem ) which
get garbled or do not get loaded at all .. also complex URLs such as
https://www.google.com/accounts/ServiceLogin?service=ig&passive=true&continue=http://www.google.com/ig&followup=http://www.google.com/ig&cd=US&hl=en&nui=1<mpl=default
dont really work coz of my logic ... but im not sure how to pass the
connection info to the socket class for eg. would the socket connect to
www.google.com/accounts/ServiceLogin?service=ig&passive=true&continue=http://www.google.com/ig&followup=http://www.google.com/ig&cd=US&hl=en&nui=1<mpl=default
(doesnt work in my case ...
also y does this work much slower than a normal proxy ... ??
You really shouldn't be using readLine() to read the HTTP response,
you'd be better off just reading blocks of bytes. That should be
faster, and it should avoid the problems you've seen with binary
content (which the "add CR/LF" hack most likely screwed up). Something
along the lines of (you might have to work out the actual stream
classes to use):
static final int BLOCK_SIZE = 2048 ;
byte [] ab = new byte[BLOCK_SIZE] ;
java.io.InputStream is = dest.getInputStream() ;
java.io.OutputStream os = orig.getOutputStream() ;
while(true) {
// ...read the next chunk of the HTTP response
int nBytesRead = is.read(ab, 0, BLOCK_SIZE) ;
if(nBytesRead < 0) {
break ;
}
else {
// ...write it to the client
os.write(ab, 0, nBytesRead) ;
}
.
- References:
- writing a proxy ..
- From: ebby83
- Re: writing a proxy ..
- From: shakah
- Re: writing a proxy ..
- From: ebby83
- writing a proxy ..
- Prev by Date: Out Of Memory Error and Stack Size
- Next by Date: Re: Logging method information
- Previous by thread: Re: writing a proxy ..
- Next by thread: Re: writing a proxy ..
- Index(es):
Relevant Pages
|