jdk/test/sun/net/www/http/ChunkedOutputStream/Test.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6115 7c523cf2bc8a
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: 6115
diff changeset
     2
 * Copyright (c) 2004, 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: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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
88
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
    26
 * @bug 5026745 6631048
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @run main/othervm/timeout=500 Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Cannot flush output stream when writing to an HttpUrlConnection
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
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
public class Test implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    37
    static volatile int count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                                "1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                                "1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                                "1234567890";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public void handle(HttpExchange exchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        String reqbody;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        try {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    49
            switch (exchange.getRequestURI().toString()) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    50
            case "/test/test1": /* test1 -- keeps conn alive */
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    51
            case "/test/test2": /* test2 -- closes conn */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                reqbody = read(exchange.getRequestBody());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                if (!reqbody.equals(str1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    exchange.sendResponseHeaders(500, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                Headers headers = exchange.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                String chunk =  headers.getFirst("Transfer-encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                if (!"chunked".equals (chunk)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    exchange.sendResponseHeaders(501, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                exchange.sendResponseHeaders(200, reqbody.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                write(exchange.getResponseBody(), reqbody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                if (count == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    Headers resHeaders = exchange.getResponseHeaders() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    resHeaders.set("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    75
            case "/test/test3": /* test 3 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                reqbody = read(exchange.getRequestBody());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if (!reqbody.equals(str2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    exchange.sendResponseHeaders(500, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                headers = exchange.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                int clen = Integer.parseInt( headers.getFirst("Content-length"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (clen != str2.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    exchange.sendResponseHeaders(501, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                Headers resHeaders = exchange.getResponseHeaders() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                resHeaders.set("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                exchange.sendResponseHeaders(200, reqbody.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                write(exchange.getResponseBody(), reqbody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    96
            case "/test/test4": /* test 4 */
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    97
            case "/test/test5": /* test 5 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   100
            case "/test/test6": /* test 6 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                resHeaders = exchange.getResponseHeaders() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                resHeaders.set("Location", "http://foo.bar/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                resHeaders.set("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                exchange.sendResponseHeaders(307, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   107
            case "/test/test7": /* test 7 */
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   108
            case "/test/test8": /* test 8 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                reqbody = read(exchange.getRequestBody());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (reqbody != null && !"".equals(reqbody)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    exchange.sendResponseHeaders(501, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                resHeaders = exchange.getResponseHeaders() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                resHeaders.set("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                exchange.sendResponseHeaders(200, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   119
            case "/test/test9": /* test 9 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                reqbody = read(exchange.getRequestBody());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (!reqbody.equals(str1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    exchange.sendResponseHeaders(500, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                headers = exchange.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                chunk =  headers.getFirst("Transfer-encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if (!"chunked".equals(chunk)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    exchange.sendResponseHeaders(501, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                exchange.sendResponseHeaders(200, reqbody.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                write(exchange.getResponseBody(), reqbody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   137
            case "/test/test10": /* test10 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                InputStream is = exchange.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                String s = read (is, str1.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                for (int i=10; i< 200 * 1024; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    byte c = (byte)is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    if (c != (byte)i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        System.out.println ("error at position " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (!s.equals(str1) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    System.out.println ("received string : " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    exchange.sendResponseHeaders(500, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                } else if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    System.out.println ("error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    exchange.sendResponseHeaders(500, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    exchange.sendResponseHeaders(200, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   161
            case "/test/test11": /* test11 */
88
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   162
                printRequestURI(exchange);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   163
                is = exchange.getRequestBody();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   164
                s = read (is, str1.length());
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   165
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   166
                error = false;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   167
                for (int i=10; i< 30 * 1024; i++) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   168
                    byte c = (byte)is.read();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   169
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   170
                    if (c != (byte)i) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   171
                        error = true;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   172
                        System.out.println ("error at position " + i);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   173
                    }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   174
                }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   175
                if (!s.equals(str1) ) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   176
                    System.out.println ("received string : " + s);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   177
                    exchange.sendResponseHeaders(500, 0);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   178
                } else if (error) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   179
                    System.out.println ("error");
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   180
                    exchange.sendResponseHeaders(500, 0);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   181
                } else {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   182
                    exchange.sendResponseHeaders(200, 0);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   183
                }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   184
                break;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   185
            case "/test/test12": /* test12 */
88
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   186
                printRequestURI(exchange);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   187
                is = exchange.getRequestBody();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   188
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   189
                error = false;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   190
                for (int i=10; i< 30 * 1024; i++) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   191
                    byte c = (byte)is.read();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   192
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   193
                    if (c != (byte)i) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   194
                        error = true;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   195
                        System.out.println ("error at position " + i);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   196
                    }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   197
                }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   198
                if (error) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   199
                    System.out.println ("error");
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   200
                    exchange.sendResponseHeaders(500, 0);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   201
                } else {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   202
                    exchange.sendResponseHeaders(200, 0);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   203
                }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   204
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   206
            count ++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            exchange.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    static void printRequestURI(HttpExchange exchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        URI uri = exchange.getRequestURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        System.out.println("HttpServer: handle " + uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static String read (InputStream is, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            byte[] ba = new byte [len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            int l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            while ((c= is.read(ba, l, ba.length-l)) != -1 && l<len)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                l += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return new String (ba, 0, l, "ISO8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    static String read(InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            byte[] ba = new byte [8096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            int off = 0, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            while ((c= is.read(ba, off, ba.length)) != -1)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                off += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return new String(ba, 0, off, "ISO8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    static void write(OutputStream os, String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            byte[] ba = str.getBytes("ISO8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            os.write(ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    static void readAndCompare(InputStream is, String cmp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        byte buf[] = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int len = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        while ((c=is.read(buf, off, len)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            off += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            len -= c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        String s1 = new String(buf, 0, off, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (!cmp.equals(s1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw new IOException("strings not same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /* basic chunked test (runs twice) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    static void test1 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        readAndCompare (is, str1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /* basic fixed length test */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    static void test3 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        urlc.setFixedLengthStreamingMode (str2.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        readAndCompare (is, str2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /* write too few bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    static void test4 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        urlc.setFixedLengthStreamingMode (str2.length()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw new Exception ("should have thrown IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /* write too many bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static void test5 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        urlc.setFixedLengthStreamingMode (str2.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            throw new Exception ("should have thrown IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /* check for HttpRetryException on redirection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    static void test6 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            throw new Exception ("should have gotten HttpRetryException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        } catch (HttpRetryException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            if (e.responseCode() != 307) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                throw new Exception ("Wrong response code " + e.responseCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (!e.getLocation().equals ("http://foo.bar/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                throw new Exception ("Wrong location " + e.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /* next two tests send zero length posts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    static void test7 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    static void test8 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        urlc.setFixedLengthStreamingMode (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /* calling setChunkedStreamingMode with -1 should entail using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
       the default chunk size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
   static void test9 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        urlc.setChunkedStreamingMode (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        readAndCompare (is, str1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
   static void test10 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        urlc.setChunkedStreamingMode (4 * 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        byte[] buf = new byte [200 * 1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        for (int i=0; i< 200 * 1024; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            buf[i] = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        /* write a small bit first, and then the large buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
88
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   437
    static void test11 (String u) throws Exception {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   438
        URL url = new URL (u);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   439
        System.out.println ("client opening connection to: " + u);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   440
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   441
        urlc.setChunkedStreamingMode (36 * 1024);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   442
        urlc.setDoOutput(true);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   443
        urlc.setRequestMethod ("POST");
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   444
        OutputStream os = urlc.getOutputStream ();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   445
        byte[] buf = new byte [30 * 1024];
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   446
        for (int i=0; i< 30 * 1024; i++) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   447
            buf[i] = (byte) i;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   448
        }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   449
        /* write a small bit first, and then the large buffer */
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   450
        os.write (str1.getBytes());
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   451
        //os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   452
        os.write (buf, 10, (10 * 1024) - 10);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   453
        os.write (buf, (10 * 1024), (10 * 1024));
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   454
        os.write (buf, (20 * 1024), (10 * 1024));
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   455
        os.close();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   456
        InputStream is = urlc.getInputStream();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   457
        is.close();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   458
        int ret = urlc.getResponseCode();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   459
        if (ret != 200) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   460
            throw new Exception ("Expected 200: got " + ret);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   461
        }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   462
    }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   463
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   464
    static void test12 (String u) throws Exception {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   465
        URL url = new URL (u);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   466
        System.out.println ("client opening connection to: " + u);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   467
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   468
        urlc.setChunkedStreamingMode (36 * 1024);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   469
        urlc.setDoOutput(true);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   470
        urlc.setRequestMethod ("POST");
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   471
        OutputStream os = urlc.getOutputStream ();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   472
        byte[] buf = new byte [30 * 1024];
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   473
        for (int i=0; i< 30 * 1024; i++) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   474
            buf[i] = (byte) i;
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   475
        }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   476
        os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   477
        os.close();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   478
        InputStream is = urlc.getInputStream();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   479
        is.close();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   480
        int ret = urlc.getResponseCode();
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   481
        if (ret != 200) {
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   482
            throw new Exception ("Expected 200: got " + ret);
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   483
        }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   484
    }
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   485
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   486
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    static com.sun.net.httpserver.HttpServer httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            httpserver = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            HttpContext ctx = httpserver.createContext("/test/", new Test() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            httpserver.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            int port = httpserver.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            System.out.println ("Server started: listening on port: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            test1("http://localhost:"+ port + "/test/test1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            test1("http://localhost:"+ port + "/test/test2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            test3("http://localhost:"+ port + "/test/test3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            test4("http://localhost:"+ port + "/test/test4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            test5("http://localhost:"+ port + "/test/test5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            test6("http://localhost:"+ port + "/test/test6");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            test7("http://localhost:"+ port + "/test/test7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            test8("http://localhost:"+ port + "/test/test8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            test9("http://localhost:"+ port + "/test/test9");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            test10("http://localhost:"+ port + "/test/test10");
88
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   508
            test11("http://localhost:"+ port + "/test/test11");
b2fea49cad6b 6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents: 2
diff changeset
   509
            test12("http://localhost:"+ port + "/test/test12");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (httpserver != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                httpserver.stop(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
}