Skip to content

Commit

Permalink
Polishing contribution and fix failing test
Browse files Browse the repository at this point in the history
The failing test is for Apache HttpComponents where we cannot apply the
Content-Length together with other headers, and must pass it instead
explicitly to ReactiveEntityProducer when writing at which point it gets
set in the native headers.

Closes spring-projectsgh-27768
  • Loading branch information
rstoyanchev committed Jan 11, 2022
1 parent bd1f34e commit e8e7fbb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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.
Expand Down Expand Up @@ -74,6 +74,7 @@ public AbstractClientHttpRequest(HttpHeaders headers) {
this.cookies = new LinkedMultiValueMap<>();
}


@Override
public HttpHeaders getHeaders() {
if (this.readOnlyHeaders != null) {
Expand All @@ -88,6 +89,16 @@ else if (State.COMMITTED.equals(this.state.get())) {
}
}

/**
* Initialize the read-only headers after the request is committed.
* <p>By default, this method simply applies a read-only wrapper.
* Subclasses can do the same for headers from the native request.
* @since 5.3.15
*/
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(this.headers);
}

@Override
public MultiValueMap<String, HttpCookie> getCookies() {
if (State.COMMITTED.equals(this.state.get())) {
Expand Down Expand Up @@ -143,14 +154,6 @@ protected Mono<Void> doCommit(@Nullable Supplier<? extends Publisher<Void>> writ
return Flux.concat(actions).then();
}

/**
* Initialize read-only headers with underlying request headers.
* @return read-only headers
*/
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(this.headers);
}


/**
* Apply header changes from {@link #getHeaders()} to the underlying request.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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.
Expand Down Expand Up @@ -62,6 +62,8 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest {
@Nullable
private Flux<ByteBuffer> byteBufferFlux;

private transient long contentLength = -1;


public HttpComponentsClientHttpRequest(HttpMethod method, URI uri, HttpClientContext context,
DataBufferFactory dataBufferFactory) {
Expand Down Expand Up @@ -118,11 +120,6 @@ public Mono<Void> setComplete() {
return doCommit();
}

@Override
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(new HttpComponentsHeadersAdapter(this.httpRequest));
}

@Override
protected void applyHeaders() {
HttpHeaders headers = getHeaders();
Expand All @@ -135,6 +132,8 @@ protected void applyHeaders() {
if (!this.httpRequest.containsHeader(HttpHeaders.ACCEPT)) {
this.httpRequest.addHeader(HttpHeaders.ACCEPT, ALL_VALUE);
}

this.contentLength = headers.getContentLength();
}

@Override
Expand All @@ -156,6 +155,11 @@ protected void applyCookies() {
});
}

@Override
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(new HttpComponentsHeadersAdapter(this.httpRequest));
}

public AsyncRequestProducer toRequestProducer() {
ReactiveEntityProducer reactiveEntityProducer = null;

Expand All @@ -165,8 +169,8 @@ public AsyncRequestProducer toRequestProducer() {
if (getHeaders().getContentType() != null) {
contentType = ContentType.parse(getHeaders().getContentType().toString());
}
reactiveEntityProducer = new ReactiveEntityProducer(this.byteBufferFlux, getHeaders().getContentLength(),
contentType, contentEncoding);
reactiveEntityProducer = new ReactiveEntityProducer(
this.byteBufferFlux, this.contentLength, contentType, contentEncoding);
}

return new BasicRequestProducer(this.httpRequest, reactiveEntityProducer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
Expand Down Expand Up @@ -126,11 +126,6 @@ public void failed(Throwable t) {
});
}

@Override
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(new JettyHeadersAdapter(this.jettyRequest.getHeaders()));
}

@Override
protected void applyCookies() {
getCookies().values().stream().flatMap(Collection::stream)
Expand All @@ -147,9 +142,13 @@ protected void applyHeaders() {
}
}

@Override
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(new JettyHeadersAdapter(this.jettyRequest.getHeaders()));
}

public ReactiveRequest toReactiveRequest() {
return this.builder.build();
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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.
Expand Down Expand Up @@ -122,11 +122,6 @@ public Mono<Void> setComplete() {
return doCommit(this.outbound::then);
}

@Override
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(new NettyHeadersAdapter(this.request.requestHeaders()));
}

@Override
protected void applyHeaders() {
getHeaders().forEach((key, value) -> this.request.requestHeaders().set(key, value));
Expand All @@ -139,4 +134,9 @@ protected void applyCookies() {
.forEach(this.request::addCookie);
}

@Override
protected HttpHeaders initReadOnlyHeaders() {
return HttpHeaders.readOnlyHttpHeaders(new NettyHeadersAdapter(this.request.requestHeaders()));
}

}

0 comments on commit e8e7fbb

Please sign in to comment.