src/java.net.http/share/classes/jdk/internal/net/http/ResponseBodyHandlers.java
author chegar
Sat, 24 Feb 2018 11:27:11 +0000
branchhttp-client-branch
changeset 56167 96fa4f49a9ff
parent 56138 4f92b988600e
child 56257 82a9340bdda6
permissions -rw-r--r--
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
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;
56138
4f92b988600e http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents: 56132
diff changeset
    34
import java.nio.file.StandardOpenOption;
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
     */
56138
4f92b988600e http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents: 56132
diff changeset
    56
    public static class PathBodyHandler implements BodyHandler<Path>{
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    57
        private final Path file;
56138
4f92b988600e http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents: 56132
diff changeset
    58
        private final List<OpenOption> openOptions;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    59
56138
4f92b988600e http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents: 56132
diff changeset
    60
        public PathBodyHandler(Path file, List<OpenOption> openOptions) {
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    61
            this.file = file;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    62
            this.openOptions = openOptions;
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
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    65
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    66
        public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) {
56138
4f92b988600e http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents: 56132
diff changeset
    67
            return new PathSubscriber(file, openOptions);
56079
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
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    70
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    71
    /** With push promise Map implementation */
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    72
    public static class PushPromisesHandlerWithMap<T>
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    73
        implements HttpResponse.PushPromiseHandler<T>
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    74
    {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    75
        private final ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    76
        private final Function<HttpRequest,BodyHandler<T>> pushPromiseHandler;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    77
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    78
        public PushPromisesHandlerWithMap(Function<HttpRequest,BodyHandler<T>> pushPromiseHandler,
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    79
                                          ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    80
            this.pushPromiseHandler = pushPromiseHandler;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    81
            this.pushPromisesMap = pushPromisesMap;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    82
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    83
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    84
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    85
        public void applyPushPromise(
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    86
                HttpRequest initiatingRequest, HttpRequest pushRequest,
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    87
                Function<BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor)
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
            URI initiatingURI = initiatingRequest.uri();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    90
            URI pushRequestURI = pushRequest.uri();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    91
            if (!initiatingURI.getHost().equalsIgnoreCase(pushRequestURI.getHost()))
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    92
                return;
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
            int initiatingPort = initiatingURI.getPort();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    95
            if (initiatingPort == -1 ) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    96
                if ("https".equalsIgnoreCase(initiatingURI.getScheme()))
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    97
                    initiatingPort = 443;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    98
                else
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
    99
                    initiatingPort = 80;
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
            int pushPort = pushRequestURI.getPort();
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   102
            if (pushPort == -1 ) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   103
                if ("https".equalsIgnoreCase(pushRequestURI.getScheme()))
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   104
                    pushPort = 443;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   105
                else
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   106
                    pushPort = 80;
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
            if (initiatingPort != pushPort)
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   109
                return;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   110
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   111
            CompletableFuture<HttpResponse<T>> cf =
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   112
                    acceptor.apply(pushPromiseHandler.apply(pushRequest));
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   113
            pushPromisesMap.put(pushRequest, cf);
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   114
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   115
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   116
56138
4f92b988600e http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents: 56132
diff changeset
   117
    // 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
   118
    public static class FileDownloadBodyHandler implements BodyHandler<Path> {
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   119
        private final Path directory;
56138
4f92b988600e http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents: 56132
diff changeset
   120
        private final List<OpenOption> openOptions;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   121
56138
4f92b988600e http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents: 56132
diff changeset
   122
        public FileDownloadBodyHandler(Path directory, List<OpenOption> openOptions) {
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   123
            this.directory = directory;
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   124
            this.openOptions = openOptions;
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
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   127
        /** The "attachment" disposition-type and separator. */
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   128
        static final String DISPOSITION_TYPE = "attachment;";
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   129
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   130
        /** The "filename" parameter. */
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   131
        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
   132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   133
        static final List<String> PROHIBITED = List.of(".", "..", "", "~" , "|");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   134
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   135
        static final UncheckedIOException unchecked(int code,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   136
                                                    HttpHeaders headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   137
                                                    String msg) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   138
            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
   139
            return new UncheckedIOException(new IOException(s));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   140
        }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   141
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   142
        @Override
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   143
        public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) {
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   144
            String dispoHeader = headers.firstValue("Content-Disposition")
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   145
                    .orElseThrow(() -> unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   146
                            "No Content-Disposition header"));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   147
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   148
            if (!dispoHeader.regionMatches(true, // ignoreCase
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   149
                                           0, DISPOSITION_TYPE,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   150
                                           0, DISPOSITION_TYPE.length())) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   151
                throw unchecked(statusCode, headers, "Unknown Content-Disposition type");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   152
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   153
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   154
            Matcher matcher = FILENAME.matcher(dispoHeader);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   155
            if (!matcher.find()) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   156
                throw unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   157
                          "Bad Content-Disposition filename parameter");
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   158
            }
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   159
            int n = matcher.end();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   160
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   161
            int semi = dispoHeader.substring(n).indexOf(";");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   162
            String filenameParam;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   163
            if (semi < 0) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   164
                filenameParam = dispoHeader.substring(n);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   165
            } else {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   166
                filenameParam = dispoHeader.substring(n, n + semi);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   167
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   168
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   169
            // strip all but the last path segment
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   170
            int x = filenameParam.lastIndexOf("/");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   171
            if (x != -1) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   172
                filenameParam = filenameParam.substring(x+1);
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   173
            }
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   174
            x = filenameParam.lastIndexOf("\\");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   175
            if (x != -1) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   176
                filenameParam = filenameParam.substring(x+1);
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   177
            }
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   178
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   179
            filenameParam = filenameParam.trim();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   180
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   181
            if (filenameParam.startsWith("\"")) {  // quoted-string
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   182
                if (!filenameParam.endsWith("\"") || filenameParam.length() == 1) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   183
                    throw unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   184
                            "Badly quoted Content-Disposition filename parameter");
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
                filenameParam = filenameParam.substring(1, filenameParam.length() -1 );
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   187
            } else {  // token,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   188
                if (filenameParam.contains(" ")) {  // space disallowed
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   189
                    throw unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   190
                            "unquoted space in Content-Disposition filename parameter");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   191
                }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   192
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   193
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   194
            if (PROHIBITED.contains(filenameParam)) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   195
                throw unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   196
                        "Prohibited Content-Disposition filename parameter:"
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   197
                                + filenameParam);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   198
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   199
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   200
            Path file = Paths.get(directory.toString(), filenameParam);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   201
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   202
            if (!file.startsWith(directory)) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   203
                throw unchecked(statusCode, headers,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   204
                        "Resulting file, " + file.toString() + ", outside of given directory");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents: 56092
diff changeset
   205
            }
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   206
56138
4f92b988600e http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents: 56132
diff changeset
   207
            return new PathSubscriber(file, openOptions);
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   208
        }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   209
    }
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents:
diff changeset
   210
}