duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
ohair | bf91ea1 | 2011-04-06 22:06:11 -0700 | [diff] [blame] | 2 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4802209 |
| 27 | * @summary check that the right utf-8 encoder is used |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
| 31 | import java.net.*; |
| 32 | |
| 33 | public class Encode implements Runnable { |
| 34 | public static void main(String args[]) throws Exception { |
| 35 | new Encode(); |
| 36 | } |
| 37 | |
smarks | 40863b9 | 2011-03-01 15:05:32 -0800 | [diff] [blame] | 38 | final ServerSocket ss = new ServerSocket(0); |
| 39 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 40 | Encode() throws Exception { |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 41 | (new Thread(this)).start(); |
| 42 | String toEncode = "\uD800\uDC00 \uD801\uDC01 "; |
| 43 | String enc1 = URLEncoder.encode(toEncode, "UTF-8"); |
| 44 | byte bytes[] = {}; |
| 45 | ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
| 46 | InputStreamReader reader = new InputStreamReader( bais, "8859_1"); |
| 47 | String url = "http://localhost:" + Integer.toString(ss.getLocalPort()) + |
| 48 | "/missing.nothtml"; |
| 49 | HttpURLConnection uc = (HttpURLConnection)new URL(url).openConnection(); |
| 50 | uc.connect(); |
smarks | 40863b9 | 2011-03-01 15:05:32 -0800 | [diff] [blame] | 51 | try { |
| 52 | String enc2 = URLEncoder.encode(toEncode, "UTF-8"); |
| 53 | if (!enc1.equals(enc2)) { |
| 54 | System.out.println("test failed"); |
| 55 | throw new RuntimeException("test failed"); |
| 56 | } |
| 57 | } finally { |
| 58 | uc.disconnect(); |
| 59 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 60 | } |
| 61 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 62 | public void run() { |
smarks | 40863b9 | 2011-03-01 15:05:32 -0800 | [diff] [blame] | 63 | try (ServerSocket serv = ss; |
| 64 | Socket s = serv.accept(); |
| 65 | BufferedReader in = |
| 66 | new BufferedReader(new InputStreamReader(s.getInputStream()))) |
| 67 | { |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 68 | String req = in.readLine(); |
smarks | 40863b9 | 2011-03-01 15:05:32 -0800 | [diff] [blame] | 69 | try (OutputStream os = s.getOutputStream(); |
| 70 | BufferedOutputStream bos = new BufferedOutputStream(os); |
| 71 | PrintStream out = new PrintStream(bos)) |
| 72 | { |
| 73 | out.print("HTTP/1.1 403 Forbidden\r\n"); |
| 74 | out.print("\r\n"); |
| 75 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 76 | } catch (Exception e) { |
| 77 | e.printStackTrace(); |
| 78 | } |
| 79 | } |
| 80 | } |