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