How to send response headers from proxy to browser?



This question's got no replies on forum.java.sun.com
through its full four days and three nights life.
The proxy server intercepts every request from the
browser and redirect it to www.yahoo.com. It simply
discards original request. After connecting to yahoo.com,
it sends the received response to the browser,
displaying the first part of the yahoo page. However,
as described in the comment on the code below, it
seems the proxy can't send the response headers to
the browser, it only sends document content. What
should we do to send the response headers from proxy?

This program has no practical usefullness. It's only
an exercise for implementing proxy.

/* SimpleProxy.java
* redirect every request from browser to www.yahoo.com
*/
import java.net.*;
import java.io.*;
import java.util.*;

class SimpleProxy{
ServerSocket pxSrvSocket;
Socket clientSocket;
URL remoteUrl;
URLConnection remoteConnection;
BufferedReader readClient, readRemote;
PrintWriter writeClient;
String clientReq, remoteRes, resphName, resphValue, resphStr, resMsg;
Map<String,List<String>> respHdrs; // response headers
List<String> values; // header values
int len;

public SimpleProxy(){
try{
pxSrvSocket = new ServerSocket(9999);

clientSocket = pxSrvSocket.accept();
System.out.println("browser connected to this proxy");
readClient = new BufferedReader
(new InputStreamReader(clientSocket.getInputStream()));
writeClient
= new PrintWriter(clientSocket.getOutputStream(), true);

// setup connection and request headers
// (from Firefox 1.5.0.1 on Linux)
remoteUrl = new URL("http://www.yahoo.com/";);
remoteConnection = remoteUrl.openConnection();
remoteConnection.setAllowUserInteraction(true);
remoteConnection.setDoInput(true);
remoteConnection.setDoOutput(true);
//remoteConnection.setIfModifiedSince(0L);
//remoteConnection.setUseCaches(true);

((HttpURLConnection)remoteConnection).setRequestMethod("GET");

remoteConnection.setRequestProperty("Host", "www.yahoo.com");
remoteConnection.setRequestProperty("User-Agent",
"User-Agent: Mozilla/5.0 (X11; U; Linux i686; ja; rv:1.8.0.1)
Gecko/20060124 Firefox/1.5.0.1");
remoteConnection.setRequestProperty("Accept",
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
remoteConnection.setRequestProperty("Accept-Language",
"ja,en-us;q=0.7,en;q=0.3");
remoteConnection.setRequestProperty("Accept-Encoding",
"gzip,deflate");
remoteConnection.setRequestProperty("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
remoteConnection.setRequestProperty("Keep-Alive", "300");
remoteConnection.setRequestProperty("Proxy-Connection",
"keep-alive");

remoteConnection.connect();
System.out.println
("remote connection esatblished : " +
remoteConnection.toString());
readRemote = new BufferedReader
(new InputStreamReader(remoteConnection.getInputStream()));

// dump original request from browser
while ((clientReq = readClient.readLine()).length() > 0){
System.out.println("read from browser : " + clientReq);
}

/*** if we uncoment this part, program doesn't work, only blank screen
***

// get response headers
respHdrs = remoteConnection.getHeaderFields();
Set<String> names = respHdrs.keySet();

// first, write response status line
resphStr = "";
values = respHdrs.get(null);
for (String val : values){
resphStr += val + ","; // e.g. HTTP/1.1 200 OK
}
len = resphStr.length();
if (resphStr.charAt(len - 1) == ','){ // trim last ','
resphStr = resphStr.substring(0, len - 1);
}
System.out.println("resp hdr from remote: " + resphStr);
writeClient.println(resphStr);
// names.remove(null); // UnsupportedOperationException

// next, other response headers
for (String name : names){
if (name != null){
values = respHdrs.get(name);
resphStr = name + ": ";
for (String val : values){
resphStr += val + ",";
}
len = resphStr.length();
if (resphStr.charAt(len - 1) == ','){ // trim last ','
resphStr = resphStr.substring(0, len - 1);
}
System.out.println("resp hdr from remote: " + resphStr);
writeClient.println(resphStr);
}
}
System.out.println();
writeClient.println(); // a blank line

*** end response headers part ***/

// get and write document contents -- this part works normally
// if we comment-out the above part
while((remoteRes = readRemote.readLine()).length() > 0){
writeClient.println(remoteRes);
System.out.println("response from remote : " + remoteRes);
}

writeClient.close();
clientSocket.close();
pxSrvSocket.close();
}catch (IOException e){
e.printStackTrace();
}
}

public static void main(String a[]){
new SimpleProxy();
}
}

.



Relevant Pages

  • Re: writing a proxy ..
    ... 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 talking to a Proxy which relays the request to a Server. ...
    (comp.lang.java.programmer)
  • Re: How to send response headers from proxy to browser?
    ... browser and redirect it to www.yahoo.com. ... should we do to send the response headers from proxy? ... I suspect that the problem is that you are not correlating what the client sees ... When you send your request to Yahoo, ...
    (comp.lang.java.programmer)
  • Re: ecommerce / ssl over 3g ?
    ... The only way for a proxy to intercept a request in this way is for it to pass the request to the server, decrypt it and then re-encrypt in its own SSL channel - at which point the encrypting certificate would not match the site certificate and the browser would complain bitterly. ... The only way 3 could make this work is by having their own key set as a trusted signing authority in every single client which would be accessing the internet through their proxy, including every browser on a computer that could be connected through a phone, and then generate ...
    (uk.telecom.mobile)
  • Re: Re: HTTP tunneling to bypass proxy filter
    ... I guess you mean to say, every HTTP request that is sent to the corporate proxy should have a fingerprint of the browser and it should be matched everytime the proxy forwards the request. ...
    (Security-Basics)
  • Re: [PHP] Re: Header() - POST
    ... The headerfunction is modifying the response headers that are ... being sent back to the browser, they do not create a new request. ... If you want to do a new request I suggest looking at curl. ... server there probably isn't any need for a second request at all. ...
    (php.general)