Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose headers from native client request after request is committed #27768

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@ public AbstractClientHttpRequest(HttpHeaders headers) {
this.cookies = new LinkedMultiValueMap<>();
}


@Override
public HttpHeaders getHeaders() {
if (this.readOnlyHeaders != null) {
return this.readOnlyHeaders;
}
else if (State.COMMITTED.equals(this.state.get())) {
this.readOnlyHeaders = HttpHeaders.readOnlyHttpHeaders(this.headers);
this.readOnlyHeaders = initReadOnlyHeaders();
return this.readOnlyHeaders;
}
else {
Expand Down Expand Up @@ -144,6 +143,14 @@ 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
Expand Up @@ -115,6 +115,11 @@ 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.Set;

import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpMessage;

import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;
Expand All @@ -44,23 +44,23 @@
*/
class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {

private final HttpResponse response;
private final HttpMessage message;


HttpComponentsHeadersAdapter(HttpResponse response) {
this.response = response;
HttpComponentsHeadersAdapter(HttpMessage message) {
this.message = message;
}


@Override
public String getFirst(String key) {
Header header = this.response.getFirstHeader(key);
Header header = this.message.getFirstHeader(key);
return (header != null ? header.getValue() : null);
}

@Override
public void add(String key, @Nullable String value) {
this.response.addHeader(key, value);
this.message.addHeader(key, value);
}

@Override
Expand All @@ -75,7 +75,7 @@ public void addAll(MultiValueMap<String, String> values) {

@Override
public void set(String key, @Nullable String value) {
this.response.setHeader(key, value);
this.message.setHeader(key, value);
}

@Override
Expand All @@ -86,37 +86,37 @@ public void setAll(Map<String, String> values) {
@Override
public Map<String, String> toSingleValueMap() {
Map<String, String> map = CollectionUtils.newLinkedHashMap(size());
this.response.headerIterator().forEachRemaining(h -> map.putIfAbsent(h.getName(), h.getValue()));
this.message.headerIterator().forEachRemaining(h -> map.putIfAbsent(h.getName(), h.getValue()));
return map;
}

@Override
public int size() {
return this.response.getHeaders().length;
return this.message.getHeaders().length;
}

@Override
public boolean isEmpty() {
return (this.response.getHeaders().length == 0);
return (this.message.getHeaders().length == 0);
}

@Override
public boolean containsKey(Object key) {
return (key instanceof String headerName && this.response.containsHeader(headerName));
return (key instanceof String headerName && this.message.containsHeader(headerName));
}

@Override
public boolean containsValue(Object value) {
return (value instanceof String &&
Arrays.stream(this.response.getHeaders()).anyMatch(h -> h.getValue().equals(value)));
Arrays.stream(this.message.getHeaders()).anyMatch(h -> h.getValue().equals(value)));
}

@Nullable
@Override
public List<String> get(Object key) {
List<String> values = null;
if (containsKey(key)) {
Header[] headers = this.response.getHeaders((String) key);
Header[] headers = this.message.getHeaders((String) key);
values = new ArrayList<>(headers.length);
for (Header header : headers) {
values.add(header.getValue());
Expand All @@ -138,7 +138,7 @@ public List<String> put(String key, List<String> values) {
public List<String> remove(Object key) {
if (key instanceof String headerName) {
List<String> oldValues = get(key);
this.response.removeHeaders(headerName);
this.message.removeHeaders(headerName);
return oldValues;
}
return null;
Expand All @@ -151,13 +151,13 @@ public void putAll(Map<? extends String, ? extends List<String>> map) {

@Override
public void clear() {
this.response.setHeaders();
this.message.setHeaders();
}

@Override
public Set<String> keySet() {
Set<String> keys = new LinkedHashSet<>(size());
for (Header header : this.response.getHeaders()) {
for (Header header : this.message.getHeaders()) {
keys.add(header.getName());
}
return keys;
Expand All @@ -166,7 +166,7 @@ public Set<String> keySet() {
@Override
public Collection<List<String>> values() {
Collection<List<String>> values = new ArrayList<>(size());
for (Header header : this.response.getHeaders()) {
for (Header header : this.message.getHeaders()) {
values.add(get(header.getName()));
}
return values;
Expand Down Expand Up @@ -196,7 +196,7 @@ public String toString() {

private class EntryIterator implements Iterator<Entry<String, List<String>>> {

private final Iterator<Header> iterator = response.headerIterator();
private final Iterator<Header> iterator = message.headerIterator();

@Override
public boolean hasNext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public void failed(Throwable t) {
});
}

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

@Override
protected void applyCookies() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ZeroCopyHttpOutputMessage;

Expand Down Expand Up @@ -121,6 +122,11 @@ 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 Down