1/*
2 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 6270015
27 * @summary  Light weight HTTP server
28 * @run main/othervm -Dsun.net.httpserver.idleInterval=4 Test4
29 */
30
31import com.sun.net.httpserver.*;
32
33import java.util.*;
34import java.util.concurrent.*;
35import java.util.regex.*;
36import java.io.*;
37import java.net.*;
38import java.security.*;
39import javax.net.ssl.*;
40
41/**
42 * Test pipe-lining (block after read)
43 */
44public class Test4 extends Test {
45    static int count = 1;
46    public static void main (String[] args) throws Exception {
47        System.out.print ("Test4: ");
48        Handler handler = new Handler();
49        InetSocketAddress addr = new InetSocketAddress (0);
50        HttpServer server = HttpServer.create (addr, 0);
51        int port = server.getAddress().getPort();
52        HttpContext c2 = server.createContext ("/test", handler);
53        c2.getAttributes().put ("name", "This is the http handler");
54
55        ExecutorService exec = Executors.newCachedThreadPool();
56        server.setExecutor (exec);
57        try {
58            server.start ();
59            doClient(port);
60            System.out.println ("OK");
61        } finally {
62            delay();
63            if (server != null)
64                server.stop(2);
65            if (exec != null)
66                exec.shutdown();
67        }
68    }
69
70    static class Handler implements HttpHandler {
71        volatile int invocation = 0;
72        public void handle (HttpExchange t)
73            throws IOException
74        {
75            InputStream is = t.getRequestBody();
76            Headers map = t.getRequestHeaders();
77            Headers rmap = t.getResponseHeaders();
78            int x = invocation ++;
79            rmap.set ("XTest", Integer.toString (x));
80
81            switch (x) {
82            case 0:
83                checkBody (is, body1);
84                try {Thread.sleep (2000); } catch (Exception e) {}
85                break;
86            case 1:
87                checkBody (is, body2);
88                try {Thread.sleep (1000); } catch (Exception e) {}
89                break;
90            case 2:
91                checkBody (is, body3);
92                break;
93            case 3:
94                checkBody (is, body4);
95                break;
96            }
97            t.sendResponseHeaders (200, -1);
98            t.close();
99        }
100    }
101
102    static void checkBody (InputStream is, String cmp) throws IOException {
103        byte [] b = new byte [1024];
104        int count = 0, c;
105        while ((c=is.read(b, count, b.length-count)) != -1) {
106            count+=c;
107        }
108        is.close();
109        String s = new String (b, 0, count, "ISO8859_1");
110        if (!s.equals (cmp)) {
111            throw new RuntimeException ("strings not equal");
112        }
113    }
114
115    static String body1 = "1234567890abcdefghij";
116    static String body2 = "2234567890abcdefghij0123456789";
117    static String body3 = "3wertyuiop";
118    static String body4 = "4234567890";
119
120    static String result =
121        "HTTP/1.1 200 OK.*Xtest: 0.*"+
122        "HTTP/1.1 200 OK.*Xtest: 1.*"+
123        "HTTP/1.1 200 OK.*Xtest: 2.*"+
124        "HTTP/1.1 200 OK.*Xtest: 3.*";
125
126    public static void doClient (int port) throws Exception {
127        String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+
128        "\r\n" +body1 +
129        "GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+
130        "\r\n"+ body2 +
131        "GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+
132        "\r\n"+ body3 +
133        "GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+
134        "\r\n"+body4;
135
136        Socket socket = new Socket ("localhost", port);
137        OutputStream os = socket.getOutputStream();
138        os.write (s.getBytes());
139        InputStream is = socket.getInputStream();
140        int c, count=0;
141        byte[] b = new byte [1024];
142        while ((c=is.read(b, count, b.length-count)) != -1) {
143            count +=c;
144        }
145        is.close();
146        socket.close();
147        s = new String (b,0,count, "ISO8859_1");
148        if (!compare (s, result)) {
149            throw new RuntimeException ("wrong string result");
150        }
151    }
152
153    static boolean compare (String s, String result) {
154        Pattern pattern = Pattern.compile (result,
155                Pattern.DOTALL|Pattern.CASE_INSENSITIVE
156        );
157        Matcher matcher = pattern.matcher (s);
158        return matcher.matches();
159    }
160
161}
162