Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/B6641309.java
38867 views
/*1* Copyright (c) 2008, 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* @summary Wrong Cookie separator used in HttpURLConnection27*/2829import java.net.*;30import java.util.*;31import java.io.*;32import com.sun.net.httpserver.*;33import java.util.concurrent.Executors;34import java.util.concurrent.ExecutorService;3536public class B664130937{38com.sun.net.httpserver.HttpServer httpServer;39ExecutorService executorService;4041public static void main(String[] args)42{43new B6641309();44}4546public B6641309()47{48try {49startHttpServer();50doClient();51} catch (IOException ioe) {52System.err.println(ioe);53}54}5556void doClient() {57CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));58try {59InetSocketAddress address = httpServer.getAddress();6061// GET Request62URL url = new URL("http://localhost:" + address.getPort() + "/test/");63CookieHandler ch = CookieHandler.getDefault();64Map<String,List<String>> header = new HashMap<String,List<String>>();65List<String> values = new LinkedList<String>();66values.add("Test1Cookie=TEST1; path=/test/");67values.add("Test2Cookie=TEST2; path=/test/");68header.put("Set-Cookie", values);6970// preload the CookieHandler with a cookie for our URL71// so that it will be sent during the first request72ch.put(url.toURI(), header);73HttpURLConnection uc = (HttpURLConnection)url.openConnection();74int resp = uc.getResponseCode();75if (resp != 200)76throw new RuntimeException("Failed: Response code from GET is not 200");7778System.out.println("Response code from GET = 200 OK");7980} catch (IOException e) {81e.printStackTrace();82} catch (URISyntaxException e) {83e.printStackTrace();84} finally {85httpServer.stop(1);86executorService.shutdown();87}88}8990/**91* Http Server92*/93public void startHttpServer() throws IOException {94httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);9596// create HttpServer context97HttpContext ctx = httpServer.createContext("/test/", new MyHandler());9899executorService = Executors.newCachedThreadPool();100httpServer.setExecutor(executorService);101httpServer.start();102}103104class MyHandler implements HttpHandler {105public void handle(HttpExchange t) throws IOException {106InputStream is = t.getRequestBody();107Headers reqHeaders = t.getRequestHeaders();108int i = 0;109// Read till end of stream110do {111i = is.read();112} while (i != -1);113is.close();114115List<String> cookies = reqHeaders.get("Cookie");116if (cookies != null) {117for (String str : cookies) {118// The separator between the 2 cookies should be119// a semi-colon AND a space120if (str.equals("Test1Cookie=TEST1; Test2Cookie=TEST2"))121t.sendResponseHeaders(200, -1);122}123}124t.sendResponseHeaders(400, -1);125t.close();126}127}128}129130131