File: ResponseBodyBeforeError.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (530 lines) | stat: -rw-r--r-- 20,596 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @summary Tests that all response body is delivered to the BodySubscriber
 *          before an abortive error terminates the flow
 * @library /lib/testlibrary
 * @build jdk.testlibrary.SimpleSSLContext
 * @run testng/othervm ResponseBodyBeforeError
 */

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodySubscriber;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Flow;
import jdk.testlibrary.SimpleSSLContext;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocketFactory;
import static java.lang.System.out;
import static java.net.http.HttpClient.Builder.NO_PROXY;
import static java.net.http.HttpResponse.BodyHandlers.ofString;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

public class ResponseBodyBeforeError {

    ReplyingServer variableLengthServer;
    ReplyingServer variableLengthHttpsServer;
    ReplyingServer fixedLengthServer;
    ReplyingServer fixedLengthHttpsServer;

    String httpURIVarLen;
    String httpsURIVarLen;
    String httpURIFixLen;
    String httpsURIFixLen;

    SSLContext sslContext;

    static final String EXPECTED_RESPONSE_BODY =
            "<html><body><h1>Heading</h1><p>Some Text</p></body></html>";

    @DataProvider(name = "sanity")
    public Object[][] sanity() {
        return new Object[][]{
                { httpURIVarLen   + "?length=all" },
                { httpsURIVarLen  + "?length=all" },
                { httpURIFixLen   + "?length=all" },
                { httpsURIFixLen  + "?length=all" },
        };
    }

    @Test(dataProvider = "sanity")
    void sanity(String url) throws Exception {
        HttpClient client = HttpClient.newBuilder()
                .proxy(NO_PROXY)
                .sslContext(sslContext)
                .build();
        HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
        HttpResponse<String> response = client.send(request, ofString());
        String body = response.body();
        assertEquals(body, EXPECTED_RESPONSE_BODY);
        client.sendAsync(request, ofString())
                .thenApply(resp -> resp.body())
                .thenAccept(b -> assertEquals(b, EXPECTED_RESPONSE_BODY))
                .join();
    }

    @DataProvider(name = "uris")
    public Object[][] variants() {
        Object[][] cases = new Object[][] {
            // The length query string is the total number of response body
            // bytes in the reply, before the server closes the connection. The
            // second arg is a partial-expected-body that the body subscriber
            // should receive before onError is invoked.

            { httpURIFixLen + "?length=0",   ""               },
            { httpURIFixLen + "?length=1",   "<"              },
            { httpURIFixLen + "?length=2",   "<h"             },
            { httpURIFixLen + "?length=10",  "<html><bod"     },
            { httpURIFixLen + "?length=19",  "<html><body><h1>Hea"             },
            { httpURIFixLen + "?length=31",  "<html><body><h1>Heading</h1><p>" },

            { httpsURIFixLen + "?length=0",   ""              },
            { httpsURIFixLen + "?length=1",   "<"             },
            { httpsURIFixLen + "?length=2",   "<h"            },
            { httpsURIFixLen + "?length=10",  "<html><bod"    },
            { httpsURIFixLen + "?length=19",  "<html><body><h1>Hea"             },
            { httpsURIFixLen + "?length=31",  "<html><body><h1>Heading</h1><p>" },

            // accounts for chunk framing
            { httpURIVarLen + "?length=0",   ""               },
            { httpURIVarLen + "?length=1",   ""               },
            { httpURIVarLen + "?length=2",   ""               },
            { httpURIVarLen + "?length=4",   "<"              },
            { httpURIVarLen + "?length=5",   "<h"             },
            { httpURIVarLen + "?length=18",  "<html><bod"     },
            { httpURIVarLen + "?length=20",  "<html><body>"   },
            { httpURIVarLen + "?length=21",  "<html><body>"   }, // boundary around chunk framing
            { httpURIVarLen + "?length=22",  "<html><body>"   },
            { httpURIVarLen + "?length=23",  "<html><body>"   },
            { httpURIVarLen + "?length=24",  "<html><body>"   },
            { httpURIVarLen + "?length=25",  "<html><body>"   },
            { httpURIVarLen + "?length=26",  "<html><body>"   },
            { httpURIVarLen + "?length=27",  "<html><body><"  },
            { httpURIVarLen + "?length=51",  "<html><body><h1>Heading</h1><p>" },

            { httpsURIVarLen + "?length=0",   ""              },
            { httpsURIVarLen + "?length=1",   ""              },
            { httpsURIVarLen + "?length=2",   ""              },
            { httpsURIVarLen + "?length=4",   "<"             },
            { httpsURIVarLen + "?length=5",   "<h"            },
            { httpsURIVarLen + "?length=18",  "<html><bod"    },
            { httpsURIVarLen + "?length=20",  "<html><body>"  },
            { httpsURIVarLen + "?length=21",  "<html><body>"  },
            { httpsURIVarLen + "?length=22",  "<html><body>"  },
            { httpsURIVarLen + "?length=23",  "<html><body>"  },
            { httpsURIVarLen + "?length=24",  "<html><body>"  },
            { httpsURIVarLen + "?length=25",  "<html><body>"  },
            { httpsURIVarLen + "?length=26",  "<html><body>"  },
            { httpsURIVarLen + "?length=27",  "<html><body><" },
            { httpsURIVarLen + "?length=51",  "<html><body><h1>Heading</h1><p>" },
        };

        List<Object[]> list = new ArrayList<>();
        Arrays.asList(cases).stream()
                .map(e -> new Object[] {e[0], e[1], true})  // reuse client
                .forEach(list::add);
        Arrays.asList(cases).stream()
                .map(e -> new Object[] {e[0], e[1], false}) // do not reuse client
                .forEach(list::add);
        return list.stream().toArray(Object[][]::new);
    }

