{
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
HttpExchange exchange = new HttpExchange()
{
protected void onRequestCommitted() throws IOException
{
}
protected void onRequestComplete() throws IOException
{
}
protected void onResponseComplete() throws IOException
{
continuation.resume();
}
protected void onResponseContent(Buffer content) throws IOException
{
// TODO Avoid this copy
while (content.hasContent())
{
int len=content.get(buffer,0,buffer.length);
out.write(buffer,0,len); // May block here for a little bit!
}
}
protected void onResponseHeaderComplete() throws IOException
{
}
protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
{
if (reason!=null && reason.length()>0)
response.setStatus(status,reason.toString());
else
response.setStatus(status);
}
protected void onResponseHeader(Buffer name, Buffer value) throws IOException
{
String s = name.toString().toLowerCase();
if (!_DontProxyHeaders.contains(s))
response.addHeader(name.toString(),value.toString());
}
};
exchange.setVersion(request.getProtocol());
exchange.setMethod(request.getMethod());
exchange.setURL(url.toString());
// check connection header
String connectionHdr = request.getHeader("Connection");
if (connectionHdr!=null)
{
connectionHdr=connectionHdr.toLowerCase();
if (connectionHdr.indexOf("keep-alive")<0 &&
connectionHdr.indexOf("close")<0)
connectionHdr=null;
}
// 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();
if (_DontProxyHeaders.contains(lhdr))
continue;
if (connectionHdr!=null && connectionHdr.indexOf(lhdr)>=0)
continue;
if ("content-type".equals(lhdr))
hasContent=true;
if ("content-length".equals(lhdr))
contentLength=request.getContentLength();
Enumeration vals = request.getHeaders(hdr);
while (vals.hasMoreElements())
{
String val = (String)vals.nextElement();
if (val!=null)
{
exchange.setRequestHeader(lhdr,val);
xForwardedFor|="X-Forwarded-For".equalsIgnoreCase(hdr);
}
}
}
// Proxy headers
exchange.setRequestHeader("Via","1.1 (jetty)");
if (!xForwardedFor)
exchange.addRequestHeader("X-Forwarded-For",
request.getRemoteAddr());
if (hasContent)
exchange.setRequestContentSource(in);
_client.send(exchange);
continuation.suspend(30000);
}