You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by ed banfa <e_...@yahoo.com> on 2004/09/27 13:30:35 UTC
Http proxy servlet(Problems proxying POST request)
Greetings to you all,
I am trying to code up a HTTP proxy servlet on tomcat 4.1.27. on RedHat 9.0 with J2SDK1.4.2
I have been successful with ordinary HTTP GET request, the servlet has performed well.
My problem is with HTTP POST request, my request hang (stops with nothing happenning) on the servlet and I have traced it to the part of my code that I try to access/set the response code that will be sent back to the client, I have include the method in my servlet that handles the actual proxying, this method :
private void handle( HttpServletRequest request,HttpServletResponse response )throws IOException
is called from both doGet and dePost methods of my servlet.
The code in the handle method is a direct copy(with credits) of the handle method in class
org.mortbay.http.handler.ProxyHandler that handles proxying for the Jetty webserver. I adapted/adapting the method to work with HttpServletResponse and HttpServletRequest objects.
Please any advice will be sincerely appreciated, I am really pressed on this one.
Thank you.
PS I am thinking of writing my own code that uses direct Sockets if the HttpUrlConnection stuff does not work, as u can see from the source below I have a lot of logging/debugging code coz I am trying to find out what the hell is happening in there.
I believe I may be doing somthing wrong, I am no HTTP guru but I am learning.
below is the altered code:
/**
* @param request
* @param response
*/
private void handle( HttpServletRequest request,HttpServletResponse response )throws IOException
{
URLConnection connection;
String resourceURL = getRequestedResourceURL(request);
logger.info("Processing resource URL for client: "+ request.getRemoteAddr() + ", URL:" + resourceURL );
try
{
URL url = new URL(resourceURL);
if(!isProxied(url)){
return;
}
logger.info("Proxying resource URL for client: "+ request.getRemoteAddr());
connection = url.openConnection();
connection.setAllowUserInteraction(false);
// Set method
HttpURLConnection http=null;
if(connection instanceof HttpURLConnection)
{
http=(HttpURLConnection)connection;
http.setRequestMethod(request.getMethod());
http.setInstanceFollowRedirects(false);
}
//check connection header
String connectionHdr=request.getHeader(__Connection);
if(connectionHdr!=null
&&(connectionHdr.equalsIgnoreCase(__KeepAlive)||connectionHdr
.equalsIgnoreCase("close")))
connectionHdr=null;
//copy headers
boolean xForwardedFor=false;
boolean hasContent=false;
Enumeration enum=request.getHeaderNames();
while(enum.hasMoreElements())
{
// XXX could be better than this!
String hdr=(String)enum.nextElement();
if(_DontProxyHeaders.containsKey(hdr))
continue;
if(connectionHdr!=null&&connectionHdr.indexOf(hdr)>=0)
continue;
if(__ContentType.equals(hdr))
hasContent=true;
Enumeration vals=request.getHeaders(hdr);
while(vals.hasMoreElements())
{
String val=(String)vals.nextElement();
if(val!=null)
{
logger.info("Copying HTTP header:" +hdr+" value: "+val);
connection.addRequestProperty(hdr,val);
xForwardedFor |= __XForwardedFor.equalsIgnoreCase(hdr);
}
}
}
logger.debug("Setting proxy header....");
//Proxy headers
connection.setRequestProperty("Via","1.1 (CarterraHttpProxyServlet)");
if(!xForwardedFor)
connection.addRequestProperty(__XForwardedFor,request.getRemoteAddr());
//a little bit of cache control
String cache_control=request.getHeader(__CacheControl);
if(cache_control!=null&&(cache_control.indexOf("no-cache")>=0||cache_control.indexOf("no-store")>=0))
connection.setUseCaches(false);
// customize Connection
customizeConnection(request,connection);
//To be continued
try{
connection.setDoInput(true);
// do input thang!
logger.debug("Doing da input thang");
InputStream in=request.getInputStream();
if(hasContent)
{
connection.setDoOutput(true);
copy(in,connection.getOutputStream(),-1);
}
// Connect
connection.connect();
logger.debug("Done da input thang");
}
catch(Exception e)
{
logger.error(e.getMessage());
}
InputStream proxy_in=null;
// handler status codes etc.
int code=__500_Internal_Server_Error;
logger.debug("accessing return stream");
if(http!=null)
{
logger.debug("Accessing HTTP error stream");
proxy_in=http.getErrorStream();
logger.debug("Accessing server response code");
code=http.getResponseCode();
logger.debug("Set response status code");
response.setStatus(code);
}
logger.debug("Starting proxy stream ops");
if(proxy_in==null)
{
try
{
logger.debug("Getting proxy input stream");
proxy_in=connection.getInputStream();
}
catch(Exception e)
{
logger.error(e.getMessage());
logger.debug("Some error occured:"+e.getClass().getName());
proxy_in=http.getErrorStream();
}
}
//clear response defaults
logger.debug("Clearing response default headers");
response.setHeader("Date",null);
response.setHeader("Server",null);
logger.debug("Cleared response default headers");
// set response headers
logger.debug("Setting response headers");
int h=0;
String hdr=connection.getHeaderFieldKey(h);
String val=connection.getHeaderField(h);
while(hdr!=null||val!=null)
{
if(hdr!=null&&val!=null&&!_DontProxyHeaders.containsKey(hdr))
response.addHeader(hdr,val);
logger.info("Building HTTP response header: " +hdr+" value:"+val);
h++;
hdr=connection.getHeaderFieldKey(h);
val=connection.getHeaderField(h);
}
response.setHeader("Via","1.1 (CarterraHttpProxyServlet)");
if(proxy_in!=null)
logger.debug("Returning input stream back to client");
copy(proxy_in,response.getOutputStream(),-1);
} catch (Exception e){
logger.warn(e.getMessage());
if(!response.isCommitted())
response.sendError(__400_Bad_Request);
}
}
EDWARD BANFA
NETPLAY TECHNOLOGIES INC.
JOS, PLATEAU STATE
NIGERIA.
---------------------------------
Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile phone.