jdk/test/sun/net/www/protocol/http/B8012625.java
author michaelm
Thu, 16 May 2013 17:28:47 +0100
changeset 17485 feb8cd6a4c7d
child 38557 5c485e1ea6fa
permissions -rw-r--r--
8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17485
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     1
/*
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     4
 *
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     8
 *
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    13
 * accompanied this code).
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    14
 *
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    18
 *
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    21
 * questions.
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    22
 */
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    23
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    24
/**
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    25
 * @test
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    26
 * @bug 8012625
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    27
 * @run main B8012625
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    28
 */
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    29
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    30
import java.net.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    31
import java.io.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    32
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    33
import java.net.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    34
import java.io.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    35
import java.util.concurrent.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    36
import com.sun.net.httpserver.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    37
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    38
public class B8012625 implements HttpHandler {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    39
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    40
    public static void main (String[] args) throws Exception {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    41
        B8012625 test = new B8012625();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    42
        test.run();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    43
    }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    44
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    45
    public void run() throws Exception {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    46
        String u = "http://127.0.0.1:" + port + "/foo";
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    47
        URL url = new URL(u);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    48
        HttpURLConnection uc = (HttpURLConnection)url.openConnection();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    49
        uc.setDoOutput(true);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    50
        uc.setRequestMethod("POST");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    51
        uc.addRequestProperty("Expect", "100-Continue");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    52
        //uc.setFixedLengthStreamingMode(256);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    53
        System.out.println ("Client: getting outputstream");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    54
        long before = System.currentTimeMillis();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    55
        OutputStream os = uc.getOutputStream();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    56
        long after = System.currentTimeMillis();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    57
        System.out.println ("Client: writing to outputstream");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    58
        byte[] buf = new byte[256];
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    59
        os.write(buf);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    60
        System.out.println ("Client: done writing ");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    61
        int r = uc.getResponseCode();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    62
        System.out.println ("Client: received response code " + r);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    63
        server.stop(1);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    64
        ex.shutdownNow();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    65
        if (after - before >= 5000) {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    66
            throw new RuntimeException("Error: 5 second delay seen");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    67
        }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    68
    }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    69
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    70
    int port;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    71
    HttpServer server;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    72
    ExecutorService ex;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    73
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    74
    public B8012625 () throws Exception {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    75
        server = HttpServer.create(new InetSocketAddress(0), 10);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    76
        HttpContext ctx = server.createContext("/", this);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    77
        ex = Executors.newFixedThreadPool(5);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    78
        server.setExecutor(ex);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    79
        server.start();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    80
        port = server.getAddress().getPort();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    81
   }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    82
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    83
    public void handle(HttpExchange ex) throws IOException {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    84
        String s = ex.getRequestMethod();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    85
        if (!s.equals("POST")) {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    86
            ex.getResponseHeaders().set("Allow", "POST");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    87
            ex.sendResponseHeaders(500, -1);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    88
            ex.close();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    89
            return;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    90
        }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    91
        System.out.println ("Server: reading request body");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    92
        InputStream is = ex.getRequestBody();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    93
        // read request
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    94
        byte[] buf = new byte [1024];
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    95
        while (is.read(buf) != -1) ;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    96
        is.close();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    97
        ex.sendResponseHeaders(200, -1);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    98
        ex.close();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    99
   }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   100
}