src/java.net.http/share/classes/jdk/internal/net/http/ResponseContent.java
author mbaesken
Thu, 28 Nov 2019 13:02:39 +0100
changeset 59323 ae2eb76c486d
parent 52902 e3398b2e1ab0
permissions -rw-r--r--
8234821: remove unused functions from libjli Reviewed-by: clanger, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
48703
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48376
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    26
package jdk.internal.net.http;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    28
import java.io.IOException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.nio.ByteBuffer;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    30
import java.util.ArrayList;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    31
import java.util.Collections;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    32
import java.util.List;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    33
import java.util.function.Consumer;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    34
import java.net.http.HttpHeaders;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    35
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    36
import jdk.internal.net.http.common.Logger;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    37
import jdk.internal.net.http.common.Utils;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    38
import static java.lang.String.format;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    41
 * Implements chunked/fixed transfer encodings of HTTP/1.1 responses.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
 * Call pushBody() to read the body (blocking). Data and errors are provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    44
 * to given Consumers. After final buffer delivered, empty optional delivered
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    46
class ResponseContent {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    47
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    48
    final HttpResponse.BodySubscriber<?> pusher;
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents: 51462
diff changeset
    49
    final long contentLength;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    50
    final HttpHeaders headers;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
    // this needs to run before we complete the body
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
    // so that connection can be returned to pool
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    53
    private final Runnable onFinished;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    54
    private final String dbgTag;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    55
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
    ResponseContent(HttpConnection connection,
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents: 51462
diff changeset
    57
                    long contentLength,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    58
                    HttpHeaders h,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    59
                    HttpResponse.BodySubscriber<?> userSubscriber,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    60
                    Runnable onFinished)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
    {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    62
        this.pusher = userSubscriber;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
        this.contentLength = contentLength;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
        this.headers = h;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    65
        this.onFinished = onFinished;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    66
        this.dbgTag = connection.dbgString() + "/ResponseContent";
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    67
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    68
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    69
    static final int LF = 10;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    70
    static final int CR = 13;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    71
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    72
    private boolean chunkedContent, chunkedContentInitialized;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    73
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    74
    boolean contentChunked() throws IOException {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    75
        if (chunkedContentInitialized) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    76
            return chunkedContent;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    77
        }
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
    78
        if (contentLength == -2) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
    79
            // HTTP/1.0 content
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
    80
            chunkedContentInitialized = true;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
    81
            chunkedContent = false;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
    82
            return chunkedContent;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
    83
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    84
        if (contentLength == -1) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    85
            String tc = headers.firstValue("Transfer-Encoding")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    86
                               .orElse("");
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52283
diff changeset
    87
            if (!tc.isEmpty()) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    88
                if (tc.equalsIgnoreCase("chunked")) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    89
                    chunkedContent = true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    90
                } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
                    throw new IOException("invalid content");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
            } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
                chunkedContent = false;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    95
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    96
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    97
        chunkedContentInitialized = true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    98
        return chunkedContent;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    99
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   100
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   101
    interface BodyParser extends Consumer<ByteBuffer> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   102
        void onSubscribe(AbstractSubscription sub);
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   103
        // A current-state message suitable for inclusion in an exception
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   104
        // detail message.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   105
        String currentStateMessage();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   106
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   107
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   108
    // Returns a parser that will take care of parsing the received byte
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   109
    // buffers and forward them to the BodySubscriber.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   110
    // When the parser is done, it will call onComplete.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   111
    // If parsing was successful, the throwable parameter will be null.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   112
    // Otherwise it will be the exception that occurred
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   113
    // Note: revisit: it might be better to use a CompletableFuture than
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   114
    //       a completion handler.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   115
    BodyParser getBodyParser(Consumer<Throwable> onComplete)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
        throws IOException {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   117
        if (contentChunked()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   118
            return new ChunkedBodyParser(onComplete);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
        } else {
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   120
            return contentLength == -2
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   121
                ? new UnknownLengthBodyParser(onComplete)
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   122
                : new FixedLengthBodyParser(contentLength, onComplete);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   123
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   124
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   125
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   126
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   127
    static enum ChunkState {READING_LENGTH, READING_DATA, DONE}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
    class ChunkedBodyParser implements BodyParser {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   129
        final ByteBuffer READMORE = Utils.EMPTY_BYTEBUFFER;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   130
        final Consumer<Throwable> onComplete;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   131
        final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   132
        final String dbgTag = ResponseContent.this.dbgTag + "/ChunkedBodyParser";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   133
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   134
        volatile Throwable closedExceptionally;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   135
        volatile int partialChunklen = 0; // partially read chunk len
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   136
        volatile int chunklen = -1;  // number of bytes in chunk
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   137
        volatile int bytesremaining;  // number of bytes in chunk left to be read incl CRLF
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
        volatile boolean cr = false;  // tryReadChunkLength has found CR
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   139
        volatile int bytesToConsume;  // number of bytes that still need to be consumed before proceeding
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   140
        volatile ChunkState state = ChunkState.READING_LENGTH; // current state
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   141
        volatile AbstractSubscription sub;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   142
        ChunkedBodyParser(Consumer<Throwable> onComplete) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   143
            this.onComplete = onComplete;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   144
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   145
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   146
        String dbgString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   147
            return dbgTag;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   148
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   149
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   150
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   151
        public void onSubscribe(AbstractSubscription sub) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   152
            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   153
                debug.log("onSubscribe: "  + pusher.getClass().getName());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   154
            pusher.onSubscribe(this.sub = sub);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   155
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   156
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   157
        @Override
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   158
        public String currentStateMessage() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   159
            return format("chunked transfer encoding, state: %s", state);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   160
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   161
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   162
        @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   163
        public void accept(ByteBuffer b) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   164
            if (closedExceptionally != null) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   165
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   166
                    debug.log("already closed: " + closedExceptionally);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   167
                return;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   168
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   169
            boolean completed = false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   170
            try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   171
                List<ByteBuffer> out = new ArrayList<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   172
                do {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   173
                    if (tryPushOneHunk(b, out))  {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   174
                        // We're done! (true if the final chunk was parsed).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   175
                        if (!out.isEmpty()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   176
                            // push what we have and complete
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   177
                            // only reduce demand if we actually push something.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   178
                            // we would not have come here if there was no
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   179
                            // demand.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   180
                            boolean hasDemand = sub.demand().tryDecrement();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   181
                            assert hasDemand;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   182
                            pusher.onNext(Collections.unmodifiableList(out));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   183
                            if (debug.on()) debug.log("Chunks sent");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   184
                        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   185
                        if (debug.on()) debug.log("done!");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   186
                        assert closedExceptionally == null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   187
                        assert state == ChunkState.DONE;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   188
                        onFinished.run();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   189
                        pusher.onComplete();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   190
                        if (debug.on()) debug.log("subscriber completed");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   191
                        completed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   192
                        onComplete.accept(closedExceptionally); // should be null
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   193
                        break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   194
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   195
                    // the buffer may contain several hunks, and therefore
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   196
                    // we must loop while it's not exhausted.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   197
                } while (b.hasRemaining());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   198
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   199
                if (!completed && !out.isEmpty()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   200
                    // push what we have.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   201
                    // only reduce demand if we actually push something.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   202
                    // we would not have come here if there was no
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   203
                    // demand.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   204
                    boolean hasDemand = sub.demand().tryDecrement();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   205
                    assert hasDemand;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   206
                    pusher.onNext(Collections.unmodifiableList(out));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   207
                    if (debug.on()) debug.log("Chunk sent");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   208
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   209
                assert state == ChunkState.DONE || !b.hasRemaining();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   210
            } catch(Throwable t) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   211
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   212
                    debug.log("Error while processing buffer: %s", (Object)t );
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   213
                closedExceptionally = t;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   214
                if (!completed) onComplete.accept(t);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   215
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   216
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   217
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   218
        // reads and returns chunklen. Position of chunkbuf is first byte
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   219
        // of chunk on return. chunklen includes the CR LF at end of chunk
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   220
        // returns -1 if needs more bytes
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   221
        private int tryReadChunkLen(ByteBuffer chunkbuf) throws IOException {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   222
            assert state == ChunkState.READING_LENGTH;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   223
            while (chunkbuf.hasRemaining()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   224
                int c = chunkbuf.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   225
                if (cr) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   226
                    if (c == LF) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   227
                        return partialChunklen;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   228
                    } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   229
                        throw new IOException("invalid chunk header");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   230
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   231
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   232
                if (c == CR) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   233
                    cr = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   234
                } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   235
                    int digit = toDigit(c);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   236
                    partialChunklen = partialChunklen * 16 + digit;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   237
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   238
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   239
            return -1;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   240
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   241
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   242
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   243
        // try to consume as many bytes as specified by bytesToConsume.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   244
        // returns the number of bytes that still need to be consumed.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   245
        // In practice this method is only called to consume one CRLF pair
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   246
        // with bytesToConsume set to 2, so it will only return 0 (if completed),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   247
        // 1, or 2 (if chunkbuf doesn't have the 2 chars).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   248
        private int tryConsumeBytes(ByteBuffer chunkbuf) throws IOException {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   249
            int n = bytesToConsume;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   250
            if (n > 0) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   251
                int e = Math.min(chunkbuf.remaining(), n);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   252
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   253
                // verifies some assertions
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   254
                // this methods is called only to consume CRLF
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   255
                if (Utils.ASSERTIONSENABLED) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   256
                    assert n <= 2 && e <= 2;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   257
                    ByteBuffer tmp = chunkbuf.slice();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   258
                    // if n == 2 assert that we will first consume CR
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   259
                    assert (n == 2 && e > 0) ? tmp.get() == CR : true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   260
                    // if n == 1 || n == 2 && e == 2 assert that we then consume LF
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   261
                    assert (n == 1 || e == 2) ? tmp.get() == LF : true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   262
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   263
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   264
                chunkbuf.position(chunkbuf.position() + e);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   265
                n -= e;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   266
                bytesToConsume = n;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   267
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   268
            assert n >= 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   269
            return n;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   270
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   271
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   272
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   273
         * Returns a ByteBuffer containing chunk of data or a "hunk" of data
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   274
         * (a chunk of a chunk if the chunk size is larger than our ByteBuffers).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   275
         * If the given chunk does not have enough data this method return
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   276
         * an empty ByteBuffer (READMORE).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   277
         * If we encounter the final chunk (an empty chunk) this method
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   278
         * returns null.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   279
         */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   280
        ByteBuffer tryReadOneHunk(ByteBuffer chunk) throws IOException {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   281
            int unfulfilled = bytesremaining;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   282
            int toconsume = bytesToConsume;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   283
            ChunkState st = state;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   284
            if (st == ChunkState.READING_LENGTH && chunklen == -1) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   285
                if (debug.on()) debug.log(() ->  "Trying to read chunk len"
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   286
                        + " (remaining in buffer:"+chunk.remaining()+")");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   287
                int clen = chunklen = tryReadChunkLen(chunk);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   288
                if (clen == -1) return READMORE;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   289
                if (debug.on()) debug.log("Got chunk len %d", clen);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   290
                cr = false; partialChunklen = 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   291
                unfulfilled = bytesremaining =  clen;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   292
                if (clen == 0) toconsume = bytesToConsume = 2; // that was the last chunk
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   293
                else st = state = ChunkState.READING_DATA; // read the data
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   294
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   295
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   296
            if (toconsume > 0) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   297
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   298
                    debug.log("Trying to consume bytes: %d (remaining in buffer: %s)",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   299
                              toconsume, chunk.remaining());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   300
                if (tryConsumeBytes(chunk) > 0) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   301
                    return READMORE;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   302
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   303
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   304
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   305
            toconsume = bytesToConsume;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   306
            assert toconsume == 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   307
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   308
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   309
            if (st == ChunkState.READING_LENGTH) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   310
                // we will come here only if chunklen was 0, after having
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   311
                // consumed the trailing CRLF
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   312
                int clen = chunklen;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   313
                assert clen == 0;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   314
                if (debug.on()) debug.log("No more chunks: %d", clen);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   315
                // the DONE state is not really needed but it helps with
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   316
                // assertions...
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   317
                state = ChunkState.DONE;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   318
                return null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   319
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   320
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   321
            int clen = chunklen;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   322
            assert clen > 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   323
            assert st == ChunkState.READING_DATA;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   324
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   325
            ByteBuffer returnBuffer = READMORE; // May be a hunk or a chunk
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   326
            if (unfulfilled > 0) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   327
                int bytesread = chunk.remaining();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   328
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   329
                    debug.log("Reading chunk: available %d, needed %d",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   330
                              bytesread, unfulfilled);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   331
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   332
                int bytes2return = Math.min(bytesread, unfulfilled);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   333
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   334
                    debug.log( "Returning chunk bytes: %d", bytes2return);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   335
                returnBuffer = Utils.sliceWithLimitedCapacity(chunk, bytes2return).asReadOnlyBuffer();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   336
                unfulfilled = bytesremaining -= bytes2return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   337
                if (unfulfilled == 0) bytesToConsume = 2;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   338
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   339
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   340
            assert unfulfilled >= 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   341
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   342
            if (unfulfilled == 0) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   343
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   344
                    debug.log("No more bytes to read - %d yet to consume.",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   345
                              unfulfilled);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   346
                // check whether the trailing CRLF is consumed, try to
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   347
                // consume it if not. If tryConsumeBytes needs more bytes
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   348
                // then we will come back here later - skipping the block
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   349
                // that reads data because remaining==0, and finding
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   350
                // that the two bytes are now consumed.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   351
                if (tryConsumeBytes(chunk) == 0) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   352
                    // we're done for this chunk! reset all states and
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   353
                    // prepare to read the next chunk.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   354
                    chunklen = -1;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   355
                    partialChunklen = 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   356
                    cr = false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   357
                    state = ChunkState.READING_LENGTH;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   358
                    if (debug.on()) debug.log("Ready to read next chunk");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   359
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   360
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   361
            if (returnBuffer == READMORE) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   362
                if (debug.on()) debug.log("Need more data");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   363
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   364
            return returnBuffer;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   365
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   366
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   367
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   368
        // Attempt to parse and push one hunk from the buffer.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   369
        // Returns true if the final chunk was parsed.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   370
        // Returns false if we need to push more chunks.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   371
        private boolean tryPushOneHunk(ByteBuffer b, List<ByteBuffer> out)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   372
                throws IOException {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   373
            assert state != ChunkState.DONE;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   374
            ByteBuffer b1 = tryReadOneHunk(b);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   375
            if (b1 != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   376
                //assert b1.hasRemaining() || b1 == READMORE;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   377
                if (b1.hasRemaining()) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   378
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   379
                        debug.log("Sending chunk to consumer (%d)", b1.remaining());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   380
                    out.add(b1);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   381
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   382
                return false; // we haven't parsed the final chunk yet.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   383
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
                return true; // we're done! the final chunk was parsed.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   385
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   386
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   387
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   388
        private int toDigit(int b) throws IOException {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   389
            if (b >= 0x30 && b <= 0x39) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   390
                return b - 0x30;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   391
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   392
            if (b >= 0x41 && b <= 0x46) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   393
                return b - 0x41 + 10;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   394
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   395
            if (b >= 0x61 && b <= 0x66) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   396
                return b - 0x61 + 10;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   397
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   398
            throw new IOException("Invalid chunk header byte " + b);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   399
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   400
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   401
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   402
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   403
    class UnknownLengthBodyParser implements BodyParser {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   404
        final Consumer<Throwable> onComplete;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   405
        final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   406
        final String dbgTag = ResponseContent.this.dbgTag + "/UnknownLengthBodyParser";
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   407
        volatile Throwable closedExceptionally;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   408
        volatile AbstractSubscription sub;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   409
        volatile int breceived = 0;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   410
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   411
        UnknownLengthBodyParser(Consumer<Throwable> onComplete) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   412
            this.onComplete = onComplete;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   413
        }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   414
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   415
        String dbgString() {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   416
            return dbgTag;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   417
        }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   418
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   419
        @Override
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   420
        public void onSubscribe(AbstractSubscription sub) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   421
            if (debug.on())
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   422
                debug.log("onSubscribe: " + pusher.getClass().getName());
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   423
            pusher.onSubscribe(this.sub = sub);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   424
        }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   425
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   426
        @Override
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   427
        public String currentStateMessage() {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   428
            return format("http1_0 content, bytes received: %d", breceived);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   429
        }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   430
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   431
        @Override
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   432
        public void accept(ByteBuffer b) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   433
            if (closedExceptionally != null) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   434
                if (debug.on())
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   435
                    debug.log("already closed: " + closedExceptionally);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   436
                return;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   437
            }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   438
            boolean completed = false;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   439
            try {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   440
                if (debug.on())
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   441
                    debug.log("Parser got %d bytes ", b.remaining());
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   442
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   443
                if (b.hasRemaining()) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   444
                    // only reduce demand if we actually push something.
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   445
                    // we would not have come here if there was no
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   446
                    // demand.
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   447
                    boolean hasDemand = sub.demand().tryDecrement();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   448
                    assert hasDemand;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   449
                    breceived += b.remaining();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   450
                    pusher.onNext(List.of(b.asReadOnlyBuffer()));
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   451
                }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   452
            } catch (Throwable t) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   453
                if (debug.on()) debug.log("Unexpected exception", t);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   454
                closedExceptionally = t;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   455
                if (!completed) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   456
                    onComplete.accept(t);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   457
                }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   458
            }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   459
        }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   460
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   461
        /**
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   462
         * Must be called externally when connection has closed
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   463
         * and therefore no more bytes can be read
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   464
         */
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   465
        public void complete() {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   466
            // We're done! All data has been received.
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   467
            if (debug.on())
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   468
                debug.log("Parser got all expected bytes: completing");
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   469
            assert closedExceptionally == null;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   470
            onFinished.run();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   471
            pusher.onComplete();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   472
            onComplete.accept(closedExceptionally); // should be null
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   473
        }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   474
    }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents: 50681
diff changeset
   475
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   476
    class FixedLengthBodyParser implements BodyParser {
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents: 51462
diff changeset
   477
        final long contentLength;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   478
        final Consumer<Throwable> onComplete;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   479
        final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   480
        final String dbgTag = ResponseContent.this.dbgTag + "/FixedLengthBodyParser";
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents: 51462
diff changeset
   481
        volatile long remaining;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   482
        volatile Throwable closedExceptionally;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   483
        volatile AbstractSubscription sub;
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents: 51462
diff changeset
   484
        FixedLengthBodyParser(long contentLength, Consumer<Throwable> onComplete) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   485
            this.contentLength = this.remaining = contentLength;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   486
            this.onComplete = onComplete;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   487
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   488
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   489
        String dbgString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   490
            return dbgTag;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   491
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   492
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   493
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   494
        public void onSubscribe(AbstractSubscription sub) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   495
            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   496
                debug.log("length=" + contentLength +", onSubscribe: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   497
                           + pusher.getClass().getName());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   498
            pusher.onSubscribe(this.sub = sub);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   499
            try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   500
                if (contentLength == 0) {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48083
diff changeset
   501
                    onFinished.run();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   502
                    pusher.onComplete();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   503
                    onComplete.accept(null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   504
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   505
            } catch (Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   506
                closedExceptionally = t;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   507
                try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   508
                    pusher.onError(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   509
                } finally {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   510
                    onComplete.accept(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   511
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   512
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   513
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   514
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   515
        @Override
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   516
        public String currentStateMessage() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   517
            return format("fixed content-length: %d, bytes received: %d",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   518
                          contentLength, contentLength - remaining);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   519
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   520
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   521
        @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   522
        public void accept(ByteBuffer b) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   523
            if (closedExceptionally != null) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   524
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   525
                    debug.log("already closed: " + closedExceptionally);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   526
                return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   527
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   528
            boolean completed = false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   529
            try {
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents: 51462
diff changeset
   530
                long unfulfilled = remaining;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   531
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   532
                    debug.log("Parser got %d bytes (%d remaining / %d)",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   533
                              b.remaining(), unfulfilled, contentLength);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   534
                assert unfulfilled != 0 || contentLength == 0 || b.remaining() == 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   535
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   536
                if (unfulfilled == 0 && contentLength > 0) return;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   537
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   538
                if (b.hasRemaining() && unfulfilled > 0) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   539
                    // only reduce demand if we actually push something.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   540
                    // we would not have come here if there was no
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   541
                    // demand.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   542
                    boolean hasDemand = sub.demand().tryDecrement();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   543
                    assert hasDemand;
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents: 51462
diff changeset
   544
                    int amount = (int)Math.min(b.remaining(), unfulfilled); // safe cast
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   545
                    unfulfilled = remaining -= amount;
48703
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48376
diff changeset
   546
                    ByteBuffer buffer = Utils.sliceWithLimitedCapacity(b, amount);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   547
                    pusher.onNext(List.of(buffer.asReadOnlyBuffer()));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   548
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   549
                if (unfulfilled == 0) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   550
                    // We're done! All data has been received.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   551
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   552
                        debug.log("Parser got all expected bytes: completing");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   553
                    assert closedExceptionally == null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   554
                    onFinished.run();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   555
                    pusher.onComplete();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   556
                    completed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   557
                    onComplete.accept(closedExceptionally); // should be null
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   558
                } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   559
                    assert b.remaining() == 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   560
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   561
            } catch (Throwable t) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   562
                if (debug.on()) debug.log("Unexpected exception", t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   563
                closedExceptionally = t;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   564
                if (!completed) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   565
                    onComplete.accept(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   566
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   567
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   568
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   569
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   570
}