1 /* 2 * Copyright (c) 2008, 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 6641309 27 * @modules jdk.httpserver 28 * @summary Wrong Cookie separator used in HttpURLConnection 29 */ 30 31 import java.net.*; 32 import java.util.*; 33 import java.io.*; 34 import com.sun.net.httpserver.*; 35 import java.util.concurrent.Executors; 36 import java.util.concurrent.ExecutorService; 37 38 public class B6641309 39 { 40 com.sun.net.httpserver.HttpServer httpServer; 41 ExecutorService executorService; 42 main(String[] args)43 public static void main(String[] args) 44 { 45 new B6641309(); 46 } 47 B6641309()48 public B6641309() 49 { 50 try { 51 startHttpServer(); 52 doClient(); 53 } catch (IOException ioe) { 54 System.err.println(ioe); 55 } 56 } 57 doClient()58 void doClient() { 59 CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); 60 try { 61 InetSocketAddress address = httpServer.getAddress(); 62 63 // GET Request 64 URL url = new URL("http://localhost:" + address.getPort() + "/test/"); 65 CookieHandler ch = CookieHandler.getDefault(); 66 Map<String,List<String>> header = new HashMap<String,List<String>>(); 67 List<String> values = new LinkedList<String>(); 68 values.add("Test1Cookie=TEST1; path=/test/"); 69 values.add("Test2Cookie=TEST2; path=/test/"); 70 header.put("Set-Cookie", values); 71 72 // preload the CookieHandler with a cookie for our URL 73 // so that it will be sent during the first request 74 ch.put(url.toURI(), header); 75 HttpURLConnection uc = (HttpURLConnection)url.openConnection(); 76 int resp = uc.getResponseCode(); 77 if (resp != 200) 78 throw new RuntimeException("Failed: Response code from GET is not 200"); 79 80 System.out.println("Response code from GET = 200 OK"); 81 82 } catch (IOException e) { 83 e.printStackTrace(); 84 } catch (URISyntaxException e) { 85 e.printStackTrace(); 86 } finally { 87 httpServer.stop(1); 88 executorService.shutdown(); 89 } 90 } 91 92 /** 93 * Http Server 94 */ startHttpServer()95 public void startHttpServer() throws IOException { 96 httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); 97 98 // create HttpServer context 99 HttpContext ctx = httpServer.createContext("/test/", new MyHandler()); 100 101 executorService = Executors.newCachedThreadPool(); 102 httpServer.setExecutor(executorService); 103 httpServer.start(); 104 } 105 106 class MyHandler implements HttpHandler { handle(HttpExchange t)107 public void handle(HttpExchange t) throws IOException { 108 InputStream is = t.getRequestBody(); 109 Headers reqHeaders = t.getRequestHeaders(); 110 int i = 0; 111 // Read till end of stream 112 do { 113 i = is.read(); 114 } while (i != -1); 115 is.close(); 116 117 List<String> cookies = reqHeaders.get("Cookie"); 118 if (cookies != null) { 119 for (String str : cookies) { 120 // The separator between the 2 cookies should be 121 // a semi-colon AND a space 122 if (str.equals("Test1Cookie=TEST1; Test2Cookie=TEST2")) 123 t.sendResponseHeaders(200, -1); 124 } 125 } 126 t.sendResponseHeaders(400, -1); 127 t.close(); 128 } 129 } 130 } 131