src/java.net.http/share/classes/jdk/internal/net/http/ResponseBodyHandlers.java
author chegar
Thu, 15 Feb 2018 14:10:27 +0000
branchhttp-client-branch
changeset 56132 c8a1eccbc719
parent 56092 fd85b2bf2b0d
child 56138 4f92b988600e
permissions -rw-r--r--
http-client-branch: fix asFileDownload handler and add test
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
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    28
import java.io.IOException;
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
    29
import java.io.UncheckedIOException;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    30
import java.net.URI;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    31
import java.nio.file.OpenOption;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    32
import java.nio.file.Path;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    33
import java.nio.file.Paths;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    34
import java.security.AccessControlContext;
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
    35
import java.util.List;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    36
import java.util.concurrent.CompletableFuture;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    37
import java.util.concurrent.ConcurrentMap;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    38
import java.util.function.Function;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    39
import java.net.http.HttpHeaders;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    40
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    41
import java.net.http.HttpResponse;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    42
import java.net.http.HttpResponse.BodyHandler;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56080
diff changeset
    43
import java.net.http.HttpResponse.BodySubscriber;
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
    44
import java.util.regex.Matcher;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
    45
import java.util.regex.Pattern;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    46
import jdk.internal.net.http.ResponseSubscribers.PathSubscriber;
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
    47
import static java.util.regex.Pattern.CASE_INSENSITIVE;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    48
56080
64846522c0d5 http-client-branch: add several private <init> methods
chegar
parents: 56079
diff changeset
    49
