Skip to content

Commit 6643efe

Browse files
committed
Merge branch '5.3.x' into main
2 parents 3477ec0 + e8e7fbb commit 6643efe

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

spring-web/src/main/java/org/springframework/http/client/reactive/AbstractClientHttpRequest.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -81,14 +81,24 @@ public HttpHeaders getHeaders() {
8181
return this.readOnlyHeaders;
8282
}
8383
else if (State.COMMITTED.equals(this.state.get())) {
84-
this.readOnlyHeaders = HttpHeaders.readOnlyHttpHeaders(this.headers);
84+
this.readOnlyHeaders = initReadOnlyHeaders();
8585
return this.readOnlyHeaders;
8686
}
8787
else {
8888
return this.headers;
8989
}
9090
}
9191

92+
/**
93+
* Initialize the read-only headers after the request is committed.
94+
* <p>By default, this method simply applies a read-only wrapper.
95+
* Subclasses can do the same for headers from the native request.
96+
* @since 5.3.15
97+
*/
98+
protected HttpHeaders initReadOnlyHeaders() {
99+
return HttpHeaders.readOnlyHttpHeaders(this.headers);
100+
}
101+
92102
@Override
93103
public MultiValueMap<String, HttpCookie> getCookies() {
94104
if (State.COMMITTED.equals(this.state.get())) {

spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -61,6 +61,8 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest {
6161
@Nullable
6262
private Flux<ByteBuffer> byteBufferFlux;
6363

64+
private transient long contentLength = -1;
65+
6466

6567
public HttpComponentsClientHttpRequest(HttpMethod method, URI uri, HttpClientContext context,
6668
DataBufferFactory dataBufferFactory) {
@@ -127,6 +129,8 @@ protected void applyHeaders() {
127129
if (!this.httpRequest.containsHeader(HttpHeaders.ACCEPT)) {
128130
this.httpRequest.addHeader(HttpHeaders.ACCEPT, MediaType.ALL_VALUE);
129131
}
132+
133+
this.contentLength = headers.getContentLength();
130134
}
131135

132136
@Override
@@ -148,6 +152,11 @@ protected void applyCookies() {
148152
});
149153
}
150154

155+
@Override
156+
protected HttpHeaders initReadOnlyHeaders() {
157+
return HttpHeaders.readOnlyHttpHeaders(new HttpComponentsHeadersAdapter(this.httpRequest));
158+
}
159+
151160
public AsyncRequestProducer toRequestProducer() {
152161
ReactiveEntityProducer reactiveEntityProducer = null;
153162

@@ -157,8 +166,8 @@ public AsyncRequestProducer toRequestProducer() {
157166
if (getHeaders().getContentType() != null) {
158167
contentType = ContentType.parse(getHeaders().getContentType().toString());
159168
}
160-
reactiveEntityProducer = new ReactiveEntityProducer(this.byteBufferFlux, getHeaders().getContentLength(),
161-
contentType, contentEncoding);
169+
reactiveEntityProducer = new ReactiveEntityProducer(
170+
this.byteBufferFlux, this.contentLength, contentType, contentEncoding);
162171
}
163172

164173
return new BasicRequestProducer(this.httpRequest, reactiveEntityProducer);

spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsHeadersAdapter.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.Set;
2929

3030
import org.apache.hc.core5.http.Header;
31-
import org.apache.hc.core5.http.HttpResponse;
31+
import org.apache.hc.core5.http.HttpMessage;
3232

3333
import org.springframework.http.HttpHeaders;
3434
import org.springframework.lang.Nullable;
@@ -44,23 +44,23 @@
4444
*/
4545
class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
4646

47-
private final HttpResponse response;
47+
private final HttpMessage message;
4848

4949

50-
HttpComponentsHeadersAdapter(HttpResponse response) {
51-
this.response = response;
50+
HttpComponentsHeadersAdapter(HttpMessage message) {
51+
this.message = message;
5252
}
5353

5454

5555
@Override
5656
public String getFirst(String key) {
57-
Header header = this.response.getFirstHeader(key);
57+
Header header = this.message.getFirstHeader(key);
5858
return (header != null ? header.getValue() : null);
5959
}
6060

6161
@Override
6262
public void add(String key, @Nullable String value) {
63-
this.response.addHeader(key, value);
63+
this.message.addHeader(key, value);
6464
}
6565

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

7676
@Override
7777
public void set(String key, @Nullable String value) {
78-
this.response.setHeader(key, value);
78+
this.message.setHeader(key, value);
7979
}
8080

8181
@Override
@@ -86,37 +86,37 @@ public void setAll(Map<String, String> values) {
8686
@Override
8787
public Map<String, String> toSingleValueMap() {
8888
Map<String, String> map = CollectionUtils.newLinkedHashMap(size());
89-
this.response.headerIterator().forEachRemaining(h -> map.putIfAbsent(h.getName(), h.getValue()));
89+
this.message.headerIterator().forEachRemaining(h -> map.putIfAbsent(h.getName(), h.getValue()));
9090
return map;
9191
}
9292

9393
@Override
9494
public int size() {
95-
return this.response.getHeaders().length;
95+
return this.message.getHeaders().length;
9696
}
9797

9898
@Override
9999
public boolean isEmpty() {
100-
return (this.response.getHeaders().length == 0);
100+
return (this.message.getHeaders().length == 0);
101101
}
102102

103103
@Override
104104
public boolean containsKey(Object key) {
105-
return (key instanceof String headerName && this.response.containsHeader(headerName));
105+
return (key instanceof String headerName && this.message.containsHeader(headerName));
106106
}
107107

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

114114
@Nullable
115115
@Override
116116
public List<String> get(Object key) {
117117
List<String> values = null;
118118
if (containsKey(key)) {
119-
Header[] headers = this.response.getHeaders((String) key);
119+
Header[] headers = this.message.getHeaders((String) key);
120120
values = new ArrayList<>(headers.length);
121121
for (Header header : headers) {
122122
values.add(header.getValue());
@@ -138,7 +138,7 @@ public List<String> put(String key, List<String> values) {
138138
public List<String> remove(Object key) {
139139
if (key instanceof String headerName) {
140140
List<String> oldValues = get(key);
141-
this.response.removeHeaders(headerName);
141+
this.message.removeHeaders(headerName);
142142
return oldValues;
143143
}
144144
return null;
@@ -151,13 +151,13 @@ public void putAll(Map<? extends String, ? extends List<String>> map) {
151151

152152
@Override
153153
public void clear() {
154-
this.response.setHeaders();
154+
this.message.setHeaders();
155155
}
156156

157157
@Override
158158
public Set<String> keySet() {
159159
Set<String> keys = new LinkedHashSet<>(size());
160-
for (Header header : this.response.getHeaders()) {
160+
for (Header header : this.message.getHeaders()) {
161161
keys.add(header.getName());
162162
}
163163
return keys;
@@ -166,7 +166,7 @@ public Set<String> keySet() {
166166
@Override
167167
public Collection<List<String>> values() {
168168
Collection<List<String>> values = new ArrayList<>(size());
169-
for (Header header : this.response.getHeaders()) {
169+
for (Header header : this.message.getHeaders()) {
170170
values.add(get(header.getName()));
171171
}
172172
return values;
@@ -196,7 +196,7 @@ public String toString() {
196196

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

199-
private final Iterator<Header> iterator = response.headerIterator();
199+
private final Iterator<Header> iterator = message.headerIterator();
200200

201201
@Override
202202
public boolean hasNext() {

spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpRequest.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -125,7 +125,6 @@ public void failed(Throwable t) {
125125
});
126126
}
127127

128-
129128
@Override
130129
protected void applyCookies() {
131130
getCookies().values().stream().flatMap(Collection::stream)
@@ -144,9 +143,13 @@ protected void applyHeaders() {
144143
});
145144
}
146145

146+
@Override
147+
protected HttpHeaders initReadOnlyHeaders() {
148+
return HttpHeaders.readOnlyHttpHeaders(new JettyHeadersAdapter(this.jettyRequest.getHeaders()));
149+
}
150+
147151
public ReactiveRequest toReactiveRequest() {
148152
return this.builder.build();
149153
}
150154

151-
152155
}

spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpRequest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -31,6 +31,7 @@
3131
import org.springframework.core.io.buffer.DataBuffer;
3232
import org.springframework.core.io.buffer.DataBufferFactory;
3333
import org.springframework.core.io.buffer.NettyDataBufferFactory;
34+
import org.springframework.http.HttpHeaders;
3435
import org.springframework.http.HttpMethod;
3536
import org.springframework.http.ZeroCopyHttpOutputMessage;
3637

@@ -133,4 +134,9 @@ protected void applyCookies() {
133134
.forEach(this.request::addCookie);
134135
}
135136

137+
@Override
138+
protected HttpHeaders initReadOnlyHeaders() {
139+
return HttpHeaders.readOnlyHttpHeaders(new NettyHeadersAdapter(this.request.requestHeaders()));
140+
}
141+
136142
}

0 commit comments

Comments
 (0)