author | dfuchs |
Wed, 23 Oct 2019 15:54:39 +0100 | |
changeset 58758 | 2b13d126a2d8 |
parent 50681 | 4254bed3c09d |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
58758
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.io.UncheckedIOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.net.URI; |
49765 | 31 |
import java.net.http.HttpClient; |
32 |
import java.net.http.HttpHeaders; |
|
33 |
import jdk.internal.net.http.common.Log; |
|
34 |
import jdk.internal.net.http.common.Utils; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
class RedirectFilter implements HeaderFilter { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
HttpRequestImpl request; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
HttpClientImpl client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
HttpClient.Redirect policy; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
String method; |
49765 | 42 |
MultiExchange<?> exchange; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
static final int DEFAULT_MAX_REDIRECTS = 5; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
URI uri; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
|
50681 | 46 |
/* |
47 |
* NOT_MODIFIED status code results from a conditional GET where |
|
48 |
* the server does not (must not) return a response body because |
|
49 |
* the condition specified in the request disallows it |
|
50 |
*/ |
|
51 |
static final int HTTP_NOT_MODIFIED = 304; |
|
52 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
static final int max_redirects = Utils.getIntegerNetProperty( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
"jdk.httpclient.redirects.retrylimit", DEFAULT_MAX_REDIRECTS |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
|
48083 | 57 |
// A public no-arg constructor is required by FilterFactory |
58 |
public RedirectFilter() {} |
|
59 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
@Override |
49765 | 61 |
public synchronized void request(HttpRequestImpl r, MultiExchange<?> e) throws IOException { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
this.request = r; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
this.client = e.client(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
this.policy = client.followRedirects(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
this.method = r.method(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
this.uri = r.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
68 |
this.exchange = e; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
69 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
public synchronized HttpRequestImpl response(Response r) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
return handleResponse(r); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
|
49765 | 76 |
private static String redirectedMethod(int statusCode, String orig) { |
77 |
switch (statusCode) { |
|
78 |
case 301: |
|
79 |
case 302: |
|
80 |
return orig.equals("POST") ? "GET" : orig; |
|
81 |
case 303: |
|
82 |
return "GET"; |
|
83 |
case 307: |
|
84 |
case 308: |
|
85 |
return orig; |
|
86 |
default: |
|
87 |
// unexpected but return orig |
|
88 |
return orig; |
|
89 |
} |
|
90 |
} |
|
91 |
||
58758
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
92 |
private static boolean isRedirecting(int statusCode) { |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
93 |
// < 300: not a redirect codes |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
94 |
if (statusCode < 300) return false; |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
95 |
// 309-399 Unassigned => don't follow |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
96 |
// > 399: not a redirect code |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
97 |
if (statusCode > 308) return false; |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
98 |
switch (statusCode) { |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
99 |
// 300: MultipleChoice => don't follow |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
100 |
case 300: |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
101 |
return false; |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
102 |
// 304: Not Modified => don't follow |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
103 |
case 304: |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
104 |
return false; |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
105 |
// 305: Proxy Redirect => don't follow. |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
106 |
case 305: |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
107 |
return false; |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
108 |
// 306: Unused => don't follow |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
109 |
case 306: |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
110 |
return false; |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
111 |
// 301, 302, 303, 307, 308: OK to follow. |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
112 |
default: |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
113 |
return true; |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
114 |
} |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
115 |
} |
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
116 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
117 |
/** |
49765 | 118 |
* Checks to see if a new request is needed and returns it. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
* Null means response is ok to return to user. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
private HttpRequestImpl handleResponse(Response r) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
int rcode = r.statusCode(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
if (rcode == 200 || policy == HttpClient.Redirect.NEVER) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
} |
50681 | 126 |
|
127 |
if (rcode == HTTP_NOT_MODIFIED) |
|
128 |
return null; |
|
129 |
||
58758
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
130 |
if (isRedirecting(rcode)) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
URI redir = getRedirectedURI(r.headers()); |
49765 | 132 |
String newMethod = redirectedMethod(rcode, method); |
133 |
Log.logTrace("response code: {0}, redirected URI: {1}", rcode, redir); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
if (canRedirect(redir) && ++exchange.numberOfRedirects < max_redirects) { |
49765 | 135 |
Log.logTrace("redirect to: {0} with method: {1}", redir, newMethod); |
58758
2b13d126a2d8
8232625: HttpClient redirect policy should be more conservative
dfuchs
parents:
50681
diff
changeset
|
136 |
return HttpRequestImpl.newInstanceForRedirection(redir, newMethod, request, rcode != 303); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
} else { |
49765 | 138 |
Log.logTrace("not redirecting"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
145 |
private URI getRedirectedURI(HttpHeaders headers) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
URI redirectedURI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
147 |
redirectedURI = headers.firstValue("Location") |
48083 | 148 |
.map(URI::create) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
.orElseThrow(() -> new UncheckedIOException( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
150 |
new IOException("Invalid redirection"))); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
151 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
// redirect could be relative to original URL, but if not |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
// then redirect is used. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
redirectedURI = uri.resolve(redirectedURI); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
return redirectedURI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
156 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
157 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
private boolean canRedirect(URI redir) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
String newScheme = redir.getScheme(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
160 |
String oldScheme = uri.getScheme(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
161 |
switch (policy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
162 |
case ALWAYS: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
163 |
return true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
case NEVER: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
165 |
return false; |
49765 | 166 |
case NORMAL: |
167 |
return newScheme.equalsIgnoreCase(oldScheme) |
|
168 |
|| newScheme.equalsIgnoreCase("https"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
169 |
default: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
170 |
throw new InternalError(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
171 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
} |