public final class ResponseBodyHandlers {
64846522c0d5 http-client-branch: add several private <init> methods
chegar
parents: 56079
diff changeset
    50
64846522c0d5 http-client-branch: add several private <init> methods
chegar
parents: 56079
diff changeset
    51
    private ResponseBodyHandlers() { }
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    52
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    53
    /**
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    54
     * A Path body handler.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    55
     *
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    56
     * Note: Exists mainly too allow setting of the senders ACC post creation of
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    57
     * the handler.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    58
     */
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    59
    public static class PathBodyHandler implements UntrustedBodyHandler<Path> {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    60
        private final Path file;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    61
        private final OpenOption[]openOptions;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    62
        private volatile AccessControlContext acc;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    63
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    64
        public PathBodyHandler(Path file, OpenOption... openOptions) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    65
            this.file = file;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    66
            this.openOptions = openOptions;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    67
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    68
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    69
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    70
        public void setAccessControlContext(AccessControlContext acc) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    71
            this.acc = acc;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    72
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    73
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    74
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    75
        public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    76
            PathSubscriber bs = (PathSubscriber) asFileImpl(file, openOptions);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    77
            bs.setAccessControlContext(acc);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    78
            return bs;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    79
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    80
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    81
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    82
    /** With push promise Map implementation */
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    83
    public static class PushPromisesHandlerWithMap<T>
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    84
        implements HttpResponse.PushPromiseHandler<T>
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    85
    {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    86
        private final ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    87
        private final Function<HttpRequest,BodyHandler<T>> pushPromiseHandler;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    88
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    89
        public PushPromisesHandlerWithMap(Function<HttpRequest,BodyHandler<T>> pushPromiseHandler,
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    90
                                          ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    91
            this.pushPromiseHandler = pushPromiseHandler;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    92
            this.pushPromisesMap = pushPromisesMap;
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 void applyPushPromise(
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    97
                HttpRequest initiatingRequest, HttpRequest pushRequest,
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    98
                Function<BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor)
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
            URI initiatingURI = initiatingRequest.uri();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   101
            URI pushRequestURI = pushRequest.uri();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   102
            if (!initiatingURI.getHost().equalsIgnoreCase(pushRequestURI.getHost()))
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   103
                return;
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
            int initiatingPort = initiatingURI.getPort();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   106
            if (initiatingPort == -1 ) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   107
                if ("https".equalsIgnoreCase(initiatingURI.getScheme()))
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   108
                    initiatingPort = 443;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   109
                else
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   110
                    initiatingPort = 80;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   111
            }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   112
            int pushPort = pushRequestURI.getPort();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   113
            if (pushPort == -1 ) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   114
                if ("https".equalsIgnoreCase(pushRequestURI.getScheme()))
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   115
                    pushPort = 443;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   116
                else
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   117
                    pushPort = 80;
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
            if (initiatingPort != pushPort)
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   120
                return;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   121
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   122
            CompletableFuture<HttpResponse<T>> cf =
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   123
                    acceptor.apply(pushPromiseHandler.apply(pushRequest));
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   124
            pushPromisesMap.put(pushRequest, cf);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   125
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   126
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   127
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   128
    // Similar to Path body handler, but for file download. Supports setting ACC.
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   129
    public static class FileDownloadBodyHandler implements UntrustedBodyHandler<Path> {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   130
        private final Path directory;
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   131
        private final OpenOption[] openOptions;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   132
        private volatile AccessControlContext acc;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   133
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   134
        public FileDownloadBodyHandler(Path directory, OpenOption... openOptions) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   135
            this.directory = directory;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   136
            this.openOptions = openOptions;
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
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   139
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   140
        public void setAccessControlContext(AccessControlContext acc) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   141
            this.acc = acc;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   142
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   143
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   144
        /** The "attachment" disposition-type and separator. */
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   145
        static final String DISPOSITION_TYPE = "attachment;";
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   146
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   147
        /** The "filename" parameter. */
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   148
        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
   149
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   150
        static final List<String> PROHIBITED = List.of(".", "..", "", "~" , "|");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   151
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   152
        static final UncheckedIOException unchecked(int code,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   153
                                                    HttpHeaders headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   154
                                                    String msg) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   155
            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
   156
            return new UncheckedIOException(new IOException(s));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   157
        }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   158
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   159
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   160
        public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   161
            String dispoHeader = headers.firstValue("Content-Disposition")
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   162
                    .orElseThrow(() -> unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   163
                            "No Content-Disposition header"));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   164
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   165
            if (!dispoHeader.regionMatches(true, // ignoreCase
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   166
                                           0, DISPOSITION_TYPE,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   167
                                           0, DISPOSITION_TYPE.length())) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   168
                throw unchecked(statusCode, headers, "Unknown Content-Disposition type");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   169
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   170
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   171
            Matcher matcher = FILENAME.matcher(dispoHeader);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   172
            if (!matcher.find()) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   173
                throw unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   174
                          "Bad Content-Disposition filename parameter");
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   175
            }
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   176
            int n = matcher.end();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   177
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   178
            int semi = dispoHeader.substring(n).indexOf(";");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   179
            String filenameParam;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   180
            if (semi < 0) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   181
                filenameParam = dispoHeader.substring(n);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   182
            } else {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   183
                filenameParam = dispoHeader.substring(n, n + semi);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   184
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   185
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   186
            // strip all but the last path segment
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   187
            int x = filenameParam.lastIndexOf("/");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   188
            if (x != -1) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   189
                filenameParam = filenameParam.substring(x+1);
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   190
            }
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   191
            x = filenameParam.lastIndexOf("\\");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   192
            if (x != -1) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   193
                filenameParam = filenameParam.substring(x+1);
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   194
            }
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   195
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   196
            filenameParam = filenameParam.trim();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   197
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   198
            if (filenameParam.startsWith("\"")) {  // quoted-string
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   199
                if (!filenameParam.endsWith("\"") || filenameParam.length() == 1) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   200
                    throw unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   201
                            "Badly quoted Content-Disposition filename parameter");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   202
                }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   203
                filenameParam = filenameParam.substring(1, filenameParam.length() -1 );
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   204
            } else {  // token,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   205
                if (filenameParam.contains(" ")) {  // space disallowed
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   206
                    throw unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   207
                            "unquoted space in Content-Disposition filename parameter");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   208
                }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   209
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   210
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   211
            if (PROHIBITED.contains(filenameParam)) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   212
                throw unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   213
                        "Prohibited Content-Disposition filename parameter:"
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   214
                                + filenameParam);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   215
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   216
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   217
            Path file = Paths.get(directory.toString(), filenameParam);
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 (!file.startsWith(directory)) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   220
                throw unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   221
                        "Resulting file, " + file.toString() + ", outside of given directory");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   222
            }
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   223
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   224
            PathSubscriber bs = (PathSubscriber)asFileImpl(file, openOptions);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   225
            bs.setAccessControlContext(acc);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   226
            return bs;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   227
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   228
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   229
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   230
    // no security check
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   231
    private static BodySubscriber<Path> asFileImpl(Path file, OpenOption... openOptions) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   232
        return new ResponseSubscribers.PathSubscriber(file, openOptions);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   233
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   234
}