View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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  package org.eclipse.jetty.server;
20  
21  import java.util.Objects;
22  
23  /**
24   * Customizes requests that lack the {@code Host} header (for example, HTTP 1.0 requests).
25   * <p />
26   * In case of HTTP 1.0 requests that lack the {@code Host} header, the application may issue
27   * a redirect, and the {@code Location} header is usually constructed from the {@code Host}
28   * header; if the {@code Host} header is missing, the server may query the connector for its
29   * IP address in order to construct the {@code Location} header, and thus leak to clients
30   * internal IP addresses.
31   * <p />
32   * This {@link HttpConfiguration.Customizer} is configured with a {@code serverName} and
33   * optionally a {@code serverPort}.
34   * If the {@code Host} header is absent, the configured {@code serverName} will be set on
35   * the request so that {@link HttpServletRequest#getServerName()} will return that value,
36   * and likewise for {@code serverPort} and {@link HttpServletRequest#getServerPort()}.
37   */
38  public class HostHeaderCustomizer implements HttpConfiguration.Customizer
39  {
40      private final String serverName;
41      private final int serverPort;
42  
43      /**
44       * @param serverName the {@code serverName} to set on the request (the {@code serverPort} will not be set)
45       */
46      public HostHeaderCustomizer(String serverName)
47      {
48          this(serverName, 0);
49      }
50  
51      /**
52       * @param serverName the {@code serverName} to set on the request
53       * @param serverPort the {@code serverPort} to set on the request
54       */
55      public HostHeaderCustomizer(String serverName, int serverPort)
56      {
57          this.serverName = Objects.requireNonNull(serverName);
58          this.serverPort = serverPort;
59      }
60  
61      @Override
62      public void customize(Connector connector, HttpConfiguration channelConfig, Request request)
63      {
64          if (request.getHeader("Host") == null)
65          {
66              request.setServerName(serverName);
67              if (serverPort > 0)
68                  request.setServerPort(serverPort);
69          }
70      }
71  }