Skip to content

Commit

Permalink
Fall back to all media types if encountering invalid Accept header
Browse files Browse the repository at this point in the history
A warn log message is printed, and if log level is set to debug, the
exception stacktrace is logged, too.

Closes gh-37455
  • Loading branch information
mhalbritter committed Sep 19, 2023
1 parent 1f527c3 commit 95690f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.core.io.Resource;
import org.springframework.core.log.LogMessage;
import org.springframework.http.HttpHeaders;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
Expand All @@ -41,6 +42,7 @@
*
* @author Andy Wilkinson
* @author Bruce Brouwer
* @author Moritz Halbritter
* @see WelcomePageNotAcceptableHandlerMapping
*/
final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping {
Expand Down Expand Up @@ -80,7 +82,13 @@ private boolean isHtmlTextAccepted(HttpServletRequest request) {
private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request) {
String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
if (StringUtils.hasText(acceptHeader)) {
return MediaType.parseMediaTypes(acceptHeader);
try {
return MediaType.parseMediaTypes(acceptHeader);
}
catch (InvalidMediaTypeException ex) {
logger.warn("Received invalid Accept header. Assuming all media types are accepted",
logger.isDebugEnabled() ? ex : null);
}
}
return MEDIA_TYPES_ALL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.servlet.http.HttpServletResponse;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -31,6 +32,8 @@
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -54,7 +57,9 @@
* Tests for {@link WelcomePageHandlerMapping}.
*
* @author Andy Wilkinson
* @author Moritz Halbritter
*/
@ExtendWith(OutputCaptureExtension.class)
class WelcomePageHandlerMappingTests {

private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
Expand Down Expand Up @@ -164,6 +169,17 @@ void prefersAStaticResourceToATemplate() {
});
}

@Test
void logsInvalidAcceptHeader(CapturedOutput output) {
this.contextRunner.withUserConfiguration(TemplateConfiguration.class).run((context) -> {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
mockMvc.perform(get("/").accept("*/*q=0.8"))
.andExpect(status().isOk())
.andExpect(content().string("index template"));
});
assertThat(output).contains("Received invalid Accept header. Assuming all media types are accepted");
}

@Configuration(proxyBeanMethods = false)
static class HandlerMappingConfiguration {

Expand Down

0 comments on commit 95690f7

Please sign in to comment.