J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 6439651 |
| 27 | * @run main/othervm UserAuth |
| 28 | * @summary Sending "Cookie" header with JRE 1.5.0_07 doesn't work anymore |
| 29 | */ |
| 30 | |
| 31 | import java.net.*; |
| 32 | import com.sun.net.httpserver.*; |
| 33 | import java.util.*; |
| 34 | import java.io.*; |
| 35 | |
| 36 | public class UserCookie |
| 37 | { |
| 38 | com.sun.net.httpserver.HttpServer httpServer; |
| 39 | |
| 40 | public static void main(String[] args) { |
| 41 | new UserCookie(); |
| 42 | } |
| 43 | |
| 44 | public UserCookie() { |
| 45 | try { |
| 46 | startHttpServer(); |
| 47 | doClient(); |
| 48 | } catch (IOException ioe) { |
| 49 | ioe.printStackTrace(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void doClient() { |
| 54 | try { |
| 55 | // set default CookieHandler to accept only accepts cookies from original server. |
| 56 | CookieHandler.setDefault(new CookieManager()); |
| 57 | |
| 58 | InetSocketAddress address = httpServer.getAddress(); |
| 59 | |
| 60 | URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/"); |
| 61 | HttpURLConnection uc = (HttpURLConnection)url.openConnection(); |
| 62 | uc.setRequestProperty("Cookie", "value=ValueDoesNotMatter"); |
| 63 | int resp = uc.getResponseCode(); |
| 64 | |
| 65 | System.out.println("Response Code is " + resp); |
| 66 | if (resp != 200) |
| 67 | throw new RuntimeException("Failed: Cookie header was not retained"); |
| 68 | |
| 69 | } catch (IOException e) { |
| 70 | e.printStackTrace(); |
| 71 | } finally { |
| 72 | httpServer.stop(1); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Http Server |
| 78 | */ |
| 79 | void startHttpServer() throws IOException { |
| 80 | httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); |
| 81 | |
| 82 | // create HttpServer context |
| 83 | HttpContext ctx = httpServer.createContext("/test/", new MyHandler()); |
| 84 | |
| 85 | httpServer.start(); |
| 86 | } |
| 87 | |
| 88 | class MyHandler implements HttpHandler { |
| 89 | public void handle(HttpExchange t) throws IOException { |
| 90 | Headers reqHeaders = t.getRequestHeaders(); |
| 91 | |
| 92 | List<String> cookie = reqHeaders.get("Cookie"); |
| 93 | |
| 94 | if (cookie == null || !cookie.get(0).equals("value=ValueDoesNotMatter")) |
| 95 | t.sendResponseHeaders(400, -1); |
| 96 | |
| 97 | t.sendResponseHeaders(200, -1); |
| 98 | t.close(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | } |