jdk/test/sun/net/www/protocol/http/ChunkedErrorStream.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7022 1066bfde0f5e
child 38557 5c485e1ea6fa
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7022
diff changeset
     2
 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    26
 * @bug 6488669 6595324 6993490
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @run main/othervm ChunkedErrorStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Chunked ErrorStream tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Part 1: 6488669
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * 1) Http server that responds with an error code (>=400)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *    and a chunked response body. It also indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *    the connection will be closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * 2) Client sends request to server and tries to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *    getErrorStream(). Some data must be able to be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *    from the errorStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Part 2: 6595324
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * 1) Http server that responds with an error code (>=400)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *    and a chunked response body greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *    sun.net.http.errorstream.bufferSize, 4K + 10 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * 2) Client sends request to server and tries to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *    getErrorStream(). 4K + 10 bytes must be read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *    the errorStream.
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    51
 *
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    52
 * Part 3: 6993490
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    53
 *    Reuse persistent connection from part 2, the error stream
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    54
 *    buffering will have set a reduced timeout on the socket and
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    55
 *    tried to reset it to the default, infinity. Client must not
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    56
 *    throw a timeout exception. If it does, it indicates that the
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    57
 *    default timeout was not reset correctly.
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    58
 *    If no timeout exception is thrown, it does not guarantee that
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    59
 *    the timeout was reset correctly, as there is a potential race
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    60
 *    between the sleeping server and the client thread. Typically,
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    61
 *    1000 millis has been enought to reliable reproduce this problem
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    62
 *    since the error stream buffering sets the timeout to 60 millis.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class ChunkedErrorStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    com.sun.net.httpserver.HttpServer httpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // Enable ErrorStream buffering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        System.getProperties().setProperty("sun.net.http.errorstream.enableBuffering", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // No need to set this as 4K is the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // System.getProperties().setProperty("sun.net.http.errorstream.bufferSize", "4096");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        new ChunkedErrorStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public ChunkedErrorStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            startHttpServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            doClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }  finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            httpServer.stop(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    void doClient() {
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    93
        for (int times=0; times<3; times++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            HttpURLConnection uc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                InetSocketAddress address = httpServer.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                String URLStr = "http://localhost:" + address.getPort() + "/test/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (times == 0) {
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    99
                    URLStr += "first";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                } else {
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   101
                    URLStr += "second";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                System.out.println("Trying " + URLStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                URL url = new URL(URLStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                uc = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                uc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                throw new RuntimeException("Failed: getInputStream should throw and IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }  catch (IOException e) {
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   111
                if (e instanceof SocketTimeoutException) {
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   112
                    e.printStackTrace();
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   113
                    throw new RuntimeException("Failed: SocketTimeoutException should not happen");
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   114
                }
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   115
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                // This is what we expect to happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                InputStream es = uc.getErrorStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                byte[] ba = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                int count = 0, ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    while ((ret = es.read(ba)) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        count += ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    es.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                } catch  (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (count == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    throw new RuntimeException("Failed: ErrorStream returning 0 bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   131
                if (times >= 1 && count != (4096+10))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    throw new RuntimeException("Failed: ErrorStream returning " + count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                                 " bytes. Expecting " + (4096+10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                System.out.println("Read " + count + " bytes from the errorStream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Http Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    void startHttpServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // create HttpServer context
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   147
        httpServer.createContext("/test/first", new FirstHandler());
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   148
        httpServer.createContext("/test/second", new SecondHandler());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        httpServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   153
    class FirstHandler implements HttpHandler {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public void handle(HttpExchange t) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            byte[] ba = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            while (is.read(ba) != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            Headers resHeaders = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            resHeaders.add("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            t.sendResponseHeaders(404, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // actual data doesn't matter. Just send 2K worth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            byte b = 'a';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            for (int i=0; i<2048; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                os.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   175
    static class SecondHandler implements HttpHandler {
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   176
        /* count greater than 0, slow response */
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   177
        static int count = 0;
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   178
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        public void handle(HttpExchange t) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            byte[] ba = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            while (is.read(ba) != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   185
            if (count > 0) {
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   186
                System.out.println("server sleeping...");
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   187
                try { Thread.sleep(1000); } catch(InterruptedException e) {}
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   188
            }
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   189
            count++;
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   190
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            t.sendResponseHeaders(404, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // actual data doesn't matter. Just send more than 4K worth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            byte b = 'a';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            for (int i=0; i<(4096+10); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                os.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}