jdk/test/sun/net/www/protocol/http/B6641309.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 74 068494063b1b
child 38557 5c485e1ea6fa
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 74
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
     4
 *
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
     7
 * published by the Free Software Foundation.
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
     8
 *
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    13
 * accompanied this code).
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    14
 *
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 74
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 74
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 74
diff changeset
    21
 * questions.
74
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    22
 */
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    23
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    24
/*
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    25
 * @test
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    26
 * @bug 6641309
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    27
 * @summary Wrong Cookie separator used in HttpURLConnection
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    28
 */
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    29
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    30
import java.net.*;
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    31
import java.util.*;
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    32
import java.io.*;
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    33
import com.sun.net.httpserver.*;
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    34
import java.util.concurrent.Executors;
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    35
import java.util.concurrent.ExecutorService;
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    36
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    37
public class B6641309
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    38
{
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    39
    com.sun.net.httpserver.HttpServer httpServer;
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    40
    ExecutorService executorService;
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    41
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    42
    public static void main(String[] args)
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    43
    {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    44
        new B6641309();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    45
    }
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    46
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    47
    public B6641309()
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    48
    {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    49
        try {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    50
            startHttpServer();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    51
            doClient();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    52
        } catch (IOException ioe) {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    53
            System.err.println(ioe);
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    54
        }
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    55
    }
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    56
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    57
    void doClient() {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    58
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    59
        try {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    60
            InetSocketAddress address = httpServer.getAddress();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    61
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    62
            // GET Request
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    63
            URL url = new URL("http://localhost:" + address.getPort() + "/test/");
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    64
            CookieHandler ch = CookieHandler.getDefault();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    65
            Map<String,List<String>> header = new HashMap<String,List<String>>();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    66
            List<String> values = new LinkedList<String>();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    67
            values.add("Test1Cookie=TEST1; path=/test/");
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    68
            values.add("Test2Cookie=TEST2; path=/test/");
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    69
            header.put("Set-Cookie", values);
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    70
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    71
            // preload the CookieHandler with a cookie for our URL
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    72
            // so that it will be sent during the first request
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    73
            ch.put(url.toURI(), header);
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    74
            HttpURLConnection uc = (HttpURLConnection)url.openConnection();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    75
            int resp = uc.getResponseCode();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    76
            if (resp != 200)
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    77
                throw new RuntimeException("Failed: Response code from GET is not 200");
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    78
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    79
            System.out.println("Response code from GET = 200 OK");
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    80
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    81
        } catch (IOException e) {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    82
            e.printStackTrace();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    83
        } catch (URISyntaxException e) {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    84
            e.printStackTrace();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    85
        } finally {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    86
            httpServer.stop(1);
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    87
            executorService.shutdown();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    88
        }
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    89
    }
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    90
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    91
    /**
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    92
     * Http Server
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    93
     */
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    94
    public void startHttpServer() throws IOException {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    95
        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    96
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    97
        // create HttpServer context
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    98
        HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
    99
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   100
        executorService = Executors.newCachedThreadPool();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   101
        httpServer.setExecutor(executorService);
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   102
        httpServer.start();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   103
    }
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   104
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   105
    class MyHandler implements HttpHandler {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   106
        public void handle(HttpExchange t) throws IOException {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   107
            InputStream is = t.getRequestBody();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   108
            Headers reqHeaders = t.getRequestHeaders();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   109
            int i = 0;
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   110
            // Read till end of stream
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   111
            do {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   112
                i = is.read();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   113
            } while (i != -1);
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   114
            is.close();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   115
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   116
            List<String> cookies = reqHeaders.get("Cookie");
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   117
            if (cookies != null) {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   118
                for (String str : cookies) {
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   119
                    // The separator between the 2 cookies should be
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   120
                    // a semi-colon AND a space
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   121
                    if (str.equals("Test1Cookie=TEST1; Test2Cookie=TEST2"))
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   122
                        t.sendResponseHeaders(200, -1);
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   123
                }
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   124
            }
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   125
            t.sendResponseHeaders(400, -1);
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   126
            t.close();
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   127
        }
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   128
    }
068494063b1b 6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
diff changeset
   129
}