HI,,I am currently writting a web proxy in java.The problem that I currently have is that my http response keeps hanging..Can anyone please spot the error in my code.
import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; public class Server { ServerSocket proxyserver=null; InetSocketAddress adr; ClientHandler handler; Socket socket; public Server(){ adr= new InetSocketAddress("localhost",8088); try { proxyserver= new ServerSocket(); proxyserver.bind(adr); System.out.println("Serversocket created"); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Server server= new Server(); server.run(); } public void run() { while(true){ try { System.out.println("Waiting for connection to be made to proxy server"); //socket= proxyserver.accept(); new Thread( new ClientHandler(proxyserver.accept())).start(); // calls the start method to start a thread which also starts the run method } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } import java.net.*; import java.io.*; public class ClientHandler implements Runnable{ Socket socket; Socket serverSocket; private InputStream clientinput; private OutputStream clientoutput; private HttpRequest httprequest; private HttpResponse httpresponse; BufferedWriter toClient; BufferedWriter toServer; public ClientHandler(Socket socket){ this.socket=socket; System.out.println("Accepted a request!\n" + httprequest); try { this.clientinput=socket.getInputStream(); this.clientoutput=socket.getOutputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } httprequest= new HttpRequest(clientinput); } @Override public void run() { while (true){ try{ System.out.println("Accepted a request!\n" + httprequest); while(httprequest.busy); URL url = null; try { url = new URL(httprequest.getRequestURL()); } catch (MalformedURLException e){ try { url = new URL("http:\\"+httprequest.getRequestHost()+httprequest.getRequestURL()); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } // { int remotePort = (url.getPort() == -1) ? 80 : url.getPort(); System.out.println("I am going to try to connect to: " + url.getHost() + " at port " + remotePort); serverSocket = new Socket(url.getHost(), 80); System.out.println("Connected to tcd proxy"+ serverSocket.getLocalAddress()); //write to the server - keep it open. System.out.println("Writing to the server's buffer..."); toServer = new BufferedWriter(new OutputStreamWriter(serverSocket.getOutputStream())); toServer.write(httprequest.getFullRequest()); toServer.flush(); System.out.println("flushed."); System.out.println("Getting a response..."); httpresponse = new HttpResponse(serverSocket.getInputStream()); System.out.println("Got a response!\n" + httpresponse); while(httpresponse.isBusy()); // } toClient = new BufferedWriter(new OutputStreamWriter(clientoutput)); toClient.write(httpresponse.getFullResponse()); toClient.flush(); toClient.close(); this.socket.close(); } catch (IOException e) { e.printStackTrace(); continue; } }//while } } import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class HttpResponse{ private String fullResponse = ""; private BufferedReader reader; private boolean busy=true; // private int responseCode; // private CacheControl cacheControl; public HttpResponse(String input) { this(new ByteArrayInputStream(input.getBytes())); } public HttpResponse(InputStream input) { //busy=true; reader = new BufferedReader(new InputStreamReader(input)); try { while (!reader.ready());//wait for initialization. String line; while ((line = reader.readLine()) != null) { fullResponse += "\r\n" + line; //System.out.println("hellooo " +fullResponse); } reader.close(); fullResponse = "\r\n" + fullResponse.trim() + "\r\n\r\n"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } busy = false; } public String getFullResponse() { return fullResponse; } public boolean isBusy() { return busy; } //@Override public String toString() { return "Response\n==============================\n" + fullResponse; } } import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /*This class is used to take in a stream which contains a Http request and produce parse valuable information form like host name url The http request would be get request in this case */ public class HttpRequest{ private BufferedReader request; // bufferedreader String httpfullrequest=""; private String httprequestHost; private String httprequestURL; public boolean busy=true; public HttpRequest(String input) { this(new ByteArrayInputStream(input.getBytes())); } public HttpRequest( InputStream inputstream){ request = new BufferedReader(new InputStreamReader(inputstream)); parserequest(); } /* * This private function parses a request */ private void parserequest(){ try { while (!request.ready());//wait for buffer not to be empty String line; while ((line = request.readLine()) != null) { if(!line.startsWith("Accept-Encoding:")) httpfullrequest += "\r\n" + line; if(line.startsWith("GET ")){httprequestURL=line.split(" ")[1];System.out.println("url \""+httprequestURL+"\"");} if(line.startsWith("Host:")){httprequestHost=line.substring(6);System.out.println("Host \""+httprequestHost+"\"");} if(line.length()==0){System.out.println("empty line");break;} } httpfullrequest = "\r\n" + httpfullrequest.trim() + "\r\n\r\n"; // request.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } busy=false; } public boolean isBusy() { return busy; } public String getFullRequest() { return httpfullrequest; } public String getRequestURL() { return httprequestURL; } public String getRequestHost() { return httprequestHost; } //@Override public String toString() { return "Request\n==============================\n" +httpfullrequest ; } }//http request class