test/jdk/java/net/httpclient/UnknownBodyLengthTest.java
changeset 53572 f5480f924571
parent 53522 40eb23e0a8c5
child 54086 ccb4a50bee06
equal deleted inserted replaced
53571:3997614d4834 53572:f5480f924571
    33 import javax.net.ssl.SSLContext;
    33 import javax.net.ssl.SSLContext;
    34 import javax.net.ssl.SSLParameters;
    34 import javax.net.ssl.SSLParameters;
    35 import java.net.http.HttpClient;
    35 import java.net.http.HttpClient;
    36 import java.net.http.HttpRequest;
    36 import java.net.http.HttpRequest;
    37 import java.net.http.HttpResponse;
    37 import java.net.http.HttpResponse;
       
    38 import java.util.List;
       
    39 import java.util.concurrent.CopyOnWriteArrayList;
       
    40 
    38 import jdk.test.lib.net.SimpleSSLContext;
    41 import jdk.test.lib.net.SimpleSSLContext;
    39 
    42 
    40 /**
    43 /**
    41  * @test
    44  * @test
    42  * @bug 8207966
    45  * @bug 8207966
    58     volatile SSLContext ctx;
    61     volatile SSLContext ctx;
    59     volatile ServerSocketFactory factory;
    62     volatile ServerSocketFactory factory;
    60     volatile String clientURL;
    63     volatile String clientURL;
    61     volatile int port;
    64     volatile int port;
    62     final ServerSocket ss;
    65     final ServerSocket ss;
       
    66     final List<Socket> acceptedList = new CopyOnWriteArrayList<>();
    63 
    67 
    64     UnknownBodyLengthTest(boolean useSSL) throws Exception {
    68     UnknownBodyLengthTest(boolean useSSL) throws Exception {
    65         ctx = new SimpleSSLContext().get();
    69         ctx = new SimpleSSLContext().get();
    66         SSLContext.setDefault(ctx);
    70         SSLContext.setDefault(ctx);
    67         factory = useSSL ? SSLServerSocketFactory.getDefault()
    71         factory = useSSL ? SSLServerSocketFactory.getDefault()
    92 
    96 
    93     void server(final boolean withContentLength) {
    97     void server(final boolean withContentLength) {
    94         fillBuf(BUF);
    98         fillBuf(BUF);
    95         try {
    99         try {
    96             while (!stopped) {
   100             while (!stopped) {
    97                 try (Socket s = ss.accept()) {
   101                 try {
       
   102                     Socket s = ss.accept();
       
   103                     acceptedList.add(s);
    98                     s.setTcpNoDelay(true);
   104                     s.setTcpNoDelay(true);
    99                     s.setSoLinger(true, 1);
   105                     // if we use linger=1 we still see some
       
   106                     // intermittent failures caused by IOException
       
   107                     // "Connection reset by peer".
       
   108                     // The client side is expecting EOF, but gets reset instead.
       
   109                     // 30 is a 'magic' value that may need to be adjusted again.
       
   110                     s.setSoLinger(true, 30);
   100                     System.out.println("Accepted: " + s.getRemoteSocketAddress());
   111                     System.out.println("Accepted: " + s.getRemoteSocketAddress());
   101                     System.out.println("Accepted: " + s);
   112                     System.out.println("Accepted: " + s);
   102                     OutputStream os = s.getOutputStream();
   113                     OutputStream os = s.getOutputStream();
   103                     InputStream is = s.getInputStream();
   114                     InputStream is = s.getInputStream();
   104                     boolean done = false;
   115                     boolean done = false;
   119                     System.out.println(chdr);
   130                     System.out.println(chdr);
   120                     if (withContentLength)
   131                     if (withContentLength)
   121                         os.write(chdr.getBytes());
   132                         os.write(chdr.getBytes());
   122                     os.write("\r\n".getBytes());
   133                     os.write("\r\n".getBytes());
   123                     os.write(BUF);
   134                     os.write(BUF);
   124                     if (is.available() > 0)
   135                     if (is.available() > 0) {
       
   136                         System.out.println("Draining input: " + s);
   125                         is.read(buf);
   137                         is.read(buf);
       
   138                     }
   126                     os.flush();
   139                     os.flush();
   127                     os.close();
       
   128                     s.shutdownOutput();
   140                     s.shutdownOutput();
       
   141                     System.out.println("Closed output: " + s);
   129                 } catch(Exception e) {
   142                 } catch(Exception e) {
   130                     if (!stopped) {
   143                     if (!stopped) {
   131                         System.out.println("Unexpected server exception: " + e);
   144                         System.out.println("Unexpected server exception: " + e);
   132                         e.printStackTrace();
   145                         e.printStackTrace();
   133                     }
   146                     }
   134                 }
   147                 }
   135             }
   148             }
   136         } catch(final Throwable t) {
   149         } catch(final Throwable t) {
   137             if (!stopped) t.printStackTrace();
   150             if (!stopped) t.printStackTrace();
   138         } finally {
   151         } finally {
   139             try {stopped = true; ss.close(); } catch (Exception e) {}
   152             stop();
   140         }
   153         }
   141     }
   154     }
   142 
   155 
   143     void client(boolean useSSL) throws Exception {
   156     void client(boolean useSSL) throws Exception {
   144         SSLContext ctx = SSLContext.getDefault();
   157         SSLContext ctx = SSLContext.getDefault();
   180     }
   193     }
   181 
   194 
   182     public void stop() {
   195     public void stop() {
   183         stopped = true;
   196         stopped = true;
   184         try { ss.close(); } catch (Throwable t) { }
   197         try { ss.close(); } catch (Throwable t) { }
       
   198         for (Socket s : acceptedList) {
       
   199             try { s.close(); } catch (Throwable t) { }
       
   200         }
   185     }
   201     }
   186 }
   202 }