49765
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.internal.net.http;
|
|
27 |
|
|
28 |
import java.io.File;
|
|
29 |
import java.io.FilePermission;
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.io.UncheckedIOException;
|
|
32 |
import java.net.URI;
|
|
33 |
import java.nio.file.Files;
|
|
34 |
import java.nio.file.OpenOption;
|
|
35 |
import java.nio.file.Path;
|
|
36 |
import java.nio.file.Paths;
|
|
37 |
import java.util.List;
|
|
38 |
import java.util.concurrent.CompletableFuture;
|
|
39 |
import java.util.concurrent.ConcurrentMap;
|
|
40 |
import java.util.function.Function;
|
|
41 |
import java.net.http.HttpHeaders;
|
|
42 |
import java.net.http.HttpRequest;
|
|
43 |
import java.net.http.HttpResponse;
|
|
44 |
import java.net.http.HttpResponse.BodyHandler;
|
|
45 |
import java.net.http.HttpResponse.ResponseInfo;
|
|
46 |
import java.net.http.HttpResponse.BodySubscriber;
|
|
47 |
import java.util.regex.Matcher;
|
|
48 |
import java.util.regex.Pattern;
|
|
49 |
import jdk.internal.net.http.ResponseSubscribers.PathSubscriber;
|
|
50 |
import static java.util.regex.Pattern.CASE_INSENSITIVE;
|
|
51 |
|
|
52 |
public final class ResponseBodyHandlers {
|
|
53 |
|
|
54 |
private ResponseBodyHandlers() { }
|
|
55 |
|
|
56 |
private static final String pathForSecurityCheck(Path path) {
|
|
57 |
return path.toFile().getPath();
|
|
58 |
}
|
|
59 |
|
|
60 |
/**
|
|
61 |
* A Path body handler.
|
|
62 |
*/
|
|
63 |
public static class PathBodyHandler implements BodyHandler<Path>{
|
|
64 |
private final Path file;
|
|
65 |
private final List<OpenOption> openOptions; // immutable list
|
|
66 |
private final FilePermission filePermission;
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Factory for creating PathBodyHandler.
|
|
70 |
*
|
|
71 |
* Permission checks are performed here before construction of the
|
|
72 |
* PathBodyHandler. Permission checking and construction are
|
|
73 |
* deliberately and tightly co-located.
|
|
74 |
*/
|
|
75 |
public static PathBodyHandler create(Path file,
|
|
76 |
List<OpenOption> openOptions) {
|
|
77 |
FilePermission filePermission = null;
|
|
78 |
SecurityManager sm = System.getSecurityManager();
|
|
79 |
if (sm != null) {
|
|
80 |
String fn = pathForSecurityCheck(file);
|
|
81 |
FilePermission writePermission = new FilePermission(fn, "write");
|
|
82 |
sm.checkPermission(writePermission);
|
|
83 |
filePermission = writePermission;
|
|
84 |
}
|
|
85 |
return new PathBodyHandler(file, openOptions, filePermission);
|
|
86 |
}
|
|
87 |
|
|
88 |
private PathBodyHandler(Path file,
|
|
89 |
List<OpenOption> openOptions,
|
|
90 |
FilePermission filePermission) {
|
|
91 |
this.file = file;
|
|
92 |
this.openOptions = openOptions;
|
|
93 |
this.filePermission = filePermission;
|
|
94 |
}
|
|
95 |
|
|
96 |
@Override
|
|
97 |
public BodySubscriber<Path> apply(ResponseInfo responseInfo) {
|
|
98 |
return new PathSubscriber(file, openOptions, filePermission);
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
/** With push promise Map implementation */
|
|
103 |
public static class PushPromisesHandlerWithMap<T>
|
|
104 |
implements HttpResponse.PushPromiseHandler<T>
|
|
105 |
{
|
|
106 |
private final ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap;
|
|
107 |
private final Function<HttpRequest,BodyHandler<T>> pushPromiseHandler;
|
|
108 |
|
|
109 |
public PushPromisesHandlerWithMap(Function<HttpRequest,BodyHandler<T>> pushPromiseHandler,
|
|
110 |
ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap) {
|
|
111 |
this.pushPromiseHandler = pushPromiseHandler;
|
|
112 |
this.pushPromisesMap = pushPromisesMap;
|
|
113 |
}
|
|
114 |
|
|
115 |
@Override
|
|
116 |
public void applyPushPromise(
|
|
117 |
HttpRequest initiatingRequest, HttpRequest pushRequest,
|
|
118 |
Function<BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor)
|
|
119 |
{
|
|
120 |
URI initiatingURI = initiatingRequest.uri();
|
|
121 |
URI pushRequestURI = pushRequest.uri();
|
|
122 |
if (!initiatingURI.getHost().equalsIgnoreCase(pushRequestURI.getHost()))
|
|
123 |
return;
|
|
124 |
|
|
125 |
int initiatingPort = initiatingURI.getPort();
|
|
126 |
if (initiatingPort == -1 ) {
|
|
127 |
if ("https".equalsIgnoreCase(initiatingURI.getScheme()))
|
|
128 |
initiatingPort = 443;
|
|
129 |
else
|
|
130 |
initiatingPort = 80;
|
|
131 |
}
|
|
132 |
int pushPort = pushRequestURI.getPort();
|
|
133 |
if (pushPort == -1 ) {
|
|
134 |
if ("https".equalsIgnoreCase(pushRequestURI.getScheme()))
|
|
135 |
pushPort = 443;
|
|
136 |
else
|
|
137 |
pushPort = 80;
|
|
138 |
}
|
|
139 |
if (initiatingPort != pushPort)
|
|
140 |
return;
|
|
141 |
|
|
142 |
CompletableFuture<HttpResponse<T>> cf =
|
|
143 |
acceptor.apply(pushPromiseHandler.apply(pushRequest));
|
|
144 |
pushPromisesMap.put(pushRequest, cf);
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
// Similar to Path body handler, but for file download.
|
|
149 |
public static class FileDownloadBodyHandler implements BodyHandler<Path> {
|
|
150 |
private final Path directory;
|
|
151 |
private final List<OpenOption> openOptions;
|
|
152 |
private final FilePermission[] filePermissions; // may be null
|
|
153 |
|
|
154 |
/**
|
|
155 |
* Factory for creating FileDownloadBodyHandler.
|
|
156 |
*
|
|
157 |
* Permission checks are performed here before construction of the
|
|
158 |
* FileDownloadBodyHandler. Permission checking and construction are
|
|
159 |
* deliberately and tightly co-located.
|
|
160 |
*/
|
|
161 |
public static FileDownloadBodyHandler create(Path directory,
|
|
162 |
List<OpenOption> openOptions) {
|
|
163 |
FilePermission filePermissions[] = null;
|
|
164 |
SecurityManager sm = System.getSecurityManager();
|
|
165 |
if (sm != null) {
|
|
166 |
String fn = pathForSecurityCheck(directory);
|
|
167 |
FilePermission writePermission = new FilePermission(fn, "write");
|
|
168 |
String writePathPerm = fn + File.separatorChar + "*";
|
|
169 |
FilePermission writeInDirPermission = new FilePermission(writePathPerm, "write");
|
|
170 |
sm.checkPermission(writeInDirPermission);
|
|
171 |
FilePermission readPermission = new FilePermission(fn, "read");
|
|
172 |
sm.checkPermission(readPermission);
|
|
173 |
|
|
174 |
// read permission is only needed before determine the below checks
|
|
175 |
// only write permission is required when downloading to the file
|
|
176 |
filePermissions = new FilePermission[] { writePermission, writeInDirPermission };
|
|
177 |
}
|
|
178 |
|
|
179 |
// existence, etc, checks must be after permission checks
|
|
180 |
if (Files.notExists(directory))
|
|
181 |
throw new IllegalArgumentException("non-existent directory: " + directory);
|
|
182 |
if (!Files.isDirectory(directory))
|
|
183 |
throw new IllegalArgumentException("not a directory: " + directory);
|
|
184 |
if (!Files.isWritable(directory))
|
|
185 |
throw new IllegalArgumentException("non-writable directory: " + directory);
|
|
186 |
|
|
187 |
return new FileDownloadBodyHandler(directory, openOptions, filePermissions);
|
|
188 |
|
|
189 |
}
|
|
190 |
|
|
191 |
private FileDownloadBodyHandler(Path directory,
|
|
192 |
List<OpenOption> openOptions,
|
|
193 |
FilePermission... filePermissions) {
|
|
194 |
this.directory = directory;
|
|
195 |
this.openOptions = openOptions;
|
|
196 |
this.filePermissions = filePermissions;
|
|
197 |
}
|
|
198 |
|
|
199 |
/** The "attachment" disposition-type and separator. */
|
|
200 |
static final String DISPOSITION_TYPE = "attachment;";
|
|
201 |
|
|
202 |
/** The "filename" parameter. */
|
|
203 |
static final Pattern FILENAME = Pattern.compile("filename\\s*=", CASE_INSENSITIVE);
|
|
204 |
|
|
205 |
static final List<String> PROHIBITED = List.of(".", "..", "", "~" , "|");
|
|
206 |
|
|
207 |
static final UncheckedIOException unchecked(ResponseInfo rinfo,
|
|
208 |
String msg) {
|
|
209 |
String s = String.format("%s in response [%d, %s]", msg, rinfo.statusCode(), rinfo.headers());
|
|
210 |
return new UncheckedIOException(new IOException(s));
|
|
211 |
}
|
|
212 |
|
|
213 |
@Override
|
|
214 |
public BodySubscriber<Path> apply(ResponseInfo responseInfo) {
|
|
215 |
String dispoHeader = responseInfo.headers().firstValue("Content-Disposition")
|
|
216 |
.orElseThrow(() -> unchecked(responseInfo, "No Content-Disposition header"));
|
|
217 |
|
|
218 |
if (!dispoHeader.regionMatches(true, // ignoreCase
|
|
219 |
0, DISPOSITION_TYPE,
|
|
220 |
0, DISPOSITION_TYPE.length())) {
|
|
221 |
throw unchecked(responseInfo, "Unknown Content-Disposition type");
|
|
222 |
}
|
|
223 |
|
|
224 |
Matcher matcher = FILENAME.matcher(dispoHeader);
|
|
225 |
if (!matcher.find()) {
|
|
226 |
throw unchecked(responseInfo, "Bad Content-Disposition filename parameter");
|
|
227 |
}
|
|
228 |
int n = matcher.end();
|
|
229 |
|
|
230 |
int semi = dispoHeader.substring(n).indexOf(";");
|
|
231 |
String filenameParam;
|
|
232 |
if (semi < 0) {
|
|
233 |
filenameParam = dispoHeader.substring(n);
|
|
234 |
} else {
|
|
235 |
filenameParam = dispoHeader.substring(n, n + semi);
|
|
236 |
}
|
|
237 |
|
|
238 |
// strip all but the last path segment
|
|
239 |
int x = filenameParam.lastIndexOf("/");
|
|
240 |
if (x != -1) {
|
|
241 |
filenameParam = filenameParam.substring(x+1);
|
|
242 |
}
|
|
243 |
x = filenameParam.lastIndexOf("\\");
|
|
244 |
if (x != -1) {
|
|
245 |
filenameParam = filenameParam.substring(x+1);
|
|
246 |
}
|
|
247 |
|
|
248 |
filenameParam = filenameParam.trim();
|
|
249 |
|
|
250 |
if (filenameParam.startsWith("\"")) { // quoted-string
|
|
251 |
if (!filenameParam.endsWith("\"") || filenameParam.length() == 1) {
|
|
252 |
throw unchecked(responseInfo,
|
|
253 |
"Badly quoted Content-Disposition filename parameter");
|
|
254 |
}
|
|
255 |
filenameParam = filenameParam.substring(1, filenameParam.length() -1 );
|
|
256 |
} else { // token,
|
|
257 |
if (filenameParam.contains(" ")) { // space disallowed
|
|
258 |
throw unchecked(responseInfo,
|
|
259 |
"unquoted space in Content-Disposition filename parameter");
|
|
260 |
}
|
|
261 |
}
|
|
262 |
|
|
263 |
if (PROHIBITED.contains(filenameParam)) {
|
|
264 |
throw unchecked(responseInfo,
|
|
265 |
"Prohibited Content-Disposition filename parameter:"
|
|
266 |
+ filenameParam);
|
|
267 |
}
|
|
268 |
|
|
269 |
Path file = Paths.get(directory.toString(), filenameParam);
|
|
270 |
|
|
271 |
if (!file.startsWith(directory)) {
|
|
272 |
throw unchecked(responseInfo,
|
|
273 |
"Resulting file, " + file.toString() + ", outside of given directory");
|
|
274 |
}
|
|
275 |
|
|
276 |
return new PathSubscriber(file, openOptions, filePermissions);
|
|
277 |
}
|
|
278 |
}
|
|
279 |
}
|