test/jdk/java/net/httpclient/LargeResponseContent.java
author aeubanks
Wed, 27 Mar 2019 09:06:43 -0700
changeset 54314 46cf212cdcca
parent 52283 ef0fed0a3953
permissions -rw-r--r--
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
     1
/*
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
     4
 *
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
     8
 *
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    13
 * accompanied this code).
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    14
 *
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    18
 *
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    21
 * questions.
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    22
 */
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    23
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    24
import java.io.IOException;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    25
import java.io.InputStream;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    26
import java.io.OutputStream;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    27
import java.net.InetAddress;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    28
import java.net.InetSocketAddress;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    29
import java.net.ServerSocket;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    30
import java.net.Socket;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    31
import java.net.URI;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    32
import java.net.http.HttpClient;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    33
import java.net.http.HttpHeaders;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    34
import java.net.http.HttpRequest;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    35
import java.net.http.HttpResponse;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    36
import java.nio.ByteBuffer;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    37
import java.nio.charset.StandardCharsets;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    38
import java.util.List;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    39
import java.util.concurrent.CompletableFuture;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    40
import java.util.concurrent.CompletionStage;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    41
import java.util.concurrent.Flow;
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 52283
diff changeset
    42
import jdk.test.lib.net.URIBuilder;
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    43
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    44
/**
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    45
 * @test
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    46
 * @bug 8212926
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 52283
diff changeset
    47
 * @library /test/lib
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    48
 * @summary Basic tests for response timeouts
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    49
 * @run main/othervm LargeResponseContent
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    50
 */
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    51
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    52
public class LargeResponseContent {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    53
    final ServerSocket server;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    54
    final int port;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    55
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    56
    public LargeResponseContent() throws Exception {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    57
        server = new ServerSocket(0, 10, InetAddress.getLoopbackAddress());
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    58
        Thread serverThread = new Thread(this::handleConnection);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    59
        serverThread.setDaemon(false);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    60
        port = server.getLocalPort();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    61
        serverThread.start();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    62
    }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    63
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    64
    void runClient() throws IOException, InterruptedException {
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 52283
diff changeset
    65
        URI uri = URIBuilder.newBuilder()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 52283
diff changeset
    66
            .scheme("http")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 52283
diff changeset
    67
            .loopback()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 52283
diff changeset
    68
            .port(port)
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 52283
diff changeset
    69
            .path("/foo")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 52283
diff changeset
    70
            .buildUnchecked();
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 52283
diff changeset
    71
        System.out.println("URI: " + uri);
52283
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    72
        HttpClient client = HttpClient.newHttpClient();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    73
        HttpRequest request = HttpRequest.newBuilder(uri)
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    74
                .GET()
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    75
                .build();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    76
        HttpResponse<Long> response = client.send(request, new ClientHandler());
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    77
        System.out.println("Response code = " + response.statusCode());
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    78
        long blen = response.body();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    79
        if (blen != CONTENT_LEN)
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    80
            throw new RuntimeException("wrong content length");
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    81
    }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    82
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    83
    public static void main(String[] args) throws Exception {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    84
        System.out.println ("CONTENT_LEN = " + CONTENT_LEN);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    85
        System.out.println ("CLEN_STR = " + CLEN_STR);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    86
        LargeResponseContent test = new LargeResponseContent();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    87
        test.runClient();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    88
    }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    89
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    90
    static class ClientHandler implements HttpResponse.BodyHandler<Long> {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    91
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    92
        @Override
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    93
        public HttpResponse.BodySubscriber<Long> apply(HttpResponse.ResponseInfo responseInfo) {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    94
            HttpHeaders headers = responseInfo.headers();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    95
            headers.firstValue("content-length");
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    96
            long  clen = headers.firstValueAsLong("content-length").orElse(-1);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    97
            if (clen != CONTENT_LEN)
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    98
                return new Subscriber(new RuntimeException("Wrong content length received"));
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
    99
            return new Subscriber(null);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   100
        }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   101
    }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   102
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   103
    static class Subscriber implements HttpResponse.BodySubscriber<Long> {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   104
        final CompletableFuture<Long> cf = new CompletableFuture<>();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   105
        volatile Flow.Subscription subscription;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   106
        volatile long counter = 0;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   107
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   108
        Subscriber(Throwable t) {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   109
            if (t != null)
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   110
                cf.completeExceptionally(t);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   111
        }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   112
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   113
        @Override
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   114
        public CompletionStage<Long> getBody() {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   115
            return cf;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   116
        }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   117
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   118
        @Override
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   119
        public void onSubscribe(Flow.Subscription subscription) {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   120
            this.subscription = subscription;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   121
            subscription.request(Long.MAX_VALUE);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   122
        }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   123
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   124
        @Override
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   125
        public void onNext(List<ByteBuffer> item) {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   126
            long v = 0;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   127
            for (ByteBuffer b : item)
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   128
                v+= b.remaining();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   129
            counter += v;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   130
        }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   131
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   132
        @Override
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   133
        public void onError(Throwable throwable) {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   134
            throwable.printStackTrace();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   135
        }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   136
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   137
        @Override
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   138
        public void onComplete() {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   139
            cf.complete(counter);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   140
        }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   141
    }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   142
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   143
    static final long CONTENT_LEN = Integer.MAX_VALUE + 1000L;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   144
    static final String CLEN_STR = Long.valueOf(CONTENT_LEN).toString();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   145
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   146
    static String RESPONSE = "HTTP/1.1 200 OK\r\n" +
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   147
            "Content-length: " + CLEN_STR + "\r\n" +
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   148
            "\r\n";
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   149
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   150
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   151
    void readHeaders(InputStream is) throws IOException {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   152
        String s = "";
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   153
        byte[] buf = new byte[128];
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   154
        while (!s.endsWith("\r\n\r\n")) {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   155
            int c = is.read(buf);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   156
            String f = new String(buf, 0, c, StandardCharsets.ISO_8859_1);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   157
            s = s + f;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   158
        }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   159
    }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   160
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   161
    public void handleConnection() {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   162
        long remaining = CONTENT_LEN;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   163
        try {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   164
            Socket socket = server.accept();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   165
            InputStream is = socket.getInputStream();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   166
            readHeaders(is); // read first byte
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   167
            OutputStream os = socket.getOutputStream();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   168
            os.write(RESPONSE.getBytes());
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   169
            byte[] buf = new byte[64 * 1024];
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   170
            while (remaining > 0) {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   171
                int amount = (int)Math.min(remaining, buf.length);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   172
                os.write(buf, 0, amount);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   173
                remaining -= amount;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   174
            }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   175
            System.out.println("Server: finished writing");
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   176
            os.close();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   177
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   178
        } catch (IOException e) {
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   179
            long sent = CONTENT_LEN - remaining;
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   180
            System.out.println("Sent " + sent);
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   181
            e.printStackTrace();
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   182
        }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   183
    }
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   184
}
ef0fed0a3953 8212926: HttpClient does not retrieve files with large sizes over HTTP/1.1
michaelm
parents:
diff changeset
   185