jdk/test/com/sun/net/httpserver/Test5.java
author jjg
Tue, 08 Sep 2009 11:43:57 -0700
changeset 3784 182e4a28c0ce
parent 3070 0175bbf9a833
child 5506 202f599c92aa
permissions -rw-r--r--
6879371: javap does not close internal default file manager Reviewed-by: darcy
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 2005-2006 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 6270015
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary  Light weight HTTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/othervm -Dsun.net.httpserver.idleInterval=4 Test5
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 com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Test pipe-lining (no block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class Test5 extends Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static int count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        System.out.print ("Test5: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Handler handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        InetSocketAddress addr = new InetSocketAddress (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        HttpServer server = HttpServer.create (addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        int port = server.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        HttpContext c2 = server.createContext ("/test", handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        c2.getAttributes().put ("name", "This is the http handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ExecutorService exec = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        server.setExecutor (exec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            doClient(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.out.println ("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            delay ();
3070
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    64
            if (server != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    65
                server.stop(2);
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    66
            if (exec != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    67
                exec.shutdown();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static class Handler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        volatile int invocation = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        public void handle (HttpExchange t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            Headers map = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            Headers rmap = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            int x = invocation ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            rmap.set ("XTest", Integer.toString (x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            switch (x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                checkBody (is, body1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                checkBody (is, body2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                checkBody (is, body3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                checkBody (is, body4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            t.sendResponseHeaders (200, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static void checkBody (InputStream is, String cmp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        byte [] b = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int count = 0, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        while ((c=is.read(b, count, b.length-count)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            count+=c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        String s = new String (b, 0, count, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (!s.equals (cmp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new RuntimeException ("strings not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static String body1 = "1234567890abcdefghij";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static String body2 = "2234567890abcdefghij0123456789";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static String body3 = "3wertyuiop";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static String body4 = "4234567890";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static String result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        "HTTP/1.1 200 OK.*Xtest: 0.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        "HTTP/1.1 200 OK.*Xtest: 1.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        "HTTP/1.1 200 OK.*Xtest: 2.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        "HTTP/1.1 200 OK.*Xtest: 3.*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static void doClient (int port) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        "\r\n" +body1 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        "GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        "\r\n"+ body2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        "GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        "\r\n"+ body3 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        "GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        "\r\n"+body4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Socket socket = new Socket ("localhost", port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        OutputStream os = socket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        os.write (s.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        InputStream is = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int c, count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        byte[] b = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        while ((c=is.read(b, count, b.length-count)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            count +=c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        s = new String (b,0,count, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (!compare (s, result)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new RuntimeException ("wrong string result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static boolean compare (String s, String result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Pattern pattern = Pattern.compile (result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                Pattern.DOTALL|Pattern.CASE_INSENSITIVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Matcher matcher = pattern.matcher (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return matcher.matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}