View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  import java.util.HashSet;
26  import java.util.Set;
27  
28  import org.eclipse.jetty.spdy.api.Stream;
29  import org.eclipse.jetty.spdy.api.StreamFrameListener;
30  import org.eclipse.jetty.spdy.api.SynInfo;
31  import org.eclipse.jetty.spdy.server.http.HTTPSPDYHeader;
32  import org.eclipse.jetty.util.Fields;
33  
34  /**
35   * <p>{@link ProxyEngine} is the class for SPDY proxy functionalities that receives a SPDY request and converts it to
36   * any protocol to its server side.</p>
37   * <p>This class listens for SPDY events sent by clients; subclasses are responsible for translating
38   * these SPDY client events into appropriate events to forward to the server, in the appropriate
39   * protocol that is understood by the server.</p>
40   */
41  public abstract class ProxyEngine
42  {
43      private static final Set<String> HOP_HEADERS = new HashSet<>();
44      static
45      {
46          HOP_HEADERS.add("proxy-connection");
47          HOP_HEADERS.add("connection");
48          HOP_HEADERS.add("keep-alive");
49          HOP_HEADERS.add("transfer-encoding");
50          HOP_HEADERS.add("te");
51          HOP_HEADERS.add("trailer");
52          HOP_HEADERS.add("proxy-authorization");
53          HOP_HEADERS.add("proxy-authenticate");
54          HOP_HEADERS.add("upgrade");
55      }
56  
57      private final String name;
58  
59      protected ProxyEngine()
60      {
61          this(name());
62      }
63  
64      private static String name()
65      {
66          try
67          {
68              return InetAddress.getLocalHost().getHostName();
69          }
70          catch (UnknownHostException x)
71          {
72              return "localhost";
73          }
74      }
75  
76      public abstract StreamFrameListener proxy(Stream clientStream, SynInfo clientSynInfo, ProxyEngineSelector.ProxyServerInfo proxyServerInfo);
77  
78      protected ProxyEngine(String name)
79      {
80          this.name = name;
81      }
82  
83      public String getName()
84      {
85          return name;
86      }
87  
88      protected void removeHopHeaders(Fields headers)
89      {
90          for (String hopHeader : HOP_HEADERS)
91              headers.remove(hopHeader);
92      }
93  
94      protected void addRequestProxyHeaders(Stream stream, Fields headers)
95      {
96          addViaHeader(headers);
97          Fields.Field schemeField = headers.get(HTTPSPDYHeader.SCHEME.name(stream.getSession().getVersion()));
98          if(schemeField != null)
99              headers.add("X-Forwarded-Proto", schemeField.value());
100         InetSocketAddress address = stream.getSession().getRemoteAddress();
101         if (address != null)
102         {
103             headers.add("X-Forwarded-Host", address.getHostName());
104             headers.add("X-Forwarded-For", address.toString());
105         }
106         headers.add("X-Forwarded-Server", name());
107     }
108 
109     protected void addResponseProxyHeaders(Stream stream, Fields headers)
110     {
111         addViaHeader(headers);
112     }
113 
114     private void addViaHeader(Fields headers)
115     {
116         headers.add("Via", "http/1.1 " + getName());
117     }
118 
119     protected void customizeRequestHeaders(Stream stream, Fields headers)
120     {
121     }
122 
123     protected void customizeResponseHeaders(Stream stream, Fields headers)
124     {
125     }
126 
127 }