J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 5017051 6360774 |
| 27 | * @run main/othervm B5017051 |
| 28 | * @summary Tests CR 5017051 & 6360774 |
| 29 | */ |
| 30 | |
| 31 | import java.net.*; |
| 32 | import java.util.*; |
| 33 | import java.io.*; |
| 34 | import com.sun.net.httpserver.*; |
| 35 | import java.util.concurrent.Executors; |
| 36 | import java.util.concurrent.ExecutorService; |
| 37 | |
| 38 | /* |
| 39 | * Part 1: |
| 40 | * First request sent to the http server will not have an "Authorization" header set and |
| 41 | * the server will respond with a 401, but not until it has set a cookie in the response |
| 42 | * headers. The subsequent request ( comes from HttpURLConnection's authentication retry ) |
| 43 | * will have the appropriate Authorization header and the servers context handler will be |
| 44 | * invoked. The test passes only if the client (HttpURLConnection) has sent the cookie |
| 45 | * in its second request that had been set via the first response from the server. |
| 46 | * |
| 47 | * Part 2: |
| 48 | * Preload the CookieManager with a cookie. Make a http request that requires authentication |
| 49 | * The cookie will be sent in the first request (without the Authorization header), the |
| 50 | * server will respond with a 401 (from MyBasicAuthFilter) and the client will add the |
| 51 | * appropriate Authorization header. This tests ensures that there is only one Cookie header |
| 52 | * in the request that actually makes it to the Http servers context handler. |
| 53 | */ |
| 54 | |
| 55 | public class B5017051 |
| 56 | { |
| 57 | com.sun.net.httpserver.HttpServer httpServer; |
| 58 | ExecutorService executorService; |
| 59 | |
| 60 | public static void main(String[] args) |
| 61 | { |
| 62 | new B5017051(); |
| 63 | } |
| 64 | |
| 65 | public B5017051() |
| 66 | { |
| 67 | try { |
| 68 | startHttpServer(); |
| 69 | doClient(); |
| 70 | } catch (IOException ioe) { |
| 71 | System.err.println(ioe); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void doClient() { |
| 76 | java.net.Authenticator.setDefault(new MyAuthenticator()); |
| 77 | CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); |
| 78 | |
| 79 | try { |
| 80 | InetSocketAddress address = httpServer.getAddress(); |
| 81 | |
| 82 | // Part 1 |
| 83 | URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/"); |
| 84 | HttpURLConnection uc = (HttpURLConnection)url.openConnection(); |
| 85 | int resp = uc.getResponseCode(); |
| 86 | if (resp != 200) |
| 87 | throw new RuntimeException("Failed: Part 1, Response code is not 200"); |
| 88 | |
| 89 | System.out.println("Response code from Part 1 = 200 OK"); |
| 90 | |
| 91 | // Part 2 |
| 92 | URL url2 = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test2/"); |
| 93 | |
| 94 | // can use the global CookieHandler used for the first test as the URL's are different |
| 95 | CookieHandler ch = CookieHandler.getDefault(); |
| 96 | Map<String,List<String>> header = new HashMap<String,List<String>>(); |
| 97 | List<String> values = new LinkedList<String>(); |
| 98 | values.add("Test2Cookie=\"TEST2\"; path=\"/test2/\""); |
| 99 | header.put("Set-Cookie2", values); |
| 100 | |
| 101 | // preload the CookieHandler with a cookie for our URL |
| 102 | // so that it will be sent during the first request |
| 103 | ch.put(url2.toURI(), header); |
| 104 | |
| 105 | uc = (HttpURLConnection)url2.openConnection(); |
| 106 | resp = uc.getResponseCode(); |
| 107 | if (resp != 200) |
| 108 | throw new RuntimeException("Failed: Part 2, Response code is not 200"); |
| 109 | |
| 110 | System.out.println("Response code from Part 2 = 200 OK"); |
| 111 | |
| 112 | |
| 113 | } catch (IOException e) { |
| 114 | e.printStackTrace(); |
| 115 | } catch (URISyntaxException ue) { |
| 116 | ue.printStackTrace(); |
| 117 | } finally { |
| 118 | httpServer.stop(1); |
| 119 | executorService.shutdown(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Http Server |
| 125 | */ |
| 126 | public void startHttpServer() throws IOException { |
| 127 | httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); |
| 128 | |
| 129 | // create HttpServer context for Part 1. |
| 130 | HttpContext ctx = httpServer.createContext("/test/", new MyHandler()); |
| 131 | ctx.setAuthenticator( new MyBasicAuthenticator("foo")); |
| 132 | // CookieFilter needs to be executed before Authenticator. |
| 133 | ctx.getFilters().add(0, new CookieFilter()); |
| 134 | |
| 135 | // create HttpServer context for Part 2. |
| 136 | HttpContext ctx2 = httpServer.createContext("/test2/", new MyHandler2()); |
| 137 | ctx2.setAuthenticator( new MyBasicAuthenticator("foobar")); |
| 138 | |
| 139 | executorService = Executors.newCachedThreadPool(); |
| 140 | httpServer.setExecutor(executorService); |
| 141 | httpServer.start(); |
| 142 | } |
| 143 | |
| 144 | class MyHandler implements HttpHandler { |
| 145 | public void handle(HttpExchange t) throws IOException { |
| 146 | InputStream is = t.getRequestBody(); |
| 147 | Headers reqHeaders = t.getRequestHeaders(); |
| 148 | Headers resHeaders = t.getResponseHeaders(); |
| 149 | while (is.read () != -1) ; |
| 150 | is.close(); |
| 151 | |
| 152 | if (!reqHeaders.containsKey("Authorization")) |
| 153 | t.sendResponseHeaders(400, -1); |
| 154 | |
| 155 | List<String> cookies = reqHeaders.get("Cookie"); |
| 156 | if (cookies != null) { |
| 157 | for (String str : cookies) { |
| 158 | if (str.equals("Customer=WILE_E_COYOTE")) |
| 159 | t.sendResponseHeaders(200, -1); |
| 160 | } |
| 161 | } |
| 162 | t.sendResponseHeaders(400, -1); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | class MyHandler2 implements HttpHandler { |
| 167 | public void handle(HttpExchange t) throws IOException { |
| 168 | InputStream is = t.getRequestBody(); |
| 169 | Headers reqHeaders = t.getRequestHeaders(); |
| 170 | Headers resHeaders = t.getResponseHeaders(); |
| 171 | while (is.read () != -1) ; |
| 172 | is.close(); |
| 173 | |
| 174 | if (!reqHeaders.containsKey("Authorization")) |
| 175 | t.sendResponseHeaders(400, -1); |
| 176 | |
| 177 | List<String> cookies = reqHeaders.get("Cookie"); |
| 178 | |
| 179 | // there should only be one Cookie header |
| 180 | if (cookies != null && (cookies.size() == 1)) { |
| 181 | t.sendResponseHeaders(200, -1); |
| 182 | } |
| 183 | t.sendResponseHeaders(400, -1); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | class MyAuthenticator extends java.net.Authenticator { |
| 188 | public PasswordAuthentication getPasswordAuthentication () { |
| 189 | return new PasswordAuthentication("tester", "passwd".toCharArray()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | class MyBasicAuthenticator extends BasicAuthenticator |
| 194 | { |
| 195 | public MyBasicAuthenticator(String realm) { |
| 196 | super(realm); |
| 197 | } |
| 198 | |
| 199 | public boolean checkCredentials (String username, String password) { |
| 200 | return username.equals("tester") && password.equals("passwd"); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | class CookieFilter extends Filter |
| 205 | { |
| 206 | public void doFilter(HttpExchange t, Chain chain) throws IOException |
| 207 | { |
| 208 | Headers resHeaders = t.getResponseHeaders(); |
| 209 | Headers reqHeaders = t.getRequestHeaders(); |
| 210 | |
| 211 | if (!reqHeaders.containsKey("Authorization")) |
| 212 | resHeaders.set("Set-Cookie2", "Customer=\"WILE_E_COYOTE\"; path=\"/test/\""); |
| 213 | |
| 214 | chain.doFilter(t); |
| 215 | } |
| 216 | |
| 217 | public void destroy(HttpContext c) { } |
| 218 | |
| 219 | public void init(HttpContext c) { } |
| 220 | |
| 221 | public String description() { |
| 222 | return new String("Filter for setting a cookie for requests without an \"Authorization\" header."); |
| 223 | } |
| 224 | } |
| 225 | } |