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