Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpClient/RequestURI.java
38867 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 646966326* @run main/othervm RequestURI27* @summary HTTP Request-URI contains fragment when connecting through proxy28*/2930import java.net.*;31import java.io.*;32import sun.net.www.MessageHeader;3334// Create a Server listening on port 5001 to act as the proxy. Requests35// never need to be forwared from it. We are only interested in the36// request being sent to it. Set the system proxy properties to the37// value of the RequestURIServer so that the HTTP request will to sent to it.3839public class RequestURI40{41public static void main(String[] args) {42ServerSocket ss;43int port;4445try {46ss = new ServerSocket(5001);47port = ss.getLocalPort();48} catch (Exception e) {49System.out.println ("Exception: " + e);50return;51}5253RequestURIServer server = new RequestURIServer(ss);54server.start();5556try {57System.getProperties().setProperty("http.proxyHost", "localhost");58System.getProperties().setProperty("http.proxyPort", Integer.toString(port));5960URL url = new URL("http://boo.bar.com/foo.html#section5");61HttpURLConnection uc = (HttpURLConnection) url.openConnection();6263int resp = uc.getResponseCode();64if (resp != 200)65throw new RuntimeException("Failed: Fragment is being passed as part of the RequestURI");6667ss.close();68} catch (IOException e) {69e.printStackTrace();70}71}72}7374class RequestURIServer extends Thread75{76ServerSocket ss;7778String replyOK = "HTTP/1.1 200 OK\r\n" +79"Content-Length: 0\r\n\r\n";80String replyFAILED = "HTTP/1.1 404 Not Found\r\n\r\n";8182public RequestURIServer(ServerSocket ss) {83this.ss = ss;84}8586public void run() {87try {88Socket sock = ss.accept();89InputStream is = sock.getInputStream();90OutputStream os = sock.getOutputStream();9192MessageHeader headers = new MessageHeader (is);93String requestLine = headers.getValue(0);9495int first = requestLine.indexOf(' ');96int second = requestLine.lastIndexOf(' ');97String URIString = requestLine.substring(first+1, second);9899URI requestURI = new URI(URIString);100if (requestURI.getFragment() != null)101os.write(replyFAILED.getBytes("UTF-8"));102else103os.write(replyOK.getBytes("UTF-8"));104105sock.close();106} catch (Exception e) {107e.printStackTrace();108}109}110111}112113114