> I replaced most of the readLine() 's except one ( while reading from
> the browser .. to get the destination address ) but I convert it to
> bytes and then write it to the server ... So now I do get a reply from
> the browser but I get a BAD REQUEST.
I dont quite understand your code, To clear things up, is this code for
an http proxy? if so why does the request originate with the proxy and
where does your browser come into it. Can you explain the architecture
of this thing, I usually expect there to be a Client (C) talking to a
Proxy (P) which relays the request to a Server (S). P rewrites the
request to fit its policy and to adhere to the HTTP rfc and it also does
this with any replies. To me it does not seem to be that way it works on
your system?
> I get this output
>
[quoted text clipped - 18 lines]
>
> Now Reading...
I can understand you are getting a bad request, because this does not
look much like a proper HTTP request, even if its proxied. I have no
idea what the stuff after the "---:---" is.
What I suggest is that you use a network sniffer to read the raw tcp
packets sent to the proxy from the browser, and from the proxy to the
server. This way you can see what is the original request and how your
server is modifying it, and finally what the responses are.
But before doing this I recommend you do the same thing for a pure
request from the browser to a server, without a proxy or anything you
have produced in between. That way you can see what the real data is
supposed to look like. You could, if you want set up an apache proxy to
study how that looks like as well. But I recommend reading the rfc's in
any case.
/tom
ebby83@gmail.com - 20 Feb 2006 19:45 GMT
Thanks all .. Im having different problems though . I cant get a method
synchronized ...
I am loggin (appending) entries into a text file with a sync block and
locking the whole class ie sync (this) .. unfortunately the text in the
file always gets overwritten ..
Its in the writedata method
//method
void writedata(String str) {
synchronized (this) {
try {
BufferedWriter w3 = new BufferedWriter(new FileWriter(file));
w3.append(str);
w3.flush();
w3.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//actual class code ...
package com.webproxy.fthread;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;
public class fthread extends Thread {
/**
* A Socket that connects to the clients browser.
*/
Socket orig;
/**
* A socket that connects to the http server.
*/
Socket dest;
/**
* The log file that logs the status of all requests.
*/
File file = new File("c:\\proxy.log");
/**
* The constructor for this thread.
*
* @param s
* The socket representing the connection to the clients
browser.
*/
public fthread(Socket s) {
this.orig = s;
}
void writedata(String str) {
synchronized (this) {
try {
BufferedWriter w3 = new BufferedWriter(new FileWriter(file));
w3.append(str);
w3.flush();
w3.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
String line;
try {
BufferedReader r = new BufferedReader(new InputStreamReader(orig
.getInputStream()));
OutputStream w = orig.getOutputStream();
InputStream r2 = null;
OutputStream w2 = null;
while (true) {
line = r.readLine(); // read line from the browser
// System.out.println(line);
if (line.equals(null) || line.equals("")) // check if this is
// the end of the
// stream.
break;
if (line.indexOf("GET") > -1 || line.indexOf("HEAD") > -1
|| line.indexOf("POST") > -1) {// CHECK the first line
// of the request for a
// HEAD ,GET ,POST to
// retrieve the
// sestination address
// for this request.
String url = line.split(" ")[1].split("/")[2]; // retrieve
// the dest
// HostName
// for this
// request.
System.out.println("\t\t" + url);
dest = new Socket(url, 80);// connect to the destination.
w2 = dest.getOutputStream(); // get input and output
// streams to the dest
// 'server'.
r2 = dest.getInputStream();
}
w2.write(line.getBytes()); // write line to server
w2.write("\015\012".getBytes()); // end it with CRLF
w2.flush(); // flush
}
w2.write("\n\n".getBytes()); // end stream with 2 blank lines
w2.flush();
System.out.println("\nNow Reading...\n\n");
int bytes;
int totbytes = 0;
byte[] bytebuf = new byte[4096];
while ((bytes = r2.read(bytebuf, 0, 4096)) > -1) {
totbytes += bytes;
// System.out.println(new String(bytebuf));
w.write(bytebuf, 0, bytes);
w.flush();
// file.write(bytebuf, 0, bytes);
}
w.close();
r2.close();
r.close();
w2.close();
String writestr = new Date().toString() + " "
+ this.orig.getInetAddress().getHostAddress() + " "
+ dest.getInetAddress().getHostName() + " "
+ String.valueOf(totbytes) + "\n";
System.out.println(writestr);
writedata(writestr);
dest.close();
} catch (IOException e) { // Catch any IO errors.
e.printStackTrace();
}
}
}