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.proxy;
21  
22  import java.net.InetAddress;
23  import java.net.UnknownHostException;
24  
25  import org.eclipse.jetty.spdy.api.Headers;
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.log.Log;
30  import org.eclipse.jetty.util.log.Logger;
31  
32  /**
33   * <p>{@link ProxyEngine} is the class for SPDY proxy functionalities that receives a SPDY request and converts it to
34   * any protocol to its server side.</p>
35   * <p>This class listens for SPDY events sent by clients; subclasses are responsible for translating
36   * these SPDY client events into appropriate events to forward to the server, in the appropriate
37   * protocol that is understood by the server.</p>
38   */
39  public abstract class ProxyEngine
40  {
41      protected final Logger logger = Log.getLogger(getClass());
42      private final String name;
43  
44      protected ProxyEngine()
45      {
46          this(name());
47      }
48  
49      private static String name()
50      {
51          try
52          {
53              return InetAddress.getLocalHost().getHostName();
54          }
55          catch (UnknownHostException x)
56          {
57              return "localhost";
58          }
59      }
60  
61      public abstract StreamFrameListener proxy(Stream clientStream, SynInfo clientSynInfo, ProxyEngineSelector.ProxyServerInfo proxyServerInfo);
62  
63      protected ProxyEngine(String name)
64      {
65          this.name = name;
66      }
67  
68      public String getName()
69      {
70          return name;
71      }
72  
73      protected void addRequestProxyHeaders(Stream stream, Headers headers)
74      {
75          addViaHeader(headers);
76          String address = (String)stream.getSession().getAttribute("org.eclipse.jetty.spdy.remoteAddress");
77          if (address != null)
78              headers.add("X-Forwarded-For", address);
79      }
80  
81      protected void addResponseProxyHeaders(Stream stream, Headers headers)
82      {
83          addViaHeader(headers);
84      }
85  
86      private void addViaHeader(Headers headers)
87      {
88          headers.add("Via", "http/1.1 " + getName());
89      }
90  
91      protected void customizeRequestHeaders(Stream stream, Headers headers)
92      {
93      }
94  
95      protected void customizeResponseHeaders(Stream stream, Headers headers)
96      {
97      }
98  
99  }