All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ratpack.server.internal.DefaultPublicAddress Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ratpack.server.internal;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.net.HostAndPort;
import ratpack.handling.Context;
import ratpack.http.Headers;
import ratpack.server.PublicAddress;
import ratpack.util.ExceptionUtils;
import ratpack.util.internal.ProtocolUtil;

import java.net.*;

import static ratpack.http.internal.HttpHeaderConstants.*;
import static ratpack.util.internal.ProtocolUtil.HTTPS_SCHEME;

/**
 * A best-effort attempt at providing meaningful default behaviors for determining the appropriate advertised address for a request.
 * This implementation supports a number of strategies, depending on what information is available (listed in order of precedence):
 *
 * 
    *
  • Configured public address URI (optional)
  • *
  • X-Forwarded-Host header (if included in request)
  • *
  • X-Forwarded-Proto or X-Forwarded-Ssl headers (if included in request)
  • *
  • Absolute request URI (if included in request)
  • *
  • Host header (if included in request)
  • *
  • Service's bind address and scheme (http vs. https)
  • *
* * @see Header Field Definitions: Host * @see Request: The Resource Identified by a Request */ public class DefaultPublicAddress implements PublicAddress { private static final Splitter FORWARDED_HOST_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings(); private final URI publicAddress; private final String scheme; public DefaultPublicAddress(URI publicAddress, String scheme) { this.publicAddress = publicAddress; this.scheme = scheme; } public URI getAddress(Context context) { URI currentUrl; if (this.publicAddress != null) { currentUrl = this.publicAddress; } else { String scheme; String host; int port; HostAndPort forwardedHostData = getForwardedHostData(context); if (forwardedHostData != null) { scheme = determineScheme(context, this.scheme); host = forwardedHostData.getHostText(); port = forwardedHostData.getPortOrDefault(-1); } else { URI absoluteRequestURI = getAbsoluteRequestUri(context); if (absoluteRequestURI != null) { scheme = determineScheme(context, absoluteRequestURI.getScheme()); host = absoluteRequestURI.getHost(); port = absoluteRequestURI.getPort(); } else { scheme = determineScheme(context, this.scheme); HostAndPort hostData = getHostData(context); if (hostData != null) { host = hostData.getHostText(); port = hostData.getPortOrDefault(-1); } else { HostAndPort localAddress = context.getRequest().getLocalAddress(); host = localAddress.getHostText(); port = ProtocolUtil.isDefaultPortForScheme(localAddress.getPort(), this.scheme) ? -1 : localAddress.getPort(); } } } try { currentUrl = new URI(scheme, null, host, port, null, null, null); } catch (URISyntaxException ex) { throw ExceptionUtils.uncheck(ex); } } return currentUrl; } private URI getAbsoluteRequestUri(Context context) { String rawUri = Strings.nullToEmpty(context.getRequest().getRawUri()); if (rawUri.isEmpty() || rawUri.startsWith("/")) { return null; } return URI.create(rawUri); } private HostAndPort getForwardedHostData(Context context) { Headers headers = context.getRequest().getHeaders(); String forwardedHostHeader = Strings.emptyToNull(headers.get(X_FORWARDED_HOST.toString())); String hostPortString = forwardedHostHeader != null ? Iterables.getFirst(FORWARDED_HOST_SPLITTER.split(forwardedHostHeader), null) : null; return hostPortString != null ? HostAndPort.fromString(hostPortString) : null; } private HostAndPort getHostData(Context context) { Headers headers = context.getRequest().getHeaders(); String hostPortString = Strings.emptyToNull(headers.get(HOST.toString())); return hostPortString != null ? HostAndPort.fromString(hostPortString) : null; } private String determineScheme(Context context, String defaultScheme) { Headers headers = context.getRequest().getHeaders(); String forwardedSsl = headers.get(X_FORWARDED_SSL.toString()); String forwardedProto = headers.get(X_FORWARDED_PROTO.toString()); if (ON.toString().equalsIgnoreCase(forwardedSsl)) { return HTTPS_SCHEME; } return forwardedProto != null ? forwardedProto : defaultScheme; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy