jdk/test/sun/net/www/http/ChunkedOutputStream/Test.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 88 b2fea49cad6b
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 5026745
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static int count = 0;
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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            case 0: /* test1 -- keeps conn alive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            case 1: /* test2 -- closes conn */
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            case 2: /* test 3 */
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            case 3: /* test 4 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            case 4: /* test 5 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                printRequestURI(exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            case 5: /* test 6 */
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            case 6: /* test 7 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            case 7: /* test 8 */
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            case 8: /* test 9 */
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            case 9: /* test10 */
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            exchange.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    static void printRequestURI(HttpExchange exchange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        URI uri = exchange.getRequestURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        System.out.println("HttpServer: handle " + uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static String read (InputStream is, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            byte[] ba = new byte [len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            int l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            while ((c= is.read(ba, l, ba.length-l)) != -1 && l<len)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                l += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return new String (ba, 0, l, "ISO8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static String read(InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            byte[] ba = new byte [8096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            int off = 0, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            while ((c= is.read(ba, off, ba.length)) != -1)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                off += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return new String(ba, 0, off, "ISO8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    static void write(OutputStream os, String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            byte[] ba = str.getBytes("ISO8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            os.write(ba);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } catch (Exception 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 readAndCompare(InputStream is, String cmp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        byte buf[] = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int len = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        while ((c=is.read(buf, off, len)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            off += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            len -= c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        String s1 = new String(buf, 0, off, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (!cmp.equals(s1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new IOException("strings not same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /* basic chunked test (runs twice) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    static void test1 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        readAndCompare (is, str1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /* basic fixed length test */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    static void test3 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        urlc.setFixedLengthStreamingMode (str2.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        readAndCompare (is, str2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /* write too few bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    static void test4 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        urlc.setFixedLengthStreamingMode (str2.length()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new Exception ("should have thrown IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /* write too many bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    static void test5 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        urlc.setFixedLengthStreamingMode (str2.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            os.write (str2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw new Exception ("should have thrown IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /* check for HttpRetryException on redirection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    static void test6 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            throw new Exception ("should have gotten HttpRetryException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } catch (HttpRetryException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (e.responseCode() != 307) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                throw new Exception ("Wrong response code " + e.responseCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (!e.getLocation().equals ("http://foo.bar/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                throw new Exception ("Wrong location " + e.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /* next two tests send zero length posts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    static void test7 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        urlc.setChunkedStreamingMode (20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    static void test8 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        urlc.setFixedLengthStreamingMode (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /* calling setChunkedStreamingMode with -1 should entail using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
       the default chunk size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
   static void test9 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        urlc.setChunkedStreamingMode (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        readAndCompare (is, str1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
   static void test10 (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        urlc.setChunkedStreamingMode (4 * 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        urlc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        urlc.setRequestMethod ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        OutputStream os = urlc.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        byte[] buf = new byte [200 * 1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        for (int i=0; i< 200 * 1024; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            buf[i] = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        /* write a small bit first, and then the large buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        os.write (str1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        int ret = urlc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (ret != 200) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            throw new Exception ("Expected 200: got " + ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    static com.sun.net.httpserver.HttpServer httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            httpserver = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            HttpContext ctx = httpserver.createContext("/test/", new Test() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            httpserver.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            int port = httpserver.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            System.out.println ("Server started: listening on port: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            test1("http://localhost:"+ port + "/test/test1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            test1("http://localhost:"+ port + "/test/test2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            test3("http://localhost:"+ port + "/test/test3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            test4("http://localhost:"+ port + "/test/test4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            test5("http://localhost:"+ port + "/test/test5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            test6("http://localhost:"+ port + "/test/test6");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            test7("http://localhost:"+ port + "/test/test7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            test8("http://localhost:"+ port + "/test/test8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            test9("http://localhost:"+ port + "/test/test9");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            test10("http://localhost:"+ port + "/test/test10");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (httpserver != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                httpserver.stop(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
}