jdk/test/com/sun/net/httpserver/Test4.java
author xdono
Thu, 09 Apr 2009 10:37:22 -0700
changeset 2504 0734512b1faf
parent 2 90ce3da70b43
child 3070 0175bbf9a833
permissions -rw-r--r--
Added tag jdk7-b54 for changeset 31bec7a68275
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 Test4
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 (block after read)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class Test4 extends Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static int count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        System.out.print ("Test4: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        Handler handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        InetSocketAddress addr = new InetSocketAddress (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        HttpServer server = HttpServer.create (addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        int port = server.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        HttpContext c2 = server.createContext ("/test", handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        c2.getAttributes().put ("name", "This is the http handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ExecutorService exec = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        server.setExecutor (exec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            doClient(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.out.println ("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            delay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            server.stop(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            exec.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static class Handler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        volatile int invocation = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        public void handle (HttpExchange t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            Headers map = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            Headers rmap = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            int x = invocation ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            rmap.set ("XTest", Integer.toString (x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            switch (x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                checkBody (is, body1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                try {Thread.sleep (2000); } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                checkBody (is, body2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                try {Thread.sleep (1000); } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                checkBody (is, body3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                checkBody (is, body4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            t.sendResponseHeaders (200, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static void checkBody (InputStream is, String cmp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        byte [] b = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int count = 0, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        while ((c=is.read(b, count, b.length-count)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            count+=c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        String s = new String (b, 0, count, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (!s.equals (cmp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new RuntimeException ("strings not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static String body1 = "1234567890abcdefghij";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static String body2 = "2234567890abcdefghij0123456789";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static String body3 = "3wertyuiop";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static String body4 = "4234567890";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static String result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        "HTTP/1.1 200 OK.*Xtest: 0.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        "HTTP/1.1 200 OK.*Xtest: 1.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        "HTTP/1.1 200 OK.*Xtest: 2.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        "HTTP/1.1 200 OK.*Xtest: 3.*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static void doClient (int port) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        "\r\n" +body1 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        "GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        "\r\n"+ body2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        "GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        "\r\n"+ body3 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        "GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        "\r\n"+body4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Socket socket = new Socket ("localhost", port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        OutputStream os = socket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        os.write (s.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        InputStream is = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int c, count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        byte[] b = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        while ((c=is.read(b, count, b.length-count)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            count +=c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        s = new String (b,0,count, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (!compare (s, result)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new RuntimeException ("wrong string result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static boolean compare (String s, String result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Pattern pattern = Pattern.compile (result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                Pattern.DOTALL|Pattern.CASE_INSENSITIVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Matcher matcher = pattern.matcher (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return matcher.matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}