Path: blob/master/test/jdk/sun/net/www/http/HttpClient/RequestURI.java
41154 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* @summary HTTP Request-URI contains fragment when connecting through proxy27* @modules java.base/sun.net.www28* @run main/othervm RequestURI29*/3031import java.net.*;32import java.io.*;33import sun.net.www.MessageHeader;3435// Create a Server listening on port 5001 to act as the proxy. Requests36// never need to be forwared from it. We are only interested in the37// request being sent to it. Set the system proxy properties to the38// value of the RequestURIServer so that the HTTP request will to sent to it.3940public class RequestURI41{42public static void main(String[] args) {43ServerSocket ss;44int port;4546try {47ss = new ServerSocket(5001);48port = ss.getLocalPort();49} catch (Exception e) {50System.out.println ("Exception: " + e);51return;52}5354RequestURIServer server = new RequestURIServer(ss);55server.start();5657try {58System.getProperties().setProperty("http.proxyHost", "localhost");59System.getProperties().setProperty("http.proxyPort", Integer.toString(port));6061URL url = new URL("http://boo.bar.com/foo.html#section5");62HttpURLConnection uc = (HttpURLConnection) url.openConnection();6364int resp = uc.getResponseCode();65if (resp != 200)66throw new RuntimeException("Failed: Fragment is being passed as part of the RequestURI");6768ss.close();69} catch (IOException e) {70e.printStackTrace();71}72}73}7475class RequestURIServer extends Thread76{77ServerSocket ss;7879String replyOK = "HTTP/1.1 200 OK\r\n" +80"Content-Length: 0\r\n\r\n";81String replyFAILED = "HTTP/1.1 404 Not Found\r\n\r\n";8283public RequestURIServer(ServerSocket ss) {84this.ss = ss;85}8687public void run() {88try {89Socket sock = ss.accept();90InputStream is = sock.getInputStream();91OutputStream os = sock.getOutputStream();9293MessageHeader headers = new MessageHeader (is);94String requestLine = headers.getValue(0);9596int first = requestLine.indexOf(' ');97int second = requestLine.lastIndexOf(' ');98String URIString = requestLine.substring(first+1, second);99100URI requestURI = new URI(URIString);101if (requestURI.getFragment() != null)102os.write(replyFAILED.getBytes("UTF-8"));103else104os.write(replyOK.getBytes("UTF-8"));105106sock.close();107} catch (Exception e) {108e.printStackTrace();109}110}111112}113114115