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