vendredi 10 avril 2015

Java proxy server fails to load the whole web page, but succeeds in loading a part of it

my Java proxy server fails to load the whole web page, but succeeds in loading a part of it. For example, when I go to webpage, connection will time out. But if I just load one component (eg. http://ift.tt/1yk12LS) then it is okay. Sorry the code is a bit long...



class ThreadPerConnect implements Runnable {
Socket clientSocket = null;

public ThreadPerConnect(Socket cs) {
clientSocket = cs;
}

public void run() {
try {
System.out.println(clientSocket.getRemoteSocketAddress());
StringBuilder sb = new StringBuilder();

// handle IO in same thread now
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
clientSocket.getOutputStream());
String request = inFromClient.readLine();
// read request from client
System.out.println(request);
sb.append(request + "\r\n");
String abc;

System.out.println("inFromClient: ");
while (!(abc = inFromClient.readLine()).isEmpty()) {
sb.append(abc + "\r\n");
System.out.println("abc is " + abc);
}
sb.append("\r\n");

String finalS = sb.toString();
System.out.println("finalS is: \n" + finalS);

// get the url
StringTokenizer st = new StringTokenizer(request);
String requestType = st.nextToken(); // skip the "GET"
String url = st.nextToken();
System.out.println("Url is " + url);

// remove "http://"
if (url.startsWith("http://"))
url = url.substring(7);

String[] parts = url.split("/", 2);
String ipRaw = parts[0];
String[] parts2 = ipRaw.split(":");
String ip = parts2[0];

int port;
if (parts2.length > 1)
port = Integer.parseInt(parts2[1]);
// get port number from request
else
port = 80;

String fileName;
if (parts.length > 1)
fileName = "/" + parts[1];
else
fileName = "/";

String httpVersion = st.nextToken();

// get the filename

System.out.printf(
"Ip is %s\t \nfileName is %s \nhttpVersion is %s", ip,
fileName, httpVersion);

// go surf actual web
Socket anotherSocket = new Socket(ip, port);

System.out.println("\ncreate anotherSocket success");

PrintWriter out = new PrintWriter(anotherSocket.getOutputStream());
DataInputStream in = new DataInputStream(
anotherSocket.getInputStream());
out.print(finalS);
out.flush();

// getting actual content
String x;
int contentLength = 0;

while (true) {
x = in.readLine();

if (x == null)
break;
if (x.isEmpty()) {
outToClient.writeBytes("\r\n");
System.out.println("END HEAD");
break;
}
outToClient.writeBytes(x + "\r\n");
System.out.println("x = " + x);
outToClient.flush();

if (x.startsWith("Content-Length:")) {
contentLength = Integer.parseInt((x.split(": "))[1]);
}
}

byte[] content = new byte[contentLength];
in.readFully(content);

outToClient.write(content, 0, contentLength);
outToClient.flush();
System.out.println("write done: " + contentLength + " bytes");

// now write back information to client
in.close();
out.close();
anotherSocket.close();
} catch (Exception e) {
e.printStackTrace();
}

}


}





Aucun commentaire:

Enregistrer un commentaire