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