Fall back to HTTP GET in case of 405 from HTTP HEAD
Closes gh-34217
This commit is contained in:
parent
cecebd0ef1
commit
ee60eb7207
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2023 the original author or authors.
|
* Copyright 2002-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -66,6 +66,20 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
|
||||||
else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
|
else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
else if (code == HttpURLConnection.HTTP_BAD_METHOD) {
|
||||||
|
con = url.openConnection();
|
||||||
|
customizeConnection(con);
|
||||||
|
if (con instanceof HttpURLConnection newHttpCon) {
|
||||||
|
code = newHttpCon.getResponseCode();
|
||||||
|
if (code == HttpURLConnection.HTTP_OK) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
httpCon = newHttpCon;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (con.getContentLengthLong() > 0) {
|
if (con.getContentLengthLong() > 0) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -111,6 +125,15 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
|
||||||
if (con instanceof HttpURLConnection httpCon) {
|
if (con instanceof HttpURLConnection httpCon) {
|
||||||
httpCon.setRequestMethod("HEAD");
|
httpCon.setRequestMethod("HEAD");
|
||||||
int code = httpCon.getResponseCode();
|
int code = httpCon.getResponseCode();
|
||||||
|
if (code == HttpURLConnection.HTTP_BAD_METHOD) {
|
||||||
|
con = url.openConnection();
|
||||||
|
customizeConnection(con);
|
||||||
|
if (!(con instanceof HttpURLConnection newHttpCon)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
code = newHttpCon.getResponseCode();
|
||||||
|
httpCon = newHttpCon;
|
||||||
|
}
|
||||||
if (code != HttpURLConnection.HTTP_OK) {
|
if (code != HttpURLConnection.HTTP_OK) {
|
||||||
httpCon.disconnect();
|
httpCon.disconnect();
|
||||||
return false;
|
return false;
|
||||||
|
@ -259,7 +282,14 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
|
||||||
if (con instanceof HttpURLConnection httpCon) {
|
if (con instanceof HttpURLConnection httpCon) {
|
||||||
httpCon.setRequestMethod("HEAD");
|
httpCon.setRequestMethod("HEAD");
|
||||||
}
|
}
|
||||||
return con.getContentLengthLong();
|
long length = con.getContentLengthLong();
|
||||||
|
if (length <= 0 && con instanceof HttpURLConnection httpCon &&
|
||||||
|
httpCon.getResponseCode() == HttpURLConnection.HTTP_BAD_METHOD) {
|
||||||
|
con = url.openConnection();
|
||||||
|
customizeConnection(con);
|
||||||
|
length = con.getContentLengthLong();
|
||||||
|
}
|
||||||
|
return length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -288,9 +318,17 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
|
||||||
httpCon.setRequestMethod("HEAD");
|
httpCon.setRequestMethod("HEAD");
|
||||||
}
|
}
|
||||||
long lastModified = con.getLastModified();
|
long lastModified = con.getLastModified();
|
||||||
if (fileCheck && lastModified == 0 && con.getContentLengthLong() <= 0) {
|
if (lastModified == 0) {
|
||||||
throw new FileNotFoundException(getDescription() +
|
if (con instanceof HttpURLConnection httpCon &&
|
||||||
" cannot be resolved in the file system for checking its last-modified timestamp");
|
httpCon.getResponseCode() == HttpURLConnection.HTTP_BAD_METHOD) {
|
||||||
|
con = url.openConnection();
|
||||||
|
customizeConnection(con);
|
||||||
|
lastModified = con.getLastModified();
|
||||||
|
}
|
||||||
|
if (fileCheck && con.getContentLengthLong() <= 0) {
|
||||||
|
throw new FileNotFoundException(getDescription() +
|
||||||
|
" cannot be resolved in the file system for checking its last-modified timestamp");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return lastModified;
|
return lastModified;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2024 the original author or authors.
|
* Copyright 2002-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -306,7 +306,9 @@ class ResourceTests {
|
||||||
@Nested
|
@Nested
|
||||||
class UrlResourceTests {
|
class UrlResourceTests {
|
||||||
|
|
||||||
private MockWebServer server = new MockWebServer();
|
private static final String LAST_MODIFIED = "Wed, 09 Apr 2014 09:57:42 GMT";
|
||||||
|
|
||||||
|
private final MockWebServer server = new MockWebServer();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void sameResourceWithRelativePathIsEqual() throws Exception {
|
void sameResourceWithRelativePathIsEqual() throws Exception {
|
||||||
|
@ -385,22 +387,44 @@ class ResourceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void missingRemoteResourceDoesNotExist() throws Exception {
|
void missingRemoteResourceDoesNotExist() throws Exception {
|
||||||
String baseUrl = startServer();
|
String baseUrl = startServer(true);
|
||||||
UrlResource resource = new UrlResource(baseUrl + "/missing");
|
UrlResource resource = new UrlResource(baseUrl + "/missing");
|
||||||
assertThat(resource.exists()).isFalse();
|
assertThat(resource.exists()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void remoteResourceExists() throws Exception {
|
void remoteResourceExists() throws Exception {
|
||||||
String baseUrl = startServer();
|
String baseUrl = startServer(true);
|
||||||
UrlResource resource = new UrlResource(baseUrl + "/resource");
|
UrlResource resource = new UrlResource(baseUrl + "/resource");
|
||||||
assertThat(resource.exists()).isTrue();
|
assertThat(resource.exists()).isTrue();
|
||||||
|
assertThat(resource.isReadable()).isTrue();
|
||||||
assertThat(resource.contentLength()).isEqualTo(6);
|
assertThat(resource.contentLength()).isEqualTo(6);
|
||||||
|
assertThat(resource.lastModified()).isGreaterThan(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void remoteResourceExistsFallback() throws Exception {
|
||||||
|
String baseUrl = startServer(false);
|
||||||
|
UrlResource resource = new UrlResource(baseUrl + "/resource");
|
||||||
|
assertThat(resource.exists()).isTrue();
|
||||||
|
assertThat(resource.isReadable()).isTrue();
|
||||||
|
assertThat(resource.contentLength()).isEqualTo(6);
|
||||||
|
assertThat(resource.lastModified()).isGreaterThan(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void canCustomizeHttpUrlConnectionForExists() throws Exception {
|
void canCustomizeHttpUrlConnectionForExists() throws Exception {
|
||||||
String baseUrl = startServer();
|
String baseUrl = startServer(true);
|
||||||
|
CustomResource resource = new CustomResource(baseUrl + "/resource");
|
||||||
|
assertThat(resource.exists()).isTrue();
|
||||||
|
RecordedRequest request = this.server.takeRequest();
|
||||||
|
assertThat(request.getMethod()).isEqualTo("HEAD");
|
||||||
|
assertThat(request.getHeader("Framework-Name")).isEqualTo("Spring");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void canCustomizeHttpUrlConnectionForExistsFallback() throws Exception {
|
||||||
|
String baseUrl = startServer(false);
|
||||||
CustomResource resource = new CustomResource(baseUrl + "/resource");
|
CustomResource resource = new CustomResource(baseUrl + "/resource");
|
||||||
assertThat(resource.exists()).isTrue();
|
assertThat(resource.exists()).isTrue();
|
||||||
RecordedRequest request = this.server.takeRequest();
|
RecordedRequest request = this.server.takeRequest();
|
||||||
|
@ -410,7 +434,7 @@ class ResourceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void canCustomizeHttpUrlConnectionForRead() throws Exception {
|
void canCustomizeHttpUrlConnectionForRead() throws Exception {
|
||||||
String baseUrl = startServer();
|
String baseUrl = startServer(true);
|
||||||
CustomResource resource = new CustomResource(baseUrl + "/resource");
|
CustomResource resource = new CustomResource(baseUrl + "/resource");
|
||||||
assertThat(resource.getInputStream()).hasContent("Spring");
|
assertThat(resource.getInputStream()).hasContent("Spring");
|
||||||
RecordedRequest request = this.server.takeRequest();
|
RecordedRequest request = this.server.takeRequest();
|
||||||
|
@ -420,7 +444,7 @@ class ResourceTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void useUserInfoToSetBasicAuth() throws Exception {
|
void useUserInfoToSetBasicAuth() throws Exception {
|
||||||
startServer();
|
startServer(true);
|
||||||
UrlResource resource = new UrlResource(
|
UrlResource resource = new UrlResource(
|
||||||
"http://alice:secret@localhost:" + this.server.getPort() + "/resource");
|
"http://alice:secret@localhost:" + this.server.getPort() + "/resource");
|
||||||
assertThat(resource.getInputStream()).hasContent("Spring");
|
assertThat(resource.getInputStream()).hasContent("Spring");
|
||||||
|
@ -436,8 +460,8 @@ class ResourceTests {
|
||||||
this.server.shutdown();
|
this.server.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String startServer() throws Exception {
|
private String startServer(boolean withHeadSupport) throws Exception {
|
||||||
this.server.setDispatcher(new ResourceDispatcher());
|
this.server.setDispatcher(new ResourceDispatcher(withHeadSupport));
|
||||||
this.server.start();
|
this.server.start();
|
||||||
return "http://localhost:" + this.server.getPort();
|
return "http://localhost:" + this.server.getPort();
|
||||||
}
|
}
|
||||||
|
@ -456,15 +480,26 @@ class ResourceTests {
|
||||||
|
|
||||||
class ResourceDispatcher extends Dispatcher {
|
class ResourceDispatcher extends Dispatcher {
|
||||||
|
|
||||||
|
boolean withHeadSupport;
|
||||||
|
|
||||||
|
public ResourceDispatcher(boolean withHeadSupport) {
|
||||||
|
this.withHeadSupport = withHeadSupport;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MockResponse dispatch(RecordedRequest request) {
|
public MockResponse dispatch(RecordedRequest request) {
|
||||||
if (request.getPath().equals("/resource")) {
|
if (request.getPath().equals("/resource")) {
|
||||||
return switch (request.getMethod()) {
|
return switch (request.getMethod()) {
|
||||||
case "HEAD" -> new MockResponse()
|
case "HEAD" -> (this.withHeadSupport ?
|
||||||
.addHeader("Content-Length", "6");
|
new MockResponse()
|
||||||
|
.addHeader("Content-Type", "text/plain")
|
||||||
|
.addHeader("Content-Length", "6")
|
||||||
|
.addHeader("Last-Modified", LAST_MODIFIED) :
|
||||||
|
new MockResponse().setResponseCode(405));
|
||||||
case "GET" -> new MockResponse()
|
case "GET" -> new MockResponse()
|
||||||
.addHeader("Content-Length", "6")
|
|
||||||
.addHeader("Content-Type", "text/plain")
|
.addHeader("Content-Type", "text/plain")
|
||||||
|
.addHeader("Content-Length", "6")
|
||||||
|
.addHeader("Last-Modified", LAST_MODIFIED)
|
||||||
.setBody("Spring");
|
.setBody("Spring");
|
||||||
default -> new MockResponse().setResponseCode(404);
|
default -> new MockResponse().setResponseCode(404);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue