001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.web.embedded;
018
019import org.springframework.boot.autoconfigure.web.ServerProperties;
020import org.springframework.boot.cloud.CloudPlatform;
021import org.springframework.boot.context.properties.PropertyMapper;
022import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
023import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
024import org.springframework.boot.web.server.WebServerFactoryCustomizer;
025import org.springframework.core.Ordered;
026import org.springframework.core.env.Environment;
027import org.springframework.util.unit.DataSize;
028
029/**
030 * Customization for Netty-specific features.
031 *
032 * @author Brian Clozel
033 * @author Chentao Qu
034 * @since 2.1.0
035 */
036public class NettyWebServerFactoryCustomizer
037                implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory>, Ordered {
038
039        private final Environment environment;
040
041        private final ServerProperties serverProperties;
042
043        public NettyWebServerFactoryCustomizer(Environment environment,
044                        ServerProperties serverProperties) {
045                this.environment = environment;
046                this.serverProperties = serverProperties;
047        }
048
049        @Override
050        public int getOrder() {
051                return 0;
052        }
053
054        @Override
055        public void customize(NettyReactiveWebServerFactory factory) {
056                factory.setUseForwardHeaders(
057                                getOrDeduceUseForwardHeaders(this.serverProperties, this.environment));
058                PropertyMapper propertyMapper = PropertyMapper.get();
059                propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).whenNonNull()
060                                .asInt(DataSize::toBytes)
061                                .to((maxHttpRequestHeaderSize) -> customizeMaxHttpHeaderSize(factory,
062                                                maxHttpRequestHeaderSize));
063        }
064
065        private boolean getOrDeduceUseForwardHeaders(ServerProperties serverProperties,
066                        Environment environment) {
067                if (serverProperties.isUseForwardHeaders() != null) {
068                        return serverProperties.isUseForwardHeaders();
069                }
070                CloudPlatform platform = CloudPlatform.getActive(environment);
071                return platform != null && platform.isUsingForwardHeaders();
072        }
073
074        private void customizeMaxHttpHeaderSize(NettyReactiveWebServerFactory factory,
075                        Integer maxHttpHeaderSize) {
076                factory.addServerCustomizers((NettyServerCustomizer) (httpServer) -> httpServer
077                                .httpRequestDecoder((httpRequestDecoderSpec) -> httpRequestDecoderSpec
078                                                .maxHeaderSize(maxHttpHeaderSize)));
079        }
080
081}