Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default append response to the request from Socket and write to another socket

    I have created proxy server. It runs on port 9000.

    I made proxy settings in my web browser to handle the request coming at port 9000. When I hit any URL on Web browser then I read the data from socket InputStream and write thr data back to Socket outputstream.

    Now I want to append some response (in the form of Javascript) with the Incoming request to get the properties of various component on the webpage but when I write the response I can't see any script (sent in the response ) appended in the webpage that is loaded in the web browser .

    My code is as under....Please help


    Java Code:
    import java.net.*; 
    import java.io.*; 
    
    import com.impetus.logging.*; 
    import com.impetus.threadpool.*; 
    import com.impetus.util.*; 
    
    public class ProxyJob implements Runnable { 
    
    //client socket 
    Socket client_socket = null; 
    
    //flag for the HTTP Tunnel Threads 
    boolean runFlag = false; 
    
    /** 
    * Constructor 
    * 
    * @param socket client socket 
    */ 
    public ProxyJob(Socket socket) { 
    client_socket = socket; 
    } 
    
    /** 
    * Run method the thread 
    */ 
    public void run() { 
    
    //TODO: proper status code to be set and sent to client on exceptions 
    //also log the exceptions 
    
    BufferedReader br = null; 
    HTTPHeader httpHeader = null; 
    Socket server_socket = null; 
    
    try { 
    
    //first read the HTTP header from client request 
    br = new BufferedReader ( new InputStreamReader( client_socket.getInputStream())); 
    String result = null; 
    
    httpHeader = new HTTPHeader(); 
    
    while ( (result=br.readLine()) != null) { 
    
    //in HTTP header, there is a blank line "\r\n" after the header part 
    if ( result.trim().equals("")) { 
    break; 
    } 
    httpHeader.addHeader(result); 
    } 
    
    } catch ( IOException ex) { 
    Log.log(ex); 
    HTTPErrorResponse.sendErrorResponse(client_socket, 400); 
    return; 
    } 
    
    try { 
    //create the server socket 
    server_socket = new Socket(httpHeader.getHost(), httpHeader.getPort()); 
    
    //TODO:whether to set timeout 
    //server_socket.setSoTimeout(); 
    } catch (Exception ex) { 
    Log.log(ex); 
    HTTPErrorResponse.sendErrorResponse(client_socket, 502); 
    return; 
    } 
    
    try { 
    
    //check whether SSL or CONNECT method 
    if ( ! httpHeader.getMethod().equals("CONNECT")) { 
    
    //write to server the HTTP Header received from client 
    BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( server_socket.getOutputStream())); 
    bw.write(httpHeader.toString()); 
    System.out.println("Header : "+httpHeader.toString()); 
    
    //if POST method, post the necessary data 
    if ( httpHeader.getMethod().equals("POST")) { 
    int result = 0; 
    char buf[] = new char[4096]; 
    int total_char_read = 0; 
    while( (result = br.read(buf,0,buf.length)) != -1) { 
    bw.write(buf,0,result); 
    total_char_read += result; 
    if ( total_char_read >= httpHeader.getContentLength()) break; 
    } 
    } 
    
    bw.flush(); 
    
    [B]Here onwards I am reading data from socket and writing to another socket [/B]
    ///////////////////////////////////////////////////////////////////////////////////////
    //TODO: deal with HTTP/1.1 
    // read the data from server and send the same to client 
    BufferedInputStream in = new BufferedInputStream(server_socket.getInputStream()); 
    BufferedOutputStream os = new BufferedOutputStream( client_socket.getOutputStream()); 
    byte[] buf = new byte[512]; 
    int result1 = 0; 
    
    
    while( (result1=in.read(buf,0,buf.length)) != -1) { 
    //System.out.println("Result : "+new String(buf)); 
    os.write(buf,0,result1); 
    os.flush(); 
    
    /*os.write(js.getBytes()); 
    os.flush();*/ 
    } 
    
    //////////////////////////////////////////////////// 
    [B]Here I am writing my own response to socket.
    
    
    String js="<script language=\"text/javasctipt\">alert(\"done\");</script></body>";
    
    
    os.write(js.getBytes());
    
    os.flush();[/B]
    System.out.println("Data written"); 
    bw.close(); 
    in.close(); 
    os.close(); 
    
    } else { 
    
    //CONNECT METHOD 
    
    //inform client that server socket connection has been made 
    String response = "HTTP/1.0 200 Connection Established\n\r\n"; 
    OutputStream os = client_socket.getOutputStream(); 
    os.write(response.getBytes()); 
    os.flush(); 
    
    //launch two threads for HTTP Tunnelling 
    runFlag = true; 
    
    ProxyHTTPTunnel rt = new ProxyHTTPTunnel(this, client_socket, server_socket); 
    ThreadPoolManager.getInstance().addRequest(rt); 
    
    ProxyHTTPTunnel wt = new ProxyHTTPTunnel(this,server_socket, client_socket); 
    ThreadPoolManager.getInstance().addRequest(wt); 
    } 
    } catch (Exception ex) { 
    Log.log(ex); 
    } 
    } 
    }

    My issue is How can I append my response with the Incoming request from Socket and write it to another socket.......................
    :confused:
    Last edited by vaibhav_singh_vs@yahoo.co; 04-17-2009 at 07:09 AM.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Well firstly, you close and then try to re-use the OutputStream of client_socket: that's not going to work. Once you close the stream you can't use that socket any more.

    Secondly, if you're going to append to the returned data you need to deal with the fact that the Content-Length header is going to be wrong and that the html document will have already been terminated with </html>.

    The rest of your code is too difficult to read because you haven't used [code] tags.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    I am not closing the outputstream here just after all the data from Inputstream is writen to output stream I m writing my data to outputstream....

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You did before you edited it.
    My other points still stand - have you addressed them?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Replies: 3
    Last Post: 04-13-2010, 04:43 PM
  2. help about Socket
    By fahien_akim in forum New To Java
    Replies: 0
    Last Post: 04-16-2009, 11:41 AM
  3. Socket ?
    By barusk in forum Networking
    Replies: 0
    Last Post: 03-05-2009, 05:51 PM
  4. Replies: 4
    Last Post: 10-02-2008, 07:32 PM
  5. passively and quickly capturing request/response
    By sideswipe091976 in forum Networking
    Replies: 1
    Last Post: 07-10-2008, 02:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •