51N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
51N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
51N/A *
51N/A * This code is free software; you can redistribute it and/or modify it
51N/A * under the terms of the GNU General Public License version 2 only, as
51N/A * published by the Free Software Foundation.
51N/A *
51N/A * This code is distributed in the hope that it will be useful, but WITHOUT
51N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
51N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
51N/A * version 2 for more details (a copy is included in the LICENSE file that
51N/A * accompanied this code).
51N/A *
51N/A * You should have received a copy of the GNU General Public License version
51N/A * 2 along with this work; if not, write to the Free Software Foundation,
51N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
51N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
51N/A */
51N/A
51N/A/*
51N/A * @test
51N/A * @bug 6641309
51N/A * @summary Wrong Cookie separator used in HttpURLConnection
51N/A */
51N/A
51N/Aimport java.net.*;
51N/Aimport java.util.*;
51N/Aimport java.io.*;
51N/Aimport com.sun.net.httpserver.*;
51N/Aimport java.util.concurrent.Executors;
51N/Aimport java.util.concurrent.ExecutorService;
51N/A
51N/Apublic class B6641309
51N/A{
51N/A com.sun.net.httpserver.HttpServer httpServer;
51N/A ExecutorService executorService;
51N/A
51N/A public static void main(String[] args)
51N/A {
51N/A new B6641309();
51N/A }
51N/A
51N/A public B6641309()
51N/A {
51N/A try {
51N/A startHttpServer();
51N/A doClient();
51N/A } catch (IOException ioe) {
51N/A System.err.println(ioe);
51N/A }
51N/A }
51N/A
51N/A void doClient() {
51N/A CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
51N/A try {
51N/A InetSocketAddress address = httpServer.getAddress();
51N/A
51N/A // GET Request
51N/A URL url = new URL("http://localhost:" + address.getPort() + "/test/");
51N/A CookieHandler ch = CookieHandler.getDefault();
51N/A Map<String,List<String>> header = new HashMap<String,List<String>>();
51N/A List<String> values = new LinkedList<String>();
51N/A values.add("Test1Cookie=TEST1; path=/test/");
51N/A values.add("Test2Cookie=TEST2; path=/test/");
51N/A header.put("Set-Cookie", values);
51N/A
51N/A // preload the CookieHandler with a cookie for our URL
51N/A // so that it will be sent during the first request
51N/A ch.put(url.toURI(), header);
51N/A HttpURLConnection uc = (HttpURLConnection)url.openConnection();
51N/A int resp = uc.getResponseCode();
51N/A if (resp != 200)
51N/A throw new RuntimeException("Failed: Response code from GET is not 200");
51N/A
51N/A System.out.println("Response code from GET = 200 OK");
51N/A
51N/A } catch (IOException e) {
51N/A e.printStackTrace();
51N/A } catch (URISyntaxException e) {
51N/A e.printStackTrace();
51N/A } finally {
51N/A httpServer.stop(1);
51N/A executorService.shutdown();
51N/A }
51N/A }
51N/A
51N/A /**
51N/A * Http Server
51N/A */
51N/A public void startHttpServer() throws IOException {
51N/A httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
51N/A
51N/A // create HttpServer context
51N/A HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
51N/A
51N/A executorService = Executors.newCachedThreadPool();
51N/A httpServer.setExecutor(executorService);
51N/A httpServer.start();
51N/A }
51N/A
51N/A class MyHandler implements HttpHandler {
51N/A public void handle(HttpExchange t) throws IOException {
51N/A InputStream is = t.getRequestBody();
51N/A Headers reqHeaders = t.getRequestHeaders();
51N/A int i = 0;
51N/A // Read till end of stream
51N/A do {
51N/A i = is.read();
51N/A } while (i != -1);
51N/A is.close();
51N/A
51N/A List<String> cookies = reqHeaders.get("Cookie");
51N/A if (cookies != null) {
51N/A for (String str : cookies) {
51N/A // The separator between the 2 cookies should be
51N/A // a semi-colon AND a space
51N/A if (str.equals("Test1Cookie=TEST1; Test2Cookie=TEST2"))
51N/A t.sendResponseHeaders(200, -1);
51N/A }
51N/A }
51N/A t.sendResponseHeaders(400, -1);
51N/A t.close();
51N/A }
51N/A }
51N/A}