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