39
39
import java .net .http .HttpClient ;
40
40
import java .net .http .HttpHeaders ;
41
41
import java .net .http .HttpRequest ;
42
+ import java .util .function .BiPredicate ;
42
43
43
44
import jdk .internal .net .http .common .Alpns ;
44
45
import jdk .internal .net .http .common .HttpHeadersBuilder ;
@@ -148,7 +149,11 @@ public static HttpRequestImpl newInstanceForRedirection(URI uri,
148
149
String method ,
149
150
HttpRequestImpl other ,
150
151
boolean mayHaveBody ) {
151
- return new HttpRequestImpl (uri , method , other , mayHaveBody );
152
+ if (uri .getScheme ().equalsIgnoreCase (other .uri .getScheme ()) &&
153
+ uri .getRawAuthority ().equals (other .uri .getRawAuthority ())) {
154
+ return new HttpRequestImpl (uri , method , other , mayHaveBody , Optional .empty ());
155
+ }
156
+ return new HttpRequestImpl (uri , method , other , mayHaveBody , Optional .of (Utils .ALLOWED_REDIRECT_HEADERS ));
152
157
}
153
158
154
159
/** Returns a new instance suitable for authentication. */
@@ -168,9 +173,19 @@ private HttpRequestImpl(URI uri,
168
173
String method ,
169
174
HttpRequestImpl other ,
170
175
boolean mayHaveBody ) {
176
+ this (uri , method , other , mayHaveBody , Optional .empty ());
177
+ }
178
+
179
+ private HttpRequestImpl (URI uri ,
180
+ String method ,
181
+ HttpRequestImpl other ,
182
+ boolean mayHaveBody ,
183
+ Optional <BiPredicate <String , String >> redirectHeadersFilter ) {
171
184
assert method == null || Utils .isValidName (method );
172
- this .method = method == null ? "GET" : method ;
173
- this .userHeaders = other .userHeaders ;
185
+ this .method = method == null ? "GET" : method ;
186
+ HttpHeaders userHeaders = redirectHeadersFilter .isPresent () ?
187
+ HttpHeaders .of (other .userHeaders .map (), redirectHeadersFilter .get ()) : other .userHeaders ;
188
+ this .userHeaders = userHeaders ;
174
189
this .isWebSocket = other .isWebSocket ;
175
190
this .systemHeadersBuilder = new HttpHeadersBuilder ();
176
191
if (userHeaders .firstValue ("User-Agent" ).isEmpty ()) {
0 commit comments