blob: becdd38284002a917552067004656b95597a5a2e [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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.util.logging.*;
35import java.io.*;
36import java.net.*;
37import java.security.*;
38import javax.security.auth.callback.*;
39import javax.net.ssl.*;
40
41/**
42 * Test POST large file via fixed len encoding
43 */
44
45public class Test8a extends Test {
46
47 public static void main (String[] args) throws Exception {
48 //Logger log = Logger.getLogger ("com.sun.net.httpserver");
49 //ConsoleHandler h = new ConsoleHandler();
50 //h.setLevel (Level.INFO);
51 //log.addHandler (h);
52 //log.setLevel (Level.INFO);
53 Handler handler = new Handler();
54 InetSocketAddress addr = new InetSocketAddress (0);
55 HttpsServer server = HttpsServer.create (addr, 0);
56 HttpContext ctx = server.createContext ("/test", handler);
57 ExecutorService executor = Executors.newCachedThreadPool();
58 SSLContext ssl = new SimpleSSLContext(System.getProperty("test.src")).get();
59 server.setHttpsConfigurator(new HttpsConfigurator (ssl));
60 server.setExecutor (executor);
61 server.start ();
62
63 URL url = new URL ("https://localhost:"+server.getAddress().getPort()+"/test/foo.html");
64 System.out.print ("Test8a: " );
65 HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection ();
66 urlc.setDoOutput (true);
67 urlc.setRequestMethod ("POST");
68 urlc.setHostnameVerifier (new DummyVerifier());
69 urlc.setSSLSocketFactory (ssl.getSocketFactory());
70 OutputStream os = new BufferedOutputStream (urlc.getOutputStream(), 8000);
71 for (int i=0; i<SIZE; i++) {
72 os.write (i % 250);
73 }
74 os.close();
75 int resp = urlc.getResponseCode();
76 if (resp != 200) {
77 throw new RuntimeException ("test failed response code");
78 }
79 InputStream is = urlc.getInputStream ();
80 for (int i=0; i<SIZE; i++) {
81 int f = is.read();
82 if (f != (i % 250)) {
83 System.out.println ("Setting error(" +f +")("+i+")" );
84 error = true;
85 break;
86 }
87 }
88 is.close();
89
90 delay();
91 server.stop(2);
92 executor.shutdown();
93 if (error) {
94 throw new RuntimeException ("test failed error");
95 }
96 System.out.println ("OK");
97
98 }
99
100 public static boolean error = false;
101 //final static int SIZE = 999999;
102 final static int SIZE = 9999;
103
104 static class Handler implements HttpHandler {
105 int invocation = 1;
106 public void handle (HttpExchange t)
107 throws IOException
108 {
109 System.out.println ("Handler.handle");
110 InputStream is = t.getRequestBody();
111 Headers map = t.getRequestHeaders();
112 Headers rmap = t.getResponseHeaders();
113 int c, count=0;
114 while ((c=is.read ()) != -1) {
115 if (c != (count % 250)) {
116 System.out.println ("Setting error 1");
117 error = true;
118 break;
119 }
120 count ++;
121 }
122 if (count != SIZE) {
123 System.out.println ("Setting error 2");
124 error = true;
125 }
126 is.close();
127 t.sendResponseHeaders (200, SIZE);
128 System.out.println ("Sending 200 OK");
129 OutputStream os = new BufferedOutputStream(t.getResponseBody(), 8000);
130 for (int i=0; i<SIZE; i++) {
131 os.write (i % 250);
132 }
133 os.close();
134 System.out.println ("Finished");
135 }
136 }
137}