Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
bclozel committed Jan 7, 2020
1 parent ea6d2ea commit ffc1f96
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
32 changes: 16 additions & 16 deletions spring-web/src/main/java/org/springframework/http/HttpHeaders.java
Expand Up @@ -1522,6 +1522,22 @@ public List<String> getValuesAsList(String headerName) {
return Collections.emptyList();
}

/**
* Remove the well-known {@code "Content-*"} HTTP headers.
* <p>Such headers should be cleared from the response if the intended
* body can't be written due to errors.
* @since 5.2.3
*/
public void clearContentHeaders() {
this.headers.remove(HttpHeaders.CONTENT_DISPOSITION);
this.headers.remove(HttpHeaders.CONTENT_ENCODING);
this.headers.remove(HttpHeaders.CONTENT_LANGUAGE);
this.headers.remove(HttpHeaders.CONTENT_LENGTH);
this.headers.remove(HttpHeaders.CONTENT_LOCATION);
this.headers.remove(HttpHeaders.CONTENT_RANGE);
this.headers.remove(HttpHeaders.CONTENT_TYPE);
}

/**
* Retrieve a combined result from the field values of the ETag header.
* @param headerName the header name
Expand Down Expand Up @@ -1827,22 +1843,6 @@ public static String encodeBasicAuth(String username, String password, @Nullable
return new String(encodedBytes, charset);
}

/**
* Remove the well-known {@code "Content-*"} HTTP headers from the given instance.
* <p>Such headers should be cleared, if possible, from the response if the intended
* body can't be written due to errors.
* @since 5.2.3
*/
public static void clearContentHeaders(HttpHeaders headers) {
headers.remove(HttpHeaders.CONTENT_DISPOSITION);
headers.remove(HttpHeaders.CONTENT_ENCODING);
headers.remove(HttpHeaders.CONTENT_LANGUAGE);
headers.remove(HttpHeaders.CONTENT_LENGTH);
headers.remove(HttpHeaders.CONTENT_LOCATION);
headers.remove(HttpHeaders.CONTENT_RANGE);
headers.remove(HttpHeaders.CONTENT_TYPE);
}

// Package-private: used in ResponseCookie
static String formatDate(long date) {
Instant instant = Instant.ofEpochMilli(date);
Expand Down
Expand Up @@ -75,6 +75,11 @@ public List<MediaType> getAccept() {
}
}

@Override
public void clearContentHeaders() {
// No-op.
}

@Override
public List<String> get(Object key) {
List<String> values = this.headers.get(key);
Expand Down
Expand Up @@ -182,22 +182,16 @@ public final Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
return ((Mono<? extends DataBuffer>) body).flatMap(buffer ->
doCommit(() -> writeWithInternal(Mono.just(buffer)))
.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release))
.doOnError(t -> clearContentHeaders());
.doOnError(t -> this.getHeaders().clearContentHeaders());
}
return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeWithInternal(inner)))
.doOnError(t -> clearContentHeaders());
.doOnError(t -> this.getHeaders().clearContentHeaders());
}

@Override
public final Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeAndFlushWithInternal(inner)))
.doOnError(t -> clearContentHeaders());
}

private void clearContentHeaders() {
if (!this.isCommitted()) {
HttpHeaders.clearContentHeaders(this.getHeaders());
}
.doOnError(t -> this.getHeaders().clearContentHeaders());
}

@Override
Expand Down
Expand Up @@ -29,7 +29,6 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.http.HttpHeaders;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -210,9 +209,7 @@ private Mono<HandlerResult> handleException(Throwable exception, HandlerMethod h

// Success and error responses may use different content types
exchange.getAttributes().remove(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
if (!exchange.getResponse().isCommitted()) {
HttpHeaders.clearContentHeaders(exchange.getResponse().getHeaders());
}
exchange.getResponse().getHeaders().clearContentHeaders();

InvocableHandlerMethod invocable = this.methodResolver.getExceptionHandlerMethod(exception, handlerMethod);
if (invocable != null) {
Expand Down

0 comments on commit ffc1f96

Please sign in to comment.