From 323af718e20d2cd3796dd0273e646a674d532260 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 1 Oct 2020 12:17:03 +0100 Subject: [PATCH] Only change the method of requests that Jetty won't handle by default Previously, JettyEmbeddedErrorHandler would change the method of every request that is handles to GET. This was being done to work around Jetty's error handling only dealing with GET, POST, and HEAD requests by default. It had the unwanted side-effect of causing an error response to a HEAD request having a body as, from the error handling's perspective, it was a GET request. This commit updates JettyEmbeddedErrorHandler to only set the method on a request for which error handling is being performed if the method isn't already one that will be handled, leaving the method of GET, POST, and HEAD requests unchanged. Unfortunately, short of implementing an HTTP client, this change cannot be tested as the Apache HttpClient, OkHttp, and the JDK's HttpURLConnection all silently drop the body of a response to a HEAD request, preventing a test from asserting that a body hasn't been sent. Closes gh-23551 --- .../web/embedded/jetty/JettyEmbeddedErrorHandler.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyEmbeddedErrorHandler.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyEmbeddedErrorHandler.java index 170f4680fda..8804193a075 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyEmbeddedErrorHandler.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyEmbeddedErrorHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-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. @@ -17,6 +17,9 @@ package org.springframework.boot.web.embedded.jetty; import java.io.IOException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -37,6 +40,8 @@ import org.eclipse.jetty.servlet.ErrorPageErrorHandler; */ class JettyEmbeddedErrorHandler extends ErrorPageErrorHandler { + private static final Set HANDLED_HTTP_METHODS = new HashSet<>(Arrays.asList("GET", "POST", "HEAD")); + @Override public boolean errorPageForMethod(String method) { return true; @@ -45,7 +50,9 @@ class JettyEmbeddedErrorHandler extends ErrorPageErrorHandler { @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { - baseRequest.setMethod("GET"); + if (!HANDLED_HTTP_METHODS.contains(baseRequest.getMethod())) { + baseRequest.setMethod("GET"); + } super.doError(target, baseRequest, request, response); }