Path: blob/master/test/jdk/sun/net/www/protocol/http/UserCookie.java
41159 views
/*1* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 643965126* @modules jdk.httpserver27* @run main/othervm UserAuth28* @summary Sending "Cookie" header with JRE 1.5.0_07 doesn't work anymore29*/3031import java.net.*;32import com.sun.net.httpserver.*;33import java.util.*;34import java.io.*;35import static java.net.Proxy.NO_PROXY;3637public class UserCookie38{39com.sun.net.httpserver.HttpServer httpServer;4041public static void main(String[] args) {42new UserCookie();43}4445public UserCookie() {46try {47startHttpServer();48doClient();49} catch (IOException ioe) {50ioe.printStackTrace();51}52}5354void doClient() {55try {56// set default CookieHandler to accept only accepts cookies from original server.57CookieHandler.setDefault(new CookieManager());5859InetSocketAddress address = httpServer.getAddress();6061URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");62HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);63uc.setRequestProperty("Cookie", "value=ValueDoesNotMatter");64int resp = uc.getResponseCode();6566System.out.println("Response Code is " + resp);67if (resp != 200)68throw new RuntimeException("Failed: Cookie header was not retained");6970} catch (IOException e) {71e.printStackTrace();72} finally {73httpServer.stop(1);74}75}7677/**78* Http Server79*/80void startHttpServer() throws IOException {81InetAddress address = InetAddress.getLocalHost();82if (!InetAddress.getByName(address.getHostName()).equals(address)) {83// if this happens then we should possibly change the client84// side to use the address literal in its URL instead of85// the host name.86throw new IOException(address.getHostName()87+ " resolves to "88+ InetAddress.getByName(address.getHostName())89+ " not to "90+ address + ": check host configuration.");91}9293httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(address, 0), 0);9495// create HttpServer context96HttpContext ctx = httpServer.createContext("/test/", new MyHandler());9798httpServer.start();99}100101class MyHandler implements HttpHandler {102public void handle(HttpExchange t) throws IOException {103Headers reqHeaders = t.getRequestHeaders();104105List<String> cookie = reqHeaders.get("Cookie");106107if (cookie == null || !cookie.get(0).equals("value=ValueDoesNotMatter"))108t.sendResponseHeaders(400, -1);109110t.sendResponseHeaders(200, -1);111t.close();112}113}114115116117}118119120