2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6393710
|
|
27 |
* @summary Non authenticated call followed by authenticated call never returns
|
|
28 |
*/
|
|
29 |
|
|
30 |
import com.sun.net.httpserver.*;
|
|
31 |
|
|
32 |
import java.util.*;
|
|
33 |
import java.util.concurrent.*;
|
|
34 |
import java.io.*;
|
|
35 |
import java.net.*;
|
|
36 |
|
|
37 |
/*
|
|
38 |
* Test checks for following bug(s) when a POST containing a request body
|
|
39 |
* needs to be authenticated
|
|
40 |
*
|
|
41 |
* 1) we were not reading the request body
|
|
42 |
*
|
|
43 |
* 2) we were not re-enabling the interestops for the socket channel
|
|
44 |
*/
|
|
45 |
|
|
46 |
public class B6393710 {
|
|
47 |
|
|
48 |
static String CRLF = "\r\n";
|
|
49 |
|
|
50 |
/* Two post requests containing data. The second one
|
|
51 |
* has the expected authorization credentials
|
|
52 |
*/
|
|
53 |
static String cmd =
|
|
54 |
"POST /test/foo HTTP/1.1"+CRLF+
|
|
55 |
"Content-Length: 22"+CRLF+
|
|
56 |
"Pragma: no-cache"+CRLF+
|
|
57 |
"Cache-Control: no-cache"+CRLF+ CRLF+
|
|
58 |
"<item desc=\"excuse\" />"+
|
|
59 |
"POST /test/foo HTTP/1.1"+CRLF+
|
|
60 |
"Content-Length: 22"+CRLF+
|
|
61 |
"Pragma: no-cache"+CRLF+
|
|
62 |
"Authorization: Basic ZnJlZDpmcmVkcGFzc3dvcmQ="+CRLF+
|
|
63 |
"Cache-Control: no-cache"+CRLF+ CRLF+
|
|
64 |
"<item desc=\"excuse\" />";
|
|
65 |
|
|
66 |
public static void main (String[] args) throws Exception {
|
|
67 |
Handler handler = new Handler();
|
|
68 |
InetSocketAddress addr = new InetSocketAddress (0);
|
|
69 |
HttpServer server = HttpServer.create (addr, 0);
|
|
70 |
HttpContext ctx = server.createContext ("/test", handler);
|
|
71 |
ctx.setAuthenticator (new BasicAuthenticator ("test") {
|
|
72 |
public boolean checkCredentials (String user, String pass) {
|
|
73 |
return user.equals ("fred") && pass.equals("fredpassword");
|
|
74 |
}
|
|
75 |
});
|
|
76 |
|
|
77 |
server.start ();
|
|
78 |
|
|
79 |
Socket s = new Socket ("localhost", server.getAddress().getPort());
|
|
80 |
s.setSoTimeout (5000);
|
|
81 |
|
|
82 |
OutputStream os = s.getOutputStream();
|
|
83 |
os.write (cmd.getBytes());
|
|
84 |
InputStream is = s.getInputStream ();
|
|
85 |
try {
|
|
86 |
ok = readAndCheck (is, "401 Unauthorized") &&
|
|
87 |
readAndCheck (is, "200 OK");
|
|
88 |
} catch (SocketTimeoutException e) {
|
|
89 |
System.out.println ("Did not received expected data");
|
|
90 |
ok = false;
|
|
91 |
} finally {
|
|
92 |
s.close();
|
|
93 |
server.stop(2);
|
|
94 |
}
|
|
95 |
|
|
96 |
if (requests != 1) {
|
|
97 |
throw new RuntimeException ("server handler did not receive the request");
|
|
98 |
}
|
|
99 |
if (!ok) {
|
|
100 |
throw new RuntimeException ("did not get 200 OK");
|
|
101 |
}
|
|
102 |
System.out.println ("OK");
|
|
103 |
}
|
|
104 |
|
|
105 |
/* check for expected string and return true if found in stream */
|
|
106 |
|
|
107 |
static boolean readAndCheck (InputStream is, String expected) throws IOException {
|
|
108 |
int c;
|
|
109 |
int count = 0;
|
|
110 |
int expLen = expected.length();
|
|
111 |
expected = expected.toLowerCase();
|
|
112 |
|
|
113 |
while ((c=is.read()) != -1) {
|
|
114 |
c = Character.toLowerCase (c);
|
|
115 |
if (c == expected.charAt (count)) {
|
|
116 |
count ++;
|
|
117 |
if (count == expLen) {
|
|
118 |
return true;
|
|
119 |
}
|
|
120 |
} else {
|
|
121 |
count = 0;
|
|
122 |
}
|
|
123 |
}
|
|
124 |
return false;
|
|
125 |
}
|
|
126 |
|
|
127 |
public static boolean ok = false;
|
|
128 |
static int requests = 0;
|
|
129 |
|
|
130 |
static class Handler implements HttpHandler {
|
|
131 |
int invocation = 1;
|
|
132 |
public void handle (HttpExchange t)
|
|
133 |
throws IOException
|
|
134 |
{
|
|
135 |
int count = 0;
|
|
136 |
InputStream is = t.getRequestBody();
|
|
137 |
Headers map = t.getRequestHeaders();
|
|
138 |
Headers rmap = t.getResponseHeaders();
|
|
139 |
while (is.read () != -1) {
|
|
140 |
count ++;
|
|
141 |
}
|
|
142 |
if (count != 22) {
|
|
143 |
System.out.println ("Handler expected 22. got " + count);
|
|
144 |
ok = false;
|
|
145 |
}
|
|
146 |
is.close();
|
|
147 |
t.sendResponseHeaders (200, -1);
|
|
148 |
t.close();
|
|
149 |
requests ++;
|
|
150 |
}
|
|
151 |
}
|
|
152 |
}
|