J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6469663 |
| 27 | * @run main/othervm RequestURI |
| 28 | * @summary HTTP Request-URI contains fragment when connecting through proxy |
| 29 | */ |
| 30 | |
| 31 | import java.net.*; |
| 32 | import java.io.*; |
| 33 | import sun.net.www.MessageHeader; |
| 34 | |
| 35 | // Create a Server listening on port 5001 to act as the proxy. Requests |
| 36 | // never need to be forwared from it. We are only interested in the |
| 37 | // request being sent to it. Set the system proxy properties to the |
| 38 | // value of the RequestURIServer so that the HTTP request will to sent to it. |
| 39 | |
| 40 | public class RequestURI |
| 41 | { |
| 42 | public static void main(String[] args) { |
| 43 | ServerSocket ss; |
| 44 | int port; |
| 45 | |
| 46 | try { |
| 47 | ss = new ServerSocket(5001); |
| 48 | port = ss.getLocalPort(); |
| 49 | } catch (Exception e) { |
| 50 | System.out.println ("Exception: " + e); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | RequestURIServer server = new RequestURIServer(ss); |
| 55 | server.start(); |
| 56 | |
| 57 | try { |
| 58 | System.getProperties().setProperty("http.proxyHost", "localhost"); |
| 59 | System.getProperties().setProperty("http.proxyPort", Integer.toString(port)); |
| 60 | |
| 61 | URL url = new URL("http://boo.bar.com/foo.html#section5"); |
| 62 | HttpURLConnection uc = (HttpURLConnection) url.openConnection(); |
| 63 | |
| 64 | int resp = uc.getResponseCode(); |
| 65 | if (resp != 200) |
| 66 | throw new RuntimeException("Failed: Fragment is being passed as part of the RequestURI"); |
| 67 | |
| 68 | ss.close(); |
| 69 | } catch (IOException e) { |
| 70 | e.printStackTrace(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | class RequestURIServer extends Thread |
| 76 | { |
| 77 | ServerSocket ss; |
| 78 | |
| 79 | String replyOK = "HTTP/1.1 200 OK\r\n" + |
| 80 | "Content-Length: 0\r\n\r\n"; |
| 81 | String replyFAILED = "HTTP/1.1 404 Not Found\r\n\r\n"; |
| 82 | |
| 83 | public RequestURIServer(ServerSocket ss) { |
| 84 | this.ss = ss; |
| 85 | } |
| 86 | |
| 87 | public void run() { |
| 88 | try { |
| 89 | Socket sock = ss.accept(); |
| 90 | InputStream is = sock.getInputStream(); |
| 91 | OutputStream os = sock.getOutputStream(); |
| 92 | |
| 93 | MessageHeader headers = new MessageHeader (is); |
| 94 | String requestLine = headers.getValue(0); |
| 95 | |
| 96 | int first = requestLine.indexOf(' '); |
| 97 | int second = requestLine.lastIndexOf(' '); |
| 98 | String URIString = requestLine.substring(first+1, second); |
| 99 | |
| 100 | URI requestURI = new URI(URIString); |
| 101 | if (requestURI.getFragment() != null) |
| 102 | os.write(replyFAILED.getBytes("UTF-8")); |
| 103 | else |
| 104 | os.write(replyOK.getBytes("UTF-8")); |
| 105 | |
| 106 | sock.close(); |
| 107 | } catch (Exception e) { |
| 108 | e.printStackTrace(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } |