1/*
2 * Copyright (c) 2005, 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 */
29
30import com.sun.net.httpserver.*;
31
32import java.util.*;
33import java.util.concurrent.*;
34import java.io.*;
35import java.net.*;
36import java.security.*;
37import javax.security.auth.callback.*;
38import javax.net.ssl.*;
39
40/**
41 * Test POST large file via fixed len encoding
42 */
43
44public class Test8 extends Test {
45
46    public static void main (String[] args) throws Exception {
47        Handler handler = new Handler();
48        InetSocketAddress addr = new InetSocketAddress (0);
49        HttpServer server = HttpServer.create (addr, 0);
50        HttpContext ctx = server.createContext ("/test", handler);
51        ExecutorService executor = Executors.newCachedThreadPool();
52        server.setExecutor (executor);
53        server.start ();
54
55        URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
56        System.out.print ("Test8: " );
57        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
58        urlc.setDoOutput (true);
59        urlc.setRequestMethod ("POST");
60        OutputStream os = new BufferedOutputStream (urlc.getOutputStream());
61        for (int i=0; i<SIZE; i++) {
62            os.write (i % 100);
63        }
64        os.close();
65        int resp = urlc.getResponseCode();
66        if (resp != 200) {
67            throw new RuntimeException ("test failed response code");
68        }
69        if (error) {
70            throw new RuntimeException ("test failed error");
71        }
72        delay();
73        server.stop(2);
74        executor.shutdown();
75        System.out.println ("OK");
76
77    }
78
79    public static boolean error = false;
80    final static int SIZE = 999999;
81
82    static class Handler implements HttpHandler {
83        int invocation = 1;
84        public void handle (HttpExchange t)
85            throws IOException
86        {
87            InputStream is = t.getRequestBody();
88            Headers map = t.getRequestHeaders();
89            Headers rmap = t.getResponseHeaders();
90            int c, count=0;
91            while ((c=is.read ()) != -1) {
92                if (c != (count % 100)) {
93                    error = true;
94                    break;
95                }
96                count ++;
97            }
98            if (count != SIZE) {
99                error = true;
100            }
101            if (!t.getRequestMethod().equals ("POST")) {
102                error = true;
103            }
104            is.close();
105            t.sendResponseHeaders (200, -1);
106            t.close();
107        }
108    }
109}
110