    static final int ITERATION_COUNT = 3;

    @Test(dataProvider = "uris")
    void testSynchronousAllRequestBody(String url,
                                       String expectedPatrialBody,
                                       boolean sameClient)
        throws Exception
    {
        out.print("---\n");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = HttpClient.newBuilder()
                        .proxy(NO_PROXY)
                        .sslContext(sslContext)
                        .build();
            HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
            CustomBodySubscriber bs = new CustomBodySubscriber();
            try {
                HttpResponse<String> response = client.send(request, r -> bs);
                String body = response.body();
                out.println(response + ": " + body);
                fail("UNEXPECTED RESPONSE: " + response);
            } catch (IOException expected) {
                String pm = bs.receivedAsString();
                out.println("partial body received: " + pm);
                assertEquals(pm, expectedPatrialBody);
            }
        }
    }

    @Test(dataProvider = "uris")
    void testAsynchronousAllRequestBody(String url,
                                        String expectedPatrialBody,
                                        boolean sameClient)
        throws Exception
    {
        out.print("---\n");
        HttpClient client = null;
        for (int i=0; i< ITERATION_COUNT; i++) {
            if (!sameClient || client == null)
                client = HttpClient.newBuilder()
                        .proxy(NO_PROXY)
                        .sslContext(sslContext)
                        .build();
            HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
            CustomBodySubscriber bs = new CustomBodySubscriber();
            try {
                HttpResponse<String> response = client.sendAsync(request, r -> bs).get();
                String body = response.body();
                out.println(response + ": " + body);
                fail("UNEXPECTED RESPONSE: " + response);
            } catch (ExecutionException ee) {
                if (ee.getCause() instanceof IOException) {
                    String pm = bs.receivedAsString();
                    out.println("partial body received: " + pm);
                    assertEquals(pm, expectedPatrialBody);
                } else {
                    throw ee;
                }
            }
        }
    }

    static final class CustomBodySubscriber implements BodySubscriber<String> {

        Flow.Subscription subscription;
        private final List<ByteBuffer> received = new ArrayList<>();
        private final CompletableFuture<String> cf = new CompletableFuture<>();

        @Override
        public CompletionStage<String> getBody() {
            return cf;
        }

        @Override
        public void onSubscribe(Flow.Subscription subscription) {
            out.println("CustomBodySubscriber got onSubscribe: ");
            this.subscription = subscription;
            subscription.request(1);
        }

        @Override
        public void onNext(List<ByteBuffer> items) {
            out.println("CustomBodySubscriber got onNext: " + items);
            received.addAll(items);
            subscription.request(1);
        }

        @Override
        public void onError(Throwable expected) {
            out.println("CustomBodySubscriber got expected: " + expected);
            cf.completeExceptionally(expected);
        }

        String receivedAsString() {
            int size = received.stream().mapToInt(ByteBuffer::remaining).sum();
            byte[] res = new byte[size];
            int from = 0;
            for (ByteBuffer b : received) {
                int l = b.remaining();
                b.get(res, from, l);
                from += l;
            }
            return new String(res, UTF_8);
        }

        @Override
        public void onComplete() {
            out.println("CustomBodySubscriber got complete: ");
            assert false : "Unexpected onComplete";
        }
    }

    // -- infra

    /**
     * A server that replies with headers and a, possibly partial, reply, before
     * closing the connection. The number of body bytes of written, is
     * controllable through the "length" query string param in the requested
     * URI.
     */
    static abstract class ReplyingServer extends Thread implements Closeable {

        private final String name;
        private final ServerSocket ss;
        private volatile boolean closed;

        private ReplyingServer(String name) throws IOException {
            super(name);
            this.name = name;
            ss = newServerSocket();
            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
            this.start();
        }

        protected ServerSocket newServerSocket() throws IOException {
            return new ServerSocket();
        }

        abstract String responseHeaders();

        abstract String responseBody();

        @Override
        public void run() {
            while (!closed) {
                try (Socket s = ss.accept()) {
                    out.print(name + ": got connection ");
                    InputStream is = s.getInputStream();
                    URI requestMethod = readRequestMethod(is);
                    out.print(requestMethod + " ");
                    URI uriPath = readRequestPath(is);
                    out.println(uriPath);
                    readRequestHeaders(is);

                    String query = uriPath.getRawQuery();
                    assert query != null;
                    String qv = query.split("=")[1];
                    int len;
                    if (qv.equals("all")) {
                        len = responseBody().getBytes(US_ASCII).length;
                    } else {
                        len = Integer.parseInt(query.split("=")[1]);
                    }

                    OutputStream os = s.getOutputStream();
                    os.write(responseHeaders().getBytes(US_ASCII));
                    out.println(name  + ": headers written, writing " + len  + " body bytes");
                    byte[] responseBytes = responseBody().getBytes(US_ASCII);
                    for (int i = 0; i< len; i++) {
                        os.write(responseBytes[i]);
                        os.flush();
                    }
                } catch (IOException e) {
                    if (!closed)
                        throw new UncheckedIOException("Unexpected", e);
                }
            }
        }

        static final byte[] requestEnd = new byte[] { '\r', '\n', '\r', '\n' };

        // Read the request method
        static URI readRequestMethod(InputStream is) throws IOException {
            StringBuilder sb = new StringBuilder();
            int r;
            while ((r = is.read()) != -1 && r != 0x20) {
                sb.append((char)r);
            }
            return URI.create(sb.toString());
        }

        // Read the request URI path
        static URI readRequestPath(InputStream is) throws IOException {
            StringBuilder sb = new StringBuilder();
            int r;
            while ((r = is.read()) != -1 && r != 0x20) {
                sb.append((char)r);
            }
            return URI.create(sb.toString());
        }

        // Read until the end of a HTTP request headers
        static void readRequestHeaders(InputStream is) throws IOException {
            int requestEndCount = 0, r;
            while ((r = is.read()) != -1) {
                if (r == requestEnd[requestEndCount]) {
                    requestEndCount++;
                    if (requestEndCount == 4) {
                        break;
                    }
                } else {
                    requestEndCount = 0;
                }
            }
        }

        public int getPort() { return ss.getLocalPort(); }

        @Override
        public void close() {
            if (closed)
                return;
            closed = true;
            try {
                ss.close();
            } catch (IOException e) {
                throw new UncheckedIOException("Unexpected", e);
            }
        }
    }

    /** A server that issues a possibly-partial chunked reply. */
    static class PlainVariableLengthServer extends ReplyingServer {

        static final String CHUNKED_RESPONSE_BODY =
                "6\r\n"+ "<html>\r\n" +
                "6\r\n"+ "<body>\r\n" +
                "10\r\n"+ "<h1>Heading</h1>\r\n" +
                "10\r\n"+ "<p>Some Text</p>\r\n" +
                "7\r\n"+ "</body>\r\n" +
                "7\r\n"+ "</html>\r\n" +
                "0\r\n"+ "\r\n";

        static final String RESPONSE_HEADERS =
                "HTTP/1.1 200 OK\r\n" +
                "Content-Type: text/html; charset=utf-8\r\n" +
                "Transfer-Encoding: chunked\r\n" +
                "Connection: close\r\n\r\n";


        PlainVariableLengthServer() throws IOException {
            super("PlainVariableLengthServer");
        }

        protected PlainVariableLengthServer(String name) throws IOException {
            super(name);
        }

        @Override
        String responseHeaders( ) { return RESPONSE_HEADERS; }

        @Override
        String responseBody( ) { return CHUNKED_RESPONSE_BODY; }
    }

    /** A server that issues a, possibly-partial, chunked reply over SSL */
    static final class SSLVariableLengthServer extends PlainVariableLengthServer {
        SSLVariableLengthServer() throws IOException {
            super("SSLVariableLengthServer");
        }
        @Override
        public ServerSocket newServerSocket() throws IOException {
            return SSLServerSocketFactory.getDefault().createServerSocket();
        }
    }

    /** A server that issues a, possibly-partial, fixed-length reply. */
    static class PlainFixedLengthServer extends ReplyingServer {

        static final String RESPONSE_BODY = EXPECTED_RESPONSE_BODY;

        static final String RESPONSE_HEADERS =
                "HTTP/1.1 200 OK\r\n" +
                "Content-Type: text/html; charset=utf-8\r\n" +
                "Content-Length: " + RESPONSE_BODY.length() + "\r\n" +
                "Connection: close\r\n\r\n";

        PlainFixedLengthServer() throws IOException {
            super("PlainFixedLengthServer");
        }

        protected PlainFixedLengthServer(String name) throws IOException {
            super(name);
        }

        @Override
        String responseHeaders( ) { return RESPONSE_HEADERS; }

        @Override
        String responseBody( ) { return RESPONSE_BODY; }
    }

    /** A server that issues a,  possibly-partial, fixed-length reply over SSL */
    static final class SSLFixedLengthServer extends PlainFixedLengthServer {
        SSLFixedLengthServer() throws IOException {
            super("SSLFixedLengthServer");
        }
        @Override
        public ServerSocket newServerSocket() throws IOException {
            return SSLServerSocketFactory.getDefault().createServerSocket();
        }
    }

    static String serverAuthority(ReplyingServer server) {
        return InetAddress.getLoopbackAddress().getHostName() + ":"
                + server.getPort();
    }

    @BeforeTest
    public void setup() throws Exception {
        sslContext = new SimpleSSLContext().get();
        if (sslContext == null)
            throw new AssertionError("Unexpected null sslContext");
        SSLContext.setDefault(sslContext);

        variableLengthServer = new PlainVariableLengthServer();
        httpURIVarLen = "http://" + serverAuthority(variableLengthServer)
                + "/http1/variable/foo";

        variableLengthHttpsServer = new SSLVariableLengthServer();
        httpsURIVarLen = "https://" + serverAuthority(variableLengthHttpsServer)
                + "/https1/variable/bar";

        fixedLengthServer = new PlainFixedLengthServer();
        httpURIFixLen = "http://" + serverAuthority(fixedLengthServer)
                + "/http1/fixed/baz";

        fixedLengthHttpsServer = new SSLFixedLengthServer();
        httpsURIFixLen = "https://" + serverAuthority(fixedLengthHttpsServer)
                + "/https1/fixed/foz";
    }

    @AfterTest
    public void teardown() throws Exception {
        variableLengthServer.close();
        variableLengthHttpsServer.close();
        fixedLengthServer.close();
        fixedLengthHttpsServer.close();
    }
}