View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  
20  package org.eclipse.jetty.spdy.server.proxy;
21  
22  import java.net.InetAddress;
23  import java.net.InetSocketAddress;
24  import java.net.UnknownHostException;
25  
26  import org.eclipse.jetty.spdy.api.Stream;
27  import org.eclipse.jetty.spdy.api.StreamFrameListener;
28  import org.eclipse.jetty.spdy.api.SynInfo;
29  import org.eclipse.jetty.util.Fields;
30  import org.eclipse.jetty.util.log.Log;
31  import org.eclipse.jetty.util.log.Logger;
32  
33  /**
34   * <p>{@link ProxyEngine} is the class for SPDY proxy functionalities that receives a SPDY request and converts it to
35   * any protocol to its server side.</p>
36   * <p>This class listens for SPDY events sent by clients; subclasses are responsible for translating
37   * these SPDY client events into appropriate events to forward to the server, in the appropriate
38   * protocol that is understood by the server.</p>
39   */
40  public abstract class ProxyEngine
41  {
42      protected final Logger logger = Log.getLogger(getClass());
43      private final String name;
44  
45      protected ProxyEngine()
46      {
47          this(name());
48      }
49  
50      private static String name()
51      {
52          try
53          {
54              return InetAddress.getLocalHost().getHostName();
55          }
56          catch (UnknownHostException x)
57          {
58              return "localhost";
59          }
60      }
61  
62      public abstract StreamFrameListener proxy(Stream clientStream, SynInfo clientSynInfo, ProxyEngineSelector.ProxyServerInfo proxyServerInfo);
63  
64      protected ProxyEngine(String name)
65      {
66          this.name = name;
67      }
68  
69      public String getName()
70      {
71          return name;
72      }
73  
74      protected void addRequestProxyHeaders(Stream stream, Fields headers)
75      {
76          addViaHeader(headers);
77          InetSocketAddress address = (InetSocketAddress)stream.getSession().getAttribute("org.eclipse.jetty.spdy.remoteAddress");
78          if (address != null)
79              headers.add("X-Forwarded-For", address.getHostName());
80      }
81  
82      protected void addResponseProxyHeaders(Stream stream, Fields headers)
83      {
84          addViaHeader(headers);
85      }
86  
87      private void addViaHeader(Fields headers)
88      {
89          headers.add("Via", "http/1.1 " + getName());
90      }
91  
92      protected void customizeRequestHeaders(Stream stream, Fields headers)
93      {
94      }
95  
96      protected void customizeResponseHeaders(Stream stream, Fields headers)
97      {
98      }
99  
100 }