Skip to content

Commit

Permalink
Avoid one layer of HttpHeaders wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed May 11, 2020
1 parent a64e709 commit 3276f81
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -99,11 +99,7 @@ public HttpEntity(MultiValueMap<String, String> headers) {
*/
public HttpEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers) {
this.body = body;
HttpHeaders tempHeaders = new HttpHeaders();
if (headers != null) {
tempHeaders.putAll(headers);
}
this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
this.headers = HttpHeaders.readOnlyHttpHeaders(headers != null ? headers : new HttpHeaders());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -76,7 +76,7 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
* @param contextPath the context path for the request
* @param headers the headers for the request
*/
public AbstractServerHttpRequest(URI uri, @Nullable String contextPath, HttpHeaders headers) {
public AbstractServerHttpRequest(URI uri, @Nullable String contextPath, MultiValueMap<String, String> headers) {
this.uri = uri;
this.path = RequestPath.parse(uri, contextPath);
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
Expand Down Expand Up @@ -36,6 +36,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;

/**
* {@link ServletHttpHandlerAdapter} extension that uses Jetty APIs for writing
Expand Down Expand Up @@ -79,9 +80,9 @@ private static final class JettyServerHttpRequest extends ServletServerHttpReque
super(createHeaders(request), request, asyncContext, servletPath, bufferFactory, bufferSize);
}

private static HttpHeaders createHeaders(HttpServletRequest request) {
private static MultiValueMap<String, String> createHeaders(HttpServletRequest request) {
HttpFields fields = ((Request) request).getMetaData().getFields();
return new HttpHeaders(new JettyHeadersAdapter(fields));
return new JettyHeadersAdapter(fields);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
Expand All @@ -59,7 +58,7 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
public ReactorServerHttpRequest(HttpServerRequest request, NettyDataBufferFactory bufferFactory)
throws URISyntaxException {

super(initUri(request), "", initHeaders(request));
super(initUri(request), "", new NettyHeadersAdapter(request.requestHeaders()));
Assert.notNull(bufferFactory, "DataBufferFactory must not be null");
this.request = request;
this.bufferFactory = bufferFactory;
Expand Down Expand Up @@ -127,11 +126,6 @@ private static String resolveRequestUri(HttpServerRequest request) {
return uri;
}

private static HttpHeaders initHeaders(HttpServerRequest channel) {
NettyHeadersAdapter headersMap = new NettyHeadersAdapter(channel.requestHeaders());
return new HttpHeaders(headersMap);
}


@Override
public String getMethodValue() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
Expand All @@ -23,6 +23,7 @@
import java.nio.charset.Charset;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

import javax.servlet.AsyncContext;
Expand All @@ -44,6 +45,7 @@
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand Down Expand Up @@ -77,8 +79,8 @@ public ServletServerHttpRequest(HttpServletRequest request, AsyncContext asyncCo
this(createDefaultHttpHeaders(request), request, asyncContext, servletPath, bufferFactory, bufferSize);
}

public ServletServerHttpRequest(HttpHeaders headers, HttpServletRequest request, AsyncContext asyncContext,
String servletPath, DataBufferFactory bufferFactory, int bufferSize)
public ServletServerHttpRequest(MultiValueMap<String, String> headers, HttpServletRequest request,
AsyncContext asyncContext, String servletPath, DataBufferFactory bufferFactory, int bufferSize)
throws IOException, URISyntaxException {

super(initUri(request), request.getContextPath() + servletPath, initHeaders(headers, request));
Expand All @@ -99,8 +101,9 @@ public ServletServerHttpRequest(HttpHeaders headers, HttpServletRequest request,
}


private static HttpHeaders createDefaultHttpHeaders(HttpServletRequest request) {
HttpHeaders headers = new HttpHeaders();
private static MultiValueMap<String, String> createDefaultHttpHeaders(HttpServletRequest request) {
MultiValueMap<String, String> headers =
CollectionUtils.toMultiValueMap(new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH));
for (Enumeration<?> names = request.getHeaderNames(); names.hasMoreElements(); ) {
String name = (String) names.nextElement();
for (Enumeration<?> values = request.getHeaders(name); values.hasMoreElements(); ) {
Expand All @@ -120,34 +123,36 @@ private static URI initUri(HttpServletRequest request) throws URISyntaxException
return new URI(url.toString());
}

private static HttpHeaders initHeaders(HttpHeaders headers, HttpServletRequest request) {
MediaType contentType = headers.getContentType();
if (contentType == null) {
private static MultiValueMap<String, String> initHeaders(
MultiValueMap<String, String> headerValues, HttpServletRequest request) {

HttpHeaders headers = null;
MediaType contentType = null;
if (!StringUtils.hasLength(headerValues.getFirst(HttpHeaders.CONTENT_TYPE))) {
String requestContentType = request.getContentType();
if (StringUtils.hasLength(requestContentType)) {
contentType = MediaType.parseMediaType(requestContentType);
headers = new HttpHeaders(headerValues);
headers.setContentType(contentType);
}
}
if (contentType != null && contentType.getCharset() == null) {
String encoding = request.getCharacterEncoding();
if (StringUtils.hasLength(encoding)) {
Charset charset = Charset.forName(encoding);
Map<String, String> params = new LinkedCaseInsensitiveMap<>();
params.putAll(contentType.getParameters());
params.put("charset", charset.toString());
headers.setContentType(
new MediaType(contentType.getType(), contentType.getSubtype(),
params));
params.put("charset", Charset.forName(encoding).toString());
headers.setContentType(new MediaType(contentType, params));
}
}
if (headers.getContentLength() == -1) {
if (headerValues.getFirst(HttpHeaders.CONTENT_TYPE) == null) {
int contentLength = request.getContentLength();
if (contentLength != -1) {
headers = (headers != null ? headers : new HttpHeaders(headerValues));
headers.setContentLength(contentLength);
}
}
return headers;
return (headers != null ? headers : headerValues);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ReflectionUtils;

/**
Expand Down Expand Up @@ -105,14 +106,13 @@ private static final class TomcatServerHttpRequest extends ServletServerHttpRequ
this.bufferSize = bufferSize;
}

private static HttpHeaders createTomcatHttpHeaders(HttpServletRequest request) {
private static MultiValueMap<String, String> createTomcatHttpHeaders(HttpServletRequest request) {
RequestFacade requestFacade = getRequestFacade(request);
org.apache.catalina.connector.Request connectorRequest = (org.apache.catalina.connector.Request)
ReflectionUtils.getField(COYOTE_REQUEST_FIELD, requestFacade);
Assert.state(connectorRequest != null, "No Tomcat connector request");
Request tomcatRequest = connectorRequest.getCoyoteRequest();
TomcatHeadersAdapter headers = new TomcatHeadersAdapter(tomcatRequest.getMimeHeaders());
return new HttpHeaders(headers);
return new TomcatHeadersAdapter(tomcatRequest.getMimeHeaders());
}

private static RequestFacade getRequestFacade(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.springframework.core.io.buffer.DataBufferWrapper;
import org.springframework.core.io.buffer.PooledDataBuffer;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
Expand All @@ -63,7 +62,7 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest {
public UndertowServerHttpRequest(HttpServerExchange exchange, DataBufferFactory bufferFactory)
throws URISyntaxException {

super(initUri(exchange), "", initHeaders(exchange));
super(initUri(exchange), "", new UndertowHeadersAdapter(exchange.getRequestHeaders()));
this.exchange = exchange;
this.body = new RequestBodyPublisher(exchange, bufferFactory);
this.body.registerListeners(exchange);
Expand All @@ -77,10 +76,6 @@ private static URI initUri(HttpServerExchange exchange) throws URISyntaxExceptio
return new URI(requestUriAndQuery);
}

private static HttpHeaders initHeaders(HttpServerExchange exchange) {
return new HttpHeaders(new UndertowHeadersAdapter(exchange.getRequestHeaders()));
}

@Override
public String getMethodValue() {
return this.exchange.getRequestMethod().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
class DefaultRendering implements Rendering {

private static final HttpHeaders EMPTY_HEADERS = HttpHeaders.readOnlyHttpHeaders(new HttpHeaders());
private static final HttpHeaders EMPTY_HEADERS = HttpHeaders.EMPTY;


private final Object view;
Expand Down

0 comments on commit 3276f81

Please sign in to comment.