1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 import java.io.IOException;
  26 import jdk.incubator.http.HttpClient;
  27 import jdk.incubator.http.HttpRequest;
  28 import jdk.incubator.http.HttpResponse;
  29 import java.net.URI;
  30 import java.util.concurrent.CompletableFuture;
  31 import java.util.concurrent.Executor;
  32 import java.util.concurrent.ExecutorService;
  33 import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
  34 
  35 /**
  36  * @test
  37  * @bug 8087112
  38  * @key intermittent
  39  * @build Server
  40  * @run main/othervm -Djava.net.HttpClient.log=all SplitResponse
  41  */
  42 
  43 /**
  44  * Similar test to QuickResponses except that each byte of the response
  45  * is sent in a separate packet, which tests the stability of the implementation
  46  * for receiving unusual packet sizes.
  47  */
  48 public class SplitResponse {
  49 
  50     static Server server;
  51 
  52     static String response(String body) {
  53         return "HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-length: "
  54                 + Integer.toString(body.length())
  55                 + "\r\n\r\n" + body;
  56     }
  57 
  58     static final String responses[] = {
  59         "Lorem ipsum",
  60         "dolor sit amet",
  61         "consectetur adipiscing elit, sed do eiusmod tempor",
  62         "quis nostrud exercitation ullamco",
  63         "laboris nisi",
  64         "ut",
  65         "aliquip ex ea commodo consequat." +
  66         "Duis aute irure dolor in reprehenderit in voluptate velit esse" +
  67         "cillum dolore eu fugiat nulla pariatur.",
  68         "Excepteur sint occaecat cupidatat non proident."
  69     };
  70 
  71     public static void main(String[] args) throws Exception {
  72         server = new Server(0);
  73         URI uri = new URI(server.getURL());
  74         server.start();
  75 
  76         HttpClient client = HttpClient.newHttpClient();
  77         HttpRequest request = HttpRequest.newBuilder(uri).build();
  78         HttpResponse<String> r;
  79         CompletableFuture<HttpResponse<String>> cf1;
  80 
  81         try {
  82             for (int i=0; i<responses.length; i++) {
  83                 cf1 = client.sendAsync(request, asString());
  84                 String body = responses[i];
  85 
  86                 Server.Connection c = server.activity();
  87                 sendSplitResponse(response(body), c);
  88                 r = cf1.get();
  89                 if (r.statusCode()!= 200)
  90                     throw new RuntimeException("Failed");
  91 
  92                 String rxbody = r.body();
  93                 System.out.println("received " + rxbody);
  94                 if (!rxbody.equals(body))
  95                     throw new RuntimeException("Failed");
  96                 c.close();
  97             }
  98         } finally {
  99             Executor def = client.executor();
 100             if (def instanceof ExecutorService) {
 101                 ((ExecutorService)def).shutdownNow();
 102             }
 103         }
 104         System.out.println("OK");
 105     }
 106 
 107     // send the response one byte at a time with a small delay between bytes
 108     // to ensure that each byte is read in a separate read
 109     static void sendSplitResponse(String s, Server.Connection conn) {
 110         System.out.println("Sending: ");
 111         Thread t = new Thread(() -> {
 112             try {
 113                 int len = s.length();
 114                 for (int i = 0; i < len; i++) {
 115                     String onechar = s.substring(i, i + 1);
 116                     conn.send(onechar);
 117                     Thread.sleep(30);
 118                 }
 119                 System.out.println("sent");
 120             } catch (IOException | InterruptedException e) {
 121             }
 122         });
 123         t.setDaemon(true);
 124         t.start();
 125     }
 126 }