author | dfuchs |
Wed, 23 Oct 2019 15:54:39 +0100 | |
changeset 58758 | 2b13d126a2d8 |
parent 50681 | 4254bed3c09d |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
58758
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
48083 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
import java.io.IOException; |
|
25 |
import java.io.InputStream; |
|
26 |
import java.io.OutputStream; |
|
27 |
import java.util.function.Supplier; |
|
50681 | 28 |
import jdk.internal.net.http.common.HttpHeadersBuilder; |
48083 | 29 |
|
30 |
public class Http2RedirectHandler implements Http2Handler { |
|
31 |
||
32 |
final Supplier<String> supplier; |
|
33 |
||
34 |
public Http2RedirectHandler(Supplier<String> redirectSupplier) { |
|
35 |
supplier = redirectSupplier; |
|
36 |
} |
|
37 |
||
38 |
@Override |
|
39 |
public void handle(Http2TestExchange t) throws IOException { |
|
40 |
examineExchange(t); |
|
41 |
try (InputStream is = t.getRequestBody()) { |
|
42 |
is.readAllBytes(); |
|
43 |
String location = supplier.get(); |
|
44 |
System.err.printf("RedirectHandler request to %s from %s\n", |
|
58758
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
45 |
t.getRequestURI().toString(), t.getRemoteAddress().toString()); |
48083 | 46 |
System.err.println("Redirecting to: " + location); |
50681 | 47 |
HttpHeadersBuilder headersBuilder = t.getResponseHeaders(); |
48 |
headersBuilder.addHeader("Location", location); |
|
58758
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
49 |
t.sendResponseHeaders(redirectCode(), 1024); |
48083 | 50 |
byte[] bb = new byte[1024]; |
51 |
OutputStream os = t.getResponseBody(); |
|
52 |
os.write(bb); |
|
53 |
os.close(); |
|
54 |
t.close(); |
|
55 |
} |
|
56 |
} |
|
57 |
||
58758
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
58 |
protected int redirectCode() { |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
59 |
return 301; |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
60 |
} |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
61 |
|
48083 | 62 |
// override in sub-class to examine the exchange, but don't |
63 |
// alter transaction state by reading the request body etc. |
|
64 |
protected void examineExchange(Http2TestExchange t) { |
|
65 |
} |
|
66 |
} |