Skip to content

Commit 27c1280

Browse files
committed
Consider negative contentLength() result as not resolvable
Issue: SPR-13571
1 parent 37de0b2 commit 27c1280

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

spring-web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -217,7 +217,7 @@ else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) {
217217
headers.setContentType(contentTypeToUse);
218218
}
219219
}
220-
if (headers.getContentLength() == -1) {
220+
if (headers.getContentLength() < 0) {
221221
Long contentLength = getContentLength(t, headers.getContentType());
222222
if (contentLength != null) {
223223
headers.setContentLength(contentLength);

spring-web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,7 +90,11 @@ protected MediaType getDefaultContentType(Resource resource) {
9090
protected Long getContentLength(Resource resource, MediaType contentType) throws IOException {
9191
// Don't try to determine contentLength on InputStreamResource - cannot be read afterwards...
9292
// Note: custom InputStreamResource subclasses could provide a pre-calculated content length!
93-
return (InputStreamResource.class == resource.getClass() ? null : resource.contentLength());
93+
if (InputStreamResource.class == resource.getClass()) {
94+
return null;
95+
}
96+
long contentLength = resource.contentLength();
97+
return (contentLength < 0 ? null : contentLength);
9498
}
9599

96100
@Override

0 commit comments

Comments
 (0)