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