Added capability of customizing request and response headers by overriding a method, and added Via header also on responses.

This commit is contained in:
Simone Bordet 2012-07-09 15:57:49 +02:00
parent e3b6344641
commit 9253bb791e
2 changed files with 26 additions and 17 deletions

View File

@ -25,6 +25,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.eclipse.jetty.spdy.api.Headers;
import org.eclipse.jetty.spdy.api.Stream;
import org.eclipse.jetty.spdy.api.StreamFrameListener;
import org.eclipse.jetty.spdy.api.server.ServerSessionFrameListener;
import org.eclipse.jetty.util.log.Log;
@ -71,19 +72,27 @@ public abstract class ProxyEngine extends ServerSessionFrameListener.Adapter imp
return name;
}
protected void addRequestProxyHeaders(Headers headers)
protected void addRequestProxyHeaders(Stream stream, Headers headers)
{
String newValue = "";
Headers.Header header = headers.get("via");
if (header != null)
newValue = header.valuesAsString() + ", ";
newValue += "http/1.1 " + getName();
headers.put("via", newValue);
addViaHeader(headers);
}
protected void addResponseProxyHeaders(Headers headers)
protected void addResponseProxyHeaders(Stream stream, Headers headers)
{
addViaHeader(headers);
}
private void addViaHeader(Headers headers)
{
headers.add("Via", "http/1.1 " + getName());
}
protected void customizeRequestHeaders(Stream stream, Headers headers)
{
}
protected void customizeResponseHeaders(Stream stream, Headers headers)
{
// TODO: add Via header
}
public Map<String, ProxyInfo> getProxyInfos()

View File

@ -130,8 +130,6 @@ public class SPDYProxyEngine extends ProxyEngine
return null;
}
// TODO: give a chance to modify headers and rewrite URI
short serverVersion = proxyInfo.getVersion();
InetSocketAddress address = proxyInfo.getAddress();
Session serverSession = produceSession(host, serverVersion, address);
@ -145,10 +143,10 @@ public class SPDYProxyEngine extends ProxyEngine
Set<Session> sessions = (Set<Session>)serverSession.getAttribute(CLIENT_SESSIONS_ATTRIBUTE);
sessions.add(clientSession);
addRequestProxyHeaders(clientStream, headers);
customizeRequestHeaders(clientStream, headers);
convert(clientVersion, serverVersion, headers);
addRequestProxyHeaders(headers);
SynInfo serverSynInfo = new SynInfo(headers, clientSynInfo.isClose());
StreamFrameListener listener = new ProxyStreamFrameListener(clientStream);
StreamHandler handler = new StreamHandler(clientStream, serverSynInfo);
@ -256,11 +254,12 @@ public class SPDYProxyEngine extends ProxyEngine
short serverVersion = stream.getSession().getVersion();
Headers headers = new Headers(replyInfo.getHeaders(), false);
addResponseProxyHeaders(stream, headers);
customizeResponseHeaders(stream, headers);
short clientVersion = this.clientStream.getSession().getVersion();
convert(serverVersion, clientVersion, headers);
addResponseProxyHeaders(headers);
this.replyInfo = new ReplyInfo(headers, replyInfo.isClose());
if (replyInfo.isClose())
reply(stream);
@ -484,14 +483,15 @@ public class SPDYProxyEngine extends ProxyEngine
Headers headers = new Headers(serverSynInfo.getHeaders(), false);
addResponseProxyHeaders(serverStream, headers);
customizeResponseHeaders(serverStream, headers);
Stream clientStream = (Stream)serverStream.getAssociatedStream().getAttribute(CLIENT_STREAM_ATTRIBUTE);
convert(serverStream.getSession().getVersion(), clientStream.getSession().getVersion(), headers);
addResponseProxyHeaders(headers);
StreamHandler handler = new StreamHandler(clientStream, serverSynInfo);
serverStream.setAttribute(STREAM_HANDLER_ATTRIBUTE, handler);
clientStream.syn(new SynInfo(headers, serverSynInfo.isClose()), getTimeout(), TimeUnit.MILLISECONDS, handler);
return this;
}