Path: blob/master/test/jdk/sun/net/www/protocol/http/B6641309.java
41159 views
/*1* Copyright (c) 2008, 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 664130926* @modules jdk.httpserver27* @library /test/lib28* @run main/othervm B664130929* @run main/othervm -Djava.net.preferIPv6Addresses=true B664130930* @summary Wrong Cookie separator used in HttpURLConnection B664130931*/3233import java.net.*;34import java.util.*;35import java.io.*;36import com.sun.net.httpserver.*;37import java.util.concurrent.Executors;38import java.util.concurrent.ExecutorService;3940import jdk.test.lib.net.URIBuilder;4142public class B664130943{44com.sun.net.httpserver.HttpServer httpServer;45ExecutorService executorService;4647public static void main(String[] args) throws Exception {48new B6641309();49}5051public B6641309() throws Exception {52startHttpServer();53doClient();54}5556void doClient() throws Exception {57CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));58ProxySelector.setDefault(ProxySelector.of(null));5960InetSocketAddress address = httpServer.getAddress();6162// GET Request63URL url = URIBuilder.newBuilder()64.scheme("http")65.host(address.getAddress())66.port(address.getPort())67.path("/test/")68.toURL();6970CookieHandler ch = CookieHandler.getDefault();71Map<String,List<String>> header = new HashMap<String,List<String>>();72List<String> values = new LinkedList<String>();73values.add("Test1Cookie=TEST1; path=/test/");74values.add("Test2Cookie=TEST2; path=/test/");75header.put("Set-Cookie", values);7677// preload the CookieHandler with a cookie for our URL78// so that it will be sent during the first request79ch.put(url.toURI(), header);80HttpURLConnection uc = (HttpURLConnection)url.openConnection();81int resp = uc.getResponseCode();82if (resp != 200) {83throw new RuntimeException("Failed: Response code from GET is not 200: "84+ resp);85}86System.out.println("Response code from GET = 200 OK");8788httpServer.stop(1);89executorService.shutdown();90}9192/**93* Http Server94*/95public void startHttpServer() throws IOException {96InetAddress loopback = InetAddress.getLoopbackAddress();97InetSocketAddress address = new InetSocketAddress(loopback, 0);98httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);99100// create HttpServer context101HttpContext ctx = httpServer.createContext("/test/", new MyHandler());102103executorService = Executors.newCachedThreadPool();104httpServer.setExecutor(executorService);105httpServer.start();106}107108class MyHandler implements HttpHandler {109public void handle(HttpExchange t) throws IOException {110InputStream is = t.getRequestBody();111Headers reqHeaders = t.getRequestHeaders();112int i = 0;113// Read till end of stream114do {115i = is.read();116} while (i != -1);117is.close();118119List<String> cookies = reqHeaders.get("Cookie");120if (cookies != null) {121for (String str : cookies) {122// The separator between the 2 cookies should be123// a semi-colon AND a space124if (str.equals("Test1Cookie=TEST1; Test2Cookie=TEST2"))125t.sendResponseHeaders(200, -1);126}127}128t.sendResponseHeaders(400, -1);129t.close();130}131}132}133134135