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          // Header names are case-insensitive (RFC2616) and oej.util.Fields.add converts the names to lowercase. So we
91          // need to compare with the lowercase values only
92          for (String hopHeader : HOP_HEADERS)
93              headers.remove(hopHeader);
94      }
95  
96      protected void addRequestProxyHeaders(Stream stream, Fields headers)
97      {
98          addViaHeader(headers);
99          Fields.Field schemeField = headers.get(HTTPSPDYHeader.SCHEME.name(stream.getSession().getVersion()));
100         if(schemeField != null)
101             headers.add("X-Forwarded-Proto", schemeField.value());
102         InetSocketAddress address = stream.getSession().getRemoteAddress();
103         if (address != null)
104         {
105             headers.add("X-Forwarded-Host", address.getHostName());
106             headers.add("X-Forwarded-For", address.toString());
107         }
108         headers.add("X-Forwarded-Server", name());
109     }
110 
111     protected void addResponseProxyHeaders(Stream stream, Fields headers)
112     {
113         addViaHeader(headers);
114     }
115 
116     private void addViaHeader(Fields headers)
117     {
118         headers.add("Via", "http/1.1 " + getName());
119     }
120 
121     protected void customizeRequestHeaders(Stream stream, Fields headers)
122     {
123     }
124 
125     protected void customizeResponseHeaders(Stream stream, Fields headers)
126     {
127     }
128 
129 }