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