1/*
2 * Copyright (c) 2007, 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 6529200
27 * @run main/othervm B6529200
28 * @summary  lightweight http server does not work with http1.0 clients
29 */
30
31import com.sun.net.httpserver.*;
32
33import java.util.*;
34import java.util.concurrent.*;
35import java.io.*;
36import java.net.*;
37import java.security.*;
38import java.security.cert.*;
39import javax.net.ssl.*;
40
41public class B6529200 {
42
43    public static void main (String[] args) throws Exception {
44        Handler handler = new Handler();
45        InetSocketAddress addr = new InetSocketAddress (0);
46        HttpServer server = HttpServer.create (addr, 0);
47        HttpContext ctx = server.createContext ("/test", handler);
48
49        ExecutorService executor = Executors.newCachedThreadPool();
50        server.setExecutor (executor);
51        server.start ();
52
53        /* test 1: keep-alive */
54
55        Socket sock = new Socket ("localhost", server.getAddress().getPort());
56        OutputStream os = sock.getOutputStream();
57        System.out.println ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");
58        os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());
59        os.flush();
60        InputStream is = sock.getInputStream();
61        StringBuffer s = new StringBuffer();
62        boolean finished = false;
63
64        sock.setSoTimeout (10 * 1000);
65        try {
66            while (!finished) {
67                char c = (char) is.read();
68                s.append (c);
69                finished = s.indexOf ("\r\n\r\nhello") != -1;
70                /* test will timeout otherwise */
71            }
72        } catch (SocketTimeoutException e) {
73            server.stop (2);
74            executor.shutdown ();
75            throw new RuntimeException ("Test failed in test1");
76        }
77
78        System.out.println (new String (s));
79
80        /* test 2: even though we request keep-alive, server must close
81         * because it is sending unknown content length response */
82
83        System.out.println("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");
84        os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());
85        os.flush();
86        int i=0,c;
87        byte [] buf = new byte [8*1024];
88        try {
89            while ((c=is.read()) != -1) {
90                buf[i++] = (byte)c;
91            }
92        } catch (SocketTimeoutException e) {
93            server.stop (2);
94            executor.shutdown ();
95            throw new RuntimeException ("Test failed in test2");
96        }
97
98        String ss = new String (buf, "ISO-8859-1");
99        if (ss.indexOf ("\r\n\r\nhello world") == -1) {
100            server.stop (2);
101            executor.shutdown ();
102            throw new RuntimeException ("Test failed in test2: wrong string");
103        }
104        System.out.println (ss);
105        is.close ();
106        server.stop (2);
107        executor.shutdown();
108    }
109
110
111    static class Handler implements HttpHandler {
112        int invocation = 1;
113        public void handle (HttpExchange t)
114            throws IOException
115        {
116            InputStream is;
117            OutputStream os;
118            switch (invocation++) {
119              case 1:
120                is = t.getRequestBody();
121                while (is.read() != -1) ;
122                is.close();
123                t.sendResponseHeaders (200, "hello".length());
124                os = t.getResponseBody();
125                os.write ("hello".getBytes());
126                os.close();
127                break;
128              case 2:
129                is = t.getRequestBody();
130                while (is.read() != -1) ;
131                is.close();
132                t.sendResponseHeaders (200, 0);
133                os = t.getResponseBody();
134                os.write ("hello world".getBytes());
135                os.close();
136                break;
137            }
138        }
139    }
140}
141