Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Couldn't parser response content in ProxyServlet

Dear all,
I've got a little progress about the parsing content problem.
Finally I find out that if I don't copy the "User-Agent" request header from the original request to the contentexchange, I could be able to parse the response content from the back end server correctly. Codes are as follows:
// copy headers
    boolean xForwardedFor = false;
    boolean hasContent = false;
    long contentLength = -1;
    Enumeration<?> enm = request.getHeaderNames();
    while (enm.hasMoreElements()) {
     // TODO could be better than this!
     String hdr = (String) enm.nextElement();
     String lhdr = hdr.toLowerCase();
     String value=request.getHeader(hdr);
     System.out.println("HeaderName:["+hdr+"] Value:["+value+"]");
     
     if (_DontProxyHeaders.contains(lhdr))
      continue;
     if (connectionHdr != null
       && connectionHdr.indexOf(lhdr) >= 0)
      continue;
     if (_hostHeader != null && "host".equals(lhdr))
      continue;
     if ("user-agent".equals(lhdr))
     continue;
     if ("content-type".equals(lhdr))
      hasContent = true;
     else if ("content-length".equals(lhdr)) {
      contentLength = request.getContentLength();
      exchange.setRequestHeader(HttpHeaders.CONTENT_LENGTH,Long.toString(contentLength));
      if (contentLength > 0)
       hasContent = true;
      //add here
      continue;
     } else if ("x-forwarded-for".equals(lhdr))
      xForwardedFor = true;
     
     Enumeration<?> vals = request.getHeaders(hdr);
     while (vals.hasMoreElements()) {
      String val = (String) vals.nextElement();
      if (val != null) {
       if (debug != 0)
        _log.debug(debug + " " + hdr + ": " + val);
       exchange.setRequestHeader(hdr, val);
      }
     }
    }
 
But if I don't copy "User-Agent" request header to exchange, I got new problems. The browser couldn't receive the response content.
Do anyone knows how to solve this problem?
Thank you very much.
 
Regards!
Santa
 
2011/3/22 黄志兰 <santahzl@xxxxxxxxx>
Dear all,
Sorry for bothering. I'm new in jetty. Now I'm working on a task, I have to change jetty ProxyServlet a little bit. So as to make it availbale for our whole project.
What I have to do is to change the content that Jetty Proxy get from the backend server. And forward it back to the client.
I got problem when doing this. It seem that the byte array that I get from ContentExchange couldn't transfer to String correctly.
I've try the following codes:
ContentExchange exchange = new ContentExchange() {
     protected void onResponseComplete() throws IOException {
      super.onResponseComplete();
      //do something with the response content
      String responseContent = this.getResponseContent();
      System.out.println(responseContent); // this prints messy code in console.
       byte[] responseContentBytes = this.getResponseContentBytes();   
       System.out.println(new String (responseContentBytes,"UTF-8")); // Still prints out messy code, the html's charset is UTF-8
     }
 
 
I test the following code in my developing environment, it works well:
public static void main1(String[] args) throws Exception {  
        HttpClient httpClient = new HttpClient();  
        // set up httpClient  
        httpClient.start();  
        ContentExchange contentExchange = new ContentExchange();  
        httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);  
        contentExchange.setURL("http://www.google.com.hk");  
        httpClient.send(contentExchange);  
        contentExchange.waitForDone();  
        System.err.println("Response status: " 
                + contentExchange.getResponseStatus());  
        byte[] responseContentBytes = contentExchange.getResponseContentBytes();  
        System.out.println(new String (responseContentBytes,"UTF-8"));  
    }
 
So, my question is: what cause the messy code problem? How can I resolve the response content from ContentExchange's getResponseContentBytes() or getResponseContent() correctly?
 
Thanks very much!
Regards!
Santa


Back to the top