2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2002, 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 |
import java.io.*;
|
|
25 |
import java.net.*;
|
|
26 |
import java.util.*;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* @test
|
|
30 |
* @bug 4623722
|
|
31 |
* @summary performance hit for Basic Authentication
|
|
32 |
*/
|
|
33 |
|
|
34 |
public class BasicTest4 {
|
|
35 |
|
|
36 |
static class BasicServer extends Thread {
|
|
37 |
|
|
38 |
ServerSocket server;
|
|
39 |
|
|
40 |
Socket s;
|
|
41 |
InputStream is;
|
|
42 |
OutputStream os;
|
|
43 |
|
|
44 |
static final String realm = "wallyworld";
|
|
45 |
|
|
46 |
String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+
|
|
47 |
"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";
|
|
48 |
|
|
49 |
String reply2 = "HTTP/1.1 200 OK\r\n"+
|
|
50 |
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
|
|
51 |
"Server: Apache/1.3.14 (Unix)\r\n" +
|
|
52 |
"Connection: close\r\n" +
|
|
53 |
"Content-Type: text/html; charset=iso-8859-1\r\n" +
|
|
54 |
"Content-Length: 10\r\n\r\n";
|
|
55 |
|
|
56 |
BasicServer (ServerSocket s) {
|
|
57 |
server = s;
|
|
58 |
}
|
|
59 |
|
|
60 |
static boolean checkFor (InputStream in, char[] seq) throws IOException {
|
|
61 |
System.out.println ("checkfor");
|
|
62 |
try {
|
|
63 |
int i=0, count=0;
|
|
64 |
while (true) {
|
|
65 |
int c = in.read();
|
|
66 |
if (c == -1)
|
|
67 |
return false;
|
|
68 |
count++;
|
|
69 |
if (c == seq[i]) {
|
|
70 |
i++;
|
|
71 |
if (i == seq.length)
|
|
72 |
return true;
|
|
73 |
continue;
|
|
74 |
} else {
|
|
75 |
i = 0;
|
|
76 |
}
|
|
77 |
}
|
|
78 |
}
|
|
79 |
catch (SocketTimeoutException e) {
|
|
80 |
return false;
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
boolean success = false;
|
|
85 |
|
|
86 |
void readAll (Socket s) throws IOException {
|
|
87 |
byte[] buf = new byte [128];
|
|
88 |
InputStream is = s.getInputStream ();
|
|
89 |
s.setSoTimeout(1000);
|
|
90 |
try {
|
|
91 |
while (is.read(buf) > 0) ;
|
|
92 |
} catch (SocketTimeoutException x) { }
|
|
93 |
}
|
|
94 |
|
|
95 |
public void run () {
|
|
96 |
try {
|
|
97 |
System.out.println ("Server 1: accept");
|
|
98 |
s = server.accept ();
|
|
99 |
readAll (s);
|
|
100 |
System.out.println ("accepted");
|
|
101 |
os = s.getOutputStream();
|
|
102 |
os.write (reply1.getBytes());
|
|
103 |
s.close ();
|
|
104 |
|
|
105 |
System.out.println ("Server 2: accept");
|
|
106 |
s = server.accept ();
|
|
107 |
readAll (s);
|
|
108 |
System.out.println ("accepted");
|
|
109 |
os = s.getOutputStream();
|
|
110 |
os.write ((reply2+"HelloWorld").getBytes());
|
|
111 |
s.close ();
|
|
112 |
|
|
113 |
/* Second request now */
|
|
114 |
|
|
115 |
System.out.println ("Server 3: accept");
|
|
116 |
s = server.accept ();
|
|
117 |
readAll (s);
|
|
118 |
System.out.println ("accepted");
|
|
119 |
os = s.getOutputStream();
|
|
120 |
os.write (reply1.getBytes());
|
|
121 |
s.close ();
|
|
122 |
|
|
123 |
System.out.println ("Server 4: accept");
|
|
124 |
s = server.accept ();
|
|
125 |
readAll (s);
|
|
126 |
System.out.println ("accepted");
|
|
127 |
os = s.getOutputStream();
|
|
128 |
os.write ((reply2+"HelloAgain").getBytes());
|
|
129 |
s.close ();
|
|
130 |
|
|
131 |
/* Third request now */
|
|
132 |
|
|
133 |
/* This should include pre-emptive authorization */
|
|
134 |
|
|
135 |
System.out.println ("Server 5: accept");
|
|
136 |
s = server.accept ();
|
|
137 |
s.setSoTimeout (1000);
|
|
138 |
System.out.println ("accepted");
|
|
139 |
InputStream is = s.getInputStream ();
|
|
140 |
success = checkFor (is, "Authorization".toCharArray());
|
|
141 |
System.out.println ("checkfor returned " + success);
|
|
142 |
readAll (s);
|
|
143 |
os = s.getOutputStream();
|
|
144 |
os.write (reply2.getBytes());
|
|
145 |
s.close ();
|
|
146 |
|
|
147 |
if (success)
|
|
148 |
return;
|
|
149 |
|
|
150 |
System.out.println ("Server 6: accept");
|
|
151 |
s = server.accept ();
|
|
152 |
System.out.println ("accepted");
|
|
153 |
os = s.getOutputStream();
|
|
154 |
readAll (s);
|
|
155 |
os.write ((reply2+"HelloAgain").getBytes());
|
|
156 |
s.close ();
|
|
157 |
}
|
|
158 |
catch (Exception e) {
|
|
159 |
System.out.println (e);
|
|
160 |
}
|
|
161 |
finished ();
|
|
162 |
}
|
|
163 |
|
|
164 |
public synchronized void finished () {
|
|
165 |
notifyAll();
|
|
166 |
}
|
|
167 |
|
|
168 |
}
|
|
169 |
|
|
170 |
static class MyAuthenticator extends Authenticator {
|
|
171 |
MyAuthenticator () {
|
|
172 |
super ();
|
|
173 |
}
|
|
174 |
|
|
175 |
public PasswordAuthentication getPasswordAuthentication ()
|
|
176 |
{
|
|
177 |
System.out.println ("Auth called");
|
|
178 |
return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
|
|
179 |
}
|
|
180 |
|
|
181 |
}
|
|
182 |
|
|
183 |
|
|
184 |
static void read (InputStream is) throws IOException {
|
|
185 |
int c;
|
|
186 |
System.out.println ("reading");
|
|
187 |
while ((c=is.read()) != -1) {
|
|
188 |
System.out.write (c);
|
|
189 |
}
|
|
190 |
System.out.println ("");
|
|
191 |
System.out.println ("finished reading");
|
|
192 |
}
|
|
193 |
|
|
194 |
public static void main (String args[]) throws Exception {
|
|
195 |
MyAuthenticator auth = new MyAuthenticator ();
|
|
196 |
Authenticator.setDefault (auth);
|
|
197 |
ServerSocket ss = new ServerSocket (0);
|
|
198 |
int port = ss.getLocalPort ();
|
|
199 |
BasicServer server = new BasicServer (ss);
|
|
200 |
synchronized (server) {
|
|
201 |
server.start();
|
|
202 |
System.out.println ("client 1");
|
|
203 |
URL url = new URL ("http://localhost:"+port+"/d1/d3/foo.html");
|
|
204 |
URLConnection urlc = url.openConnection ();
|
|
205 |
InputStream is = urlc.getInputStream ();
|
|
206 |
read (is);
|
|
207 |
System.out.println ("client 2");
|
|
208 |
url = new URL ("http://localhost:"+port+"/d1/d2/bar.html");
|
|
209 |
urlc = url.openConnection ();
|
|
210 |
is = urlc.getInputStream ();
|
|
211 |
System.out.println ("client 3");
|
|
212 |
url = new URL ("http://localhost:"+port+"/d1/d4/foobar.html");
|
|
213 |
urlc = url.openConnection ();
|
|
214 |
is = urlc.getInputStream ();
|
|
215 |
read (is);
|
|
216 |
server.wait ();
|
|
217 |
if (!server.success) {
|
|
218 |
throw new RuntimeException ("3rd request did not use pre-emptive authorization");
|
|
219 |
}
|
|
220 |
}
|
|
221 |
}
|
|
222 |
}
|