blob: c4b45eb06ef5e701f10a233cf7d7ed0561b6bcef [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 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 6498566
27 * @run main/othervm ProxyFromCache
28 * @summary URL.openConnection(Proxy.NO_PROXY) may connect through a proxy.
29 */
30
31import java.net.*;
32import java.io.*;
33import sun.net.www.MessageHeader;
34
35/* Creates a simple proxy and http server that just return 200 OK.
36 * Open a URL pointing to the http server and specify that the
37 * connection should use the proxy. Now make a second connection
38 * to the same URL, specifying that no proxy is to be used.
39 * We count the amount of requests being sent to each server. There
40 * should be only one request sent to each.
41 */
42
43public class ProxyFromCache
44{
45 public static void main(String[] args) {
46 ServerSocket proxySSocket, httpSSocket;
47 int proxyPort, httpPort;
48
49 try {
50 proxySSocket = new ServerSocket(0);
51 proxyPort = proxySSocket.getLocalPort();
52 httpSSocket = new ServerSocket(0);
53 httpPort = httpSSocket.getLocalPort();
54 } catch (Exception e) {
55 System.out.println ("Exception: " + e);
56 return;
57 }
58
59 SimpleServer proxyServer = new SimpleServer(proxySSocket);
60 proxyServer.start();
61 SimpleServer httpServer = new SimpleServer(httpSSocket);
62 httpServer.start();
63
64 InetSocketAddress addr = new InetSocketAddress("localhost", proxyPort);
65 Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
66
67 try {
68 String urlStr = "http://localhost:" + httpPort + "/";
69 URL url = new URL(urlStr);
70
71 // 1st connection.
72 HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
73 InputStream is = uc.getInputStream();
74
75 byte[] ba = new byte[1024];
76 while(is.read(ba) != -1);
77 is.close();
78
79 // 2nd connection.
80 uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
81 is = uc.getInputStream();
82
83 while(is.read(ba) != -1);
84 is.close();
85
86 try {
87 proxySSocket.close();
88 httpSSocket.close();
89 } catch (IOException e) {}
90
91 proxyServer.terminate();
92 httpServer.terminate();
93
94 int httpCount = httpServer.getConnectionCount();
95 int proxyCount = proxyServer.getConnectionCount();
96
97 if (proxyCount != 1 && httpCount != 1) {
98 System.out.println("Proxy = " + proxyCount + ", http = " + httpCount);
99 throw new RuntimeException("Failed: Proxy being sent " + proxyCount + " requests");
100 }
101 } catch (IOException e) {
102 throw new RuntimeException(e);
103 }
104 }
105}
106
107class SimpleServer extends Thread
108{
109 private ServerSocket ss;
110 private Socket sock;
111 private int connectionCount;
112
113 String replyOK = "HTTP/1.1 200 OK\r\n" +
114 "Content-Length: 0\r\n\r\n";
115
116 public SimpleServer(ServerSocket ss) {
117 this.ss = ss;
118 }
119
120 public void run() {
121 try {
122 sock = ss.accept();
123 connectionCount++;
124 InputStream is = sock.getInputStream();
125 OutputStream os = sock.getOutputStream();
126
127 MessageHeader headers = new MessageHeader (is);
128 os.write(replyOK.getBytes("UTF-8"));
129
130 headers = new MessageHeader (is);
131 // If we get here then we received a second request.
132 connectionCount++;
133 os.write(replyOK.getBytes("UTF-8"));
134
135 sock.close();
136 } catch (Exception e) {
137 //e.printStackTrace();
138 if (sock != null && !sock.isClosed()) {
139 try { sock.close();
140 } catch (IOException ioe) {}
141 }
142 }
143 }
144
145 public int getConnectionCount() {
146 return connectionCount;
147 }
148
149 public void terminate() {
150 if (sock != null && !sock.isClosed()) {
151 try { sock.close();
152 } catch (IOException ioe) {}
153 }
154 }
155}